上一篇博客:ros机器人编程实践(16.1)- 仿真SmartCar之搭建模型
上一章已经成功搭建SmartCar模型,这一章在基础上做一些优化,并让他跑起来
安装arbotix功能包,用于发布速度命令并执行
sudo apt-get install ros-kinetic-arbotix
安装joint-state-publisher发布器,可以控制速度
sudo apt-get install ros-kinetic-joint-state-publisher-gui
创建配置文件夹config
cd ~/SmartCar_ws/src/smartcar_description
mkdir config
创建配置文件
cd config
sudo gedit smartcar_arbotix.yaml
贴上如下代码:
port: /dev/ttyUSB0
baud: 115200
rate: 20
sync_write: True
sync_read: True
read_rate: 20
write_rate: 20
controllers: {
# Pololu motors: 1856 cpr = 0.3888105m travel = 4773 ticks per meter (empirical: 4100)
base_controller: {type: diff_controller, base_frame_id: base_link, base_width: 0.26, ticks_meter: 4100, Kp: 12, Kd: 12, Ki: 0, Ko: 50, accel_limit: 1.0 }
}
注:这个配置文件和odom坐标系有关,笔者学的也不深入,不是很理解这里的原理,不过不重要
根据古月居老师的讲解,gazebo对urdf的文件支持不如xacro文件,所以将上篇博客写的urdf改成xacro
cd ~/SmartCar_ws/src/smartcar_description/urdf
gedit gazebo.urdf.xacro
gedit smartcar.urdf.xacro
gedit smartcar_body.urdf.xacro
gazebo.urdf.xacro:
<robot xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"
xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"
xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"
xmlns:xacro="http://ros.org/wiki/xacro"
name="smartcar_gazebo">
<xacro:macro name="smartcar_sim">
<gazebo reference="base_link">
<material>Gazebo/Bluematerial>
<turnGravityOff>falseturnGravityOff>
gazebo>
<gazebo reference="right_front_wheel">
<material>Gazebo/FlatBlackmaterial>
gazebo>
<gazebo reference="right_back_wheel">
<material>Gazebo/FlatBlackmaterial>
gazebo>
<gazebo reference="left_front_wheel">
<material>Gazebo/FlatBlackmaterial>
gazebo>
<gazebo reference="left_back_wheel">
<material>Gazebo/FlatBlackmaterial>
gazebo>
<gazebo reference="head">
<material>Gazebo/Whitematerial>
gazebo>
xacro:macro>
robot>
smartcar.urdf.xacro:
<robot name="smartcar"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:gazebo="http://playerstage.sourceforge.net/gazebo/xmlschema/#gz"
xmlns:model="http://playerstage.sourceforge.net/gazebo/xmlschema/#model"
xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"
xmlns:body="http://playerstage.sourceforge.net/gazebo/xmlschema/#body"
xmlns:geom="http://playerstage.sourceforge.net/gazebo/xmlschema/#geom"
xmlns:joint="http://playerstage.sourceforge.net/gazebo/xmlschema/#joint"
xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"
xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"
xmlns:rendering="http://playerstage.sourceforge.net/gazebo/xmlschema/#rendering"
xmlns:renderable="http://playerstage.sourceforge.net/gazebo/xmlschema/#renderable"
xmlns:physics="http://playerstage.sourceforge.net/gazebo/xmlschema/#physics"
xmlns:xacro="http://ros.org/wiki/xacro">
<include filename="$(find smartcar_description)/urdf/smartcar_body.urdf.xacro" />
<smartcar_body/>
<smartcar_sim/>
robot>
smartcar_body.urdf.xacro:
<robot name="smartcar" xmlns:xacro="http://ros.org/wiki/xacro">
<property name="M_PI" value="3.14159"/>
<include filename="$(find smartcar_description)/urdf/gazebo.urdf.xacro"/>
<property name="base_x" value="0.33" />
<property name="base_y" value="0.33" />
<xacro:macro name="smartcar_body">
<link name="base_link">
<inertial>
<origin xyz="0 0 0.055"/>
<mass value="100.0" />
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
<visual>
<geometry>
<box size="0.25 .16 .05"/>
geometry>
<origin rpy="0 0 0" xyz="0 0 0.055"/>
<material name="blue">
<color rgba="0 0 .8 1"/>
material>
visual>
<collision>
<origin rpy="0 0 0" xyz="0 0 0.055"/>
<geometry>
<box size="0.25 .16 .05" />
geometry>
collision>
link>
<link name="left_front_wheel">
<inertial>
<origin xyz="0.08 0.08 0.025"/>
<mass value="1" />
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
<visual>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
<material name="black">
<color rgba="0 0 0 1"/>
material>
visual>
<collision>
<origin rpy="0 1.57075 1.57075" xyz="0.08 0.08 0.025"/>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
collision>
link>
<joint name="left_front_wheel_joint" type="continuous">
<axis xyz="0 0 1"/>
<parent link="base_link"/>
<child link="left_front_wheel"/>
<origin rpy="0 1.57075 1.57075" xyz="0.08 0.08 0.025"/>
<limit effort="100" velocity="100"/>
<joint_properties damping="0.0" friction="0.0"/>
joint>
<link name="right_front_wheel">
<inertial>
<origin xyz="0.08 -0.08 0.025"/>
<mass value="1" />
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
<visual>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
<material name="black">
<color rgba="0 0 0 1"/>
material>
visual>
<collision>
<origin rpy="0 1.57075 1.57075" xyz="0.08 -0.08 0.025"/>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
collision>
link>
<joint name="right_front_wheel_joint" type="continuous">
<axis xyz="0 0 1"/>
<parent link="base_link"/>
<child link="right_front_wheel"/>
<origin rpy="0 1.57075 1.57075" xyz="0.08 -0.08 0.025"/>
<limit effort="100" velocity="100"/>
<joint_properties damping="0.0" friction="0.0"/>
joint>
<link name="left_back_wheel">
<inertial>
<origin xyz="-0.08 0.08 0.025"/>
<mass value="1" />
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
<visual>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
<material name="black">
<color rgba="0 0 0 1"/>
material>
visual>
<collision>
<origin rpy="0 1.57075 1.57075" xyz="-0.08 0.08 0.025"/>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
collision>
link>
<joint name="left_back_wheel_joint" type="continuous">
<axis xyz="0 0 1"/>
<parent link="base_link"/>
<child link="left_back_wheel"/>
<origin rpy="0 1.57075 1.57075" xyz="-0.08 0.08 0.025"/>
<limit effort="100" velocity="100"/>
<joint_properties damping="0.0" friction="0.0"/>
joint>
<link name="right_back_wheel">
<inertial>
<origin xyz="-0.08 -0.08 0.025"/>
<mass value="1" />
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
<visual>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
<material name="black">
<color rgba="0 0 0 1"/>
material>
visual>
<collision>
<origin rpy="0 1.57075 1.57075" xyz="-0.08 -0.08 0.025"/>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
collision>
link>
<joint name="right_back_wheel_joint" type="continuous">
<axis xyz="0 0 1"/>
<parent link="base_link"/>
<child link="right_back_wheel"/>
<origin rpy="0 1.57075 1.57075" xyz="-0.08 -0.08 0.025"/>
<limit effort="100" velocity="100"/>
<joint_properties damping="0.0" friction="0.0"/>
joint>
<link name="head">
<inertial>
<origin xyz="0.08 0 0.08"/>
<mass value="1" />
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
inertial>
<visual>
<geometry>
<box size=".02 .03 .03"/>
geometry>
<material name="white">
<color rgba="1 1 1 1"/>
material>
visual>
<collision>
<origin xyz="0.08 0 0.08"/>
<geometry>
<cylinder length=".02" radius="0.025"/>
geometry>
collision>
link>
<joint name="tobox" type="fixed">
<parent link="base_link"/>
<child link="head"/>
<origin xyz="0.08 0 0.08"/>
joint>
xacro:macro>
robot>
在rviz中启动模型
roslaunch smartcar_description smartcar_display.rviz.launch
和上一节一样添加模型如下图:
这里Fixed Frame选择odom坐标系:
发布速度命令:
rostopic pub -r 10 /cmd_vel geometry_msgs/Twist '{linear: {x: 0.5, y: 0, z: 0}, angular: {x: 0, y: 0, z: 0.5}}'
前面三个参数是x,y,z三个方向的速度,后面三个参数是绕x,y,z三个轴旋转的角速度,上述命令就是让小车以0.5m/s的线速度,0.5rad/s的角速度画圆。
执行效果: