gramaziokohler / roslibpy

Python ROS Bridge library
MIT License
273 stars 56 forks source link

Guidance on Publishing messages with multiple componets #47

Closed wegunterjr closed 4 years ago

wegunterjr commented 4 years ago

I am publishing data to a rosbridge, and seem to be just fine with 'std_msgs/String', and 'std_msgs/Float32, but when i try to do 'geometry_messages/Pose' which has orientation and position, I see the topic being published, but there is no data.

rvr_Pose = roslibpy.Topic(client, '/rvr_pose', 'geometry_msgs/Pose')

rvr_pose = Pose()
 rvr_pose.position.x = locator_X
rvr_pose.position.y = locator_Y
rvr_pose.orientation.z = imu_Yaw
print(locator_X)
print(rvr_pose)
print(rvr_pose.position)
print(rvr_pose.orientation)
print(type(rvr_pose.orientation))
rvr_Pose.publish(roslibpy.Message({'position': rvr_pose.position, 'orientation' : rvr_pose.orientation}))

Thoughts?

gonzalocasas commented 4 years ago

It's not clear what the type of rvr_pose is, but I assume it is not a dictionary, so very likely, the serialization is failing. Make sure you pass a dictionary in there and it will work.

wegunterjr commented 4 years ago

Ah..It is Pose from ROS, did I setup the Message right? With the two parts?https://docs.ros.org/melodic/api/geometry_msgs/html/msg/Pose.html

# A representation of pose in free space, composed of position and orientation. 
Point position
Quaternion orientation
gonzalocasas commented 4 years ago

ROS messages need to be converted to dictionaries to be sent via the rosbridge, the easiest way to do that is using the yaml module, so, in your case, you the following should work:

import yaml

rvr_pose = Pose()
rvr_pose.position.x = locator_X
rvr_pose.position.y = locator_Y
rvr_pose.orientation.z = imu_Yaw

rvr_pose_topic = roslibpy.Topic(client, '/rvr_pose', 'geometry_msgs/Pose')
rvr_pose_serialized = dict(yaml.load(str(rvr_pose)))
rvr_pose_topic.publish(roslibpy.Message(rvr_pose_serialized))

NOTE: In your code, you use rvr_pose as a Pose instance, and rvr_Pose as a topic instance, that's not a very good idea, because they look almost the same, except the casing. In my example, I have renamed them to rvr_pose and rvr_pose_topic respectively.

wegunterjr commented 4 years ago

WOOHOO!!!! That worked!

gonzalocasas commented 4 years ago

Great 👍

danialdunson commented 2 years ago

this should be added to the docs. thank you.