rosrt is a package that contains realtime-safe tools for interacting with ROS. It is currently experimental and not API stable, so use at your own risk. Used together with a Xenomai Kernel the ROS processes get guaranteed CPU time and are not affected by other processes. rosrt can also slightly improve performance by avoiding memory allocations by preallocating. Those speed-ups should probably be helpful even if not running a real time system if small margins are important.


Before using any of rosrt you must call:

   1 rosrt::init();

There is an optional InitOptions argument to rosrt::init().

rosrt::init() starts three threads, one for publishing, one for subscribing and one for garbage collection. The publisher thread uses a lock-free multi-writer single-reader queue with a fixed limit on the # of messages specified by InitOptions::pubmanager_queue_size.


See also: rosrt::Publisher API docs

Using the rosrt::Publisher is very similar to the standard ros::Publisher but with some realtime-specific requirements. First, it provides you with a fixed-size buffer of messages (10 in the example below). To be realtime safe you must allocate your messages out of this rosrt::Publisher you're publishing it with. Second, you must initialize each of those messages with a template message when you initialize the rosrt::Publisher. Anything that needs to be preallocated should be preallocated in this template.

   1 rosrt::Publisher<std_msgs::UInt32> pub(nh.advertise<std_msgs::String>("topic", 1), 10, std_msgs::UInt32());
   2 std_msgs::UInt32Ptr msg = pub.allocate();
   3 if (msg)
   4 {
   5   msg->data = 10;
   6   pub.publish(msg);
   7 }

Deallocation of the Publisher's pool of memory is done by a separate garbage collection thread, and will not happen until all messages allocated out of the Publisher have been freed (or on program exit).


See also: rosrt::Subscriber API docs

Using the rosrt::Subscriber is a bit different from the standard ros::Subscriber, mainly because it's polling based instead of callback based:

   1 rosrt::Subscriber<std_msgs::UInt32> sub(3, nh, "test");
   2 std_msgs::UInt32ConstPtr msg = sub.poll();
   3 if (msg)
   4 {
   5   ...
   6 }

As with the rosrt::Publisher, you must specify a message pool size. If you are not going to be storing off received messages the optimal size for this is 3 (1 for the ROS-side subscription queue, 1 waiting in the Subscriber, 1 in use by realtime).

poll() will never return the same message twice. This means:

<message received>
sub.poll()  <---  returns a message
sub.poll()  <---  returns NULL
<message received>
sub.poll()  <---  returns a message

Deallocation of the Subscriber's pool of memory is done by a separate garbage collection thread, and will not happen until all messages allocated out of the Subscriber have been freed (or on program exit).

Allocation Tracking

rosrt also provides a way of tracking allocations and frees per thread. See the getThreadAllocInfo(), resetThreadAllocInfo(), setThreadBreakOnAllocOrFree() functions in the rosrt namespace, as well as the AllocInfo struct.

Wiki: rosrt (last edited 2013-09-11 14:23:34 by RobotOperator)