Gazebo是一種多機器人仿真器,可用於室內外機器人仿真。Gazebo在ROS中有良好的接口,包含ROS和Gazebo的所有控制。
若要實現ROS到Gazebo的通信,我們必須安裝ROS-Gazebo接口。
應該安裝以下軟件包:
$ sudo apt install ros-melodic-gazebo-ros-pkgs ros-melodic-gazebo-msgs ros-melodic-gazebo-plugins ros-melodic-gazebo-ros-control
*gazebo_ros_pkgs : 它包含用於將ROS和Gazebo連接的封裝和工具。
*gazebo-msgs : 它包含ROS和Gazebo交互的消息和服務的數據結構。
*gazebo-plugins : 它包含用於傳感器、執行結構的Gazebo插件。
*gazebo-ros-control : 它包含用於在ROS和Gazebo之間通信的標准控制器。
安裝后,請使用以下命令檢查Gazebo是否安裝正確:
$ roscore & rosrun gazebo_ros gazebo
一.為Gazebo創建機械臂仿真模型
我們可以通過添加仿真參數來更新現有的機器人描述,從而創建一個機械臂仿真模型。
我們需要去創建一個軟件包:
$ catkin_create_pkg seven_dof_arm_gazebo gazebo_msgs gazebo_plugins gazebo_ros gazebo_ros_control mastering_ros_robot_description_pkg
也可以在相應的Git庫中獲得完整的軟件包。如下:
$ git clone https://github.com/jocacace/seven_dof_arm_gazebo.git
可以在seven_dof_arm.xacro文件中看到機器人的完整仿真模型,上一章講過。
該文件包含了URDF標簽,這對於仿真是必要的,我們將定義碰撞、慣性、傳動、關節、連桿、以及Gazebo。
我們可以使用seven_dof_arm_gazebo軟件包來啟動現有的仿真模型,啟動文件為:seven_dof_arm_world.launch的啟動文件。
代碼如下:
1 <launch> 2 3 <!-- these are the arguments you can pass this launch file, for example paused:=t rue --> 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 10 <!-- We resume the logic in empty_world.launch --> 11 <include file="$(find gazebo_ros)/launch/empty_world.launch"> 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 <!-- Load the URDF into the ROS Parameter Server --> 20 <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find ma stering_ros_robot_description_pkg)/urdf/seven_dof_arm.xacro'" /> 21 22 23 <!-- Run a python script to the send a service call to gazebo_ros to spawn a URDF robot --> 24 <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" out put="screen" 25 args="-urdf -model seven_dof_arm -param robot_description"/> 26 27 28 </launch>
啟動以下命令來顯示仿真機械臂
$ roslaunch seven_dof_arm_gazebo seven_dof_arm_world.launch
模型如下:
接下來我們來詳細的討論一下機器人的仿真模型文件
1.為Gazebo機器人模型添加顏色和紋理
在機器人仿真中我們可以看到每個連桿都有不同的顏色和紋理。在xacro文件中,下面的標簽可以為機器人的連桿提供紋理和顏色:
140 <gazebo reference="bottom_link"> 141 <material>Gazebo/White</material> 142 </gazebo> 172 <gazebo reference="base_link"> 173 <material>Gazebo/White</material> 174 </gazebo> 210 <gazebo reference="shoulder_pan_link"> 211 <material>Gazebo/Red</material> 212 </gazebo>
2.添加transmission標簽來啟動模型
為了使用ROS控制器來啟動機器人,我們需要定義<transmission>(傳動)標簽來連接執行機構和關節,以下是為傳動的宏:
92 <xacro:macro name="transmission_block" params="joint_name"> 93 <transmission name="tran1"> 94 <type>transmission_interface/SimpleTransmission</type> 95 <joint name="${joint_name}"> 96 <hardwareInterface>hardware_interface/PositionJointInterface</hardwareIn terface> 97 </joint> 98 <actuator name="motor1"> 99 <hardwareInterface>hardware_interface/PositionJointInterface</hardwareIn terface> 100 <mechanicalReduction>1</mechanicalReduction> 101 </actuator> 102 </transmission> 103 </xacro:macro>
在這里<joint name="">是連接啟動器的關節。<type>標簽是傳動類型。目前,僅支持簡單的傳動transmission_interface/SimpleTransmission. <hardwareInterface>
標簽是要加載的硬件接口類型(位置、速度或力度),在該示例中,使用了位置控制硬件接口。這個硬件接口由gazebo_ros_control插件加載,下一節將看到。
3.添加gazebo_ros_control插件
在添加傳動標簽后,我們應該在仿真模型中添加gazebo_ros_control插件來解析傳動標簽並分配適當的硬件接口和控制管理器。代碼如下:
563 <gazebo> 564 <plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so"> 565 <robotNamespace>/seven_dof_arm</robotNamespace> 566 </plugin> 567 </gazebo> 568
<plugin>標簽指定了要加載的插件名是libgazebo_ros_control.so。可以將<robotNamespace>標簽作為機器人的名稱,
如果我們沒有指定名稱,它將從URDF 自動加載機器人的名稱。我們還可以在參數服務器(<robotParam>)上指定控制器刷新速率(<control-Period>),robot_description(URDF)
的位置以及機器人硬件接口的類型(<robotSimType>).默認的硬件接口可以是:JointStateInerface、EffortJointInterface或VelocityJointInterface.
4.在Gazebo中添加3D視覺傳感器
在Gazebo中,我們可以仿真機器人的運動和物理特征,也可以對傳感器進行仿真。
要在Gazebo中創建一個傳感器,我們就必須對Gazebo中傳感器的行為進行建模。
Gazebo中有一些預先創建的傳感器模型,可以直接在代碼中使用而無需編寫新模型。
這里,我們在Gazebo中添加了一個名為Asus Xtion Pro模型的3D視覺傳感器(通常稱為RGB-D傳感器)。傳感器已經在gazebo_ros_pkgs/gazebo_plugins的ROS軟件包中實現。
Gazebo中的每個模型都可以作為Gazebo_ROS插件實現,可以將其插入URDF文件來加載。
以下是我們如何在seven_dof_arm_with_rgbd.xacro機器人的xacro文件中把Gazebo定義和Xtion Pro的物理機器人模型包含進來的:
<xacro:include filename="$(find mastering_ros_robot_description_pkg)/urdf/sensor s/xtion_pro_live.urdf.xacro"/>
在xtion_pro_live.urdf.xacro文件內部,我們可以看到以下代碼:
1 <?xml version="1.0"?> 2 <robot xmlns:xacro="http://ros.org/wiki/xacro"> 3 4 <xacro:include filename="$(find mastering_ros_robot_description_pkg)/urdf/sensors /xtion_pro_live.gazebo.xacro"/> 5 6 <xacro:macro name="dummy_inertial"> 7 <inertial> 8 <origin xyz="0 0 0" rpy="0 0 0"/> 9 <mass value="0.001"/> 10 <inertia ixx="0.00001" ixy="0" ixz="0" 11 iyy="0.00001" iyz="0" 12 izz="0.00001"/> 13 </inertial> 14 </xacro:macro> 15 16 <xacro:macro name="xtion_pro_live" params="name parent *origin *optical_origin"> 17 18 <!-- frames in the center of the camera --> 19 <joint name="${name}_joint" type="fixed"> 20 <xacro:insert_block name="origin"/> 21 <parent link="${parent}_link"/> 22 <child link="${name}_link"/> 23 </joint> 24 25 <link name="${name}_link"> 26 <inertial> 27 <origin xyz="-0.00936000000 -0.00003000000 -0.00273000000" rpy="0 0 0"/> 28 <mass value="0.21970000000"/> 29 <inertia ixx="0.00000429247" ixy="0.00000000000" ixz="0.00000002565" 30 iyy="0.00000008027" iyz="0.00000000000" 31 izz="0.00000427339"/> 32 </inertial> 33 <visual> 34 <origin xyz="0 0 0" rpy="0 0 0"/> 35 <geometry> 36 <mesh filename="package://mastering_ros_robot_description_pkg/meshes/sens ors/xtion_pro_live/xtion_pro_live.dae"/> 37 </geometry> 38 <material name="DarkGrey"/> 39 </visual> 40 </link> 41 42 <joint name="${name}_optical_joint" type="fixed"> 43 <xacro:insert_block name="optical_origin"/> 44 <parent link="${name}_link"/> 45 <child link="${name}_optical_frame"/> 46 </joint> 47 48 <link name="${name}_optical_frame"> 49 <dummy_inertial/> 50 </link> 51 52 <!-- Depth sensor frames --> 53 <joint name="${name}_depth_joint" type="fixed"> 54 <origin xyz="0.0 0.049 0.0" rpy="0 0 0"/> 55 <parent link="${name}_link"/> 56 <child link="${name}_depth_frame"/> 57 </joint> 58 59 <link name="${name}_depth_frame"> 60 <dummy_inertial/> 61 </link> 62 63 <joint name="${name}_depth_optical_joint" type="fixed"> 64 <origin xyz="0 0 0" rpy="${-90.0 * deg_to_rad} 0.0 ${-90.0 * deg_to_rad}"/> 65 <parent link="${name}_depth_frame"/> 66 <child link="${name}_depth_optical_frame"/> 67 </joint> 68 69 <link name="${name}_depth_optical_frame"> 70 <dummy_inertial/> 71 </link> 72 73 <!-- RGB sensor frames --> 74 <joint name="${name}_rgb_joint" type="fixed"> 75 <origin xyz="0.0 0.022 0.0" rpy="0 0 0"/> 76 <parent link="${name}_link"/> 77 <child link="${name}_rgb_frame"/> 78 </joint> 79 80 <link name="${name}_rgb_frame"> 81 <dummy_inertial/> 82 </link> 83 84 <joint name="${name}_rgb_optical_joint" type="fixed"> 85 <origin xyz="0 0 0" rpy="${-90.0 * deg_to_rad} 0.0 ${-90.0 * deg_to_rad}"/> 86 <parent link="${name}_rgb_frame"/> 87 <child link="${name}_rgb_optical_frame"/> 88 </joint> 89 90 <link name="${name}_rgb_optical_frame"> 91 <dummy_inertial/> 92 </link> 93 94 <xacro:xtion_pro_live_rgbd_camera_gazebo name="${name}"/> 95 </xacro:macro> 96 97 </robot>
在這里,我們可以看到它包含另一個名為xtion_pro_live.gazebo.xacro的文件,該文件包含Xtion Pro在Gazebo中的完整定義。
我們還可以看到一個名為xtion_pro_live的宏定義,其中包含Xtion Pro的完整模型定義,包括連桿和關節:
<mesh filename="package://mastering_ros_robot_description_pkg/meshes/sens ors/xtion_pro_live/xtion_pro_live.dae"/>
在宏定義中,我們將導入一個Asus Xtion Pro的網格文件該文件將在Gazebo中顯示為相機連桿。
在mastering_ros_robot_description_pkg/urdf/sensors/xtion_pro_live.gazebo.xacro文件中,
我們可以設置Xtion Pro的Gazebo-ROS插件,我們將插件定義為宏,並支持RGB和深度相機,插件的定義如下:
67 <plugin name="${name}_frame_controller" filename="libgazebo_ros_openni_kine ct.so"> 68 <alwaysOn>true</alwaysOn> 69 <updateRate>6.0</updateRate> 70 <cameraName>${name}</cameraName> 71 72 <imageTopicName>rgb/image_raw</imageTopicName> 73 <cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName> 74 <pointCloudTopicName>rgb/points</pointCloudTopicName> 75 76 <depthImageTopicName>depth/image_raw</depthImageTopicName> 77 <depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTop icName> 78 79 <frameName>${name}_optical_frame</frameName> 80 <pointCloudCutoff>0.05</pointCloudCutoff> 81 <pointCloudCutoffMax>5</pointCloudCutoffMax> 82 <rangeMax>1.5</rangeMax> 83 84 </plugin>
Xtion Pro的插件文件名是libgazebo_ros_openni_kinect.so,我們可以定義插件參數,例如相機名稱,圖像話題等。
二.仿真裝有Xtion Pro的機械臂
現在已經了解了Gazebo中相機插件的定義,我們可以使用以下命令啟動完整的仿真:
$ roslaunch seven_dof_arm_gazebo seven_dof_arm_with_rgbd_world.launch
我們就可以看到一個機器人模型,在機械臂頂部裝有一個傳感器,如圖所示:
可視化三維傳感器數據
使用上述命令啟動仿真后,我們可以檢查由傳感器插件生成的話題,如圖所示:
命令:qqtsj ~ catkin_ws :rostopic list /clock /gazebo/link_states /gazebo/model_states /gazebo/parameter_descriptions /gazebo/parameter_updates /gazebo/set_link_state /gazebo/set_model_state /image_view_1581403067208774218/output /image_view_1581403067208774218/parameter_descriptions /image_view_1581403067208774218/parameter_updates /rgbd_camera/depth/camera_info /rgbd_camera/depth/image_raw /rgbd_camera/depth/points /rgbd_camera/ir/camera_info /rgbd_camera/ir/image_raw /rgbd_camera/ir/image_raw/compressed /rgbd_camera/ir/image_raw/compressed/parameter_descriptions /rgbd_camera/ir/image_raw/compressed/parameter_updates /rgbd_camera/ir/image_raw/compressedDepth /rgbd_camera/ir/image_raw/compressedDepth/parameter_descriptions /rgbd_camera/ir/image_raw/compressedDepth/parameter_updates /rgbd_camera/ir/image_raw/theora /rgbd_camera/ir/image_raw/theora/parameter_descriptions /rgbd_camera/ir/image_raw/theora/parameter_updates /rgbd_camera/parameter_descriptions /rgbd_camera/parameter_updates /rgbd_camera/rgb/camera_info /rgbd_camera/rgb/image_raw /rgbd_camera/rgb/image_raw/compressed /rgbd_camera/rgb/image_raw/compressed/parameter_descriptions /rgbd_camera/rgb/image_raw/compressed/parameter_updates /rgbd_camera/rgb/image_raw/compressedDepth /rgbd_camera/rgb/image_raw/compressedDepth/parameter_descriptions /rgbd_camera/rgb/image_raw/compressedDepth/parameter_updates /rgbd_camera/rgb/image_raw/theora /rgbd_camera/rgb/image_raw/theora/parameter_descriptions /rgbd_camera/rgb/image_raw/theora/parameter_updates /rgbd_camera/rgb/points /rosout /rosout_agg
我們使用名為image_view的工具來查看3D視覺傳感器的圖像數據:
*查看RGB原始圖像:
$ rosrun image_view image_view image:=/rgbd_camera/rgb/image_raw
*查看IR原始圖像:
$rosrun image_view image_view image:=/rgbd_camera/ir/image_raw
*查看深度圖像
$rosrun image_view image_view image:=/rgbd_camera/depth/image_raw
如下為以上的截圖: