• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

xacro机器人模型文件转urdf文件怎么编写launch文件启动gazebo仿真和在rviz2内显示模型

武飞扬头像
JT_BOT
帮助1

urdf文件很直白,每个零件的</link>  </joint>都要编写一遍,每个零件数据都要自己算出来结果,很麻烦,但是用起来很简单。xacro写的模型文件可以把好多常量提前定义出来,不同大小的机器人只要只要改一下常量,机器人模型就可以重新生成,代码可以复用,编写起来简单多了,但是编写launch启动文件麻烦一些。

urdf编写的小车模型文件:

  1.  
    <!-- base.urdf -->
  2.  
    <?xml version="1.0" ?>
  3.  
    <robot name="jtbot">
  4.  
    <!-- 机器人底盘 -->
  5.  
    <link name="base_link">
  6.  
    <visual>
  7.  
    <geometry>
  8.  
    <box size="0.46 0.46 0.11"/>
  9.  
    </geometry>
  10.  
    <material name="Cyan">
  11.  
    <color rgba="0 1.0 1.0 1.0"/>
  12.  
    </material>
  13.  
    </visual>
  14.  
    <!-- 碰撞区域 -->
  15.  
    <collision>
  16.  
    <geometry>
  17.  
    <box size="0.46 0.46 0.11"/>
  18.  
    </geometry>
  19.  
    </collision>
  20.  
    <inertial>
  21.  
    <origin rpy="1.5707963267948966 0 1.5707963267948966" xyz="0 0 0"/>
  22.  
    <mass value="15"/>
  23.  
    <inertia ixx="0.279625" ixy="0.0" ixz="0.0" iyy="0.529" iyz="0.0" izz="0.279625"/>
  24.  
    </inertial>
  25.  
    </link>
  26.  
    <!-- 机器人 Footprint -->
  27.  
    <link name="base_footprint"/>
  28.  
    <!-- 底盘关节 -->
  29.  
    <joint name="base_joint" type="fixed">
  30.  
    <parent link="base_link"/>
  31.  
    <child link="base_footprint"/>
  32.  
    <origin rpy="0 0 0" xyz="0.0 0.0 -0.1325"/>
  33.  
    </joint>
  34.  
    <link name="left_wheel_link">
  35.  
    <visual>
  36.  
    <origin rpy="1.5707963267948966 0 0" xyz="0 0 0"/>
  37.  
    <geometry>
  38.  
    <cylinder length="0.06" radius="0.0775"/>
  39.  
    </geometry>
  40.  
    <material name="Gray">
  41.  
    <color rgba="0.5 0.5 0.5 1.0"/>
  42.  
    </material>
  43.  
    </visual>
  44.  
    <!-- 碰撞区域 -->
  45.  
    <collision>
  46.  
    <origin rpy="1.5707963267948966 0 0" xyz="0 0 0"/>
  47.  
    <geometry>
  48.  
    <cylinder length="0.06" radius="0.0775"/>
  49.  
    </geometry>
  50.  
    </collision>
  51.  
    <inertial>
  52.  
    <origin rpy="1.5707963267948966 0 0" xyz="0 0 0"/>
  53.  
    <mass value="0.8"/>
  54.  
    <inertia ixx="0.0014412499999999998" ixy="0" ixz="0" iyy="0.0014412499999999998" iyz="0" izz="0.0024025"/>
  55.  
    </inertial>
  56.  
    </link>
  57.  
    <!-- 轮子关节 -->
  58.  
    <joint name="left_wheel_joint" type="continuous">
  59.  
    <parent link="base_link"/>
  60.  
    <child link="left_wheel_link"/>
  61.  
    <origin rpy="0 0 0" xyz="0.15 0.27 -0.055"/>
  62.  
    <axis xyz="0 1 0"/>
  63.  
    </joint>
  64.  
    <link name="right_wheel_link">
  65.  
    <visual>
  66.  
    <origin rpy="1.5707963267948966 0 0" xyz="0 0 0"/>
  67.  
    <geometry>
  68.  
    <cylinder length="0.06" radius="0.0775"/>
  69.  
    </geometry>
  70.  
    <material name="Gray">
  71.  
    <color rgba="0.5 0.5 0.5 1.0"/>
  72.  
    </material>
  73.  
    </visual>
  74.  
    <!-- 碰撞区域 -->
  75.  
    <collision>
  76.  
    <origin rpy="1.5707963267948966 0 0" xyz="0 0 0"/>
  77.  
    <geometry>
  78.  
    <cylinder length="0.06" radius="0.0775"/>
  79.  
    </geometry>
  80.  
    </collision>
  81.  
    <inertial>
  82.  
    <origin rpy="1.5707963267948966 0 0" xyz="0 0 0"/>
  83.  
    <mass value="0.8"/>
  84.  
    <inertia ixx="0.0014412499999999998" ixy="0" ixz="0" iyy="0.0014412499999999998" iyz="0" izz="0.0024025"/>
  85.  
    </inertial>
  86.  
    </link>
  87.  
    <!-- 轮子关节 -->
  88.  
    <joint name="right_wheel_joint" type="continuous">
  89.  
    <parent link="base_link"/>
  90.  
    <child link="right_wheel_link"/>
  91.  
    <origin rpy="0 0 0" xyz="0.15 -0.27 -0.055"/>
  92.  
    <axis xyz="0 1 0"/>
  93.  
    </joint>
  94.  
    <!-- 支撑轮 -->
  95.  
    <link name="caster_link">
  96.  
    <visual>
  97.  
    <geometry>
  98.  
    <sphere radius="0.03875"/>
  99.  
    </geometry>
  100.  
    <material name="Cyan">
  101.  
    <color rgba="0 1.0 1.0 1.0"/>
  102.  
    </material>
  103.  
    </visual>
  104.  
    <!-- 碰撞区域 -->
  105.  
    <collision>
  106.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  107.  
    <geometry>
  108.  
    <sphere radius="0.03875"/>
  109.  
    </geometry>
  110.  
    </collision>
  111.  
    <inertial>
  112.  
    <mass value="0.5"/>
  113.  
    <inertia ixx="0.0003003125" ixy="0.0" ixz="0.0" iyy="0.0003003125" iyz="0.0" izz="0.0003003125"/>
  114.  
    </inertial>
  115.  
    </link>
  116.  
    <!-- 支撑轮gazebo颜色 -->
  117.  
    <gazebo reference="caster_link">
  118.  
    <material>Gazebo/Black</material>
  119.  
    </gazebo>
  120.  
    <!-- 支撑轮gazebo摩擦力 -->
  121.  
    <gazebo reference="caster_link">
  122.  
    <mu1 value="0.0"/>
  123.  
    <mu2 value="0.0"/>
  124.  
    <kp value="1000000.0"/>
  125.  
    <kd value="10.0"/>
  126.  
    </gazebo>
  127.  
    <!-- 支撑轮关节 -->
  128.  
    <joint name="caster_joint" type="fixed">
  129.  
    <parent link="base_link"/>
  130.  
    <child link="caster_link"/>
  131.  
    <origin rpy="0 0 0" xyz="-0.205 0.0 -0.09375"/>
  132.  
    </joint>
  133.  
    <!-- imu -->
  134.  
    <link name="imu_link">
  135.  
    <visual>
  136.  
    <geometry>
  137.  
    <box size="0.06 0.03 0.03"/>
  138.  
    </geometry>
  139.  
    </visual>
  140.  
    <!-- 碰撞区域 -->
  141.  
    <collision>
  142.  
    <geometry>
  143.  
    <box size="0.06 0.03 0.03"/>
  144.  
    </geometry>
  145.  
    </collision>
  146.  
    <inertial>
  147.  
    <origin rpy="1.5707963267948966 0 1.5707963267948966" xyz="0 0 0"/>
  148.  
    <mass value="0.1"/>
  149.  
    <inertia ixx="0.0001666666666666667" ixy="0.0" ixz="0.0" iyy="0.0001666666666666667" iyz="0.0" izz="0.0001666666666666667"/>
  150.  
    </inertial>
  151.  
    </link>
  152.  
    <!-- imu关节 -->
  153.  
    <joint name="imu_joint" type="fixed">
  154.  
    <parent link="base_link"/>
  155.  
    <child link="imu_link"/>
  156.  
    <origin xyz="-0.05 0 -0.055"/>
  157.  
    </joint>
  158.  
    <!-- imu仿真插件 -->
  159.  
    <gazebo reference="imu_link">
  160.  
    <sensor name="imu_sensor" type="imu">
  161.  
    <plugin filename="libgazebo_ros_imu_sensor.so" name="imu_plugin">
  162.  
    <ros>
  163.  
    <!-- 命名空间 -->
  164.  
    <!-- <namespace>/demo</namespace> -->
  165.  
    <remapping>~/out:=imu</remapping>
  166.  
    </ros>
  167.  
    <!-- 初始方位_参考 -->
  168.  
    <initial_orientation_as_reference>false</initial_orientation_as_reference>
  169.  
    </plugin>
  170.  
    <always_on>true</always_on>
  171.  
    <!-- 更新频率 -->
  172.  
    <update_rate>100</update_rate>
  173.  
    <visualize>true</visualize>
  174.  
    <imu>
  175.  
    <!-- 角速度 -->
  176.  
    <angular_velocity>
  177.  
    <x>
  178.  
    <noise type="gaussian">
  179.  
    <mean>0.0</mean>
  180.  
    <stddev>2e-4</stddev>
  181.  
    <bias_mean>0.0000075</bias_mean>
  182.  
    <bias_stddev>0.0000008</bias_stddev>
  183.  
    </noise>
  184.  
    </x>
  185.  
    <y>
  186.  
    <noise type="gaussian">
  187.  
    <mean>0.0</mean>
  188.  
    <stddev>2e-4</stddev>
  189.  
    <bias_mean>0.0000075</bias_mean>
  190.  
    <bias_stddev>0.0000008</bias_stddev>
  191.  
    </noise>
  192.  
    </y>
  193.  
    <z>
  194.  
    <noise type="gaussian">
  195.  
    <mean>0.0</mean>
  196.  
    <stddev>2e-4</stddev>
  197.  
    <bias_mean>0.0000075</bias_mean>
  198.  
    <bias_stddev>0.0000008</bias_stddev>
  199.  
    </noise>
  200.  
    </z>
  201.  
    </angular_velocity>
  202.  
    <!-- 线性加速度 -->
  203.  
    <linear_acceleration>
  204.  
    <x>
  205.  
    <noise type="gaussian">
  206.  
    <mean>0.0</mean>
  207.  
    <stddev>1.7e-2</stddev>
  208.  
    <bias_mean>0.1</bias_mean>
  209.  
    <bias_stddev>0.001</bias_stddev>
  210.  
    </noise>
  211.  
    </x>
  212.  
    <y>
  213.  
    <noise type="gaussian">
  214.  
    <mean>0.0</mean>
  215.  
    <stddev>1.7e-2</stddev>
  216.  
    <bias_mean>0.1</bias_mean>
  217.  
    <bias_stddev>0.001</bias_stddev>
  218.  
    </noise>
  219.  
    </y>
  220.  
    <z>
  221.  
    <noise type="gaussian">
  222.  
    <mean>0.0</mean>
  223.  
    <stddev>1.7e-2</stddev>
  224.  
    <bias_mean>0.1</bias_mean>
  225.  
    <bias_stddev>0.001</bias_stddev>
  226.  
    </noise>
  227.  
    </z>
  228.  
    </linear_acceleration>
  229.  
    </imu>
  230.  
    </sensor>
  231.  
    </gazebo>
  232.  
    <!-- 差速驱动仿真插件 -->
  233.  
    <gazebo>
  234.  
    <plugin filename="libgazebo_ros_diff_drive.so" name="diff_drive">
  235.  
    <ros>
  236.  
    <!-- 命名空间 -->
  237.  
    <!-- <namespace>/demo</namespace> -->
  238.  
    </ros>
  239.  
    <!-- 左右轮子 -->
  240.  
    <left_joint>left_wheel_joint</left_joint>
  241.  
    <right_joint>right_wheel_joint</right_joint>
  242.  
    <!-- 轮距 轮子直径 -->
  243.  
    <wheel_separation>0.52</wheel_separation>
  244.  
    <!-- <wheel_separation>0.52</wheel_separation> -->
  245.  
    <wheel_diameter>0.155</wheel_diameter>
  246.  
    <!-- <wheel_diameter>0.155</wheel_diameter> -->
  247.  
    <!-- 最大扭矩 最大加速度 -->
  248.  
    <max_wheel_torque>20</max_wheel_torque>
  249.  
    <max_wheel_acceleration>1.0</max_wheel_acceleration>
  250.  
    <!-- 输出 -->
  251.  
    <!-- 是否发布里程计 -->
  252.  
    <publish_odom>true</publish_odom>
  253.  
    <!-- 是否发布里程计的tf开关 -->
  254.  
    <publish_odom_tf>true</publish_odom_tf>
  255.  
    <!-- 是否发布轮子的tf数据开关 -->
  256.  
    <publish_wheel_tf>true</publish_wheel_tf>
  257.  
    <!-- 里程计的framed ID,最终体现在话题和TF上 -->
  258.  
    <odometry_frame>odom</odometry_frame>
  259.  
    <!-- 机器人的基础frame的ID -->
  260.  
    <robot_base_frame>base_link</robot_base_frame>
  261.  
    </plugin>
  262.  
    </gazebo>
  263.  
    <!-- 雷达 -->
  264.  
    <link name="laser">
  265.  
    <visual>
  266.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  267.  
    <geometry>
  268.  
    <cylinder length="0.04" radius="0.04"/>
  269.  
    </geometry>
  270.  
    </visual>
  271.  
    <!-- 惯性属性 -->
  272.  
    <inertial>
  273.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  274.  
    <mass value="0.125"/>
  275.  
    <inertia ixx="0.001" ixy="0" ixz="0" iyy="0.001" iyz="0" izz="0.001"/>
  276.  
    </inertial>
  277.  
    <!-- 碰撞区域 -->
  278.  
    <collision>
  279.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  280.  
    <geometry>
  281.  
    <cylinder length="0.04" radius="0.04"/>
  282.  
    </geometry>
  283.  
    </collision>
  284.  
    </link>
  285.  
    <!-- 雷达关节 -->
  286.  
    <joint name="laser_joint" type="fixed">
  287.  
    <parent link="base_link"/>
  288.  
    <child link="laser"/>
  289.  
    <origin rpy="0 0 0" xyz="0.16 0 0.078"/>
  290.  
    </joint>
  291.  
    <gazebo reference="laser">
  292.  
    <sensor name="laser" type="ray">
  293.  
    <always_on>true</always_on>
  294.  
    <visualize>false</visualize>
  295.  
    <update_rate>5</update_rate>
  296.  
    <ray>
  297.  
    <scan>
  298.  
    <horizontal>
  299.  
    <samples>360</samples>
  300.  
    <resolution>1.000000</resolution>
  301.  
    <min_angle>0.000000</min_angle>
  302.  
    <max_angle>6.280000</max_angle>
  303.  
    </horizontal>
  304.  
    </scan>
  305.  
    <range>
  306.  
    <min>0.120000</min>
  307.  
    <max>3.5</max>
  308.  
    <resolution>0.015000</resolution>
  309.  
    </range>
  310.  
    <noise>
  311.  
    <type>gaussian</type>
  312.  
    <mean>0.0</mean>
  313.  
    <stddev>0.01</stddev>
  314.  
    </noise>
  315.  
    </ray>
  316.  
    <plugin filename="libgazebo_ros_ray_sensor.so" name="scan">
  317.  
    <ros>
  318.  
    <remapping>~/out:=scan</remapping>
  319.  
    </ros>
  320.  
    <output_type>sensor_msgs/LaserScan</output_type>
  321.  
    <frame_name>laser</frame_name>
  322.  
    </plugin>
  323.  
    </sensor>
  324.  
    </gazebo>
  325.  
    <!-- 相机 -->
  326.  
    <link name="camera_link">
  327.  
    <visual>
  328.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  329.  
    <geometry>
  330.  
    <box size="0.015 0.130 0.022"/>
  331.  
    </geometry>
  332.  
    </visual>
  333.  
    <!-- 碰撞区域 -->
  334.  
    <collision>
  335.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  336.  
    <geometry>
  337.  
    <box size="0.015 0.130 0.022"/>
  338.  
    </geometry>
  339.  
    </collision>
  340.  
    <!-- 惯性属性 -->
  341.  
    <inertial>
  342.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  343.  
    <mass value="0.035"/>
  344.  
    <inertia ixx="0.001" ixy="0" ixz="0" iyy="0.001" iyz="0" izz="0.001"/>
  345.  
    </inertial>
  346.  
    </link>
  347.  
    <!-- 相机关节 -->
  348.  
    <joint name="camera_joint" type="fixed">
  349.  
    <parent link="base_link"/>
  350.  
    <child link="camera_link"/>
  351.  
    <origin rpy="0 0 0" xyz="0.16 0 0.11"/>
  352.  
    </joint>
  353.  
    <!-- 深度相机 -->
  354.  
    <link name="camera_depth_frame"/>
  355.  
    <joint name="camera_depth_joint" type="fixed">
  356.  
    <origin rpy="0 0 0" xyz="0 0 0"/>
  357.  
    <parent link="camera_link"/>
  358.  
    <child link="camera_depth_frame"/>
  359.  
    </joint>
  360.  
    <!-- 相机仿真 -->
  361.  
    <gazebo reference="camera_depth_link">
  362.  
    <sensor name="depth_camera" type="depth">
  363.  
    <visualize>true</visualize>
  364.  
    <update_rate>30.0</update_rate>
  365.  
    <camera name="camera">
  366.  
    <horizontal_fov>1.047198</horizontal_fov>
  367.  
    <image>
  368.  
    <width>640</width>
  369.  
    <height>480</height>
  370.  
    <format>R8G8B8</format>
  371.  
    </image>
  372.  
    <clip>
  373.  
    <near>0.05</near>
  374.  
    <far>3</far>
  375.  
    </clip>
  376.  
    </camera>
  377.  
    <plugin filename="libgazebo_ros_camera.so" name="depth_camera_controller">
  378.  
    <baseline>0.2</baseline>
  379.  
    <alwaysOn>true</alwaysOn>
  380.  
    <updateRate>0.0</updateRate>
  381.  
    <frame_name>camera_depth_frame</frame_name>
  382.  
    <pointCloudCutoff>0.5</pointCloudCutoff>
  383.  
    <pointCloudCutoffMax>3.0</pointCloudCutoffMax>
  384.  
    <distortionK1>0</distortionK1>
  385.  
    <distortionK2>0</distortionK2>
  386.  
    <distortionK3>0</distortionK3>
  387.  
    <distortionT1>0</distortionT1>
  388.  
    <distortionT2>0</distortionT2>
  389.  
    <CxPrime>0</CxPrime>
  390.  
    <Cx>0</Cx>
  391.  
    <Cy>0</Cy>
  392.  
    <focalLength>0</focalLength>
  393.  
    <hackBaseline>0</hackBaseline>
  394.  
    </plugin>
  395.  
    </sensor>
  396.  
    </gazebo>
  397.  
    </robot>
