《ROS理论与实践》学习笔记(五)构建机器人仿真平台

《ROS理论与实践》学习笔记(五)构建机器人仿真平台

  • 课程内容
    • 1.优化物理仿真模型
      • 1.使用xacro模型文件进行机器人建模
      • 2. ros_control
      • 3.仿真模型的优化
    • 2.创建物理仿真环境
  • 本讲作业
    • 1.将上讲urdf模型改写为xacro文件,进行运动控制仿真
    • 2.完成传感器仿真,在rviz中显示传感器数据
  • 结语

在学习《ROS理论与实践》课程时,记录了学习过程中的编程练习,课后作业以及发现的问题,后续会对尚未解决的问题继续分析并更新,纯小白,仅供参考。
本次学习笔记关于课程中的第六讲:构建机器人仿真平台 。主要学习了ROS的xacro建模方法和gazebo仿真。


课程内容

1.优化物理仿真模型

1.使用xacro模型文件进行机器人建模

xacro模型文件更加简洁,提供可编程接口

  • 常量定义
    < xacro:property name=“xxx” value=“xxx”/>
  • 宏定义
    < xacro:macro name=“xxx” params=“xxx”>

    < /xacro:macro>
  • 宏调用
    < name A=“xxx” B=“xxx” C=“xxx”/>
  • 文件包含
    < xacro:include filename="$(find 功能包名称)/路径/xxx.xacro"/>

2. ros_control

ros_control提供了一系列控制器接口、传动装置接口、硬件接口、控制器工具箱等
控制器(controllers)主要包括:

  • joint_state_controller
  • joint_effort_controller
  • joint_position_controller
  • joint_velocity_controller

具体内容参考ROS Wiki链接:ros_control

3.仿真模型的优化

  1. 为link添加惯性参数和碰撞属性
    添加< collision >以及< cylinder_inertial_matrix>标签
  2. 为link添加gazebo标签
    添加< gazebo>标签
  3. 为joint添加传动装置
    添加< transmission>标签
    具体内容参考ROS Wiki链接:urdf_transmission
  4. 添加gazebo控制器插件
    · < robotNamespace > 机器人命名空间
    · < leftJoint>< rightJoint> 左右轮转动的关节joint
    · < wheelSeparation>< wheelDiameter> 机器人模型尺寸,用于计算差速参数
    · < commandtTopic> 控制器订阅的速度控制指令
    · < odometryFrame> 里程计数据的参考坐标系

2.创建物理仿真环境

方法:

  • 直接添加环境模型
  • 使用building editor

本讲作业

1.将上讲urdf模型改写为xacro文件,进行运动控制仿真

  • 基本xacro模型文件nmybot_base_gazebo.xacro

