๐Ÿš€ forApollo

April 3, 2026 ยท View on GitHub

๐Ÿš€ forApollo

The math that landed on the Moon โ€” generalized for everything.

Fortran Zig License Part of


A universal state estimation, navigation, and guidance engine. Born from Apollo. Built for everything.


forApollo heritage

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

LayerFilesPurpose
๐Ÿ”ง Engineestimate ยท propagate ยท guidance ยท coordsDomain-agnostic core. Works with any state vector.
๐Ÿงฉ Modelsdynamics ยท observePluggable catalog of dynamics & measurement models.
๐ŸŒŒ Domainastro ยท environ ยท timeSpace-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?"

AlgorithmHeritageWhat it does
๐ŸŸข KFKalman 1960Optimal for linear systems
๐ŸŸข EKFApollo AGCJacobian linearization โ€” the workhorse
๐ŸŸข IEKFโ€”Re-linearizes at updated state for tighter estimates
๐Ÿ”ด ESKFApollo flight softwareError-state filter โ€” what actually flew to the Moon
๐ŸŸฃ UKFJulier & Uhlmann 1997Sigma-point โ€” no Jacobians needed
๐Ÿ”ต SR-EKF / SR-UKFโ€”Square-root variants โ€” numerically bulletproof
๐ŸŸก IF / EIFโ€”Information form โ€” natural for multi-sensor fusion
๐ŸŸ  SIR ParticleGordon et al. 1993Bootstrap โ€” handles any nonlinearity
๐ŸŸ  Auxiliary ParticlePitt & Shephard 1999Better proposal distribution
๐ŸŸ  Rao-Blackwellizedโ€”Hybrid: particles for nonlinear, Kalman for linear substates
โšช RTS SmootherRauch et al. 1965Backward 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?"

CategoryAlgorithms
๐ŸŽฏ Zero-effortZEM/ZEV (Apollo powered descent), E-guidance
๐ŸŽฏ Proportional navPure PN, Augmented PN, True PN
๐ŸŽฏ PolynomialGravity turn, Linear tangent steering, PEG
๐ŸŽฏ Optimal controlLQR, iLQR, DDP
๐ŸŽฏ Model predictiveMPC (shooting), MPC (collocation)
๐ŸŽฏ TargetingSingle/multi shooting, Lambert, Differential correction
๐ŸŽฏ Path followingPure pursuit, Stanley, Trajectory tracking
๐ŸŽฏ Energy-optimalMin-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?"

DomainModelsState dim
๐ŸŒŒ OrbitalKeplerian ยท J2 ยท CR3BP ยท Atmospheric drag6-7
๐Ÿค– Rigid body6-DOF quaternion with forces/torques13
๐Ÿš— GroundBicycle ยท Ackermann ยท Differential drive4-5
๐Ÿš AerialQuadrotor 12-state ยท Fixed-wing 6-DOF12
๐Ÿ“ก TrackingConstant-velocity ยท Constant-accel ยท Constant-turn2d-5
๐Ÿ“ˆ StochasticGeometric Brownian motion ยท Ornstein-Uhlenbeck1
โš™๏ธ ScalarDouble integrator ยท Spring-mass-damper2-2d

Measurements โ€” "What do my sensors see?"

CategoryModels
๐Ÿ“ PositionDirect position ยท Range-only ยท Bearing-only ยท Range+bearing
๐Ÿ’จ VelocityDirect velocity ยท Doppler (range-rate)
๐Ÿงญ AttitudeMagnetometer ยท Star tracker ยท Sun sensor
๐Ÿ“ InertialAccelerometer ยท Gyroscope
๐Ÿ“ก Radar/LiDARRange + azimuth + elevation
๐Ÿ”ข ScalarDirect scalar observation
๐Ÿ“ท ImagePinhole camera pixel coordinates
๐Ÿ”— RelativeRelative 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.

ModuleContents
๐Ÿช AstroJPL ephemeris (DE405/430) ยท Planetary constants ยท Eclipse geometry ยท Hohmann/bi-elliptic transfers ยท Orbital element conversions ยท Vis-viva
๐ŸŒ EnvironmentUS Standard Atmosphere 1976 ยท Exponential atmosphere ยท J2/J4/spherical harmonic gravity ยท Solar radiation pressure ยท Vincenty/Karney geodesics
โฑ๏ธ TimeUTC ยท 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).

CategoryFrames
InertialECI (J2000) ยท ICRF ยท Generic inertial
RotatingECEF ยท Moon-fixed ยท Body-fixed
LocalNED ยท ENU ยท LVLH
OrbitalPerifocal (PQW) ยท RSW ยท VNC
GeodeticWGS84 ยท Generic ellipsoid
TopocentricAzimuth/elevation/range
CameraPinhole body-to-camera
GenericUser-defined rotation/quaternion

๐Ÿ“ฆ Dependencies

forApollo links prebuilt archives from the forKernels ecosystem. No upstream .f90 files in this repo.

DependencyWhat forApollo uses
forMathLinear algebra ยท ODE solvers ยท Quaternions ยท Lie groups ยท Special functions ยท Random ยท Numerical differentiation
forFFTSpectral methods ยท Gravity harmonic expansion
forOptHeavy optimization for MPC ยท Trajectory targeting
forTernaryThree-valued logic for sensor gating ยท Mode detection ยท Estimator health
forGraphGraph 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:

DomainWhat forApollo does
๐Ÿš€ SpacecraftOrbit determination ยท Powered descent ยท Rendezvous targeting
๐Ÿค– RoboticsSensor fusion ยท Path planning ยท Trajectory optimization
๐Ÿš DronesGPS/IMU/barometer fusion ยท Waypoint guidance ยท Obstacle avoidance
๐Ÿš— Autonomous vehiclesMulti-sensor tracking ยท Lane following ยท MPC
๐Ÿ“ก Radar trackingMulti-target tracking ยท Track-before-detect
๐Ÿ“ˆ FinancePrice process estimation ยท Regime detection ยท Mean-reversion tracking
๐Ÿง  ML trainingLoss curve estimation ยท Learning rate guidance ยท Convergence prediction
โšก IoT/EdgeSensor filtering ยท Anomaly detection ยท State monitoring

๐Ÿ“œ Heritage & Sources

forApollo's algorithms are drawn from flight-proven and peer-reviewed sources:

SourceWhat 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 CFDTOOLSCoordinate transforms ยท Grid utilities
๐Ÿ”ญ SPICE (JPL/NAIF)Ephemeris concepts ยท Reference frames ยท Time system architecture
๐Ÿ“– Lee & Wright 2014Block-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)
LibraryDomainRelationship
forMathMathematicsforApollo's foundation โ€” linalg, ODE, quaternions
forFFTSpectral methodsGravity harmonic expansion, spectral analysis
forOptOptimizationMPC solvers, trajectory targeting
forTernaryThree-valued logicSensor gating, mode detection, estimator health
forGraphGraph algorithmsPath planning, multi-target assignment
forNavRobotics navigationConsumes forApollo for universal estimation
forSimPhysics simulationUses forApollo for rigid body state integration
forCVComputer visionFeeds measurements to forApollo's estimators
forEdgeEdge roboticsBundles 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