11 月 25 日 ROS 学习笔记——3D 建模与仿真

文章目录

  • 前言
  • 一、在 ROS 中自定义机器人的3D模型
    • 1. 在 rviz 里查看3D模型
    • 2. xacro
  • 二、Gazebo
    • 1. urdf 集成 gazebo
    • 2. 综合应用
      • 1). 运动控制及里程计
      • 2). 雷达仿真
      • 3). 摄像头信息仿真
      • 4). kinect 深度相机仿真
      • 5). 点云


前言

本文为11 月 25 日 ROS 学习笔记——3D 建模与仿真,分为两个章节:

  • 在 ROS 中自定义机器人的3D模型,
  • Gazebo

一、在 ROS 中自定义机器人的3D模型

<robot name="Robot1">
    <link name="base_link">
        <visual>
            <geometry>
                <box size="0.2 .3 .1" />
            geometry>
            <origin rpy="0 0 0" xyz="0 0 0.05" />
            <material name="white">
                <color rgba="1 1 1 1" />
            material>
        visual>
    link>
    <link name="wheel_1">
        <visual>
            <geometry>
                <cylinder length="0.05" radius="0.05" />
            geometry>
            <origin rpy="0 1.5 0" xyz="0.1 0.1 0" />
            <material name="black">
                <color rgba="0 0 0 1" />
            material>
        visual>
    link>
    <link name="wheel_2">
        <visual>
            <geometry>
                <cylinder length="0.05" radius="0.05" />
            geometry>
            <origin rpy="0 1.5 0" xyz="-0.1 0.1 0" />
            <material name="black" />
        visual>
    link>
    <link name="wheel_3">
        <visual>
            <geometry>
                <cylinder length="0.05" radius="0.05" />
            geometry>
            <origin rpy="0 1.5 0" xyz="0.1 -0.1 0" />
            <material name="black" />
        visual>
    link>
    <link name="wheel_4">
        <visual>
            <geometry>
                <cylinder length="0.05" radius="0.05" />
            geometry>
            <origin rpy="0 1.5 0" xyz="-0.1 -0.1 0" />
            <material name="black" />
        visual>
    link>
    <joint name="base_to_wheel1" type="fixed">
        <parent link="base_link" />
        <child link="wheel_1" />
        <origin xyz="0 0 0" />
    joint>
    <joint name="base_to_wheel2" type="fixed">
        <parent link="base_link" />
        <child link="wheel_2" />
        <origin xyz="0 0 0" />
    joint>
    <joint name="base_to_wheel3" type="fixed">
        <parent link="base_link" />
        <child link="wheel_3" />
        <origin xyz="0 0 0" />
    joint>
    <joint name="base_to_wheel4" type="fixed">
        <parent link="base_link" />
        <child link="wheel_4" />
        <origin xyz="0 0 0" />
    joint>
robot>
  • 检查书写的语法是否正确和配置是否有误
check_urdf robot1.urdf 
>>> robot name is: Robot1
	---------- Successfully Parsed XML ---------------
	root Link: base_link has 4 child(ren)
	    child(1):  wheel_1
	    child(2):  wheel_2
	    child(3):  wheel_3
	    child(4):  wheel_4
  • 以图形的方式来查看
urdf_to_graphiz robot1.urdf 
>>> Created file Robot1.gv
	Created file Robot1.pdf

evince Robot1.pdf

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第1张图片

1. 在 rviz 里查看3D模型

  • 创建 .launch 文件
<launch>
    <arg name="model" />
    <arg name="gui" default="False" />
    <param name="robot_description" textfile="$(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" />
launch>
  • 启动命令
roslaunch robot1_description display.launch model:="$(rospack find robot1_description)/urdf/robot1.urdf"

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第2张图片

  • 添加一些组件
joint>
	<link name="arm_base">
<visual>
<geometry>
<box size="0.1 .1 .1"/>
geometry>
<origin rpy="0 0 0" xyz="0 0 0.1"/>
<material name="white">
<color rgba="1 1 1 1"/>
material>
visual>

<collision>
<geometry>
<box size="0.1 .1 .1"/>
geometry>
collision>

<inertial>
<mass value="1"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
link>

