ROS自学实践(4):使用GAZEBO进行物理仿真

2023-05-16

rviz中的仿真只是视觉上的仿真,不能称得上物理仿真,gazebo是真正意义上的三维物理仿真平台,可以在里面创建环境等相关信息,方便以后的建模和导航。

1.向xacro模型文件中添加惯性矩阵和碰撞属性

(1)惯性矩阵的计算是理论力学中的内容,可以从维基百科可以查到相应的公式进行计算,下面是部分截图。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<xacro:macro name="cylinder_inertial_matrix" params="m r h">
	<inertial>
		<mass value="${m}"/>
		<inertia ixx="${m*(3*r*r+h*h)/12}" ixy="0" ixz="0" iyy="${m*(3*r*r+h*h)/12}" iyz="0" izz="${m*r*r/2}"/>
	</inertial>
</xacro:macro>

<xacro:macro name="retangle_inertial_matrix" params="m d w h">
	<inertial>
		<mass value="${m}"/>
		<inertia ixx="${m*(d*d+h*h)/12}" ixy="0" ixz="0" iyy="${m*(d*d+w*w)/12}" iyz="0" izz="${m*(h*h+w*w)/12}"/>
	</inertial>
</xacro:macro>

(2)碰撞属性应该和机器人的外形有关,因此和visual标签类似
添加轮子碰撞属性

	<collision>
	    <origin xyz="0 0 0" rpy="${M_PI/2} 0 0"/>
	    <geometry>
	    <cylinder length="${wheel_length}" radius="${wheel_radius}"/>
	    </geometry>
	</collision>
	

添加车身的碰撞属性

      <collision>
	<geometry>  
        <box size="${base_length} ${base_width} ${base_height}"/>  
        </geometry>  
        <origin rpy="0 0 0" xyz="0 0 0"/>
      </collision>

添加车头标识块的碰撞属性

	<collision>
	    <origin xyz="0 0 0" rpy="0 0 0"/>
	    <geometry>
	    <box size=".12 .13 .04"/>
	    </geometry>
	</collision>

(3)添加gazebo标签,比如颜色定义,gazebo颜色与rviz颜色不兼容,需重新定义。

<material name="yellow">
	<color rgba="1 0.4 0 1"/>
</material>
<material name="black">
	<color rgba="0 0 0 0.9"/>
</material>
<material name="gray">
	<color rgba="0.75 0.75 0.75 1"/>
</material>
<material name="white">
	<color rgba="1 1 1 1"/>
</material>
<material name="blue">  
        <color rgba="0 0 .8 1"/>  
</material>  

(4)向joint添加传动装置

<transmission name="${prefix}_wheel_joint_trans">
            <type>transmission_interface/SimpleTransmission</type>
            <joint name="${prefix}_rear_drive_joint" >    <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
            </joint>
            <actuator name="${prefix}_wheel_joint_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
                <mechanicalReduction>1</mechanicalReduction>
            </actuator>
        </transmission>

(5)添加gazebo控制器插件

        <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_rear_drive_joint</leftJoint>
                <rightJoint>right_rear_drive_joint</rightJoint>
                <wheelSeparation>0.8</wheelSeparation>
                <wheelDiameter>${2*wheel_radius}</wheelDiameter>
                <broadcastTF>1</broadcastTF>
                <wheelTorque>30</wheelTorque>
                <wheelAcceleration>1.8</wheelAcceleration>
                <commandTopic>cmd_vel</commandTopic>
                <odometryFrame>odom</odometryFrame> 
                <odometryTopic>odom</odometryTopic> 
                <robotBaseFrame>base_link</robotBaseFrame>
            </plugin>
        </gazebo> 

全部的xacro文件如下:
shcRobot2_base_gazebo.xacro,四个轮子都添加transmission模块,前后两个轮子都进行差速完成转向。

<?xml version="1.0"?> 
<robot name="robot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:property name="M_PI"        value="3.14159"/>
<xacro:property name="base_length" value="1.3"/>
<xacro:property name="base_width"  value="0.8"/>
<xacro:property name="base_height" value="0.2"/>
<xacro:property name="base_mass"   value="10"/>
<xacro:property name="head_mass"   value="0.2"/>

<xacro:property name="wheel_length" value="0.08"/>
<xacro:property name="wheel_radius" value="0.15"/>
<xacro:property name="wheel_mass"   value="1"/>

<xacro:property name="wheel_x_offset"    value="0.4"/>
<xacro:property name="wheel_y_offset"    value="0.375"/>
<xacro:property name="wheel_z_offset"    value="-0.055"/>

