ROS进阶---Gazebo物理仿真环境搭建
程序员文章站
2024-02-23 16:23:16
...
Gazebo物理仿真环境搭建
一.ros_control
ros_control为开发者提供一系列控制中间件
包含一系列控制器接口/传动装置接口/硬件接口等
二.Gazebo仿真步骤
1.配置机器人模型
完整代码
<?xml version="1.0"?>
<robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- PROPERTY LIST -->
<xacro:property name="M_PI" value="3.1415926"/>
<xacro:property name="base_mass" value="20" />
<xacro:property name="base_length" value="0.46"/>
<xacro:property name="base_width" value="0.20"/>
<xacro:property name="base_height" value="0.07"/>
<xacro:property name="wheel_mass" value="2" />
<xacro:property name="wheel_radius" value="0.07"/>
<xacro:property name="wheel_length" value="0.02"/>
<xacro:property name="wheel_joint_x" value="0.2"/>
<xacro:property name="wheel_joint_y" value="0.11"/>
<xacro:property name="wheel_joint_z" value="0.015"/>
<!-- Defining the colors used in this robot -->
<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>
<!-- Macro for inertia matrix -->
<xacro:macro name="box_inertial_matrix" params="m x y z">
<inertial>
<mass value="${m}" />
<inertia ixx="${m*(y*y+z*z)/12}" ixy="0" ixz="0"
iyy="${m*(x*x+z*z)/12}" iyz="0"
izz="${m*(x*x+z*z)/12}" />
</inertial>
</xacro:macro>
<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>
<!-- Macro for robot wheel -->
<xacro:macro name="wheel" params="prefix1 prefix2 reflect1 reflect2">
<joint name="${prefix1}_${prefix2}_wheel_joint" type="continuous">
<origin xyz="${reflect1*wheel_joint_x} ${reflect2*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
<parent link="base_link"/>
<child link="${prefix1}_${prefix2}_wheel_link"/>
<axis xyz="0 1 0"/>
</joint>
<link name="${prefix1}_${prefix2}_wheel_link">
<visual>
<origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
<material name="gray" />
</visual>
<collision>
<origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
</collision>
<cylinder_inertial_matrix m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
</link>
<gazebo reference="${prefix1}_${prefix2}_wheel_link">
<material>Gazebo/White</material>
</gazebo>
<transmission name="${prefix1}_${prefix2}_wheel_joint_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="${prefix1}_${prefix2}_wheel_joint" >
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
</joint>
<actuator name="${prefix1}_${prefix2}_wheel_joint_motor">
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>
</xacro:macro>
<xacro:macro name="mbot_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>false</turnGravityOff>
</gazebo>
<joint name="base_footprint_joint" type="fixed">
<origin xyz="0 0 ${wheel_joint_z + wheel_radius}" 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>
<box size="${base_length} ${base_width} ${base_height}" />
</geometry>
<material name="yellow" />
</visual>
<collision>
<origin xyz=" 0 0 0" rpy="0 0 0" />
<geometry>
<box size="${base_length} ${base_width} ${base_height}" />
</geometry>
</collision>
<box_inertial_matrix m="${base_mass}" x="${base_length}" y="${base_width}" z="${base_height}" />
</link>
<gazebo reference="base_link">
<material>Gazebo/Gold</material>
</gazebo>
<wheel prefix1="left" prefix2="front" reflect1="1" reflect2="1"/>
<wheel prefix1="right" prefix2="front" reflect1="1" reflect2="-1"/>
<wheel prefix1="left" prefix2="back" reflect1="-1" reflect2="1"/>
<wheel prefix1="right" prefix2="back" reflect1="-1" reflect2="-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_back_wheel_joint</leftJoint>
<rightJoint>right_back_wheel_joint</rightJoint>
<wheelSeparation>${wheel_joint_y*2-0.02}</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_footprint</robotBaseFrame>
</plugin>
</gazebo>
</xacro:macro>
</robot>
2.创建物理仿真环境
<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 mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" />
<!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
<!-- 运行robot_state_publisher节点,发布tf -->
<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>
<!-- 在gazebo中加载机器人模型-->
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
args="-urdf -model mrobot -param robot_description"/>
</launch>
3.打开Gazebo显示机器人模型
空环境下的机器人模型
roslaunch mbot_gazebo view_mbot_gazebo_empty_world.launch
感觉孤零零的~
添加环境模型后的机器人
roslaunch mbot_gazebo view_mbot_gazebo_play_ground.launch
这次好多了~
3.启动键盘节点
roslaunch mbot_teleop mbot_teleop.launch
四.传感器仿真
摄像头
<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
<xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/camera_gazebo.xacro" />
<xacro:property name="camera_offset_x" value="0.21" />
<xacro:property name="camera_offset_y" value="0" />
<xacro:property name="camera_offset_z" value="0.0715" />
<mbot_base/>
<!-- Camera -->
<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>
<xacro:usb_camera prefix="camera"/>
<mbot_base_gazebo/>
</robot>
查看摄像头仿真图像
rqt_image_view