0


Gazebo——仿真平台搭建(基于Ubuntu20.04)

Gazebo安装配置

1.设置你的电脑来接收软件

  1. sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'

2.设置秘钥

  1. wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -

3.安装Gazebo

  1. sudo apt-get update
  2. sudo apt-get install gazebo11
  3. sudo apt-get install libgazebo11-dev

4.检查你的安装是否有效果=

  1. gazebo

5.打开 /.gazebo文件夹 下载模型

  1. cd ~/.gazebo
  2. git clone https://github.com/osrf/gazebo_models

如果出现fatal连接GitHub失败请使用以下代码

  1. git clone https://gitclone.com/github.com/osrf/gazebo_models

下载时间可能偏久,读者可以选择和我一样另起终端进行其他操作,等到需要使用场景模型的时候大概也已经下载完成。

  1. 6.其他关联安装包(不定时更新)
  1. sudo apt-get install ros-noetic-rviz
  2. sudo apt-get install ros-noetic-robot-state-publisher
  3. sudo apt-get install ros-noetic-gazebo-ros-pkgs ros-noetic-gazebo-ros-control
  4. sudo apt install ros-noetic-moveit

然后运行安装包索引

  1. sudo apt-get update

接下来进行机器人模型的搭建

机器人模型配置

首先进入到mbot_description/urdf/xacro下

  1. cd ~/catkin_ws/src/mbot_description/urdf/xacro
  2. mkdir gazebo
  3. cd gazebo
  4. sudo gedit mbot_base_gazebo.xacro