<material name="yellow">
	<color rgba="1 0.4 0 1"/>
</material>
<material name="black">
	<color rgba="0 0 0 0.9"/>
</material>
<material name="gray">
	<color rgba="0.75 0.75 0.75 1"/>
</material>
<material name="white">
	<color rgba="1 1 1 1"/>
</material>
<material name="blue">  
        <color rgba="0 0 .8 1"/>  
</material>  

<xacro:macro name="cylinder_inertial_matrix" params="m r h">
	<inertial>
		<mass value="${m}"/>
		<inertia ixx="${m*(3*r*r+h*h)/12}" ixy="0" ixz="0" iyy="${m*(3*r*r+h*h)/12}" iyz="0" izz="${m*r*r/2}"/>
	</inertial>
</xacro:macro>

<xacro:macro name="retangle_inertial_matrix" params="m d w h">
	<inertial>
		<mass value="${m}"/>
		<inertia ixx="${m*(d*d+h*h)/12}" ixy="0" ixz="0" iyy="${m*(d*d+w*w)/12}" iyz="0" izz="${m*(h*h+w*w)/12}"/>
	</inertial>
</xacro:macro>

<xacro:macro name="steer_wheel" params="prefix reflect">

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

    <joint name="${prefix}_front_steer_joint"  type="revolute">
	<origin xyz="${wheel_x_offset} ${reflect*wheel_y_offset} ${wheel_z_offset}" rpy="0 0 0"/>
	<parent link="base_link"/>
	<child  link="${prefix}_front_wheel"/>
	<axis   xyz="0 1 0"/>
    </joint>
    <gazebo reference="${prefix}_front_wheel">
        <material>Gazebo/Black</material>
    </gazebo>
            <transmission name="${prefix}_wheel_joint_trans">
            <type>transmission_interface/SimpleTransmission</type>
            <joint name="${prefix}_front_steer_joint" >
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
            </joint>
            <actuator name="${prefix}_wheel_joint_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
                <mechanicalReduction>1</mechanicalReduction>
            </actuator>
        </transmission>
</xacro:macro>

<xacro:macro name="drive_wheel" params="prefix reflect">

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

    <joint name="${prefix}_rear_drive_joint"  type="continuous">
	<origin xyz="${-1*wheel_x_offset} ${reflect*wheel_y_offset} ${wheel_z_offset}" rpy="0 0 0"/>
	<parent link="base_link"/>
	<child  link="${prefix}_rear_wheel"/>
	<axis   xyz="0 1 0"/>
    </joint>
    <gazebo reference="${prefix}_rear_wheel">
        <material>Gazebo/Black</material>
    </gazebo>

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

<xacro:macro name="robot_base">

    <link name="head">
    	<visual>
	    <origin xyz="0 0 0" rpy="0 0 0"/>
	    <geometry>
		<box size=".12 .13 .04"/>
	    </geometry>
	    <material name="white"/>
        </visual>
	<collision>
	    <origin xyz="0 0 0" rpy="0 0 0"/>
	    <geometry>
	    <box size=".12 .13 .04"/>
	    </geometry>
	</collision>
	<retangle_inertial_matrix m="${head_mass}" d=".12" w=".13" h=".04"/>
    </link>
    <gazebo reference="head">
        <material>Gazebo/White</material>
    </gazebo>
    <joint name="tobox"  type="fixed">
	<origin xyz="0.3 0 0.1" rpy="0 0 0"/>
	<parent link="base_link"/>
	<child  link="head"/>
    </joint>

    <link name="base_link">  
      <visual> 
        <geometry>  
          <box size="${base_length} ${base_width} ${base_height}"/>  
        </geometry>  
        <origin rpy="0 0 0" xyz="0 0 0"/>
        <material name="blue"/>  
      </visual>
      <collision>
	<geometry>  
        <box size="${base_length} ${base_width} ${base_height}"/>  
        </geometry>  
        <origin rpy="0 0 0" xyz="0 0 0"/>
      </collision>
      <retangle_inertial_matrix m="${base_mass}" d="${base_length}" w="${base_width}" h="${base_height}"/>  
    </link>
    
    <gazebo reference="base_link">
	<material>Gazebo/Blue</material>
    </gazebo>

    <steer_wheel prefix="left"  reflect="1"/>
    <steer_wheel prefix="right" reflect="-1"/>
    <drive_wheel prefix="left"  reflect="1"/>
    <drive_wheel prefix="right" reflect="-1"/>

        <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_rear_drive_joint</leftJoint>
                <rightJoint>right_rear_drive_joint</rightJoint>
                <wheelSeparation>0.8</wheelSeparation>
                <wheelDiameter>${2*wheel_radius}</wheelDiameter>
                <broadcastTF>1</broadcastTF>
                <wheelTorque>80</wheelTorque>
                <wheelAcceleration>3</wheelAcceleration>
                <commandTopic>cmd_vel</commandTopic>
                <odometryFrame>odom</odometryFrame> 
                <odometryTopic>odom</odometryTopic> 
                <robotBaseFrame>base_link</robotBaseFrame>
            </plugin>
        </gazebo> 
        <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_front_steer_joint</leftJoint>
                <rightJoint>right_front_steer_joint</rightJoint>
                <wheelSeparation>0.8</wheelSeparation>
                <wheelDiameter>${2*wheel_radius}</wheelDiameter>
                <broadcastTF>1</broadcastTF>
                <wheelTorque>80</wheelTorque>
                <wheelAcceleration>3</wheelAcceleration>
                <commandTopic>cmd_vel</commandTopic>
                <odometryFrame>odom</odometryFrame> 
                <odometryTopic>odom</odometryTopic> 
                <robotBaseFrame>base_link</robotBaseFrame>
            </plugin>
        </gazebo> 
