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

Extracting message type in callback (Python)

asked 2013-09-02 22:40:23 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hey all, I want to extract data of a specific type from the message in the callback. I have only one callback where I get messages from different topics of different type like Float64, Odometry,.etc. Now I want to extract only the data of type Quaternion in side any message of any topic. My call back looks like (although its not correctly working): def callback(self, msg, arg): for m in msg.__slots__: if type(m) == [Quaternion]: self.process(m) but Quaternion is not directly recognized. Also I have to dig into the messages like Odometry has quaternion data but inside the pose.pose.position, how to check the whole structure of message for quaternion datatype data. Thanks in advance,

edit retag flag offensive close merge delete


Are you sure that you can't find a better/cleaner design for what you want? Seem's kinda odd to me.

dornhege gravatar image dornhege  ( 2013-09-03 00:10:14 -0500 )edit

thanks for the suggestion! can you please just write me a hint for the better/cleaner design.I just want to extract Quaternion data xywz (if any) from the msgs, AND I do not know in prior how many topics I shall get and of which type. so I have one generic callback for all the topics...regards

safzam gravatar image safzam  ( 2013-09-03 00:17:06 -0500 )edit

The thing is that I"m not sure why you'd want something like that.

dornhege gravatar image dornhege  ( 2013-09-03 01:34:57 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-03 00:09:39 -0500

dornhege gravatar image

Might it be geometry_msgs.Quaternion?

Digging through the structure can be achieved in the same way that you accesses msg.__slots__, just recursively.

edit flag offensive delete link more


thanks for the comment. but msg.__slots__ gives the sub fields names but their type is 'str' rather than Point,.etc. is there any way to dig out the msg structure with type by type dynamically?

safzam gravatar image safzam  ( 2013-09-03 00:20:56 -0500 )edit

There is _slot_types defined for ros msgs.

dornhege gravatar image dornhege  ( 2013-09-03 01:34:36 -0500 )edit

yes but it does not help for further digging like I do: for l1 in msg._slot_types: print l1 and output i get [std_msgs/Header,string,geometry_msgs/PoseWithCovariance,geometry_msgs/TwistWithCovariance] but when I want to dig using 'print l1.__slots__' or 'print l1._slot_types' gives error. ?

safzam gravatar image safzam  ( 2013-09-03 03:31:39 -0500 )edit

Hallo donhege, thanks a lot, now it works. the only problem was that 'print l1.__slots__' or 'print l1._slot_types' gives error when datatype encountered is simple and basic datatype because it does not have further substructure. now it works, thank you. regards

safzam gravatar image safzam  ( 2013-09-03 06:49:27 -0500 )edit

Question Tools

1 follower


Asked: 2013-09-02 22:40:23 -0500

Seen: 1,064 times

Last updated: Sep 03 '13