教程代码

First step with gazebo and ros

• setup a ROS workspace

• create projects for your simulated robot

• create a Gazebo world

• create your own robot model

• connect your robot model to ROS

• use a teleoperation node to control your robot

• add a camera to your robot

• use Rviz to vizualize all the robot information

Setup a new workspace

  1. mkdir -p ~/catkin_ws/src
  2. cd ~/catkin_ws/src
  3. catkin_init_workspace
  4. cd ..
  5. catkin_make
  6. source ~/catkin_ws/devel/setup.bash

Create projects for your simulated robot

  1. cd ~/catkin_ws/src
  2. catkin_create_pkg mybot_gazebo gazebo_ros
  3. catkin_create_pkg mybot_description
  4. catkin_create_pkg mybot_control

Creating your own World

  1. roscd mybot_gazebo
  2. mkdir launch worlds
  3. cd worlds
  4. vim mybot.world

A basic world file defines at least a name:

  1. <?xml version="1.0"?>
  2. <sdf version="1.4">
  3. <world name="myworld">
  4. </world>
  5. </sdf>

At first we just want to add some basic objects, like a ground and a basic illumination source inside the world tag.

  1. <include>
  2. <uri>model://sun</uri>
  3. </include>
  4. <include>
  5. <uri>model://ground_plane</uri>
  6. </include>
  7. <include>
  8. <uri>model://turtlebot</uri>
  9. </include>
  1. roscd mybot_gazebo/launch
  2. vim mybot_world.launch

mybot_world.launch

  1. <launch>
  2. <include file="$(find gazebo_ros)/launch/empty_world.launch">
  3. <arg name="world_name" value="$(find mybot_gazebo)/worlds/mybot.world"/>
  4. <arg name="gui" value="true"/>
  5. </include>
  6. </launch>

make a test

  1. roslaunch mybot_gazebo mybot_world.launch

Creating your own Model

  1. roscd mybot_description
  2. mkdir urdf
  3. cd urdf
  4. gedit mybot.xacro
XACRO CONCEPTS

• xacro:include: Import the content from other file. We can divide the content in different xacros and merge them using xacro:include.

• property: Useful to define constant values. Use it later using ${property_name}

• xacro:macro: Macro with variable values. Later, we can use this macro from another xacro file, and we specify the required value for the variables. To use a macro, you have to include the file where the macro is, and call it using the macro's name and filling

the required values.

mybot.xacro

This file will be the main description of our robot. Let's put some xacro basic structure:

  1. <?xml version="1.0"?>
  2. <robot name="mybot" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <!-- Put here the robot description -->
  4. </robot>

Let's define some physical properties for our robot, mainly the dimensions of the chassis, the caster wheel, the wheels and the camera:

  1. <xacro:property name="PI" value="3.1415926535897931"/>
  2. <xacro:property name="chassisHeight" value="0.1"/>
  3. <xacro:property name="chassisLength" value="0.4"/>
  4. <xacro:property name="chassisWidth" value="0.2"/>
  5. <xacro:property name="chassisMass" value="50"/>
  6. <xacro:property name="casterRadius" value="0.05"/>
  7. <xacro:property name="casterMass" value="5"/>
  8. <xacro:property name="wheelWidth" value="0.05"/>
  9. <xacro:property name="wheelRadius" value="0.1"/>
  10. <xacro:property name="wheelPos" value="0.2"/>
  11. <xacro:property name="wheelMass" value="5"/>
  12. <xacro:property name="cameraSize" value="0.05"/>
  13. <xacro:property name="cameraMass" value="0.1"/>

We will also include three files :

  1. <xacro:include filename="$(find mybot_description)/urdf/mybot.gazebo" />
  2. <xacro:include filename="$(find mybot_description)/urdf/materials.xacro" />
  3. <xacro:include filename="$(find mybot_description)/urdf/macros.xacro" />

These three correspond respectively to:

• all the gazebo-specific aspects of our robot

• definition of the materials used (mostly colors)

• definitions of some macros for easier description of the robot

