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

97 lines
1.6 KiB
C++
Raw Normal View History

2017-12-09 12:23:18 -05:00
//
// FILE: AverageAngle.cpp
// AUTHOR: Rob Tillaart
2022-12-07 14:30:13 -05:00
// VERSION: 0.1.9
// DATE: 2017-11-21
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
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 )
{
2022-10-29 07:59:29 -04: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 )
{
2022-10-29 07:59:29 -04:00
alpha *= GRAD_TO_RAD; // (PI / 200.0);
2021-10-18 15:44:20 -04:00
}
_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
2022-10-29 07:59:29 -04:00
uint32_t AverageAngle::count()
{
return _count;
};
2017-12-09 12:23:18 -05:00
float AverageAngle::getAverage()
{
float angle = atan2(_sumy, _sumx);
2022-10-29 07:59:29 -04:00
if (angle < 0) angle += TWO_PI; // (PI * 2);
2017-12-09 12:23:18 -05:00
if (_type == AverageAngle::DEGREES )
{
2022-10-29 07:59:29 -04:00
angle *= RAD_TO_DEG; // (180.0 / PI);
2021-10-18 15:44:20 -04:00
}
else if (_type == AverageAngle::GRADIANS )
{
2022-10-29 07:59:29 -04:00
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
2022-10-29 07:59:29 -04:00
AverageAngle::AngleType AverageAngle::type()
{
return _type;
}
bool AverageAngle::setType(const enum AngleType type)
{
if (type > GRADIANS) return false;
_type = type;
return true;
}
2020-11-27 05:10:47 -05:00
// -- END OF FILE --
2022-10-29 07:59:29 -04:00