ROS 基础教程

欢迎访问我的博客首页


1.urdf 文件


  假设我们的工作空间是 ws_ros。我们自己实现的包将会放在 ws_ros/src/toturial。首先创建一个 ROS 功能包 urdf_pkg:

# 在 ws_ros/src/toturial 下执行:
catkin_create_pkg urdf_pkg urdf xacro gazebo_ros gazebo_ros_control gazebo_plugins

该功能包有 5 个依赖 urdf xacro gazebo_ros gazebo_ros_control gazebo_plugins:urdf 和 xacro 让我们能使用 urdf 和 xacro 文件定义机器人模型;后面三个依赖让我们能使用 Gazebo 仿真。

1.1 在 Rviz 中显示 urdf


  使用 urdf 文件定义一个仅有两个坐标系的机器人模型,并在 Rviz 中显示。

1.1.1 定义 urdf


  创建文件 ws_ros/src/toturial/urdf_pkg/urdf/urdf1.urdf。

<!--urdf1-->
<robot name="urdf1">
    <!-- 坐标系 1:长方体-->
    <link name="base_link">
        <visual>
            <geometry>
                <box size="0.5 0.2 0.1" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0.785" />
            <material name="green">
                <color rgba="0 1 0 0.5" />
            </material>
        </visual>
    </link>
    <!-- 坐标系 2:圆柱体-->
    <link name="laser_link">
        <visual>
            <geometry>
                <cylinder radius="0.05" length="0.01" />
            </geometry>
            <origin xyz="0 0 0.05" rpy="0 0 0" />
            <material name="blue">
                <color rgba="0 0 1 1" />
            </material>
        </visual>
    </link>
    <!-- 变换 1:坐标系 1 到坐标系 2-->
    <joint name="laser_link_to_base_link" type="fixed">
        <parent link="base_link" />
        <child link="laser_link" />
        <origin xyz="0 0 0" />
        <axis xyz="0 1 0" />
    </joint>
</robot>

  该模型有两个坐标系组成。第一个坐标系是名为 base_link 的长方体:标签 geometry 定义了尺寸;标签 origin 定义了几何中心的坐标和欧拉角 Roll-Pitch-Yaw,0.785 为 π / 4 \pi/4 π/4;标签 color 定义了颜色。

  当 urdf 文件中定义的坐标系超过 1 个时,必须定义坐标系间的变换:定义了 n 个坐标系,需要定义 n-1 个变换。

1.1.2 在 Rviz 中查看 urdf


  创建文件 ws_ros/src/toturial/urdf_pkg/launch/urdf1.launch。

<!--urdf1-->
<launch>
    <!-- 1.结点:Rviz 可视化-->
    <node pkg="rviz" type="rviz" name="rviz" args="-d $(find urdf_pkg)/config/urdf1.rviz" />

    <!-- 2.结点:发布机器人坐标变换-->
    <param name="robot_description" textfile="$(find urdf_pkg)/urdf/urdf1.urdf" />
    <node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" output="screen" />
</launch>

  该文件启动两个结点。rviz 结点用于可视化,urdf1.rviz 是 Rviz 的配置文件,需要我们启动后配置并保存,下次再启动时才能使用;robot_state_publisher 结点发布坐标系变换,必需为其指定 robot_description 参数。

  Rviz 启动时,默认只有 Global Options、Global Status 和 Gird 三项。Global Options/Fixed Frame 默认坐标系是 map,而我们的 urdf1.urdf 中只定义了坐标系 base_link 和 laser_link,所以会报错 Global Status/Fixed Frame/Unknown frame map。

  解决方法是在 Global Options/Fixed Frame 的下拉选框中选择 base_link 或 laser_link。因为我们的 urdf1.urdf 中只定义了坐标系 base_link 和 laser_link,所以下拉选框只有这两个。然后点击 Add,添加 RobotModel 就可以在 Rviz 中看到我们定义的机器人了。点击 File/Save Config As,保存 ws_ros/src/toturial/urdf_pkg/config/urdf1.rviz,下次就无需配置 Rviz 了。

请添加图片描述

图 1.1 在 Rviz 中显示 urdf

1.2 在 Gazebo 中显示 urdf


  在 urdf1.urdf 的基础上定义 urdf2.urdf,并在 Gazebo 中显示。

1.2.1 定义 urdf


  创建文件 ws_ros/src/toturial/urdf_pkg/urdf/urdf2.urdf。