Add it to mybot.xacro

  1. <link name='chassis'>
  2. <collision>
  3. <origin xyz="0 0 ${wheelRadius}" rpy="0 0 0"/>
  4. <geometry>
  5. <box size="${chassisLength} ${chassisWidth} ${chassisHeight}"/>
  6. </geometry>
  7. </collision>
  8. <visual>
  9. <origin xyz="0 0 ${wheelRadius}" rpy="0 0 0"/>
  10. <geometry>
  11. <box size="${chassisLength} ${chassisWidth} ${chassisHeight}"/>
  12. </geometry>
  13. <material name="orange"/>
  14. </visual>
  15. <inertial>
  16. <origin xyz="0 0 ${wheelRadius}" rpy="0 0 0"/>
  17. <mass value="${chassisMass}"/>
  18. <box_inertia m="${chassisMass}" x="${chassisLength}" y="${chassisWidth}" z="${chassisHeight}"/>
  19. </inertial>
  20. </link>

Add it to mybot.gazebo

  1. <gazebo reference="chassis">
  2. <material>Gazebo/Orange</material>
  3. </gazebo>

Add this in the “materials.xacro” :

  1. <?xml version="1.0"?>
  2. <robot>
  3. <material name="black">
  4. <color rgba="0.0 0.0 0.0 1.0"/>
  5. </material>
  6. <material name="blue">
  7. <color rgba="0.0 0.0 0.8 1.0"/>
  8. </material>
  9. <material name="green">
  10. <color rgba="0.0 0.8 0.0 1.0"/>
  11. </material>
  12. <material name="grey">
  13. <color rgba="0.2 0.2 0.2 1.0"/>
  14. </material>
  15. <material name="orange">
  16. <color rgba="${255/255} ${108/255} ${10/255} 1.0"/>
  17. </material>
  18. <material name="brown">
  19. <color rgba="${222/255} ${207/255} ${195/255} 1.0"/>
  20. </material>
  21. <material name="red">
  22. <color rgba="0.8 0.0 0.0 1.0"/>
  23. </material>
  24. <material name="white">
  25. <color rgba="1.0 1.0 1.0 1.0"/>
  26. </material>
  27. </robot>

Add this in the macros.xacro file, within the robot tag

  1. <?xml version="1.0" ?>
  2. <robot name="mybot" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <macro name="cylinder_inertia" params="m r h">
  4. <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"
  5. iyy="${m*(3*r*r+h*h)/12}" iyz = "0"
  6. izz="${m*r*r/2}"
  7. />
  8. </macro>
  9. <macro name="box_inertia" params="m x y z">
  10. <inertia ixx="${m*(y*y+z*z)/12}" ixy = "0" ixz = "0"
  11. iyy="${m*(x*x+z*z)/12}" iyz = "0"
  12. izz="${m*(x*x+z*z)/12}"
  13. />
  14. </macro>
  15. <macro name="sphere_inertia" params="m r">
  16. <inertia ixx="${2*m*r*r/5}" ixy = "0" ixz = "0"
  17. iyy="${2*m*r*r/5}" iyz = "0"
  18. izz="${2*m*r*r/5}"
  19. />
  20. </macro>
  21. </robot>

Add this before the chassis link in the mybot.xacro file :

  1. <link name="footprint" />
  2. <joint name="base_joint" type="fixed">
  3. <parent link="footprint"/>
  4. <child link="chassis"/>
  5. </joint>

mybot_world.launch by adding the following two tags in the launch tag:

  1. <!-- urdf xml robot description loaded on the Parameter Server, converting the xacro into a proper urdf file-->
  2. <param name="robot_description" command="$(find xacro)/xacro.py '$(find mybot_description)/urdf/mybot.xacro'" />
  3. <!-- push robot_description to factory and spawn robot in gazebo -->
  4. <node name="mybot_spawn" pkg="gazebo_ros" type="spawn_model" output="screen"
  5. args="-urdf -param robot_description -model mybot" />

make a test

  1. roslaunch mybot_gazebo mybot_world.launch

mybot.xacro As a next step we add a caster wheel to the robot. This is the simplest wheel as we have no axis and no friction

  1. <joint name="fixed" type="fixed">
  2. <parent link="chassis"/>
  3. <child link="caster_wheel"/>
  4. </joint>
  5. <link name="caster_wheel">
  6. <collision>
  7. <origin xyz="${casterRadius-chassisLength/2} 0 ${casterRadius-chassisHeight+wheelRadius}" rpy="0 0 0"/>
  8. <geometry>
  9. <sphere radius="${casterRadius}"/>
  10. </geometry>
  11. </collision>
  12. <visual>
  13. <origin xyz="${casterRadius-chassisLength/2} 0 ${casterRadius-chassisHeight+wheelRadius}" rpy="0 0 0"/>
  14. <geometry>
  15. <sphere radius="${casterRadius}"/>
  16. </geometry>
  17. <material name="red"/>
  18. </visual>
  19. <inertial>
  20. <origin xyz="${casterRadius-chassisLength/2} 0 ${casterRadius-chassisHeight+wheelRadius}" rpy="0 0 0"/>
  21. <mass value="${casterMass}"/>
  22. <sphere_inertia m="${casterMass}" r="${casterRadius}"/>
  23. </inertial>
  24. </link>

