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

Access field in message inside a message

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

Oevli gravatar image

Hallo

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):
    rospy.loginfo(Imu.orientation.z)
   #do something with z

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

1 Answer

Sort by » oldest newest most voted
0

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

Thomas D gravatar image

In imu_callback try

rospy.loginfo(data.orientation.z)

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

Comments

Thanks :-)

Oevli gravatar image Oevli  ( 2015-11-24 09:32:19 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 591 times

Last updated: Nov 24 '15