How it works…

After setting up the Arduino, we will go to File | Preferences for configuring the sketchbook folder of Arduino (for example, /home/<user>/Arduino).

You may have noticed a folder called libraries inside the Arduino folder. There, we have to generate ros_lib using a script called make_libraries.py, which is present inside the rosserial_arduino package. The following commands show how to generate ros_lib for Arduino:

ros_lib is the rosserial_client for Arduino, which provides the ROS client APIs inside an Arduino IDE environment.
$ cd ~/Arduino/libraries/
$ rosrun rosserial_arduino make_libraries.py 

The make_libraries.py script will generate a wrapper of the ROS topic and service messages, which are optimized for Arduino data types, and generate a folder called ros_lib inside the libraries folder. If we restart the Arduino IDE, we will see that ros_lib appears in the File | Examples tab.

We can take an example and make sure that it is building properly to ensure that the ros_lib APIs are working fine. Next, we will discuss the basic program structure of the ROS Arduino node and the necessary APIs:

#include <ros.h>
  
ros::NodeHandle node;
  
void setup() {
  node.initNode();
}  
  
void loop() {
  node.spinOnce();
} 

The preceding code snippet shows the program structure of the Arduino node, where Nodehandle should be declared before the setup() function, which will give a global scope to the NodeHandle instance called node. The initialization of this node should be done inside the setup() function. Note that the Arduino setup() function will only execute once when the device starts. It is important to know that we can only create one node for a serial device.

Moreover, inside the loop() function, we have to use spinOnce() to invoke the ROS callback:

ros::Subscriber<std_msgs::String> listner("listener", callback);
ros::Publisher talker("talker", &amp;str_msg);

After defining the publisher and the subscriber, we have to initiate this inside the setup() function as follows:

node.advertise(chatter);
node.subscribe(sub);

We can find more reference code from ros_lib example programs.

Now, we will discuss our first example using the Arduino and ROS interfaces as listener and talker interfaces. Users can send a String message to the talker topic and Arduino will publish the same message in a listener topic.

The following source code implements a ROS node for Arduino:

#include <ros.h>
#include <std_msgs/String.h>
  
//Creating Nodehandle
ros::NodeHandle  node;
   
std_msgs::String str_msg;
  
//Defining Publisher and callback 
ros::Publisher talker("talker", &amp;str_msg);
void callback ( const std_msgs::String&amp; msg){
  
 str_msg.data = msg.data;
 talker.publish( &amp;str_msg );
      
}
  
//Defining Subscriber
ros::Subscriber<std_msgs::String> listener("listener", callback);
  
  
void setup()
{
  //Initializing node
  node.initNode();
   
  //Configure Subscriber and Publisher
  node.advertise(talker);
  node.subscribe(listener);
}
  
void loop()
{
  node.spinOnce();
  delay(2);
} 

We can compile the preceding code and upload it to the Arduino board. After uploading the code, we will select the desired Arduino board that we are using for this example and the device serial port of the Arduino IDE. More details can be found at https://www.arduino.cc/en/Guide/HomePage. (Go to Tools | Boards to select the board and Tools | Port to select the device port name of the board.)

After compiling and uploading the code to the Arduino board, we will have to start the ROS bridge nodes on the Linux system that connects Arduino and the Linux system.

We will create an arduino.launch file inside the rosserial_python packages with the following contents:

<launch>
<node pkg = "rosserial_python" type="serial_node.py" name="serial_node" output="screen">
        <param name="~port" value="/dev/ttyACM0" />
        <param name="~baud" value="57600" />
</node>
</launch>

Before launching and getting the configuration parameter for arduino.launch, we have to ensure that Arduino is already connected to the Linux system.

We are using the rosserial_python node here as the ROS bridging node. We have to mention the device name port and baud-rate as a parameter. For the device name port, we have to search for the port name listing from the contents of the /dev directory. We are using the /dev/ttyACM0 port here. Note that root permissions are required to use this port. However, we can change the permissions using the following command in order to read and write data on the desired port:

$ sudo chmod 666 /dev/ttyACM0

Where 666 allows read and write permissions to all. The default baud-rate of this communication is 57,600. However, we can change the baud-rate according to our application. The usage of serial_node.py inside the rosserial_python package is given at http://wiki.ros.org/rosserial_python.

Now, we can launch arduino.launch and start the communication between the Linux system and the Arduino board:

$ roslaunch rosserial_python arduino.launch

When serial_node.py starts running from the Linux system, it will send some serial data packets called query packets to get the number of topics, the topic names, and the types of topics that are received from the Arduino node.

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

The buffer size allocated for publishing and subscribing is 512 bytes by default. However, the buffer allocation is dependent on the amount of RAM available on the particular microcontroller that we are working with. We can override these settings by changing the BUFFER_SIZE macro inside ros.h.

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

$ rostopic list

 You may have noticed that topics such as /listener and /talker are being generated. We can simply publish a message to the /listener topic by using the following command:

$ rostopic pub -r 5 listener std_msgs/String "Hello Arduino"

We can echo the /talker topic, and we will get the same message as we have published:

$ rostopic echo /talker

Great! This shows that we have established the communication channel between the Linux system and the Arduino board. In the next section, we will look into some more applications that use the Arduino-ROS interface.

..................Content has been hidden....................

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