在ROS中完成一个机器小车的模型创建,传感器配置,仿真环境的设置,并控制机器小车进行运动。(1)

一、urdf模型+rviz可视化

创建功能包,添加依赖。用的是Ubantu 20.04

cd catkin_ws  #进入工作空间
catkin_create_pkg jubot_demo urdf xacro #创建功能包、添加依赖
cd jubot_demo/
mkdir urdf
mkdir launch
mkdir meshes #存放渲染机器人模型的文件
mkdir config #存放rviz配置的文件

新建两个文档,jubot_base.urdf(放urdf文件夹下)
display_jubot_base_urdf.launch(放launch文件夹下)
config里的rviz文件是保存生成的,不用写

1. urdf文件

Unified Robot Description Format,统一机器人描述格式,简称为URDF
在这里插入图片描述
上图展示了模型的环节(link)与关节(joint)坐标关系,在基础模型之上,我们为机器人添加尺寸大小。由于每个环节的参考系都位于该环节的底部,关节也是如此,所以在表示尺寸大小时,只需要描述其相对于连接的关节的相对位置关系即可。URDF中的 origin 域就是用来表示这种相对关系。

如果我们为机器人的关节添加 axis 旋转轴参数,那么该机器人模型就可以具备基本的运动学参数。
在这里插入图片描述

关于参数欧拉角rpy,是roll(滚转角)、pitch(俯仰角)、yaw(偏航角),分别对应绕x轴、y轴、z轴

<?xml version="1.0" ?>
<robot name="jubot">
    
<!--base_car-->>
    <link name="base_link">   
        <visual> 
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/> 
            <geometry>  
                <cylinder radius="0.20" length="0.16"/>             
            </geometry>
            <material name="yellow"> 
                <color rgba="1 0.4 0 1"/>  
            </material>
        </visual>
    </link>

<!--left_wheel-->>
    <joint name="left_wheel_joint" type="continuous">   
        <origin xyz="0.0 0.19 -0.05" rpy="0.0 0.0 0.0"/> 
        <parent link="base_link"/>  
        <child link="left_wheel_link"/> 
        <axis xyz="0.0 1.0 0.0"/>  
    </joint>

    <link name="left_wheel_link">
        <visual>
            <origin xyz="0.0 0.0 0.0" rpy="1.5707 0.0 0.0"/>
            <geometry>
                <cylinder radius="0.06" length="0.025"/>
            </geometry>
            <material name="white">
                <color rgba="1 1 1 0.9"/>
            </material>
        </visual>
    </link>
 
<!--right_wheel-->>
    <joint name="right_wheel_joint" type="continuous">
        <origin xyz="0.0 -0.19 -0.05"/>
        <parent link="base_link"/>
        <child link="right_wheel_link"/>
        <axis xyz="0.0 1.0 0.0"/>
    </joint>

    <link name="right_wheel_link">
        <visual>
            <origin xyz="0.0 0.0 0.0" rpy="1.5707 0.0 0.0"/>
            <geometry>
                <cylinder radius="0.06" length="0.025"/>
            </geometry>
            <material name="white">
                <color rgba="1 1.0 1.0 0.9"/>
            </material>
        </visual>
    </link>

<!--front_caster-->
    <joint name="front_caster_joint" type="continuous">
        <origin xyz="0.18 0.0 -0.095" rpy="0.0 0.0 0.0"/>
        <parent link="base_link"/>
        <child link="front_caster_link"/>
        <axis xyz="0.0 1.0 0.0"/>
    </joint>

    <link name="front_caster_link">
        <visual>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
                <geometry>
                    <sphere radius="0.015"/>
                </geometry>
                <material name="black">
                    <color rgba="0.0 0.0 0.0 0.95"/>
                </material>
        </visual>
    </link>

<!--back_caster-->
    <joint name="back_caster_joint" type="continuous">
        <origin xyz="-0.18 0.0 -0.095" rpy="0.0 0.0 0.0"/>
        <parent link="base_link"/>
        <child link="back_caster_link"/>
        <axis xyz="0.0 1.0 0.0"/>
    </joint>

    <link name="back_caster_link">
        <visual>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
                <geometry>
                    <sphere radius="0.015"/>
                </geometry>
            <material name="black">
                <color rgba="0.0 0.0 0.0 0.95"/>
            </material>
        </visual>
    </link>
</robot>

2. launch文件

