Ask Your Question

Twist Message Example and /cmd_vel

asked 2012-03-14 06:00:15 -0500

anonymous user


updated 2012-03-14 07:23:14 -0500

mjcarroll gravatar image

Hi there, i try to find any example of Twist_Message and cmd_vel example but i cant find it. If there any example that a subscriber listen to twist message using /cmd_vel topic. I try to drive a motor using arduino. Thank you.

edit retag flag offensive close merge delete


I not very familiar with Python. can you give me in C++ so i can directly compile it into arduino IDE ? Or you have servo motor subscriber example which is controlling by using ultrasonic sensor as a publisher ? Thnks in advance

MyloXyloto gravatar imageMyloXyloto ( 2012-03-25 10:10:57 -0500 )edit
mjcarroll gravatar imagemjcarroll ( 2012-03-25 15:24:55 -0500 )edit
mjcarroll gravatar imagemjcarroll ( 2012-03-25 15:25:18 -0500 )edit

Thanks, but i already done this rosserial tutorials and it works perfectly. The main problem is to mix the Servo, Rangefinder, arduino and my main processor in C language. My main goal is to build an normal arduino obstacle avoidance robot which is implement by ROS.

MyloXyloto gravatar imageMyloXyloto ( 2012-03-26 06:20:23 -0500 )edit

I believe, many people out there is not clear about this 'mixing' up method even though its a simple robot project. Sorry about that :)

MyloXyloto gravatar imageMyloXyloto ( 2012-03-26 06:29:15 -0500 )edit

So are you trying to do all of this on an Arduino? Or is there some interface to another computer?

mjcarroll gravatar imagemjcarroll ( 2012-03-26 10:31:55 -0500 )edit

Have you taken a look at this All the base code is done so it makes it much easier to get started and you can easily add support for different hardware.The readme gives a twist example, and it just enough to start playing with twist messages.

MobileWill gravatar imageMobileWill ( 2013-10-25 15:49:18 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-03-14 07:22:52 -0500

mjcarroll gravatar image

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)

def listener():
    rospy.Subscriber("/cmd_vel", Twist, callback)

if __name__ == '__main__':

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.

edit flag offensive delete link more


Where is the function set_speed and how are you writing it in arduino can you give me example of that?

Dhagash Desai gravatar imageDhagash Desai ( 2018-09-02 15:20:43 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2012-03-14 06:00:15 -0500

Seen: 33,977 times

Last updated: Mar 14 '12