ROS Basics in 5 Days Unit 2 Exercise 3.1


I had a question about the variables in the Twist message type. I’m going to try to get straight to the point here.

move = Twist()
move.linear.x = 0.5 #Move the robot with a linear velocity in the x axis
move.angular.z = 0.5 #Move the with an angular velocity in the z axis

My confusion was in determining move.linear.x and move.angular.z

specifically .linear.x and .angular.z

I used rostopic info /cmd_vel to determine the type geometry/msgs/Twist
Then roscd geometry/msgs/msg to enter the directory where the Twist.msg was
cat Twist.msg shows Vector3 linear and Vector3 angular

I’m new to python as in just began learning everything from this course within the last week. From my understanding Twist is a class. I figured Twist.linear and Twist.angular were variables of this class, but I’m not sure where the .x or .z are coming from. I’m also not sure where to look to find whatever file this is all declared in.

The essence of what I’m trying to ask is how could I have determined without looking at the solution that it was move.linear.x = 0.5 instead of move.linear = 0.5?

For anyone reading, I’ve made some progress on this question:
part of what I was missing was rosmsg show geometry_msgs/Twist
this indicates linear x,y,z float 64 and angular x,y,z float64

Additionally rosmsg show geometry_msgs/Twist indicates there is a geometry_msgs/Vector3 : what is this, and how can I access it? When I attempt to go there the terminal tells me the directory does not exist.

Any clarification on the syntax meaning of Twist().linear.x would be appreciated: in terms of python what is linear? Once again my understanding is Twist is a class, x is a variable, is linear a data structure or something?

1 Like

Helo @jg-latham,

If you execute the rosmsg show command as you said before, you will get the following:

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

Here you can see the structure of the Twist message. Basically this means that the Twist message is composed of 2 other messages (named linear and angular), which are of the type Vector3.

Again, if you use the rosmsg show Vector3, you will see the following:

float64 x
float64 y
float64 z

Summarizing, the Twist message contains inside 2 “sub-messages” (linear and angular) which are defined by the variables x, y and z. In case you want to check the file where this is defined, you can do the following:

  • First, go to the folder that contains the message file:
    roscd geometry_msgs

  • next, access the msg folder (which is always the folder that contains the message files):
    cd msg

  • Here you will find the Twist.msg file:
    cat Twist.msg

So yes, when you execute the below piece of code:

move = Twist()

What you are actually doing is to create an instance of the Twist message and assigning it to the move variable.

Hope this clarifies your doubts,