编辑模型文件

  1. <?xml version="1.0"?>
  2. <robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <!--存放下面相关定义内容-->
  4. </robot>
  5. <!-- PROPERTY LIST -->
  6. <xacro:property name="M_PI" value="3.1415926"/>
  7. <xacro:property name="base_mass" value="20" />
  8. <xacro:property name="base_radius" value="0.20"/>
  9. <xacro:property name="base_length" value="0.16"/>
  10. <xacro:property name="wheel_mass" value="2" />
  11. <xacro:property name="wheel_radius" value="0.06"/>
  12. <xacro:property name="wheel_length" value="0.025"/>
  13. <xacro:property name="wheel_joint_y" value="0.19"/>
  14. <xacro:property name="wheel_joint_z" value="0.05"/>
  15. <xacro:property name="caster_mass" value="0.5" />
  16. <xacro:property name="caster_radius" value="0.015"/> <!-- wheel_radius - ( base_length/2 - wheel_joint_z) -->
  17. <xacro:property name="caster_joint_x" value="0.18"/>
  18. <!-- Defining the colors used in this robot -->
  19. <material name="yellow">
  20. <color rgba="1 0.4 0 1"/>
  21. </material>
  22. <material name="black">
  23. <color rgba="0 0 0 0.95"/>
  24. </material>
  25. <material name="gray">
  26. <color rgba="0.75 0.75 0.75 1"/>
  27. </material>
  28. <!-- Macro for inertia matrix -->
  29. <xacro:macro name="sphere_inertial_matrix" params="m r">
  30. <inertial>
  31. <mass value="${m}" />
  32. <inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0"
  33. iyy="${2*m*r*r/5}" iyz="0"
  34. izz="${2*m*r*r/5}" />
  35. </inertial>
  36. </xacro:macro>
  37. <xacro:macro name="cylinder_inertial_matrix" params="m r h">
  38. <inertial>
  39. <mass value="${m}" />
  40. <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"
  41. iyy="${m*(3*r*r+h*h)/12}" iyz = "0"
  42. izz="${m*r*r/2}" />
  43. </inertial>
  44. </xacro:macro>
  45. <!-- Macro for robot wheel -->
  46. <xacro:macro name="wheel" params="prefix reflect">
  47. <joint name="${prefix}_wheel_joint" type="continuous">
  48. <origin xyz="0 ${reflect*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
  49. <parent link="base_link"/>
  50. <child link="${prefix}_wheel_link"/>
  51. <axis xyz="0 1 0"/>
  52. </joint>
  53. <link name="${prefix}_wheel_link">
  54. <visual>
  55. <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
  56. <geometry>
  57. <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
  58. </geometry>
  59. <material name="gray" />
  60. </visual>
  61. <!-- collision -->
  62. <!-- the same with visual -->
  63. <!--增加惯性属性和碰撞属性-->
  64. <collision>
  65. <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
  66. <geometry>
  67. <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
  68. </geometry>
  69. </collision>
  70. <!-- inertial -->
  71. <cylinder_inertial_matrix m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
  72. </link>
  73. <!--添加gazebo标签为各link配颜色 ,gazebo与rivz颜色设置不兼容-->
  74. <!-- Add gazebo tag to link -->
  75. <gazebo reference="${prefix}_wheel_link">
  76. <material>Gazebo/Gray</material>
  77. </gazebo>
  78. <!--joint添加传动装置,用得 transmission 标签,小车轮子用速度控制接口-->
  79. <!-- Transmission is important to link the joints and the controller -->
  80. <transmission name="${prefix}_wheel_joint_trans">
  81. <type>transmission_interface/SimpleTransmission</type>
  82. <joint name="${prefix}_wheel_joint" >
  83. <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
  84. </joint>
  85. <actuator name="${prefix}_wheel_joint_motor">
  86. <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
  87. <mechanicalReduction>1</mechanicalReduction>
  88. </actuator>
  89. </transmission>
  90. </xacro:macro>
  91. <!-- Macro for robot caster -->
  92. <xacro:macro name="caster" params="prefix reflect">
  93. <joint name="${prefix}_caster_joint" type="continuous">
  94. <origin xyz="${reflect*caster_joint_x} 0 ${-(base_length/2 + caster_radius)}" rpy="0 0 0"/>
  95. <parent link="base_link"/>
  96. <child link="${prefix}_caster_link"/>
  97. <axis xyz="0 1 0"/>
  98. </joint>
  99. <link name="${prefix}_caster_link">
  100. <visual>
  101. <origin xyz="0 0 0" rpy="0 0 0"/>
  102. <geometry>
  103. <sphere radius="${caster_radius}" />
  104. </geometry>
  105. <material name="black" />
  106. </visual>
  107. <!-- 碰撞属性 -->
  108. <collision>
  109. <origin xyz="0 0 0" rpy="0 0 0"/>
  110. <geometry>
  111. <sphere radius="${caster_radius}" />
  112. </geometry>
  113. </collision>
  114. <!-- 惯性属性 -->
  115. <sphere_inertial_matrix m="${caster_mass}" r="${caster_radius}" />
  116. </link>
  117. <!--添加gazebo标签,为各link配颜色-->
  118. <gazebo reference="${prefix}_caster_link">
  119. <material>Gazebo/Black</material>
  120. </gazebo>
  121. </xacro:macro>
  122. <xacro:macro name="mbot_base_gazebo">
  123. <link name="base_footprint">
  124. <visual>
  125. <origin xyz="0 0 0" rpy="0 0 0" />
  126. <geometry>
  127. <box size="0.001 0.001 0.001" />
  128. </geometry>
  129. </visual>
  130. </link>
  131. <!-- 给 base_footprint 添加标签 -->
  132. <gazebo reference="base_footprint">
  133. <turnGravityOff>false</turnGravityOff>
  134. </gazebo>
  135. <joint name="base_footprint_joint" type="fixed">
  136. <origin xyz="0 0 ${base_length/2 + caster_radius*2}" rpy="0 0 0" />
  137. <parent link="base_footprint"/>
  138. <child link="base_link" />
  139. </joint>
  140. <!--base_link添加碰撞属性和惯性属性-->
  141. <link name="base_link">
  142. <visual>
  143. <origin xyz=" 0 0 0" rpy="0 0 0" />
  144. <geometry>
  145. <cylinder length="${base_length}" radius="${base_radius}"/>
  146. </geometry>
  147. <material name="yellow" />
  148. </visual>
  149. <collision>
  150. <origin xyz=" 0 0 0" rpy="0 0 0" />
  151. <geometry>
  152. <cylinder length="${base_length}" radius="${base_radius}"/>
  153. </geometry>
  154. </collision>
  155. <cylinder_inertial_matrix m="${base_mass}" r="${base_radius}" h="${base_length}" />
  156. </link>
  157. <!--base_link添加gazebo标签-->
  158. <gazebo reference="base_link">
  159. <material>Gazebo/Blue</material>
  160. </gazebo>
  161. <wheel prefix="left" reflect="-1"/> <!-- 调用驱动轮子宏定义 -->
  162. <wheel prefix="right" reflect="1"/> <!-- 调用驱动轮子宏定义 -->
  163. <caster prefix="front" reflect="-1"/> <!--调用支撑轮子宏定义-->
  164. <caster prefix="back" reflect="1"/> <!-- 调用支撑轮子宏定义 -->
  165. </xacro:macro>
  166. <!-- controller -->
  167. <gazebo>
  168. <plugin name="differential_drive_controller"
  169. filename="libgazebo_ros_diff_drive.so"> <!-- gazebo提供得差速控制器插件 -->
  170. <!-- 控制器所需参数 -->
  171. <rosDebugLevel>Debug</rosDebugLevel>
  172. <publishWheelTF>true</publishWheelTF>
  173. <robotNamespace>/</robotNamespace><!-- 机器人命名空间 订阅和发布得话题 前面 会加上命名空间 /说明没有添加-->
  174. <publishTf>1</publishTf>
  175. <publishWheelJointState>true</publishWheelJointState>
  176. <alwaysOn>true</alwaysOn>
  177. <updateRate>100.0</updateRate>
  178. <legacyMode>true</legacyMode>
  179. <leftJoint>left_wheel_joint</leftJoint> <!-- 控制得joint在哪里,必须和上面得joint名称一致 -->
  180. <rightJoint>right_wheel_joint</rightJoint><!-- 控制得joint在哪里,必须和上面得joint名称一致 -->
  181. <wheelSeparation>${wheel_joint_y*2}</wheelSeparation><!-- 两个轮子得间距 -->
  182. <wheelDiameter>${2*wheel_radius}</wheelDiameter>
  183. <broadcastTF>1</broadcastTF>
  184. <wheelTorque>30</wheelTorque>
  185. <wheelAcceleration>1.8</wheelAcceleration>
  186. <commandTopic>cmd_vel</commandTopic> <!-- 订阅得话题:速度控制指令 -->
  187. <odometryFrame>odom</odometryFrame>
  188. <odometryTopic>odom</odometryTopic> <!-- 发布里程计信息 -->
  189. <robotBaseFrame>base_footprint</robotBaseFrame><!-- 设置controler所控制的机器人的坐标系是哪个坐标系 -->
  190. </plugin>
  191. </gazebo>

