Arduino ROS - arieldo/MorBot GitHub Wiki
Enriching Your Robot's Interaction: A ROS and Arduino Integration
In the realm of robotics, the synergy between software frameworks like ROS and hardware platforms such as Arduino offers boundless possibilities. From simple sensors to complex modules, this integration unlocks a plethora of capabilities. By bridging the gap between high-level computational power and low-level hardware interfacing, we can achieve more nuanced robot behaviors and adaptability. This tutorial delves into one such integration—combining the power of ROS with the simplicity of Arduino to enhance a Jetbot's perception using a Sharp IR sensor. This approach can be expanded to include other sensors and devices, enabling a richer interaction between your robot and its environment. Let's embark on this journey of upgrading our robot's sensory world.
In this tutorial, we will explore how to integrate sensors connected to an Arduino with the Jetbot through ROS. Specifically, we will use an Arduino Nano and a Sharp IR distance sensor.
- JetBot environment set up Jetson Nano and Linux Setup
- ROS environment set up on your jetson , IF not Check out HowTo ROS
- Basic knowledge of ROS concepts, such as nodes, topics, and messages ,IF not Check out ROS 101
- Basic knowledge of Publisher and Subscriber in Python for ROS ,IF not Check out ROS PubSubPy
- Basic knowledge of Creating new Packages in ROS
- Basic knowledge of Python programming language
- Arduino Nano (or any other compatible board).
- Sharp IR distance sensor (or any other compatible sensor).
- Basic knowledge of Arduino and ROS.
- Connect the Sharp IR sensor to the analog A0 pin on your Arduino Nano.
- Upload the following code to your Arduino using the Arduino IDE:
#include <SharpIR.h>
#define ir A0
#define model 20150
SharpIR SharpIR(ir, model);
void setup() {
Serial.begin(115200);
}
void loop() {
int sensorValue = analogRead(ir);
float voltage = sensorValue * (5.0 / 1023.0);
float distance = 27.728 * pow(voltage, -1.2045);
Serial.println(distance);
delay(10);
}
- Connect the Arduino to your Jetson Nano using a USB cable.
- First Installe pyserial library:
pip3 install pyserial
- Create a new package named
ir_sensor
inside your workspace directory (~/MorBot/morbot_ros/morbot_ws/src
):
cd ~/MorBot/morbot_ros/morbot_ws/src
catkin_create_pkg ir_sensor rospy std_msgs
- Create a new directory named
scripts
inside your package directory (~/MorBot/morbot_ros/morbot_ws/src/ir_sensor
):
cd ~/MorBot/morbot_ros/morbot_ws/src/ir_sensor
mkdir scripts
- Create a new source file inside the
scripts
directory. For example, create a Python file namedir_sensor_publisher.py
:
cd scripts
touch ir_sensor_publisher.py
Make the script executable:
cd ~/MorBot/morbot_ros/morbot_ws/src/ir_sensor/scripts
chmod +x ir_sensor_publisher.py
- The
ir_sensor_publisher.py
This script reads the distance from the Sharp IR sensor and publishes it to the /ir_sensor
ROS topic.
#!/usr/bin/env python3
import serial
import rospy
from std_msgs.msg import Float32
# Set up the serial line
ser = serial.Serial('/dev/ttyUSB0', 115200)
ser.flushInput()
# Set up ROS
rospy.init_node('sharp_ir')
pub = rospy.Publisher('sharp_ir_range', Float32, queue_size=10)
rate = rospy.Rate(100) # 100Hz
try:
while not rospy.is_shutdown():
if ser.in_waiting > 0:
try:
distance = float(ser.readline().strip())
msg = Float32()
msg.data = distance
pub.publish(msg)
except ValueError:
pass # if the float conversion fails, just ignore it
rate.sleep()
except rospy.ROSInterruptException:
pass
finally:
ser.close()
- Build the package:
cd ~/MorBot/morbot_ros/morbot_ws
catkin_make
source devel/setup.bash
- Run the node:
- Open new terminal windows and run :
roscore
- Open new terminal windows and run :
sudo chmod 777 /dev/ttyUSB0
rosrun ir_sensor ir_sensor_publisher.py
- Open new terminal windows and run :
rostopic echo /sharp_ir_range
- Create a new directory named
launch
inside your package directory (~/MorBot/morbot_ros/morbot_ws/src/ir_sensor
):
cd ~/MorBot/morbot_ros/morbot_ws/src/ir_sensor
mkdir launch
- Create a new launch file inside the
launch
directory. For example, create a launch file namedir_sensor.launch
:
cd launch
touch ir_sensor.launch
- Edit the launch file to include the following:
<launch>
<node name="ir_sensor_publisher" pkg="ir_sensor" type="ir_sensor_publisher.py" output="screen" />
</launch>
- Run the launch file:
roslaunch ir_sensor ir_sensor.launch
- Check the published distance:
rostopic echo /sharp_ir_range
Now, your Arduino Nano with the Sharp IR sensor
should be communicating with the Jetbot through ROS. The sensor's readings are published on the sharp_ir_range
topic.
In this tutorial, we covered the integration of a Sharp IR sensor connected to an Arduino Nano with a Jetbot using ROS. This approach can be expanded to include other sensors and devices, enabling a richer interaction between your robot and its environment.