Starting with ROS topics and MATLAB callback functions

In this section, we will discuss how to publish and subscribe ROS messages using MATLAB scripts. The first script that we analyze defines a typical template to develop the control loop of our robot. Firstly, we will subscribe to an input topic, and, successively, we will republish its value on an output topic for a certain amount of time. The complete source code is contained in the talker.m, in the code provided with the book, or  you can clone the following Git repository:

$ git clone https://github.com/jocacace/ros_matlab_test

Let's see the content of the talker.m script:

ros_master_ip = 'http://192.168.1.5:11311'; 
matlab_ip = '192.168.1.13'; 
rosinit(ros_master_ip, 'NodeHost', matlab_ip); 
pause(2) % wait a bit the roscore initialization 
 
talker_sub = rossubscriber( '/talker' ); 
[chatter_pub, chatter_msg] = rospublisher('/chatter','std_msgs/String'); 
r = rosrate(2); % 2 Hz loop rate  
 
for i = 1:20 
    data = talker_sub.LatestMessage; 
    chatter_msg.Data = data.Data; 
    send(chatter_pub, chatter_msg); 
    waitfor(r); 
end 
rosshutdown 

Let's see how the script works:

ros_master_ip = 'http://192.168.1.5:11311'; 
matlab_ip = '192.168.1.13'; 
rosinit(ros_master_ip, 'NodeHost', matlab_ip); 

In the preceding code, we initialize the MATLAB-ROS node. In this example, we want to connect MATLAB to an external ROS network and make it able to both read and write data on topics. For this reason, we should export both ROS_MASTER_URI and ROS_HOSTNAME environmental variables. Change the IP addresses on the base of your system configuration:

talker_sub = rossubscriber('/talker'); 
[chatter_pub, chatter_msg] = rospublisher('/chatter','std_msgs/String'); 

Then, we subscribe to the /talker topic while initializing the advertiser to the /chatter topic of the std_msgs/String type:

data = talker_sub.LatestMessage; 
send(chatter_pub, chatter_msg); 

Finally, we use the LatestMessage function to get the last message on the input topic, while publishing the message on the/chatter topic.

At this point, you can publish the desired message on the /talker topic, using the command line from one of the computers running Linux in the same network of the MATLAB computer, and visualize the message published on the /chatter topic. Before running the MATLAB script, be sure to have correctly exported the ROS_HOSTNAME variable on the computer where you want to publish the message in order to enable MATLAB to receive the published data.

Now, you can run the script by typing its name in the Command Window:

>> talker  

If everything has been correctly set, the output on the Linux machine should appear like in the following screenshot:

Figure 8: Communication between MATLAB and ROS.

The previous script defines a typical template to implement the control loop of an autonomous robot. Instead of continuously asking for the last message received on the topics, we can define a callback function that is called every time that a new message is received. In this way, we could write more complex control loops to handle the robot behavior, asynchronously receiving multiple information from ROS topics. In the next example, we will start to connect ROS-MATLAB to Gazebo, simulating the Turtlebot robot and plotting the value of its laser sensor using MATLAB.

To run the Gazebo simulation, we will use the turtlebot_gazebo package:

$ roslaunch turtlebot_gazebo turtlebot_world.launch

After starting Gazebo, different topics are published, among which is/scan . In this example, we need the following MATLAB functions:

  • plot_laser.m: This initializes the ROS-MATLAB interface subscribing to the desired laser scanner topic and plots the laser data at a desired frame rate
  • get_laser.m: This receives and stores the value of the laser scanner data

Let's look at the code of the plot_laser script:

function plot_laser() 
    global laser_msg; 
    ros_master_ip = 'http://192.168.1.5:11311';  
    matlab_ip = '192.168.1.13';  
    rosinit(ros_master_ip, 'NodeHost', matlab_ip); 
    pause(2)  
   
    laser_sub = rossubscriber('/scan', @get_laser ); 
    r = rosrate(2); % 2 Hz loop rate  
    for i=1:50 
        plot(laser_msg,'MaximumRange',7)   
        waitfor(r); 
    end 
    rosshutdown 
    close all 
end 

After setting up the ROS-MATLAB interface, we initialize the subscriber to the laser scan topic:

laser_sub = rossubscriber('/scan', @get_laser );

With this line, we demand, the get_laser function handles the data contained in the /scan topic. To exchange data between different MATLAB scripts, we use a global variable:

global laser_msg;

Finally, we plot the laser scanner data of the laser data for 25 seconds:

plot(laser_msg,'MaximumRange',7)

Let's now look at the code of the get_laser function:

function get_laser(~, message) 
    global laser_msg; 
    laser_msg = message; 
end 

In this function, we just save the value of the laser scanner data.

After launching the Gazebo simulation, we can run the MATLAB script:

>> plot_laser  

The output of the the default placement of the scene objects is shown in the following screenshot:

Figure 9: Gazebo laser scanner data plotted in MATLAB
..................Content has been hidden....................

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