Matlab/Simulink - Gazebo -Co-Simulation - rosbag

6 views (last 30 days)
im planning to do a CO-Simulation with Matlab/Simulink and Gazebo with the ROS-Toolbox in Simulink. There is a specific tool named 'Gazebo Pacer' which synchronize Gazebo simulation with the time step of Simulink. Do you have any idea to play a rosbag under this circumstances?
For clarification. I want to play a rosbag, since i press the start button in Simulink. One approach was to add the rosbag in the launchfile which is launching the whole gazebo environment. But if i do this, the playback starts immediately. But i want to start it with the play button from Simulink. Furthermore, i couldn't figure out a way to synchronize the rosbag and the simulink timestep.
Is there a possible solution for it? Or have anyone similiar problems? Maybe there is an approach with the c++ api? I'm very interested in a solution or at least a solution suggestion...

Answers (1)

Cam Salzberger
Cam Salzberger on 22 Apr 2020
Hello Matthias,
One suggestion is to use the Read Data block from ROS Toolbox in your Simulink model. This is designed to output messages for a selected topic from the rosbag, synchronized with the Simulink simulation time. You can run the output to an Enabled Subsystem with a Publish block inside. You can set up multiple Read Data + Enabled Subsystem + Publish combinations if you need multiple topics out of the rosbag.
Matthias B
Matthias B on 23 Apr 2020
Hello Cam,
thanks for your answer. I was very excited to hear your answer.
I tried your solution and it works fine, at the first glance. But I evaluated this time synchronization by differenciating rosbag time and simulink time. I have an amount of time error which let me conclude that this solution is not quite synchron.
My setting:
ROS Bag block -> sample time: 0.1
ode1 solver -> sample time: 0.01
Gazebo Pacer -> sample time: 0.1
I want to achieve an time error less then 0.1 or in other words, less then sample time of the pacer... Do you have any further suggestions? I think I did a crucial mistake...
P.S.: Same result with enables subsystem..
Cam Salzberger
Cam Salzberger on 24 Apr 2020
I'm a little confused about what you are asking for (not sure what you mean by "time error"), but I can try to provide more information that may help get towards solutions.
The intent behind the Read Data block design is to be similar to the Subscribe block. This allows users who want to test their system with recorded data or live data from a robot to easily switch the data input mechanism. Thus, the sampling of the data is similar to the way the Subscribe block works, which may cause some downsides for your use-case. If you have a Subscribe block, and data is being published at 15 Hz, but the sample time of the Subscribe block is 0.1, then (if you synchronize simulation time to real time) you will miss some messages, and only the most recently received message is output at a given timestep. The Read Data block will behave similarly, outputting the most "recent" message based on the rosbag timestamp (offset from the start of the bag) compared to the simulation time.
If you would like to ensure that you are sending all of the messages from the rosbag, you will need to select the block sample time such that it is smaller than the smallest gap between messages. It's been a while since I've worked with it, and can't verify right now, but I believe the topic selection dialog provides this "minimum gap" information for each topic to help this workflow. Alternatively, I believe you can use Variable sample time (-2), which will calculate the sampling time for each step based on when the next message is. This has some dependency on the simulation step size though, which I can't recall offhand.
If you are trying to have the Read Data block sample faster than the Gazebo Pacer block, is there any reason you can't just set the sample times in that way (i.e. Read Data -> 0.01, Gazebo Pacer -> 0.1)?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!