实现自己的仿真小车移动

上一章讲到使用第三方包来实现仿真小车的移动,这一章我们来创建一个自己的仿真小车,并使它在rviz中移动。
我会稍微介绍以下知识

  1. urdf文件语法
  2. tf广播与坐标转换

一、urdf语法

有这方面知识的同学可以跳过。
a)什么是urdf?
用于统一机器人描述格式(URDF)的C ++解析器,它是用于表示机器人模型的XML格式。(google翻译)也就是说,如果你想在ros平台中,仿真出来一个机器人,你就需要用这个包来描述一个仿真机器人。

b)urdf的标签

官网上介绍了这几种标签
1. link:描述链接的运动和动态属性。
2. joint:描述接头的运动学和动态特性。
3. gazebo:描述仿真性能,如阻尼,摩擦等。
4. sensor:描述一个传感器,如相机,射线传感器等。
5. model_state:描述某一时间模型的状态。
6. model:描述机器人结构的运动学和动态特性。
7. transmission:传动装置将执行机构连接到接头上,代表其机械联轴器。
8. sensor/proposals:描述一个传感器,如相机,射线传感器等。

我们这次用到的标签是

我们这里先给出一个模型,这是官网写好的。


<robot name="visual">

<material name="blue">
    <color rgba="0 0 0.8 1"/>
  material>
  <material name="black">
    <color rgba="0 0 0 1"/>
  material>
  <material name="white">
    <color rgba="1 1 1 1"/>
  material>

  <link name="base_link">
    <visual>
      <geometry>
        <cylinder length="0.6" radius="0.2"/>
      geometry>
      <material name="blue"/>
    visual>
  link>

  <link name="right_leg">
    <visual>
      <geometry>
        <box size="0.6 0.1 0.2"/>
      geometry>
      <origin rpy="0 1.57075 0" xyz="0 0 -0.3"/>
      <material name="white"/>
    visual>
  link>

  <joint name="base_to_right_leg" type="fixed">
    <parent link="base_link"/>
    <child link="right_leg"/>
    <origin xyz="0 -0.22 0.25"/>
  joint>

  <link name="right_base">
    <visual>
      <geometry>
        <box size="0.4 0.1 0.1"/>
      geometry>
      <material name="white"/>
    visual>
  link>

  <joint name="right_base_joint" type="fixed">
    <parent link="right_leg"/>
    <child link="right_base"/>
    <origin xyz="0 0 -0.6"/>
  joint>

  <link name="right_front_wheel">
    <visual>
      <origin rpy="1.57075 0 0" xyz="0 0 0"/>
      <geometry>
        <cylinder length="0.1" radius="0.035"/>
      geometry>
      <material name="black"/>
      <origin rpy="0 0 0" xyz="0 0 0"/>
    visual>
  link>
  <joint name="right_front_wheel_joint" type="fixed">
    <parent link="right_base"/>
    <child link="right_front_wheel"/>
    <origin rpy="0 0 0" xyz="0.133333333333 0 -0.085"/>
  joint>

  <link name="right_back_wheel">
    <visual>
      <origin rpy="1.57075 0 0" xyz="0 0 0"/>
      <geometry>
        <cylinder length="0.1" radius="0.035"/>
      geometry>
      <material name="black"/>
    visual>
  link>
  <joint name="right_back_wheel_joint" type="fixed">
    <parent link="right_base"/>
    <child link="right_back_wheel"/>
    <origin rpy="0 0 0" xyz="-0.133333333333 0 -0.085"/>
  joint>

  <link name="left_leg">
    <visual>
      <geometry>
        <box size="0.6 0.1 0.2"/>
      geometry>
      <origin rpy="0 1.57075 0" xyz="0 0 -0.3"/>
      <material name="white"/>
    visual>
  link>

  <joint name="base_to_left_leg" type="fixed">
    <parent link="base_link"/>
    <child link="left_leg"/>
    <origin xyz="0 0.22 0.25"/>
  joint>

  <link name="left_base">
    <visual>
      <geometry>
        <box size="0.4 0.1 0.1"/>
      geometry>
      <material name="white"/>
    visual>
  link>

  <joint name="left_base_joint" type="fixed">
    <parent link="left_leg"/>
    <child link="left_base"/>
    <origin xyz="0 0 -0.6"/>
  joint>

  <link name="left_front_wheel">
    <visual>
      <origin rpy="1.57075 0 0" xyz="0 0 0"/>
      <geometry>
        <cylinder length="0.1" radius="0.035"/>
      geometry>
      <material name="black"/>
    visual>
  link>
  <joint name="left_front_wheel_joint" type="fixed">
    <parent link="left_base"/>
    <child link="left_front_wheel"/>
    <origin rpy="0 0 0" xyz="0.133333333333 0 -0.085"/>
  joint>

  <link name="left_back_wheel">
    <visual>
      <origin rpy="1.57075 0 0" xyz="0 0 0"/>
      <geometry>
        <cylinder length="0.1" radius="0.035"/>
      geometry>
      <material name="black"/>
    visual>
  link>
  <joint name="left_back_wheel_joint" type="fixed">
    <parent link="left_base"/>
    <child link="left_back_wheel"/>
    <origin rpy="0 0 0" xyz="-0.133333333333 0 -0.085"/>
  joint>

  <joint name="gripper_extension" type="fixed">
    <parent link="base_link"/>
    <child link="gripper_pole"/>
    <origin rpy="0 0 0" xyz="0.19 0 0.2"/>
  joint>

  <link name="gripper_pole">
    <visual>
      <geometry>
        <cylinder length="0.2" radius="0.01"/>
      geometry>
      <origin rpy="0 1.57075 0 " xyz="0.1 0 0"/>
    visual>
  link>

  <joint name="left_gripper_joint" type="fixed">
    <origin rpy="0 0 0" xyz="0.2 0.01 0"/>
    <parent link="gripper_pole"/>
    <child link="left_gripper"/>
  joint>

  <link name="left_gripper">
    <visual>
      <origin rpy="0.0 0 0" xyz="0 0 0"/>
      <geometry>
        <mesh filename="package://urdf_tutorial/meshes/l_finger.dae"/>
      geometry>
    visual>
  link>

  <joint name="left_tip_joint" type="fixed">
    <parent link="left_gripper"/>
    <child link="left_tip"/>
  joint>

  <link name="left_tip">
    <visual>
      <origin rpy="0.0 0 0" xyz="0.09137 0.00495 0"/>
      <geometry>
        <mesh filename="package://urdf_tutorial/meshes/l_finger_tip.dae"/>
      geometry>
    visual>
  link>
  <joint name="right_gripper_joint" type="fixed">
    <origin rpy="0 0 0" xyz="0.2 -0.01 0"/>
    <parent link="gripper_pole"/>
    <child link="right_gripper"/>
  joint>

  <link name="right_gripper">
    <visual>
      <origin rpy="-3.1415 0 0" xyz="0 0 0"/>
      <geometry>
        <mesh filename="package://urdf_tutorial/meshes/l_finger.dae"/>
      geometry>
    visual>
  link>

  <joint name="right_tip_joint" type="fixed">
    <parent link="right_gripper"/>
    <child link="right_tip"/>
  joint>

  <link name="right_tip">
    <visual>
      <origin rpy="-3.1415 0 0" xyz="0.09137 0.00495 0"/>
      <geometry>
        <mesh filename="package://urdf_tutorial/meshes/l_finger_tip.dae"/>
      geometry>
    visual>
  link>

  <link name="head">
    <visual>
      <geometry>
        <sphere radius="0.2"/>
      geometry>
      <material name="white"/>
    visual>
  link>
  <joint name="head_swivel" type="fixed">
    <parent link="base_link"/>
    <child link="head"/>
    <origin xyz="0 0 0.3"/>
  joint>

  <link name="box">
    <visual>
      <geometry>
        <box size="0.08 0.08 0.08"/>
      geometry>
      <material name="blue"/>
    visual>
  link>

  <joint name="tobox" type="fixed">
    <parent link="head"/>
    <child link="box"/>
    <origin xyz="0.1814 0 0.1414"/>
  joint>
