This example explores more in-depth interaction with the Gazebo® Simulator from MATLAB®. Topics include creating simple models, adding links and joints to models, connecting models together, and applying forces to bodies.
On your Linux® machine, start Gazebo. If you are using the virtual machine from Get Started with Gazebo and a Simulated TurtleBot, use the Gazebo Empty world.
Initialize ROS by replacing the sample IP address (192.168.1.1) with the IP address of the virtual machine. Create an instance of the
ipaddress = '192.168.1.1'
rosinit(ipaddress) gazebo = ExampleHelperGazeboCommunicator();
To create a model, use the
ExampleHelperGazeboModel class. In the following code, a ball is created. Define properties (using
addLink) and spawn the ball using the
ball = ExampleHelperGazeboModel('Ball') spherelink = addLink(ball,'sphere',1,'color',[0 0 1 1]) spawnModel(gazebo,ball,[8.5,0,1]);
Your output from these commands should look similar to this:
All units for Gazebo commands are specified in SI units. Depending on your view, you might have to zoom out to see the ball, because it is placed at
[8.5, 0, 1]. Here is an image of the scene:
y for the location of the bowling pins (in meters).
x = [1.5 1.5 1.5 1.5 2.5 2.5 2.5 3.5 3.5 4.5]; y = [-1.5 -0.5 0.5 1.5 -1 0 1 -0.5 0.5 0];
Define a basic model for the bowling pin using the
ExampleHelperGazeboModel object. Use
addLink to create the cylinder and the ball.
pin = ExampleHelperGazeboModel('BowlPin'); link1 = addLink(pin,'cylinder',[1 0.2],'position',[0,0,0.5]) link2 = addLink(pin,'sphere',0.2,'position',[0,0, 1.2],'color',[0.7 0 0.2 1])
The output of
addLink produces a variable containing the assigned name of the link. These variables create the joint.
addJoint to define the relationship between the two links. In this case they are attached together by a revolute joint.
joint = addJoint(pin,link1,link2,'revolute',[0 0],[0 0 1])
The arguments of the
addJoint function are object, parent, child, type, limits, and axis.
bowlPin once, You can create all ten bowling pins from the preceding
ExampleHelperGazeboModel. The following
for loop spawns the models in Gazebo using the
for i = 1:10 spawnModel(gazebo,pin,[x(i),y(i),0.7]); pause(1); end
After adding the pins to the world, it looks like this figure:
If the TurtleBot® exists in the scene, remove it. Look in the list of models. Remove the one named
if ismember('mobile_base',getSpawnedModels(gazebo)) removeModel(gazebo,'mobile_base'); end
ExampleHelperGazeboModel for a Jersey barrier. The object finds this model on the Gazebo website.
barrier = ExampleHelperGazeboModel('jersey_barrier','gazeboDB');
Spawn two Jersey barriers in the world using
spawnModel(gazebo,barrier,[1.5,-3,0]); % Right barrier pause(1); spawnModel(gazebo,barrier,[1.5,3,0]); % Left barrier
Note: You need an Internet connection to spawn models that are not included in these examples. However, if you have previously spawned a model in your Gazebo simulation, it is cached, so you can spawn it later without an Internet connection.
The image looks like this figure:
Retrieve the handle to the ball through the
spawnedBall = ExampleHelperGazeboSpawnedModel(ball.Name,gazebo)
Define parameters for the application of force. Here the duration is set to one second and the force vector is set to -75 Newtons in the x direction.
duration = 1; % Seconds forcevec = [-75 0 0]; % Newtons
Apply the force to the model using the
applyForce(spawnedBall, spherelink, duration, forcevec); pause(5);
Following are images of the collision and the aftermath
To clean up, delete the models created for this example.
It is good practice to clear the workspace of publishers, subscribers, and other ROS-related objects when you are finished with them
It is recommended to use
once you are done working with the ROS network. Shut down the global node and disconnect from Gazebo.
When you are finished, close the Gazebo window on your virtual machine.
Refer to the next example: Apply Forces and Torques in Gazebo