【ROS进阶篇】第十一讲 基于Gazebo和Rviz的机器人联合仿真(运动控制与传感器)

【ROS进阶篇】基于Gazebo和Rviz的机器人联合仿真(运动控制与传感器)

在这里插入图片描述

前言

在上一节博客中我们系统的学习了如何使用Gazebo对使用URDF文件完成的机器人模型进行集成仿真,从基本的仿真流程出发,在后续给出了具体的机器人实例,并在最后附上了使用Gazebo创建仿真环境的教程,本节内容主要针对于使用URDF、Gazebo、Rviz进行联合仿真,URDF 用于创建机器人模型、Rviz 可以显示机器人感知到的环境信息,Gazebo 用于仿真,可以模拟外界环境,以及机器人传感器,本节内容则更加聚焦于使用Gazebo模拟传感器数据,并在Rviz中完成显示和分析。
在这里插入图片描述

一、机器人运动控制(ros_control)

1. 组件介绍

  • 引入原因:在RVIZ中,我们通过Arbotix辅助实现了对于基于URDF文件的机器人模型的运动控制,而在Gazebo仿真中,虽然我们已经建立了具体的仿真环境,但是想要真正控制机器人运动,也需要着一个关键组件包。

在这里插入图片描述

  • 应用场景:同样的ROS程序部署与不同的实际具体机器人系统上。
    在这里插入图片描述

  • 介绍:ROS_CONTROL,是一组软件包,包含控制器、管理器、硬件、传输等接口,本质上是一个控制中间件,提供了一套规范,除了这些,ros_control还提供了一个硬件的抽象层,用于负责硬件资源管理,controller从抽象层请求资源即可。

在这里插入图片描述

  • 各数据流层功能介绍:
  1. Controller Manager:
    每个机器人可能有多个controller,所以这里有一个控制器管理器的概念,提供一种通用的接口来管理不同的controller。controller manager的输入就是ROS上层应用的输出。
  2. Controller:
    完成每个joint的控制,请求下层的硬件资源,提供了PID控制器,读取硬件资源接口中的状态,在发布控制命令。
  3. Hardware Rescource:
    为上下两层提供硬件资源的接口。
  4. RobotHW:
    硬件抽象层和硬件直接打交道,通过write和read方法来完成硬件的操作,这一层也包含关节限位、力矩转换、状态转换等功能。
  5. Real Robot:
    实际的机器人上也需要有自己的嵌入式控制器,接收到命令后需要反映到执行器上,比如接收到位置1的命令后,那就需要让执行器快速、稳定的到达位置1。
  • 特点:对于不同的机器人平台来说,ros_control提供了一个标准的规范接口架构,提高了程序的兼容性、涉及效率与灵活可移植性,使用时直接调用gazebo中的相关接口即可。

2. 运动控制实现

  • 基本流程:
  1. 创建机器人模型;
  2. 编写一个单独的xacro文件,添加传动装置及控制器,并集成到一起;
  3. 启动Gazebo发布/cmd_vel消息控制运动
  • 基本机器人URDF文件略,传动、控制文件实例如下:
<robot name="my_car_move" xmlns:xacro="http://wiki.ros.org/xacro">

    <!-- 传动实现:用于连接控制器与关节 -->
    <xacro:macro name="joint_trans" params="joint_name">
        <!-- Transmission is important to link the joints and the controller -->
        <transmission name="${joint_name}_trans">
            <type>transmission_interface/SimpleTransmission</type>
            <joint name="${joint_name}">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
            </joint>
            <actuator name="${joint_name}_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
                <mechanicalReduction>1</mechanicalReduction>
            </actuator>
        </transmission>
    </xacro:macro>

    <!-- 每一个驱动轮都需要配置传动装置 -->
    <xacro:joint_trans joint_name="left_wheel2base_link" />
    <xacro:joint_trans joint_name="right_wheel2base_link" />

    <!-- 控制器 -->
    <gazebo>
        <plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so">
            <rosDebugLevel>Debug</rosDebugLevel>
            <publishWheelTF>true</publishWheelTF>
            <robotNamespace>/</robotNamespace>
            <publishTf>1</publishTf>
            <publishWheelJointState>true</publishWheelJointState>
            <alwaysOn>true</alwaysOn>
            <updateRate>100.0</updateRate>
            <legacyMode>true</legacyMode>
            <leftJoint>left_wheel2base_link</leftJoint> <!-- 左轮 -->
            <rightJoint>right_wheel2base_link</rightJoint> <!-- 右轮 -->
            <wheelSeparation>${base_link_radius * 2}</wheelSeparation> <!-- 车轮间距 -->
            <wheelDiameter>${wheel_radius * 2}</wheelDiameter> <!-- 车轮直径 -->
            <broadcastTF>1</broadcastTF>
            <wheelTorque>30</wheelTorque>
            <wheelAcceleration>1.8</wheelAcceleration>
            <commandTopic>cmd_vel</commandTopic> <!-- 运动控制话题 -->
            <odometryFrame>odom</odometryFrame> 
            <odometryTopic>odom</odometryTopic> <!-- 里程计话题 -->
            <robotBaseFrame>base_footprint</robotBaseFrame> <!-- 根坐标系 -->
        </plugin>
    </gazebo>

