Wie man ein ROS-Paket in einem Unterverzeichnis aus einer Launch-Datei automatisch baut & einrichtet

Dieses Beispiel zeigt, wie man ein ROS-Paket in einem Unterverzeichnis mit colcon build (falls erforderlich) baut und es zum AMENT_PREFIX_PATH für den aktuellen Launch hinzufügt.

In diesem Beispiel verwenden wir franka_description als das zu bauende Paket:

Bibliotheksdatei: ros_autobuild.py:

ros_autobuild.py
import subprocess
import os.path

def autobuild_and_setup_ros_package(package_name, package_path):
    # If [package]/install does not exist, run colcon_build there
    install_path = os.path.join(package_path, "install")
    if not os.path.exists(os.path.join(package_path, "install")):
        print(
            f"{package_path}/install does not exist. Running 'colcon build' in {package_path}"
        )
        subprocess.run(["colcon", "build"], cwd=package_path)
    # Preprend absolute path of [package]/install/[package] to AMENT_PREFIX_PATH
    extra_path = os.path.abspath(os.path.join(install_path, package_name))
    os.environ["AMENT_PREFIX_PATH"] = extra_path + os.pathsep + os.environ.get("AMENT_PREFIX_PATH", "")

In Ihrer Launch-Datei, so verwenden Sie es:

example_generate_launch.py
import ros_autobuild

def generate_launch_description():
  ros_autobuild.autobuild_and_setup_ros_package(
    "franka_description",
    # Example: franka_description folder in the same directory
    os.path.join(os.path.dirname(__file__), "franka_description")
  )
  # [...] The rest of your launch file

Check out similar posts by category: ROS, Python