ros2_rust icon indicating copy to clipboard operation
ros2_rust copied to clipboard

Add dynamic message functionality

Open nnmm opened this issue 3 years ago • 1 comments

This is an overview PR to see the changes at a high level. I recommend you check out this branch and run cargo doc to see what the API looks like. For the review, it will be split into smaller parts.

What are dynamic messages?

Dynamic messages are messages whose type is only known at runtime. You create dynamic messages by specifying the type as a string, e.g. "geometry_msgs/msg/Twist". The dynamic message module then looks for a shared introspection type support library, loads it, and parses the structure of the C message struct from it. That structure contains information about what fields the message contains and what their types are, e.g. field 'angular' of type 'Vector3' at offset 24. Using the structure, the dynamic message module can then read out the value of each field, and even modify it. Basically, it allows accessing the fields of the message as a HashMap from string to a value like this:

enum Value<'msg> {
  Float(&'msg f32),
  Double(&'msg f64),
  ...
  FloatArray(&'msg [f32]),
  ...
  FloatSequence(&'msg Sequence<f32>),
  ...
  FloatBoundedSequence(DynamicBoundedSequence<'msg, f32>)
  ..
}

This is useful for writing generic tools such as introspection tools, bridges to other communication systems, or nodes that manipulate messages à la topic_tools.

In C++, a similar thing exists in https://github.com/osrf/dynamic_message_introspection and https://github.com/facontidavide/ros2_introspection.

Features:

  • Subscription and publisher for dynamic messages
  • Conversion between statically typed and dynamically typed messages
  • Safe access of fields – no unsafe required in user code

Non-features:

  • Dynamic services and clients
  • Serde impls for the dynamic type (requires compiling the Rust message package into a dynamic library)

Notes

  • The entire dynamic_message module is behind a feature flag that will be disabled by default. Thus, there is no impact on compile times for the majority of users who do not need this.
  • I still need to finish writing some more tests

Future work:

  • Factor out the common parts of Subscription and DynamicSubscription (and the same for publishers) – this can also help compilation times by monomorphizing Subscription.

nnmm avatar Sep 18 '22 13:09 nnmm

@eduidl, you might be interested in this, since I know you wrote a client library for Rust as well as a command-line tool that uses message introspection :)

nnmm avatar Sep 21 '22 14:09 nnmm

@nnmm thanks for all the work, it's unfortunate we couldn't merge all the related PRs earlier, but if you ever come back to ros2-rust, we'd happy to review any future work. I'm closing this PR, but feel free to reopen it :slightly_smiling_face:

esteve avatar Jan 09 '24 14:01 esteve