再编辑mbot_gazebo.xacro

  1. <?xml version="1.0"?>
  2. <robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" /> <!-- 包含文件 -->
  4. <mbot_base_gazebo/> <!-- 调用宏定义 -->
  5. </robot>

编辑launch文件

  1. cd ~/catkin_ws/src/mbot_description/launch/xacro
  2. mkdir gazebo
  3. cd gazebo
  4. sudo gedit mbot_base_gazebo.launch
  1. <launch>
  2. <!-- 设置launch文件的参数 -->
  3. <arg name="paused" default="false"/>
  4. <arg name="use_sim_time" default="true"/>
  5. <arg name="gui" default="true"/>
  6. <arg name="headless" default="false"/>
  7. <arg name="debug" default="false"/>
  8. <!-- 运行gazebo仿真环境 -->
  9. <include file="$(find gazebo_ros)/launch/empty_world.launch">
  10. <arg name="debug" value="$(arg debug)" />
  11. <arg name="gui" value="$(arg gui)" />
  12. <arg name="paused" value="$(arg paused)"/>
  13. <arg name="use_sim_time" value="$(arg use_sim_time)"/>
  14. <arg name="headless" value="$(arg headless)"/>
  15. </include>
  16. <!-- 加载机器人模型描述参数 -->
  17. <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" />
  18. <!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
  19. <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
  20. <!-- 运行robot_state_publisher节点,发布tf -->
  21. <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
  22. <param name="publish_frequency" type="double" value="50.0" />
  23. </node>
  24. <!-- 在gazebo中加载机器人模型-->
  25. <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
  26. args="-urdf -model mrobot -param robot_description"/>
  27. </launch>

配置完成后运行显示模型

  1. roslaunch mbot_description mbot_base_gazebo.launch

注意

由于Ubuntu20.04对应的ros版本为noetic,所以xacro模型文件的宏定义和宏调用方式都需要更改

