|
| 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 | + "prefix", |
| 31 | + default_value='""', |
| 32 | + description="Prefix of the joint names, useful for \ |
| 33 | + multi-robot setup. If changed than also joint names in the controllers' configuration \ |
| 34 | + have to be updated.", |
| 35 | + ) |
| 36 | + ) |
| 37 | + declared_arguments.append( |
| 38 | + DeclareLaunchArgument( |
| 39 | + "slowdown", default_value="50.0", description="Slowdown factor of the RRbot." |
| 40 | + ) |
| 41 | + ) |
| 42 | + declared_arguments.append( |
| 43 | + DeclareLaunchArgument( |
| 44 | + "robot_controller", |
| 45 | + default_value="forward_velocity_controller", |
| 46 | + description="Robot controller to start.", |
| 47 | + ) |
| 48 | + ) |
| 49 | + declared_arguments.append( |
| 50 | + DeclareLaunchArgument( |
| 51 | + "gui", |
| 52 | + default_value="true", |
| 53 | + description="Start RViz2 automatically with this launch file.", |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + # Initialize Arguments |
| 58 | + prefix = LaunchConfiguration("prefix") |
| 59 | + slowdown = LaunchConfiguration("slowdown") |
| 60 | + robot_controller = LaunchConfiguration("robot_controller") |
| 61 | + gui = LaunchConfiguration("gui") |
| 62 | + |
| 63 | + # Get URDF via xacro |
| 64 | + robot_description_content = Command( |
| 65 | + [ |
| 66 | + PathJoinSubstitution([FindExecutable(name="xacro")]), |
| 67 | + " ", |
| 68 | + PathJoinSubstitution( |
| 69 | + [ |
| 70 | + FindPackageShare("ros2_control_demo_example_14"), |
| 71 | + "urdf", |
| 72 | + "rrbot_modular_actuators_without_feedback_sensors_for_position_feedback.urdf.xacro", |
| 73 | + ] |
| 74 | + ), |
| 75 | + " ", |
| 76 | + "prefix:=", |
| 77 | + prefix, |
| 78 | + " ", |
| 79 | + "slowdown:=", |
| 80 | + slowdown, |
| 81 | + ] |
| 82 | + ) |
| 83 | + robot_description = {"robot_description": robot_description_content} |
| 84 | + |
| 85 | + robot_controllers = PathJoinSubstitution( |
| 86 | + [ |
| 87 | + FindPackageShare("ros2_control_demo_example_14"), |
| 88 | + "config", |
| 89 | + "rrbot_modular_actuators_without_feedback_sensors_for_position_feedback.yaml", |
| 90 | + ] |
| 91 | + ) |
| 92 | + rviz_config_file = PathJoinSubstitution( |
| 93 | + [FindPackageShare("ros2_control_demo_description"), "rrbot/rviz", "rrbot.rviz"] |
| 94 | + ) |
| 95 | + |
| 96 | + control_node = Node( |
| 97 | + package="controller_manager", |
| 98 | + executable="ros2_control_node", |
| 99 | + parameters=[robot_description, robot_controllers], |
| 100 | + output="both", |
| 101 | + ) |
| 102 | + robot_state_pub_node = Node( |
| 103 | + package="robot_state_publisher", |
| 104 | + executable="robot_state_publisher", |
| 105 | + output="both", |
| 106 | + parameters=[robot_description], |
| 107 | + ) |
| 108 | + rviz_node = Node( |
| 109 | + package="rviz2", |
| 110 | + executable="rviz2", |
| 111 | + name="rviz2", |
| 112 | + output="log", |
| 113 | + arguments=["-d", rviz_config_file], |
| 114 | + condition=IfCondition(gui), |
| 115 | + ) |
| 116 | + |
| 117 | + joint_state_broadcaster_spawner = Node( |
| 118 | + package="controller_manager", |
| 119 | + executable="spawner", |
| 120 | + arguments=["joint_state_broadcaster", "--controller-manager", "/controller_manager"], |
| 121 | + ) |
| 122 | + |
| 123 | + robot_controller_spawner = Node( |
| 124 | + package="controller_manager", |
| 125 | + executable="spawner", |
| 126 | + arguments=[robot_controller, "--controller-manager", "/controller_manager"], |
| 127 | + ) |
| 128 | + |
| 129 | + # Delay rviz start after `joint_state_broadcaster` |
| 130 | + delay_rviz_after_joint_state_broadcaster_spawner = RegisterEventHandler( |
| 131 | + event_handler=OnProcessExit( |
| 132 | + target_action=joint_state_broadcaster_spawner, |
| 133 | + on_exit=[rviz_node], |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + # Delay start of robot_controller after `joint_state_broadcaster` |
| 138 | + delay_robot_controller_spawner_after_joint_state_broadcaster_spawner = RegisterEventHandler( |
| 139 | + event_handler=OnProcessExit( |
| 140 | + target_action=joint_state_broadcaster_spawner, |
| 141 | + on_exit=[robot_controller_spawner], |
| 142 | + ) |
| 143 | + ) |
| 144 | + |
| 145 | + nodes = [ |
| 146 | + control_node, |
| 147 | + robot_state_pub_node, |
| 148 | + joint_state_broadcaster_spawner, |
| 149 | + delay_rviz_after_joint_state_broadcaster_spawner, |
| 150 | + delay_robot_controller_spawner_after_joint_state_broadcaster_spawner, |
| 151 | + ] |
| 152 | + |
| 153 | + return LaunchDescription(declared_arguments + nodes) |
0 commit comments