Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 84 additions & 94 deletions example_1/bringup/launch/rrbot.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,109 +17,99 @@
from launch.actions import DeclareLaunchArgument, RegisterEventHandler
from launch.conditions import IfCondition
from launch.event_handlers import OnProcessExit
from launch.substitutions import Command, FindExecutable, LaunchConfiguration, PathJoinSubstitution
from launch.substitutions import Command, LaunchConfiguration, PathSubstitution

from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare


def generate_launch_description():
# Declare arguments
declared_arguments = []
declared_arguments.append(
DeclareLaunchArgument(
"gui",
default_value="true",
description="Start RViz2 automatically with this launch file.",
)
)

# Initialize Arguments
gui = LaunchConfiguration("gui")

# Get URDF via xacro
robot_description_content = Command(
[
PathJoinSubstitution([FindExecutable(name="xacro")]),
" ",
PathJoinSubstitution(
[
FindPackageShare("ros2_control_demo_example_1"),
"urdf",
"rrbot.urdf.xacro",
]
),
]
)
robot_description = {"robot_description": robot_description_content}

robot_controllers = PathJoinSubstitution(
[
FindPackageShare("ros2_control_demo_example_1"),
"config",
"rrbot_controllers.yaml",
]
)
rviz_config_file = PathJoinSubstitution(
[FindPackageShare("ros2_control_demo_description"), "rrbot/rviz", "rrbot.rviz"]
)

control_node = Node(
package="controller_manager",
executable="ros2_control_node",
parameters=[robot_controllers],
output="both",
)
robot_state_pub_node = Node(
package="robot_state_publisher",
executable="robot_state_publisher",
output="both",
parameters=[robot_description],
)
rviz_node = Node(
package="rviz2",
executable="rviz2",
name="rviz2",
output="log",
arguments=["-d", rviz_config_file],
condition=IfCondition(gui),
)

joint_state_broadcaster_spawner = Node(
package="controller_manager",
executable="spawner",
arguments=["joint_state_broadcaster"],
)

# Define Node action objects needed to be referenced by RegisterEventHandler
robot_controller_spawner = Node(
package="controller_manager",
executable="spawner",
arguments=["forward_position_controller", "--param-file", robot_controllers],
)

# Delay rviz start after `joint_state_broadcaster`
delay_rviz_after_joint_state_broadcaster_spawner = RegisterEventHandler(
event_handler=OnProcessExit(
target_action=joint_state_broadcaster_spawner,
on_exit=[rviz_node],
)
arguments=[
"forward_position_controller",
"--param-file",
PathSubstitution(FindPackageShare("ros2_control_demo_example_1"))
/ "config"
/ "rrbot_controllers.yaml",
],
)

# Delay start of joint_state_broadcaster after `robot_controller`
# TODO(anyone): This is a workaround for flaky tests. Remove when fixed.
delay_joint_state_broadcaster_after_robot_controller_spawner = RegisterEventHandler(
event_handler=OnProcessExit(
target_action=robot_controller_spawner,
on_exit=[joint_state_broadcaster_spawner],
)
return LaunchDescription(
[
DeclareLaunchArgument(
"gui",
default_value="true",
description="Start RViz2 automatically with this launch file.",
),
# Control node
Node(
package="controller_manager",
executable="ros2_control_node",
parameters=[
PathSubstitution(FindPackageShare("ros2_control_demo_example_1"))
/ "config"
/ "rrbot_controllers.yaml"
],
output="both",
),
# robot_state_publisher with robot_description from xacro
Node(
package="robot_state_publisher",
executable="robot_state_publisher",
output="both",
parameters=[
{
"robot_description": Command(
[
"xacro",
" ",
PathSubstitution(FindPackageShare("ros2_control_demo_example_1"))
/ "urdf"
/ "rrbot.urdf.xacro",
]
)
}
],
),
# Include the robot_controller_spawner action (starts at launch)
robot_controller_spawner,
# When robot_controller_spawner exits, start the joint_state_broadcaster
RegisterEventHandler(
event_handler=OnProcessExit(
target_action=robot_controller_spawner,
on_exit=[
joint_state_broadcaster_spawner := Node(
package="controller_manager",
executable="spawner",
arguments=["joint_state_broadcaster"],
)
],
)
),
# When joint_state_broadcaster_spawner exits, start RViz
RegisterEventHandler(
event_handler=OnProcessExit(
target_action=joint_state_broadcaster_spawner,
on_exit=[
Node(
package="rviz2",
executable="rviz2",
name="rviz2",
output="log",
arguments=[
"-d",
PathSubstitution(FindPackageShare("ros2_control_demo_description"))
/ "rrbot"
/ "rviz"
/ "rrbot.rviz",
],
condition=IfCondition(LaunchConfiguration("gui")),
)
],
)
),
]
)

nodes = [
control_node,
robot_state_pub_node,
robot_controller_spawner,
delay_rviz_after_joint_state_broadcaster_spawner,
delay_joint_state_broadcaster_after_robot_controller_spawner,
]

return LaunchDescription(declared_arguments + nodes)