首页 > 代码库 > Robot Operating System (ROS)学习笔记2---使用smartcar进行仿真

Robot Operating System (ROS)学习笔记2---使用smartcar进行仿真

搭建环境:XMWare  Ubuntu14.04  ROS(indigo)

转载自古月居  转载连接:http://www.guyuehome.com/248

技术分享

 

一、模型完善

  文件夹urdf下,创建gazebo.urdf.xacro、smartcar.urdf.xacro、smartcar_body.urdf.xacro三个文件

1、机器人主体smartcar_body.urdf.xacro文件

 

  1 <?xml version="1.0"?>  
  2 <robot name="smartcar" xmlns:xacro="http://ros.org/wiki/xacro">  
  3   <property name="M_PI" value="3.14159"/>  
  4  
  5   <!-- Macro for SmartCar body. Including Gazebo extensions, but does not include Kinect -->  
  6   <include filename="$(find smartcar_description)/urdf/gazebo.urdf.xacro"/>  
  7  
  8   <property name="base_x" value="0.33" />  
  9   <property name="base_y" value="0.33" />  
 10  
 11   <xacro:macro name="smartcar_body">  
 12  
 13  
 14     <link name="base_link">  
 15     <inertial>  
 16       <origin xyz="0 0 0.055"/>  
 17       <mass value="1.0" />  
 18       <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
 19     </inertial>  
 20     <visual>  
 21       <geometry>  
 22         <box size="0.25 .16 .05"/>  
 23       </geometry>  
 24       <origin rpy="0 0 0" xyz="0 0 0.055"/>  
 25       <material name="blue">  
 26       <color rgba="0 0 .8 1"/>  
 27       </material>  
 28    </visual>  
 29    <collision>  
 30       <origin rpy="0 0 0" xyz="0 0 0.055"/>  
 31       <geometry>  
 32         <box size="0.25 .16 .05" />  
 33       </geometry>  
 34     </collision>  
 35   </link>  
 36  
 37  
 38  <link name="left_front_wheel">  
 39     <inertial>  
 40       <origin  xyz="0.08 0.08 0.025"/>  
 41       <mass value="0.1" />  
 42        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
 43     </inertial>  
 44     <visual>  
 45       <geometry>  
 46         <cylinder length=".02" radius="0.025"/>  
 47       </geometry>  
 48       <material name="black">  
 49         <color rgba="0 0 0 1"/>  
 50       </material>  
 51     </visual>  
 52     <collision>  
 53       <origin rpy="0 1.57075 1.57075" xyz="0.08 0.08 0.025"/>  
 54       <geometry>  
 55          <cylinder length=".02" radius="0.025"/>  
 56       </geometry>  
 57     </collision>  
 58   </link>  
 59  
 60   <joint name="left_front_wheel_joint" type="continuous">  
 61     <axis xyz="0 0 1"/>  
 62     <parent link="base_link"/>  
 63     <child link="left_front_wheel"/>  
 64     <origin rpy="0 1.57075 1.57075" xyz="0.08 0.08 0.025"/>  
 65     <limit effort="100" velocity="100"/>  
 66     <joint_properties damping="0.0" friction="0.0"/>  
 67   </joint>  
 68  
 69   <link name="right_front_wheel">  
 70     <inertial>  
 71       <origin xyz="0.08 -0.08 0.025"/>  
 72       <mass value="0.1" />  
 73        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
 74     </inertial>  
 75     <visual>  
 76       <geometry>  
 77         <cylinder length=".02" radius="0.025"/>  
 78       </geometry>  
 79       <material name="black">  
 80         <color rgba="0 0 0 1"/>  
 81       </material>  
 82     </visual>  
 83     <collision>  
 84       <origin rpy="0 1.57075 1.57075" xyz="0.08 -0.08 0.025"/>  
 85       <geometry>  
 86          <cylinder length=".02" radius="0.025"/>  
 87       </geometry>  
 88     </collision>  
 89   </link>  
 90  
 91   <joint name="right_front_wheel_joint" type="continuous">  
 92     <axis xyz="0 0 1"/>  
 93     <parent link="base_link"/>  
 94     <child link="right_front_wheel"/>  
 95     <origin rpy="0 1.57075 1.57075" xyz="0.08 -0.08 0.025"/>  
 96     <limit effort="100" velocity="100"/>  
 97     <joint_properties damping="0.0" friction="0.0"/>  
 98  </joint>  
 99  
