I need to know the translation and rotation of this robot. I was thinking to use the translation = linear.velocity *dt, and rotation = linear.velocity.z * dt. But I don't know what dt is. I have a subscriber with ros::spin() to subscriber the /cmd_vel topic.
The cmd_vel
topic is typically used to command new velocity setpoints to a mobile base. It doesn't carry any state information in those cases -- only desired state. It would seem strange to me to use that to derive actual state of your robot, unless you don't care about discrepancies between commanded and actual state of course.
I can also use the data in /odom.
The odom
topic (nav_msgs/Odometry) does carry information about the actual state of your system. Both position and velocity, so the integration step has already been done for you (and possibly in a more advanced and robust way than simply integration velocity over a time interval, but that depends on the driver used and how that is written).
But I still want to know the dt.
For message types that include a header (such as Odom
and TwistStamped
), I would advise you to make use of the stamp
field in the header. If the publisher has correctly populated the fields, the stamp
field should correspond to when the measurement (or calculation) that was used to populate the fields in the message associated with the timestamp took place (or was performed).
In that case, dt
would simply be msg1.header.stamp - msg0.header.stamp
. As long as you keep a single-message history in your receiving node, you can calculate the dt
between two successive messages that way.
For messages that don't have headers (such as a regular Twist
), you have two options:
- use
ros::Time::now()
to approximate the time your code received the message as shown by @HankYuan in his answer - use a callback for a
ros::MessageEvent<M>
instead of the message type itself and use the receipt time as an approximation of when the message was received by the middleware
Refer to wiki/roscpp/Overview/Publishers and Subscribers: Subscribing to a Topic - MessageEvent for information on MessageEvent
.
I can also use the data in /odom. But I still want to know the dt. Thank you!