65 lines
1.2 KiB
C++
Raw Normal View History

2021-01-29 12:31:58 +01:00
//
// FILE: runningAngle.cpp
// AUTHOR: Rob Tillaart
2021-12-28 09:50:06 +01:00
// VERSION: 0.1.3
2021-01-29 12:31:58 +01:00
// PURPOSE: Library to average angles by means of low pass filtering with wrapping.
// URL: https://github.com/RobTillaart/runningAngle
// RELATED: https://github.com/RobTillaart/AverageAngle
//
// HISTORY:
2021-05-28 13:43:31 +02:00
// 0.1.0 2020-10-28 initial version (based on code of Edgar Bonet)
2021-12-28 09:50:06 +01:00
// 0.1.1 2021-01-07 Arduino-CI + unit test
2021-05-28 13:43:31 +02:00
// 0.1.2 2021-05-27 Arduino-lint
2021-12-28 09:50:06 +01:00
// 0.1.3 2021-12-28 update library.json, readme, license, minor edits
2021-01-29 12:31:58 +01:00
#include "runningAngle.h"
runningAngle::runningAngle(const enum AngleType type)
{
_type = type;
reset();
}
void runningAngle::reset()
{
_average = 0;
_weight = 0.80;
_reset = true;
}
2021-12-28 09:50:06 +01:00
2021-01-29 12:31:58 +01:00
float runningAngle::add(float angle)
{
if (_reset)
{
_average = angle;
_reset = false;
}
else
{
_average = wrap(_average + _weight * wrap(angle - _average));
}
return _average;
}
float runningAngle::wrap(float angle)
{
if (_type == DEGREES) {
while (angle < -180) angle += 360;
while (angle >= 180) angle -= 360;
} else {
while (angle < -PI) angle += TWO_PI;
while (angle >= PI) angle -= TWO_PI;
}
return angle;
}
2021-05-28 13:43:31 +02:00
2021-01-29 12:31:58 +01:00
// -- END OF FILE --
2021-12-28 09:50:06 +01:00