rewrite the launch file

This commit is contained in:
ryleu
2026-03-18 00:12:42 -05:00
parent ce39d0aeb9
commit c814f34ca6

View File

@@ -1,139 +1,91 @@
#!/usr/bin/env python3
from launch import LaunchDescription from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, OpaqueFunction, Shutdown from launch.actions import DeclareLaunchArgument, Shutdown
from launch.substitutions import (
LaunchConfiguration,
ThisLaunchFileDir,
PathJoinSubstitution,
)
from launch_ros.actions import Node
from launch.conditions import IfCondition from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
# Prevent making __pycache__ directories def generate_launch_description():
from sys import dont_write_bytecode connector = LaunchConfiguration("connector")
use_ptz = LaunchConfiguration("use_ptz")
dont_write_bytecode = True ld = LaunchDescription()
# arguments
ld.add_action(
DeclareLaunchArgument(
"connector",
default_value="auto",
description="Connector parameter for anchor node (default: auto)",
)
)
def launch_setup(context, *args, **kwargs): ld.add_action(
# Retrieve the resolved value of the launch argument 'mode' DeclareLaunchArgument(
mode = LaunchConfiguration("mode").perform(context) "use_ptz",
connector = LaunchConfiguration("connector").perform(context) default_value="true", # must be string for launch system
nodes = [] description="Whether to launch PTZ node (default: true)",
)
)
if mode == "anchor": # nodes
# Launch every node and pass "anchor" as the parameter ld.add_action(
nodes.append(
Node(
package="arm_pkg",
executable="arm", # change as needed
name="arm",
output="both",
parameters=[{"launch_mode": mode}],
on_exit=Shutdown(),
)
)
nodes.append(
Node(
package="core_pkg",
executable="core", # change as needed
name="core",
output="both",
parameters=[{"launch_mode": mode}],
on_exit=Shutdown(),
)
)
nodes.append(
Node(
package="core_pkg",
executable="ptz", # change as needed
name="ptz",
output="both",
condition=IfCondition(LaunchConfiguration("use_ptz", default="true")),
# Currently don't shutdown all nodes if the PTZ node fails, as it is not critical
# on_exit=Shutdown() # Uncomment if you want to shutdown on PTZ failure
)
)
nodes.append(
Node(
package="bio_pkg",
executable="bio", # change as needed
name="bio",
output="both",
parameters=[{"launch_mode": mode}],
on_exit=Shutdown(),
)
)
nodes.append(
Node(
package="anchor_pkg",
executable="anchor", # change as needed
name="anchor",
output="both",
parameters=[{"launch_mode": mode, "connector": connector}],
on_exit=Shutdown(),
)
)
elif mode in ["arm", "core", "bio", "ptz"]:
# Only launch the node corresponding to the provided mode.
if mode == "arm":
nodes.append(
Node( Node(
package="arm_pkg", package="arm_pkg",
executable="arm", executable="arm",
name="arm", name="arm",
output="both", output="both",
parameters=[{"launch_mode": mode}], parameters=[{"launch_mode": "anchor"}],
on_exit=Shutdown(), on_exit=Shutdown(),
) )
) )
elif mode == "core":
nodes.append( ld.add_action(
Node( Node(
package="core_pkg", package="core_pkg",
executable="core", executable="core",
name="core", name="core",
output="both", output="both",
parameters=[{"launch_mode": mode}], parameters=[{"launch_mode": "anchor"}],
on_exit=Shutdown(), on_exit=Shutdown(),
) )
) )
elif mode == "bio":
nodes.append( ld.add_action(
Node(
package="bio_pkg",
executable="bio",
name="bio",
output="both",
parameters=[{"launch_mode": mode}],
on_exit=Shutdown(),
)
)
elif mode == "ptz":
nodes.append(
Node( Node(
package="core_pkg", package="core_pkg",
executable="ptz", executable="ptz",
name="ptz", name="ptz",
output="both", output="both",
on_exit=Shutdown(), # on fail, shutdown if this was the only node to be launched condition=IfCondition(use_ptz),
) )
) )
else:
# If an invalid mode is provided, print an error.
print("Invalid mode provided. Choose one of: arm, core, bio, anchor, ptz.")
return nodes ld.add_action(
Node(
package="bio_pkg",
def generate_launch_description(): executable="bio",
declare_arg = DeclareLaunchArgument( name="bio",
"mode", output="both",
default_value="anchor", parameters=[{"launch_mode": "anchor"}],
description="Launch mode: arm, core, bio, anchor, or ptz", on_exit=Shutdown(),
)
) )
return LaunchDescription([declare_arg, OpaqueFunction(function=launch_setup)]) ld.add_action(
Node(
package="anchor_pkg",
executable="anchor",
name="anchor",
output="both",
parameters=[
{
"launch_mode": "anchor",
"connector": connector,
}
],
on_exit=Shutdown(),
)
)
return ld