Ubuntu20.04的mbot_base_gazebo.xacro文件对应的内容如下

  1. <?xml version="1.0"?>
  2. <robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <!-- PROPERTY LIST -->
  4. <xacro:property name="M_PI" value="3.1415926"/>
  5. <xacro:property name="base_mass" value="20" />
  6. <xacro:property name="base_radius" value="0.20"/>
  7. <xacro:property name="base_length" value="0.16"/>
  8. <xacro:property name="wheel_mass" value="2" />
  9. <xacro:property name="wheel_radius" value="0.06"/>
  10. <xacro:property name="wheel_length" value="0.025"/>
  11. <xacro:property name="wheel_joint_y" value="0.19"/>
  12. <xacro:property name="wheel_joint_z" value="0.05"/>
  13. <xacro:property name="caster_mass" value="0.5" />
  14. <xacro:property name="caster_radius" value="0.015"/> <!-- wheel_radius - ( base_length/2 - wheel_joint_z) -->
  15. <xacro:property name="caster_joint_x" value="0.18"/>
  16. <!-- Defining the colors used in this robot -->
  17. <material name="yellow">
  18. <color rgba="1 0.4 0 1"/>
  19. </material>
  20. <material name="black">
  21. <color rgba="0 0 0 0.95"/>
  22. </material>
  23. <material name="gray">
  24. <color rgba="0.75 0.75 0.75 1"/>
  25. </material>
  26. <!-- Macro for inertia matrix -->
  27. <xacro:macro name="sphere_inertial_matrix" params="m r">
  28. <inertial>
  29. <mass value="${m}" />
  30. <inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0"
  31. iyy="${2*m*r*r/5}" iyz="0"
  32. izz="${2*m*r*r/5}" />
  33. </inertial>
  34. </xacro:macro>
  35. <xacro:macro name="cylinder_inertial_matrix" params="m r h">
  36. <inertial>
  37. <mass value="${m}" />
  38. <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"
  39. iyy="${m*(3*r*r+h*h)/12}" iyz = "0"
  40. izz="${m*r*r/2}" />
  41. </inertial>
  42. </xacro:macro>
  43. <!-- Macro for robot wheel -->
  44. <xacro:macro name="wheel" params="prefix reflect">
  45. <joint name="${prefix}_wheel_joint" type="continuous">
  46. <origin xyz="0 ${reflect*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
  47. <parent link="base_link"/>
  48. <child link="${prefix}_wheel_link"/>
  49. <axis xyz="0 1 0"/>
  50. </joint>
  51. <link name="${prefix}_wheel_link">
  52. <visual>
  53. <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
  54. <geometry>
  55. <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
  56. </geometry>
  57. <material name="gray" />
  58. </visual>
  59. <collision>
  60. <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
  61. <geometry>
  62. <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
  63. </geometry>
  64. </collision>
  65. <xacro:cylinder_inertial_matrix m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
  66. </link>
  67. <gazebo reference="${prefix}_wheel_link">
  68. <material>Gazebo/Gray</material>
  69. </gazebo>
  70. <!-- Transmission is important to link the joints and the controller -->
  71. <transmission name="${prefix}_wheel_joint_trans">
  72. <type>transmission_interface/SimpleTransmission</type>
  73. <joint name="${prefix}_wheel_joint" >
  74. <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
  75. </joint>
  76. <actuator name="${prefix}_wheel_joint_motor">
  77. <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
  78. <mechanicalReduction>1</mechanicalReduction>
  79. </actuator>
  80. </transmission>
  81. </xacro:macro>
  82. <!-- Macro for robot caster -->
  83. <xacro:macro name="caster" params="prefix reflect">
  84. <joint name="${prefix}_caster_joint" type="continuous">
  85. <origin xyz="${reflect*caster_joint_x} 0 ${-(base_length/2 + caster_radius)}" rpy="0 0 0"/>
  86. <parent link="base_link"/>
  87. <child link="${prefix}_caster_link"/>
  88. <axis xyz="0 1 0"/>
  89. </joint>
  90. <link name="${prefix}_caster_link">
  91. <visual>
  92. <origin xyz="0 0 0" rpy="0 0 0"/>
  93. <geometry>
  94. <sphere radius="${caster_radius}" />
  95. </geometry>
  96. <material name="black" />
  97. </visual>
  98. <collision>
  99. <origin xyz="0 0 0" rpy="0 0 0"/>
  100. <geometry>
  101. <sphere radius="${caster_radius}" />
  102. </geometry>
  103. </collision>
  104. <xacro:sphere_inertial_matrix m="${caster_mass}" r="${caster_radius}" />
  105. </link>
  106. <gazebo reference="${prefix}_caster_link">
  107. <material>Gazebo/Black</material>
  108. </gazebo>
  109. </xacro:macro>
  110. <xacro:macro name="mbot_base_gazebo">
  111. <link name="base_footprint">
  112. <visual>
  113. <origin xyz="0 0 0" rpy="0 0 0" />
  114. <geometry>
  115. <box size="0.001 0.001 0.001" />
  116. </geometry>
  117. </visual>
  118. </link>
  119. <gazebo reference="base_footprint">
  120. <turnGravityOff>false</turnGravityOff>
  121. </gazebo>
  122. <joint name="base_footprint_joint" type="fixed">
  123. <origin xyz="0 0 ${base_length/2 + caster_radius*2}" rpy="0 0 0" />
  124. <parent link="base_footprint"/>
  125. <child link="base_link" />
  126. </joint>
  127. <link name="base_link">
  128. <visual>
  129. <origin xyz=" 0 0 0" rpy="0 0 0" />
  130. <geometry>
  131. <cylinder length="${base_length}" radius="${base_radius}"/>
  132. </geometry>
  133. <material name="yellow" />
  134. </visual>
  135. <collision>
  136. <origin xyz=" 0 0 0" rpy="0 0 0" />
  137. <geometry>
  138. <cylinder length="${base_length}" radius="${base_radius}"/>
  139. </geometry>
  140. </collision>
  141. <xacro:cylinder_inertial_matrix m="${base_mass}" r="${base_radius}" h="${base_length}" />
  142. </link>
  143. <gazebo reference="base_link">
  144. <material>Gazebo/Blue</material>
  145. </gazebo>
  146. <xacro:wheel prefix="left" reflect="-1"/>
  147. <xacro:wheel prefix="right" reflect="1"/>
  148. <xacro:caster prefix="front" reflect="-1"/>
  149. <xacro:caster prefix="back" reflect="1"/>
  150. <!-- controller -->
  151. <gazebo>
  152. <plugin name="differential_drive_controller"
  153. filename="libgazebo_ros_diff_drive.so">
  154. <rosDebugLevel>Debug</rosDebugLevel>
  155. <publishWheelTF>true</publishWheelTF>
  156. <robotNamespace>/</robotNamespace>
  157. <publishTf>1</publishTf>
  158. <publishWheelJointState>true</publishWheelJointState>
  159. <alwaysOn>true</alwaysOn>
  160. <updateRate>100.0</updateRate>
  161. <legacyMode>true</legacyMode>
  162. <leftJoint>left_wheel_joint</leftJoint>
  163. <rightJoint>right_wheel_joint</rightJoint>
  164. <wheelSeparation>${wheel_joint_y*2}</wheelSeparation>
  165. <wheelDiameter>${2*wheel_radius}</wheelDiameter>
  166. <broadcastTF>1</broadcastTF>
  167. <wheelTorque>30</wheelTorque>
  168. <wheelAcceleration>1.8</wheelAcceleration>
  169. <commandTopic>cmd_vel</commandTopic>
  170. <odometryFrame>odom</odometryFrame>
  171. <odometryTopic>odom</odometryTopic>
  172. <robotBaseFrame>base_footprint</robotBaseFrame>
  173. </plugin>
  174. </gazebo>
  175. </xacro:macro>
  176. </robot>

需要更改的地方为

在轮子的宏定义语法以及支撑轮的宏定义语法前添加xacro引用

cylinder_inertial_matrix,

sphere_inertial_matrix

两个个标签引用的时候也需要加上xacro引用

以及mbot_gazebo.xacro文件也需要更改宏定义

  1. <?xml version="1.0"?>
  2. <robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" /> <!-- 包含文件 -->
  4. <xacro:mbot_base_gazebo/> <!-- 调用宏定义 -->
  5. </robot>

launch文件同样需要更改,这里有三个更改方法,一般情况下将

  1. <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" />

改为xacro 即可

也有其他的解决方法例如将其改为

xacro --i

还有