mybot.gazebo Add a gazebo tag in the gazebo file for this link :

  1. <gazebo reference="caster_wheel">
  2. <mu1>0.0</mu1>
  3. <mu2>0.0</mu2>
  4. <material>Gazebo/Red</material>
  5. </gazebo>

As usual, we specify the color used in material. We also added mu1 and mu2, with value 0 to remove the friction.

macros.xacro .We could add the two links in the main file, but let's make one macro to make it simple.

  1. <macro name="wheel" params="lr tY">
  2. <link name="${lr}_wheel">
  3. <collision>
  4. <origin xyz="0 0 0" rpy="0 ${PI/2} ${PI/2}" />
  5. <geometry>
  6. <cylinder length="${wheelWidth}" radius="${wheelRadius}"/>
  7. </geometry>
  8. </collision>
  9. <visual>
  10. <origin xyz="0 0 0" rpy="0 ${PI/2} ${PI/2}" />
  11. <geometry>
  12. <cylinder length="${wheelWidth}" radius="${wheelRadius}"/>
  13. </geometry>
  14. <material name="black"/>
  15. </visual>
  16. <inertial>
  17. <origin xyz="0 0 0" rpy="0 ${PI/2} ${PI/2}" />
  18. <mass value="${wheelMass}"/>
  19. <cylinder_inertia m="${wheelMass}" r="${wheelRadius}" h="${wheelWidth}"/>
  20. </inertial>
  21. </link>
  22. <gazebo reference="${lr}_wheel">
  23. <mu1 value="1.0"/>
  24. <mu2 value="1.0"/>
  25. <kp value="10000000.0" />
  26. <kd value="1.0" />
  27. <fdir1 value="1 0 0"/>
  28. <material>Gazebo/Black</material>
  29. </gazebo>
  30. <joint name="${lr}_wheel_hinge" type="continuous">
  31. <parent link="chassis"/>
  32. <child link="${lr}_wheel"/>
  33. <origin xyz="${-wheelPos+chassisLength/2} ${tY*wheelWidth/2+tY*chassisWidth/2} ${wheelRadius}" rpy="0 0 0" />
  34. <axis xyz="0 1 0" rpy="0 0 0" />
  35. <limit effort="100" velocity="100"/>
  36. <joint_properties damping="0.0" friction="0.0"/>
  37. </joint>
  38. <transmission name="${lr}_trans">
  39. <type>transmission_interface/SimpleTransmission</type>
  40. <joint name="${lr}_wheel_hinge">
  41. <hardwareInterface>EffortJointInterface</hardwareInterface>
  42. </joint>
  43. <actuator name="${lr}Motor">
  44. <hardwareInterface>EffortJointInterface</hardwareInterface>
  45. <mechanicalReduction>10</mechanicalReduction>
  46. </actuator>
  47. </transmission>
  48. </macro>

mybot.xacro

  1. <wheel lr="left" tY="1"/>
  2. <wheel lr="right" tY="-1"/>

Connect your robot to ROS

Alright, our robot is all nice and has this new car smell, but we can't do anything with it yet as it has no connection with

ROS In order to add this connection we need to add gazebeo plugins to our model. There are different kinds of plugins:

  • World: Dynamic changes to the world, e.g. Physics, like illumination or gravity, inserting models
  • Model: Manipulation of models (robots), e.g. move the robots
  • Sensor: Feedback from virtual sensor, like camera, laser scanner
  • System: Plugins that are loaded by the GUI, like saving images

First of all we'll use a plugin to provide access to the joints of the wheels. The transmission tags in our URDF will be used by this plugin the define how to link the joints to controllers. To activate the plugin, add the following to mybot.gazebo:

  1. <gazebo>
  2. <plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
  3. <robotNamespace>/mybot</robotNamespace>
  4. </plugin>
  5. </gazebo>

With this plugin, we will be able to control the joints, however we need to provide some extra configuration and explicitely

start controllers for the joints. In order to do so, we'll use the package mybot_control that we have defined before. Let's first create the configuration file:

  1. roscd mybot_control
  2. mkdir config
  3. cd config
  4. vim mybot_control.yaml

