6.1 ROS Publisher Factory

ros_publisher_factory is used to create a ROS2 publisher handle that can be used to publish or write messages to the topic.

Publisher must have compatible Quality of Service (QoS) parameters.

6.1.1. Command

ros_publisher_factory(topic, type, history="default", depth=1, reliability="default", durability="default", deadline=0, lifespan=0, liveliness="default", lease_duration=0)

6.1.2. Parameters

(deafult parameters will fall back on the ROS backends defaults)

  1. topic : The name of the topic.

  2. type : The type of the messages on the topic. A dynamic library corresponding to the type must be present on the robot, e.g libstd_msgs__rosidl_typesupport_introspection_c.so for std_msgs/String.

  3. history : How many messages to store in the history. Options are “default”, “keep_last” and “keep_all”.

  4. depth : The size of the queue of old messages. It requires history to be “keep_last” to take effect.

  5. reliability : How reliable the topic should be. Options are “default”, “reliable”, and “best_effort”.

  6. durability : Options are “default”, “transient_local”, and “volatile”.

  7. deadline: A contract for how much time is allowed between messages.

  8. lifespan : The amount of time a message will be considered valid. 0 will mean lifespan is disabled.

  9. liveliness : Specifies for how long the publisher is considered alive. Options are “default”, “automatic”, and “manual”.

  10. lease_duration: The period of time a publisher has to indicate that it is alive before it is considered to have lost liveliness.

6.1.3. Methods on Handle

  1. publish(myStruct) : Takes one parameter which is expected to be a struct that matches the message type of the topic.

  • write(myStruct) is an alias to this method.

  1. close() : Indicates that the publisher will not be used further, and releases memory. Method should be used to actively close handles created in the local scope.

6.1.4. Example Command

ros_publisher_factory("test", "std_msgs/String")

In the above example, it publishes to the topic “test” with a message type from ROS2 standard messages (std_msgs) with the specific type String.

In order to use a message type, the corresponding dynamic library (in this case libstd_msgs__rosidl_typesupport_introspection_c.so) must be present on the robot, where it will be dynamically loaded when needed.

The std_msgs/String message type must have a single member named data of the type string.

Example:

handle.publish(struct(data="Hello Robot"))

6.1.5. Limitations

Please notice, that the publisher can completely release the resources only when the program stops, or close is called on the object. Keep the number of publishers as minimum as possible to avoid potential out-of-memory issues.