学新通

xacro编写的小车模型文件:

  1.  
    <!-- base.urdf.xacro -->
  2.  
    <?xml version="1.0"?>
  3.  
    <robot name="jtbot"
  4.  
    xmlns:xacro="http://ros.org/wiki/xacro">
  5.  
     
  6.  
    <!-- 定义机器人常量 -->
  7.  
    <!-- 底盘 长 宽 高 -->
  8.  
    <xacro:property name="base_width" value="0.46"/>
  9.  
    <xacro:property name="base_length" value="0.46"/>
  10.  
    <xacro:property name="base_height" value="0.11"/>
  11.  
    <!-- 轮子半径 -->
  12.  
    <xacro:property name="wheel_radius" value="0.0775"/>
  13.  
    <!-- 轮子宽度 -->
  14.  
    <xacro:property name="wheel_width" value="0.06"/>
  15.  
    <!-- 轮子和底盘的间距 -->
  16.  
    <xacro:property name="wheel_ygap" value="0.01"/>
  17.  
    <!-- 轮子z轴偏移量 -->
  18.  
    <xacro:property name="wheel_zoff" value="0.055"/>
  19.  
    <!-- 轮子x轴偏移量 -->
  20.  
    <xacro:property name="wheel_xoff" value="0.15"/>
  21.  
    <!-- 支撑轮x轴偏移量 -->
  22.  
    <xacro:property name="caster_xoff" value="0.205"/>
  23.  
     
  24.  
    <!-- 定义长方形惯性属性宏 -->
  25.  
    <xacro:macro name="box_inertia" params="m w h d">
  26.  
    <inertial>
  27.  
    <origin xyz="0 0 0" rpy="${pi/2} 0 ${pi/2}"/>
  28.  
    <mass value="${m}"/>
  29.  
    <inertia ixx="${(m/12) * (h*h d*d)}" ixy="0.0" ixz="0.0" iyy="${(m/12) * (w*w d*d)}" iyz="0.0" izz="${(m/12) * (w*w h*h)}"/>
  30.  
    </inertial>
  31.  
    </xacro:macro>
  32.  
    <!-- 定义圆柱惯性属性宏 -->
  33.  
    <xacro:macro name="cylinder_inertia" params="m r h">
  34.  
    <inertial>
  35.  
    <origin xyz="0 0 0" rpy="${pi/2} 0 0" />
  36.  
    <mass value="${m}"/>
  37.  
    <inertia ixx="${(m/12) * (3*r*r h*h)}" ixy = "0" ixz = "0" iyy="${(m/12) * (3*r*r h*h)}" iyz = "0" izz="${(m/2) * (r*r)}"/>
  38.  
    </inertial>
  39.  
    </xacro:macro>
  40.  
    <!-- 定义球体惯性属性宏 -->
  41.  
    <xacro:macro name="sphere_inertia" params="m r">
  42.  
    <inertial>
  43.  
    <mass value="${m}"/>
  44.  
    <inertia ixx="${(2/5) * m * (r*r)}" ixy="0.0" ixz="0.0" iyy="${(2/5) * m * (r*r)}" iyz="0.0" izz="${(2/5) * m * (r*r)}"/>
  45.  
    </inertial>
  46.  
    </xacro:macro>
  47.  
     
  48.  
    <!-- 机器人底盘 -->
  49.  
    <link name="base_link">
  50.  
    <visual>
  51.  
    <geometry>
  52.  
    <box size="${base_length} ${base_width} ${base_height}"/>
  53.  
    </geometry>
  54.  
    <material name="Cyan">
  55.  
    <color rgba="0 1.0 1.0 1.0"/>
  56.  
    </material>
  57.  
    </visual>
  58.  
    <!-- 碰撞区域 -->
  59.  
    <collision>
  60.  
    <geometry>
  61.  
    <box size="${base_length} ${base_width} ${base_height}"/>
  62.  
    </geometry>
  63.  
    </collision>
  64.  
    <!-- 惯性特性 -->
  65.  
    <xacro:box_inertia m="15" w="${base_width}" d="${base_length}" h="${base_height}"/>
  66.  
    </link>
  67.  
     
  68.  
    <!-- 机器人 Footprint -->
  69.  
    <link name="base_footprint"/>
  70.  
    <!-- 底盘关节 -->
  71.  
    <joint name="base_joint" type="fixed">
  72.  
    <parent link="base_link"/>
  73.  
    <child link="base_footprint"/>
  74.  
    <origin xyz="0.0 0.0 ${-(wheel_radius wheel_zoff)}" rpy="0 0 0"/>
  75.  
    </joint>
  76.  
     
  77.  
    <!-- 创建轮子宏函数 -->
  78.  
    <xacro:macro name="wheel" params="prefix x_reflect y_reflect">
  79.  
    <link name="${prefix}_link">
  80.  
    <visual>
  81.  
    <origin xyz="0 0 0" rpy="${pi/2} 0 0"/>
  82.  
    <geometry>
  83.  
    <cylinder radius="${wheel_radius}" length="${wheel_width}"/>
  84.  
    </geometry>
  85.  
    <material name="Gray">
  86.  
    <color rgba="0.5 0.5 0.5 1.0"/>
  87.  
    </material>
  88.  
    </visual>
  89.  
    <!-- 碰撞区域 -->
  90.  
    <collision>
  91.  
    <origin xyz="0 0 0" rpy="${pi/2} 0 0"/>
  92.  
    <geometry>
  93.  
    <cylinder radius="${wheel_radius}" length="${wheel_width}"/>
  94.  
    </geometry>
  95.  
    </collision>
  96.  
    <!-- 惯性属性 -->
  97.  
    <xacro:cylinder_inertia m="0.8" r="${wheel_radius}" h="${wheel_width}"/>
  98.  
    </link>
  99.  
    <!-- 轮子关节 -->
  100.  
    <joint name="${prefix}_joint" type="continuous">
  101.  
    <parent link="base_link"/>
  102.  
    <child link="${prefix}_link"/>
  103.  
    <origin xyz="${x_reflect*wheel_xoff} ${y_reflect*(base_width/2 wheel_width/2 wheel_ygap)} ${-wheel_zoff}" rpy="0 0 0"/>
  104.  
    <axis xyz="0 1 0"/>
  105.  
    </joint>
  106.  
    </xacro:macro>
  107.  
    <!-- 根据上面的宏函数实例化左右轮 -->
  108.  
    <xacro:wheel prefix="left_wheel" x_reflect="1" y_reflect="1" />
  109.  
    <xacro:wheel prefix="right_wheel" x_reflect="1" y_reflect="-1" />
  110.  
     
  111.  
    <!-- 支撑轮 -->
  112.  
    <link name="caster_link">
  113.  
    <visual>
  114.  
    <geometry>
  115.  
    <sphere radius="${(wheel_radius/2)}"/>
  116.  
    </geometry>
  117.  
    <material name="Cyan">
  118.  
    <color rgba="0 1.0 1.0 1.0"/>
  119.  
    </material>
  120.  
    </visual>
  121.  
    <!-- 碰撞区域 -->
  122.  
    <collision>
  123.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  124.  
    <geometry>
  125.  
    <sphere radius="${(wheel_radius/2)}"/>
  126.  
    </geometry>
  127.  
    </collision>
  128.  
    <!-- 惯性属性 -->
  129.  
    <xacro:sphere_inertia m="0.5" r="${(wheel_radius/2)}"/>
  130.  
    </link>
  131.  
    <!-- 支撑轮gazebo颜色 -->
  132.  
    <gazebo reference="caster_link">
  133.  
    <material>Gazebo/Black</material>
  134.  
    </gazebo>
  135.  
    <!-- 支撑轮gazebo摩擦力 -->
  136.  
    <gazebo reference="caster_link">
  137.  
    <mu1 value="0.0"/>
  138.  
    <mu2 value="0.0"/>
  139.  
    <kp value="1000000.0" />
  140.  
    <kd value="10.0" />
  141.  
    </gazebo>
  142.  
    <!-- 支撑轮关节 -->
  143.  
    <joint name="caster_joint" type="fixed">
  144.  
    <parent link="base_link"/>
  145.  
    <child link="caster_link"/>
  146.  
    <origin xyz="${-caster_xoff} 0.0 ${-(base_height wheel_radius)/2}" rpy="0 0 0"/>
  147.  
    </joint>
  148.  
    <!-- imu -->
  149.  
    <link name="imu_link">
  150.  
    <visual>
  151.  
    <geometry>
  152.  
    <box size="0.06 0.03 0.03"/>
  153.  
    </geometry>
  154.  
    </visual>
  155.  
    <!-- 碰撞区域 -->
  156.  
    <collision>
  157.  
    <geometry>
  158.  
    <box size="0.06 0.03 0.03"/>
  159.  
    </geometry>
  160.  
    </collision>
  161.  
    <!-- 惯性属性 -->
  162.  
    <xacro:box_inertia m="0.1" w="0.1" d="0.1" h="0.1"/>
  163.  
    </link>
  164.  
    <!-- imu关节 -->
  165.  
    <joint name="imu_joint" type="fixed">
  166.  
    <parent link="base_link"/>
  167.  
    <child link="imu_link"/>
  168.  
    <origin xyz="-0.05 0 -0.055"/>
  169.  
    </joint>
  170.  
    <!-- imu仿真插件 -->
  171.  
    <gazebo reference="imu_link">
  172.  
    <sensor name="imu_sensor" type="imu">
  173.  
    <plugin filename="libgazebo_ros_imu_sensor.so" name="imu_plugin">
  174.  
    <ros>
  175.  
    <!-- 命名空间 -->
  176.  
    <!-- <namespace>/demo</namespace> -->
  177.  
    <remapping>~/out:=imu</remapping>
  178.  
    </ros>
  179.  
    <!-- 初始方位_参考 -->
  180.  
    <initial_orientation_as_reference>true</initial_orientation_as_reference>
  181.  
    </plugin>
  182.  
    <always_on>true</always_on>
  183.  
    <!-- 更新频率 -->
  184.  
    <update_rate>100</update_rate>
  185.  
    <visualize>true</visualize>
  186.  
    <imu>
  187.  
    <!-- 角速度 -->
  188.  
    <angular_velocity>
  189.  
    <x>
  190.  
    <noise type="gaussian">
  191.  
    <mean>0.0</mean>
  192.  
    <stddev>2e-4</stddev>
  193.  
    <bias_mean>0.0000075</bias_mean>
  194.  
    <bias_stddev>0.0000008</bias_stddev>
  195.  
    </noise>
  196.  
    </x>
  197.  
    <y>
  198.  
    <noise type="gaussian">
  199.  
    <mean>0.0</mean>
  200.  
    <stddev>2e-4</stddev>
  201.  
    <bias_mean>0.0000075</bias_mean>
  202.  
    <bias_stddev>0.0000008</bias_stddev>
  203.  
    </noise>
  204.  
    </y>
  205.  
    <z>
  206.  
    <noise type="gaussian">
  207.  
    <mean>0.0</mean>
  208.  
    <stddev>2e-4</stddev>
  209.  
    <bias_mean>0.0000075</bias_mean>
  210.  
    <bias_stddev>0.0000008</bias_stddev>
  211.  
    </noise>
  212.  
    </z>
  213.  
    </angular_velocity>
  214.  
    <!-- 线性加速度 -->
  215.  
    <linear_acceleration>
  216.  
    <x>
  217.  
    <noise type="gaussian">
  218.  
    <mean>0.0</mean>
  219.  
    <stddev>1.7e-2</stddev>
  220.  
    <bias_mean>0.1</bias_mean>
  221.  
    <bias_stddev>0.001</bias_stddev>
  222.  
    </noise>
  223.  
    </x>
  224.  
    <y>
  225.  
    <noise type="gaussian">
  226.  
    <mean>0.0</mean>
  227.  
    <stddev>1.7e-2</stddev>
  228.  
    <bias_mean>0.1</bias_mean>
  229.  
    <bias_stddev>0.001</bias_stddev>
  230.  
    </noise>
  231.  
    </y>
  232.  
    <z>
  233.  
    <noise type="gaussian">
  234.  
    <mean>0.0</mean>
  235.  
    <stddev>1.7e-2</stddev>
  236.  
    <bias_mean>0.1</bias_mean>
  237.  
    <bias_stddev>0.001</bias_stddev>
  238.  
    </noise>
  239.  
    </z>
  240.  
    </linear_acceleration>
  241.  
    </imu>
  242.  
    </sensor>
  243.  
    </gazebo>
  244.  
    <!-- 差速驱动仿真插件 -->
  245.  
    <gazebo>
  246.  
    <plugin name='diff_drive' filename='libgazebo_ros_diff_drive.so'>
  247.  
    <ros>
  248.  
    <!-- 命名空间 -->
  249.  
    <!-- <namespace>/demo</namespace> -->
  250.  
    </ros>
  251.  
     
  252.  
    <!-- 左右轮子 -->
  253.  
    <left_joint>left_wheel_joint</left_joint>
  254.  
    <right_joint>right_wheel_joint</right_joint>
  255.  
     
  256.  
    <!-- 轮距 轮子直径 -->
  257.  
    <wheel_separation>${base_width wheel_width}</wheel_separation>
  258.  
    <!-- <wheel_separation>0.52</wheel_separation> -->
  259.  
    <wheel_diameter>${wheel_radius*2}</wheel_diameter>
  260.  
    <!-- <wheel_diameter>0.155</wheel_diameter> -->
  261.  
     
  262.  
    <!-- 最大扭矩 最大加速度 -->
  263.  
    <max_wheel_torque>20</max_wheel_torque>
  264.  
    <max_wheel_acceleration>1.0</max_wheel_acceleration>
  265.  
     
  266.  
    <!-- 输出 -->
  267.  
    <!-- 是否发布里程计 -->
  268.  
    <publish_odom>true</publish_odom>
  269.  
    <!-- 是否发布里程计的tf开关 -->
  270.  
    <publish_odom_tf>true</publish_odom_tf>
  271.  
    <!-- 是否发布轮子的tf数据开关 -->
  272.  
    <publish_wheel_tf>true</publish_wheel_tf>
  273.  
     
  274.  
    <!-- 里程计的framed ID,最终体现在话题和TF上 -->
  275.  
    <odometry_frame>odom</odometry_frame>
  276.  
    <!-- 机器人的基础frame的ID -->
  277.  
    <robot_base_frame>base_link</robot_base_frame>
  278.  
    </plugin>
  279.  
    </gazebo>
  280.  
    <!-- 雷达 -->
  281.  
    <link name="laser">
  282.  
    <visual>
  283.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  284.  
    <geometry>
  285.  
    <cylinder radius="0.04" length="0.04"/>
  286.  
    </geometry>
  287.  
    </visual>
  288.  
    <!-- 惯性属性 -->
  289.  
    <inertial>
  290.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  291.  
    <mass value="0.125"/>
  292.  
    <inertia ixx="0.001" ixy="0" ixz="0" iyy="0.001" iyz="0" izz="0.001" />
  293.  
    </inertial>
  294.  
    <!-- 碰撞区域 -->
  295.  
    <collision>
  296.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  297.  
    <geometry>
  298.  
    <cylinder radius="0.04" length="0.04"/>
  299.  
    </geometry>
  300.  
    </collision>
  301.  
    </link>
  302.  
    <!-- 雷达关节 -->
  303.  
    <joint name="laser_joint" type="fixed">
  304.  
    <parent link="base_link"/>
  305.  
    <child link="laser"/>
  306.  
    <origin xyz="0.16 0 0.078" rpy="0 0 0"/>
  307.  
    </joint>
  308.  
     
  309.  
    <gazebo reference="laser">
  310.  
    <sensor name="laser" type="ray">
  311.  
    <always_on>true</always_on>
  312.  
    <visualize>false</visualize>
  313.  
    <update_rate>5</update_rate>
  314.  
    <ray>
  315.  
    <scan>
  316.  
    <horizontal>
  317.  
    <samples>360</samples>
  318.  
    <resolution>1.000000</resolution>
  319.  
    <min_angle>0.000000</min_angle>
  320.  
    <max_angle>6.280000</max_angle>
  321.  
    </horizontal>
  322.  
    </scan>
  323.  
    <range>
  324.  
    <min>0.120000</min>
  325.  
    <max>3.5</max>
  326.  
    <resolution>0.015000</resolution>
  327.  
    </range>
  328.  
    <noise>
  329.  
    <type>gaussian</type>
  330.  
    <mean>0.0</mean>
  331.  
    <stddev>0.01</stddev>
  332.  
    </noise>
  333.  
    </ray>
  334.  
    <plugin name="scan" filename="libgazebo_ros_ray_sensor.so">
  335.  
    <ros>
  336.  
    <remapping>~/out:=scan</remapping>
  337.  
    </ros>
  338.  
    <output_type>sensor_msgs/LaserScan</output_type>
  339.  
    <frame_name>laser</frame_name>
  340.  
    </plugin>
  341.  
    </sensor>
  342.  
    </gazebo>
  343.  
    <!-- 相机 -->
  344.  
    <link name="camera_link">
  345.  
    <visual>
  346.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  347.  
    <geometry>
  348.  
    <box size="0.015 0.130 0.022"/>
  349.  
    </geometry>
  350.  
    </visual>
  351.  
    <!-- 碰撞区域 -->
  352.  
    <collision>
  353.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  354.  
    <geometry>
  355.  
    <box size="0.015 0.130 0.022"/>
  356.  
    </geometry>
  357.  
    </collision>
  358.  
    <!-- 惯性属性 -->
  359.  
    <inertial>
  360.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  361.  
    <mass value="0.035"/>
  362.  
    <inertia ixx="0.001" ixy="0" ixz="0" iyy="0.001" iyz="0" izz="0.001" />
  363.  
    </inertial>
  364.  
    </link>
  365.  
    <!-- 相机关节 -->
  366.  
    <joint name="camera_joint" type="fixed">
  367.  
    <parent link="base_link"/>
  368.  
    <child link="camera_link"/>
  369.  
    <origin xyz="0.16 0 0.11" rpy="0 0 0"/>
  370.  
    </joint>
  371.  
    <!-- 深度相机 -->
  372.  
    <link name="camera_depth_frame"/>
  373.  
     
  374.  
    <joint name="camera_depth_joint" type="fixed">
  375.  
    <origin xyz="0 0 0" rpy="0 0 0"/>
  376.  
    <parent link="camera_link"/>
  377.  
    <child link="camera_depth_frame"/>
  378.  
    </joint>
  379.  
    <!-- 相机仿真 -->
  380.  
    <gazebo reference="camera_depth_link">
  381.  
    <sensor name="depth_camera" type="depth">
  382.  
    <visualize>true</visualize>
  383.  
    <update_rate>30.0</update_rate>
  384.  
    <camera name="camera">
  385.  
    <horizontal_fov>1.047198</horizontal_fov>
  386.  
    <image>
  387.  
    <width>640</width>
  388.  
    <height>480</height>
  389.  
    <format>R8G8B8</format>
  390.  
    </image>
  391.  
    <clip>
  392.  
    <near>0.05</near>
  393.  
    <far>3</far>
  394.  
    </clip>
  395.  
    </camera>
  396.  
    <plugin name="depth_camera_controller" filename="libgazebo_ros_camera.so">
  397.  
    <baseline>0.2</baseline>
  398.  
    <alwaysOn>true</alwaysOn>
  399.  
    <updateRate>0.0</updateRate>
  400.  
    <frame_name>camera_depth_frame</frame_name>
  401.  
    <pointCloudCutoff>0.5</pointCloudCutoff>
  402.  
    <pointCloudCutoffMax>3.0</pointCloudCutoffMax>
  403.  
    <distortionK1>0</distortionK1>
  404.  
    <distortionK2>0</distortionK2>
  405.  
    <distortionK3>0</distortionK3>
  406.  
    <distortionT1>0</distortionT1>
  407.  
    <distortionT2>0</distortionT2>
  408.  
    <CxPrime>0</CxPrime>
  409.  
    <Cx>0</Cx>
  410.  
    <Cy>0</Cy>
  411.  
    <focalLength>0</focalLength>
  412.  
    <hackBaseline>0</hackBaseline>
  413.  
    </plugin>
  414.  
    </sensor>
  415.  
    </gazebo>
  416.  
     
  417.  
    </robot>
