ros / geometry2

A set of ROS packages for keeping track of coordinate transforms.
190 stars 275 forks source link

Documentation on convert/transform registration in Python #515

Open MatthijsBurgh opened 3 years ago

MatthijsBurgh commented 3 years ago

Is there any documentation on the convert/transform registration in Python?

MatthijsBurgh commented 3 years ago

ping ;)

MatthijsBurgh commented 3 years ago

@tfoote ping ;)

tfoote commented 3 years ago

There's no significant docs. You can find examples here and here

For converting: You write a to and from message and register them with tf2_ros.ConvertRegistration()

For transforming: You write the function to add it and then register it using the tf2_ros.TransformRegistration()

MatthijsBurgh commented 3 years ago

For me it is unclear why all the to_msg and from_msg functions can just return the same message.

I would expect that if I have class A and I want to transform to class B, but there is no direct conversion from A to B, it will use the to_msg and from_msg way. So I have to go from A to a msg. Lets call it X. And then convert msg X to class B.

In my mind, msg X has to be ONE type of msg. As a random pair of to_msg and from_msg aren't compatible, unless the to_msg functions outputs a type, which the from_msg functions accepts. Otherwise AttributeError are doomed to happen IMO.

Maybe I am missing something, please explain.

tfoote commented 3 years ago

The first example I sent isn't the best as those datatypes are already messages. So the convert to and from message is an identity transform which is why they return themselves.

For the KDL datatypes you can see a more complex version https://github.com/ros/geometry2/blob/noetic-devel/tf2_kdl/src/tf2_kdl/tf2_kdl.py

The logic is to chain the conversions if they exist via a star topology. Any datatype X trying to go to Y look to apply from_msg<Y>(to_msg(X). Thus if X is already a msg it can return itself.

MatthijsBurgh commented 2 years ago

What happens in the following situations?

  1. to_msg returns a PointStamped, but the from_msg can only handle PoseStamped as input.
  2. Again to_msg returns a PointStamped, but Y is a PoseStamped and its from_msg functions just returns the input msg. Just like https://github.com/ros/geometry2/blob/c73b5939723db078c9bbe18523230ad54f859682/tf2_geometry_msgs/src/tf2_geometry_msgs/tf2_geometry_msgs.py#L43-L47
tfoote commented 2 years ago

1) The registrations are keyed on the datatype. A Vector3Stamped converter and a PoseStamped converter will never be chained.

2) Those theto_msg and from_msg will not be paired like you said because they have different datatype keys.

The noop returns are required because in python we can't detect what the datatype is so we don't know if the datatype matches the input datatype. So convert always triggers to_msg then from_msg to make the conversion. If either end of that goal is already a message it will be the noop.