xacro.py

  1. <launch>
  2. <!-- 设置launch文件的参数 -->
  3. <arg name="paused" default="false"/>
  4. <arg name="use_sim_time" default="true"/>
  5. <arg name="gui" default="true"/>
  6. <arg name="headless" default="false"/>
  7. <arg name="debug" default="false"/>
  8. <!-- 运行gazebo仿真环境 -->
  9. <include file="$(find gazebo_ros)/launch/empty_world.launch">
  10. <arg name="debug" value="$(arg debug)" />
  11. <arg name="gui" value="$(arg gui)" />
  12. <arg name="paused" value="$(arg paused)"/>
  13. <arg name="use_sim_time" value="$(arg use_sim_time)"/>
  14. <arg name="headless" value="$(arg headless)"/>
  15. </include>
  16. <!-- 加载机器人模型描述参数 -->
  17. <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" />
  18. <!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
  19. <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
  20. <!-- 运行robot_state_publisher节点,发布tf -->
  21. <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
  22. <param name="publish_frequency" type="double" value="50.0" />
  23. </node>
  24. <!-- 在gazebo中加载机器人模型-->
  25. <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
  26. args="-urdf -model mrobot -param robot_description"/>
  27. </launch>

正常启动后如下所示

**创建仿真环境 **

这里有两种方法添加场景模型

第一:直接添加

将模型放置到~/.gazebo/models 文件夹下——在gazebo的左侧列表点击“insert”(可以看到里面有很多的模型,我们只需要从列表中拖出我们需要的模型放置到仿真环境中就可以)

  1. https://bitbucket.org/osrf/gazebo_models/downloads/

这里本文开始时的下载如果已经下载完成可以跳过,如果还未下载完成请等待下载完成再进行后续操作。

选择需要的模型直接拖动到场景中搭建即可

注意这一步需要将原本的机器人模型进行删除然后保存。

这里如果显示conectting----说明还未连接完成,等待出现http-----连接完成后即可正常显示模型,再选择需要的模型添加即可。

自定义文件名称和位置即可

建议存放在catkin_ws/src/mbot_descritpion/worlds下

第二:使用Building editor

模型创建:

Edit——Building editor——绘制环境模型——File——Save保存我们的模型文件(自己设置模型文件名字)——Exit Building Editor(退出编辑界面),可以看到我们的仿真环境已经在gazebo中显示;

保存环境模型同第一种方法一致,然后关闭gazebo界面即可。

仿真使用

  1. <!-- 设置launch文件的参数 -->
  2. <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/><!-- 要加入的部分 -->
  3. <arg name="paused" default="false"/>
  4. <arg name="use_sim_time" default="true"/>
  5. <arg name="gui" default="true"/>
  6. <arg name="headless" default="false"/>
  7. <arg name="debug" default="false"/>

**传感器仿真 **

  1. cd ~/catkin_ws/src/mbot_description/urdf/xacro
  2. mkdir sensors
  3. cd sensors
  4. sudo gedit camera_gazebo.xacro
  1. <?xml version="1.0"?>
  2. <robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="camera">
  3. <xacro:macro name="usb_camera" params="prefix:=camera">
  4. <link name="${prefix}_link">
  5. <inertial>
  6. <mass value="0.1" />
  7. <origin xyz="0 0 0" />
  8. <inertia ixx="0.01" ixy="0.0" ixz="0.0"
  9. iyy="0.01" iyz="0.0"
  10. izz="0.01" />
  11. </inertial>
  12. <visual>
  13. <origin xyz=" 0 0 0 " rpy="0 0 0" />
  14. <geometry>
  15. <box size="0.01 0.04 0.04" />
  16. </geometry>
  17. <material name="black"/>
  18. </visual>
  19. <collision>
  20. <origin xyz="0.0 0.0 0.0" rpy="0 0 0" />
  21. <geometry>
  22. <box size="0.01 0.04 0.04" />
  23. </geometry>
  24. </collision>
  25. </link>
  26. <gazebo reference="${prefix}_link">
  27. <material>Gazebo/Black</material>
  28. </gazebo>
  29. <gazebo reference="${prefix}_link"> <!-- 这个sensor代表的link -->
  30. <sensor type="camera" name="camera_node">
  31. <update_rate>30.0</update_rate><!-- 摄像头发布频率 -->
  32. <camera name="head">
  33. <horizontal_fov>1.3962634</horizontal_fov><!-- 摄像头可视范围 -->
  34. <image>
  35. <width>1280</width><!-- 摄像头分辨率 -->
  36. <height>720</height><!-- 摄像头分辨率 -->
  37. <format>R8G8B8</format><!-- 摄像头数据格式 -->
  38. </image>
  39. <clip>
  40. <near>0.02</near><!-- 最近距离 -->
  41. <far>300</far><!-- 最远距离 -->
  42. </clip>
  43. <noise>
  44. <type>gaussian</type><!-- 摄像头高斯噪声 -->
  45. <mean>0.0</mean>
  46. <stddev>0.007</stddev>
  47. </noise>
  48. </camera>
  49. <plugin name="gazebo_camera" filename="libgazebo_ros_camera.so"><!-- 加载插件,实现摄像头功能 -->
  50. <alwaysOn>true</alwaysOn>
  51. <updateRate>0.0</updateRate>
  52. <cameraName>/camera</cameraName><!-- 命名空间 -->
  53. <imageTopicName>image_raw</imageTopicName><!-- 发布图片信息话题名称 -->
  54. <cameraInfoTopicName>camera_info</cameraInfoTopicName><!-- 发布摄像头信息话题名称 -->
  55. <frameName>camera_link</frameName><!-- 数据的坐标系统 -->
  56. <hackBaseline>0.07</hackBaseline>
  57. <distortionK1>0.0</distortionK1>
  58. <distortionK2>0.0</distortionK2>
  59. <distortionK3>0.0</distortionK3>
  60. <distortionT1>0.0</distortionT1>
  61. <distortionT2>0.0</distortionT2>
  62. </plugin>
  63. </sensor>
  64. </gazebo>
  65. </xacro:macro>
  66. </robot>
  1. cd ~/catkin_ws/src/mbot_description/urdf/xacro/gazebo
  2. sudo gedit mbot_with_camera_gazebo.xacro

