How to auto-build & setup ROS package in sub-directory from launch file
This example shows how to colcon build
(if neccessary) a ROS package in a sub-directory, and add it to the AMENT_PREFIX_PATH
for the current launch.
In this example, we’ll use franka_description
as the package to be built:
Library file: 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 your launch file, here’s how to use it:
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
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow