|
| 1 | +# Copyright 2021 Stogl Robotics Consulting UG (haftungsbeschränkt) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from launch import LaunchDescription |
| 16 | +from launch.actions import DeclareLaunchArgument, RegisterEventHandler |
| 17 | +from launch.conditions import IfCondition |
| 18 | +from launch.event_handlers import OnProcessExit |
| 19 | +from launch.substitutions import Command, FindExecutable, LaunchConfiguration, PathJoinSubstitution |
| 20 | + |
| 21 | +from launch_ros.actions import Node |
| 22 | +from launch_ros.substitutions import FindPackageShare |
| 23 | + |
| 24 | + |
| 25 | +def generate_launch_description(): |
| 26 | + # Declare arguments |
| 27 | + declared_arguments = [] |
| 28 | + declared_arguments.append( |
| 29 | + DeclareLaunchArgument( |
| 30 | + "runtime_config_package", |
| 31 | + default_value="ros2_control_demo_example_3", |
| 32 | + description='Package with the controller\'s configuration in "config" folder. \ |
| 33 | + Usually the argument is not set, it enables use of a custom setup.', |
| 34 | + ) |
| 35 | + ) |
| 36 | + declared_arguments.append( |
| 37 | + DeclareLaunchArgument( |
| 38 | + "controllers_file", |
| 39 | + default_value="rrbot_controllers.yaml", |
| 40 | + description="YAML file with the controllers configuration.", |
| 41 | + ) |
| 42 | + ) |
| 43 | + declared_arguments.append( |
| 44 | + DeclareLaunchArgument( |
| 45 | + "description_package", |
| 46 | + default_value="ros2_control_demo_example_3", |
| 47 | + description="Description package with robot URDF/xacro files. Usually the argument \ |
| 48 | + is not set, it enables use of a custom description.", |
| 49 | + ) |
| 50 | + ) |
| 51 | + declared_arguments.append( |
| 52 | + DeclareLaunchArgument( |
| 53 | + "description_file", |
| 54 | + description="URDF/XACRO description file with the robot.", |
| 55 | + ) |
| 56 | + ) |
| 57 | + declared_arguments.append( |
| 58 | + DeclareLaunchArgument( |
| 59 | + "prefix", |
| 60 | + default_value='""', |
| 61 | + description="Prefix of the joint names, useful for \ |
| 62 | + multi-robot setup. If changed than also joint names in the controllers' configuration \ |
| 63 | + have to be updated.", |
| 64 | + ) |
| 65 | + ) |
| 66 | + declared_arguments.append( |
| 67 | + DeclareLaunchArgument( |
| 68 | + "use_mock_hardware", |
| 69 | + default_value="true", |
| 70 | + description="Start robot with mock hardware mirroring command to its states.", |
| 71 | + ) |
| 72 | + ) |
| 73 | + declared_arguments.append( |
| 74 | + DeclareLaunchArgument( |
| 75 | + "mock_sensor_commands", |
| 76 | + default_value="false", |
| 77 | + description="Enable fake command interfaces for sensors used for simple simulations. \ |
| 78 | + Used only if 'use_mock_hardware' parameter is true.", |
| 79 | + ) |
| 80 | + ) |
| 81 | + declared_arguments.append( |
| 82 | + DeclareLaunchArgument( |
| 83 | + "slowdown", default_value="3.0", description="Slowdown factor of the RRbot." |
| 84 | + ) |
| 85 | + ) |
| 86 | + declared_arguments.append( |
| 87 | + DeclareLaunchArgument( |
| 88 | + "robot_controller", |
| 89 | + default_value="forward_position_controller", |
| 90 | + description="Robot controller to start.", |
| 91 | + ) |
| 92 | + ) |
| 93 | + declared_arguments.append( |
| 94 | + DeclareLaunchArgument( |
| 95 | + "start_rviz", |
| 96 | + default_value="true", |
| 97 | + description="Start RViz2 automatically with this launch file.", |
| 98 | + ) |
| 99 | + ) |
| 100 | + |
| 101 | + # Initialize Arguments |
| 102 | + runtime_config_package = LaunchConfiguration("runtime_config_package") |
| 103 | + controllers_file = LaunchConfiguration("controllers_file") |
| 104 | + description_package = LaunchConfiguration("description_package") |
| 105 | + description_file = LaunchConfiguration("description_file") |
| 106 | + prefix = LaunchConfiguration("prefix") |
| 107 | + use_mock_hardware = LaunchConfiguration("use_mock_hardware") |
| 108 | + mock_sensor_commands = LaunchConfiguration("mock_sensor_commands") |
| 109 | + slowdown = LaunchConfiguration("slowdown") |
| 110 | + robot_controller = LaunchConfiguration("robot_controller") |
| 111 | + start_rviz = LaunchConfiguration("start_rviz") |
| 112 | + |
| 113 | + # Get URDF via xacro |
| 114 | + robot_description_content = Command( |
| 115 | + [ |
| 116 | + PathJoinSubstitution([FindExecutable(name="xacro")]), |
| 117 | + " ", |
| 118 | + PathJoinSubstitution( |
| 119 | + [FindPackageShare(description_package), "urdf", description_file] |
| 120 | + ), |
| 121 | + " ", |
| 122 | + "prefix:=", |
| 123 | + prefix, |
| 124 | + " ", |
| 125 | + "use_mock_hardware:=", |
| 126 | + use_mock_hardware, |
| 127 | + " ", |
| 128 | + "mock_sensor_commands:=", |
| 129 | + mock_sensor_commands, |
| 130 | + " ", |
| 131 | + "slowdown:=", |
| 132 | + slowdown, |
| 133 | + ] |
| 134 | + ) |
| 135 | + robot_description = {"robot_description": robot_description_content} |
| 136 | + |
| 137 | + robot_controllers = PathJoinSubstitution( |
| 138 | + [ |
| 139 | + FindPackageShare(runtime_config_package), |
| 140 | + "config", |
| 141 | + controllers_file, |
| 142 | + ] |
| 143 | + ) |
| 144 | + rviz_config_file = PathJoinSubstitution( |
| 145 | + [FindPackageShare(description_package), "rviz", "rrbot.rviz"] |
| 146 | + ) |
| 147 | + |
| 148 | + control_node = Node( |
| 149 | + package="controller_manager", |
| 150 | + executable="ros2_control_node", |
| 151 | + parameters=[robot_description, robot_controllers], |
| 152 | + output="both", |
| 153 | + ) |
| 154 | + robot_state_pub_node = Node( |
| 155 | + package="robot_state_publisher", |
| 156 | + executable="robot_state_publisher", |
| 157 | + output="both", |
| 158 | + parameters=[robot_description], |
| 159 | + ) |
| 160 | + rviz_node = Node( |
| 161 | + package="rviz2", |
| 162 | + executable="rviz2", |
| 163 | + name="rviz2", |
| 164 | + output="log", |
| 165 | + arguments=["-d", rviz_config_file], |
| 166 | + condition=IfCondition(start_rviz), |
| 167 | + ) |
| 168 | + |
| 169 | + joint_state_broadcaster_spawner = Node( |
| 170 | + package="controller_manager", |
| 171 | + executable="spawner", |
| 172 | + arguments=["joint_state_broadcaster", "--controller-manager", "/controller_manager"], |
| 173 | + ) |
| 174 | + |
| 175 | + robot_controller_spawner = Node( |
| 176 | + package="controller_manager", |
| 177 | + executable="spawner", |
| 178 | + arguments=[robot_controller, "--controller-manager", "/controller_manager"], |
| 179 | + ) |
| 180 | + |
| 181 | + # Delay rviz start after `joint_state_broadcaster` |
| 182 | + delay_rviz_after_joint_state_broadcaster_spawner = RegisterEventHandler( |
| 183 | + event_handler=OnProcessExit( |
| 184 | + target_action=joint_state_broadcaster_spawner, |
| 185 | + on_exit=[rviz_node], |
| 186 | + ) |
| 187 | + ) |
| 188 | + |
| 189 | + # Delay start of robot_controller after `joint_state_broadcaster` |
| 190 | + delay_robot_controller_spawner_after_joint_state_broadcaster_spawner = RegisterEventHandler( |
| 191 | + event_handler=OnProcessExit( |
| 192 | + target_action=joint_state_broadcaster_spawner, |
| 193 | + on_exit=[robot_controller_spawner], |
| 194 | + ) |
| 195 | + ) |
| 196 | + |
| 197 | + nodes = [ |
| 198 | + control_node, |
| 199 | + robot_state_pub_node, |
| 200 | + joint_state_broadcaster_spawner, |
| 201 | + delay_rviz_after_joint_state_broadcaster_spawner, |
| 202 | + delay_robot_controller_spawner_after_joint_state_broadcaster_spawner, |
| 203 | + ] |
| 204 | + |
| 205 | + return LaunchDescription(declared_arguments + nodes) |
0 commit comments