<!--urdf2-->
<robot name="urdf2">
    <!-- 预定义的颜色-->
    <material name="green">
        <color rgba="0 1 0 0.5" />
    </material>
    <material name="blue">
        <color rgba="0 0 1 1" />
    </material>

    <!-- 坐标系 1:长方体-->
    <link name="base_link">
        <!--外观-->
        <visual>
            <geometry>
                <box size="0.5 0.2 0.1" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0.785" />
            <material name="green" />
        </visual>
        <!--碰撞-->
        <collision>
            <geometry>
                <box size="0.5 0.2 0.1" />
            </geometry>
        </collision>
        <!--惯性-->
        <inertial>
            <mass value="6" />
            <inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1" />
        </inertial>
    </link>
    <!-- 坐标系 2:圆柱体-->
    <link name="laser_link">
        <visual>
            <geometry>
                <cylinder radius="0.05" length="0.01" />
            </geometry>
            <origin xyz="0 0 0.05" rpy="0 0 0" />
            <material name="blue" />
        </visual>
    </link>

    <!-- 变换 1:坐标系 1 到坐标系 2-->
    <joint name="laser_link_to_base_link" type="fixed">
        <parent link="base_link" />
        <child link="laser_link" />
        <origin xyz="0 0 0" rpy="0 0 0" />
        <axis xyz="0 1 0" />
    </joint>

    <!-- 在Gazebo中的属性:颜色-->
    <gazebo reference="base_link">
        <material>Gazebo/Green</material>
    </gazebo>
    <gazebo reference="laser_link">
        <material>Gazebo/Blue</material>
    </gazebo>
</robot>

  和 urdf1 一样,该模型有两个坐标系组成。为了能在 Gazebo 中显示机器人,必须有一个坐标系定义了碰撞标签 collision 和惯性标签 inertial。此外,为了在 Gazebo 中显示颜色,必须在标签 gazebo 下使用标签 material 指定。

1.2.2 在 Gazebo 中查看 urdf


  创建文件 ws_ros/src/toturial/urdf_pkg/launch/urdf2.launch。

<!--urdf2-->
<launch>
    <!-- 1.启动仿真环境-->
    <include file="$(find gazebo_ros)/launch/empty_world.launch" />

    <!-- 2.显示机器人模型-->
    <param name="robot_description" textfile="$(find urdf_pkg)/urdf/urdf2.urdf" />
    <node pkg="gazebo_ros" type="spawn_model" name="robot_urdf_tutorial" args="-urdf -model robot_urdf_tutorial -param robot_description" />
</launch>

  该 launch 文件使用官方 ROS 包 gazebo_ros,这是安装 ROS 是自动安装的。使用 empty_world.launch 显示仿真环境,使用 spawn_model 显示我们的 urdf 文件。与 Rviz 不同的是,Gazebo 不需配置文件。

在这里插入图片描述

图 1.2 在 Gazebo 中显示 urdf

2.建图-仿真


  在仿真环境下,使用 gmapping 建图。

2.1 模型


  由于要使用带有差速轮的机器人模型,所以我们直接使用别人定义好的模型文件 ws_ros/src/toturial/navigation_pkg/urdf/agv.urdf。

