SOAR-UWU / UWU-Blobfish

General repo for Underwater Unit 2024
6 stars 0 forks source link

Refactor Launch Files to be Standard & Succinct #27

Open Interpause opened 3 weeks ago

Interpause commented 3 weeks ago

Due to an update in ros2launch, its now possible to write IncludeLaunchDescription in a more succinct manner:

### BEFORE
cv_pkg = get_package_share_directory("blobfish_cv")
cv_launch_dir = os.path.join(cv_pkg, "launch")
cv_launch = IncludeLaunchDescription(
    PythonLaunchDescriptionSource(os.path.join(cv_launch_dir, "cv.launch.py"))
)

### AFTER
pkg_cv = get_package_share_path("blobfish_cv")
launch_cv = IncludeLaunchDescription(str(pkg_cv / "launch" / "cv.launch.py"))

Note in the above:

Also lets to try to standardize some other things optionally:

# Any hardcoded values up here, in caps, C++ define style.
WORLD_SDF = "pool_world.sdf"

# Any externally available arguments.
def declare_launch_arguments():
    """Documentation of available arguments go here."""
    world_sdf = DeclareLaunchArgument("world_sdf", default_value=WORLD_SDF)
    return [world_sdf]

def generate_launch_description():
    # Prefix with pkg_ for package path.
    pkg_ros_gz_sim = get_package_share_path("ros_gz_sim")
    # Prefix with arg_ for launch argument substitution.
    arg_world_sdf = LaunchConfiguration("world_sdf")

    # Optionally can do launch_gazebo if want to indication its an action
    # to launch another launch file.
    gazebo = IncludeLaunchDescription(
        str(pkg_ros_gz_sim / "launch" / "gz_sim.launch.py"),
        launch_arguments={"gz_args": [arg_world_sdf, " -v 0"]}.items(),
    )
    # Likewise for nodes, node_smth is optional, but in both cases, the name
    # itself should make it clear what launch action it is and what it does.

    # Write as multiline list to make it easy to comment out specific actions.
    return LaunchDescription([
        *declare_launch_arguments(),
        gazebo,
    ])