</xacro:macro>
</robot>

shcRobot_xacro_gazebo.xacro

<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
    <xacro:include filename="$(find shcrobot_description)/urdf/xacro/shcRobot2_base_gazebo.xacro"/>
    <robot_base/>
</robot>

2.创建gazebo仿真环境

在launch文件中添加gazebo环境的描述,下面是对整个launch文件的解析。
(1)添加启动gazebo仿真环境

    <!-- 设置launch文件的参数 -->
           <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="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>

(2)加载机器人模型描述参数

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

(3)运行关节发布节点

	<!--运行joint_state_publisher节点,发布机器人关节状态-->
	<node name = "robot_state_publisher" pkg = "robot_state_publisher" type = "state_publisher">
		<param name="publish_frequency" type="double" value="20.0" />
	</node>
	<!--运行robot_state_publisher节点,发布tf-->

(4)在gazebo中加载机器人模型

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

3.运行launch文件

完整的launch文件

<launch>
	    <!-- 设置launch文件的参数 -->
    <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"/>

	<!--运行gazebo仿真环境-->
	<include file="$(find gazebo_ros)/launch/empty_world.launch">
        	<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 shcrobot_description)/urdf/xacro/shcRobot2_xacro_gazebo.xacro'"/>
	<!--运行joint_state_publisher节点,发布机器人关节状态-->
	<node name = "robot_state_publisher" pkg = "robot_state_publisher" type = "state_publisher">
		<param name="publish_frequency" type="double" value="20.0" />
	</node>
	    <!-- 在gazebo中加载机器人模型-->
    <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
          args="-urdf -model shcrobot -param robot_description"/> 
</launch>

在这里插入图片描述

