小猫

版主
  • 主题:39
  • 回复:39
  • 金钱:132
  • 积分:181
      我们使用rviz进行了TurtleBot的仿真,而且使用urdf文件建立了自己的机器人smartcar,本篇博客是将两者进行结合,使用smartcar机器人在rviz中进行仿真。一、模型完善
        之前我们使用的都是urdf文件格式的模型,在很多情况下,ROSurdf文件的支持并不是很好,使用宏定义的.xacro文件兼容性更好,扩展性也更好。所以我们把之前的urdf文件重新整理编写成.xacro文件。
xacro文件主要分为三部分:
1、机器人主体
  1. [plain] view plaincopy
  2. <?xml version="1.0"?>  
  3. <robot name="smartcar" xmlns:xacro="http://ros.org/wiki/xacro">  
  4.   <property name="M_PI" value="3.14159"/>  
  5.   
  6.   <!-- Macro for SmartCar body. Including Gazebo extensions, but does not include Kinect -->  
  7.   <include filename="$(find smartcar_description)/urdf/gazebo.urdf.xacro"/>  
  8.   
  9.   <property name="base_x" value="0.33" />  
  10.   <property name="base_y" value="0.33" />  
  11.   
  12.   <xacro:macro name="smartcar_body">  
  13.   
  14.   
  15.     <link name="base_link">  
  16.     <inertial>  
  17.       <origin xyz="0 0 0.055"/>  
  18.       <mass value="1.0" />  
  19.       <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
  20.     </inertial>  
  21.     <visual>  
  22.       <geometry>  
  23.         <box size="0.25 .16 .05"/>  
  24.       </geometry>  
  25.       <origin rpy="0 0 0" xyz="0 0 0.055"/>  
  26.       <material name="blue">  
  27.       <color rgba="0 0 .8 1"/>  
  28.       </material>  
  29.    </visual>  
  30.    <collision>  
  31.       <origin rpy="0 0 0" xyz="0 0 0.055"/>  
  32.       <geometry>  
  33.         <box size="0.25 .16 .05" />  
  34.       </geometry>  
  35.     </collision>  
  36.   </link>  
  37.   
  38.   
  39. <link name="left_front_wheel">  
  40.     <inertial>  
  41.       <origin  xyz="0.08 0.08 0.025"/>  
  42.       <mass value="0.1" />  
  43.        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
  44.     </inertial>  
  45.     <visual>  
  46.       <geometry>  
  47.         <cylinder length=".02" radius="0.025"/>  
  48.       </geometry>  
  49.       <material name="black">  
  50.         <color rgba="0 0 0 1"/>  
  51.       </material>  
  52.     </visual>  
  53.     <collision>  
  54.       <origin rpy="0 1.57075 1.57075" xyz="0.08 0.08 0.025"/>  
  55.       <geometry>  
  56.          <cylinder length=".02" radius="0.025"/>  
  57.       </geometry>  
  58.     </collision>  
  59.   </link>  
  60.   
  61.   <joint name="left_front_wheel_joint" type="continuous">  
  62.     <axis xyz="0 0 1"/>  
  63.     <parent link="base_link"/>  
  64.     <child link="left_front_wheel"/>  
  65.     <origin rpy="0 1.57075 1.57075" xyz="0.08 0.08 0.025"/>  
  66.     <limit effort="100" velocity="100"/>  
  67.     <joint_properties damping="0.0" friction="0.0"/>  
  68.   </joint>  
  69.   
  70.   <link name="right_front_wheel">  
  71.     <inertial>  
  72.       <origin xyz="0.08 -0.08 0.025"/>  
  73.       <mass value="0.1" />  
  74.        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
  75.     </inertial>  
  76.     <visual>  
  77.       <geometry>  
  78.         <cylinder length=".02" radius="0.025"/>  
  79.       </geometry>  
  80.       <material name="black">  
  81.         <color rgba="0 0 0 1"/>  
  82.       </material>  
  83.     </visual>  
  84.     <collision>  
  85.       <origin rpy="0 1.57075 1.57075" xyz="0.08 -0.08 0.025"/>  
  86.       <geometry>  
  87.          <cylinder length=".02" radius="0.025"/>  
  88.       </geometry>  
  89.     </collision>  
  90.   </link>  
  91.   
  92.   <joint name="right_front_wheel_joint" type="continuous">  
  93.     <axis xyz="0 0 1"/>  
  94.     <parent link="base_link"/>  
  95.     <child link="right_front_wheel"/>  
  96.     <origin rpy="0 1.57075 1.57075" xyz="0.08 -0.08 0.025"/>  
  97.     <limit effort="100" velocity="100"/>  
  98.     <joint_properties damping="0.0" friction="0.0"/>  
  99. </joint>  
  100.   
  101. <link name="left_back_wheel">  
  102.     <inertial>  
  103.       <origin xyz="-0.08 0.08 0.025"/>  
  104.       <mass value="0.1" />  
  105.        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
  106.     </inertial>  
  107.     <visual>  
  108.       <geometry>  
  109.         <cylinder length=".02" radius="0.025"/>  
  110.       </geometry>  
  111.       <material name="black">  
  112.         <color rgba="0 0 0 1"/>  
  113.       </material>  
  114.    </visual>  
  115.    <collision>  
  116.        <origin rpy="0 1.57075 1.57075" xyz="-0.08 0.08 0.025"/>  
  117.       <geometry>  
  118.          <cylinder length=".02" radius="0.025"/>  
  119.       </geometry>  
  120.     </collision>  
  121.   </link>  
  122.   
  123.   <joint name="left_back_wheel_joint" type="continuous">  
  124.     <axis xyz="0 0 1"/>  
  125.     <parent link="base_link"/>  
  126.     <child link="left_back_wheel"/>  
  127.     <origin rpy="0 1.57075 1.57075" xyz="-0.08 0.08 0.025"/>  
  128.     <limit effort="100" velocity="100"/>  
  129.     <joint_properties damping="0.0" friction="0.0"/>  
  130.   </joint>  
  131.   
  132.   <link name="right_back_wheel">  
  133.     <inertial>  
  134.        <origin xyz="-0.08 -0.08 0.025"/>  
  135.        <mass value="0.1" />  
  136.        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
  137.     </inertial>  
  138.     <visual>  
  139.       <geometry>  
  140.         <cylinder length=".02" radius="0.025"/>  
  141.       </geometry>  
  142.       <material name="black">  
  143.         <color rgba="0 0 0 1"/>  
  144.       </material>  
  145.    </visual>  
  146.    <collision>  
  147.       <origin rpy="0 1.57075 1.57075" xyz="-0.08 -0.08 0.025"/>  
  148.       <geometry>  
  149.          <cylinder length=".02" radius="0.025"/>  
  150.       </geometry>  
  151.     </collision>  
  152.   </link>  
  153.   
  154.   
  155.   <joint name="right_back_wheel_joint" type="continuous">  
  156.     <axis xyz="0 0 1"/>  
  157.     <parent link="base_link"/>  
  158.     <child link="right_back_wheel"/>  
  159.     <origin rpy="0 1.57075 1.57075" xyz="-0.08 -0.08 0.025"/>  
  160.     <limit effort="100" velocity="100"/>  
  161.     <joint_properties damping="0.0" friction="0.0"/>  
  162.   </joint>  
  163.   
  164.   <link name="head">  
  165.     <inertial>  
  166.       <origin xyz="0.08 0 0.08"/>  
  167.       <mass value="0.1" />  
  168.       <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
  169.     </inertial>  
  170.     <visual>  
  171.       <geometry>  
  172.         <box size=".02 .03 .03"/>  
  173.       </geometry>  
  174.       <material name="white">  
  175.         <color rgba="1 1 1 1"/>  
  176.       </material>  
  177.      </visual>  
  178.      <collision>  
  179.       <origin xyz="0.08 0 0.08"/>  
  180.       <geometry>  
  181.          <cylinder length=".02" radius="0.025"/>  
  182.       </geometry>  
  183.     </collision>  
  184.   </link>  
  185.   
  186.   <joint name="tobox" type="fixed">  
  187.     <parent link="base_link"/>  
  188.     <child link="head"/>  
  189.     <origin xyz="0.08 0 0.08"/>  
  190.   </joint>  
  191.   </xacro:macro>  
  192.   
  193. </robot>  
