Explaining callbacks

You can get the complete code from chapter_8_codes/Matlab/teleop.m. Let's look at the content and functions of each callback. The first callback we are going to see is for the ROS MASTER IP edit box:

    function edit1_Callback(hObject, eventdata, handles) 
    global ros_master_ip 
    ros_master_ip = get(hObject,'String') 

When we enter an IP address from the ROS network in this edit box, it will store the IP address as a string in a global variable called ros_master_ip. If you don't enter the IP, then a default value is loaded, defined outside the callback.

Here are the initial values of ros_master_ip, ros_master_port, and teleop topic.

    ros_master_ip = '192.168.1.102'; 
    ros_master_port = '11311'; 
    teleop_topic_name = '/cmd_vel_mux/input/teleop'; 

If we don't provide any values in the textbox, these initial values get loaded.

The next GUI element is for obtaining the ROS MASTER PORT. This is the callback of this edit box:

    function edit2_Callback(hObject, eventdata, handles) 
    global ros_master_port 
    ros_master_port = get(hObject,'String')

In this function too, the port from the edit box is stored as string type in a global variable called ros_master_port.

The next edit box is for obtaining the teleop_topic_name. Here is its callback function definition:

    function edit3_Callback(hObject, eventdata, handles) 
    global teleop_topic_name 
    teleop_topic_name = get(hObject,'String') 

Similar to ros_master_port and port, this too is stored as string in a global variable.

After obtaining all these values, we can press the Connect to Robot button for connecting to the ROS robot/ROS PC. If the connection is successful, you can see proper messages in the command line. Here are the callback definitions of the Connect to Robot button:

    function pushbutton6_Callback(hObject, eventdata, handles) 
 
    global ros_master_ip 
    global ros_master_port 
    global teleop_topic_name 
    global robot 
    global velmsg 
 
    ros_master_uri = 
    strcat('http://',ros_master_ip,':',ros_master_port) 
    setenv('ROS_MASTER_URI',ros_master_uri) 
 
    rosinit 
 
    robot = rospublisher(teleop_topic_name,'geometry_msgs/Twist'), 
    velmsg = rosmessage(robot); 

This callback will set the ROS_MASTER_URI variable by concatenating ros_master_ip and the port. Then, it initialize the connection by calling rosinit. After connecting, it will create a publisher of geometry_msgs/Twist, which is for sending the command velocity. The topic name is the name that we give in the edit box.

After successful connection, we can control the robot by pressing keys such as Forward, Backward, Left, and Right.

The speeds of linear and angular velocity are initialized as follows:

    global left_spinVelocity 
    global right_spinVelocity 
 
    global forwardVelocity 
    global backwardVelocity 
 
    left_spinVelocity = 2;        
    right_spinVelocity = -2;        
    forwardVelocity = 3;     
    backwardVelocity = -3;  

Let's look at the function definition of Forward first:

    function pushbutton4_Callback(hObject, eventdata, handles) 
    global velmsg 
    global robot 
    global teleop_topic_name 
    global forwardVelocity 
    velmsg.Angular.Z = 0; 
    velmsg.Linear.X = forwardVelocity; 
 
    send(robot,velmsg); 
    latchpub = rospublisher(teleop_topic_name, 'IsLatching', true); 

What it basically does is it publishes a linear velocity and latches it on the topic. In the Backward callback, we are providing a negative linear velocity. In the Left and Right callbacks, we are only providing an angular velocity.

After doing all this, we can save the figure file, which is the .fig and .m file, which is the MATLAB file.

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

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