</robot>
  • 集成xacro文件:
<!-- 组合小车底盘与摄像头 -->
<robot name="my_car_camera" xmlns:xacro="http://wiki.ros.org/xacro">
    <xacro:include filename="my_head.urdf.xacro" />
    <xacro:include filename="my_base.urdf.xacro" />
    <xacro:include filename="my_camera.urdf.xacro" />
    <xacro:include filename="my_laser.urdf.xacro" />
    <xacro:include filename="move.urdf.xacro" />
</robot>
  • 启动launch文件:
<launch>
    <!-- 将 Urdf 文件的内容加载到参数服务器 -->
    <param name="robot_description" command="$(find xacro)/xacro $(find demo02_urdf_gazebo)/urdf/xacro/my_base_camera_laser.urdf.xacro" />
    <!-- 启动 gazebo -->
    <include file="$(find gazebo_ros)/launch/empty_world.launch">
        <arg name="world_name" value="$(find demo02_urdf_gazebo)/worlds/hello.world" />
    </include>

    <!-- 在 gazebo 中显示机器人模型 -->
    <node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description"  />
</launch>
  • 控制机器人运动:命令行控制/编写节点控制:

在这里插入图片描述


二、传感器信息仿真及显示

1. 里程计信息

  • 里程计信息:机器人相对于出发点坐标系的位姿状态(位置坐标与运动朝向)

  • 操作流程:

  1. 通过启动文件启动Rviz,打开状态发布节点:
<launch>
   <!-- 启动 rviz -->
   <node pkg="rviz" type="rviz" name="rviz" />
   <!-- 关节以及机器人状态发布节点 -->
   <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>
  1. 在Rviz中添加组件:

在这里插入图片描述


2. 雷达信息

  • 2.1 编写xacro文件,添加雷达传感器信息
<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 0</pose>
      <visualize>true</visualize>
      <update_rate>5.5</update_rate>
      <ray>
        <scan>
          <horizontal>
            <samples>360</samples>
            <resolution>1</resolution>
            <min_angle>-3</min_angle>
            <max_angle>3</max_angle>
          </horizontal>
        </scan>
        <range>
          <min>0.10</min>
          <max>30.0</max>
          <resolution>0.01</resolution>
        </range>
        <noise>
          <type>gaussian</type>
          <mean>0.0</mean>
          <stddev>0.01</stddev>
        </noise>
      </ray>
      <plugin name="gazebo_rplidar" filename="libgazebo_ros_laser.so">
        <topicName>/scan</topicName>
        <frameName>laser</frameName>
      </plugin>
    </sensor>
  </gazebo>

</robot>
  • 2.2 集成文件到机器人模型中:
<!-- 组合小车底盘与传感器 -->
<robot name="my_car_camera" xmlns:xacro="http://wiki.ros.org/xacro">
    <xacro:include filename="my_head.urdf.xacro" />
    <xacro:include filename="my_base.urdf.xacro" />
    <xacro:include filename="my_camera.urdf.xacro" />
    <xacro:include filename="my_laser.urdf.xacro" />
    <xacro:include filename="move.urdf.xacro" />
    <!-- 雷达仿真的 xacro 文件 -->
    <xacro:include filename="my_sensors_laser.urdf.xacro" />
</robot>
  • 2.3 启动gazebo,启动rviz,添加雷达信息显示插件:

在这里插入图片描述


3. 摄像头信息

  • 3.1 配置摄像头传感器
