Start the ROS simulation car by hand (1) Simulation environment and car construction

Recently, I plan to complete a self-driving car simulation operation project, so record the process, hoping to help people who need to build a ROS simulation environment, mainly refer to the courses of Bingda Robot, and there are also related videos on station B to learn

Implementation process

1. Create a workspace
2. Gazebo draws a map and saves it as a .world file
3. Urdf car model and xacro car model
4. Ros runs the map and car model
5. View sensor data

1. Create a workspace

mkdir -p ~/catkin_ws/src/tutorials  // 创建文件夹
cd ~/catkin_ws/src/tutorials
mkdir launch  // 存放 launch 文件
mkdir urdf  // 存放小车模型文件
mkdir world  // 存放地图文件

A simple workspace is created, and then add files to it step by step

2. Gazebo draws the map

1. Open gazebo

sudo gazebo  // 打开gazebo,后续保存文件可能需要管理员权限

gazebo interface
2. Enter the editing interface
Enter the file editing interface
Map editing interface
3. Click wall to draw the wall
draw the walls
4. Click Add Texture to modify the wall texture
Modify Wall Textures
5. Click save as of file to save the model file 6. Select the save name and save location 7. Click the model and modify the model position through pose 8. Other models can be added through insert 9. After the map is drawn, save it as a .world map file (the file extension must be .world) 10. Copy the .world file to the ~/catkin_ws/src/world folder, the map The environment is set up
save model

Modify the name and save location

Modify model position

Add other models

save map file

3. Car model

There are usually two types of trolley models, one is urdf , the other is sacro , both are similar, and the definition also has a fixed template, so here directly put the code of the two models
1.urdf trolley model file 1: myrot.urdf , The specific content is as follows:

<?xml version="1.0"?>  
<robot name="mybot">  

  <link name="base_footprint"/> 

  <joint name="base_joint" type="fixed">  
    <parent link="base_footprint"/>  
    <child link="base_link"/>  
    <origin rpy="0 0 0" xyz="0 0 0"/>  
  </joint>  
  
  <link name="base_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0.0001"  ixy="0"  ixz="0" iyy="0.0001" iyz="0" izz="0.001" />
    </inertial>

    <visual>  
      <geometry>  
        <box size="0.25 0.16 0.05"/>  
      </geometry>  
      <origin rpy="0 0 0" xyz="0 0 0"/>  
      <material name="blue">  
          <color rgba="0 0 0.8 1"/>  
      </material>  
    </visual>  

   <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <box size="0.25 0.16 0.05"/>
     </geometry>
   </collision>

  </link>  
 
  <link name="right_wheel_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0.0001"  ixy="0"  ixz="0" iyy="0.0001" iyz="0" izz="0.0001" />
    </inertial>

    <visual>  
      <geometry>  
        <cylinder length="0.02" radius="0.025"/>  
      </geometry>  
      <material name="black">  
        <color rgba="0 0 0 1"/>  
      </material>  
    </visual>  

    <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <cylinder length="0.02" radius="0.025"/> 
     </geometry>
    </collision>
  </link>  
 
  <joint name="right_wheel_joint" type="continuous">  
    <axis xyz="0 0 -1"/>  
    <parent link="base_link"/>  
    <child link="right_wheel_link"/>  
    <origin rpy="1.5707 0 0" xyz=" 0.1 -0.09 -0.03"/>  
  </joint>  
 
  <link name="left_wheel_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0.0001"  ixy="0"  ixz="0" iyy="0.0001" iyz="0" izz="0.0001" />
    </inertial>

    <visual>  
      <geometry>  
        <cylinder length="0.02" radius="0.025"/>  
      </geometry>  
      <material name="black">  
        <color rgba="0 0 0 1"/>  
      </material>  
    </visual>  

    <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <cylinder length="0.02" radius="0.025"/> 
     </geometry>
    </collision>   
  </link>  
 
  <joint name="left_wheel_joint" type="continuous">  
    <axis xyz="0 0 -1"/>  
    <parent link="base_link"/>  
    <child link="left_wheel_link"/>  
    <origin rpy="1.5707 0 0" xyz="0.1 0.09 -0.03"/>  
  </joint>  
 
  <link name="ball_wheel_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0"  ixy="0"  ixz="0" iyy="0" iyz="0" izz="0" />
    </inertial>

    <visual>  
      <geometry>  
        <sphere radius="0.025"/>  
      </geometry>  
      <material name="black">  
        <color rgba="0 0 0 1"/>  
      </material>  
    </visual>  

    <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <sphere radius="0.025"/> 
     </geometry>
    </collision>   
  </link>  

  <joint name="ball_wheel_joint" type="fixed">  
    <axis xyz="0 0 1"/>  
    <parent link="base_link"/>  
    <child link="ball_wheel_link"/>  
    <origin rpy="0 0 0" xyz="-0.10 0 -0.03"/>  
  </joint>  