在文件中写入以下内容即可

  1. <?xml version="1.0"?>
  2. <robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
  4. <xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/camera_gazebo.xacro" />
  5. <xacro:property name="camera_offset_x" value="0.17" />
  6. <xacro:property name="camera_offset_y" value="0" />
  7. <xacro:property name="camera_offset_z" value="0.10" />
  8. <mbot_base/>
  9. <!-- Camera -->
  10. <joint name="camera_joint" type="fixed">
  11. <origin xyz="${camera_offset_x} ${camera_offset_y} ${camera_offset_z}" rpy="0 0 0" />
  12. <parent link="base_link"/>
  13. <child link="camera_link"/>
  14. </joint>
  15. <xacro:usb_camera prefix="camera"/>
  16. <xacro:mbot_base_gazebo/>
  17. </robot>

带摄像头的机器人launch启动文件的编写

  1. cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
  2. sudo gedit view_mbot_with_camera_gazebo.launch
  1. <launch>
  2. <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/>
  3. <arg name="paused" default="false"/>
  4. <arg name="use_sim_time" default="true"/>
  5. <arg name="gui" default="true"/>
  6. <arg name="headless" default="false"/>
  7. <arg name="debug" default="false"/>
  8. <include file="$(find gazebo_ros)/launch/empty_world.launch">
  9. <arg name="world_name" value="$(arg world_name)" />
  10. <arg name="debug" value="$(arg debug)" />
  11. <arg name="gui" value="$(arg gui)" />
  12. <arg name="paused" value="$(arg paused)"/>
  13. <arg name="use_sim_time" value="$(arg use_sim_time)"/>
  14. <arg name="headless" value="$(arg headless)"/>
  15. </include>
  16. <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_camera_gazebo.xacro'" />
  17. <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
  18. <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
  19. <param name="publish_frequency" type="double" value="50.0" />
  20. </node>
  21. <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
  22. args="-urdf -model mrobot -param robot_description"/>
  23. </launch>

然后运行显示模型

  1. roslaunch mbot_description view_mbot_with_camera_gazebo.launch

注意如果说出现

[ERROR] [1666006852.591762, 106.467000]: Spawn service failed. Exiting.
[urdf_spawner-6] process has died [pid 35261, exit code 1, cmd /opt/ros/noetic/lib/gazebo_ros/spawn_model -urdf -model mrobot -param robot_description __name:=urdf_spawner __log:=/home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6.log].
log file: /home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6*.log
则说明gazebo进程关闭不完全,需要将所有gazebo进程关闭

  1. killall gzserver

然后发现可以正常启动

使用qt可视化工具查看摄像头画面显示

另起终端

  1. rqt_image_view

注意摄像头参数选定

启用键盘控制

  1. roslaunch mbot_teleop mbot_teleop.launch

注意键盘控制按键

这里如果报错无法定位python 软件包

需要进行下载配置python插件

  1. sudo ln -s /usr/bin/python3 /usr/bin/python

然后再允许键盘控制节点即可

如果读者功能包的src目录下没有mbot_teleop功能包,也可下载另键盘控制功能包

  1. cd ~/carkin_ws/src
  2. git clone https://github.com/ros-teleop/teleop_twist_keyboard.git
  3. //然后启用即可
  4. rosrun teleop_twist_keyboard teleop_twist_keyboard.py

注意该teleop_twist_keyboard.py需要更改为可执行的文件

如果不想使用该功能包,想知道如何自主创建mbot_teleop功能包可参考Arbotix+rviz那篇文章mbot_teleop 功能包创建方法

Rviz查看摄像头采集的信息

  1. rosrun rviz rviz

首先固定框架(fixed frame)选择base_footprint

然后Add——robotmodel——ok

然后Add——image——ok

然后image——image topic——/camera/image_raw

** 激光雷达仿真**

  1. cd ~/catkin_ws/src/mbot_description/urdf/xacro/sensors
  2. sudo gedit lidar_gazebo.xacro
  1. <?xml version="1.0"?>
  2. <robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="laser">
  3. <xacro:macro name="rplidar" params="prefix">
  4. <!-- Create laser reference frame -->
  5. <link name="${prefix}_link">
  6. <inertial>
  7. <mass value="0.1" />
  8. <origin xyz="0 0 0" />
  9. <inertia ixx="0.01" ixy="0.0" ixz="0.0"
  10. iyy="0.01" iyz="0.0"
  11. izz="0.01" />
  12. </inertial>
  13. <visual>
  14. <origin xyz=" 0 0 0 " rpy="0 0 0" />
  15. <geometry>
  16. <cylinder length="0.05" radius="0.05"/>
  17. </geometry>
  18. <material name="black"/>
  19. </visual>
  20. <collision>
  21. <origin xyz="0.0 0.0 0.0" rpy="0 0 0" />
  22. <geometry>
  23. <cylinder length="0.06" radius="0.05"/>
  24. </geometry>
  25. </collision>
  26. </link>
  27. <gazebo reference="${prefix}_link">
  28. <material>Gazebo/Black</material>
  29. </gazebo>
  30. <gazebo reference="${prefix}_link">
  31. <sensor type="ray" name="rplidar">
  32. <pose>0 0 0 0 0 0</pose>
  33. <visualize>false</visualize>
  34. <update_rate>5.5</update_rate>
  35. <ray>
  36. <scan>
  37. <horizontal>
  38. <samples>360</samples>
  39. <resolution>1</resolution>
  40. <min_angle>-3</min_angle>
  41. <max_angle>3</max_angle>
  42. </horizontal>
  43. </scan>
  44. <range>
  45. <min>0.10</min>
  46. <max>6.0</max>
  47. <resolution>0.01</resolution>
  48. </range>
  49. <noise>
  50. <type>gaussian</type>
  51. <mean>0.0</mean>
  52. <stddev>0.01</stddev>
  53. </noise>
  54. </ray>
  55. <plugin name="gazebo_rplidar" filename="libgazebo_ros_laser.so">
  56. <topicName>/scan</topicName>
  57. <frameName>laser_link</frameName>
  58. </plugin>
  59. </sensor>
  60. </gazebo>
  61. </xacro:macro>
  62. </robot>

