The production-ready flight control system for autonomous altitude hold and safety.
Built with modern embedded patterns for precision UAV navigation and stability.
Note
Production-Grade UAV Flight Software
This is a high-performance, real-time implementation of an Altitude Hold system for unmanned aerial vehicles (UAVs). Built on the AltiOS micro-kernel philosophy, it provides precise PID-based altitude maintenance, noise-suppressed sensor feedback, and real-time visual telemetry.
Perfect for: Autonomous drone development, flight control research, embedded systems education, and high-precision altitude maintenance projects.
AltiOS UAV provides the reliable path from raw sensor data to stable flight, offering real-time PID tuning, visual state diagnostics, and robust error handling.
The Altitude Hold algorithm is the backbone of autonomous hover. This implementation leverages a high-speed PID controller and moving average filtering to transform noisy ultrasonic distance readings into a smooth, stable throttle response.
// Core PID altitude hold implementation
void computePID(double dt) {
double error = setpoint - altCurrent;
// Integral with anti-windup protection
integral += error * dt;
integral = constrain(integral, -INTEGRAL_MAX, INTEGRAL_MAX);
// Derivative component
double derivative = (error - errorPrev) / dt;
errorPrev = error;
pidOutput = (Kp * error) + (Ki * integral) + (Kd * derivative);
}- What is AltiOS?
- Why This Implementation?
- System Features
- Hardware Setup
- Quick Start
- Usage & Calibration
- Visual Telemetry
- Algorithm Implementation
- Architecture
- Deployment
- Contributing
- Senior Developer Information
AltiOS (Altitude Operating System) is a specialized flight control layer designed for the Arduino UNO R4 WiFi architecture. It focuses on:
- Stability: Utilizing 20Hz PID loops for millisecond-accurate throttle correction.
- Safety: Implementing anti-windup integral logic and throttle safety bounds.
- Awareness: Real-time 12x8 LED Matrix visualization of the UAV's flight state.
- Interaction: A comprehensive serial CLI for run-time gain tuning and system diagnosis.
The system simulates a virtual "safety tether," ensuring that even with fluctuating sensor data, the UAV maintains its target altitude within a Β±10cm hold zone.
- PID Control: The mathematical heart that manages proportional, integral, and derivative corrections.
- Filtering: A multi-sample moving average filter that ignores sonic reflections and sensor glitches.
- Telemetry: Visual feedback on the R4's built-in LED matrix for immediate state identification.
- Arming Sequence: A safe startup routine that initializes sensors before enabling ESC output.
This firmware handles the complex physics of vertical displacement while providing a high-level developer interface.
π High-Performance: Optimized PID loop running at 20Hz for immediate response.
π Modular: Clean separation between sensor logic, control theory, and visualization.
π Transparent: Real-time telemetry via Serial and LED Matrix.
π§ͺ Safety-First: Hardcoded throttle limits (1100-1700Β΅s) and anti-windup logic.
π» Future-Proof: Built for the Renesas RA4M1 on the Arduino UNO R4.
π± Visual: Interactive boot animations and dynamic flight indicators.
π¨ Refined UI: Custom hand-drawn animation frames for the LED matrix.
- Safety-First PID: Complete implementation with user-tunable Kp, Ki, and Kd constants.
- Precision Filtering: 5-sample moving average filter for ultrasonic stability.
- Dynamic Setpoints: Modify target altitude on-the-fly via Serial commands.
- Anti-Windup: Prevents integral saturation during long climb phases.
- Emergency Stop: Global
stopcommand to immediately disarm all motors.
- π― Hold Indicator: Solid square when within the Β±10cm target zone.
- πΌ Climb Indicator: Single/Double arrows for slight or critical altitude gain.
- π½ Descend Indicator: Single/Double arrows for slight or critical altitude loss.
- π Radar Sweep: A custom 2.2s radar-style boot sequence during initialization.
- β¨οΈ Live Tuning: Change PID gains (
p,i,d) without reflashing. - π Status Snapshots: Comprehensive system health report with a single command.
- π Telemetry Stream: Detailed CSV-ready output for flight analysis.
| Component | Pin | Description |
|---|---|---|
| TRIG | D9 | Ultrasonic Trigger Signal |
| ECHO | D10 | Ultrasonic Echo Return |
| ESC 1 | D3 | Electronic Speed Controller 1 |
| ESC 2 | D5 | Electronic Speed Controller 2 |
| ESC 3 | D6 | Electronic Speed Controller 3 |
| ESC 4 | D11 | Electronic Speed Controller 4 |
- Microcontroller: Arduino UNO R4 WiFi (Mandatory for LED Matrix)
- Sensor: HY-SRF05 or HC-SR04 Ultrasonic module
- Power: 5V stable source for sensor array
- Frames:
animation.hmust reside in the same directory asaltitude_hold.ino.
# Clone the repository
git clone https://github.com/AlphsX/AntiOS_UAV.git
cd AntiOS_UAV/altitude_hold
# Open altitude_hold.ino in Arduino IDE 2.x
# Ensure 'Arduino UNO R4 WiFi' board package is installed- Mount Sensor: Ensure the ultrasonic head points directly downward.
- Flash Firmware: Upload via USB-C to your R4 WiFi.
- Verify Boot: Observe the "Radar Sweep" animation on the 12x8 matrix.
- Open Serial Monitor: Set baud rate to
115200.
Enter these commands into the Serial Monitor to fine-tune your flight:
| Command | Action | Example |
|---|---|---|
t[val] |
Set Target Altitude (cm) | t100 (Hold at 1 meter) |
p[val] |
Set Proportional Gain | p2.8 |
i[val] |
Set Integral Gain | i0.05 |
d[val] |
Set Derivative Gain | d1.5 |
status |
System Health Report | status |
stop |
Emergency Disarm | stop |
The LED matrix reflects the Error = Setpoint - Current_Altitude:
- Double-Up (Flash): Error > 40cm (Add aggressive power)
- Single-Up (Slow): Error 10-40cm (Add slight power)
- Solid Square: Target Reached (HOLD)
- Single-Down (Slow): Error -10 to -40cm (Reduce slight power)
- Double-Down (Flash): Error < -40cm (Reduce aggressive power)
The system utilizes high-precision timing for the ultrasonic return, converted to metric units before entering the filter pipeline.
double readDistanceCM() {
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long dur = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout
return (dur * 0.0343) / 2.0;
}To combat sonic reflections from ground surfaces, we use a 5-sample circular buffer. This prevents the drone from reacting to "phantom" spikes in distance readings.
AltiOS_UAV/
βββ altitude_hold/
β βββ altitude_hold.ino # Main controller & loops
β βββ animation.h # LED Matrix frame definitions
βββ .gitignore # Repository hygiene
βββ README.md # You are here
Project Architect specializing in Embedded Flight Systems, Control Theory, and Real-Time Architectures.
- π Autonomous Systems: PID tuning, Kalman filtering, and flight dynamics.
- βοΈ Embedded Architecture: Arduino R4 (Renesas), ESP32, and ARM Cortex.
- π» Modern Embedded C++: OOP patterns and efficient memory management.
- π Signal Processing: Noise reduction, sensor fusion, and telemetry streams.
- GitHub: @AlphsX
- Repo: AntiOS_UAV
This project is licensed under the MIT License - see the LICENSE file for details.
β Star this repository if you find it helpful!
Made with β£οΈ for the UAV development community
Β© 2026 AlphsX, Inc.