</robot>

The above car model file has no sensor configuration!
2. The xacro car model file is divided into two parts, namely myrot.xacro and myrot.gazebo.xacro , the specific content is as follows:
myrot.xacro:

<?xml version="1.0"?>  
<robot name="mybot" xmlns:xacro="http://ros.org/wiki/xacro">  

  <xacro:include filename="$(find tutorials)/urdf/mybot.gazebo.xacro" /> 

  <link name="base_footprint"/>

  <joint name="base_joint" type="fixed">  
    <parent link="base_footprint"/>  
    <child link="base_link"/>  
    <origin rpy="0 0 0" xyz="0 0 0"/>  
  </joint>  
  
  <link name="base_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0.0001"  ixy="0"  ixz="0" iyy="0.0001" iyz="0" izz="0.001" />
    </inertial>

    <visual>  
      <geometry>  
        <box size="0.25 0.16 0.05"/>  
      </geometry>  
      <origin rpy="0 0 0" xyz="0 0 0"/>  
      <material name="blue">  
          <color rgba="0 0 0.8 1"/>  
      </material>  
    </visual>  

   <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <box size="0.25 0.16 0.05"/>
     </geometry>
   </collision>

  </link>  
 
  <link name="right_wheel_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0.0001"  ixy="0"  ixz="0" iyy="0.0001" iyz="0" izz="0.0001" />
    </inertial>

    <visual>  
      <geometry>  
        <cylinder length="0.02" radius="0.025"/>  
      </geometry>  
      <material name="black">  
        <color rgba="0 0 0 1"/>  
      </material>  
    </visual>  

    <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <cylinder length="0.02" radius="0.025"/> 
     </geometry>
    </collision>
  </link>  
 
  <joint name="right_wheel_joint" type="continuous">  
    <axis xyz="0 0 -1"/>  
    <parent link="base_link"/>  
    <child link="right_wheel_link"/>  
    <origin rpy="1.5707 0 0" xyz=" 0.1 -0.09 -0.03"/>  
  </joint>  
 
  <link name="left_wheel_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0.0001"  ixy="0"  ixz="0" iyy="0.0001" iyz="0" izz="0.0001" />
    </inertial>

    <visual>  
      <geometry>  
        <cylinder length="0.02" radius="0.025"/>  
      </geometry>  
      <material name="black">  
        <color rgba="0 0 0 1"/>  
      </material>  
    </visual>  

    <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <cylinder length="0.02" radius="0.025"/> 
     </geometry>
    </collision>   
  </link>  
 
  <joint name="left_wheel_joint" type="continuous">  
    <axis xyz="0 0 -1"/>  
    <parent link="base_link"/>  
    <child link="left_wheel_link"/>  
    <origin rpy="1.5707 0 0" xyz="0.1 0.09 -0.03"/>  
  </joint>  
 
  <link name="ball_wheel_link">  
    <inertial>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <mass value="0.1"/>
     <inertia ixx="0"  ixy="0"  ixz="0" iyy="0" iyz="0" izz="0" />
    </inertial>

    <visual>  
      <geometry>  
        <sphere radius="0.025"/>  
      </geometry>  
      <material name="black">  
        <color rgba="0 0 0 1"/>  
      </material>  
    </visual>  

    <collision>
     <origin xyz="0 0 0" rpy="0 0 0"/>
     <geometry>
       <sphere radius="0.025"/> 
     </geometry>
    </collision>   
  </link>  

  <joint name="ball_wheel_joint" type="fixed">  
    <axis xyz="0 0 1"/>  
    <parent link="base_link"/>  
    <child link="ball_wheel_link"/>  
    <origin rpy="0 0 0" xyz="-0.10 0 -0.03"/>  
  </joint>  
  <!-- imu sensor -->
  <link name="imu">  
    <visual>  
      <geometry>  
        <box size="0.01 0.01 0.01"/>  
      </geometry>  
      <material name="white">  
          <color rgba="1 1 1 1"/>  
      </material>  
    </visual>  
  </link>  

  <joint name="imu_joint" type="fixed">  
    <parent link="base_link"/>  
    <child link="imu"/>  
    <origin xyz="0.08 0 0.025"/>  
  </joint> 

  <!-- camera -->
  <link name="base_camera_link">  
    <visual>  
      <geometry>  
        <box size="0.02 0.03 0.03"/>  
      </geometry>  
      <material name="white">  
          <color rgba="1 1 1 1"/>  
      </material>  
    </visual>  
  </link>  

  <joint name="camera_joint" type="fixed">  
    <parent link="base_link"/>  
    <child link="base_camera_link"/>  
    <origin xyz="0.1 0 0.025"/>  
  </joint> 
  <!-- laser lidar -->
  <link name="base_laser_link">  
    <visual>  
      <geometry>  
        <cylinder length="0.06" radius="0.04"/>   
      </geometry>  
      <material name="white">  
          <color rgba="1 1 1 1"/>  
      </material>  
    </visual>  
  </link>  
  
  <joint name="laser_joint" type="fixed">  
    <parent link="base_link"/>  
    <child link="base_laser_link"/>  
    <origin xyz="0 0.0 0.06"/>  
  </joint> 