<launch>
	<!-- 设置机器人模型路径参数 -->
	<param name="robot_description" textfile="$(find jubot_demo)/urdf/jubot_base.urdf" />

	<!-- 运行joint_state_publisher节点,发布机器人的关节状态  -->
	<node name="joint_state_publisher_gui" pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" />
	
	<!-- 运行robot_state_publisher节点,发布tf  -->
	<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />
	
	<!-- 运行rviz可视化界面 -->
    <node name="rviz" pkg="rviz" type="rviz" args="-d $(find jubot_demo)/config/jubot_urdf.rviz" required="true" />  
</launch>

3. 图形化显示

安装一个检查urdf语法的工具:

sudo apt-get install liburdfdom-tools

在urdf文件夹下打开终端检查语法:

check_urdf jubot_base.urdf

在这里插入图片描述
在urdf文件夹下打开终端,图形化显示URDF模型

urdf_to_graphiz jubot_base.urdf

会生成两个文件
在这里插入图片描述
jubot.pdf

在这里插入图片描述
启动launch文件

roslaunch jubot_demo display_jubot_base_urdf.launch
rviz

在这里插入图片描述

二、gazebo绘制地图

创建工作空间

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

打开gazebo

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

在这里插入图片描述
点击左上角Edit,再点击倒数第二个,进入编辑页面。
在这里插入图片描述
点击Wall,建模墙壁
在这里插入图片描述
点击 file 的 save as 保存模型文件,地图绘制好后,保存为 .world 地图文件(文件后缀名一定要是 .world),将 .world 文件复制到 ~/catkin_ws/src/world 文件夹内,地图环境就搭建好了
在这里插入图片描述

1. xacro小车模型文件

小车模型文件分为两部分,分别为 myrot.xacro 和 myrot.gazebo.xacro ,具体内容如下:
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>

将小车模型文件 myrot.xacro 和 myrot.gazebo.xacro 放到 urdf/文件夹下

2. ROS 运行环境和小车模型

编写 .launch 文件,放到 ~/catkin/src/tutorials/launch 文件夹
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>

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="nan@todo.todo">nanorobot</maintainer>

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

放在 ~/catkin_ws/src/tutorials 文件夹下 在 ~/catkin_ws 文件夹下 catkin 编译

catkin build

如果这个不行,换用

catkin_make

不要忘了 source 一下,否则会报错

source devel/setup.bash

运行 launch 文件

roslaunch tutorials simulation_robot.launch 

这个时候打开是这样,其实点一下暂停会发现这个没有地面,所以模型掉下去了。
在这里插入图片描述
百度了一下参考这篇文章下载了缺少的模型包,重新学习了。
链接: ROS gazebo仿真 物体直接掉落 缺少ground_plane

  • 16
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些指导。 首先,您需要确保已经安装了ROS Melodic和RVIZ。如果您还没有安装,请按照ROS官方文档进行安装。 接下来,您需要创建一个ROS包,并在其创建一个小车模型。您可以使用URDF格式来描述模型,也可以使用SolidWorks等工具创建模型并将其转换为URDF格式。这里我们假设您已经有了一个小车模型,保存在`/path/to/your/urdf/file`。 接下来,您需要启动ROS核心和RVIZ。打开一个终端窗口并输入以下命令: ``` roscore ``` 在另一个终端窗口输入以下命令来启动RVIZ: ``` rviz ``` 在RVIZ,您需要添加一个URDF模型。点击左侧的“Add”按钮,然后选择“RobotModel”。在“Fixed Frame”下选择“base_link”。接下来,您需要将小车的URDF文件加载到RVIZ。点击“File”菜单,选择“Open Config File”,然后选择包含小车URDF文件路径的配置文件。这将加载小车模型到RVIZ。 现在,您需要为小车添加一个控制器,以便通过RVIZ控制小车运动。打开一个新的终端窗口,并输入以下命令: ``` roslaunch robot_control robot_control.launch ``` 其,`robot_control.launch`是您需要创建一个launch文件,用于启动控制器节点。该节点将订阅RVIZ发送的控制指令,并将指令转换为小车的速度和转向角度。 接下来,在RVIZ添加一个“InteractiveMarkers”插件,用于在RVIZ生成控制器。点击左侧的“Add”按钮,选择“InteractiveMarkers”,然后在“Topic”下输入您在控制器节点定义的控制话题名称。 现在,您可以通过在RVIZ移动控制器来控制小车运动了。控制器的移动将发送速度和转向指令到控制器节点,该节点将相应地控制小车运动。 希望这能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值