100  <link name="left_back_wheel">  
101     <inertial>  
102       <origin xyz="-0.08 0.08 0.025"/>  
103       <mass value="0.1" />  
104        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
105     </inertial>  
106     <visual>  
107       <geometry>  
108         <cylinder length=".02" radius="0.025"/>  
109       </geometry>  
110       <material name="black">  
111         <color rgba="0 0 0 1"/>  
112       </material>  
113    </visual>  
114    <collision>  
115        <origin rpy="0 1.57075 1.57075" xyz="-0.08 0.08 0.025"/>  
116       <geometry>  
117          <cylinder length=".02" radius="0.025"/>  
118       </geometry>  
119     </collision>  
120   </link>  
121  
122   <joint name="left_back_wheel_joint" type="continuous">  
123     <axis xyz="0 0 1"/>  
124     <parent link="base_link"/>  
125     <child link="left_back_wheel"/>  
126     <origin rpy="0 1.57075 1.57075" xyz="-0.08 0.08 0.025"/>  
127     <limit effort="100" velocity="100"/>  
128     <joint_properties damping="0.0" friction="0.0"/>  
129   </joint>  
130  
131   <link name="right_back_wheel">  
132     <inertial>  
133        <origin xyz="-0.08 -0.08 0.025"/>  
134        <mass value="0.1" />  
135        <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
136     </inertial>  
137     <visual>  
138       <geometry>  
139         <cylinder length=".02" radius="0.025"/>  
140       </geometry>  
141       <material name="black">  
142         <color rgba="0 0 0 1"/>  
143       </material>  
144    </visual>  
145    <collision>  
146       <origin rpy="0 1.57075 1.57075" xyz="-0.08 -0.08 0.025"/>  
147       <geometry>  
148          <cylinder length=".02" radius="0.025"/>  
149       </geometry>  
150     </collision>  
151   </link>  
152  
153  
154   <joint name="right_back_wheel_joint" type="continuous">  
155     <axis xyz="0 0 1"/>  
156     <parent link="base_link"/>  
157     <child link="right_back_wheel"/>  
158     <origin rpy="0 1.57075 1.57075" xyz="-0.08 -0.08 0.025"/>  
159     <limit effort="100" velocity="100"/>  
160     <joint_properties damping="0.0" friction="0.0"/>  
161   </joint>  
162  
163   <link name="head">  
164     <inertial>  
165       <origin xyz="0.08 0 0.08"/>  
166       <mass value="0.1" />  
167       <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>  
168     </inertial>  
169     <visual>  
170       <geometry>  
171         <box size=".02 .03 .03"/>  
172       </geometry>  
173       <material name="white">  
174         <color rgba="1 1 1 1"/>  
175       </material>  
176      </visual>  
177      <collision>  
178       <origin xyz="0.08 0 0.08"/>  
179       <geometry>  
180          <cylinder length=".02" radius="0.025"/>  
181       </geometry>  
182     </collision>  
183   </link>  
184  
185   <joint name="tobox" type="fixed">  
186     <parent link="base_link"/>  
187     <child link="head"/>  
188     <origin xyz="0.08 0 0.08"/>  
189   </joint>  
190   </xacro:macro>  
191  
192 </robot>

 

2、gazebo属性部分gazebo.urdf.xacro

 1 <?xml version="1.0"?>  
 2  
 3 <robot xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"   
 4     xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"   
 5     xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"   
 6     xmlns:xacro="http://ros.org/wiki/xacro"   
 7     name="smartcar_gazebo">  
 8  
 9 <!-- ASUS Xtion PRO camera for simulation -->  
10 <!-- gazebo_ros_wge100 plugin is in kt2_gazebo_plugins package -->  
11 <xacro:macro name="smartcar_sim">  
12     <gazebo reference="base_link">  
13         <material>Gazebo/Blue</material>  
14     </gazebo>  
15  
16     <gazebo reference="right_front_wheel">  
17         <material>Gazebo/FlatBlack</material>  
18     </gazebo>  
19  
20     <gazebo reference="right_back_wheel">  
21         <material>Gazebo/FlatBlack</material>  
22     </gazebo>  
23  
24     <gazebo reference="left_front_wheel">  
25         <material>Gazebo/FlatBlack</material>  
26     </gazebo>  
27  
28     <gazebo reference="left_back_wheel">  
29         <material>Gazebo/FlatBlack</material>  
30     </gazebo>  
31  
32     <gazebo reference="head">  
33         <material>Gazebo/White</material>  
34     </gazebo>  
35  
36 </xacro:macro>  
37  
38 </robot>

