Read Model and Simulation Properties from Gazebo

This example shows how to interact with the Gazebo® Simulator from MATLAB®. It shows how to pause the Gazebo simulation, read the physics properties, and retrieve information about objects in the Gazebo world.

Prerequisites: Get Started with Gazebo and a Simulated TurtleBot

Connect to a Gazebo World

Open a Gazebo world from the desktop on your virtual machine or Linux® machine. If you are using the Ubuntu® virtual machine from Get Started with Gazebo and a Simulated TurtleBot, use Gazebo Empty or Gazebo Playground.

In your MATLAB instance on the host computer, run the following commands, replacing ipaddress with the IP address of your virtual machine. This command initializes ROS and connects your MATLAB instance to the ROS master at the specified IP address.

ipaddress = '192.168.203.129'; % Replace with your IP
rosinit(ipaddress)
Initializing global node /matlab_global_node_41902 with NodeURI http://192.168.203.1:56855/

Note: If the network you are using to connect to the simulation is not your default network, you can manually specify your IP address. The syntax is as follows. Replace IP_OF_VM with the IP address of the simulation and IP_OF_HOST_COMPUTER with the IP address of the host.

rosinit('IP_OF_VM','NodeHost','IP_OF_HOST_COMPUTER');

Show all the available ROS topics on the ROS network.

rostopic list
/camera/depth/camera_info                                   
/camera/depth/image_raw                                     
/camera/depth/points                                        
/camera/parameter_descriptions                              
/camera/parameter_updates                                   
/camera/rgb/camera_info                                     
/camera/rgb/image_raw                                       
/camera/rgb/image_raw/compressed                            
/camera/rgb/image_raw/compressed/parameter_descriptions     
/camera/rgb/image_raw/compressed/parameter_updates          
/camera/rgb/image_raw/compressedDepth                       
/camera/rgb/image_raw/compressedDepth/parameter_descriptions
/camera/rgb/image_raw/compressedDepth/parameter_updates     
/camera/rgb/image_raw/theora                                
/camera/rgb/image_raw/theora/parameter_descriptions         
/camera/rgb/image_raw/theora/parameter_updates              
/clock                                                      
/cmd_vel_mux/active                                         
/cmd_vel_mux/input/navi                                     
/cmd_vel_mux/input/safety_controller                        
/cmd_vel_mux/input/teleop                                   
/cmd_vel_mux/parameter_descriptions                         
/cmd_vel_mux/parameter_updates                              
/depthimage_to_laserscan/parameter_descriptions             
/depthimage_to_laserscan/parameter_updates                  
/gazebo/link_states                                         
/gazebo/model_states                                        
/gazebo/parameter_descriptions                              
/gazebo/parameter_updates                                   
/gazebo/set_link_state                                      
/gazebo/set_model_state                                     
/joint_states                                               
/laserscan_nodelet_manager/bond                             
/mobile_base/commands/motor_power                           
/mobile_base/commands/reset_odometry                        
/mobile_base/commands/velocity                              
/mobile_base/events/bumper                                  
/mobile_base/events/cliff                                   
/mobile_base/sensors/bumper_pointcloud                      
/mobile_base/sensors/core                                   
/mobile_base/sensors/imu_data                               
/mobile_base_nodelet_manager/bond                           
/odom                                                       
/rosout                                                     
/rosout_agg                                                 
/scan                                                       
/tf                                                         
/tf_static                                                  

If the list of topics is not displayed, then the network has not been set up properly. For network setup steps, refer to Get Started with Gazebo and a Simulated TurtleBot.

Set up a connection to the Gazebo world through the ExampleHelperGazeboCommunicator class, which makes interaction with Gazebo easier

gazebo = ExampleHelperGazeboCommunicator;

Read Physics Properties of the Gazebo Engine

Using the ExampleHelperGazeboCommunicator object, extract the physics properties of the simulation.

phys = readPhysics(gazebo)
phys = struct with fields:
                      Gravity: [0 0 -9.8000]
                   UpdateRate: 100
                     TimeStep: 0.0100
             SimulationStatus: 0
                DisableBodies: 0
    PreconditioningIterations: 0
              InnerIterations: 50
                   Relaxation: 1.3000
            ErrorToleranceRMS: 0
                 ContactWidth: 1.0000e-03
        MaxCorrectingVelocity: 100
          ConstantForceMixing: 0
      ErrorReductionParameter: 0.2000
                  MaxContacts: 20

Gazebo uses SI units.

To explore, change the gravity of the simulation so that it goes in the opposite direction in z. Reduce the value, too. Reset the simulation for the physics to affect all objects.

phys.Gravity = [0 0 0.1];       % Units are m/s^2
setPhysics(gazebo,phys);
resetSim(gazebo);

pause(5);                        % Let gravity take effect for 5 seconds

Pause, Unpause and Restart Simulation

You see the TurtleBot® (and anything else in the world) slowly accelerating up and away from the ground. Pause the simulation using the ExampleHelperGazeboCommunicator.

pauseSim(gazebo);
pause(3);

When you pause, the scene might look like this figure:

Resume the simulation. See that the objects start moving up again.

resumeSim(gazebo);
pause(3);

The TurtleBot floats up again

The resetSim function does not reset the physics to the original values. Manually reset the physics using setPhysics.

Run Simulation at 1/8 Speed

This section describes how to run the Gazebo simulator in slow motion. Using slow motion can be very useful for careful examination of abrupt or short-term physical interactions and behaviors.

Run the simulation at a reduced speed by changing the update rate on the physics. You can see how it looks running at 1/8 speed.

pauseSim(gazebo);
phys = readPhysics(gazebo);
phys.UpdateRate = phys.UpdateRate/8;
setPhysics(gazebo,phys);

Set gravity back to the normal value.

phys.Gravity = [0 0 -9.8];   % Set gravity back to normal value (m/s^2)
setPhysics(gazebo,phys);
resumeSim(gazebo);
pause(5);

When you resume the simulation the TurtleBot falls back to the ground, but in slow motion.

Switch the update rate back to normal so the simulation runs in real time.

phys.UpdateRate = phys.UpdateRate*8;   % Set update rate back to normal value
setPhysics(gazebo,phys);
pause(3);

Reset the simulation.

resetSim(gazebo);

Obtain List of Models in Gazebo World

The ExampleHelperGazeboCommunicator class allows you to find all models in the world.

models = getSpawnedModels(gazebo)
models = 2×1 cell array
    {'ground_plane'}
    {'mobile_base' }

Obtain Links from a Model

Retrieve specific information about a model (the Kobuki® TurtleBot® in this case) by creating an object for it with the ExampleHelperGazeboSpawnedModel class.

kobuki = ExampleHelperGazeboSpawnedModel('mobile_base',gazebo)
kobuki = 
  ExampleHelperGazeboSpawnedModel with properties:

      Name: 'mobile_base'
     Links: {3×1 cell}
    Joints: {2×1 cell}

[kobukiLinks, kobukiJoints] = getComponents(kobuki)
kobukiLinks = 3×1 cell array
    {'base_footprint'  }
    {'wheel_left_link' }
    {'wheel_right_link'}

kobukiJoints = 2×1 cell array
    {'wheel_left_joint' }
    {'wheel_right_joint'}

Read Properties of a Model - Pose and Orientation

Using the ExampleHelperGazeboSpawnedModel object for the TurtleBot, obtain the position, orientation, and velocity by using the getState function.

[position, orientation, velocity] = getState(kobuki)
position = 1×3

   -0.0005    0.0001   -0.0011

orientation = 1×3

   -0.2551   -0.4581    0.0049

velocity = struct with fields:
     Linear: [0.0011 6.9205e-05 -1.3551e-05]
    Angular: [8.9766e-04 1.6358e-04 0.0025]

Obtain List of All Models from Gazebo Online Model Database

Use the exampleHelperGazeboListModels function to get all Gazebo models available for download from the Gazebo website.

builtInModels = exampleHelperGazeboListModels;

Shutdown

Clear the workspace of publishers, subscribers, and other ROS-related objects when you are finished with them.

clear

Use rosshutdown once you are done working with the ROS network. Shut down the global node and disconnect from Gazebo.

rosshutdown
Shutting down global node /matlab_global_node_41902 with NodeURI http://192.168.203.1:56855/

Next Steps