UAV: Multiple Altitude Reference Systems¶
Library:
include/geographic.h- Geographic primitives (MSL altitude, position)
Example:
unmanned_aerial_vehicle.cpp- UAV altitude reference systems
This advanced example demonstrates a critical challenge in aviation software: managing multiple altitude reference systems simultaneously. In aviation, altitudes can be measured relative to different reference points (mean sea level, ground, ellipsoid, launch point), and confusing these can have catastrophic consequences. mp-units makes these distinctions type-safe and impossible to mix up.
The Problem: Multiple Altitude Datums¶
In aviation and UAV operations, altitude can be expressed relative to several different reference points:
- MSL (Mean Sea Level) - standard barometric altitude
- AGL (Above Ground Level) - height above local terrain
- HAE (Height Above Ellipsoid) - GPS altitude relative to WGS84 ellipsoid
- HAL (Height Above Launch) - relative altitude from takeoff point
Each serves a different purpose, and mixing them is dangerous. For example:
- ATC (Air Traffic Control) uses MSL for separation
- Obstacle clearance requires AGL
- GPS provides HAE
- UAVs often track HAL for relative navigation
Type-Safe Altitude References¶
Mean Sea Level (MSL)¶
The example uses MSL altitude from the geographic module as the base reference:
| geographic.h | |
|---|---|
This defines the standard barometric altitude reference used in aviation, with custom formatting that appends "AMSL" (Above Mean Sea Level) to clearly identify the reference system.
Multiple Earth Gravity Models¶
HAE altitudes depend on which Earth gravity model is used. The example defines an enum for different Earth gravity models and a templated point origin:
The template parameter ensures that HAE altitudes using different models are distinct types that cannot be accidentally mixed. Each combination of altitude reference and gravity model creates a unique type, making it impossible to accidentally mix them.
Height Above Launch¶
For UAVs, tracking altitude relative to the launch point is critical:
| unmanned_aerial_vehicle.cpp | |
|---|---|
This creates a completely separate altitude reference system for UAV operations, with its own absolute point origin distinct from MSL, HAE, or any other reference.
Converting Between Reference Systems¶
The type system prevents accidental conversions, but allows intentional ones:
MSL to HAE Conversion¶
Converting from MSL to HAE requires accounting for the geoid undulation (the difference between the ellipsoid and mean sea level at a specific location):
This conversion:
- Requires geographic position (latitude/longitude)
- Accounts for local geoid undulation
- Returns altitude of the correct HAE type for the specified Earth model
Relative Altitude Calculations¶
The UAV class demonstrates safe relative altitude tracking:
The quantity_from() method safely computes the difference between two points in the same
reference system, returning a relative altitude (height) quantity.
Custom Formatting for Each System¶
Each altitude reference system has custom formatting to make output unambiguous:
Output clearly shows which reference system is being used:
hal = 1219.20 m HAL
agl = 2925.00 m
EPPR: 54.24772° N 18.6745° E, 4.88 m AMSL, -24.61 m HAE(EGM2008-1)
Practical UAV Operations¶
The example shows realistic UAV scenarios:
Each altitude is:
- Type-safe - Cannot be accidentally mixed
- Self-documenting - The type tells you which reference system
- Correctly formatted - Output makes the reference system explicit
- Efficiently stored - Zero runtime overhead for the type safety
Real-World Safety Impact¶
In actual UAV operations, altitude confusion can cause:
- Crashes - Flying into terrain thinking you're higher than you are
- Airspace violations - Using wrong altitude reference for clearances
- Loss of aircraft - GPS (HAE) vs barometric (MSL) confusion
- Regulatory violations - Incorrect altitude reporting
mp-units makes these errors impossible at compile-time:
msl_altitude msl = mean_sea_level + 1000 * m;
hal_altitude hal = height_above_launch + 500 * m;
// auto sum = msl + hal; // ✗ Compile error!
// auto diff = msl - hal; // ✗ Compile error!
// if (msl < hal) { /* ... */ } // ✗ Compile error!
The compiler prevents all accidental mixing of altitude references.
Key Features Demonstrated¶
1. Multiple Absolute Point Origins¶
Different origins create incompatible types, preventing dangerous mistakes:
mean_sea_level(MSL)height_above_ellipsoid<Model>(HAE, templated by gravity model)height_above_launch(HAL)
2. Template Parameters for Configuration¶
template<earth_gravity_model M>
struct height_above_ellipsoid_t final : absolute_point_origin<isq::altitude> {
static constexpr earth_gravity_model egm = M;
};
The Earth model becomes part of the type, ensuring that HAE altitudes using different models cannot be confused.
3. Safe Relative Calculations¶
The quantity_from() method safely computes relative altitudes between points in the same
reference system.
4. Custom Formatting Per Reference¶
Each altitude type has specialized formatting showing its reference system, making output unambiguous and safe to interpret.
5. Integration with Geographic Systems¶
The example integrates with geographic coordinates and Earth models. The geoid undulation calculation is simplified (using a stub function that returns a constant value), but in production code this would call a proper geodetic library like GeographicLib.
Key Takeaways¶
- Multiple altitude references are a critical safety issue in aviation
- mp-units makes mixing different references a compile error
- Type safety extends to template parameters (gravity models)
- Zero runtime overhead for this additional safety
- Custom formatting prevents interpretation errors
- The type system documents which reference is being used
- Integration with geographic libraries is seamless
For UAV and aviation software, this level of compile-time safety is invaluable and can literally save lives by preventing altitude confusion errors.