| FRDM-KW40Z Demo Software Reference Manual Rev 0.0 NXP Semiconductors |

Functions
e-Compass Driver
e-Compass

## Overview

e-Compass Driver implements the functions to calculate the compass heading based on the accelerometer and magnetometer data.

All functions are taken from and explained in the NXP application note AN4248 "Implementing a Tilt-Compansated eCompass using Accelerometer and Magnetometer Sensors"

Warning
  The magnetometer sensor must be calibrated before calculating the compass heading by moving the board in all directions while the magnetometer is enabled.

Version 1.0

This driver version does not include these features:

1. Calibration function

## Functions
int16_t | ecompass_calculate_heading (int16_t iBpx, int16_t iBpy, int16_t iBpz, int16_t iGpx, int16_t iGpy, int16_t iGpz)
void | ecompass_calibrate_hard_iron (int16_t xAxisOffset, int16_t yAxisOffset, int16_t zAxisOffset)

## Function Documentation

int16_t ecompass_calculate_heading | ( | int16_t | iBpx,
|  | int16_t | iBpy,
|  | int16_t | iBpz,
|  | int16_t | iGpx,
|  | int16_t | iGpy,
|  | int16_t | iGpz
| ) |  |

Calculates the current compass heading (in degrees) based on the current accelerometer and magnetometer data.

Note
  The accelerometer and magnetometer input parameters must be passed using the polarity and directions in the NED coordinate system. If your sensors have a different position on the board, adjust the measurements obtained to fit the NED system.

Parameters

  [in] | iBpx | Magnetometer X-axis reading.
  [in] | iBpy | Magnetometer Y-Axis reading.
  [in] | iBpz | Magnetometer Z-Axis reading.
  [in] | iGpx | Accelerometer X-axis reading.
  [in] | iGpy | Accelerometer Y-axis reading.
  [in] | iGpz | Accelerometer Z-axis reading.

Returns
  Compass heading position (in degrees, from -180 to 180).

Warning
  The magnetometer must be calibrated before using this function for accurate results.

void ecompass_calibrate_hard_iron | ( | int16_t | xAxisOffset,
|  | int16_t | yAxisOffset,
|  | int16_t | zAxisOffset
| ) |  |

Calibrates the magnetometer by determining the current hard iron offset.

Note
  This function must be periodically called during the program execution to constantly calibrate the magnetometer sensor. It is not neccesary to call this function if the magnetometer sensor has the autocalibration feature.

Parameters

  [in] | xAxisOffset | Magnetometer X-axis reading.
  [in] | yAxisOffset | Magnetometer Y-Axis reading.
  [in] | zAxisOffset | Magnetometer Z-Axis reading.

Returns
  void