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

86 lines
1.8 KiB
C++
Raw Normal View History

2017-12-09 12:23:18 -05:00
//
// FILE: AverageAngle.cpp
// AUTHOR: Rob Tillaart
2021-12-13 14:51:57 -05:00
// VERSION: 0.1.7
2017-12-09 12:23:18 -05:00
// PURPOSE: class for averaging angles
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/AverageAngle
2017-12-09 12:23:18 -05:00
//
2021-10-18 15:44:20 -04:00
// HISTORY:
// 0.1.0 2017-11-21 initial version
// 0.1.1 2017-12-09 fixed negative values of average
// 0.1.2 2018-03-30 added getAverageLength, getTotalLength + zero-test
// 0.1.3 2020-03-26 #pragma once; removed pre 1.00 support; readme.md
// 0.1.4 2020-05-27 update library.json
2021-12-13 14:51:57 -05:00
// 0.1.5 2020-12-12 added Arduino-CI, unit tests, minor refactor.
2021-10-18 15:44:20 -04:00
// 0.1.6 2021-10-18 update Arduino-CI, add GRADIANS
2021-12-13 14:51:57 -05:00
// 0.1.7 2021-12-13 update library.json, fix badges, license, readme
2021-10-18 15:44:20 -04:00
2017-12-09 12:23:18 -05:00
#include "AverageAngle.h"
AverageAngle::AverageAngle(const enum AngleType type)
{
_type = type;
reset();
}
2021-10-18 15:44:20 -04:00
2021-01-29 06:31:58 -05:00
uint32_t AverageAngle::add(float alpha, float length)
2017-12-09 12:23:18 -05:00
{
if (_type == AverageAngle::DEGREES )
{
2021-01-29 06:31:58 -05:00
alpha *= DEG_TO_RAD; // (PI / 180.0);
2017-12-09 12:23:18 -05:00
}
2021-10-18 15:44:20 -04:00
else if (_type == AverageAngle::GRADIANS )
{
alpha *= GRAD_TO_RAD; // (PI / 200.0);
}
_sumx += (cos(alpha) * length);
_sumy += (sin(alpha) * length);
2017-12-09 12:23:18 -05:00
_count++;
2021-01-29 06:31:58 -05:00
return _count;
2017-12-09 12:23:18 -05:00
}
2021-10-18 15:44:20 -04:00
2017-12-09 12:23:18 -05:00
void AverageAngle::reset()
{
_sumx = 0;
_sumy = 0;
_count = 0;
}
2021-10-18 15:44:20 -04:00
2017-12-09 12:23:18 -05:00
float AverageAngle::getAverage()
{
float angle = atan2(_sumy, _sumx);
2021-01-29 06:31:58 -05:00
if (angle < 0) angle += TWO_PI; // (PI * 2);
2017-12-09 12:23:18 -05:00
if (_type == AverageAngle::DEGREES )
{
2021-10-18 15:44:20 -04:00
angle *= RAD_TO_DEG; // (180.0 / PI);
}
else if (_type == AverageAngle::GRADIANS )
{
angle *= RAD_TO_GRAD; // (200.0 / PI);
2017-12-09 12:23:18 -05:00
}
return angle;
}
2021-10-18 15:44:20 -04:00
float AverageAngle::getTotalLength()
{
if (_count == 0) return 0;
return hypot(_sumy, _sumx);
}
2021-10-18 15:44:20 -04:00
float AverageAngle::getAverageLength()
{
if (_count == 0) return 0;
return hypot(_sumy, _sumx) / _count;
}
2017-12-09 12:23:18 -05:00
2021-10-18 15:44:20 -04:00
2020-11-27 05:10:47 -05:00
// -- END OF FILE --