cpp
复制代码
import os
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import ExecuteProcess
from ament_index_python.packages import get_package_share_directory
from moveit_configs_utils import MoveItConfigsBuilder
# 生成启动描述函数
def generate_launch_description():
# 配置 MoveIt
moveit_config = (
MoveItConfigsBuilder("dual_arm_panda")
.robot_description(file_path="config/panda.urdf.xacro")
.robot_description_semantic(file_path="config/panda.srdf")
.trajectory_execution(file_path="config/moveit_controllers.yaml")
.planning_pipelines(pipelines=["ompl"])
.to_moveit_configs()
)
# 启动 move_group 节点/动作服务器
move_group_node = Node(
package="moveit_ros_move_group",
executable="move_group",
output="screen",
parameters=[moveit_config.to_dict()],
)
# RViz 配置
rviz_config = os.path.join(
get_package_share_directory("dual_arm_panda_moveit_config"),
"launch/moveit.rviz",
)
rviz_node = Node(
package="rviz2",
executable="rviz2",
name="rviz2",
output="log",
arguments=["-d", rviz_config],
parameters=[
moveit_config.robot_description,
moveit_config.robot_description_semantic,
moveit_config.planning_pipelines,
moveit_config.robot_description_kinematics,
],
)
# 发布 TF
robot_state_publisher = Node(
package="robot_state_publisher",
executable="robot_state_publisher",
name="robot_state_publisher",
output="both",
parameters=[moveit_config.robot_description],
)
# 使用 FakeSystem 作为硬件的 ros2_control
ros2_controllers_path = os.path.join(
get_package_share_directory("dual_arm_panda_moveit_config"),
"config/",
"ros2_controllers.yaml",
)
ros2_control_node = Node(
package="controller_manager",
executable="ros2_control_node",
parameters=[ros2_controllers_path],
remappings=[
("/controller_manager/robot_description", "/robot_description"),
],
output="both",
)
# 加载控制器
load_controllers = []
for controller in [
"joint_state_broadcaster",
"left_arm_controller",
"right_arm_controller",
]:
load_controllers += [
ExecuteProcess(
cmd=["ros2 run controller_manager spawner {}".format(controller)],
shell=True,
output="screen",
)
]
# 返回启动描述
return LaunchDescription(
[
rviz_node,
robot_state_publisher,
move_group_node,
ros2_control_node,
]
+ load_controllers
)