Accel to angle
This library provides functionality to convert accelerometer data into pitch and roll angles. You can use the calculated data further to check for the motion detection of the device.
Overview
This library processes raw accelerometer data and computes rotation values based on the input. Using an accelerometer instead of a gyroscope decreases the power consumption and cost.
The library supports multiple axes and you can configure it to work with different accelerometer sensors. You can use it simultaneously or independently for rotation calculation and threshold-based motion detection.
Implementation
The library is divided into the following two parts:
The core library that handles the conversion of accelerometer data to rotation data.
The motion detection module that uses the rotation data to determine if motion has occurred based on thresholds.
You can use both parts independently or together, depending on the application requirements.
The library also supports optional filtering of the accelerometer data before conversion. You can use the already implemented filter or create your own custom filter that is compatible with the library API.
Configuration
To enable the library, set the CONFIG_ACCEL_TO_ANGLE Kconfig option to y in the project configuration file prj.conf.
Usage
To use the library, perform the following steps:
(Optional) If you want to use filtering in the data conversion pipeline, you must define the filter instance. Ensure that the filter structure is always available during library usage. The contents of it must never be accessed or used directly by the application. You have the following two options to define a filter:
Use one of the already implemented filters that are delivered together with the library. Currently, the library only supports the exponential moving average (EMA) filter. You can allocate the EMA filter instance with the help of the
ACCEL_TO_ANGLE_FILTER_EMA_DEFINEmacro that is available in theinclude/accel_to_angle/filter/ema.hheader.Use a custom filter that is compatible with the library API. For an example on how to develop a custom filter, see the Accel to angle EMA filter.
Define and initialize the library context structure by calling the
ACCEL_TO_ANGLE_CTX_DEFINEmacro. The context structure must never be accessed or used directly by the application. If you want to use optional filtering step, pass the filter instance to theACCEL_TO_ANGLE_CTX_DEFINEmacro. Otherwise, you can use the NULL value.(Optional) Reconfigure the filter with the
accel_to_angle_filter_set()function to change a filter.Fetch accelerometer data from the sensor and populate the sensor values array.
Call the
accel_to_angle_calc()function to convert accelerometer data to rotation data.Use the calculated rotation data in your application.
(Optional) Call the
accel_to_angle_diff_check()function to check if the change in rotation exceeds thresholds for motion detection.(Optional) Use the
accel_to_angle_state_clean()function to reset the accumulated state of the motion difference and filter state (if used).
Limitations
Due to the nature of mathematical calculations involved in converting accelerometer data to rotation data, the library can only calculate pitch and roll angles (without yaw).
Samples using the library
The Bluetooth Fast Pair: Locator tag sample uses this library.
Dependencies
This library has the following dependencies:
Zephyr Sensors
Standard C library
math.h
API documentation
The library uses the following API elements:
Accel to angle library
include/accel_to_angle/accel_to_angle.hlib/accel_to_angle/accel_to_angle.cAccel to angle EMA filter
include/accel_to_angle/filter/ema.hlib/accel_to_angle/filter_ema.c