<!-- agv -->
<robot name="agv">
    <!-- 坐标系:球体-->
    <link name="base_coordinate">
        <visual>
            <geometry>
                <sphere radius="0.001" />
            </geometry>
        </visual>
    </link>

    <link name="base_link">
        <visual>
            <geometry>
                <mesh filename="package://navigation_pkg/meshes/agv.stl"/>
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 1.57" />
            <material name="blue">
                <color rgba="0 0.25 0.5 0.8" />
            </material>
        </visual>
        <collision>
            <geometry>
                <box size="0.34 0.34 0.34" />
            </geometry>
            <origin xyz="-0.8 0.0 0.0" rpy="0.0 0.0 1.57" />
        </collision>
	    <inertial>
            <origin xyz="0 0 0" />
            <mass value="1" />
            <inertia ixx="0.2" ixy="0" ixz="0" iyy="0.2" iyz="0" izz="0.2" />
        </inertial>
    </link>
    <gazebo reference="base_link">
        <material>Gazebo/Blue</material>
    </gazebo>

    <joint name="base_link_to_base_coordinate" type="fixed">
        <parent link="base_coordinate" />
        <child link="base_link"/>
        <origin xyz="0 0 0.205" />
    </joint>



    <link name="right_wheel">
        <visual>
            <geometry>
                <cylinder radius="0.09" length="0.06" />
            </geometry>
            <origin xyz="0 0 0" rpy="1.57 0 0" />
            <material name="black">
                <color rgba="0.0 0.0 0.0 1.0" />
            </material>
        </visual>
        <collision>
            <geometry>
                <cylinder radius="0.09" length="0.06" />
            </geometry>
            <origin xyz="0 0 0" rpy="1.57 0 0" />
        </collision>
	    <inertial>
            <origin xyz="0 0 0" />
            <mass value="2" />
            <inertia ixx="0.02" ixy="0" ixz="0" iyy="0.02" iyz="0" izz="0.04" />
        </inertial>
    </link>
    <gazebo reference="right_wheel">
        <material>Gazebo/Black</material>
    </gazebo>

    <joint name="base_r_wheel_joint" type="continuous">
        <parent link="base_link" />
        <child link="right_wheel" />
        <origin xyz="0.13 -0.1575 -0.115" />
        <axis xyz="0 1 0" />
    </joint>


    <link name="left_wheel">
        <visual>
            <geometry>
                <cylinder radius="0.09" length="0.06" />
            </geometry>
            <origin xyz="0 0 0" rpy="1.57 0 0" />
            <material name="black">
                <color rgba="0.0 0.0 0.0 1.0" />
            </material>
        </visual>
        <collision>
            <geometry>
                <cylinder radius="0.09" length="0.06" />
            </geometry>
            <origin xyz="0 0 0" rpy="1.57 0 0" />
        </collision>
	    <inertial>
            <origin xyz="0 0 0" />
            <mass value="2" />
            <inertia ixx="0.02" ixy="0" ixz="0" iyy="0.02" iyz="0" izz="0.04" />
        </inertial>
    </link>
    <gazebo reference="left_wheel">
        <material>Gazebo/Black</material>
    </gazebo>

    <joint name="base_l_wheel_joint" type="continuous">
        <parent link="base_link" />
        <child link="left_wheel" />
        <origin xyz="0.13 0.1575 -0.115" />
        <axis xyz="0 1 0" />
    </joint>



    <link name="universal_wheel">
        <visual>
            <geometry>
                <sphere radius="0.035" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <material name="black">
                <color rgba="0.0 0.0 0.0 1.0" />
            </material>
        </visual>
        <collision>
            <geometry>
                <sphere radius="0.035" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
        </collision>
	    <inertial>
            <origin xyz="0 0 0" />
            <mass value="0.5" />
            <inertia ixx="0.0002" ixy="0" ixz="0" iyy="0.0002" iyz="0" izz="0.0002" />
        </inertial>
    </link>
    <gazebo reference="universal_wheel">
        <material>Gazebo/White</material>
    </gazebo>

    <joint name="universal_wheel_to_base_link" type="continuous">
        <parent link="base_link" />
        <child link="universal_wheel" />
        <origin xyz="-0.135 0 -0.17" />
        <axis xyz="1 1 1" />
    </joint>

    <link name="laser">
        <visual>
            <geometry>
                <box size="0.05 0.05 0.05" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <material name="red">
                <color rgba="1.0 0.0 0.0 1.0" />
            </material>
        </visual>
        <collision>
            <geometry>
                <box size="0.05 0.05 0.05" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
        </collision>
	    <inertial>
            <origin xyz="0 0 0" />
            <mass value="0.2" />
            <inertia ixx="0.00008" ixy="0" ixz="0" iyy="0.00008" iyz="0" izz="0.00008" />
        </inertial>
    </link>
    <gazebo reference="laser">
        <material>Gazebo/Red</material>
    </gazebo>

    <joint name="laser_to_base_link" type="fixed">
        <parent link="base_link" />
        <child link="laser" />
        <origin xyz="0.12 0 0.195" />
    </joint>


    <link name="camera">
        <visual>
            <geometry>
                <cylinder radius="0.02" length="0.01" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 1.57 0" />
            <material name="yellow">
                <color rgba="1.0 1.0 0.0 1.0" />
            </material>
        </visual>
        <collision>
            <geometry>
                <cylinder radius="0.02" length="0.01" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 1.57 0" />
        </collision>
	    <inertial>
            <origin xyz="0 0 0" />
            <mass value="0.1" />
            <inertia ixx="0.00001" ixy="0" ixz="0" iyy="0.00001" iyz="0" izz="0.00002" />
        </inertial>
    </link>
    <gazebo reference="camera">
        <material>Gazebo/Yellow</material>
    </gazebo>

    <joint name="camera_to_base_link" type="fixed">
        <parent link="base_link" />
        <child link="camera" />
        <origin xyz="0.23 0 0.145" />
    </joint>


    <gazebo>
        <plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so">
            <rosDebugLevel>Debug</rosDebugLevel>
            <publishWheelTF>true</publishWheelTF>
            <robotNamespace>/</robotNamespace>
            <publishTf>1</publishTf>
            <publishWheelJointState>true</publishWheelJointState>
            <alwaysOn>true</alwaysOn>
            <updateRate>100.0</updateRate>
            <legacyMode>true</legacyMode>
            <leftJoint>base_l_wheel_joint</leftJoint> 
            <rightJoint>base_r_wheel_joint</rightJoint>
            <wheelSeparation>0.315</wheelSeparation> 
            <wheelDiameter>0.18</wheelDiameter>
            <broadcastTF>1</broadcastTF>
            <wheelTorque>30</wheelTorque>
            <wheelAcceleration>1.8</wheelAcceleration>
            <commandTopic>cmd_vel</commandTopic>
            <odometryFrame>odom</odometryFrame> 
            <odometryTopic>odom</odometryTopic>
            <robotBaseFrame>base_coordinate</robotBaseFrame>
        </plugin>
    </gazebo>




    <gazebo reference="laser">
        <sensor type="ray" name="rplidar">
            <pose>0 0 0 0 0 0</pose>
            <visualize>true</visualize>
            <update_rate>5.5</update_rate>
            <ray>
                <scan>
                    <horizontal>
                        <samples>360</samples>
                        <resolution>1</resolution>
                        <min_angle>-3</min_angle>
                        <max_angle>3</max_angle>
                    </horizontal>
                </scan>
                <range>
                    <min>0.10</min>
                    <max>30.0</max>
                    <resolution>0.01</resolution>
                </range>
                <noise>
                    <type>gaussian</type>
                    <mean>0.0</mean>
                    <stddev>0.01</stddev>
                </noise>
            </ray>
            <plugin name="gazebo_rplidar" filename="libgazebo_ros_laser.so">
                <topicName>/scan</topicName>
                <frameName>laser</frameName>
            </plugin>
        </sensor>
  </gazebo>