</robot>

myrot.gazebo.xacro:

<?xml version="1.0"?>
<robot name="mybot" xmlns:xacro="http://ros.org/wiki/xacro">
  <xacro:arg name="laser_visual" default="false"/>
  <xacro:arg name="camera_visual" default="false"/>
  <xacro:arg name="imu_visual"   default="false"/>

  <gazebo reference="base_link">
    <material>Gazebo/DarkGrey</material>
  </gazebo>

  <gazebo reference="left_wheel_link">
    <mu1>0.5</mu1>
    <mu2>0.5</mu2>
    <kp>500000.0</kp>
    <kd>10.0</kd>
    <minDepth>0.001</minDepth>
    <maxVel>1.0</maxVel>
    <fdir1>1 0 0</fdir1>
    <material>Gazebo/DarkGrey</material>
  </gazebo>

  <gazebo reference="right_wheel_link">
    <mu1>0.5</mu1>
    <mu2>0.5</mu2>
    <kp>500000.0</kp>
    <kd>10.0</kd>
    <minDepth>0.001</minDepth>
    <maxVel>1.0</maxVel>
    <fdir1>1 0 0</fdir1>
    <material>Gazebo/FlatBlack</material>
  </gazebo>

  <gazebo reference="ball_wheel_link">
    <mu1>0.1</mu1>
    <mu2>0.1</mu2>
    <kp>500000.0</kp>
    <kd>100.0</kd>
    <minDepth>0.001</minDepth>
    <maxVel>1.0</maxVel>
    <material>Gazebo/FlatBlack</material>
  </gazebo>

  <gazebo reference="imu">
    <sensor type="imu" name="imu">
      <always_on>true</always_on>
      <visualize>$(arg imu_visual)</visualize>
    </sensor>
    <material>Gazebo/FlatBlack</material>
  </gazebo>

  <gazebo>
    <plugin name="mybot_controller" filename="libgazebo_ros_diff_drive.so">
      <commandTopic>cmd_vel</commandTopic>
      <odometryTopic>odom</odometryTopic>
      <odometryFrame>odom</odometryFrame>
      <odometrySource>world</odometrySource>
      <publishOdomTF>true</publishOdomTF>
      <robotBaseFrame>base_footprint</robotBaseFrame>
      <publishWheelTF>false</publishWheelTF>
      <publishTf>true</publishTf>
      <publishWheelJointState>true</publishWheelJointState>
      <legacyMode>false</legacyMode>
      <updateRate>30</updateRate>
      <leftJoint>left_wheel_joint</leftJoint>
      <rightJoint>right_wheel_joint</rightJoint>
      <wheelSeparation>0.180</wheelSeparation>
      <wheelDiameter>0.05</wheelDiameter>
      <wheelAcceleration>10</wheelAcceleration>
      <wheelTorque>100</wheelTorque>
      <rosDebugLevel>na</rosDebugLevel>
    </plugin>
  </gazebo>

  <gazebo>
    <plugin name="imu_plugin" filename="libgazebo_ros_imu.so">
      <alwaysOn>true</alwaysOn>
      <bodyName>imu</bodyName>  
      <frameName>imu</frameName>
      <topicName>imu</topicName>
      <serviceName>imu_service</serviceName>
      <gaussianNoise>0.0</gaussianNoise>
      <updateRate>0</updateRate>
      <imu>
        <noise>
          <type>gaussian</type>
          <rate>
            <mean>0.0</mean>
            <stddev>2e-4</stddev>
            <bias_mean>0.0000075</bias_mean>
            <bias_stddev>0.0000008</bias_stddev>
          </rate>
          <accel>
            <mean>0.0</mean>
            <stddev>1.7e-2</stddev>
            <bias_mean>0.1</bias_mean>
            <bias_stddev>0.001</bias_stddev>
          </accel>
        </noise>
      </imu>
    </plugin>
  </gazebo>

  <gazebo reference="base_laser_link">
    <material>Gazebo/FlatBlack</material>
    <sensor type="ray" name="rplidar_sensor">
      <pose>0 0 0 0 0 0</pose>
      <visualize>$(arg laser_visual)</visualize>
      <update_rate>7</update_rate>
      <ray>
        <scan>
          <horizontal>
            <samples>720</samples>
            <resolution>0.5</resolution>
            <min_angle>0.0</min_angle>
            <max_angle>6.28319</max_angle>
          </horizontal>
        </scan>
        <range>
          <min>0.120</min>
          <max>12.0</max>
          <resolution>0.015</resolution>
        </range>
        <noise>
          <type>gaussian</type>
          <mean>0.0</mean>
          <stddev>0.01</stddev>
        </noise>
      </ray>
      <plugin name="gazebo_ros_rplidar_controller" filename="libgazebo_ros_laser.so">
        <topicName>scan</topicName>
        <frameName>base_laser_link</frameName>
      </plugin>
    </sensor>
  </gazebo>

  <gazebo reference="base_camera_link">
    <sensor type="camera" name="csi Camera">
      <always_on>true</always_on>
      <visualize>$(arg camera_visual)</visualize>
      <camera>
          <horizontal_fov>1.085595</horizontal_fov>
          <image>
              <width>640</width>
              <height>480</height>
              <format>R8G8B8</format>
          </image>
          <clip>
              <near>0.03</near>
              <far>100</far>
          </clip>
      </camera>
      <plugin name="camera_controller" filename="libgazebo_ros_camera.so">
        <alwaysOn>true</alwaysOn>
        <updateRate>30.0</updateRate>
        <cameraName>/</cameraName>
        <frameName>base_camera_link</frameName>
        <imageTopicName>image_raw</imageTopicName>
        <cameraInfoTopicName>camera_info</cameraInfoTopicName>
        <hackBaseline>0.07</hackBaseline>
        <distortionK1>0.0</distortionK1>
        <distortionK2>0.0</distortionK2>
        <distortionK3>0.0</distortionK3>
        <distortionT1>0.0</distortionT1>
        <distortionT2>0.0</distortionT2>
      </plugin>
    </sensor>
  </gazebo>

</robot>

3. Put the car model files myrot.xacro and myrot.gazebo.xacro in the ~/catkin_ws/src/tutorials/urdf/ folder

4. ROS operating environment and car model

1. Write the .launch file and put it in the ~/catkin/src/tutorials/launch folder
gazebo_world.launch:

<launch>
  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="$(find tutorials)/world/room.world"/>  // 注意这里是你的地图文件名
    <arg name="paused" value="false"/>
    <arg name="use_sim_time" value="true"/>
    <arg name="gui" value="true"/>
    <arg name="headless" value="false"/>
    <arg name="debug" value="false"/>
  </include>
</launch>

simulation_robot.launch:

<launch>
  <arg name="x_pos" default="0.0"/>
  <arg name="y_pos" default="0.0"/>
  <arg name="z_pos" default="0.0"/>
  <param name="/use_sim_time" value="true" />  
  
  <include file="$(find tutorials)/launch/gazebo_world.launch"/>

  <param name="robot_description" command="$(find xacro)/xacro --inorder $(find tutorials)/urdf/mybot.xacro" />

  <node pkg="gazebo_ros" type="spawn_model" name="spawn_urdf" args="-urdf -model mybot.xacro -x $(arg x_pos) -y $(arg y_pos) -z $(arg z_pos) -param robot_description" />

  <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