然后编写主体xacro文件

  1. cd ~/catkin_ws/src/mbot_description/urdf/xacro/gazebo
  2. sudo gedit mbot_with_laser_gazebo.xacro

内容如下所示

  1. <?xml version="1.0"?>
  2. <robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
  4. <xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/lidar_gazebo.xacro" />
  5. <xacro:property name="lidar_offset_x" value="0" />
  6. <xacro:property name="lidar_offset_y" value="0" />
  7. <xacro:property name="lidar_offset_z" value="0.105" />
  8. <!-- lidar -->
  9. <joint name="lidar_joint" type="fixed">
  10. <origin xyz="${lidar_offset_x} ${lidar_offset_y} ${lidar_offset_z}" rpy="0 0 0" />
  11. <parent link="base_link"/>
  12. <child link="laser_link"/>
  13. </joint>
  14. <xacro:rplidar prefix="laser"/>
  15. <xacro:mbot_base_gazebo/>
  16. </robot>

然后编写launch 启动文件

  1. cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
  2. sudo gedit view_mbot_with_laser_gazebo.launch
  1. <launch>
  2. <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/>
  3. <arg name="paused" default="false"/>
  4. <arg name="use_sim_time" default="true"/>
  5. <arg name="gui" default="true"/>
  6. <arg name="headless" default="false"/>
  7. <arg name="debug" default="false"/>
  8. <include file="$(find gazebo_ros)/launch/empty_world.launch">
  9. <arg name="world_name" value="$(arg world_name)" />
  10. <arg name="debug" value="$(arg debug)" />
  11. <arg name="gui" value="$(arg gui)" />
  12. <arg name="paused" value="$(arg paused)"/>
  13. <arg name="use_sim_time" value="$(arg use_sim_time)"/>
  14. <arg name="headless" value="$(arg headless)"/>
  15. </include>
  16. <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_laser_gazebo.xacro'" />
  17. <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
  18. <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
  19. <param name="publish_frequency" type="double" value="50.0" />
  20. </node>
  21. <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
  22. args="-urdf -model mrobot -param robot_description"/>
  23. </launch>

激光雷达正常显示

然后启用键盘控制

  1. roslaunch mbot_teleop mbot_teleop.launch

rviz查看雷达采集信息

  1. rosrun rviz rviz

需要注意的地方与刚才类似

首先固定框架(fixed frame)选择base_footprint

然后Add——robotmodel——ok

然后Add——laserscan——ok

然后laserscan——topic——scan

在图中的红色线条即为雷达检测到的障碍物,由于我的小车卡在消防车和救护车之间懒得动了,就不附图说明了,红色线条很淡,建议将小车周围拿障碍物包满。

Kinect仿真

方法跟上面摄像头和激光雷达实现的方法类似,这里就不多加说明了,直接给出结果显示了。
gazebo显示

  1. cd ~/catkin_ws/src/mbot_description/urdf/xacro/sensors
  2. sudo gedit kinect_gazebo.xacro
  1. <?xml version="1.0"?>
  2. <robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="kinect_camera">
  3. <xacro:macro name="kinect_camera" params="prefix:=camera">
  4. <!-- Create kinect reference frame -->
  5. <!-- Add mesh for kinect -->
  6. <link name="${prefix}_link">
  7. <origin xyz="0 0 0" rpy="0 0 0"/>
  8. <visual>
  9. <origin xyz="0 0 0" rpy="0 0 ${M_PI/2}"/>
  10. <geometry>
  11. <mesh filename="package://mbot_description/meshes/kinect.dae" />
  12. </geometry>
  13. </visual>
  14. <collision>
  15. <geometry>
  16. <box size="0.07 0.3 0.09"/>
  17. </geometry>
  18. </collision>
  19. </link>
  20. <joint name="${prefix}_optical_joint" type="fixed">
  21. <origin xyz="0 0 0" rpy="-1.5708 0 -1.5708"/>
  22. <parent link="${prefix}_link"/>
  23. <child link="${prefix}_frame_optical"/>
  24. </joint>
  25. <link name="${prefix}_frame_optical"/>
  26. <gazebo reference="${prefix}_link">
  27. <sensor type="depth" name="${prefix}">
  28. <always_on>true</always_on>
  29. <update_rate>20.0</update_rate>
  30. <camera>
  31. <horizontal_fov>${60.0*M_PI/180.0}</horizontal_fov>
  32. <image>
  33. <format>R8G8B8</format>
  34. <width>640</width>
  35. <height>480</height>
  36. </image>
  37. <clip>
  38. <near>0.05</near>
  39. <far>8.0</far>
  40. </clip>
  41. </camera>
  42. <plugin name="kinect_${prefix}_controller" filename="libgazebo_ros_openni_kinect.so">
  43. <cameraName>${prefix}</cameraName>
  44. <alwaysOn>true</alwaysOn>
  45. <updateRate>10</updateRate>
  46. <imageTopicName>rgb/image_raw</imageTopicName>
  47. <depthImageTopicName>depth/image_raw</depthImageTopicName>
  48. <pointCloudTopicName>depth/points</pointCloudTopicName>
  49. <cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName>
  50. <depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTopicName>
  51. <frameName>${prefix}_frame_optical</frameName>
  52. <baseline>0.1</baseline>
  53. <distortion_k1>0.0</distortion_k1>
  54. <distortion_k2>0.0</distortion_k2>
  55. <distortion_k3>0.0</distortion_k3>
  56. <distortion_t1>0.0</distortion_t1>
  57. <distortion_t2>0.0</distortion_t2>
  58. <pointCloudCutoff>0.4</pointCloudCutoff>
  59. </plugin>
  60. </sensor>
  61. </gazebo>
  62. </xacro:macro>
  63. </robot>