</robot>

3.建图-实况


3.参考


1.机器人工匠阿杰,B 站github

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在RVIZ中固定Fixed frame的方法如下: 1. 首先,确保你已经正确加载了TF数据。TF是ROS中用于处理坐标系转换的库,RVIZ使用TF来显示不同坐标系之间的关系。 2. 在RVIZ的顶部工具栏中,找到并点击“Global Options”按钮。 3. 在“Global Options”面板中,找到“Fixed Frame”选项。这个选项允许你选择一个固定的坐标系作为RVIZ中显示的参考坐标系。 4. 如果你看到错误消息“Fixed Frame does not exist”或者“unknown frame”,这意味着RVIZ无法找到你所选择的固定坐标系。 5. 确保你选择的固定坐标系存在于你的TF数据中。你可以通过运行`rosrun tf view_frames`命令来查看当前ROS网络中的TF坐标系树结构。 6. 如果你的固定坐标系不在TF坐标系树中,你需要确保你的TF数据正确发布,并且TF坐标系树正确连接。 7. 如果你的固定坐标系存在于TF坐标系树中,但仍然显示为未知的,请确保你正确设置了TF数据的发布者,并且发布者的坐标系名称与你选择的固定坐标系名称相匹配。 8. 一旦你选择了正确的固定坐标系,并且RVIZ成功找到它,你的Fixed frame就会被固定在RVIZ中,你可以通过其他功能和工具来查看和操作与固定坐标系相关的数据。 希望这些步骤可以帮助你固定Fixed frame!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [RVIZ中的fixed frame选项以及“For frame [XX]: Fixed Frame [map] does not exist”](https://blog.csdn.net/ljh5930/article/details/125043385)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [解决iframe中fixed失效的问题](https://download.csdn.net/download/hejiancsdn/10450089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [在fixed frame栏中出现了 unknown frame map错误](https://blog.csdn.net/pvmsmfchcs/article/details/128793848)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值