robot>

这个就是官网上写好的,每个人都会有这个模型。就在这个文件夹/opt/ros/kinetic/share/urdf_tutorial/urdf
然后我们可以运行看看roslaunch urdf_tutorial display.launch model:='$(find urdf_tutorial)/urdf/05-visual.urdf'
实现自己的仿真小车移动_第1张图片

二、tf广播与坐标转换

先来扯一点物理知识。中学物理告诉我们,位移是相对的,你意识到某件物体在移动,那么在你的意识里必定会有一个参考系,物体相对于参考系在移动。在仿真世界中也是这样的,如果你想要小车移动,那么你必须告诉rviz,我们选择一个不动的物体作为参考系,然后小车相对于这个参考系,距离越来越远,这样我们就可以看到小车在仿真世界中移动了!
那么我们只需下面这些步骤:

  1. 编写监听传感器(我这里用的是手柄)
  2. 传感器数据使用tf坐标转换,转成tf格式的消息。
  3. 在rviz中看我们的成果

实现自己的仿真小车移动_第2张图片
这是我的文档结构,tf_boradcaster.py写的就是监听传感器并使用tf坐标转换

#!/usr/bin/env python  
#coding=utf-8 
import rospy
import tf
from sensor_msgs.msg import Joy

def handle_turtle_pose(msg):
    br = tf.TransformBroadcaster()
    br.sendTransform((msg.axes[4], msg.axes[3], 0),
                     tf.transformations.quaternion_from_euler(0, 0, msg.axes[0]),
                     rospy.Time.now(),
                     "odom",#固定坐标叫什么都可以
                     "base_link")#你的物体的参考系 link标签里面的name属性值都可以