3、主文件smartcar.urdf.xacro

 1 <?xml version="1.0"?>
 2 
 3 <robot name="smartcar"  
 4     xmlns:xi="http://www.w3.org/2001/XInclude"
 5     xmlns:gazebo="http://playerstage.sourceforge.net/gazebo/xmlschema/#gz"
 6     xmlns:model="http://playerstage.sourceforge.net/gazebo/xmlschema/#model"
 7     xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"
 8     xmlns:body="http://playerstage.sourceforge.net/gazebo/xmlschema/#body"
 9     xmlns:geom="http://playerstage.sourceforge.net/gazebo/xmlschema/#geom"
10     xmlns:joint="http://playerstage.sourceforge.net/gazebo/xmlschema/#joint"
11     xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"
12 
13     xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"
14     xmlns:rendering="http://playerstage.sourceforge.net/gazebo/xmlschema/#rendering"
15     xmlns:renderable="http://playerstage.sourceforge.net/gazebo/xmlschema/#renderable"
16 
17     xmlns:physics="http://playerstage.sourceforge.net/gazebo/xmlschema/#physics"
18     xmlns:xacro="http://ros.org/wiki/xacro">
19 
20   <include filename="$(find smartcar_description)/urdf/smartcar_body.urdf.xacro" />
21 
22   <!-- Body of SmartCar, with plates, standoffs and Create (including sim sensors) -->
23   <smartcar_body/>
24 
25   <smartcar_sim/>
26 
27 </robot>

二、lanuch文件

    在launch文件创建smartcar_display.rviz.launch文件(touch smartcar_display.rviz.launch)。(注:最后一行(find smartcar_description)/urdf.vcg"改为(find smartcar_description)/urdf.rviz"

 

 1 <launch>
 2     <param name="/use_sim_time" value="false" />
 3     
 4     <!-- Load the URDF/Xacro model of our robot -->
 5         <arg name="urdf_file" default="$(find xacro)/xacro.py ‘$(find smartcar_description)/urdf/smartcar.urdf.xacro‘" />
 6     <arg name="gui" default="false" />
 7 
 8     <param name="robot_description" command="$(arg urdf_file)" />
 9     <param name="use_gui" value="$(arg gui)"/>
10 
11     <node name="arbotix" pkg="arbotix_python" type="arbotix_driver" output="screen"><!--type="driver.py"-->
12         <rosparam command="delete" param="/arbotix" />
13         <rosparam file="$(find smartcar_description)/config/smartcar_arbotix.yaml" command="load" />
14         <param name="sim" value="true"/>    
15         </node>
16 
17     <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" >
18     </node>
19     <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher">
20               <param name="publish_frequency" type="double" value="20.0" />      
21         </node>
22     
23     <node pkg="tf" type="static_transform_publisher" name="odom_left_wheel_broadcaster"  args="0 0 0 0 0 0 /base_link /left_front_link 100" /> 
24     <node pkg="tf" type="static_transform_publisher" name="odom_right_wheel_broadcaster" args="0 0 0 0 0 0 /base_link /right_front_link 100" />
25  
26     <node name="rviz" pkg="rviz" type="rviz" args="-d $(find smartcar_description)/urdf.rviz" />
27 </launch>

 

三、创建config文件夹

在文件中创建smartcar_arbotix.yaml文件

 

 1 port: /dev/ttyUSB0
 2 baud: 115200
 3 rate: 20
 4 sync_write: True
 5 sync_read: True
 6 read_rate: 20
 7 write_rate: 20
 8 
 9 controllers: {
10    #  Pololu motors: 1856 cpr = 0.3888105m travel = 4773 ticks per meter (empirical: 4100)
11    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 }
12 }

 