然后编写主体xacro文件

  1. cd ~/catkin_ws/src/mbot_desctiption/urdf/xacro/gazebo
  2. sudo gedit mbot_with_kinect_gazebo.xacro
  1. <?xml version="1.0"?>
  2. <robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
  3. <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
  4. <xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/kinect_gazebo.xacro" />
  5. <xacro:property name="kinect_offset_x" value="0.15" />
  6. <xacro:property name="kinect_offset_y" value="0" />
  7. <xacro:property name="kinect_offset_z" value="0.11" />
  8. <mbot_base/>
  9. <!-- kinect -->
  10. <joint name="kinect_joint" type="fixed">
  11. <origin xyz="${kinect_offset_x} ${kinect_offset_y} ${kinect_offset_z}" rpy="0 0 0" />
  12. <parent link="base_link"/>
  13. <child link="kinect_link"/>
  14. </joint>
  15. <xacro:kinect_camera prefix="kinect"/>
  16. <xacro:mbot_base_gazebo/>
  17. </robot>

然后编写launch 启动文件

  1. cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
  2. sudo gedit view_mbot_with_kinect_gazebo.launch
  1. <launch>
  2. <!-- 设置launch文件的参数 -->
  3. <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/><!-- 设置仿真环境文件路径 -->
  4. <arg name="paused" default="false"/>
  5. <arg name="use_sim_time" default="true"/>
  6. <arg name="gui" default="true"/>
  7. <arg name="headless" default="false"/>
  8. <arg name="debug" default="false"/>
  9. <!-- 运行gazebo仿真环境 -->
  10. <include file="$(find gazebo_ros)/launch/empty_world.launch">
  11. <arg name="world_name" value="$(arg world_name)" />
  12. <arg name="debug" value="$(arg debug)" />
  13. <arg name="gui" value="$(arg gui)" />
  14. <arg name="paused" value="$(arg paused)"/>
  15. <arg name="use_sim_time" value="$(arg use_sim_time)"/>
  16. <arg name="headless" value="$(arg headless)"/>
  17. </include>
  18. <!-- 加载机器人模型描述参数 -->
  19. <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_kinect_gazebo.xacro'" /> <!-- 设置机器人模型文件路径 -->
  20. <!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
  21. <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
  22. <!-- 运行robot_state_publisher节点,发布tf -->
  23. <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
  24. <param name="publish_frequency" type="double" value="50.0" />
  25. </node>
  26. <!-- 在gazebo中加载机器人模型-->
  27. <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
  28. args="-urdf -model mrobot -param robot_description"/>
  29. </launch>

运行后如下所示

查看kinect采集到的信息

  1. rosrun rviz rviz

注意:

首先固定框架(fixed frame)选择base_footprint

然后Add——robotmodel——ok

然后Add——pointcloud2——ok

然后pointcloud2——topic——/kinect/depth/points

以上就是本节内容,关于Gazebo物理仿真平台搭建已经全部完成。

问题解决:

1.gazebo--SpawnModel: Failure - model name mrobot already exists.

  1. roslaunch gazebo_ros empty_world.launch

在开启后的gazebo界面中选中mrobot模型delete删除即可

然后退出。

在上述模型创建且.world 保存前需要把机器人模型删除再保存,不然会出现模型重名报错。

  1. [ERROR] [1666006852.591762, 106.467000]: Spawn service failed. Exiting.
    [urdf_spawner-6] process has died [pid 35261, exit code 1, cmd
    /opt/ros/noetic/lib/gazebo_ros/spawn_model -urdf -model mrobot -param robot_description __name:=urdf_spawner __log:=/home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6.log].
    log file: /home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6*.log

将所有gazebo进程关闭再重启即可

  1. killall gzserver
标签: ubuntu linux 运维

本文转载自: https://blog.csdn.net/LYY_WJL/article/details/127352386
版权归原作者 哩lililili 所有, 如有侵权,请联系我们删除。

“Gazebo——仿真平台搭建(基于Ubuntu20.04)”的评论:

还没有评论