drone simulation environment setup (PX4, ROS2, gazebo)
Introduction
There are many ways to simulate drones, but this article focuses on a setup aimed at ultimately using the same code for autonomous drone flight on an actual drone.
- PX4: An open-source flight controller, easy to use with hardware like pixhawk
- ROS2: An open-source framework for robots, ROS is also fine
- gazebo: An open-source 3D simulator, lightweight enough for laptop use
In theory, using pixhawk and an onboard Ubuntu computer should make it easy to transfer to an actual drone.
Setup
First, install Ubuntu 22.04.
https://ubuntu.com/tutorials/install-ubuntu-desktop#1-overview
Then install PX4, ROS2, and gazebo.
At this point, you should be able to move a drone in gazebo using sample scripts with ROS2 and PX4.
Development
Camera Data Stream
To bring camera image data from gazebo topics to ROS2 requires a bit of work.
Install ros-humble-ros-gzgarden:
sudo apt install ros-humble-ros-gzgarden
You need to create a bridge with either ros_gz_image or ros_gz_bridge.
ros2 run ros_gz_image image_bridge /camera
ros2 run ros_gz_bridge parameter_bridge /camera@sensor_msgs/msg/Image@ignition.msgs.Image
ros_gz_bridge can be used for other topics (like depth) as well, so it seems more versatile.
ros2 run ros_gz_bridge parameter_bridge /camera@sensor_msgs/msg/Image@ignition.msgs.Image
/camera_info@sensor_msgs/msg/CameraInfo@ignition.msgs.CameraInfo /depth_camera/points@sensor_msgs/msg/PointCloud2@gz.msgs.PointCloudPacked /depth_camera@sensor_msgs/msg/Image@ignition.msgs.Image
Keyboard Control
Since PX4’s sample code is waypoint-based, you’ll need code to control things like the drone’s speed for keyboard operation.
This code makes it easy to use the keyboard, but note that it uses mavsdk, which is different from the px4_ros_com sample code.
Trouble Shooting
I spent some time debugging this issue, so I’m including it just in case.
When trying to start PX4, I encountered this error:
ERROR [gz_bridge] timed out waiting for clock message
ERROR [gz_bridge] Task start failed (-1)
ERROR [init] gz_bridge failed to start
ERROR [px4] Startup script returned with return value: 256
This command fixed it:
pkill -9 ruby
unset GZ_IP
unset GZ_PARTITION
Reference:
The PX4 user forum is very active, so it’s recommended to consult there if you get stuck in debugging.