四、创建urdf.rviz文件

  urdf.rviz文件代码如下:

 

  1 Panels:
  2   - Class: rviz/Displays
  3     Help Height: 78
  4     Name: Displays
  5     Property Tree Widget:
  6       Expanded:
  7         - /Global Options1
  8         - /Status1
  9         - /Odometry1
 10       Splitter Ratio: 0.652661
 11     Tree Height: 422
 12   - Class: rviz/Selection
 13     Name: Selection
 14   - Class: rviz/Tool Properties
 15     Expanded:
 16       - /2D Pose Estimate1
 17       - /2D Nav Goal1
 18     Name: Tool Properties
 19     Splitter Ratio: 0.588679
 20   - Class: rviz/Views
 21     Expanded:
 22       - /Current View1
 23     Name: Views
 24     Splitter Ratio: 0.5
 25   - Class: rviz/Time
 26     Name: Time
 27 Visualization Manager:
 28   Class: ""
 29   Displays:
 30     - Alpha: 0.5
 31       Cell Size: 0.5
 32       Class: rviz/Grid
 33       Color: 160; 160; 164
 34       Enabled: true
 35       Line Style:
 36         Line Width: 0.03
 37         Value: Lines
 38       Name: Grid
 39       Normal Cell Count: 0
 40       Offset:
 41         X: 0
 42         Y: 0
 43         Z: 0
 44       Plane: XY
 45       Plane Cell Count: 20
 46       Reference Frame: /odom
 47       Value: true
 48     - Angle Tolerance: 0.05
 49       Class: rviz/Odometry
 50       Color: 221; 200; 14
 51       Enabled: true
 52       Keep: 100
 53       Length: 0.6
 54       Name: Odometry
 55       Position Tolerance: 0.1
 56       Topic: /odom
 57       Value: true
 58     - Angle Tolerance: 0.1
 59       Class: rviz/Odometry
 60       Color: 253; 124; 0
 61       Enabled: false
 62       Keep: 100
 63       Length: 0.6
 64       Name: Odometry EKF
 65       Position Tolerance: 0.1
 66       Topic: /odom
 67       Value: false
 68     - Alpha: 1
 69       Class: rviz/RobotModel
 70       Collision Enabled: false
 71       Enabled: true
 72       Links:
 73         base_footprint:
 74           Alpha: 1
 75           Show Axes: false
 76           Show Trail: false
 77           Value: true
 78         base_link:
 79           Alpha: 1
 80           Show Axes: false
 81           Show Trail: false
 82           Value: true
 83         camera_link:
 84           Alpha: 1
 85           Show Axes: false
 86           Show Trail: false
 87           Value: true
 88         front_wheel_link:
 89           Alpha: 1
 90           Show Axes: false
 91           Show Trail: false
 92           Value: true
 93         laser:
 94           Alpha: 1
 95           Show Axes: false
 96           Show Trail: false
 97           Value: true
 98         left_wheel_link:
 99           Alpha: 1
