文件目录结构
CmakeLists
cmake_minimum_required(VERSION 3.8)
project(cpp06_urdf)
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)
find_package(rclcpp REQUIRED)
install(DIRECTORY launch rviz urdf meshes
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()
package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>cpp06_urdf</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="slh@todo.todo">slh</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<exec_depend>ros2launch</exec_depend>
<exec_depend>xacro</exec_depend>
<exec_depend>robot_state_publisher</exec_depend>
<exec_depend>joint_state_publisher</exec_depend>
<exec_depend>rviz2</exec_depend>
<depend>rclcpp</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
xxx.urdf
<robot name="helloworld">
<link name="base_link">
<visual>
<geometry>
<box size="1.0 0.5 0.1"/>
</geometry>
</visual>
</link>
</robot>
urdf文件存放在 功能包/urdf/urdf 下
xxx.launch.py
import os
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from launch_ros.parameter_descriptions import ParameterValue
from launch.substitutions import Command,LaunchConfiguration
from launch.actions import DeclareLaunchArgument
launch_ros.parameter_descriptions import ParameterValue
from launch import LaunchDescription
from launch_ros.actions import Node
import os
from ament_index_python.packages import get_package_share_directory
from launch_ros.parameter_descriptions import ParameterValue
from launch.substitutions import Command,LaunchConfiguration
from launch.actions import DeclareLaunchArgument
#示例:ros2 launch cpp06_urdf display.launch.py model:=`ros2 pkg prefix --share cpp06_urdf`/urdf/urdf/urdf文件名及后缀
def generate_launch_description():
cpp06_urdf_dir = get_package_share_directory("这里替换你打功能包名称")
default_model_path = os.path.join(功能包名称_dir,"urdf/urdf","urdf文件名和后缀")
default_rviz_path = os.path.join(功能包名称_dir,"rviz","display.rviz")
model = DeclareLaunchArgument(name="model", default_value=default_model_path)
# 加载机器人模型
# 1.启动 robot_state_publisher 节点并以参数方式加载 urdf 文件
robot_description = ParameterValue(Command(["xacro ",LaunchConfiguration("model")]))
robot_state_publisher = Node(
package="robot_state_publisher",
executable="robot_state_publisher",
parameters=[{"robot_description": robot_description}]
)
# 2.启动 joint_state_publisher 节点发布非固定关节状态
joint_state_publisher = Node(
package="joint_state_publisher_gui",
executable="joint_state_publisher_gui"
)
# rviz2 节点
rviz2 = Node(
package="rviz2",
executable="rviz2"
# arguments=["-d", default_rviz_path]
)
return LaunchDescription([
model,
robot_state_publisher,
joint_state_publisher,
rviz2
])