ROS與Gazebo:基於URDF實現Gazebo中的機器人仿真

前言

本文主要介紹在ROS中使用urdf實現Gazebo中仿真時需要配置的基本內容。流程上包括URDF標籤結構,ROS下的關節發佈(robot_state_publisher),ROS與Gazebo間插件配置,launch文件配置(模型robot_description參數加載,相關ros_control控制器yaml配置與加載,一般包含使Gazebo向ROS發佈關節信息的控制器類型,ROS控制Gazebo中機器人關節運動的控制器等)
另,如果由錯漏的地方,希望大家及時指出,同時也多多包涵。

1. 基礎預備

下面通過準備工作了解ros,ros_control與真實機器人或gazebo間的結構及數據流。

1.1 準備工作1:ros_control

https://wiki.ros.org/ros_control
大概是作爲ROS與機器人交互的中介,其使用結構如圖。左側是控制器類型設置,右側是數據流。例如對於joint_position_controller,以機器人當前關節數據及ROS目標點爲輸入數據,再通過PID控制器實現輸出控制。
在這裏插入圖片描述

1.2 準備工作2:Gazebo與ROS間的關係

http://gazebosim.org/tutorials?tut=ros_control&cat=connect_ros
通過ros_control與Gazebo插件實現gazebo中機器人運動仿真。具體結構如下
在這裏插入圖片描述

2. 主要教程

官方教程:https://wiki.ros.org/urdf/Tutorials/Using%20a%20URDF%20in%20Gazebo
對應github: https://github.com/ros/urdf_sim_tutorial
Gazebo方面教程:http://gazebosim.org/tutorials?tut=ros_control&cat=connect_ros
urdf中的gazebo標籤及其他:http://gazebosim.org/tutorials/?tut=ros_urdf

上面兩個教程結合參考以後,會更加清晰。本文按官方教程順序展開,並進行適當補充說明。

2.1 通用的啓動文件格式內容

以官方教程下的gazebo.launch文件來介紹通用的啓動文件格式內容。


<!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="debug" value="$(arg debug)" />
<arg name="gui" value="$(arg gui)" />
<arg name="paused" value="$(arg paused)"/>
<arg name="use_sim_time" value="$(arg use_sim_time)"/>
<arg name="headless" value="$(arg headless)"/>
</include>

<param name="robot_description" command="$(find xacro)/xacro.py $(arg model)" />

<!-- push robot_description to factory and spawn robot in gazebo -->
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model"
args="-z 1.0 -unpause -urdf -model robot -param robot_description" respawn="false" output="screen" />

<node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher">
<param name="publish_frequency" type="double" value="30.0" />
</node>

它主要完成三件事:

  1. Launches an empty gazebo world.
  2. Loads the urdf from the macro tutorial into the parameter.
  3. Runs the script to read the urdf from the parameter and spawn it in gazebo.

這裏主要介紹gazebo_ros功能包與robot_state_publisher功能包

2.1.1 gazebo_ros功能包

gazebo_ros用來加載機器人模型並在GAZEBO中生成。在其參數中指定了其啓動時爲unpause狀態,並指定讀取名爲robot_description的參數,指出其爲 urdf類型,模型標籤爲robot。
關於它的一些信息參見:http://gazebosim.org/tutorials?tut=ros_roslaunch&cat=connect_ros

2.1.2 robot_state_publisher功能包

