๐ forApollo
April 3, 2026 ยท View on GitHub
๐ forApollo
The math that landed on the Moon โ generalized for everything.
A universal state estimation, navigation, and guidance engine. Born from Apollo. Built for everything.
Buzz Aldrin on the lunar surface, July 20, 1969. The guidance computer that got him there ran at 2 MHz with 74KB of memory. The algorithms inside it are timeless.
๐ The Story
On July 20, 1969, the Apollo Guidance Computer โ a machine slower than a modern calculator โ landed two humans on the Moon. It did this with 74 kilobytes of memory, a 2 MHz clock, and some of the most elegant algorithms ever written.
Those algorithms didn't care that they were running on a spacecraft.
The Kalman filter that tracked Apollo's position? It's the same math that fuses GPS and IMU data on your phone. The Lambert solver that computed transfer orbits? It's the same boundary-value problem that plans robot arm trajectories. The powered descent guidance that landed Eagle on the Sea of Tranquility? It's the same optimal control that lands a drone on a rooftop.
The state vector doesn't care what it represents.
A position in orbit. A stock price. A robot joint angle. A training loss curve. They're all just numbers โ and they all need the same thing: estimation (where am I?), prediction (where will I be?), and guidance (how do I get where I want to go?).
forApollo takes the flight-proven algorithms from the Apollo Guidance Computer and generalizes them into a universal engine. Same math. Any domain. Any state space.
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ YOUR APPLICATION โ
โ Python ยท Rust ยท C++ ยท JavaScript ยท Zig โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ZIG SAFETY LAYER โ
โ Bounds checking ยท Error mapping ยท Size-based dispatch โ
โ @import("formath") ยท @import("forternary") โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FORTRAN 2008 KERNELS (fa_*) โ
โ Pure math ยท OpenMP parallel ยท ISO_C_BINDING โ
โ Zero dependencies ยท Edge-deployable โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Three Layers, One Engine
| Layer | Files | Purpose |
|---|---|---|
| ๐ง Engine | estimate ยท propagate ยท guidance ยท coords | Domain-agnostic core. Works with any state vector. |
| ๐งฉ Models | dynamics ยท observe | Pluggable catalog of dynamics & measurement models. |
| ๐ Domain | astro ยท environ ยท time | Space-specific utilities. Non-space users never touch these. |
๐ง The Engine (Domain-Agnostic)
The engine doesn't know if it's tracking a spacecraft, a drone, or a stock price. It just sees state vectors, covariance matrices, and dynamics functions.
Estimators โ "Where am I?"
| Algorithm | Heritage | What it does |
|---|---|---|
| ๐ข KF | Kalman 1960 | Optimal for linear systems |
| ๐ข EKF | Apollo AGC | Jacobian linearization โ the workhorse |
| ๐ข IEKF | โ | Re-linearizes at updated state for tighter estimates |
| ๐ด ESKF | Apollo flight software | Error-state filter โ what actually flew to the Moon |
| ๐ฃ UKF | Julier & Uhlmann 1997 | Sigma-point โ no Jacobians needed |
| ๐ต SR-EKF / SR-UKF | โ | Square-root variants โ numerically bulletproof |
| ๐ก IF / EIF | โ | Information form โ natural for multi-sensor fusion |
| ๐ SIR Particle | Gordon et al. 1993 | Bootstrap โ handles any nonlinearity |
| ๐ Auxiliary Particle | Pitt & Shephard 1999 | Better proposal distribution |
| ๐ Rao-Blackwellized | โ | Hybrid: particles for nonlinear, Kalman for linear substates |
| โช RTS Smoother | Rauch et al. 1965 | Backward pass โ optimal offline estimation |
| โช URTSS | โ | Unscented smoother |
| ๐ท Batch WLS | โ | Weighted least squares (orbit determination) |
| ๐ท Batch MAP | โ | Maximum a posteriori with prior |
Guidance โ "How do I get there?"
| Category | Algorithms |
|---|---|
| ๐ฏ Zero-effort | ZEM/ZEV (Apollo powered descent), E-guidance |
| ๐ฏ Proportional nav | Pure PN, Augmented PN, True PN |
| ๐ฏ Polynomial | Gravity turn, Linear tangent steering, PEG |
| ๐ฏ Optimal control | LQR, iLQR, DDP |
| ๐ฏ Model predictive | MPC (shooting), MPC (collocation) |
| ๐ฏ Targeting | Single/multi shooting, Lambert, Differential correction |
| ๐ฏ Path following | Pure pursuit, Stanley, Trajectory tracking |
| ๐ฏ Energy-optimal | Min-fuel, Min-time, Min-energy transfers |
Ternary Gating โ "Can I trust this sensor?"
Every measurement passes through a forTernary validity gate before fusion:
Sensor โ Ternary Gate โ +1 (fuse) ยท 0 (hold, prediction only) ยท -1 (reject & flag)
No more binary thresholds on uncertain data. Three-valued logic propagates uncertainty honestly.
๐งฉ The Models (Pluggable Catalog)
Use a built-in model by ID, or supply your own function pointer. Built-in models ship with analytic Jacobians โ the EKF gets exact derivatives for free.
Dynamics โ "How does my system evolve?"
| Domain | Models | State dim |
|---|---|---|
| ๐ Orbital | Keplerian ยท J2 ยท CR3BP ยท Atmospheric drag | 6-7 |
| ๐ค Rigid body | 6-DOF quaternion with forces/torques | 13 |
| ๐ Ground | Bicycle ยท Ackermann ยท Differential drive | 4-5 |
| ๐ Aerial | Quadrotor 12-state ยท Fixed-wing 6-DOF | 12 |
| ๐ก Tracking | Constant-velocity ยท Constant-accel ยท Constant-turn | 2d-5 |
| ๐ Stochastic | Geometric Brownian motion ยท Ornstein-Uhlenbeck | 1 |
| โ๏ธ Scalar | Double integrator ยท Spring-mass-damper | 2-2d |
Measurements โ "What do my sensors see?"
| Category | Models |
|---|---|
| ๐ Position | Direct position ยท Range-only ยท Bearing-only ยท Range+bearing |
| ๐จ Velocity | Direct velocity ยท Doppler (range-rate) |
| ๐งญ Attitude | Magnetometer ยท Star tracker ยท Sun sensor |
| ๐ Inertial | Accelerometer ยท Gyroscope |
| ๐ก Radar/LiDAR | Range + azimuth + elevation |
| ๐ข Scalar | Direct scalar observation |
| ๐ท Image | Pinhole camera pixel coordinates |
| ๐ Relative | Relative position/velocity (rendezvous, formation) |
Custom Models
! Custom dynamics โ supply your own function pointer
call fa_ekf_predict(n, x, P, my_f_ptr, my_df_ptr, Q, dt, 0, params, np, info)
! Built-in model โ pass null, select by ID (free analytic Jacobians)
call fa_ekf_predict(n, x, P, c_null_funptr, c_null_funptr, Q, dt, FA_DYN_KEPLER, params, np, info)
๐ The Domain Layer (Space-Specific)
For orbital mechanics users. Everyone else can ignore this entirely.
| Module | Contents |
|---|---|
| ๐ช Astro | JPL ephemeris (DE405/430) ยท Planetary constants ยท Eclipse geometry ยท Hohmann/bi-elliptic transfers ยท Orbital element conversions ยท Vis-viva |
| ๐ Environment | US Standard Atmosphere 1976 ยท Exponential atmosphere ยท J2/J4/spherical harmonic gravity ยท Solar radiation pressure ยท Vincenty/Karney geodesics |
| โฑ๏ธ Time | UTC ยท TAI ยท TT ยท TDB ยท GPS ยท MJD ยท JD ยท Unix epoch ยท Leap seconds ยท Relativistic corrections (TDB-TT) |
๐ Coordinate Frames
forApollo defines what to rotate. forMath handles how (quaternions, SO(3), Lie groups).
| Category | Frames |
|---|---|
| Inertial | ECI (J2000) ยท ICRF ยท Generic inertial |
| Rotating | ECEF ยท Moon-fixed ยท Body-fixed |
| Local | NED ยท ENU ยท LVLH |
| Orbital | Perifocal (PQW) ยท RSW ยท VNC |
| Geodetic | WGS84 ยท Generic ellipsoid |
| Topocentric | Azimuth/elevation/range |
| Camera | Pinhole body-to-camera |
| Generic | User-defined rotation/quaternion |
๐ฆ Dependencies
forApollo links prebuilt archives from the forKernels ecosystem. No upstream .f90 files in this repo.
| Dependency | What forApollo uses |
|---|---|
| forMath | Linear algebra ยท ODE solvers ยท Quaternions ยท Lie groups ยท Special functions ยท Random ยท Numerical differentiation |
| forFFT | Spectral methods ยท Gravity harmonic expansion |
| forOpt | Heavy optimization for MPC ยท Trajectory targeting |
| forTernary | Three-valued logic for sensor gating ยท Mode detection ยท Estimator health |
| forGraph | Graph search for path planning ยท Multi-target assignment ยท Mission phase DAG |
deps/
โโโ formath/ { lib/*.a + zig/ }
โโโ forfft/ { lib/*.a + zig/ }
โโโ foropt/ { lib/*.a + zig/ }
โโโ forternary/ { lib/*.a + zig/ }
โโโ forgraph/ { lib/*.a + zig/ }
๐จ Build
zig build # build library (links prebuilt deps)
zig build test # run tests
zig build -Duse-prebuilt=true # use prebuilt Fortran objects
zig build -Dgenerate-prebuilt=true # regenerate prebuilt objects
zig build -Ddev=true # source-build deps (development only)
Cross-compilation
zig build -Dtarget=aarch64-linux-gnu # Jetson Orin / Raspberry Pi / Cloud
zig build # macOS (development)
๐ฏ Use Cases
The same engine powers all of these:
| Domain | What forApollo does |
|---|---|
| ๐ Spacecraft | Orbit determination ยท Powered descent ยท Rendezvous targeting |
| ๐ค Robotics | Sensor fusion ยท Path planning ยท Trajectory optimization |
| ๐ Drones | GPS/IMU/barometer fusion ยท Waypoint guidance ยท Obstacle avoidance |
| ๐ Autonomous vehicles | Multi-sensor tracking ยท Lane following ยท MPC |
| ๐ก Radar tracking | Multi-target tracking ยท Track-before-detect |
| ๐ Finance | Price process estimation ยท Regime detection ยท Mean-reversion tracking |
| ๐ง ML training | Loss curve estimation ยท Learning rate guidance ยท Convergence prediction |
| โก IoT/Edge | Sensor filtering ยท Anomaly detection ยท State monitoring |
๐ Heritage & Sources
forApollo's algorithms are drawn from flight-proven and peer-reviewed sources:
| Source | What we took |
|---|---|
| ๐ Apollo AGC (Colossus/Luminary) | Kepler propagation ยท Lambert targeting ยท Powered descent guidance ยท ESKF navigation ยท Attitude maneuvers |
| ๐ Fortran Astrodynamics Toolkit (jacobwilliams, BSD) | Orbital element conversions ยท Coordinate transforms ยท Time systems |
| ๐ฐ๏ธ NASA CFDTOOLS | Coordinate transforms ยท Grid utilities |
| ๐ญ SPICE (JPL/NAIF) | Ephemeris concepts ยท Reference frames ยท Time system architecture |
| ๐ Lee & Wright 2014 | Block-tridiagonal solver for spectral codes |
All reimplemented in modern Fortran 2008+ with iso_c_binding. No legacy code vendored.
๐๏ธ forKernels Ecosystem
forApollo is part of the forKernels high-performance computing ecosystem:
Fortran (pure math) โ Zig (safety + dispatch) โ Any language (user API)
| Library | Domain | Relationship |
|---|---|---|
| forMath | Mathematics | forApollo's foundation โ linalg, ODE, quaternions |
| forFFT | Spectral methods | Gravity harmonic expansion, spectral analysis |
| forOpt | Optimization | MPC solvers, trajectory targeting |
| forTernary | Three-valued logic | Sensor gating, mode detection, estimator health |
| forGraph | Graph algorithms | Path planning, multi-target assignment |
| forNav | Robotics navigation | Consumes forApollo for universal estimation |
| forSim | Physics simulation | Uses forApollo for rigid body state integration |
| forCV | Computer vision | Feeds measurements to forApollo's estimators |
| forEdge | Edge robotics | Bundles forApollo + forNav + forCV for deployment |
"The math doesn't care what the state vector represents."
Copyright ยฉ The Fantastic Planet โ By David Clabaugh
Built with ๐ง Zig + ๐ฌ Fortran | Zero dependencies | Edge-deployable | OpenMP parallel