ROS - Arduino Publisher and Subscriber example

The first example using Arduino and ROS interface is a chatter and talker interface. Users can send a String message to the talker topic and Arduino will publish the same message in a chatter topic. The following ROS node is implemented for Arduino and we will discuss this example in detail:

#include <ros.h> 
#include <std_msgs/String.h> 
 
//Creating Nodehandle 
ros::NodeHandle  nh; 
 
//Declaring String variable 
std_msgs::String str_msg; 
 
//Defining Publisher 
ros::Publisher chatter("chatter", &str_msg); 
//Defining callback 
void callback ( const std_msgs::String& msg){ 
 
  str_msg.data = msg.data; 
  chatter.publish( &str_msg ); 
     
} 
 
//Defining Subscriber 
ros::Subscriber<std_msgs::String> sub("talker", callback); 
 
 
void setup() 
{ 
  //Initializing node 
  nh.initNode(); 
  //Start advertising and subscribing  
  nh.advertise(chatter); 
  nh.subscribe(sub); 
} 
 
void loop() 
{ 
  nh.spinOnce(); 
  delay(3); 
} 

We can compile the above code and upload to the Arduino board. After uploading the code, select the desired Arduino board that we are using for this example and the device serial port of the Arduino IDE.

Take Tools | Boards to select the board and Tools | Port to select the device port name of the board. We are using Arduino Mega for these examples.

After compiling and uploading the code, we can start the ROS bridge nodes in the PC which connects Arduino and the PC using the following command. Ensure that Arduino is already connected to the PC before executing of this command.

    $ rosrun rosserial_python serial_node.py /dev/ttyACM0  

We are using the rosserial_python node here as the ROS bridging node. We have to mention the device name and baud-rate as arguments. The default baud-rate of this communication is 57600. We can change the baud-rate according to our application and the usage of serial_node.py inside the rosserial_python package is given at http://wiki.ros.org/rosserial_python. If the communication between the ROS node and the Arduino node is correct, we will get the following message:

Figure 6 : Running the rosserial_python node

When serial_node.py starts running from the PC, it will send some serial data packets called query packets to get the number of topics, the topic names, and the types of topics which are received from the Arduino node. We have already seen the structure of serial packets which is being used for Arduino ROS communication. Given next is the structure of a query packet which is sent from serial_node.py to Arduino:

Figure 7 : Structure of Query Packet

The query topic contains fields such as Sync Flag, ROS version, length of the message, MD5 sum, Topic ID, and so on. When the query packet receives on the Arduino, it will reply with a topic info message which contains topic name, type, length, topic data, and so on. Following is a typical response packet from Arduino:

Figure 8 : Structure of Response Packet

If there is no response for the query packet, it will send it again. The synchronization in communication is based on ROS time.

From Figure 6, we can see that when we run the serial_node.py, the buffer size allocated for publish and subscribe is 512 bytes. The buffer allocation is dependent on the amount of RAM available on each microcontroller that we are working with. Following is a table showing the buffer allocation of each Arduino controller. We can override these settings by changing the BUFFER_SIZE macro inside ros.h.

AVR Model

Buffer Size

Publishers/Subscribers

ATMEGA 168

150 bytes

6/6

ATMEGA 328P

280 bytes

25/25

All others

512 bytes

25/25

 

There are also some limitations in the float64 data type of ROS in Arduino, it will truncate to 32-bit. Also, when we use string data types, use the unsigned char pointer for saving memory.

After running serial_node.py, we will get the list of topics using the
following command:

    $ rostopic list

We can see that topics such as chatter and talker are being generated. We can simply publish a message to the talker topic using the following command:

    $ rostopic pub -r 5 talker std_msgs/String "Hello World"

It will publish the "Hello World" message with a rate of 5.

We can echo the chatter topic and we will get the same message as we published:

    $rostopic echo /chatter  

The screenshot of this command is shown next:

Figure 9 : Echoing /chatter topic
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.122.162