复制代码
2gazebo属性部分
  1. [plain] view plaincopy
  2. <?xml version="1.0"?>  
  3.   
  4. <robot xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"   
  5.     xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"   
  6.     xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"   
  7.     xmlns:xacro="http://ros.org/wiki/xacro"   
  8.     name="smartcar_gazebo">  
  9.   
  10. <!-- ASUS Xtion PRO camera for simulation -->  
  11. <!-- gazebo_ros_wge100 plugin is in kt2_gazebo_plugins package -->  
  12. <xacro:macro name="smartcar_sim">  
  13.     <gazebo reference="base_link">  
  14.         <material>Gazebo/Blue</material>  
  15.     </gazebo>  
  16.   
  17.     <gazebo reference="right_front_wheel">  
  18.         <material>Gazebo/FlatBlack</material>  
  19.     </gazebo>  
  20.   
  21.     <gazebo reference="right_back_wheel">  
  22.         <material>Gazebo/FlatBlack</material>  
  23.     </gazebo>  
  24.   
  25.     <gazebo reference="left_front_wheel">  
  26.         <material>Gazebo/FlatBlack</material>  
  27.     </gazebo>  
  28.   
  29.     <gazebo reference="left_back_wheel">  
  30.         <material>Gazebo/FlatBlack</material>  
  31.     </gazebo>  
  32.   
  33.     <gazebo reference="head">  
  34.         <material>Gazebo/White</material>  
  35.     </gazebo>  
  36.   
  37. </xacro:macro>  
  38.   
  39. </robot>  
