D1 – ROSjava Time – publish/subscribe

Tre are two possibilities how to represent time in the ROS network. First is to use time provided by the OS, this is called Wall-time. This time cannot be manipulated well. The second option is to use one ROS node in the network as a Clock Master, which defines clock for the rest of the network. This enables us to change the speed, time step of the clock or e.g. restarting it.

This tutorial briefly shows how to use the time in the rosjava. For more information, look here.

The simulated time controlled by the Clock Master is controlled by means of publishing ROS messages over the network, the Clock Master publishes messages of type rosgraph_msgs.Clock to the topic “/clock“. To use the simulated time, ROS nodes must have set this parameter before are launched:


The files for this demo are stored under: demonodes/basic/org.hanns.demonodes.time.pubsub.

Send the Clock data over the ROS network – Wall Time Used

The first demo shows how the:

  • Pub node publishes Clock messages on the topic “/clock”
  • Sub node receives messages as normally and reads the current Wall time

To run the demo, start some roscore, e.g.:

cd nengoros/jroscore && ./jroscore

Start a node which subscribes to the topic `clock` and receives messages normally:

./runner org.hanns.demonodes.time.pubsub.Sub /use_sim_time:=false
(sim time is not used by default if not set in the parameterTree).

Note that the node reads WallTime data normally.

Start the node which periodically publishes Clock data (e.g. now run from the class files) from the folder demonodes/basic run:

./runner org.hanns.demonodes.time.pubsub.Pub /use_sim_time:=false
Note that both nodes use `Wall` clock (time provided by the OS), so the subscriber also prints out these data, so the time is something like this: 1383703785:529000000.

The Clock Master – Simulated Time Used

This part shows how to use the same nodes, but this time, the simulation time will be provided by the Publisher node, publisher will speed up and slow down the time during the simulation.

Start the normal node which will now use the simulated time:

./runner org.hanns.demonodes.time.pubsub.Sub /use_sim_time:=true

Node should be now waiting for a time provider to obtain current time. Start the time provider:

./runner org.hanns.demonodes.time.pubsub.Pub /use_sim_time:=false

Now, the rest of the networks should be set to accept the received time as the real time.

Now, we can see that the same method getCurrentTime() in the Subscriber node shows the time provided by the Publisher. The speed of time is speeded up and slowed down. When stopping the publisher node, the time stops.

Parameter Server

Default parameters can be set also in the parameter server. These values are stored in the ROS master and are deleted after shutdown (if not saved in file). Therefore one can set default values for all nodes to be launched. ROS installation has the application rosparam, in case of ROS is not installed, it can be done by the application jrosparam as follows:

Start the core:

cd nengoros/jroscore && ./jroscore

Start the jrosparam application in a new terminal:

cd nengoros/jrosparam && ./jrosparam

It can be set any parameter, this parameter will be used by other nodes, e.g.:

set /use_sim_time true

for help, type “h” in the console.

Now, to use simulated time, just launch nodes as follows:

./runner org.hanns.demonodes.time.pubsub.Sub
./runner org.hanns.demonodes.time.pubsub.Pub /use_sim_time:=false

Note that in use_sim_time mode, the method getCurrentTime() should return the time 0, but (probably due to error) the Null Pointer Exception is thrown. So it is advised to subclass the org.hanns.demonodes.time.AbstractTimeNode (and call super.onStart(node)), which waits until the Clock Master is started.