<joint name="base_to_arm_base" type="continuous">
<parent link="base_link"/>
<child link="arm_base"/>
<axis xyz="0 0 1"/>
<origin xyz="0 0 0"/>
joint>

<link name="arm_1">
<visual>
<geometry>
<box size="0.05 .05 0.5"/>
geometry>
<origin rpy="0 0 0" xyz="0 0 0.25"/>
<material name="white">
<color rgba="1 1 1 1"/>
material>
visual>
<collision>
<geometry>
<box size="0.05 .05 0.5"/>
geometry>
collision>
<inertial>
<mass value="1"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
link>

<joint name="arm_1_to_arm_base" type="revolute">
<parent link="arm_base"/>
<child link="arm_1"/>
<axis xyz="1 0 0"/>
<origin xyz="0 0 0.15"/>
<limit effort ="1000.0" lower="-1.0" upper="1.0" velocity="0.5"/>
joint>
<link name="arm_2">
<visual>
<geometry>
<box size="0.05 0.05 0.5"/>
geometry>
<origin rpy="0 0 0" xyz="0.06 0 0.15"/>
<material name="white">
<color rgba="1 1 1 1"/>
material>
visual>
<collision>
<geometry>
<box size="0.05 .05 0.5"/>
geometry>
collision>
<inertial>
<mass value="1"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
link>
<joint name="arm_2_to_arm_1" type="revolute">
<parent link="arm_1"/>
<child link="arm_2"/>
<axis xyz="1 0 0"/>
<origin xyz="0.0 0 0.45"/>
<limit effort ="1000.0" lower="-2.5" upper="2.5" velocity="0.5"/>
joint>
<joint name="left_gripper_joint" type="revolute">
<axis xyz="0 0 1"/>
<limit effort="1000.0" lower="0.0" upper="0.548" velocity="0.5"/>
<origin rpy="0 -1.57 0" xyz="0.06 0 0.4"/>
<parent link="arm_2"/>
<child link="left_gripper"/>
joint>
<link name="left_gripper">
<visual>
<origin rpy="0 0 0" xyz="0 0 0"/>
<geometry>
<mesh filename="package://pr2_description/meshes/gripper_v0/l_finger.dae"/>
geometry>
visual>
<collision>
<geometry>
<box size="0.1 .1 .1"/>
geometry>
collision>
<inertial>
<mass value="1"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
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://pr2_description/meshes/gripper_v0/l_finger_tip.dae"/>
geometry>
visual>
<collision>
<geometry>
<box size="0.1 .1 .1"/>
geometry>
collision>
<inertial>
<mass value="1"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
link>
<joint name="right_gripper_joint" type="revolute">
<axis xyz="0 0 -1"/>
<limit effort="1000.0" lower="0.0" upper="0.548" velocity="0.5"/>
<origin rpy="0 -1.57 0" xyz="0.06 0 0.4"/>
<parent link="arm_2"/>
<child link="right_gripper"/>
joint>
<link name="right_gripper">
<visual>
<origin rpy="-3.1415 0 0" xyz="0 0 0"/>
<geometry>
<mesh filename="package://pr2_description/meshes/gripper_v0/l_finger.dae"/>
geometry>
visual>
<collision>
<geometry>
<box size="0.1 .1 .1"/>
geometry>
collision>
<inertial>
<mass value="1"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
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://pr2_description/meshes/gripper_v0/l_finger_tip.dae"/>
geometry>
visual>
<collision>
<geometry>
<box size="0.1 .1 .1"/>
geometry>
collision>
<inertial>
<mass value="1"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
link>

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第3张图片

  • 旋转关节
<joint name="arm_1_to_arm_base" type="revolute">
	<parent link="arm_base"/>
	<child link="arm_1"/>
	<axis xyz="1 0 0"/>
	<origin xyz="0 0 0.15"/>
	<limit effort ="1000.0" lower="-1.0" upper="1.0" velocity="0.5"/>
joint>

2. xacro

Xacro 可帮助我们压缩 URDF 文件的大小, 增加文件的可读性和可维护性。它还允许我们创建模型并复用这些模型以创建相同的结构,如更多的手臂和腿.

  • 使用 xacro 声明常量,避免在很多行重复定义同一个 数值
<xacro:property name="length_wheel" value="0.05" />
<xacro:property name="radius_wheel" value="0.05" />
  • 使用常量
${name_of_variable}:
<cylinder length="${length_wheel}" radius="${radius_wheel}" />
  • 将 .xacro 转换为 .urdf
rosrun xacro xacro demo01_helloworld.urdf.xacro
>>> <robot name="mycar">
	  <link name="left_wheel">
	    <visual>
	      <geometry>
	        <cylinder length="0.0015" radius="0.0325"/>
	      </geometry>
	      <origin rpy="1.57079635 0 0" xyz="0 0 0"/>
	      <material name="wheel_color">
	        <color rgba="0 0 0 0.3"/>
	      </material>
	    </visual>
	  </link>
	  <!-- 3-2.joint -->
	  <joint name="left2link" type="continuous">
	    <parent link="base_link"/>
	    <child link="left_wheel"/>
	    <!-- 
	                x 无偏移
	                y 车体半径
	                z z= 车体高度 / 2 + 离地间距 - 车轮半径
	
	            -->
	    <origin rpy="0 0 0" xyz="0 0.1 -0.0225"/>
	    <axis xyz="0 1 0"/>
	  </joint>
	  <link name="right_wheel">
	    <visual>
	      <geometry>
	        <cylinder length="0.0015" radius="0.0325"/>
	      </geometry>
	      <origin rpy="1.57079635 0 0" xyz="0 0 0"/>
	      <material name="wheel_color">
	        <color rgba="0 0 0 0.3"/>
	      </material>
	    </visual>
	  </link>
	  <!-- 3-2.joint -->
	  <joint name="right2link" type="continuous">
	    <parent link="base_link"/>
	    <child link="right_wheel"/>
	    <!-- 
            x 无偏移
            y 车体半径
            z z= 车体高度 / 2 + 离地间距 - 车轮半径

        -->
	    <origin rpy="0 0 0" xyz="0 -0.1 -0.0225"/>
	    <axis xyz="0 1 0"/>
	  </joint>
	</robot>
rosrun xacro xacro demo01_helloworld.urdf.xacro > demo01_helloworld.urdf
  • 属性与运算
<xacro:property name="PI" value="3.1415927" />
<xacro:property name="radius" value="0.03" />


<myUsePropertyxxx name="${PI}" />
<myUsePropertyxxx name="${radius}" />

rosrun xacro xacro demo02_field.urdf.xacro
>>> <robot name="mycar">
	  
	  <myUsePropertyxxx name="3.1415927"/>
	  <myUsePropertyxxx name="0.03"/>
	  
	robot>
	

<myUsePropertyyy result="${PI / 2}" />

rosrun xacro xacro demo02_field.urdf.xacro
>>> <robot name="mycar">
	  
	  <myUsePropertyxxx name="3.1415927"/>
	  <myUsePropertyxxx name="0.03"/>
	  
	  <myUsePropertyyy result="1.57079635"/>
	robot>

<xacro:macro name="getSum" params="num1 num2">
     <result value="${num1 + num2}" />
xacro:macro>


<xacro:getSum num1="1" num2="5" />

>>> rosrun xacro xacro demo03_macro.urdf.xacro
<robot name="mycar">
  <result value="6"/>
robot>

  • 文件包含
<xacro:include filename="demo02_field.urdf.xacro" />
<xacro:include filename="demo03_macro.urdf.xacro" />

rosrun xacro xacro demo04_sum.urdf.xacro
>>> <robot name="mycar">
	  
	  <myUsePropertyxxx name="3.1415927"/>
	  <myUsePropertyxxx name="0.03"/>
	  
	  <myUsePropertyyy result="1.57079635"/>
	  <result value="6"/>
	robot>
  • xacro 集成 rviz
<param name="robot_description" command="$(find xacro)/xacro $(find urdf01_rviz)/urdf/xacro/demo05_car_base.urdf.xacro" />
  • 控制移动机器人做圆周运动
<launch>
    
    
    <param name="robot_description" command="$(find xacro)/xacro $(find urdf01_rviz)/urdf/xacro/car.urdf.xacro" />

    
    <node pkg="rviz" type="rviz" name="rviz" args="-d $(find urdf01_rviz)/config/show_mycar.rviz" />
    
    <node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher" />
    
    <node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" />
    
    <node pkg="arbotix_python" type="arbotix_driver" name="driver" output="screen">
        <rosparam command="load" file="$(find urdf01_rviz)/config/control.yaml" />
        <param name="sim" value="true" />
    node>
launch>
rostopic pub -r 10 /cmd_vel geometry_msgs/Twist "linear:
  x: 1.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 1.0"

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第4张图片


二、Gazebo

1. urdf 集成 gazebo

  • 创建机器人模型
<robot name="mycar">
    <link name="base_link">
        <visual>
            <geometry>
                <box size="0.5 0.2 0.1" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
            <material name="yellow">
                <color rgba="0.5 0.3 0.0 1" />
            material>
        visual>
        <collision>
            <geometry>
                <box size="0.5 0.2 0.1" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
        collision>
        <inertial>
            <origin xyz="0 0 0" />
            <mass value="6" />
            <inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1" />
        inertial>
    link>
    <gazebo reference="base_link">
        <material>Gazebo/Redmaterial>
    gazebo>

robot>
  • 创建 .launch 文件
<launch>
    
    <param name="robot_description" textfile="$(find urdf02_gazebo)/urdf/demo01_helloworld.urdf" />
    
    <include file="$(find gazebo_ros)/launch/empty_world.launch" />
    
    <node pkg="gazebo_ros" type="spawn_model" name="spawn_model" args="-urdf -model mycar -param robot_description" />
launch>

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第5张图片

  • 仿真环境集成
<launch>
    
    <param name="robot_description" command="$(find xacro)/xacro $(find urdf02_gazebo)/urdf/car.urdf.xacro" />
    
    <include file="$(find gazebo_ros)/launch/empty_world.launch">
        <arg name="world_name" value="${find urdf02_gazebo}/worlds/box_house.world" />
    include>

    
    <node pkg="gazebo_ros" type="spawn_model" name="spawn_model" args="-urdf -model mycar -param robot_description" />
launch>

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第6张图片

2. 综合应用

1). 运动控制及里程计

  • 在 gazebo 中运动控制
<robot name="my_car_move" xmlns:xacro="http://wiki.ros.org/xacro">

    
    <xacro:macro name="joint_trans" params="joint_name">
        
        <transmission name="${joint_name}_trans">
            <type>transmission_interface/SimpleTransmissiontype>
            <joint name="${joint_name}">
                <hardwareInterface>hardware_interface/VelocityJointInterfacehardwareInterface>
            joint>
            <actuator name="${joint_name}_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterfacehardwareInterface>
                <mechanicalReduction>1mechanicalReduction>
            actuator>
        transmission>
    xacro:macro>

    
    <xacro:joint_trans joint_name="base_l_wheel_joint" />
    <xacro:joint_trans joint_name="base_r_wheel_joint" />

    
    <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>base_l_wheel_jointleftJoint> 
            <rightJoint>base_r_wheel_jointrightJoint> 
            <wheelSeparation>${base_radius * 2}wheelSeparation> 
            <wheelDiameter>${wheel_radius * 2}wheelDiameter> 
            <broadcastTF>1broadcastTF>
            <wheelTorque>30wheelTorque>
            <wheelAcceleration>1.8wheelAcceleration>
            <commandTopic>cmd_velcommandTopic> 
            <odometryFrame>odomodometryFrame> 
            <odometryTopic>odomodometryTopic> 
            <robotBaseFrame>base_footprintrobotBaseFrame> 
        plugin>
    gazebo>

robot>
rostopic pub -r 10 /cmd_vel geometry_msgs/Twist "linear:
  x: 1.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 1.0"

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第7张图片

  • 在 rviz 中查看里程计消息
<launch>
    
    <node pkg="rviz" type="rviz" name="rviz" args="-d $(find urdf01_rviz)/config/show_mycar.rviz" />

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

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第8张图片