复制代码
3、主文件
  1. [cpp] view plaincopy
  2. <span style="font-size:14px;"><?xml version="1.0"?>  
  3.   
  4. <robot name="smartcar"   
  5.     xmlns:xi="http://www.w3.org/2001/XInclude"  
  6.     xmlns:gazebo="http://playerstage.sourceforge.net/gazebo/xmlschema/#gz"  
  7.     xmlns:model="http://playerstage.sourceforge.net/gazebo/xmlschema/#model"  
  8.     xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"  
  9.     xmlns:body="http://playerstage.sourceforge.net/gazebo/xmlschema/#body"  
  10.     xmlns:geom="http://playerstage.sourceforge.net/gazebo/xmlschema/#geom"  
  11.     xmlns:joint="http://playerstage.sourceforge.net/gazebo/xmlschema/#joint"  
  12.     xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"  
  13.     xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"  
  14.     xmlns:rendering="http://playerstage.sourceforge.net/gazebo/xmlschema/#rendering"  
  15.     xmlns:renderable="http://playerstage.sourceforge.net/gazebo/xmlschema/#renderable"  
  16.     xmlns:physics="http://playerstage.sourceforge.net/gazebo/xmlschema/#physics"  
  17.     xmlns:xacro="http://ros.org/wiki/xacro">  
  18.   
  19.   <include filename="$(find smartcar_description)/urdf/smartcar_body.urdf.xacro" />  
  20.   
  21.   <!-- Body of SmartCar, with plates, standoffs and Create (including sim sensors) -->  
  22.   <smartcar_body/>  
  23.   
  24.   <smartcar_sim/>  
  25.   
  26. </robot></span>  
复制代码
二、lanuch文件        launch文件中要启动节点和模拟器。
  1. [plain] view plaincopy
  2. <launch>  
  3.     <param name="/use_sim_time" value="false" />  
  4.       
  5.     <!-- Load the URDF/Xacro model of our robot -->  
  6.     <arg name="urdf_file" default="$(find xacro)/xacro.py '$(find smartcar_description)/urdf/smartcar.urdf.xacro'" />  
  7.     <arg name="gui" default="false" />  
  8.   
  9.     <param name="robot_description" command="$(arg urdf_file)" />  
  10.     <param name="use_gui" value="$(arg gui)"/>  
  11.   
  12.     <node name="arbotix" pkg="arbotix_python" type="driver.py" output="screen">  
  13.         <rosparam file="$(find smartcar_description)/config/smartcar_arbotix.yaml" command="load" />  
  14.         <param name="sim" value="true"/>  
  15.     </node>  
  16.   
  17.     <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" >  
  18.     </node>  
  19.   
  20.     <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher">  
  21.         <param name="publish_frequency" type="double" value="20.0" />  
  22.     </node>  
  23.   
  24.      <!-- We need a static transforms for the wheels -->  
  25.     <node pkg="tf" type="static_transform_publisher" name="odom_left_wheel_broadcaster" args="0 0 0 0 0 0 /base_link /left_front_link 100" />  
  26.     <node pkg="tf" type="static_transform_publisher" name="odom_right_wheel_broadcaster" args="0 0 0 0 0 0 /base_link /right_front_link 100" />  
  27.   
  28.     <node name="rviz" pkg="rviz" type="rviz" args="-d $(find smartcar_description)/urdf.vcg" />  
  29. </launch>  
复制代码
三、仿真测试
        首先运行lanuch,既可以看到rviz中的机器人:
  1. [plain] view plaincopy
  2. roslaunch smartcar_description smartcar_display.rviz.launch  
复制代码


  发布一条动作的消息。
  1. [plain] view plaincopy
  2. rostopic pub -r 10 /cmd_vel geometry_msgs/Twist '{linear: {x: 0.5, y: 0, z: 0}, angular: {x: 0, y: 0, z: 0.5}}'  
复制代码

四、节点关系