使用 launch 启动 rviz2 并加载机器人模型
·
视频资料:《ROS 2机器人开发从入门到实践》6.2.2 在RViz中显示机器人_哔哩哔哩_bilibili
1、创建工作空间 chapt6_ws/src,创建包 fishrobot_description
ros2 create fishrobot_description --build-type ament_cmake --license Apache-2.0
2、创建机器人描述文件,chapt6_ws/src/fishbot_description/urdf/first_robot.urdf
<?xml version="1.0"?>
<robot name="first_robot">
<!-- 机器人的身体部分-->
<link name="base_link">
<!-- 部件的外观描述-->
<visual>
<!-- 沿着自己几何中心的偏移旋转量-->
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
<!-- 几何形状-->
<geometry>
<!-- 圆柱体 半径:0.10,高度:0.12-->
<cylinder radius="0.10" length="0.12"/>
</geometry>
<!-- 材质颜色-->
<material name="white">
<color rgba="1.0 1.0 1.0 0.5"/>
</material>
</visual>
</link>
<!-- 机器人的IMU部件,惯性测量传感器-->
<link name="imu_link">
<!-- 部件的外观描述-->
<visual>
<!-- 沿着自己几何中心的偏移旋转量-->
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
<!-- 几何形状-->
<geometry>
<!-- 正方体 长宽高为0.02-->
<box size="0.02 0.02 0.02"/>
</geometry>
<!-- 材质颜色-->
<material name="black">
<color rgba="0.0 0.0 0.0 0.5"/>
</material>
</visual>
</link>
<!-- 机器人的关节,用于组合机器人的部件-->
<joint name="imu_joint" type="fixed">
<parent link="base_link"/>
<child link="imu_link"/>
<origin xyz="0.0 0.0 0.03" rpy="0.0 0.0 0.0"/>
</joint>
</robot>
3、执行命令生成 first_robot.gv 和 first_robot.pdf
urdf_to_graphviz first_robot.urdf
4、创建 chapt6_ws/src/fishbot_description/launch/display_robot.launch.py
注意.rviz文件可以通过第一次加载机器人后保存得到。
joint_state_publisher 节点:发布 /joint_states 话题,内容是当前所有关节的位置(角度/长度),真实机器人一般不需要它,因为真实传感器或驱动会发布 /joint_states
robot_state_publisher 节点:接收 /joint_states 话题,输出每个link的tf/tf_static消息(坐标系关系),RViz根据这些信息,动态显示机器人的三维模型运动
import launch
import launch_ros
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
# 获取默认 urdf 路径
urdf_package_path = get_package_share_directory('fishbot_description')
default_urdf_path = os.path.join(urdf_package_path, 'urdf', 'first_robot.urdf')
default_rviz_config_path = os.path.join(urdf_package_path, 'config', 'display_robot_model.rviz')
# 声明参数,用于修改 urdf 目录
action_declare_arg_mode_path = launch.actions.DeclareLaunchArgument(
name = 'model',
default_value = str(default_urdf_path),
description = '加载的模型文件路径'
)
# 通过文件路径获取内容,并转换为参数对象
substitution_command_result = launch.substitutions.Command(['cat ', launch.substitutions.LaunchConfiguration('model')])
robot_description_value = launch_ros.parameter_descriptions.ParameterValue(substitution_command_result, value_type = str)
action_robot_state_publisher = launch_ros.actions.Node(
package = 'robot_state_publisher',
executable = 'robot_state_publisher',
parameters = [{'robot_description':robot_description_value}]
)
action_joint_state_publisher = launch_ros.actions.Node(
package = 'joint_state_publisher',
executable = 'joint_state_publisher'
)
action_rviz_node = launch_ros.actions.Node(
package = 'rviz2',
executable = 'rviz2',
arguments = ['-d', default_rviz_config_path]
)
return launch.LaunchDescription([
action_declare_arg_mode_path,
action_robot_state_publisher,
action_joint_state_publisher,
action_rviz_node
])
5、修改 chapt6_ws/src/fishbot_description/CMakeLists
cmake_minimum_required(VERSION 3.8)
project(fishbot_description)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
find_package(ament_cmake REQUIRED)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
set(ament_cmake_copyright_FOUND TRUE)
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
# 添加这行
install(DIRECTORY launch urdf config
DESTINATION share/${PROJECT_NAME})
ament_package()
6、
colcon build
source install/setup.bash
ros2 launch fishbot_description display_robot.launch.py
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)