</launch>

2. Write CMakeLists.txt and package.xml file
CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(tutorials)

find_package(catkin REQUIRED COMPONENTS)

package.xml:

<?xml version="1.0"?>
<package format="2">
  <name>tutorials</name>
  <version>0.0.0</version>
  <description>The tutorials package</description>

  <maintainer email="[email protected]">nanorobot</maintainer>

  <license>TODO</license>
</package>

3. After writing, put it in the ~/catkin_ws/src/tutorials folder
4. Compile catkin in the ~/catkin_ws folder

catkin build

5. Don't forget to source , otherwise an error will be reported

source devel/setup.bash

6. Run the launch file

roslaunch tutorials simulation_robot.launch 

7. The running result is as shown in the figure:
Result Figure 1
Result Figure 2
your simulation environment and car have been built here!

5. View sensor data

1. First, you can view the ros node data through rostopic list on the terminal . My results are as follows

/camera_info
/clock
/cmd_vel
/gazebo/link_states
/gazebo/model_states
/gazebo/parameter_descriptions
/gazebo/parameter_updates
/gazebo/set_link_state
/gazebo/set_model_state
/image_raw  // 图像传感器数据节点
/image_raw/compressed
/image_raw/compressed/parameter_descriptions
/image_raw/compressed/parameter_updates
/image_raw/compressedDepth
/image_raw/compressedDepth/parameter_descriptions
/image_raw/compressedDepth/parameter_updates
/image_raw/theora
/image_raw/theora/parameter_descriptions
/image_raw/theora/parameter_updates
/imu
/joint_states
/odom
/parameter_descriptions
/parameter_updates
/rosout
/rosout_agg
/scan  // 激光传感器数据节点
/tf
/tf_static

2. Then visualize the data through rviz
rviz interface
, enter rviz in the terminal to enter the following interface: 3. Modify the frame to odom , click Add , add TF , RobotModel , LaserScan , Image
modify frame
Add
4. After adding, as shown in the figure:
Add to
5. Modify the LaserScan and Image Topic is the output of the ros node, which are /Scan and /Image_raw respectively , and the display results are shown in the figure:
result
so far, the environment and car simulation based on gazebo have been completed

Again, I recommend everyone to go to station B to watch the video learning. Most of the content of this blog refers to the video of Bingda robot station B. The video link is as follows:

Guess you like

Origin blog.csdn.net/m0_52843710/article/details/124572596
car