Next Tutorial: Environment Variables Previous Tutorial: Relay
Overview
In this tutorial, we are going to describe the process of recording and playing back a collection of messages.
Gazebo Transport provides two mechanisms for logging: a C++ API and a set of command line utilities as part of the optional gz
CLI tool (available via Gazebo Tools). We use SQLite3 to create a file containing all the messages recorded during a session. You can imagine it as a container where all the original messages have been efficiently stored and timestamped.
Let's create a directory for compiling and running our examples:
Record
Download the record.cc file within the gz_transport_tutorial
folder and open it with your favorite editor:
Walkthrough
The Recorder.hh
header contains all the recording functionality. Make sure that is included in your source code.
The next step requires instantiation of a log::Recorder
object and specification of the topics that we're interested in logging. The AddTopic()
function accepts a regular expression to set the appropriate topic filter. In our example, we are recording all topics.
The Start()
method starts recording messages. Note that the function accepts a parameter with the name of the log file.
In our example, we are logging messages until the user hits CTRL-C
. The function gz::transport::waitForShutdown()
captures the appropriate signal and blocks the execution until that event occurs. Then, recorder.Stop()
stops the log recording as expected.
Play back
Download the playback.cc file within the gz_transport_tutorial
folder and open it with your favorite editor:
Walkthrough
The Playback.hh
header contains all the log play back functionality. Make sure that is included in your source code.
The next step requires to instantiate a log::Playback
object and to specify the path to the file containing the log. The AddTopic()
function accepts a regular expression to set the appropriate topic filter. In our example, we are playing back all messages.
The Start()
method starts message playback. Note that this is an asynchronous function, the messages will be published without blocking the current thread.
In this example we are not interested in doing anything else, besides playing back messages. Therefore, we can use WaitUntilFinished()
to block the current thread until all messages have been published.
Building the code
Download the CMakeLists.txt file within the gz_transport_tutorial
folder.
Once you have all your files, go ahead and create a build/
directory within the gz_transport_tutorial
directory.
Run cmake
and build the code.
Running the examples
NOTE It is essential to have a valid value of
GZ_PARTITION
environment variable and to have it set to the same value in all open terminals. AsGZ_PARTITION
is based on hostname and username, especially Windows and Mac users might have problems due to spaces in their username, which are not a valid character inGZ_PARTITION
. gz-transport prints errorInvalid partition name
in such case. To resolve that, setGZ_PARTITION
explicitly to a valid value:# Linux and Macexport GZ_PARTITION=test# Windowsset GZ_PARTITION=test
Open two new terminals and from your build/
directory run the recorder.
From terminal 1:
From terminal 2, publish a message using gz
:
From terminal 1, hit CTRL-C
in your recorder terminal to stop the recording.
Moving back to terminal 2, run a subscriber:
And from terminal 1, playback your log file:
You should receive one message in terminal 2:
Using gz for recording and playing back
As an alternative to the C++ API, we could use the gz
suite of command line tools for recording and playing back messages.
Next is how you can record a set of messages using gz
:
And here's how you can play back the previous log file using gz
:
For further options, try running:
and