<robot name="mybot" xmlns:xacro="http://www.ros.org/wiki/xacro">

    
    <xacro:property name="M_PI" value="3.1415926"/>
    <xacro:property name="base_mass"   value="20" /> 
    <xacro:property name="base_radius" value="0.20"/>
    <xacro:property name="base_length" value="0.05"/>

    <xacro:property name="motor_mass"   value="0.01" />
    <xacro:property name="motor_radius" value="0.04"/>
    <xacro:property name="motor_length" value="0.18"/>
    <xacro:property name="motor_joint_x" value="0.1"/>
    <xacro:property name="motor_joint_y" value="0.11"/>

    <xacro:property name="wheel_mass"   value="50" />                 
    <xacro:property name="wheel_radius" value="0.06"/>
    <xacro:property name="wheel_length" value="0.025"/>
    <xacro:property name="wheel_joint_y" value="0.1025"/>
    <xacro:property name="wheel_joint_z" value="0"/>

    <xacro:property name="caster_mass"    value="0.5" /> 
    <xacro:property name="caster_radius"  value="0.0175"/>             
    <xacro:property name="caster_length"  value="0.01"/>
    <xacro:property name="caster_joint_x" value="0.18"/>
    <xacro:property name="caster_joint_z" value="-0.025"/>
    <xacro:property name="caster_link_z"   value="-0.0175"/>

    
    <material name="yellow">
        <color rgba="1 0.4 0 1"/>
    material>
    <material name="black">
        <color rgba="0 0 0 0.95"/>
    material>
    <material name="gray">
        <color rgba="0.75 0.75 0.75 1"/>
    material>
    
    
    <xacro:macro name="sphere_inertial_matrix" params="m r">
        <inertial>
            <mass value="${m}" />
            <inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0"
                iyy="${2*m*r*r/5}" iyz="0" 
                izz="${2*m*r*r/5}" />
        inertial>
    xacro:macro>

    <xacro:macro name="cylinder_inertial_matrix" params="m r h">
        <inertial>
            <mass value="${m}" />
             
        inertial>
    xacro:macro>

    
    <xacro:macro name="motor" params="prefix reflect">
        <joint name="${prefix}_motor_joint" type="fixed">
            <origin xyz="${-motor_joint_x} ${reflect*motor_joint_y} 0" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_motor_link"/>
            <axis xyz="0 1 0"/>
        joint>

        <link name="${prefix}_motor_link">
            <visual>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    
                geometry>
                <material name="gray" />
            visual>
            <collision>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    
                geometry>
            collision>
            <cylinder_inertial_matrix  m="${motor_mass}" r="${motor_radius}" h="${motor_length}" />
        link>

        <gazebo reference="${prefix}_motor_link">
            <material>Gazebo/Graymaterial>
        gazebo>

        
        <transmission name="${prefix}_motor_joint_trans">
            <type>transmission_interface/SimpleTransmissiontype>
            <joint name="${prefix}_motor_joint" >
                <hardwareInterface>hardware_interface/VelocityJointInterfacehardwareInterface>
            joint>
            <actuator name="${prefix}_motor_joint_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterfacehardwareInterface>
                <mechanicalReduction>1mechanicalReduction>
            actuator>
        transmission>
    xacro:macro>

    
    <xacro:macro name="wheel" params="prefix reflect">
        <joint name="${prefix}_wheel_joint" type="continuous">
            <origin xyz="0 ${reflect*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
            <parent link="${prefix}_motor_link"/>
            <child link="${prefix}_wheel_link"/>
            <axis xyz="0 1 0"/>
        joint>

        <link name="${prefix}_wheel_link">
            <visual>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    
                geometry>
                <material name="gray" />
            visual>
            <collision>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    
                geometry>
            collision>
            <cylinder_inertial_matrix  m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
        link>

        <gazebo reference="${prefix}_wheel_link">
            <material>Gazebo/Graymaterial>
        gazebo>

        
        <transmission name="${prefix}_wheel_joint_trans">
            <type>transmission_interface/SimpleTransmissiontype>
            <joint name="${prefix}_wheel_joint" >
                <hardwareInterface>hardware_interface/VelocityJointInterfacehardwareInterface>
            joint>
            <actuator name="${prefix}_wheel_joint_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterfacehardwareInterface>
                <mechanicalReduction>1mechanicalReduction>
            actuator>
        transmission>
    xacro:macro>

    
    <xacro:macro name="caster" params="prefix reflect">
        <joint name="${prefix}_caster_joint" type="continuous">
            <origin xyz="${caster_joint_x} 0 ${caster_joint_z+caster_link_z}" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_caster_link"/>
            <axis xyz="0 1 0"/>
        joint>

        <link name="${prefix}_caster_link">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0"/>
                <geometry>
                    <sphere radius="${caster_radius}" />
                geometry>
                <material name="black" />
            visual>
            <collision>
                <origin xyz="0 0 0" rpy="0 0 0"/>
                <geometry>
                    <sphere radius="${caster_radius}" />
                geometry>
            collision>      
            <sphere_inertial_matrix  m="${caster_mass}" r="${caster_radius}" />
        link>

        <gazebo reference="${prefix}_caster_link">
            <material>Gazebo/Blackmaterial>
        gazebo>
    xacro:macro>

    
    <xacro:macro name="mybot_base_gazebo">
        <link name="base_footprint">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0" />
                <geometry>
                    <box size="0.001 0.001 0.001" />
                geometry>
            visual>
        link>
        <gazebo reference="base_footprint">
            <turnGravityOff>falseturnGravityOff>
        gazebo>

        <joint name="base_footprint_joint" type="fixed">
            <origin xyz="0 0 ${base_length/2 + caster_radius*2}" rpy="0 0 0" />        
            <parent link="base_footprint"/>
            <child link="base_link" />
        joint>

        <link name="base_link">
            <visual>
                <origin xyz=" 0 0 0" rpy="0 0 0" />
                <geometry>
                    <cylinder length="${base_length}" radius="${base_radius}"/>
                geometry>
                <material name="yellow" />
            visual>
            <collision>
                <origin xyz=" 0 0 0" rpy="0 0 0" />
                <geometry>
                    <cylinder length="${base_length}" radius="${base_radius}"/>
                geometry>
            collision>   
            <cylinder_inertial_matrix  m="${base_mass}" r="${base_radius}" h="${base_length}" />
        link>

        <gazebo reference="base_link">
            <material>Gazebo/Bluematerial>
        gazebo>

        <motor prefix="left"  reflect="-1"/>
        <motor prefix="right" reflect="1"/>

        <wheel prefix="left"  reflect="-1"/>
        <wheel prefix="right" reflect="1"/>

        <caster prefix="front" reflect="1"/>

        
        <gazebo>
            <plugin name="differential_drive_controller" 
                    filename="libgazebo_ros_diff_drive.so">
                <rosDebugLevel>DebugrosDebugLevel>
                <publishWheelTF>truepublishWheelTF>
                <robotNamespace>/robotNamespace>
                <publishTf>1publishTf>
                <publishWheelJointState>truepublishWheelJointState>
                <alwaysOn>truealwaysOn>
                <updateRate>100.0updateRate>
                <legacyMode>truelegacyMode>
                <leftJoint>left_wheel_jointleftJoint>
                <rightJoint>right_wheel_jointrightJoint>
                <wheelSeparation>${wheel_joint_y*2}wheelSeparation>
                <wheelDiameter>${2*wheel_radius}wheelDiameter>
                <broadcastTF>1broadcastTF>
                <wheelTorque>30wheelTorque>
                <wheelAcceleration>1.8wheelAcceleration>
                <commandTopic>cmd_velcommandTopic>
                <odometryFrame>odomodometryFrame> 
                <odometryTopic>odomodometryTopic> 
                <robotBaseFrame>base_footprintrobotBaseFrame>
            plugin>
        gazebo> 
    xacro:macro>

robot>

基础模型xacro文件nmybot_gazebo.xacro


<robot name="mybot" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <xacro:include filename="$(find mybot_description)/urdf/nmybot_base_gazebo.xacro" />

    <mybot_base_gazebo/>

robot>

launch文件

<launch>
	<arg name="model" default="$(find xacro)/xacro --inorder '$(find mybot_description)/urdf/nmybot_gazebo.xacro'" />
	<arg name="gui" default="true" />

	<param name="robot_description" command="$(arg model)" />

    
	<param name="use_gui" value="$(arg gui)"/>

    
	<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />

	
	<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

    
	<node name="rviz" pkg="rviz" type="rviz" args="-d $(find mybot_description)/config/mybot_xacro.rviz" required="true" />

launch>

  • 加入摄像头及Kinect后的xacro模型文件nmybot_all_gazebo.xacro,其中摄像头及Kinect模型文件来自课程代码包,不再贴出

<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <xacro:include filename="$(find mybot_description)/urdf/nmybot_base_gazebo.xacro" />
    <xacro:include filename="$(find mybot_description)/urdf/sensors/camera_gazebo.xacro" />
    <xacro:include filename="$(find mbot_description)/urdf/sensors/kinect_gazebo.xacro" />

    <xacro:property name="camera_offset_x" value="0.17" />
    <xacro:property name="camera_offset_y" value="0" />
    <xacro:property name="camera_offset_z" value="0.025" />

    <xacro:property name="kinect_offset_x" value="0.11" />
    <xacro:property name="kinect_offset_y" value="0" />
    <xacro:property name="kinect_offset_z" value="0.06" />

    
    <joint name="camera_joint" type="fixed">
        <origin xyz="${camera_offset_x} ${camera_offset_y} ${camera_offset_z}" rpy="0 0 0" />
        <parent link="base_link"/>
        <child link="camera_link"/>
    joint>

    
    <joint name="kinect_joint" type="fixed">
        <origin xyz="${kinect_offset_x} ${kinect_offset_y} ${kinect_offset_z}" rpy="0 0 0" />
        <parent link="base_link"/>
        <child link="kinect_link"/>
    joint>

    <xacro:kinect_camera prefix="kinect"/>
    <xacro:usb_camera prefix="camera"/>

    <mybot_base_gazebo/>

