1.创建工作区
1.1创建urdf文件夹
catkin_create_pkg m2wr_description urdf
后面的urdf使得 此文件夹可以编译urdf文件
删除内部文件夹,并创建一个urdf文件夹
1.2编写xacro文件
创建一个m2wr.xacro的文件
<?xml version="1.0" ?>
<robot name="m2wr" xmlns:xacro="http://www.ros.org/wiki/xacro">
<material name="black">
<color rgba="0.0 0.0 0.0 1.0"/>
</material>
<material name="blue">
<color rgba="0.203125 0.23828125 0.28515625 1.0"/>
</material>
<material name="green">
<color rgba="0.0 0.8 0.0 1.0"/>
</material>
<material name="grey">
<color rgba="0.2 0.2 0.2 1.0"/>
</material>
<material name="orange">
<color rgba="1.0 0.423529411765 0.0392156862745 1.0"/>
</material>
<material name="brown">
<color rgba="0.870588235294 0.811764705882 0.764705882353 1.0"/>
</material>
<material name="red">
<color rgba="0.80078125 0.12890625 0.1328125 1.0"/>
</material>
<material name="white">
<color rgba="1.0 1.0 1.0 1.0"/>
</material>
<gazebo reference="link_chassis">
<material>Gazebo/Orange</material>
</gazebo>
<gazebo reference="link_left_wheel">
<material>Gazebo/Blue</material>
</gazebo>
<gazebo reference="link_right_wheel">
<material>Gazebo/Blue</material>
</gazebo>
<gazebo>
<plugin filename="libgazebo_ros_diff_drive.so" name="differential_drive_controller">
<legacyMode>false</legacyMode>
<alwaysOn>true</alwaysOn>
<updateRate>20</updateRate>
<leftJoint>joint_left_wheel</leftJoint>
<rightJoint>joint_right_wheel</rightJoint>
<wheelSeparation>0.2</wheelSeparation>
<wheelDiameter>0.2</wheelDiameter>
<torque>0.1</torque>
<commandTopic>cmd_vel</commandTopic>
<odometryTopic>odom</odometryTopic>
<odometryFrame>odom</odometryFrame>
<robotBaseFrame>link_chassis</robotBaseFrame>
</plugin>
</gazebo>
<link name="link_chassis">
<!-- pose and inertial -->
<pose>0 0 0.1 0 0 0</pose>
<inertial>
<mass value="5"/>
<origin rpy="0 0 0" xyz="0 0 0.1"/>
<inertia ixx="0.0395416666667" ixy="0" ixz="0" iyy="0.106208333333" iyz="0" izz="0.106208333333"/>
</inertial>
<!-- body -->
<collision name="collision_chassis">
<geometry>
<box size="0.5 0.3 0.07"/>
</geometry>
</collision>
<visual>
<origin rpy="0 0 0" xyz="0 0 0"/>
<geometry>
<box size="0.5 0.3 0.07"/>
</geometry>
<material name="blue"/>
</visual>
<!-- caster front -->
<collision name="caster_front_collision">
<origin rpy=" 0 0 0" xyz="0.35 0 -0.05"/>
<geometry>
<sphere radius="0.05"/>
</geometry>
<surface>
<friction>
<ode>
<mu>0</mu>
<mu2>0</mu2>
<slip1>1.0</slip1>
<slip2>1.0</slip2>
</ode>
</friction>
</surface>
</collision>
<visual name="caster_front_visual">
<origin rpy=" 0 0 0" xyz="0.2 0 -0.05"/>
<geometry>
<sphere radius="0.05"/>
</geometry>
</visual>
</link>
<link name="link_right_wheel">
<inertial>
<mass value="0.2"/>
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<inertia ixx="0.000526666666667" ixy="0" ixz="0" iyy="0.000526666666667" iyz="0" izz="0.001"/>
</inertial>
<collision name="link_right_wheel_collision">
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<geometry>
<cylinder length="0.04" radius="0.1"/>
</geometry>
</collision>
<visual name="link_right_wheel_visual">
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<geometry>
<cylinder length="0.04" radius="0.1"/>
</geometry>
</visual>
</link>
<joint name="joint_right_wheel" type="continuous">
<origin rpy="0 0 0" xyz="-0.05 0.15 0"/>
<child link="link_right_wheel"/>
<parent link="link_chassis"/>
<axis rpy="0 0 0" xyz="0 1 0"/>
<limit effort="10000" velocity="1000"/>
<joint_properties damping="1.0" friction="1.0"/>
</joint>
<link name="link_left_wheel">
<inertial>
<mass value="0.2"/>
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<inertia ixx="0.000526666666667" ixy="0" ixz="0" iyy="0.000526666666667" iyz="0" izz="0.001"/>
</inertial>
<collision name="link_left_wheel_collision">
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<geometry>
<cylinder length="0.04" radius="0.1"/>
</geometry>
</collision>
<visual name="link_left_wheel_visual">
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<geometry>
<cylinder length="0.04" radius="0.1"/>
</geometry>
</visual>
</link>
<joint name="joint_left_wheel" type="continuous">
<origin rpy="0 0 0" xyz="-0.05 -0.15 0"/>
<child link="link_left_wheel"/>
<parent link="link_chassis"/>
<axis rpy="0 0 0" xyz="0 1 0"/>
<limit effort="10000" velocity="1000"/>
<joint_properties damping="1.0" friction="1.0"/>
</joint>
</robot>
1.2.1
<robot name="m2wr" xmlns:xacro="http://www.ros.org/wiki/xacro">
放的是机器人的名称 + 机器人的主链接
1.2.2 主体部分
<link name="link_chassis">
...
</link>
link内部代表的机器人的主部分,内部需要描述主体部分的外观和物理属性,包括size,colod.shape,inertial matrix,colliaion
1.2.2.1
<pose.../pose>
代表位姿 x y z 滚动 俯仰 偏航
1.2.2.2惯性部分
<inertial>
<mass value= 代表质量
<origin rpy= xyz= 初始的位姿(与上面的pose一致即可)
<inertia 惯性矩阵
</inertial>
1.2.2.3碰撞属性
<collision name="collision_chassis">
<geometry> 描述物体的大小(真实的仿真物理效果)
<box size="0.5 0.3 0.07"/>
</geometry>
</collision>
1.2.2.4视觉部分(gazebo上所能看到的)
<visual>
<origin rpy="0 0 0" xyz="0 0 0"/>
<geometry> 代表形状
<box size="0.5 0.3 0.07"/>
</geometry>
<material name="blue"/>
</visual>
1.2.2.5前面放一个脚轮(也属于主体部分,可防止碰撞)
在collision内部代表仿真里真实的物理属性
<geometry>
<sphere radius="0.05"/> sphere 代表是一个球体,radius代表半径
</geometry>
<surface> 物体的表面情况
<friction> 摩擦力
<ode>
<mu>0</mu> 两个物体之间的摩擦力
<mu2>0</mu2>
<slip1>1.0</slip1>
<slip2>1.0</slip2>
</ode>
</friction>
</surface>
1.2.3 添加侧轮
1.2.3.1 定义轮子的部分
<link name="link_right_wheel">
<inertial>
<mass value="0.2"/>
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/> 此处 子部件初始位置给000 然后在连接部分 连接即可。
<inertia ixx="0.000526666666667" ixy="0" ixz="0" iyy="0.000526666666667" iyz="0" izz="0.001"/>
</inertial>
<collision name="link_right_wheel_collision">
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<geometry>
<cylinder length="0.04" radius="0.1"/>
</geometry>
</collision>
<visual name="link_right_wheel_visual">
<origin rpy="0 1.5707 1.5707" xyz="0 0 0"/>
<geometry>
<cylinder length="0.04" radius="0.1"/>
</geometry>
</visual>
1.2.3.2 定义轮子与主体连接的部分
<joint name="joint_right_wheel" type="continuous"> 连接的
<origin rpy="0 0 0" xyz="-0.05 0.15 0"/>
<child link="link_right_wheel"/> 子部件
<parent link="link_chassis"/> 主体部分
<axis rpy="0 0 0" xyz="0 1 0"/> 旋转
<limit effort="10000" velocity="1000"/> 限制力 限制速度
<joint_properties damping="1.0" friction="1.0"/> 减震 摩擦力
</joint>
以上添加了一个右轮
1.2.4 其它(颜色)
<material name="black">
<color rgba="0.0 0.0 0.0 1.0"/> 如何成为black这个颜色
</material>
定义一个材料名black
<gazebo reference="link_chassis">
<material>Gazebo/Orange</material>
</gazebo>
为reference 涂上颜色
1.3添加rviz,顺便编写好launch文件
<?xml version="1.0"?>
<launch>
<param name="robot_description" command="cat '$(find m2wr_description)/urdf/m2wr.xacro'"/>
<!-- send fake joint values -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher">
<param name="use_gui" value="False"/>
</node>
<!-- Combine joint values -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"/>
<!-- Show in Rviz -->
<node name="rviz" pkg="rviz" type="rviz" />
</launch>
以下进行解读。
1.3.1
<param name="robot_description" command="cat '$(find m2wr_description)/urdf/m2wr.xacro'"/>
< param name 对于机器人的描述
command =cat 代表直接读取的模式,
1.3.2
<!-- send fake joint values -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher">
<param name="use_gui" value="False"/>
</node>
<!-- Combine joint values -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"/>
启动两个节点
1.3.3
启动rviz
<!-- Show in Rviz -->
<node name="rviz" pkg="rviz" type="rviz" />
编译。catkin_make
2 启动!
roslaunch m2wr_description rviz.launch
3.添加gazebo
创建一个spawn.launch文件
<?xml version="1.0" encoding="UTF-8"?>
<launch>
<param name="robot_description" command="cat '$(find m2wr_description)/urdf/m2wr.xacro'"/>
<arg name="x" default="0"/>
<arg name="y" default="0"/>
<arg name="z" default="0.5"/>
<node name="mybot_spawn" pkg="gazebo_ros" type="spawn_model" output="screen"
args="-urdf -param robot_description -model m2wr -x $(arg x) -y $(arg y) -z $(arg z)" />
</launch>
<param name="robot_description" command="cat '$(find m2wr_description)/urdf/m2wr.xacro'"/>
<arg name="x" default="0"/>
<arg name="y" default="0"/>
<arg name="z" default="0.5"/>
启动机器人文件,定义其初始位置。
2.
<node name="mybot_spawn" pkg="gazebo_ros" type="spawn_model" output="screen"
args="-urdf -param robot_description -model m2wr -x $(arg x) -y $(arg y) -z $(arg z)" />
启动gazebo环境
于是,roslaunch m2wr_description spawn.launch
结果:
经查阅后,需要在launch文件中添加下面内容:
在.launch文件中的标签中添加如下代码行,重新运行即可解决问题
<!-- these are the arguments you can pass this launch file, for example paused:=true -->
<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"/>
<!-- We resume the logic in empty_world.launch -->
<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)"/>
<!-- arg name="world_name" value="$(find task_1)/world/tutorial_testing.world"/ -->
</include>
再次启动后,就正常了
4.为机器人添加功能!
<gazebo>
<plugin filename="libgazebo_ros_diff_drive.so" name="differential_drive_controller"> 差分驱动控制,驱使行动
<legacyMode>false</legacyMode> 传统模式设置
<alwaysOn>true</alwaysOn> 功能始终打开
<updateRate>20</updateRate> 发送消息的速率
<leftJoint>joint_left_wheel</leftJoint> 左关节
<rightJoint>joint_right_wheel</rightJoint> 右关节
<wheelSeparation>0.3</wheelSeparation> 车轮之间的距离
<wheelDiameter>0.2</wheelDiameter> 车轮直径
<torque>0.1</torque> 扭矩
<commandTopic>cmd_vel</commandTopic> 命令主题
<odometryTopic>odom</odometryTopic> 里程计使用主题
<odometryFrame>odom</odometryFrame> 里程计使用的框架
<robotBaseFrame>link_chassis</robotBaseFrame> 机器人的主体部分
</plugin>
</gazebo>
在xacro文件里,可以给机器人添加功能属性!编辑在plugin内部!
再次启动
roslaunch m2wr_description spawn.launch
再次打开一个命令行:
rostopic list
有一个cmd_vel了!和上面呼应,故可以控制小车运动了!
再打开一个命令行:
rosrun teleop_twist_keyboard teleop_twist_keyboard.py
此时小车可以运动了。