GY-63_MS5611/libraries/runningAngle/runningAngle.cpp

66 lines
1.1 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: runningAngle.cpp
// AUTHOR: Rob Tillaart
2022-11-23 11:07:12 -05:00
// VERSION: 0.1.5
2021-01-29 06:31:58 -05: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
#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 03:50:06 -05:00
2021-01-29 06:31:58 -05: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)
{
2022-05-29 05:19:11 -04:00
if (_type == DEGREES)
{
2021-01-29 06:31:58 -05:00
while (angle < -180) angle += 360;
while (angle >= 180) angle -= 360;
2022-05-29 05:19:11 -04:00
}
else if (_type == RADIANS)
{
2021-01-29 06:31:58 -05:00
while (angle < -PI) angle += TWO_PI;
while (angle >= PI) angle -= TWO_PI;
}
2022-11-23 11:07:12 -05:00
else // GRADIANS
2022-05-29 05:19:11 -04:00
{
while (angle < -200) angle += 400;
while (angle >= 200) angle -= 400;
}
2021-01-29 06:31:58 -05:00
return angle;
}
2021-05-28 07:43:31 -04:00
2022-11-23 11:07:12 -05:00
// -- END OF FILE --
2021-12-28 03:50:06 -05:00