ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

So, the topic /cmd_vel topic should have the message type Twist

Looking at the message description, we can see that each incoming message should have a linear component, for the (x,y,z) velocities, and an angular component for the angular rate about the (x,y,z) axes.

I'm going to give you some example code in Python, but this could just as easily be done in C++.

#!/usr/bin/env python
import roslib; roslib.load_manifest('YOUR_PACKAGE_NAME_HERE')
import rospy
import tf.transformations
from geometry_msgs.msg import Twist

def callback(msg):
    rospy.loginfo("Received a /cmd_vel message!")
    rospy.loginfo("Linear Components: [%f, %f, %f]"%(msg.linear.x, msg.linear.y, msg.linear.z))
    rospy.loginfo("Angular Components: [%f, %f, %f]"%(msg.angular.x, msg.angular.y, msg.angular.z))

    # Do velocity processing here:
    # Use the kinematics of your robot to map linear and angular velocities into motor commands

    v_l = ...
    v_r = ...

    # Then set your wheel speeds (using wheel_left and wheel_right as examples)
    wheel_left.set_speed(v_l)
    wheel_right.set_speed(v_r)

def listener():
    rospy.init_node('cmd_vel_listener')
    rospy.Subscriber("/cmd_vel", Twist, callback)
    rospy.spin()

if __name__ == '__main__':
    listener()

In this example, we create a node called cmd_vel_listener that subscribes to the /cmd_vel topic. We can then create a callback function (called callback), which accepts the message as a parameter msg.

From here, it is as simple as reading the named fields from the message, and using that data in whatever way that you want in your application.

Hope this helps.