This file will define three controllers: one for each wheel, connections to the joint by the transmission tag, one for

publishing the joint states. It also defined the PID gains to use for this controller:

  1. mybot:
  2. # Publish all joint states -----------------------------------
  3. joint_state_controller:
  4. type: joint_state_controller/JointStateController
  5. publish_rate: 50
  6. # Effort Controllers ---------------------------------------
  7. leftWheel_effort_controller:
  8. type: effort_controllers/JointEffortController
  9. joint: left_wheel_hinge
  10. pid: {p: 100.0, i: 0.1, d: 10.0}
  11. rightWheel_effort_controller:
  12. type: effort_controllers/JointEffortController
  13. joint: right_wheel_hinge
  14. pid: {p: 100.0, i: 0.1, d: 10.0}

Now we need to create a launch file to start the controllers. For this let's do:

  1. roscd mybot_control
  2. mkdir launch
  3. cd launch
  4. vim mybot_control.launch

In this file we'll put two things. First we'll load the configuration and the controllers, and we'll also start a node that will

provide 3D transforms (tf) of our robot. This is not mandatory but that makes the simulation more complete

  1. <launch>
  2. <!-- Load joint controller configurations from YAML file to parameter server -->
  3. <rosparam file="$(find mybot_control)/config/mybot_control.yaml" command="load"/>
  4. <!-- load the controllers -->
  5. <node name="controller_spawner"
  6. pkg="controller_manager"
  7. type="spawner" respawn="false"
  8. output="screen" ns="/mybot"
  9. args="joint_state_controller
  10. rightWheel_effort_controller
  11. leftWheel_effort_controller"/>
  12. <!-- convert joint states to TF transforms for rviz, etc -->
  13. <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" respawn="false" output="screen">
  14. <param name="robot_description" command="$(find xacro)/xacro.py '$(find mybot_description)/urdf/mybot.xacro'" />
  15. <remap from="/joint_states" to="/mybot/joint_states" />
  16. </node>
  17. </launch>

make a test

  1. roslaunch mybot_gazebo mybot_world.launch
  2. roslaunch mybot_control mybot.launch
  3. rostopic list

We could launch our model on gazebo and then launch the controller, but to save some time (and terminals), we'll start the controllers automatically by adding a line to the mybot_world.launch in the mybot_gazebo package :

  1. <!-- ros_control mybot launch file -->
  2. <include file="$(find mybot_control)/launch/mybot_control.launch" />
  1. rostopic pub -1 /mybot/leftWheel_effort_controller/command std_msgs/Float64 "data: 1.5"
  2. rostopic pub -1 /mybot/rightWheel_effort_controller/command std_msgs/Float64 "data: 1.0"
  3. rostopic echo /mybot/joint_states

Teleoperation of your robot

Adding a camera

  1. <joint name="camera_joint" type="fixed">
  2. <axis xyz="0 1 0" />
  3. <origin xyz="0 0 0.2" rpy="0 0 0"/>
  4. <parent link="footprint"/>
  5. <child link="camera"/>
  6. </joint>
  7. <link name="camera">
  8. <collision>
  9. <origin xyz="0 0 0" rpy="0 0 0"/>
  10. <geometry>
  11. <box size="${cameraSize} ${cameraSize} ${cameraSize}"/>
  12. </geometry>
  13. </collision>
  14. <visual>
  15. <origin xyz="0 0 0" rpy="0 0 0"/>
  16. <geometry>
  17. <box size="${cameraSize} ${cameraSize} ${cameraSize}"/>
  18. </geometry>
  19. <material name="blue"/>
  20. </visual>
  21. <inertial>
  22. <mass value="${cameraMass}" />
  23. <origin xyz="0 0 0" rpy="0 0 0"/>
  24. <box_inertia m="${cameraMass}" x="${cameraSize}" y="${cameraSize}" z="${cameraSize}" />
  25. </inertial>
  26. </link>