100           Show Axes: false
101           Show Trail: false
102           Value: true
103         plate_0_link:
104           Alpha: 1
105           Show Axes: false
106           Show Trail: false
107           Value: true
108         plate_1_link:
109           Alpha: 1
110           Show Axes: false
111           Show Trail: false
112           Value: true
113         plate_2_link:
114           Alpha: 1
115           Show Axes: false
116           Show Trail: false
117           Value: true
118         plate_3_link:
119           Alpha: 1
120           Show Axes: false
121           Show Trail: false
122           Value: true
123         rear_wheel_link:
124           Alpha: 1
125           Show Axes: false
126           Show Trail: false
127           Value: true
128         right_wheel_link:
129           Alpha: 1
130           Show Axes: false
131           Show Trail: false
132           Value: true
133         spacer_0_link:
134           Alpha: 1
135           Show Axes: false
136           Show Trail: false
137           Value: true
138         spacer_1_link:
139           Alpha: 1
140           Show Axes: false
141           Show Trail: false
142           Value: true
143         spacer_2_link:
144           Alpha: 1
145           Show Axes: false
146           Show Trail: false
147           Value: true
148         spacer_3_link:
149           Alpha: 1
150           Show Axes: false
151           Show Trail: false
152           Value: true
153         standoff_2in_0_link:
154           Alpha: 1
155           Show Axes: false
156           Show Trail: false
157           Value: true
158         standoff_2in_1_link:
159           Alpha: 1
160           Show Axes: false
161           Show Trail: false
162           Value: true
163         standoff_2in_2_link:
164           Alpha: 1
165           Show Axes: false
166           Show Trail: false
167           Value: true
168         standoff_2in_3_link:
169           Alpha: 1
170           Show Axes: false
171           Show Trail: false
172           Value: true
173         standoff_2in_4_link:
174           Alpha: 1
175           Show Axes: false
176           Show Trail: false
177           Value: true
178         standoff_2in_5_link:
179           Alpha: 1
180           Show Axes: false
181           Show Trail: false
182           Value: true
183         standoff_2in_6_link:
184           Alpha: 1
185           Show Axes: false
186           Show Trail: false
187           Value: true
188         standoff_2in_7_link:
189           Alpha: 1
190           Show Axes: false
191           Show Trail: false
192           Value: true
193         standoff_8in_0_link:
194           Alpha: 1
195           Show Axes: false
196           Show Trail: false
197           Value: true
198         standoff_8in_1_link:
199           Alpha: 1
200           Show Axes: false
201           Show Trail: false
202           Value: true
203         standoff_8in_2_link:
204           Alpha: 1
205           Show Axes: false
206           Show Trail: false
207           Value: true
208         standoff_8in_3_link:
209           Alpha: 1
210           Show Axes: false
211           Show Trail: false
212           Value: true
213         standoff_kinect_0_link:
214           Alpha: 1
215           Show Axes: false
216           Show Trail: false
217           Value: true
218         standoff_kinect_1_link:
219           Alpha: 1
220           Show Axes: false
221           Show Trail: false
222           Value: true
223       Name: RobotModel
224       Robot Description: robot_description
225       TF Prefix: ""
226       Update Interval: 0
227       Value: true
228       Visual Enabled: true
229   Enabled: true
230   Global Options:
231     Background Color: 31; 31; 31
232     Fixed Frame: /odom
233   Name: root
234   Tools:
235     - Class: rviz/MoveCamera
236     - Class: rviz/Interact
237     - Class: rviz/Select
238     - Class: rviz/SetInitialPose
239       Topic: /initialpose
240     - Class: rviz/SetGoal
241       Topic: /move_base_simple/goal
242   Value: true
243   Views:
244     Current:
245       Angle: -1.57
246       Class: rviz/Orbit
247       Name: Current View
248       Near Clip Distance: 0.01
249       Scale: 178.742
250       Target Frame: <Fixed Frame>
251       Value: Orbit (rviz)
252       X: 0.731489
253       Y: 0.520369
254     Saved: ~
255 Window Geometry:
256   Displays:
257     collapsed: false
258   Height: 696
259   Hide Left Dock: false
260   Hide Right Dock: false
261   QMainWindow State: 000000ff00000000fd00000004000000000000012d00000235fc0200000005fb0000001200530065006c0065006300740069006f006e00000001e10000009b0000006400fffffffb0000001e0054006f006f006c002000500072006f007000650072007400690065007302000001ed000001df00000185000000a3fb000000120056006900650077007300200054006f006f02000001df000002110000018500000122fb000000200054006f006f006c002000500072006f0070006500720074006900650073003203000002880000011d000002210000017afb000000100044006900730070006c006100790073010000004100000235000000dd00ffffff000000010000010f00000270fc0200000003fb0000001e0054006f006f006c002000500072006f00700065007200740069006500730100000041000000780000000000000000fb0000000a00560069006500770073000000002800000270000000b000fffffffb0000001200530065006c0065006300740069006f006e010000025a000000b200000000000000000000000200000490000000a9fc0100000001fb0000000a00560069006500770073030000004e00000080000002e10000019700000003000004a00000003efc0100000002fb0000000800540069006d00650000000000000004a0000002bf00fffffffb0000000800540069006d00650100000000000004500000000000000000000002890000023500000004000000040000000800000008fc0000000100000002000000010000000a0054006f006f006c00730100000000ffffffff0000000000000000
262   Selection:
263     collapsed: false
264   Time:
265     collapsed: false
266   Tool Properties:
267     collapsed: false
268   Views:
269     collapsed: false
270   Width: 972
271   X: 772
272   Y: 332

 

五、仿真测试

        首先运行lanuch,既可以看到rviz中的机器人: 
  
roslaunch smartcar_description smartcar_display.rviz.launch

 

 

Robot Operating System (ROS)学习笔记2---使用smartcar进行仿真