2). 雷达仿真

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

    
    <gazebo reference="laser">
      <sensor type="ray" name="rplidar">
        <pose>0 0 0 0 0 0pose>
        <visualize>truevisualize>
        <update_rate>5.5update_rate>
        <ray>
          <scan>
            <horizontal>
              <samples>360samples>
              <resolution>1resolution>
              <min_angle>-3min_angle>
              <max_angle>3max_angle>
            horizontal>
          scan>
          <range>
            <min>0.10min>
            <max>30.0max>
            <resolution>0.01resolution>
          range>
          <noise>
            <type>gaussiantype>
            <mean>0.0mean>
            <stddev>0.01stddev>
          noise>
        ray>
        <plugin name="gazebo_rplidar" filename="libgazebo_ros_laser.so">
          <topicName>/scantopicName>
          <frameName>laserframeName>
        plugin>
      sensor>
    gazebo>
  
  robot>

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第9张图片

3). 摄像头信息仿真

<robot name="my_sensors" xmlns:xacro="http://wiki.ros.org/xacro">
    
    <gazebo reference="camera">
      
      <sensor type="camera" name="camera_node">
        <update_rate>30.0update_rate> 
        
        <camera name="head">
          <horizontal_fov>1.3962634horizontal_fov>
          <image>
            <width>1280width>
            <height>720height>
            <format>R8G8B8format>
          image>
          <clip>
            <near>0.02near>
            <far>300far>
          clip>
          <noise>
            <type>gaussiantype>
            <mean>0.0mean>
            <stddev>0.007stddev>
          noise>
        camera>
        
        <plugin name="gazebo_camera" filename="libgazebo_ros_camera.so">
          <alwaysOn>truealwaysOn>
          <updateRate>0.0updateRate>
          <cameraName>/cameracameraName>
          <imageTopicName>image_rawimageTopicName>
          <cameraInfoTopicName>camera_infocameraInfoTopicName>
          <frameName>cameraframeName>
          <hackBaseline>0.07hackBaseline>
          <distortionK1>0.0distortionK1>
          <distortionK2>0.0distortionK2>
          <distortionK3>0.0distortionK3>
          <distortionT1>0.0distortionT1>
          <distortionT2>0.0distortionT2>
        plugin>
      sensor>
    gazebo>
  robot>

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第10张图片

4). kinect 深度相机仿真

<robot name="my_sensors" xmlns:xacro="http://wiki.ros.org/xacro">
    <gazebo reference="support">  
      <sensor type="depth" name="camera">
        <always_on>truealways_on>
        <update_rate>20.0update_rate>
        <camera>
          <horizontal_fov>${60.0*PI/180.0}horizontal_fov>
          <image>
            <format>R8G8B8format>
            <width>640width>
            <height>480height>
          image>
          <clip>
            <near>0.05near>
            <far>8.0far>
          clip>
        camera>
        <plugin name="kinect_camera_controller" filename="libgazebo_ros_openni_kinect.so">
          <cameraName>cameracameraName>
          <alwaysOn>truealwaysOn>
          <updateRate>10updateRate>
          <imageTopicName>rgb/image_rawimageTopicName>
          <depthImageTopicName>depth/image_rawdepthImageTopicName>
          <pointCloudTopicName>depth/pointspointCloudTopicName>
          <cameraInfoTopicName>rgb/camera_infocameraInfoTopicName>
          <depthImageCameraInfoTopicName>depth/camera_infodepthImageCameraInfoTopicName>
          <frameName>supportframeName>
          <baseline>0.1baseline>
          <distortion_k1>0.0distortion_k1>
          <distortion_k2>0.0distortion_k2>
          <distortion_k3>0.0distortion_k3>
          <distortion_t1>0.0distortion_t1>
          <distortion_t2>0.0distortion_t2>
          <pointCloudCutoff>0.4pointCloudCutoff>
        plugin>
      sensor>
    gazebo>

robot>

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第11张图片

5). 点云

  • 修改配置文件的 FrameName 标签并添加坐标变换关系
<frameName>support_depthframeName>


<node pkg="tf2_ros" name="static_transform_publisher" type="static_transform_publisher" args="0 0 0 -1.57 0 -1.57 /support /support_depth" />

11 月 25 日 ROS 学习笔记——3D 建模与仿真_第12张图片


你可能感兴趣的:(学习,笔记,3d)