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

how to use callback() with python

asked 2018-09-11 21:15:37 -0500

lanlan gravatar image

can anyone explain how to use callback in writing a subscriber? the tutorial uses callback(data),but what it data? what content dose it have? thank you very much!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2018-09-12 01:15:56 -0500

mgruhler gravatar image

updated 2018-09-12 08:12:58 -0500

data is just the variable name for the message that is passed in. You could name it msg, foo or whateveryoulike.

The content of it depends on the message type defined in the according instantiation of the subscriber. In the tutorial this is rospy.Subscriber("chatter", String, callback) type is std_msgs::String (check the import statements). You can find the data fields of ROS messages on the docs pages, e.g. here or using rosmsg show PKG MSG.


EDIT

Well the first data in callback(data) is a ROS message, so if the ROS message is an std_msgs/Int16, data refers to that. However, this is actually a class and the actual int16 is found in the data.data member. Again, the first data is just a name, the second one is the variable of the std_msgs/Int16 which is "hardcoded".

As a more intuitive example, as it gets rid of this double data thing is e.g. std_msgs/ColorRGBA. data is the full message and you can access the respective field r, g, b and a via data.r, data.g and so on...

edit flag offensive delete link more

Comments

so if the message is int16, than the data will be exactly the int16? just the same as the message?

lanlan gravatar image lanlan  ( 2018-09-12 07:55:38 -0500 )edit

see edit above

mgruhler gravatar image mgruhler  ( 2018-09-12 08:09:51 -0500 )edit

understand, thank you!

lanlan gravatar image lanlan  ( 2018-09-12 15:56:40 -0500 )edit

@lanlan if this answered your question, please check the answer as correct by clicking the check-mark on the left...

mgruhler gravatar image mgruhler  ( 2018-09-13 00:52:13 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-09-11 21:15:37 -0500

Seen: 3,326 times

Last updated: Sep 12 '18