diff --git a/.gitignore b/.gitignore index 38c3b89..a8a6e51 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # autogenerated files lib/cmn_hdrs/ros_info.h +*.pyc diff --git a/launch/main_launch.py b/launch/main_launch.py index fc4a3db..ba01934 100644 --- a/launch/main_launch.py +++ b/launch/main_launch.py @@ -1,19 +1,46 @@ +"""Launch file that runs all nodes for the network systems ROS package.""" + import importlib import os -from types import ModuleType from typing import List from launch_ros.actions import Node -from launch import LaunchDescription +from launch import LaunchDescription, LaunchDescriptionEntity +from launch.actions import OpaqueFunction +from launch.launch_context import LaunchContext from launch.substitutions import LaunchConfiguration +# Local launch arguments and constants +PACKAGE_NAME = "network_systems" + +# Add args with DeclareLaunchArguments object(s) and utilize in setup_launch() +LOCAL_LAUNCH_ARGUMENTS = [] -def get_global_main_launch_module() -> ModuleType: - """Execute and return the main launch file of the `global_launch` package. + +def generate_launch_description() -> LaunchDescription: + """The launch file entry point. Generates the launch description for the `network_systems` + package. Returns: - ModuleType: The executed module. + LaunchDescription: The launch description. + """ + global_launch_arguments, global_environment_vars = get_global_launch_arguments() + return LaunchDescription( + [ + *global_launch_arguments, + *global_environment_vars, + *LOCAL_LAUNCH_ARGUMENTS, + OpaqueFunction(function=setup_launch), + ] + ) + + +def get_global_launch_arguments() -> List[LaunchDescriptionEntity]: + """Gets the global launch arguments defined in the global launch file. + + Returns: + List[LaunchDescriptionEntity]: List of global launch argument objects. """ global_main_launch = os.path.join( os.getenv("ROS_WORKSPACE"), "src", "global_launch", "main_launch.py" @@ -21,55 +48,49 @@ def get_global_main_launch_module() -> ModuleType: spec = importlib.util.spec_from_file_location("global_launch", global_main_launch) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) - return module - + global_launch_arguments = module.GLOBAL_LAUNCH_ARGUMENTS + global_environment_vars = module.ENVIRONMENT_VARIABLES + return global_launch_arguments, global_environment_vars -GLOBAL_LAUNCH = get_global_main_launch_module() +def setup_launch(context: LaunchContext) -> List[LaunchDescriptionEntity]: + """Collects launch descriptions that describe the system behavior in the `network_systems` + package. -def generate_launch_description() -> LaunchDescription: - """Generate the launch description for the `local_pathfinding` package. + Args: + context (LaunchContext): The current launch context. Returns: - LaunchDescription: The launch description. + List[LaunchDescriptionEntity]: Launch descriptions. """ - # get_nodes() arguments for the package launch - common_parameters = [GLOBAL_LAUNCH.GLOBAL_CONFIG] - common_ros_arguments = [*GLOBAL_LAUNCH.get_log_ros_arguments()] - mode = LaunchConfiguration("mode") - - return LaunchDescription( - [ - *GLOBAL_LAUNCH.GLOBAL_LAUNCH_ARGUMENTS, - *get_nodes(common_parameters, common_ros_arguments, mode), - ] - ) + launch_description_entities = list() + launch_description_entities.append(get_cached_fib_description(context)) + return launch_description_entities -def get_nodes(common_parameters: List, common_ros_arguments: List, mode: str) -> List[Node]: - """Get the nodes to be launched depending on an indicated mode. +def get_cached_fib_description(context: LaunchContext) -> Node: + """Gets the launch description for the cached_fib node. Args: - common_parameters (List): Parameters that are common to all nodes. - common_ros_arguments (List): ROS arguments that are common to all nodes. - mode (str): The system mode. + context (LaunchContext): The current launch context. Returns: - List[Node]: The nodes to be launched. + Node: The node object that launches the cached_fib node. """ - # cached_fib parameters and ROS arguments - cached_fib_parameters = [*common_parameters] - cached_fib_ros_arguments = [*common_ros_arguments] - - nodes = [ - Node( - package="network_systems", - namespace="example", - executable="example", - name="cached_fib", - parameters=cached_fib_parameters, - ros_arguments=cached_fib_ros_arguments, - ), + node_name = "cached_fib" + ros_parameters = [LaunchConfiguration("config").perform(context)] + ros_arguments = [ + "--log-level", + [f"{node_name}:=", LaunchConfiguration("log_level")], ] - return nodes + node = Node( + package=PACKAGE_NAME, + namespace="example", + executable="example", + name=node_name, + parameters=ros_parameters, + ros_arguments=ros_arguments, + ) + + return node