Tutorials/1.3/control robot/mobile base


 * 1) Tutorial: Controlling a Mobile Robot#


 * Prerequisites:** Building a Mobile Base

This tutorial describes the process of writing a model plugin based controller for a wheeled mobile robot to dynamically manipulate the robot model built in the previous tutorial.


 * 1) Create a model plugin for your robot

For this exercise, we'll create a model plugin to be loaded by and to control the mobile base created.


 * 1) Create a model plugin

Follow steps below to create a model plugin for your mobile base model.


 * 1) Setup your workspace

mkdir ~/my_plugin;cd ~/my_plugin


 * 1) Setup cmake

Create a file named `~/my_plugin/CMakeLists.txt`

gedit ~/my_plugin/CMakeLists.txt

Copy this content into it:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

include (FindPkgConfig) if (PKG_CONFIG_FOUND) pkg_check_modules(GAZEBO gazebo REQUIRED) else message(FATAL_ERROR "pkg-config is required; please install it") endif include_directories(${GAZEBO_INCLUDE_DIRS}) link_directories(${GAZEBO_LIBRARY_DIRS})
 * 1) find gazebo

add_library(my_plugin SHARED my_plugin.cc) target_link_libraries(my_plugin ${GAZEBO_LIBRARIES})
 * 1) compile plugin


 * 1) Create your plugin code

Create a file named `~/my_plugin/my_plugin.cc`. This plugin code overrides the Load method to keep a reference to the model and the two wheel hinges. It then creates a callback tied to the world update start event. During the update, a force of 0.2 Newton is injected in each hinge, causing the robot to spin. gedit my_plugin.cc


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 

namespace gazebo {    class MobileBasePlugin : public ModelPlugin {   public: void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf) {

// Store the pointer to the model this->model = _parent;

// Load parameters for this plugin if (this->LoadParams(_sdf)) {       // Listen to the update event. This event is broadcast every // simulation iteration. this->updateConnection = event::Events::ConnectWorldUpdateStart(           boost::bind(&MobileBasePlugin::OnUpdate, this)); }   }

public: bool LoadParams(sdf::ElementPtr _sdf) {     if (this->FindJointByParam(_sdf, this->left_wheel_joint_, "left_wheel_hinge") &&         this->FindJointByParam(_sdf, this->right_wheel_joint_, "right_wheel_hinge")) return true; else return false; }

public: bool FindJointByParam(sdf::ElementPtr _sdf,                                 physics::JointPtr &_joint,                                  std::string _param) {     if (!_sdf->HasElement(_param)) {       gzerr << "param [" << _param << "] not found\n"; return false; }     else {       _joint = this->model->GetJoint(          _sdf->GetElement(_param)->GetValueString);

if (!_joint) {         gzerr << "joint by name [" << _sdf->GetElement(_param)->GetValueString << "] not found in model\n"; return false; }     }      return true; }

// Called by the world update start event public: void OnUpdate {     this->left_wheel_joint_->SetForce(0, 0.2); this->right_wheel_joint_->SetForce(0, -0.2); }

// Pointer to the model private: physics::ModelPtr model;

// Pointer to the update event connection private: event::ConnectionPtr updateConnection;

private: physics::JointPtr left_wheel_joint_; private: physics::JointPtr right_wheel_joint_; };

// Register this plugin with the simulator GZ_REGISTER_MODEL_PLUGIN(MobileBasePlugin) }


 * 1) Compiling your pluign

mkdir ~/my_plugin/build; cd ~/my_plugin/build cmake ..; make


 * 1) Edit your model to include the model plugin

Open the robot model file `~/.gazebo/models/my_robot/model.sdf` (created by previous tutorial).

gedit ~/.gazebo/models/my_robot/model.sdf Add the following lines directly before the tag near the end of the file:  left_wheel_hinge right_wheel_hinge


 * 1) Running the simulation

Setup your gazebo plugin paths so it contains: cd ~/my_plugin/build export GAZEBO_PLUGIN_PATH=`pwd`:$GAZEBO_PLUGIN_PATH

Start gazebo (restart if it's already running) in the same terminal the you ran the `export GAZEBO_PLUGIN_PATH` from.

gazebo

Insert `my_robot`... You should see the robot spin.


 * 1) Next

Next: Controlling a Mobile Robot with its Range Sensor