Microchip® Advanced Software Framework

common/applications/sensors/compass_calibration/main.c File Reference

Common Sensor Service Compass Calibration Example.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include <stdio.h>
#include <asf.h>
#include <led.h>


#define ALL_LEDS   (LED1 | LED2 | LED3 | LED4)
#define NUM_BLINK_LEDS   (4)
#define PROMPT_LED   LED4 /* Blink while waiting for user input */
#define SWITCH_PRESSED   gpio_pin_is_low(GPIO_PUSH_BUTTON_0)


int main (void)
 Compass / magnetometer calibration application. More...
static void prompt_user (char *prompt_string)
 User prompt routine. More...


static const uint32_t led_array [NUM_BLINK_LEDS] = {LED1, LED2, LED3, LED4}

#define ALL_LEDS   (LED1 | LED2 | LED3 | LED4)

Referenced by main(), and prompt_user().

#define NUM_BLINK_LEDS   (4)

Referenced by main().

#define PROMPT_LED   LED4 /* Blink while waiting for user input */

Referenced by prompt_user().

#define SWITCH_PRESSED   gpio_pin_is_low(GPIO_PUSH_BUTTON_0)

Referenced by prompt_user().

int main ( void  )

Compass / magnetometer calibration application.

This application illustrates the use of the sensor_calibrate() function for compass/magnetometer calibration. It prompts the user (via serial output) to manipulate the sensor board and press a button to continue.

The calibration process is used to correct for fixed magnetic forces present on the board where the compass is mounted. If uncorrected, these fixed forces will prevent accurate measurement of the actual external magnetic forces (e.g. magnetic North).

The calibration sequence requires three steps. During each step, the board is placed in a specific orientation, and the user presses the button on the board to trigger a compass sensor reading.

The three orientations are:

  1. Board lying flat (on a table, for example).
  2. Board rotated 180 degrees (end-for-end), still lying flat.
  3. Board flipped (inverted) so that the opposite side is facing up.

After Step 3 is completed, the calibration values for the sensor are calculated and are written to non-volatile (flash) memory on the microcontroller. These values will continue to be used for future compass heading readings.

References ALL_LEDS, delay_ms, sensor_data_t::heading, led_array, MANUAL_CALIBRATE, NULL, NUM_BLINK_LEDS, prompt_user(), sensor_data_t::scaled, sensor_attach(), sensor_calibrate(), SENSOR_ERR_IO, sensor_get_heading(), sensor_platform_init(), and SENSOR_TYPE_COMPASS.

static void prompt_user ( char *  prompt_string)

User prompt routine.

This routine prompts the user to press the button and then waits for him to do so.

References ALL_LEDS, delay_ms, PROMPT_LED, and SWITCH_PRESSED.

Referenced by main().

const uint32_t led_array[NUM_BLINK_LEDS] = {LED1, LED2, LED3, LED4}

Referenced by main().