ROS2机器人开发:从入门到实践 +第六章 建模仿真

mkdir -p chapt6_ws/src
cd chapt6_ws/src
ros2 pkg create fishbot_description --build-type ament_cmake --license Apache-2.0
cd ..
colcon build 

在VSCode中,file菜单添加chapt6_ws,在fishbot_description下创建文件夹urdf。添加一个urdf源代码 first_robot.urdf。
~/chapt6_ws$ cd src/fishbot_description/urdf
urdf_to_graphviz first_robot.urdf
在RViz中显示机器人几何模型
rviz2
在RViz中Displays中Add按钮添加RobotModel,将Description Source修改为file选项,将Description file选first_robot.urdf。Fixed Frame输入模型文件中的连杆base_link。实现3D可视化。

image

 

image

 

 

文件树:

ubuntu@ubuntu-Lenovo-XiaoXin-14API-2019:~$ tree ~/chapt6_ws -L 4
/home/ubuntu/chapt6_ws
├── build
│   ├── 省略了
├── install
│   ├──省略了
├── log
│   ├── 省略了
└── src
    └── fishbot_description
        ├── CMakeLists.txt
        ├── include
        │   └── fishbot_description
        ├── LICENSE
        ├── package.xml
        ├── src
        └── urdf
            ├── first_robot.gv
            ├── first_robot.pdf
            └── first_robot.urdf

模型文件  first_robot.urdf

来源https://gitee.com/ohhuo/ros2bookcode

<?xml version="1.0"?>
<robot name="first_robot">
    <!-- 机器人身体部分 -->
    <link name="base_link">
        <!-- 部件外观描述 -->
        <visual>
            <!-- 沿自己几何中心的偏移与旋转量 -->
            <origin xyz="0 0 0" rpy="0 0 0" />
            <!-- 几何形状 -->
            <geometry>
                <!-- 圆柱体,半径0.1m,高度 0.12m -->
                <cylinder length="0.12" radius="0.10" />
            </geometry>
            <!-- 材质子标签-蓝色 -->
            <material name="blue">
                <color rgba="0.1 0.1 1.0 0.5" />
            </material>
        </visual>
    </link>

    <!-- 机器人IMU部件 -->
    <link name="imu_link">
        <visual>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <geometry>
                <box size="0.02 0.02 0.02" />
            </geometry>
        </visual>
        <material name="black">
            <color rgba="0 0 0 0.5" />
        </material>
    </link>

    <!-- 机器人关节 -->
    <joint name="imu_joint" type="fixed">
        <!-- 父部件 -->
        <parent link="base_link" />
        <!-- 子部件 -->
        <child link="imu_link" />
        <!-- 子部件相对父部件的平移和旋转 -->
        <origin xyz="0 0 0.03" rpy="0 0 0" />
    </joint>

</robot>

 

posted @ 2026-03-25 22:00  辛河  阅读(95)  评论(0)    收藏  举报