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

131 lines
2.1 KiB
C++
Raw Normal View History

2017-12-09 12:23:18 -05:00
//
// FILE: AverageAngle.cpp
// AUTHOR: Rob Tillaart
2023-10-17 13:38:03 -04:00
// VERSION: 0.2.1
2022-12-07 14:30:13 -05:00
// DATE: 2017-11-21
2023-10-17 13:38:03 -04:00
// PURPOSE: Arduino library to calculate correctly the average of multiple 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"
2023-02-01 07:53:15 -05:00
const float AA_OVERFLOW_THRESHOLD = 10000;
2017-12-09 12:23:18 -05:00
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
}
2023-02-01 07:53:15 -05:00
float dx = cos(alpha);
float dy = sin(alpha);
if (length != 1.0)
{
dx *= length;
dy *= length;
}
_sumx += dx;
_sumy += dy;
_error = AVERAGE_ANGLE_OK;
if ((abs(_sumx) > AA_OVERFLOW_THRESHOLD) || (abs(_sumy) > AA_OVERFLOW_THRESHOLD))
{
_error = AVERAGE_ANGLE_OVERFLOW;
}
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()
{
2023-02-01 07:53:15 -05:00
_sumx = 0;
_sumy = 0;
2017-12-09 12:23:18 -05:00
_count = 0;
2023-02-01 07:53:15 -05:00
_error = AVERAGE_ANGLE_OK;
2017-12-09 12:23:18 -05:00
}
2021-10-18 15:44:20 -04:00
2022-10-29 07:59:29 -04:00
uint32_t AverageAngle::count()
{
return _count;
2023-02-01 07:53:15 -05:00
}
2022-10-29 07:59:29 -04:00
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
}
2023-02-01 07:53:15 -05:00
// error reporting
if (isnan(angle))
{
_error = AVERAGE_ANGLE_SINGULARITY;
}
else
{
_error = AVERAGE_ANGLE_OK;
}
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;
}
2023-02-01 07:53:15 -05:00
int AverageAngle::lastError()
{
int e = _error;
_error = AVERAGE_ANGLE_OK;
return e;
}
// -- END OF FILE --
2022-10-29 07:59:29 -04:00