This workspace contains a simplified ROS2 package for autonomously flying a single drone to 5 meters altitude using PX4 offboard control.
The single_drone_flight
package demonstrates basic autonomous flight capabilities:
- Automatic arming sequence
- Autonomous takeoff
- Climbing to target altitude (5 meters)
- Position holding at target altitude
The package provides both Python and C++ implementations of the flight controller, allowing you to choose based on your preference or requirements.
Before running this package, ensure you have:
- ROS2 Humble installed
- PX4 Autopilot installed in
~/PX4-Autopilot
- MicroXRCE-DDS Agent installed and available in PATH
- Required Python dependencies:
pip3 install --user -U empy pyros-genmsg setuptools pip3 install kconfiglib pip install --user jsonschema pip install --user jinja2
-
Clone or create this workspace:
cd /home/krish/flying_single_drone_ws
-
Source ROS2:
source /opt/ros/humble/setup.bash
-
Build the workspace:
colcon build
-
Source the workspace:
source install/setup.bash
Run the complete system with Python controller:
ros2 launch single_drone_flight single_drone_flight.launch.py
Run the complete system with C++ controller:
ros2 launch single_drone_flight single_drone_flight_cpp.launch.py
Both launch files will:
- Start the simulation components (MicroXRCE-DDS Agent and PX4 SITL)
- Wait 30 seconds for startup and stabilization
- Launch the respective flight control implementation
-
Start Simulation Components:
ros2 run single_drone_flight simulation_launcher
-
Wait for PX4 to boot (look for "Ready for takeoff!" message in PX4 terminal)
-
Start Python Flight Control:
ros2 run single_drone_flight single_drone_control
-
Start Simulation Components:
ros2 run single_drone_flight simulation_launcher
-
Wait for PX4 to boot (look for "Ready for takeoff!" message in PX4 terminal)
-
Start C++ Flight Control:
ros2 run single_drone_flight single_drone_control_cpp
single_drone_control.py
: Python implementation of autonomous flight control nodesingle_drone_control_cpp
: C++ implementation of autonomous flight control nodesimulation_launcher.py
: Launches PX4 SITL and MicroXRCE-DDS Agentsingle_drone_flight.launch.py
: Coordinated launch file for Python controllersingle_drone_flight_cpp.launch.py
: Coordinated launch file for C++ controller
The drone will automatically execute this sequence:
- IDLE: Wait for flight checks to pass
- ARMING: Send arm commands until vehicle is armed
- TAKEOFF: Send takeoff command and wait for takeoff state
- CLIMBING: Wait for loiter state, then switch to offboard mode
- REACHING_TARGET: Use position control to reach exact 5m altitude
- HOVERING: Maintain position at target altitude
- Continuous monitoring of flight checks
- Automatic return to IDLE state if safety conditions fail
- Failsafe detection and handling
- Arming state monitoring
The system uses PX4's NED (North-East-Down) coordinate frame:
- Positive X: North
- Positive Y: East
- Positive Z: Down (so -5.0 means 5 meters above ground)
The flight control node provides detailed logging of:
- State transitions
- Vehicle status changes
- Current altitude
- Safety condition checks
-
"Vertical velocity unstable" and "velocity estimate error"
- Cause: PX4 needs time to stabilize its velocity estimation after startup
- Solution: Wait 30-60 seconds after PX4 starts before running flight control
- Quick Fix: Use the provided fix script:
./fix_drone_issues.sh
-
Rapid state transitions between IDLE and ARMING
- Cause: Improved safety checks in the flight control logic
- Solution: The updated code now includes proper timing delays and less frequent command sending
-
Simulation doesn't start: Check that PX4-Autopilot is installed in
~/PX4-Autopilot
-
MicroXRCE-DDS Agent not found: Make sure it's installed and in PATH
-
Drone doesn't arm:
- Check PX4 terminal for error messages
- Ensure simulation has been running for at least 30 seconds
- Look for "Ready for takeoff!" message in PX4 terminal
-
Flight checks fail: Ensure simulation is fully loaded before starting flight control
If you're experiencing the velocity estimation errors, run:
./fix_drone_issues.sh
This script will:
- Clean up any existing processes
- Start fresh simulation components
- Wait for PX4 to stabilize
- Provide guidance for next steps
-
Kill any existing processes:
pkill -f px4 pkill -f gazebo pkill -f MicroXRCE
-
Start MicroXRCE-DDS Agent:
MicroXRCEAgent udp4 -p 8888
-
Start PX4 SITL (in another terminal):
cd ~/PX4-Autopilot make px4_sitl gazebo-classic_iris
-
Wait for "Ready for takeoff!" message in PX4 terminal
-
Wait additional 30 seconds for velocity estimation to stabilize
-
Run flight control:
cd /home/krish/flying_single_drone_ws source install/setup.bash # For Python controller: ros2 run single_drone_flight single_drone_control # OR for C++ controller: ros2 run single_drone_flight single_drone_control_cpp
To modify the target altitude, edit the target_altitude
variable in single_drone_control.py
:
self.target_altitude = -5.0 # Change this value (negative for up in NED frame)
To modify the target altitude, edit the target_altitude_
variable in SingleDroneControl.cpp
:
target_altitude_ = -5.0f; // Change this value (negative for up in NED frame)
This package is simplified from the excellent ROS2_PX4_Offboard_Example by ARK Electronics: https://github.com/ARK-Electronics/ROS2_PX4_Offboard_Example