gazebo仿真系列之plugin三

本文详细介绍了如何在Gazebo仿真环境中通过Plugin动态插入模型,包括利用GAZEBO_MODEL_PATH环境变量加载模型、通过字符串数据加载和使用消息传递机制插入模型。还讲解了创建模型所需的SDF和config文件,以及如何在world文件中插入Plugin。最后,提供了官方例程代码以供参考。
摘要由CSDN通过智能技术生成

官方教程:http://gazebosim.org/tutorials?tut=plugins_world&cat=write_plugin
本系列关注的 是如何在仿真的过程中加入已经生成好的模型,就是仿真实际情况下物体出现的随机性。仿真无人机在实际过程中遇到 突然出现的障碍物的反应和路径规划等。

《一》模型的插入方法
模型有三种方法在plugin中插入到仿真环境:
其一:利用gazebo环境下的模型,这一要保证The filename must be in the GAZEBO_MODEL_PATH environment variable(官网:The first method uses a World method to load a model based on a file in the resource path defined by the GAZEBO_MODEL_PATH environment variable.)

_parent->InsertModelFile("model://box");

其二:将模型定义成字符串类型,通过函数的调用(官网:The second method uses a World method to load a model based on string data.

):

sdf::SDF sphereSDF;
    sphereSDF.SetFromString(
       "<sdf version ='1.4'>\
          <model name ='sphere'>\
            <pose>1 0 0 0 0 0</pose>\
            <link name ='link'>\
              <pose>0 0 .5 0 0 0</pose>\
              <collision name ='collision'>\
                <geometry>\
                  <sphere><radius>0.5</radius></sphere>\
                </geometry>\
              </collision>\
              <visual name ='visual'>\
                <geometry>\
                  <sphere><radius>0.5</radius></sphere>\
                </geometry>\
              </visual>\
            </link>\
          </model>\
        </sdf>");
    // Demonstrate using a custom model name.
    sdf::ElementPtr model = sphereSDF.Root()->GetElement("model");
    model->GetAttribute("name")->SetFromString("unique_sphere");
    _parent->InsertModelSDF(sphereSDF);

其三:通过消息传送机制(官网:The third method uses the message passing mechanism to insert a model. This method is most useful for stand alone applications that communicate with Gazebo over a network connection.)

  {
      // Create a new transport node
      transport::NodePtr node(new transport::Node());

      // Initia
搭建gazebo仿真环境需要以下步骤: 1. 安装ROS:在Ubuntu上安装ROS,可以使用ROS官方文档提供的指南。 2. 安装gazebo:可以使用以下命令在Ubuntu上安装gazebo: ``` sudo apt-get install gazebo ``` 3. 创建ROS工作空间:在ROS中,每个项目都是在一个工作空间中进行开发和管理的。可以使用以下命令创建一个新的ROS工作空间: ``` mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catkin_make ``` 4. 下载gazebo模型:可以从gazebo官方网站或其他第方网站下载gazebo模型,如TurtleBot、PR2等。将下载的模型放在工作空间的`src`目录下。 5. 安装ROS和gazebo的连接库:在Ubuntu上安装ROS和gazebo的连接库,可以使用以下命令: ``` sudo apt-get install ros-kinetic-gazebo-ros-pkgs ros-kinetic-gazebo-ros-control ``` 其中,`kinetic`是ROS的版本号,可以根据实际情况进行替换。 6. 编写gazebo仿真文件:在工作空间的`src`目录下创建一个新的package(包),并在其中编写gazebo仿真文件,例如: ``` roscd cd ../src catkin_create_pkg my_robot_gazebo cd my_robot_gazebo mkdir launch cd launch touch my_robot_world.launch ``` 然后使用文本编辑器打开`my_robot_world.launch`文件,并编写gazebo仿真文件的内容,例如: ``` <launch> <arg name="world_file" default="$(find my_robot_gazebo)/worlds/my_robot_world.world"/> <arg name="paused" default="false"/> <arg name="use_sim_time" default="true"/> <arg name="gui" default="true"/> <param name="robot_description" command="$(find xacro)/xacro --inorder $(find my_robot_description)/urdf/my_robot.urdf.xacro"/> <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen" args="-urdf -model my_robot -param robot_description -x 0 -y 0 -z 0"/> <node name="gazebo_gui" pkg="gazebo" type="gui" respawn="false" output="screen"/> <node name="gazebo" pkg="gazebo_ros" type="gazebo" respawn="false" output="screen"> <env name="GAZEBO_MASTER_URI" value="$(arg gazebo_master_uri)"/> <env name="GAZEBO_MODEL_PATH" value="$(arg gazebo_model_path)"/> <env name="GAZEBO_PLUGIN_PATH" value="$(arg gazebo_plugin_path)"/> <env name="GAZEBO_RESOURCE_PATH" value="$(arg gazebo_resource_path)"/> </node> </launch> ``` 其中,`my_robot.urdf.xacro`是机器人模型的描述文件,放在`my_robot_description/urdf`目录下。 7. 启动gazebo仿真环境:使用以下命令启动gazebo仿真环境: ``` cd ~/catkin_ws/ source devel/setup.bash roslaunch my_robot_gazebo my_robot_world.launch ``` 然后就可以在gazebo仿真环境中模拟机器人的运动和传感器数据了。 以上是搭建gazebo仿真环境的基本步骤,具体的实现和开发过程需要根据实际情况进行调整。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值