GY-63_MS5611/libraries/runningAngle
2021-12-28 09:50:06 +01:00
..
.github/workflows 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
examples 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
test 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
.arduino-ci.yml 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
keywords.txt 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
library.json 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
library.properties 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
LICENSE 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
README.md 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
runningAngle.cpp 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00
runningAngle.h 0.1.3 runningAngle 2021-12-28 09:50:06 +01:00

Arduino CI Arduino-lint JSON check License: MIT GitHub release

runningAngle

Arduino library to calculate the running average of a series of angles.

Description

This library provides a class, runningAngle, that computes an exponentially weighted running average of a series of angles, such as compass readings. It is aware of how angles wrap modulo 360°.

The exponentially weighted running average is a type of running average that averages all the past inputs with weights that decrease exponentially as the inputs get older. It is a type of digital filter very commonly used for smoothing noisy sensor readings. It is more memory efficient than the simple running average, while providing similar smoothing capability.

Computing an “average” of angular data, such as headings, is inherently an ambiguous problem. For example, given the headings 350° and 10°, there are two possible “averages” that lie halfway between them, namely 0° and 180°. This library assumes that the “correct” average is the one that lies in the middle of the shorter arc joining the initial headings, thus 0°. This is the right choice for smoothing noisy sensor readings, assuming the peak-to-peak amplitude of the noise is not too large. Note that the regular average of the numbers 350 and 10 is 180, which is not the result we expect when averaging angles.

This library is a spin off of AverageAngle, based on an issue raised by Edgar Bonet.

Smoothing coefficient

The output of the filter is efficiently computed as a weighted average of the current input and the previous output:

output = α × current_input + (1 α) × previous_output

The smoothing coefficient, α, is the weight of the current input in the average. It is called “weight” within the library, and should be set to a value between 0.001 and 1. The larger the weight, the weaker the smoothing. A weight α = 1 provides no smoothing at all, as the filter's output is a just a copy of its input.

The filter has a smoothing performance similar to a simple running average over N = 2/α  1 samples. For example, α = 0.2 is similar to averaging over the last 9 samples.

Usage

First, create a filter as an instance of runningAngle:

runningAngle my_filter(runningAngle::DEGREES);

The parameter of the constructor should be either runningAngle::DEGREES or runningAngle::RADIANS. It is optional and defaults to degrees.

Then, set the “weight” smoothing coefficient:

my_filter.setWeight(0.2);

Finally, within the main sketch's loop, feed the raw angle readings to the filter's add() method:

float heading = get_a_compass_reading_somehow();
float smoothed_heading = my_filter.add(heading);

The method returns the smoothed reading within ± 180° (i.e. ± π rad).

See the “examples” folder for a more complete example.

Interface

  • enum AngleType { DEGREES, RADIANS } used to get type math right.
  • runningAngle(AngleType type = DEGREES) constructor, default to DEGREES
  • float add(float angle) adds value using a certain weight, except the first value after a reset is used as initial value. The add() function returns the new average.
  • void reset() resets the internals and start over again.
  • float getAverage() returns the current average value.
  • void setWeight(float weight) sets the weight of the new added value. Value will be constrained between 0.001 and 1.00
  • float getWeight() returns the current set weight.
  • AngleType type() returns DEGREES or RADIANS.
  • float wrap(float angle) wraps an angle to <-180..+180> <-PI..PI> depending on the type set.

Operation

See examples

Future

  • get some numbers about the noise in the angles (stats on the delta?)
  • add GRADIANS as type (low prio)