Lab 1.6: Extracting Data from ROS nodes

Overview

This lab is to analyze the topics published by nodes from other packages, and extract them to do further process.

Install Keyboard Teleoperation Package

First install the keyboard teleoperation package in Ubuntu using following command:

sudo apt-get install ros-indigo-teleop-twist-keyboard

We can use the package using following command, make sure you are running roscore.

rosrun teleop_twist_keyboard teleop_twist_keyboard.py

Now lets look at the topic that is published by this teleop node:

rostopic echo /cmd_vel

Go back to the terminal window of teller node, then you can see the topic printed as following:

linear:
  x: 0.5
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0

For more detail, lets look at the type of the topic using command:

rostopic type /cmd_vel

Where it will return:

geometry_msgs/Twist

Therefore, we now understand that the type is geometry_msgs/Twist, we can write a subscribe node that query Twist type message.

Use following command to understand what types of data in geometry_msgs/Twist.

rosmsg show geometry_msgs/Twist

You will see:

geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

Now we know the keyboard values sent from teleop_twist_keyboard are actually float type.

Subscribe the keyboard messages and modify them

Use following script to extract the geometry_msgs/Twist message, note green text:

#include “ros/ros.h”
#include “std_msgs/String.h”
#include “geometry_msgs/Twist.h”

void cmdRCCallback(const geometry_msgs::Twist::ConstPtr& cmd_RC){
float m_straight=0;
float m_turn=0;
m_straight=abs(240*cmd_RC->linear.x);
m_turn=cmd_RC->angular.z;
ROS_INFO(“%f”,cmd_RC->linear.x);
}

int main(int argc, char **argv)
{
ros::init(argc, argv, “tele”);
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe(“cmd_vel”, 100, cmdRCCallback);
ros::Rate r(10);

while (ros::ok()){
ros::spinOnce();
r.sleep();
}
return 0;
}

Since we use other message type, we have to include the dependency in manifest.xml file, find the manifest.xml  file in you package folder (in our case, it should be ~/dev/CIS623/my_ex1). The modified .xml file would be look like (note green text).

<package>
<description brief=”my_ex1″>

my_ex1

</description>
<author>Yanwhun</author>
<license>BSD</license>
<review status=”unreviewed” notes=””/>
<url>http://ros.org/wiki/my_ex1</url>
<depend package=”roscpp”/>
<depend package=”rospy”/>
<depend package=”std_msgs”/>
<depend package=”geometry_msgs”/>

</package>

Do not forget add the following line to compile the script as executable node in you CMakeLists.txt.

 rosbuild_add_executable(tele src/tele.cpp)

Now we all done, rosmake the package:

rosmake my_ex1

Test your node along with roscore and teleop_twist_keyboard node.

Assignment

Get the Lab1.bag file from TA and use following command to use it, make sure your terminal is in the file folder:

rosbag play Lab1.bag

Now you can see there is a /scan topic has been published, please write a ROS node that subscribes the /scan data and modify the ranges data in the topic.

Hint: the /scan data is laser data, you can see example in the section of textbook called “Accessing the laser data and modifying it“.