Skip to content

forKernels/forApollo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ 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

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

About

Apollo-era GNC algorithms reimplemented in modern Fortran+Zig. Orbital mechanics, Kalman filtering, trajectory optimization, powered descent guidance. Drawn from AGC flight software and NASA astrodynamics heritage. Zero dependencies, OpenMP parallel, edge-deployable. The same math that landed on the Moon, running on a Jetson.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors