Gazebo构建小车模型并通过ROS控制

由于原博客中只给了可参考的代码文件,许多内容及命令都没有说清楚,所以此篇博客详细地记录一下构建的流程

一、编写小车的URDF文件

  • 这段代码借鉴的是参考博客中的代码,需要注意的是在里面不能有中文,文件的后缀名称需要是urdf,例如:model.urdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?xml version='1.0'?>
<robot name="dd_robot">
<link name="base_link">
<visual>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
<geometry>
<box size="0.5 0.5 0.25" />
</geometry>
<material name="blue">
<color rgba="0.0 0.5 1.0 1.0"/>
</material>
</visual>
<collision>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
<geometry>
<box size="0.5 0.5 0.25"/>
</geometry>
</collision>
<inertial>
<mass value="5"/>
<inertia ixx="0.13" ixy="0.0" ixz="0.0" iyy="0.21" iyz="0.0" izz="0.13"/>
</inertial>
<visual name="caster">
<origin xyz="0.2 0.0 -0.125" rpy="0.0 0.0 0.0"/>
<geometry>
<sphere radius="0.05"/>
</geometry>
</visual>
<collision>
<origin xyz="0.2 0.0 -0.125" rpy="0.0 0.0 0.0"/>
<geometry>
<sphere radius="0.05"/>
</geometry>
</collision>
</link>

<link name="right_wheel">
<visual>
<origin xyz="0.0 -0.0 0.0" rpy="1.570795 0.0 0.0"/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
</geometry>
<material name="black">
<color rgba="0.05 0.05 0.05 1.0"/>
</material>
</visual>
<collision>
<origin xyz="0.0 0.0 0.0" rpy="1.570795 0.0 0.0"/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
</geometry>
</collision>
</link>

<joint name="join_right_wheel" type="continuous">
<parent link="base_link"/>
<child link="right_wheel"/>
<origin xyz="0.0 -0.30 0.0" rpy="0.0 0.0 0.0"/>
<axis xyz="0.0 1.0 0.0"/>
</joint>

<link name="left_wheel">
<visual>
<origin xyz="0.0 0.0 0.0" rpy="1.570795 0.0 0.0"/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
</geometry>
<material name="black"/>
</visual>
<collision>
<origin xyz="0.0 0.0 0.0" rpy="1.570795 0.0 0.0"/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
</geometry>
</collision>
</link>

<joint name="join_left_wheel" type="continuous">
<parent link="base_link"/>
<child link="left_wheel"/>
<origin xyz="0.0 -0.3 0.0" rpy="0.0 0.0 0.0"/>
<axis xyz="0.0 1.0 0.0"/>
</joint>

<gazebo reference="base_link">
<material>Gazebo/Blue</material>
</gazebo>
<gazebo reference="right_wheel">
<material>Gazebo/Black</material>
</gazebo>
<gazebo reference="left_wheel">
<material>Gazebo/Black</material>
</gazebo>
</robot>
  • 编写启动launch文件,后缀名称是launch,如start.launch,该文件放在根目录下面即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<launch>
<arg name="model" />
<arg name="gui" default="False" />

<param name="robot_description" textfile="$(find ros_robotics)/urdf/$(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 ros_robotics)/urdf.rviz"
required="true" />
</launch>

二、运行启动文件

  • 在运行启动文件之前,需要先进入到根目录下的src目录下,然后使用下列命令创建一个功能包ros_robotics
1
catkin_create_pkg ros_robotics std_msgs roscpp
  • 接着在该ros_robotics目录下面创建文件夹urdf用于存放上文提到的urdf文件

  • 运行下列命令,确保 ROS 工作空间的变更被正确加载
1
source devel/setup.bash
  • 执行launch文件,当前目录中有launch文件时不用指定包路径
1
roslaunch start.launch model:=model.urdf
  • 成功渲染出了该模型文件,注意:第一次执行的时候需要先点左边窗口的Add按钮,找到RobotModel和TF,然后将Fixed Frame设定为base_link即可

三、命令记录

  • 列出ROS_PACKAGE_PATH中包含的路径,确保当前根目录在该路径下面
1
echo $ROS_PACKAGE_PATH
  • 返回你的ROS包ros_robotics 的路径
1
rospack find ros_robotics
  • 构建你的ROS工作区
1
catkin_make
  • 查看当前log占用了多大空间
1
rosclean check
  • 清理ros日志文件
1
rosclean purge