ROS笔记-将自己的urdf导入rviz和gazebo-01
首先需要有一个正式的urdf文件,urdf文件可以通过手写、三维建模软件导出和机器人厂家直接给出等方式获得,我这边用的是实验室柔性协作机器人的urdf文件,从厂家那边获得,文件内容如下图所示

接下来开始进行导入
在~/catkin_ws/src 目录下用catkin_create_pkg新建一个包,包名为testbot_descciption(可任意取名),依赖项目前是添加了urdf roscpp rospy sensor_msgs std_msgs tf 暂时来说是够用的,虽然我也不知道这些是不是必要的
接下来在testbot_description包内新建几个文件夹:config launch materials meshes rviz src urdf
其中urdf文件夹用来放机器人的描述文件,即.urdf 或者.xacro
src文件夹用来放自己编写的特定源码
rviz文件夹用来存放 urdf.rviz 即机器人模型在rviz中的显示,免得每次打开rviz还要手动添加robotmodel
meshes文件夹用来放urdf对应的STL文件
materials文件夹用来存放特定的urdf类的机器人描述文件
launch文件夹用来放相关的launch文件
config文件夹搭配launch文件来使用,其中会存放相关的配置文件用于ros与gazebo通信等功能
目前来说较为关键的是urdf meshes launch config 这几个文件夹

创建好对应的文件夹后,首先将urdf文件存放到/testbot_description/urdf 文件夹中
将对应的meshes存放到/testbot_description/meshes 文件夹中
此时需要修改urdf文件中每个link子目录下的geometry目录中的filename路径,将其修改为
filename="package://testbot_description/meshes/xxxx.STL"
然后这时候就可以通过相应的launch文件使其在rviz中显示出来了,对应的launch文件如下所示
<launch>
<!--send robot urdf to the "robot_description" parameter space-->
<arg name="model" default="$(find testbot_description)/urdf/my_robot.xacro"/>
<param name = "robot_description" command="$(find xacro)/xacro $(arg model)" />
<!--launch the robot_state_publisher node and the joint_state_pulisher_gui node-->
<node pkg="robot_state_publisher" type="robot_state_publisher" name="my_robot_state_pub" />
<node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher" />
<!--launch the rviz and save the robot model as urdf.rviz-->
<arg name="rvizconfig" default="$(find testbot_description)/rviz/urdf.rviz" />
<node name="rviz" pkg="rviz" type="rviz" args="-d $(arg rvizconfig)" />
</launch>
这个launch文件实现了三个功能
将urdf上传到参数服务器的robot_description中
启动了两个基本节点,robot_state_publisher和joint_state_publisher
启动了rviz 并且会将打开的机器人模型保存为urdf.rviz
启动launch文件后会如下图所示

注意:在launch前对testbot_description 文件夹中的CMakeList.txt文件进行修改,将
install(DIRECTORY config launch urdf
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)
添加进去,以防出现问题
此外,将urdf文件中的<robot name="XM3p"> 修改成
<robot name="XM3p" xmlns:xacro="http://www.ros.org/wiki/xacro">
将.urdf改成.xacro 这样可以把urdf改成优化的xacro文件,其中还有更多细节后面再说

接下来是将机器人模型导入到gazebo中,这比在rviz中显示相对来说要复杂一点,需要在config文件夹中配置.yaml文件,对原有的urdf文件(或xacro)进行修改,添加一些gazebo需要的参数,如每个非固定的关节joint添加<transmission></transmission>以及在末尾加上<gazebo></gazebo>等,具体的内容可以参考wiki上针对urdf的教程,最后将launch文件修改为
<launch>
<!--send robot urdf to the "robot_description" parameter space-->
<arg name="model" default="$(find testbot_description)/urdf/my_robot.xacro"/>
<param name = "robot_description" command="$(find xacro)/xacro $(arg model)" />
<!--launch the robot_state_publisher node and the joint_state_pulisher_gui node-->
<node pkg="robot_state_publisher" type="robot_state_publisher" name="my_robot_state_pub" />
<node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher" />
<!--launch the rviz and save the robot model as urdf.rviz-->
<arg name="rvizconfig" default="$(find testbot_description)/rviz/urdf.rviz" />
<node name="rviz" pkg="rviz" type="rviz" args="-d $(arg rvizconfig)" />
<!--some argument about gazebo -->
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
<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>
<!-- spawn robot model in gazebo world -->
<node pkg="gazebo_ros" type="spawn_model" name="urdf_spawner"
args="-z 1.0 -unpause -urdf -model robot -param robot_description" respawn="false" output="screen" />
<!-- load the joints.yaml file into XM3p_joint_state_controller namespace for gazebo -->
<rosparam command="load"
file="$(find testbot_description)/config/joints.yaml"
ns="XM3p_joint_state_controller" />
<!-- load the each_joint.yaml file into namespace to controll the position of each joints -->
<rosparam command="load"
file="$(find testbot_description)/config/each_joints.yaml"
ns="XM3p_joint_position_controller" />
<!-- load the .yaml file into namespace to controll the position of group joints -->
<rosparam command="load"
file="$(find testbot_description)/config/group_joints_control.yaml"
ns="XM3p_group_joint_position_controller" />
<node name="XM3p_controller_spawner" pkg="controller_manager" type="spawner"
args="XM3p_joint_state_controller
XM3p_joint_position_controller
XM3p_group_joint_position_controller
--shutdown-timeout 3"/>
</launch>
获得的效果为

目前的进度:在gazebo中能生成我所导入的urdf文件,并且可以通过rostopic pub 指令来发送控制指令控制机械臂的关节转动
下一步要解决的问题:
机械臂在gazebo的empty.world中并不是固定在地面上的
相机模型的导入
完善仿真环境