robot>

Rviz_launch文件类似基础模型,不再贴出
Gazebo_launch文件view_mybot_gazebo_world.launch

<launch>

    
    <arg name="world_name" value="$(find mybot_gazebo)/worlds/playground.world"/>
    <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="world_name" value="$(arg world_name)" />
        <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>

    
    <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find mybot_description)/urdf/nmybot_all_gazebo.xacro'" /> 

    
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" >node> 

    
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"  output="screen" >
        <param name="publish_frequency" type="double" value="50.0" />
    node>

    
    <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
          args="-urdf -model mrobot -param robot_description"/> 

launch>

启动方法:

$ roslaunch mybot_gazebo view_mybot_gazebo_world.launch

运行效果:
《ROS理论与实践》学习笔记(五)构建机器人仿真平台_第1张图片
启动键盘控制节点:

$ roslaunch mbot_teleop mbot_teleop.launch

本文件来自课程代码包,不再贴出
经测试,可以实现机器人模型在gazebo环境下的运动

2.完成传感器仿真,在rviz中显示传感器数据

  • 摄像头仿真
$ rqt_image_view

显示效果:
《ROS理论与实践》学习笔记(五)构建机器人仿真平台_第2张图片

  • Kinect仿真
$ rosrun rviz rviz

添加robotmodel,pointcloud2,选择kinect_link作为基坐标系,在pointcloud2中选择深度信息显示,结果如下图所示
《ROS理论与实践》学习笔记(五)构建机器人仿真平台_第3张图片

激光雷达仿真与上述方法类似


结语

本讲完成了xacro机器人模型的完善,并在gazebo仿真环境中完成了运动控制和传感器的仿真,并在rviz中显示了传感器的仿真效果。
本讲中有以下知识点还需要深入理解:

  • 惯性参数的设置(cylinder_inertial_matrix)
    参考博客:ROS惯性特征,视觉特征,碰撞特征
  • gazebo标签的参数设置(material)
  • joint传动装置的参数设置(type/joint/actuator)
  • ros_control的接口和使用方法
    参考博客:古月居ros_control
  • libgazebo_ros_diff_drive.so的内容

另外在本讲的练习中发现了一些问题:

  • 建立xacro模型并启动gazebo后,发现模型会发生x轴方向的滑动
    本人将模型中两个后轮的质量改为50后,有一定的改善,但未能根本解决该问题,另外修改摩擦系数几乎没有效果,初步判断为控制器controller属性没有修改正确。

你可能感兴趣的:(ROS,linux)