http://wiki.ros.org/robot_state_publisher
使用該功能包將機器人關節角度發送至tf. 並利用機器人運動學模型能發佈連桿的3D姿態。
利用robot_description指定的URDF,並結合joint_states(http://wiki.ros.org/joint_state_publisher,通過讀取默認服務器參數robot_descripition,找到非固定關節,併發送其關節信息)計算機器人前向運動學,並將結果利用tf進行發佈。
其均發生在ROS內,與GAZEBO無關,默認發佈頻率50hz

但此時無法讓Gazebo中機器人做任何事,因爲目前還未連通Gazebo與ROS間的信息交互。

2.2 Gazebo插件

In addition to the transmission tags, a Gazebo plugin needs to be added to your URDF that actually parses the transmission tags and loads the appropriate hardware interfaces and controller manager.

爲了讓ROS與GAZEBO相連,需要在URDF中添加一個GAZEBO插件,在</robot>前. 需要注意的是,ROS與Gazebo之間不同類型間的信息交互,需要不同的插件。下面提到的libgazebo_ros_control.so控制關節運動所需,具體參見http://gazebosim.org/tutorials?tut=ros_control&cat=connect_ros。
若是其他類型數據交互,需要選擇其他插件時參見
http://gazebosim.org/tutorials?tut=ros_gzplugins&cat=connect_ros

<!-- Gazebo plugin for ROS Control -->
<gazebo>
<plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
<robotNamespace>/</robotNamespace>
</plugin>
</gazebo>

2.3 URDF配置

參考鏈接http://gazebosim.org/tutorials/?tut=ros_urdf
單純的URDF無法在Gazebo中使用,需要對其進行配置。例如,給予關節重量與轉動慣量等
連桿配置:https://wiki.ros.org/urdf/XML/link
關節配置:https://wiki.ros.org/urdf/XML/joint

While URDFs are a useful and standardized format in ROS, they are lacking many features and have not been updated to deal with the evolving needs of robotics. URDF can only specify the kinematic and dynamic properties of a single robot in isolation. URDF can not specify the pose of the robot itself within a world. It is also not a universal description format since it cannot specify joint loops (parallel linkages), and it lacks friction and other properties. Additionally, it cannot specify things that are not robots, such as lights, heightmaps, etc.

To deal with this issue, a new format called the Simulation Description Format (SDF) was created for use in Gazebo to solve the shortcomings of URDF.

2.3.1 transmission標籤

爲了實現機器人運動關節運動,還需要對非固定關節在urdf中添加標籤(標籤詳細介紹https://wiki.ros.org/urdf/XML/Transmission)。相當於在運動關節上配置驅動電機。告訴他電機類型,名稱,位置,參數……
http://gazebosim.org/tutorials?tut=ros_control&cat=connect_ros
https://wiki.ros.org/urdf/XML/Transmission

Transmission-specific code (not robot-specific) implementing bidirectional (actuator <-> joint) effort and flow maps under a uniform interface shared across transmission types.

2.3.2 gazebo標籤

http://gazebosim.org/tutorials/?tut=ros_urdf

The element is an extension to the URDF used for specifying additional properties needed for simulation purposes in Gazebo. It allows you to specify the properties found in the SDF format that are not included in the URDF format. None of the elements within a element are required because default values will be automatically included. There are three different types of elements - one for the tag, one for tags, and one for tags. We will discuss the attributes and elements within each type of element throughout this tutorial.

  1. for link
    Convert visual colors to Gazebo format
    Convert stl files to dae files for better textures
    Add sensor plugins

  2. for joint
    Set proper damping dynamics
    Add actuator control plugins

  3. for the robot element

三類gazebo標籤根據需要添加即可。例如,不是每個joint都需要gazebo標籤。

2.4 控制器相關

ROS與Gazebo間的數據交互方式由控制器相關yaml文件指定.不論是,由GAZEBO向ROS發送機器人狀態,還是由ROS向gazebo發佈機器人運動指令。

2.4.1 控制器加載

控制器相關yaml文件將直接加載到參數空間。例如:

<rosparam command="load"
file="$(find urdf_sim_tutorial)/config/joints.yaml"
ns="r2d2_joint_state_controller" />

<node name="r2d2_controller_spawner" pkg="controller_manager" type="spawner"
args="r2d2_joint_state_controller
--shutdown-timeout 3"/>

從教程上看,控制器最好自己添加命名空間實現更好的區分話題。
當然,添加命名空間後,注意在rontroller_manager中用args指明。

關於controller_maneger的用法,參見:https://wiki.ros.org/controller_manager

2.4.2 joint_state_controller/JointStateController

joint_state_controller/JointStateController, 用來發送運動關節的實時數據。基本任何類型機器人都需要這個控制器。

This controller is found in the joint_state_controller package and publishes the state of the robot’s joints into ROS directly from Gazebo.

#yaml文件示例
type: "joint_state_controller/JointStateController"
publish_rate: 50

2.4.3 position_controllers/JointPositionController

配置控制器爲position_controllers/JointPositionController, 使用position_controllers package 下的JointPositionController 進行運動控制。需要在yaml中指出對應的joint名稱,並匹配URDF標籤下的控制器類型。
此時,GAZEBO將會監聽相應話題,在接收後驅動對應關節

#yaml文件示例
type: "position_controllers/JointPositionController"
joint: head_swivel
	  <!--transmission標籤示例-->
   1   <transmission name="head_swivel_trans">
   2     <type>transmission_interface/SimpleTransmission</type>
   3     <actuator name="$head_swivel_motor">
   4       <mechanicalReduction>1</mechanicalReduction>
   5     </actuator>
   6     <joint name="head_swivel">
   7       <hardwareInterface>PositionJointInterface</hardwareInterface>
   8     </joint>
   9   </transmission>
<!-- launch文件加載多個控制器示例  -->
<rosparam command="load"
file="$(find urdf_sim_tutorial)/config/joints.yaml"
ns="r2d2_joint_state_controller" />
<rosparam command="load"
file="$(find urdf_sim_tutorial)/config/head.yaml"
ns="r2d2_head_controller" />

<node name="r2d2_controller_spawner" pkg="controller_manager" type="spawner"
args="r2d2_joint_state_controller
r2d2_head_controller
--shutdown-timeout 3"/>

可以對多個控制器依次分開啓動,也可以如上同時啓動多個控制器

2.4.4 position_controllers/JointGroupPositionController

另外,當需要同時控制多個關節時,需要使用group。例如,同時控制抓手上的多個關節。串聯機器人一般都使用這個類型控制器。

#yaml文件示例
type: "position_controllers/JointGroupPositionController"
joints:
  - gripper_extension
  - left_gripper_joint
  - right_gripper_joint
<!-- launch文件加載多個控制器示例  -->
<rosparam command="load"
file="$(find urdf_sim_tutorial)/config/joints.yaml"
ns="r2d2_joint_state_controller" />
<rosparam command="load"
file="$(find urdf_sim_tutorial)/config/head.yaml"
ns="r2d2_head_controller" />
<rosparam command="load"
file="$(find urdf_sim_tutorial)/config/gripper.yaml"
ns="r2d2_gripper_controller" />

<node name="r2d2_controller_spawner" pkg="controller_manager" type="spawner"
args="r2d2_joint_state_controller
r2d2_head_controller
r2d2_gripper_controller
--shutdown-timeout 3"/>

2.4.5 其它控制器

另外還有輪式機器人底座運動控制器,請查看教程。
更多類型ROS控制器參見https://github.com/ros-controls/ros_controllers。例如在默認UR包中,除了2.4.2, 2.4.4,還使用了joint_trajectory_controller(http://wiki.ros.org/joint_trajectory_controller)

Controller for executing joint-space trajectories on a group of joints. Trajectories are specified as a set of waypoints to be reached at specific time instants, which the controller attempts to execute as well as the mechanism allows. Waypoints consist of positions, and optionally velocities and accelerations.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章