学新通

xacro模型文件需要转成urdf模型文件才能使用

方法1 提前转换:

当前文件夹打开终端输入:base.urdf.xacro > base.urdf生成纯urdf文件。

方法2 在运行launch文件的时候自动转,需要加入xacro解析步骤

package.xml文件内在<test_depend>前加入:<exec_depend>xacro</exec_depend>

launch文件编写:

  1.  
    # 此launch文件是机器人仿真程序,包含 gazebo启动,机器人仿真生成,机器人模型状态发布
  2.  
    import os
  3.  
    from launch import LaunchDescription
  4.  
    from launch.actions import ExecuteProcess
  5.  
    from launch_ros.actions import Node
  6.  
    from launch_ros.substitutions import FindPackageShare
  7.  
    from launch.substitutions import LaunchConfiguration
  8.  
    import xacro
  9.  
     
  10.  
    def generate_launch_description():
  11.  
    robot_name_in_model = 'jtbot' #机器人模型名字
  12.  
    package_name = 'jtbot_description' #模型包名
  13.  
     
  14.  
    ld = LaunchDescription()
  15.  
    use_sim_time = LaunchConfiguration('use_sim_time', default='true')
  16.  
    pkg_share = FindPackageShare(package=package_name).find(package_name)
  17.  
    gazebo_world_path = os.path.join(pkg_share, 'world/jt.world') #世界仿真文件路径
  18.  
    default_rviz_config_path = os.path.join(pkg_share, 'rviz/mrviz2.rviz') #rviz配置文件路径
  19.  
    urdf_xacro_file = os.path.join(pkg_share, 'urdf/jtbot_base.urdf.xacro') #xacro模型文件路径
  20.  
    #解析xacro模型文件
  21.  
    doc = xacro.parse(open(urdf_xacro_file))
  22.  
    xacro.process_doc(doc)
  23.  
    params = {'robot_description': doc.toxml()}
  24.  
     
  25.  
    # 开启ros Gazebo server
  26.  
    start_gazebo_cmd = ExecuteProcess(
  27.  
    cmd=['gazebo',
  28.  
    '--verbose',
  29.  
    gazebo_world_path,
  30.  
    '-s', 'libgazebo_ros_init.so',
  31.  
    '-s', 'libgazebo_ros_factory.so',
  32.  
     
  33.  
    ],
  34.  
    output='screen')
  35.  
     
  36.  
    # Start Robot State publisher
  37.  
    start_robot_state_publisher_cmd = Node(
  38.  
    package='robot_state_publisher',
  39.  
    executable='robot_state_publisher',
  40.  
    output='screen',
  41.  
     
  42.  
    parameters=[params,{'use_sim_time': use_sim_time}]
  43.  
    )
  44.  
     
  45.  
    # gazebo内生成机器人
  46.  
    spawn_entity_cmd = Node(
  47.  
    package='gazebo_ros',
  48.  
    executable='spawn_entity.py',
  49.  
    arguments=['-entity', robot_name_in_model, '-topic', 'robot_description'],
  50.  
    output='screen'
  51.  
     
  52.  
    )
  53.  
     
  54.  
     
  55.  
     
  56.  
    # Launch RViz
  57.  
    start_rviz_cmd = Node(
  58.  
    package='rviz2',
  59.  
    executable='rviz2',
  60.  
    name='rviz2',
  61.  
    output='screen',
  62.  
    arguments=['-d', default_rviz_config_path]
  63.  
    )
  64.  
     
  65.  
    ld.add_action(start_gazebo_cmd)
  66.  
    ld.add_action(spawn_entity_cmd)
  67.  
    ld.add_action(start_robot_state_publisher_cmd)
  68.  
    ld.add_action(start_rviz_cmd)
  69.  
     
  70.  
     
  71.  
    return ld
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgekjgg
系列文章
更多 icon
同类精品
更多 icon
继续加载