Add the plugin to gazebo file

  1. <gazebo reference="camera">
  2. <material>Gazebo/Blue</material>
  3. <sensor type="camera" name="camera1">
  4. <update_rate>30.0</update_rate>
  5. <camera name="head">
  6. <horizontal_fov>1.3962634</horizontal_fov>
  7. <image>
  8. <width>800</width>
  9. <height>800</height>
  10. <format>R8G8B8</format>
  11. </image>
  12. <clip>
  13. <near>0.02</near>
  14. <far>300</far>
  15. </clip>
  16. </camera>
  17. <plugin name="camera_controller" filename="libgazebo_ros_camera.so">
  18. <alwaysOn>true</alwaysOn>
  19. <updateRate>0.0</updateRate>
  20. <cameraName>mybot/camera1</cameraName>
  21. <imageTopicName>image_raw</imageTopicName>
  22. <cameraInfoTopicName>camera_info</cameraInfoTopicName>
  23. <frameName>camera_link</frameName>
  24. <hackBaseline>0.07</hackBaseline>
  25. <distortionK1>0.0</distortionK1>
  26. <distortionK2>0.0</distortionK2>
  27. <distortionK3>0.0</distortionK3>
  28. <distortionT1>0.0</distortionT1>
  29. <distortionT2>0.0</distortionT2>
  30. </plugin>
  31. </sensor>
  32. </gazebo>
  1. rosrun image_view image_view image:=/mybot/camera1/image_raw

Visualisation with RViz

  1. rosrun rviz rviz

整个代码框架如下:

Gazebo Ros入门的更多相关文章

  1. SLAM+语音机器人DIY系列:(二)ROS入门——1.ROS是什么

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  2. ROS入门学习

    ROS学习笔记 ROS入门网站; ROS入门书籍 ROS主要包含包括功能包.节点.话题.消息类型和服务; ROS功能包/软件包(Packages) ROS软件包是一组用于实现特定功能的相关文件的集合, ...

  3. ROS_Kinetic_03 ROS入门向导

    ROS_Kinetic_03 ROS入门向导 每个人都有不同的学习习惯和爱好并针对不同的应用进行ROS相关设计与开发, 没有固定不变的学习模式,但以下的内容是通常都会用到的. 1. ROS基础教程 1 ...

  4. SLAM+语音机器人DIY系列:(二)ROS入门——2.ROS系统整体架构

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  5. SLAM+语音机器人DIY系列:(二)ROS入门——3.在ubuntu16.04中安装ROS kinetic

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  6. SLAM+语音机器人DIY系列:(二)ROS入门——4.如何编写ROS的第一个程序hello_world

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  7. SLAM+语音机器人DIY系列:(二)ROS入门——5.编写简单的消息发布器和订阅器

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  8. SLAM+语音机器人DIY系列:(二)ROS入门——6.编写简单的service和client

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  9. SLAM+语音机器人DIY系列:(二)ROS入门——7.理解tf的原理

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

随机推荐

  1. HBase-配置说明

    转载自:http://www.aboutyun.com/thread-7914-1-1.html hbase.rootdir这个目录是region  server的共享目录,用来持久化Hbase.UR ...

  2. css笔记04:属性选择器

    1.属性选择器: 带有 title 属性的所有元素设置样式: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  3. gitlab备份与恢复操作方法

    github私有仓库是收费的,有些代码不方便托管到外面的git仓库,因此就产生了自己搭建git服务器的需求. 好在有广大的开源人士的贡献,有了gitlab这一神器. 手动配置较多,直接用集成包: bi ...

  4. jsp无法支持el标签及jstl标签

    在jsp页面头部添加如下 <%@ page isELIgnored="false"%> <%@ taglib uri="http://java.sun. ...

  5. xml版本学生管理系统

    一: 需求描述 学生成绩管理系统,使用xml存储学生信息,可以对学生信息进行增.删.删除操作. 主要目的:练习操作xml元素的增删改查 二:代码结构 1:xml存储数据如下 exam.xml < ...

  6. LeetCode 344

    Reverse String Write a function that takes a string as input and returns the string reversed. Exampl ...

  7. Linux中ifcfg-eth0配置参数解释

    Linux中设置IP地址经常使用到ifcfg-eth0这个文件.  vi /etc/sysconfig/network-scripts/ifcfg-eth0 附录文件中的内容: DEVICE=eth0 ...

  8. Netty线程模型

    一.Reactor模型 1.单线程模型 Reactor单线程模型,指的是所有的IO操作都在同一个NIO线程上面完成,NIO线程的职责如下: 1)作为NIO服务端,接收客户端的TCP连接: 2)作为NI ...

  9. poj 3544 Journey with Pigs

    Journey with Pigs Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3004   Accepted: 922 ...

  10. tomcat 安装

    升级系统之后很长一段时间没有用tomcat(主要是没做东西),这两天要开始干活了,发现竟然没法发用了....ok,重新整一遍.算是温习. 上次所有环境的搭建基本都是师兄帮我,自己做得东西很少,这次就正 ...