ros2 humble的 urdf模型及在rviz中显示的第一个入门例子
1、RViz2显示URDF模型,必要的节点及通信

|
节点
RViz2的一个节点/transform_listener_impl_* ,RViz2根据 |
2、安装节点
joint-state-publisher和节点robot-state-publishersudo apt install ros-$ROS_DISTRO-joint-state-publisher-gui ros-$ROS_DISTRO-robot-state-publisher
3、创建WS和包
| 命令 |
mkdir -p ros2-urdf-tutorial-ws/src cd ./ros2-urdf-tutorial-ws/src ros2 pkg create --build-type ament_cmake --license Apache-2.0 urdf_tutorial cd ./urdf_tutorial mkdir -p urdf mkdir -p launch mkdir -p rviz cd ..
cd ..
|
包urdf_tutorial内完整的文件结构
| └── urdf_tutorial ├── CMakeLists.txt ├── images │ ├── flexible.png │ └── visual.png ├── include │ └── urdf_tutorial ├── launch │ └── display.launch.py ├── LICENSE ├── meshes │ ├── l_finger_color.png │ └── l_finger_tip.dae ├── package.xml ├── rviz │ └── urdf.rviz ├── src └── urdf ├── 01-myfirst.urdf ├── 02-multipleshapes.urdf ├── 03-origins.urdf ├── 04-materials.urdf ├── 05-visual.urdf ├── 06-flexible.urdf ├── 07-physics.urdf ├── 08-macroed.urdf.xacro └── 09-two-dof.urdf |
4、编写CMakeLists.txt
cmake_minimum_required(VERSION 3.8) project(urdf_tutorial) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) # uncomment the following section in order to fill in # further dependencies manually. # find_package(<dependency> REQUIRED) install( DIRECTORY images launch meshes rviz urdf DESTINATION share/${PROJECT_NAME} ) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # the following line skips the linter which checks for copyrights # comment the line when a copyright and license is added to all source files set(ament_cmake_copyright_FOUND TRUE) # the following line skips cpplint (only works in a git repo) # comment the line when this package is in a git repo and when # a copyright and license is added to all source files set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() ament_package()
5、编写display.launch.py
from launch import LaunchDescription from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription from launch.substitutions import LaunchConfiguration, PathJoinSubstitution from launch_ros.substitutions import FindPackageShare def generate_launch_description(): ld = LaunchDescription() urdf_tutorial_path = FindPackageShare('urdf_tutorial') default_model_path = PathJoinSubstitution(['urdf', '01-myfirst.urdf']) default_rviz_config_path = PathJoinSubstitution([urdf_tutorial_path, 'rviz', 'urdf.rviz']) # These parameters are maintained for backwards compatibility gui_arg = DeclareLaunchArgument(name='gui', default_value='true', choices=['true', 'false'], description='Flag to enable joint_state_publisher_gui') ld.add_action(gui_arg) rviz_arg = DeclareLaunchArgument(name='rvizconfig', default_value=default_rviz_config_path, description='Absolute path to rviz config file') ld.add_action(rviz_arg) # This parameter has changed its meaning slightly from previous versions ld.add_action(DeclareLaunchArgument(name='model', default_value=default_model_path, description='Path to robot urdf file relative to urdf_tutorial package')) ld.add_action(IncludeLaunchDescription( PathJoinSubstitution([FindPackageShare('urdf_launch'), 'launch', 'display.launch.py']), launch_arguments={ 'urdf_package': 'urdf_tutorial', 'urdf_package_path': LaunchConfiguration('model'), 'rviz_config': LaunchConfiguration('rvizconfig'), 'jsp_gui': LaunchConfiguration('gui')}.items() )) return ld
6、通过AI工具,生成一个2自由度机械臂纯URDF,命名09-two-dof.urdf

7、补充其他代码
下载 urdf_tutorial package 代码。 https://files.cnblogs.com/files/blogs/760881/ros2-urdf-tutorial-master.zip?t=1787908394&download=true
官网教程网址:https://docs.ros.org/en/humble/ 具体参考下图。

8、运行URDF作仿真
|
source install/setup.bash ros2 launch urdf_tutorial display.launch.py model:=urdf/09-two-dof.urdf |
继续 反复练习

浙公网安备 33010602011771号