在学习《ROS理论与实践》课程时,记录了学习过程中的编程练习,课后作业以及发现的问题,后续会对尚未解决的问题继续分析并更新,纯小白,仅供参考。
本次学习笔记关于课程中的第六讲:构建机器人仿真平台 。主要学习了ROS的xacro建模方法和gazebo仿真。
xacro模型文件更加简洁,提供可编程接口
ros_control提供了一系列控制器接口、传动装置接口、硬件接口、控制器工具箱等
控制器(controllers)主要包括:
具体内容参考ROS Wiki链接:ros_control
方法:
<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>
<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
$ roslaunch mbot_teleop mbot_teleop.launch
本文件来自课程代码包,不再贴出
经测试,可以实现机器人模型在gazebo环境下的运动
$ rqt_image_view
$ rosrun rviz rviz
添加robotmodel,pointcloud2,选择kinect_link作为基坐标系,在pointcloud2中选择深度信息显示,结果如下图所示
激光雷达仿真与上述方法类似
本讲完成了xacro机器人模型的完善,并在gazebo仿真环境中完成了运动控制和传感器的仿真,并在rviz中显示了传感器的仿真效果。
本讲中有以下知识点还需要深入理解:
另外在本讲的练习中发现了一些问题: