Ask Your Question

Access field in message inside a message

asked 2015-11-24 08:49:32 -0500

Oevli gravatar image


Being new to ros, I´ve stumbled into a stupid problem..

Im writing a python listener, and I want to get the orientation from an Imu.

The variable I want to access can be echoed with : rostopic echo /imu/orientation/z

Here is what I have so far - which is not really working out...:

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Imu
from geometry_msgs.msg import Quaternion
def imu_callback(data):
   #do something with z

def imu_listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("imu", Imu, imu_callback)
if __name__ == '__main__':
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2015-11-24 09:29:34 -0500

Thomas D gravatar image

In imu_callback try


You need to use the message variable passed into your callback function (which you have named data), not the message type.

Be careful though, the orientation is represented as a quaternion, so if you are thinking that the z component is yaw you are going to have issues. See this answer for ways to convert rotations in rospy.

edit flag offensive delete link more


Thanks :-)

Oevli gravatar image Oevli  ( 2015-11-24 09:32:19 -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: 2015-11-24 08:49:32 -0500

Seen: 506 times

Last updated: Nov 24 '15