Launch PX4 via ROS2 python launch file

Hello,

I would like to know if it’s possible to launch PX4 via a ROS2 python launch file.
I have found that we can do this in ROS1 via the launch files in the folder “PX4-Autopilot/launch” so I guess that would be possible with ROS2.
I have tried and successfully installed the " RTPS/ROS2 Interface: PX4-FastRTPS Bridge". The two packages “px4_ros_com” and “px4_msgs” are available.
But I don’t know what to do with that, I’m quite lost and i spend quite a lot of time searching and trying things.
I have the latest version of PX4 and I use ROS2 Foxy.

Any help would be very appreciate.

Yes it is possible but we have not made that available upstream yet - it’s just a matter of understanding of ROS2 launch files work and you can build your own. Otherwise, you will have to wait until me and someone else has the bandwidth for that.
Also using px4_ros_com doesn’t mean you can’t run the simulation as you normally do without starting it with launch files - you can use make px4_sitl_rtps gazebo, which will start the simulation, and then in the pxh shell, issue the micrortps_client start -t UDP to start the client side of the bridge.

it’s just a matter of understanding of ROS2 launch files work and you can build your own

First of all thanks for answering me, and yes and this is the part where I struggle. I tried to convert and adapt the posix_sitl.launch file.
To begin I used https://github.com/aws-robotics/ros2-launch-file-migrator which allows me to convert a ROS .xml launch file into a ROS2 .py launch file.

But this errors occurs :
launch.invalid_launch_file_error.InvalidLaunchFileError: Caught exception when trying to load file of format [py]: "package 'mavlink_sitl_gazebo' not found, searching: ['/opt/ros/foxy']

It just can’t find the mavlink_sitl_gazebo package in /opt/ros/foxy when it try to find the empty.world file. It’s not a big error and I can “bypass” it by just not providing a world file.

But I have the same issue when I want to launch the px4 package as a ROS2 Node (I don’t know if “launch a package as a ROS2 Node” is a correct term), I think this is where the problem stands (in my comprehension of ROS2 launch file). Here is the code where I launch it (I only put what I think is necessary) :

launch_ros.actions.Node(
        package='px4',
        executable='px4',
        name='sitl',
        output='screen',
        on_exit=launch.actions.Shutdown()
    ),

But the same errors occurs :

ament_index_python.packages.PackageNotFoundError: "package 'px4' not found, searching: ['/opt/ros/foxy']"

So I thought about some ways to solve this issues :

  • locate where the package is and the path to the correct environment variable. But I didn’t understand where this package is stored and which variable should I modify ( is it $AMENT_PREFIX_PATH or $AMENT_CURRENT_PREFIX ?)
  • maybe px4 isn’t compile as a ROS2 package and it didn’t find it (yeah still the same understanding problem that I have on ROS2 package)

Ultimately what I would like to do with my ROS2 launch file is :

  1. Launch a gazebo
  2. Spawn a drone (typhoon_h480)
  3. Launch PX4 so i can communicate with it and command it with QGroundControl
  4. Launch my own ROS2 image processing node

So here it is a bit more in depth of my problem thanks you for reading me, hope it wasn’t too boring, and thanks you in advance for your answer!

That’s exactly the issue you are facing. So you rather had support to build the Firmware as an ament package, or you manually start the px4 daemon (after building with make px4_sitl gazebo) in the launch file. The daemon should be found int the build/px4_sitl/ folder.

Ok now I see more clearly, thanks you again for your answer.
I think i’ll try to build it as an ament package.

Hello @Kreace ,

Facing the same need as yours, we also wrote our own ROS 2 launchfile to launch a Gazebo simulation with an Iris running PX4, communicating over ROS 2 and controlled over QGroundControl.

We would love to contribute it to the PX4 codebase or share it with you if you still need a launchfile.

The only issue that keeps us from doing so is that since, as @TSC21 said, PX4 does not at the moment build as an Ament package, it does not install its executables and resources to a ROS 2 workspace so we had to hard-code the location of the PX4 codebase to find the executable.

We would be happy to share this launchfile with you, and if you hopefully had some time on your hands to help make PX4 build as an Ament package, it would become possible to use get_pkg_share_directory to retrieve the stuff instead of a hard-coded path and thus make this launchfile available to the larger PX4 community by contributing it.

As I already said, we would love to contribute this launchfile, the only thing is that since we got it working for ourselves we do not have enough time on our hands to modify the PX4 build system to be able to contribute it. We are a very small team.

Please let me know if you are interested.