A simple implementation of the world magnetic model suitable for microcontrollers / embedded devices
The
World Magnetic Model (WMM) is a numerical approximation of the Earth's magnetic field
and can be employed to compute e.g. magnetic declination as a function of GPS coordinates,
altitude and time in order to convert between geographic (GPS) headings and magnetic headings,
which is useful for example in aviation.
NOAA provides
regularly updated WMM, where the magnetic
field is obtained as a gradient of a potential, which is expandedd in spherical harmonic functions up to 12-th order.
They provide the expansion coefficients as well as a reference implementation.
I found this reference implementation however to be unnecessarily cumbersome and opaque and entangled code so for the use in
a microcontroller, I wrote from scratch my own implementation, which is simplified by omiting the geoid to WGS84 ellipsoid altitude correction
since the geoid data is inconveniently large for use in microcontrollers
and this approximation is good enough for practical purposes in simple avionics.
It also eliminates any file I/O to facilitate direct use in embedded devices, like
my experimental simple avionics device.
The resulting implementation has mere 400 lines of C code, including the WMM coefficients and I provide it here
under the GNU GPLv3 license:
wmm.zip
With a compile-time switch the code can use either floats or doubles and
can also be compiled as a standalone program on a regular linux computer using an #ifdef.
Later I found that there is already
another port of WMM for embedded devices,
which seems however to be based on the original NOAA code with all the unneccessary struct definitions etc. which just bloat the code,
so that is has
about five times higher number of code lines than my one. Also my code enables much simpler update of the coefficients from
future distributions of WMM.
My Electronics page
My hobby page
My main page with e-mail contact
TOP of my family pages