4.创建仿真环境

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ROS自学实践(4):使用GAZEBO进行物理仿真 的相关文章

  • SLAM-hector_slam 简介与使用

    hector slam功能包使用高斯牛顿方法 不需要里程计数据 只根据激光信息便可构建地图 所以他的总体框架如下 hector slam功能包 hector slam的核心节点是hector mapping 它订阅 scan 话题以获取SL
  • ROS诸多调试工具总结1

    ROS有许多调试工具来为ROS调试你的工具 1 rosnode 参数 用法 作用 list rosnode list 查看当前运行了哪些节点 info rosnode info node name 查看该节点发布 接受哪些话题以及服务 ki
  • 无人驾驶论坛

    1 百度Apollo论坛 http www 51apollo com 2 人工智能中文资讯网 http www ailab cn
  • ROS学习(1)——ROS1和ROS2的区别

    因为机器人是一个系统工程 它包括了机械臂结构 电子电路 驱动程序 通信框架 组装集成 调试和各种感知决策算法等方面 任何一个人甚至是一个公司都不可能完成机器人系统的研发工作 但是我们又希望自己能造出一个机器人跑一跑 验证一下自己的算法 所以
  • 线速度和角速度

    转自 https baike baidu com item E7 BA BF E9 80 9F E5 BA A6 1532652 fr aladdin https baike baidu com item E8 A7 92 E9 80 9F
  • Ubuntu下vscode配置ROS环境

    摘要 最近准备放弃用clion开发ROS使用更主流的vscode 整理一下在ubuntu18 04下的VSCode安装和ROS环境配置流程 安装 方法一 软件商店安装 个人还是推荐使用ubuntu软件下载vscode 简单不容易出错 方法二
  • ROS noetic tf demo错误处理及python版本切换

    文章目录 报错描述及解决 ubuntu20 04下python版本切换 报错描述及解决 ubuntu版本 20 04 ROS版本 noetic roslaunch turtle tf turtle tf demo launch 报错信息 t
  • 激光雷达LMS111在ROS上的使用

    LMS111 10100 在ROS上的测试与使用 准备工作 设备 硬件 LMS111 101000激光雷达 软件 ubuntu16 04 ROS 开始 设备连接 将激光雷达与处理器 电脑 工控机等 通过以太网连接好 激光雷达默认的IP地址为
  • Raspberry Pi 上 ROS 服务器/客户端通过GPIO 驱动硬件

    ROS 服务 现在 想象一下你在你的电脑后面 你想从这个服务中获取天气 你 在你身边 被认为是客户端 在线天气服务是服务器 您将能够通过带有 URL 的 HTTP 请求访问服务器 将 HTTP URL 视为 ROS 服务 首先 您的计算机将
  • 最快实现一个自己的扫地机

    作者 良知犹存 转载授权以及围观 欢迎关注微信公众号 羽林君 或者添加作者个人微信 become me 扫地机介绍 扫地机器人行业本质是技术驱动型行业 产品围绕导航系统的升级成为行业发展的主旋律 按功能划分 扫地机器人分为四大系统 即导航系
  • 局域网下ROS多机通信的网络连接配置

    1 在路由器设置中固定各机器IP地址 在浏览器中输入路由器的IP地址 例如TP LINK路由器的IP为 192 168 1 1 进入登录页面后 输入用户名和密码登录 用户名一般为admin 密码为自定义 在 基本设置 gt LAN设置 gt
  • (ros/qt报错) FATAL: ROS_MASTER_URI is not defined in the environment

    安装qt之后 明明打开roscore但是qt运行跟ros有关的节点时报错 FATAL 1450943695 306401842 ROS MASTER URI is not defined in the environment Either
  • 《机器人操作系统入门》课程代码示例安装出错解决方法

    问题描述 学习 机器人操作系统入门 课程时 在Ubuntu 16 04 上安装了kinetic 安装ROS Academy for Beginners时依赖总是报错 如下所示 rosdep install from paths src ig
  • ubuntu18.04命令安装ros2

    ROS2官方文档 本教程为apt get命令安装方式 官网教程有点问题 借鉴一下大佬的安装方式 文章目录 1 安装ROS2 1 1 安装秘钥相关指令 1 2 授权秘钥 1 3 添加ROS2软件源 1 4 安装 2 设置环境 可选但是推荐 2
  • roslaunch error: ERROR: cannot launch node of type

    今天在因为github上有个之前的包更新了 重新git clone后出现了一个问题 ERROR cannot launch node of type crazyflie demo controller py can t locate nod
  • 在 Python 3 中导入 Rosbag

    我正在尝试从 Python 3 读取 rosbag 文件 我安装了 ROS2 Eloquent Elusor 它应该支持 Python 3 当我跑步时 import rosbag bag rosbag Bag test bag 从Pytho
  • Caught exception in launch(see debug for traceback)

    Caught exception in launch see debug for traceback Caught exception when trying to load file of format xml Caught except
  • catkin_make后找不到ROS包

    我根据 ROS 的 Wiki 页面创建了一个 ROS 工作区 我还使用创建了一个包catkin create pkg在我刚刚创建的工作区下 然后 按照 ROS Wiki 中的步骤使用以下命令构建包catkin make 构建包后 我插入命令
  • ROS安装错误(Ubuntu 16.04中的ROS Kinetic)

    中列出的步骤顺序http wiki ros org kinetic Installat 已被关注 尝试在Ubuntu 16 04中安装ROSkinetic 输入以下命令时出错 sudo apt get install ros kinetic
  • catkin_make 编译报错 Unable to find either executable ‘empy‘ or Python module ‘em‘...

    文章目录 写在前面 一 问题描述 二 解决方法 参考链接 写在前面 自己的测试环境 Ubuntu20 04 一 问题描述 自己安装完 anaconda 后 再次执行 catkin make 遇到如下问题 CMake Error at opt

随机推荐