• 【ROS入门】机器人系统仿真——相关组件以及URDF集成Rviz


    相关组件

    URDF(Unified Robot Description Format)——创建机器人模型

    统一(标准化)机器人描述格式,可以以一种 XML 的方式描述机器人的部分结构,比如底盘、摄像头、激光雷达、机械臂以及不同关节的自由度,该文件可以被 C++ 内置的解释器转换成可视化的机器人模型,是 ROS 中实现机器人仿真的重要组件。

    Gazebo——搭建仿真环境

    一款3D动态模拟器,用于显示机器人模型并创建仿真环境,能够在复杂的室内和室外环境中准确有效地模拟机器人。与游戏引擎提供高保真度的视觉模拟类似,Gazebo提供高保真度的物理模拟,其提供一整套传感器模型,以及对用户和程序非常友好的交互方式。

    Rviz(ROS Visualization Tool)——显示机器人各种传感器感知到的环境信息

    ROS的三维可视化工具,主要目的是以三维方式显示ROS消息,可以将数据进行可视化表达。例如:可以显示机器人模型,可以无需编程就能表达激光测距仪(LRF)传感器中的传感器到障碍物的距离,RealSense、Kinect或Xtion等三维距离传感器的点云数据(PCD, Point Cloud Data),从相机获取的图像值等。

    三者应用中,只是创建 URDF 意义不大,一般需要结合 Gazebo 或 Rviz 使用,在 Gazebo 或 Rviz 中可以将 URDF 文件解析为图形化的机器人模型,一般的使用组合为:

    • 如果非仿真环境,那么使用 URDF 结合 Rviz 直接显示感知的真实环境信息
    • 如果是仿真环境,那么需要使用 URDF 结合 Gazebo 搭建仿真环境,并结合 Rviz 显示感知的虚拟环境信息

    URDF集成Rviz

    URDF相关语法

    robot

    robot 标签是根标签,所有的 link 和 joint 以及其他标签都必须包含在 robot 标签内,在该标签内可以通过 name 属性设置机器人模型的名称。

    属性:

    • name:指定机器人模型的名称

    子标签:

    其他标签都是子级标签

    link

    link 标签用于描述机器人某个部件(也即刚体部分)的外观和物理属性。比如: 机器人底座、轮子、激光雷达、摄像头…每一个部件都对应一个 link, 在 link 标签内,可以设计该部件的形状、尺寸、颜色、惯性矩阵、碰撞参数等一系列属性。该标签对应的数据在模型中是可见的
    请添加图片描述

    属性:

    • name:为连杆命名

    子标签:

    • visual:描述外观
      • geometry 设置连杆的形状

        • box 盒状
          属性:size=长(x) 宽(y) 高(z)
        • cylinder 圆柱
          属性:radius=半径 length=高度
        • sphere 球体
          属性:radius=半径
        • mesh 为连杆添加皮肤
          属性: filename=资源路径(格式:package://packagename/path/文件)
      • origin 设置偏移量与倾斜弧度

        • xyz=x偏移 y偏移 z偏移
        • rpy=x翻滚 y俯仰 z偏航 (右手系,单位是弧度)
      • metrial 设置材料属性(颜色)

        • 属性: name
        • 标签: color
          • 属性: rgba=红绿蓝权重值与透明度 (每个权重值以及透明度取值[0,1])
    • collision:连杆的碰撞属性(用于Gazebo)
    • Inertial:连杆的惯性矩阵(用于Gazebo)

    举例:

        <link name="base_link">
            <visual>
                
                <geometry>
                    
                    
                    
                    
                    
                    
                geometry>
                
                <origin xyz="0 0 0" rpy="0 0 0" />
                
                <material name="black">
                    <color rgba="0.7 0.5 0 0.5" />
                material>
            visual>
        link>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    joint

    joint 标签用于描述机器人关节运动学和动力学属性,还可以指定关节运动的安全极限,机器人的两个部件(分别称之为 parent linkchild link)以"关节"的形式相连接,不同的关节有不同的运动形式:旋转、滑动、固定、旋转速度、旋转角度限制等。比如:安装在底座上的轮子可以360度旋转,而摄像头则可能是完全固定在底座上。该标签对应的数据在模型中是不可见的。
    在这里插入图片描述

    属性:

    • name:关节名称

    • type:关节运动形式

      • continuous:旋转关节,可以绕单轴无限旋转

      • revolute:旋转关节,类似于 continues,但是有旋转角度限制

      • prismatic:滑动关节,沿某一轴线移动的关节,有位置极限

      • planer:平面关节,允许在平面正交方向上平移或旋转

      • floating:浮动关节,允许进行平移、旋转运动

      • fixed:固定关节,不允许运动的特殊关节

    子标签:

    • parent(必需的)
      属性:link=(强制)父级连杆的名字,是这个link在机器人结构树中的名字。
    • child(必需的)
      属性:link=(强制)子级连杆的名字,是这个link在机器人结构树中的名字。
    • origin
      属性:xyz=各轴线上的偏移量 rpy=各轴线上的偏移弧度。
    • axis
      属性: xyz用于设置围绕哪个关节轴运动。

    举例:

    
    <robot name="mycar">
        
        <link name="base_link">
            <visual>
                <geometry>
                    <box size="0.5 0.2 0.1" />
                geometry>
                <origin xyz="0 0 0" rpy="0 0 0" />
                <material name="blue">
                    <color rgba="0 0 1.0 0.5" />
                material>
            visual>
        link>
    
        
        <link name="camera">
            <visual>
                <geometry>
                    <box size="0.02 0.05 0.05" />
                geometry>
                <origin xyz="0 0 0" rpy="0 0 0" />
                <material name="red">
                    <color rgba="1 0 0 0.5" />
                material>
            visual>
        link>
    
        
        <joint name="camera2baselink" type="continuous">
        	
            <parent link="base_link"/>
            
            <child link="camera" />
            
            <origin xyz="0.2 0 0.075" rpy="0 0 0" />
            <axis xyz="0 0 1" />
        joint>
    robot>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    URDF优化——xacro

    Xacro 是一种 XML 宏语言,是可编程的 XML。Xacro 可以声明变量,可以通过数学运算求解,使用流程控制控制执行顺序,还可以通过类似函数的实现,封装固定的逻辑,将逻辑中需要的可变的数据以参数的方式暴露出去,从而提高代码复用率以及程序的安全性。

    简单来说,就是xacro可以帮你以类似函数的形式来封装代码和参数,以便复用。

    相关语法

    属性与算数运算

    属性定义:

    <xacro:property name="xxxx" value="yyyy" />
    
    • 1

    属性调用:

    ${属性名称}
    
    • 1

    算术运算:

    ${数学表达式}
    
    • 1

    类似于函数实现

    宏定义:

    <xacro:macro name="宏名称" params="参数列表(多参数之间使用空格分隔)">
        .....
        参数调用格式: ${参数名}
    xacro:macro>
    
    • 1
    • 2
    • 3
    • 4

    宏调用:

    <xacro:宏名称 参数1=xxx 参数2=xxx/>
    
    • 1
    文件包含
    <robot name="xxx" xmlns:xacro="http://wiki.ros.org/xacro">
          <xacro:include filename="my_base.xacro" />
          <xacro:include filename="my_camera.xacro" />
          <xacro:include filename="my_laser.xacro" />
    robot>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    实操

    实现流程:

    1. 创建一个新的功能包,名称自定义,导入依赖包:urdfxacro

    2. 首先编写摄像头和雷达的 xacro 文件

    3. 然后再编写一个组合文件,组合底盘、摄像头与雷达

    4. 最后,通过 launch 文件启动 Rviz 并显示模型

    底盘、摄像头和雷达Xacro文件实现

    底盘xacro文件:my_base.urdf.xacro

    
    
    <robot name="my_base" xmlns:xacro="http://www.ros.org/wiki/xacro">
        
        <xacro:property name="PI" value="3.141"/>
        
        <material name="black">
            <color rgba="0.0 0.0 0.0 1.0" />
        material>
        
        <xacro:property name="base_footprint_radius" value="0.001" /> 
        <xacro:property name="base_link_radius" value="0.1" /> 
        <xacro:property name="base_link_length" value="0.08" /> 
        <xacro:property name="earth_space" value="0.015" /> 
    
        
        <link name="base_footprint">
          <visual>
            <geometry>
              
              <sphere radius="${base_footprint_radius}" />
            geometry>
          visual>
        link>
    
        <link name="base_link">
          <visual>
            <geometry>
              <cylinder radius="${base_link_radius}" length="${base_link_length}" />
            geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <material name="yellow">
              <color rgba="0.5 0.3 0.0 0.5" />
            material>
          visual>
        link>
    
        <joint name="base_link2base_footprint" type="fixed">
          <parent link="base_footprint" />
          <child link="base_link" />
          <origin xyz="0 0 ${earth_space + base_link_length / 2 }" />
        joint>
    
        
        
        <xacro:property name="wheel_radius" value="0.0325" />
        <xacro:property name="wheel_length" value="0.015" />
        
        <xacro:macro name="add_wheels" params="name flag">
          <link name="${name}_wheel">
            <visual>
              <geometry>
                <cylinder radius="${wheel_radius}" length="${wheel_length}" />
              geometry>
              <origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />
              <material name="black" />
            visual>
          link>
    
          <joint name="${name}_wheel2base_link" type="continuous">
            <parent link="base_link" />
            <child link="${name}_wheel" />
            <origin xyz="0 ${flag * base_link_radius} ${-(earth_space + base_link_length / 2 - wheel_radius) }" />
            <axis xyz="0 1 0" />
          joint>
        xacro:macro>
        <xacro:add_wheels name="left" flag="1" />
        <xacro:add_wheels name="right" flag="-1" />
        
        
        <xacro:property name="support_wheel_radius" value="0.0075" /> 
    
        
        <xacro:macro name="add_support_wheel" params="name flag" >
          <link name="${name}_wheel">
            <visual>
                <geometry>
                    <sphere radius="${support_wheel_radius}" />
                geometry>
                <origin xyz="0 0 0" rpy="0 0 0" />
                <material name="black" />
            visual>
          link>
    
          <joint name="${name}_wheel2base_link" type="continuous">
              <parent link="base_link" />
              <child link="${name}_wheel" />
              <origin xyz="${flag * (base_link_radius - support_wheel_radius)} 0 ${-(base_link_length / 2 + earth_space / 2)}" />
              <axis xyz="1 1 1" />
          joint>
        xacro:macro>
    	
    	
        <xacro:add_support_wheel name="front" flag="1" />
        <xacro:add_support_wheel name="back" flag="-1" />
    
    robot>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    摄像头xacro文件:my_camera.urdf.xacro

    
    <robot name="my_camera" xmlns:xacro="http://wiki.ros.org/xacro">
        
        <xacro:property name="camera_length" value="0.01" /> 
        <xacro:property name="camera_width" value="0.025" /> 
        <xacro:property name="camera_height" value="0.025" /> 
        <xacro:property name="camera_x" value="0.08" /> 
        <xacro:property name="camera_y" value="0.0" /> 
        <xacro:property name="camera_z" value="${base_link_length / 2 + camera_height / 2}" /> 
    
        
        <link name="camera">
            <visual>
                <geometry>
                    <box size="${camera_length} ${camera_width} ${camera_height}" />
                geometry>
                <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
                <material name="black" />
            visual>
        link>
    
        <joint name="camera2base_link" type="fixed">
            <parent link="base_link" />
            <child link="camera" />
            <origin xyz="${camera_x} ${camera_y} ${camera_z}" />
        joint>
    robot>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    雷达xacro文件:my_laser.urdf.xacro

    
    <robot name="my_laser" xmlns:xacro="http://wiki.ros.org/xacro">
    
        
        <xacro:property name="support_length" value="0.15" /> 
        <xacro:property name="support_radius" value="0.01" /> 
        <xacro:property name="support_x" value="0.0" /> 
        <xacro:property name="support_y" value="0.0" /> 
        <xacro:property name="support_z" value="${base_link_length / 2 + support_length / 2}" /> 
    
        <link name="support">
            <visual>
                <geometry>
                    <cylinder radius="${support_radius}" length="${support_length}" />
                geometry>
                <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
                <material name="red">
                    <color rgba="0.8 0.2 0.0 0.8" />
                material>
            visual>
        link>
    
        <joint name="support2base_link" type="fixed">
            <parent link="base_link" />
            <child link="support" />
            <origin xyz="${support_x} ${support_y} ${support_z}" />
        joint>
    
    
        
        <xacro:property name="laser_length" value="0.05" /> 
        <xacro:property name="laser_radius" value="0.03" /> 
        <xacro:property name="laser_x" value="0.0" /> 
        <xacro:property name="laser_y" value="0.0" /> 
        <xacro:property name="laser_z" value="${support_length / 2 + laser_length / 2}" /> 
    
        
        <link name="laser">
            <visual>
                <geometry>
                    <cylinder radius="${laser_radius}" length="${laser_length}" />
                geometry>
                <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
                <material name="black" />
            visual>
        link>
    
        <joint name="laser2support" type="fixed">
            <parent link="support" />
            <child link="laser" />
            <origin xyz="${laser_x} ${laser_y} ${laser_z}" />
        joint>
    robot>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    组合底盘摄像头与雷达的xacro文件

    my_base_camera_laser.urdf.xacro

    
    <robot name="my_car_camera" xmlns:xacro="http://wiki.ros.org/xacro">
        <xacro:include filename="my_base.urdf.xacro" />
        <xacro:include filename="my_camera.urdf.xacro" />
        <xacro:include filename="my_laser.urdf.xacro" />
    robot>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    launch文件

    test.launch

    <launch>
        <param name="robot_description" command="$(find xacro)/xacro $(find urdf01_rviz)/urdf/xacro/my_base_camera_laser.urdf.xacro" />
        
        <node pkg="rviz" type="rviz" name="rviz" />
    	
        <node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher" output="screen" />
        
        <node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" output="screen" />
        
        <node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher_gui" output="screen" />
    launch>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    在Rviz中显示机器人模型

    调出终端并输入以下指令:

    source ./devel/setup.bash
    roslaunch 包名 test.launch
    
    • 1
    • 2

    手动添加机器人显示组件RobotModel:
    在这里插入图片描述

    在Fixed Frame中选择base_footprint,即可显示机器人:
    在这里插入图片描述

  • 相关阅读:
    LQ0117 缩位求和【进制】
    【数据库数据恢复】Oracle数据库文件出现坏块报错的数据恢复案例
    Python绘图系统16:动态更新tkinter组件
    Linux之多线程
    Spring复习面试题
    [HDLBits] Exams/review2015 shiftcount
    阿里云 ACK One 多集群管理全面升级:多集群服务、多集群监控、两地三中心应用容灾
    [LeetCode]栈,队列相关题目(C语言实现)
    6.springboot的重要注解和核心配置文件(.properties 文件和 .yml 文件)
    总账科目 前台操作关事务代码及操作要点
  • 原文地址:https://blog.csdn.net/PuddleRubbish/article/details/134033994