Gazebo 入门操作--插件使用

Gazebo插件概述

插件是一大块代码,编译为共享库并插入到模拟中.

插件类型

 Model插件附加到Gazebo并控制其中的特定模型。同样,World插件附加到一个世界,Sensor插件附加到特定传感器。系统插件在命令行中指定,并在Gazebo启动期间首先加载.启动过程中,应根据所需功能来选择插件.

使用步骤

1 安装gazebo

 

  1. 世界
  2. 模型
  3. 传感器
  4. 系统
  5. 视觉
  6. GUI
sudo apt-get install libgazebo7-dev

2  创建目录

$ mkdir ~/gazebo_plugin_tutorial

3 cd到目录下,然后创建插件的.cc文件

$ cd ~/gazebo_plugin_tutorial
$ gedit hello_world.cc
(这里hello_world可以任意更改)

4编写插件的.cc文件,我这里是hello_world.cc 文件

#include <gazebo/gazebo.hh>

namespace gazebo //命名空间
{
  class WorldPluginTutorial : public WorldPlugin //每个插件都必须从插件类型继承,在本例中是插件类型WorldPlugin
  {
    public: WorldPluginTutorial() : WorldPlugin()
            {
              printf("Hello World!\n");
            }

    public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf) //唯一的另一个强制函数是Load接收包含加载的SDF文件中指定的元素和属性的SDF元素。
            {
            }
  };
  GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial) //最后,必须使用GZ_REGISTER_WORLD_PLUGIN宏在模拟器中注册插件 
}

 5 编译插件

5.1创建一个CMakeList.txt文件

$ gedit ~/gazebo_plugin_tutorial/CMakeLists.txt

内容如下

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

find_package(gazebo REQUIRED)
include_directories(${GAZEBO_INCLUDE_DIRS})
link_directories(${GAZEBO_LIBRARY_DIRS})
list(APPEND CMAKE_CXX_FLAGS "${GAZEBO_CXX_FLAGS}")

add_library(hello_world SHARED hello_world.cc) //这句是把.cc文件编译成.so的库
target_link_libraries(hello_world ${GAZEBO_LIBRARIES})//把它链接到gazebo 中

5.2编译

$ mkdir ~/gazebo_plugin_tutorial/build
$ cd ~/gazebo_plugin_tutorial/build
$ cmake ..
$ make

6添加路径

$ export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:~/gazebo_plugin_tutorial/build

7使用插件

7.1建立一个世界文件

$ gedit ~/gazebo_plugin_tutorial/hello.world

内容如下

<?xml version="1.0"?>
<sdf version="1.4">
  <world name="default">
    <plugin name="hello_world" filename="libhello_world.so"/>
  </world>
</sdf>

7.2 打开他它

$ gzserver ~/gazebo_plugin_tutorial/hello.world --verbose

结果如图:

Gazebo multi-robot simulator, version 6.1.0
Copyright (C) 2012-2015 Open Source Robotics Foundation.
Released under the Apache 2 License.
http://gazebosim.org

[Msg] Waiting for master.
[Msg] Connected to gazebo master @ http://127.0.0.1:11345
[Msg] Publicized address: 172.23.1.52
Hello World!

 案例二

$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.cc

.cc文件内容如下

#include <functional>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <ignition/math/Vector3.hh>

namespace gazebo
{
  class ModelPush : public ModelPlugin
  {
    public: void Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
    {
      // Store the pointer to the model
      this->model = _parent;

      // Listen to the update event. This event is broadcast every
      // simulation iteration.
      this->updateConnection = event::Events::ConnectWorldUpdateBegin(
          std::bind(&ModelPush::OnUpdate, this));
    }

    // Called by the world update start event
    public: void OnUpdate()
    {
      // Apply a small linear velocity to the model.
      this->model->SetLinearVel(ignition::math::Vector3d(.3, 0, 0));
    }

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

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

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

CMakeList.txt文件如下

 

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

find_package(gazebo REQUIRED)
include_directories(${GAZEBO_INCLUDE_DIRS})
link_directories(${GAZEBO_LIBRARY_DIRS})
list(APPEND CMAKE_CXX_FLAGS "${GAZEBO_CXX_FLAGS}")

add_library(model_push SHARED model_push.cc)
target_link_libraries(model_push ${GAZEBO_LIBRARIES})

编译

 

$ cd ~/gazebo_plugin_tutorial/build
$ cmake ../
$ make

 构建世界文件

$ cd ~/gazebo_plugin_tutorial
$ gedit model_push.world

内容如下

<?xml version="1.0"?> 
<sdf version="1.4">
  <world name="default">

    <!-- Ground Plane -->
    <include>
      <uri>model://ground_plane</uri>
    </include>

    <include>
      <uri>model://sun</uri>
    </include>

    <model name="box">
      <pose>0 0 0.5 0 0 0</pose>
      <link name="link">
        <collision name="collision">
          <geometry>
            <box>
              <size>1 1 1</size>
            </box>
          </geometry>
        </collision>

        <visual name="visual">
          <geometry>
            <box>
              <size>1 1 1</size>
            </box>
          </geometry>
        </visual>
      </link>

      <plugin name="model_push" filename="libmodel_push.so"/>
    </model>        
  </world>
</sdf>

运行

$ cd ~/gazebo_plugin_tutorial/
$ gzserver -u model_push.world

该选项使服务器处于暂停状态。-u

在一个单独的终端,启动gazebo,单击窗口中的播放按钮以实现模拟,您应该看到框移动。

 

$ gzclient

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值