This tutorial describes the steps needed to simulate in Gazebo a simple pendulum controlled through a Simulink model built using the WB-Toolbox.
Before moving to the core of the tutorial, please check that you installed the needed dependecies, which are:
- Gazebo
- Matlab/Simulink
- robotology-superbuild with the options
ROBOTOLOGY_ENABLE_CORE,ROBOTOLOGY_USES_GAZEBOandROBOTOLOGY_ENABLE_DYNAMICSenabled
A description on how to set-up your environment is provided in Setting-up your environment. If your environment is already set-up correctly, move directly to Running the simulation.
If you installed robotology-superbuild, you should have some sample models in the directory GazeboYARPPlugins/tutorial/model/.
To install these models in Gazebo, append the directory GazeboYARPPlugins/tutorial/model/ to the GAZEBO_MODEL_PATH environment variable by adding the following lines to the .bashrc:
export PENDULUM_PATH=${ROBOTOLOGY_SUPERBUILD_ROOT}/robotology/GazeboYARPPlugins/tutorial/model
export GAZEBO_MODEL_PATH=${GAZEBO_MODEL_PATH}:${PENDULUM_PATH}Now if you open Gazebo, you can see that the path to the sample models is added in the Insert tab and you can finally use the simple pendulum model in Gazebo.
To be able to interface the simple pendulum model with YARP, you should update the $PATH environment variable by adding the following line to the .bashrc:
export YARP_DATA_DIRS=$YARP_DATA_DIRS:$PENDULUM_PATH/single_pendulumNow you need to let YARP know which is the robot you are going to use by setting the YARP_ROBOT_NAME:
export YARP_ROBOT_NAME=singlePendulumGazebo💡 In case you do not know the YARP_ROBOT_NAME of your robot, check the field robot in the configuration file yarpmotorgui.ini.
By typing yarp resource --find model.urdf in the command line, you can check if YARP finds the correct Gazebo model.
To be able to use WB-Toolbox in Simulink, you need to update the default search path in MATLAB. An easy way to do it is to add the following paths to the pathdef.m file located at Documents/MATLAB:
'$ROBOTOLOGY_SUPERBUILD_INSTALL_PREFIX/share:', ...
'$ROBOTOLOGY_SUPERBUILD_INSTALL_PREFIX/mex:', ...
'$ROBOTOLOGY_SUPERBUILD_INSTALL_PREFIX/share/WBToolbox:', ...
'$ROBOTOLOGY_SUPERBUILD_INSTALL_PREFIX/share/WBToolbox/cmake:', ...
'$ROBOTOLOGY_SUPERBUILD_INSTALL_PREFIX/share/WBToolbox/images:', ...This process can be done automatically by running once a startup matlab script that is installed inside the $WBTOOLBOX_INSTALL_PREFIX if WB-Toolbox is compiled outside the robotology-superbuild, or inside the $ROBOTOLOGY_SUPERBUILD_INSTALL_PREFIX if WB-Toolbox is installed through the robotology-superbuild.
Documents/MATLAB to be able to access the WB-Toolbox (it is raccomended to make an alias for simplicity — see WB-Toolbox troubleshooting).
STEP 0) Set YARP_ROBOT_NAME=singlePendulumGazebo in the .bashrc.
STEP 1) In a first terminal, run a YARP server by typing the command:
yarp serverSTEP 2) In a second terminal, start Gazebo with the clock option enabled to sychronize it with YARP:
gazebo -slibgazebo_yarp_clock.soNow you can add the simple pendulum model in Gazebo (download the model from this link).
STEP 3) In a third terminal, run the command:
yarp name listto retrieve the control board name corresponding to the model in Gazebo. For the simple pendulum, it should be singlePendulumGazebo.
STEP 4) Now you can open Simulink and start to construct your controller using WB-Toolbox. The building blocks needed to build the controller are:
Set Referencesto control the pendulum either in position, velocity or torque.Get measurementto read the position, velocity or torque of the model used in Gazebo.Simulator Synchronizerto set the control period (e.g. 0.01).Configurationto actually connect the Simulink model to the Gazebo model. Change the parametersRobot Name,Controlled JointsandControl Board Namesaccording to the model used in Gazebo. For the simple pendulum, these parameters should be set tosinglePendulumGazebo,jointandbody(seeyarpmotorgui.iniforRobot NameandControl Board Names, while seeconf/gazebo_controlboard.iniforControlled Joints).
After connecting the needed building blocks, the resulting controller should look like this (see pendulum_tutorial.mdl.
To make the controller work, note that you have to:
- save the Simulink model as
.mdlfile. - run the Simulink model using the
discrete (no continuous states)solver inFixed-step. It is recommended to set the simulation step size equal to0.01.
STEP 5) Finally, you can run the Simulink model.