if __name__ == '__main__':
    rospy.init_node('my_tf')
    rospy.Subscriber('joy',
                     Joy,#手柄节点的消息类型
                     handle_turtle_pose)
    rospy.spin()

因为使用了一些依赖,所以我们在CMakeLists.txt中加载依赖,在CMakeLists.txt文件中找到

find_package(catkin REQUIRED#加上后面的东西,重新catkin_make就行了
    rospy
    tf
    sensor_msgs
)

这里把urdf.rviz中的配置发一下,也是直接拿另一个包写好的

Panels:
  - Class: rviz/Displays
    Help Height: 78
    Name: Displays
    Property Tree Widget:
      Expanded:
        - /Global Options1
        - /Status1
        - /RobotModel1
        - /TF1
      Splitter Ratio: 0.5
    Tree Height: 557
  - Class: rviz/Selection
    Name: Selection
  - Class: rviz/Tool Properties
    Expanded:
      - /2D Pose Estimate1
      - /2D Nav Goal1
      - /Publish Point1
    Name: Tool Properties
    Splitter Ratio: 0.588679
  - Class: rviz/Views
    Expanded:
      - /Current View1
    Name: Views
    Splitter Ratio: 0.5
  - Class: rviz/Time
    Experimental: false
    Name: Time
    SyncMode: 0
    SyncSource: ""
