World Magnetic Model Library

Functionality and structures used to determine the geomagnetic field with respect to the device's location on Earth.

This file defines the function wmm_get_geomagnetic_field(), which returns the geomagnetic field for a specific location at a specific time. The location is specified by a wmm_location_t structure and the time is passed as a tm structure. The magnetic field is returned in a wmm_geomagnetic_field structure.

The wmm_get_geomagnetic_field() function is an expensive function to call, and as such, should be called sparingly.

The following sample demonstrates how to apply the effect of a magnetic field to a sensor:

/* Call when a location change occurs. */
wmm_get_geomagnetic_field(loc, dt, &gmf);

/* Apply the declination from the geomagnetic field when a sensor event occurs. */
if (bps_event_get_domain(event) == sensor_domain) {
    if (bps_event_get_code(event) == SENSOR_AZIMUTH_PITCH_ROLL_READING) {

        float azimuth, pitch, roll;

        sensor_event_get_apr(event, &azimuth, &pitch, &roll);

        // Correct for true north
        wmm_geomagnetic_field_t gmf;
        azimuth += gmf.declination_deg;

        // Handle new direction and orientation angle here 

Please note that you must link against the wmm library to use the wmm_get_geomagnetic_field() function.

Last modified: 2014-05-14

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus