moveit2
The MoveIt Motion Planning Framework for ROS 2.
servo_example.launch.py
Go to the documentation of this file.
1 import os
2 import yaml
3 from launch import LaunchDescription
4 from launch_ros.actions import Node
5 from ament_index_python.packages import get_package_share_directory
6 from launch_ros.actions import ComposableNodeContainer
7 from launch_ros.descriptions import ComposableNode
8 from launch.actions import ExecuteProcess
9 import xacro
10 from moveit_configs_utils import MoveItConfigsBuilder
11 
12 
13 def load_file(package_name, file_path):
14  package_path = get_package_share_directory(package_name)
15  absolute_file_path = os.path.join(package_path, file_path)
16 
17  try:
18  with open(absolute_file_path, "r") as file:
19  return file.read()
20  except EnvironmentError: # parent of IOError, OSError *and* WindowsError where available
21  return None
22 
23 
24 def load_yaml(package_name, file_path):
25  package_path = get_package_share_directory(package_name)
26  absolute_file_path = os.path.join(package_path, file_path)
27 
28  try:
29  with open(absolute_file_path, "r") as file:
30  return yaml.safe_load(file)
31  except EnvironmentError: # parent of IOError, OSError *and* WindowsError where available
32  return None
33 
34 
36  moveit_config = (
37  MoveItConfigsBuilder("moveit_resources_panda")
38  .robot_description(file_path="config/panda.urdf.xacro")
39  .to_moveit_configs()
40  )
41 
42  # Get parameters for the Servo node
43  servo_yaml = load_yaml("moveit_servo", "config/panda_simulated_config.yaml")
44  servo_params = {"moveit_servo": servo_yaml}
45 
46  # RViz
47  rviz_config_file = (
48  get_package_share_directory("moveit_servo") + "/config/demo_rviz_config.rviz"
49  )
50  rviz_node = Node(
51  package="rviz2",
52  executable="rviz2",
53  name="rviz2",
54  output="log",
55  arguments=["-d", rviz_config_file],
56  parameters=[
57  moveit_config.robot_description,
58  moveit_config.robot_description_semantic,
59  ],
60  )
61 
62  # ros2_control using FakeSystem as hardware
63  ros2_controllers_path = os.path.join(
64  get_package_share_directory("moveit_resources_panda_moveit_config"),
65  "config",
66  "ros2_controllers.yaml",
67  )
68  ros2_control_node = Node(
69  package="controller_manager",
70  executable="ros2_control_node",
71  parameters=[moveit_config.robot_description, ros2_controllers_path],
72  output="screen",
73  )
74 
75  joint_state_broadcaster_spawner = Node(
76  package="controller_manager",
77  executable="spawner",
78  arguments=[
79  "joint_state_broadcaster",
80  "--controller-manager-timeout",
81  "300",
82  "--controller-manager",
83  "/controller_manager",
84  ],
85  )
86 
87  panda_arm_controller_spawner = Node(
88  package="controller_manager",
89  executable="spawner",
90  arguments=["panda_arm_controller", "-c", "/controller_manager"],
91  )
92 
93  # Launch as much as possible in components
94  container = ComposableNodeContainer(
95  name="moveit_servo_demo_container",
96  namespace="/",
97  package="rclcpp_components",
98  executable="component_container_mt",
99  composable_node_descriptions=[
100  # Example of launching Servo as a node component
101  # Assuming ROS2 intraprocess communications works well, this is a more efficient way.
102  # ComposableNode(
103  # package="moveit_servo",
104  # plugin="moveit_servo::ServoServer",
105  # name="servo_server",
106  # parameters=[
107  # servo_params,
108  # moveit_config.robot_description,
109  # moveit_config.robot_description_semantic,
110  # ],
111  # ),
112  ComposableNode(
113  package="robot_state_publisher",
114  plugin="robot_state_publisher::RobotStatePublisher",
115  name="robot_state_publisher",
116  parameters=[moveit_config.robot_description],
117  ),
118  ComposableNode(
119  package="tf2_ros",
120  plugin="tf2_ros::StaticTransformBroadcasterNode",
121  name="static_tf2_broadcaster",
122  parameters=[{"child_frame_id": "/panda_link0", "frame_id": "/world"}],
123  ),
124  ComposableNode(
125  package="moveit_servo",
126  plugin="moveit_servo::JoyToServoPub",
127  name="controller_to_servo_node",
128  ),
129  ComposableNode(
130  package="joy",
131  plugin="joy::Joy",
132  name="joy_node",
133  ),
134  ],
135  output="screen",
136  )
137  # Launch a standalone Servo node.
138  # As opposed to a node component, this may be necessary (for example) if Servo is running on a different PC
139  servo_node = Node(
140  package="moveit_servo",
141  executable="servo_node_main",
142  parameters=[
143  servo_params,
144  moveit_config.robot_description,
145  moveit_config.robot_description_semantic,
146  moveit_config.robot_description_kinematics,
147  ],
148  output="screen",
149  )
150 
151  return LaunchDescription(
152  [
153  rviz_node,
154  ros2_control_node,
155  joint_state_broadcaster_spawner,
156  panda_arm_controller_spawner,
157  servo_node,
158  container,
159  ]
160  )
def generate_launch_description()
def load_file(package_name, file_path)
def load_yaml(package_name, file_path)