Visualization Manager:
  Class: ""
  Displays:
    - Alpha: 0.5
      Cell Size: 1
      Class: rviz/Grid
      Color: 160; 160; 164
      Enabled: true
      Line Style:
        Line Width: 0.03
        Value: Lines
      Name: Grid
      Normal Cell Count: 0
      Offset:
        X: 0
        Y: 0
        Z: 0
      Plane: XY
      Plane Cell Count: 10
      Reference Frame: Frame>
      Value: true
    - Alpha: 0.5
      Class: rviz/RobotModel
      Collision Enabled: false
      Enabled: true
      Links:
        All Links Enabled: true
        Expand Joint Details: false
        Expand Link Details: false
        Expand Tree: false
        Link Tree Style: Links in Alphabetic Order
        base_link:
          Alpha: 1
          Show Axes: false
          Show Trail: false
          Value: true
        right_leg:
          Alpha: 1
          Show Axes: false
          Show Trail: false
          Value: true
      Name: RobotModel
      Robot Description: robot_description
      TF Prefix: ""
      Update Interval: 0
      Value: true
      Visual Enabled: true
    - Class: rviz/TF
      Enabled: true
      Frame Timeout: 15
      Frames:
        All Enabled: true
        base_link:
          Value: true
        right_leg:
          Value: true
      Marker Scale: 0.5
      Name: TF
      Show Arrows: true
      Show Axes: true
      Show Names: true
      Tree:
        base_link:
          right_leg:
            {}
      Update Interval: 0
      Value: true
  Enabled: true
  Global Options:
    Background Color: 48; 48; 48
    Fixed Frame: base_link
    Frame Rate: 30
  Name: root
  Tools:
    - Class: rviz/Interact
      Hide Inactive Objects: true
    - Class: rviz/MoveCamera
    - Class: rviz/Select
    - Class: rviz/FocusCamera
    - Class: rviz/Measure
    - Class: rviz/SetInitialPose
      Topic: /initialpose
    - Class: rviz/SetGoal
      Topic: /move_base_simple/goal
    - Class: rviz/PublishPoint
      Single click: true
      Topic: /clicked_point
  Value: true
  Views:
    Current:
      Class: rviz/Orbit
      Distance: 4.48689
      Enable Stereo Rendering:
        Stereo Eye Separation: 0.06
        Stereo Focal Distance: 1
        Swap Stereo Eyes: false
        Value: false
      Focal Point:
        X: 0
        Y: 0
        Z: 0
      Name: Current View
      Near Clip Distance: 0.01
      Pitch: 0.695397
      Target Frame: Frame>
      Value: Orbit (rviz)
      Yaw: 0.513582
    Saved: ~
Window Geometry:
  Displays:
    collapsed: false
  Height: 882
  Hide Left Dock: false
  Hide Right Dock: false
  QMainWindow State: 000000ff00000000fd00000004000000000000013c000002c3fc0200000008fb0000001200530065006c0065006300740069006f006e00000001e10000009b0000006400fffffffb0000001e0054006f006f006c002000500072006f007000650072007400690065007302000001ed000001df00000185000000a3fb000000120056006900650077007300200054006f006f02000001df000002110000018500000122fb000000200054006f006f006c002000500072006f0070006500720074006900650073003203000002880000011d000002210000017afb000000100044006900730070006c0061007900730100000043000002c3000000de00fffffffb0000002000730065006c0065006300740069006f006e00200062007500660066006500720200000138000000aa0000023a00000294fb00000014005700690064006500530074006500720065006f02000000e6000000d2000003ee0000030bfb0000000c004b0069006e0065006300740200000186000001060000030c00000261000000010000010f000002c3fc0200000003fb0000001e0054006f006f006c002000500072006f00700065007200740069006500730100000041000000780000000000000000fb0000000a005600690065007700730100000043000002c3000000b800fffffffb0000001200530065006c0065006300740069006f006e010000025a000000b200000000000000000000000200000490000000a9fc0100000001fb0000000a00560069006500770073030000004e00000080000002e10000019700000003000004c000000044fc0100000002fb0000000800540069006d00650100000000000004c00000025800fffffffb0000000800540069006d0065010000000000000450000000000000000000000269000002c300000004000000040000000800000008fc0000000100000002000000010000000a0054006f006f006c00730100000000ffffffff0000000000000000
  Selection:
    collapsed: false
  Time:
    collapsed: false
  Tool Properties:
    collapsed: false
  Views:
    collapsed: false
  Width: 1216
  X: 17
  Y: 28

然后这个是launch文件



  "rvizconfig" default="$(find test_move)/rviz/urdf.rviz" />

  "robot_description" command="$(find xacro)/xacro.py '$(find test_move)/urdf/my_robot.urdf'" />

  "joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />
  "robot_state_publisher" pkg="robot_state_publisher" type="state_publisher" />

  "joy" type="joy_node" output="screen"/>

  "takler" pkg="test_move" type="tf_broadcaster.py"/>

  "rviz" pkg="rviz" type="rviz" args="-d $(arg rvizconfig)" required="true" />

注意里面的test_move换成你自己的包名就好了。
然后我们
roslaunch test_move display.launch
就可以启动一个可以用手柄操控的仿真机器人了

你可能感兴趣的:(ROS学习)