<robot name="my_sensors" xmlns:xacro="http://wiki.ros.org/xacro">
  <!-- 被引用的link -->
  <gazebo reference="camera">
    <!-- 类型设置为 camara -->
    <sensor type="camera" name="camera_node">
      <update_rate>30.0</update_rate> <!-- 更新频率 -->
      <!-- 摄像头基本信息设置 -->
      <camera name="head">
        <horizontal_fov>1.3962634</horizontal_fov>
        <image>
          <width>1280</width>
          <height>720</height>
          <format>R8G8B8</format>
        </image>
        <clip>
          <near>0.02</near>
          <far>300</far>
        </clip>
        <noise>
          <type>gaussian</type>
          <mean>0.0</mean>
          <stddev>0.007</stddev>
        </noise>
      </camera>
      <!-- 核心插件 -->
      <plugin name="gazebo_camera" filename="libgazebo_ros_camera.so">
        <alwaysOn>true</alwaysOn>
        <updateRate>0.0</updateRate>
        <cameraName>/camera</cameraName>
        <imageTopicName>image_raw</imageTopicName>
        <cameraInfoTopicName>camera_info</cameraInfoTopicName>
        <frameName>camera</frameName>
        <hackBaseline>0.07</hackBaseline>
        <distortionK1>0.0</distortionK1>
        <distortionK2>0.0</distortionK2>
        <distortionK3>0.0</distortionK3>
        <distortionT1>0.0</distortionT1>
        <distortionT2>0.0</distortionT2>
      </plugin>
    </sensor>
  </gazebo>
</robot>
  • 3.2 集成机器人模型,类似于前两个传感器,略
  • 3.3 启动仿真环境gazebo,启动rviz显示数据,添加组件:

在这里插入图片描述


4. kinect摄像头信息

  • 4.1 配置kinect传感器:
<robot name="my_sensors" xmlns:xacro="http://wiki.ros.org/xacro">
    <gazebo reference="kinect link名称">  
      <sensor type="depth" name="camera">
        <always_on>true</always_on>
        <update_rate>20.0</update_rate>
        <camera>
          <horizontal_fov>${60.0*PI/180.0}</horizontal_fov>
          <image>
            <format>R8G8B8</format>
            <width>640</width>
            <height>480</height>
          </image>
          <clip>
            <near>0.05</near>
            <far>8.0</far>
          </clip>
        </camera>
        <plugin name="kinect_camera_controller" filename="libgazebo_ros_openni_kinect.so">
          <cameraName>camera</cameraName>
          <alwaysOn>true</alwaysOn>
          <updateRate>10</updateRate>
          <imageTopicName>rgb/image_raw</imageTopicName>
          <depthImageTopicName>depth/image_raw</depthImageTopicName>
          <pointCloudTopicName>depth/points</pointCloudTopicName>
          <cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName>
          <depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTopicName>
          <frameName>kinect link名称</frameName>
          <baseline>0.1</baseline>
          <distortion_k1>0.0</distortion_k1>
          <distortion_k2>0.0</distortion_k2>
          <distortion_k3>0.0</distortion_k3>
          <distortion_t1>0.0</distortion_t1>
          <distortion_t2>0.0</distortion_t2>
          <pointCloudCutoff>0.4</pointCloudCutoff>
        </plugin>
      </sensor>
    </gazebo>

</robot>
  • 4.2 集成机器人模型,类似于之前的传感器,略
  • 4.3 启动仿真环境gazebo,启动rviz显示数据,添加组件:

在这里插入图片描述


总结

  • 声明:本节博客部分参考了CSDN用户赵虚左的ROS教程,本文主要内容是使用URDF文件建立机器人模型,并通过Gazebo创建仿真环境,模拟传感器使用,在RVIZ中完成对于传感器数据的可视化分析和处理,在后半部分的分析中我们发现,对于传感器(里程计、雷达、摄像头)分析来说,都是从xacro文件出发,添加相应配置,集成到机器人模型文件,最后启动各仿真组件修改配置完成联合仿真,各仿真组件各司其职,完成对应效果。
  • 至此,ROS教程的进阶篇结束了,对于ROS部分的内容主要还有关于导航部分的内容会在后续进行补充,针对于Gazebo的使用教程也会另起专栏,敬请期待。

在这里插入图片描述

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生如昭诩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值