forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageAngle.cpp
More file actions
68 lines (59 loc) · 1.42 KB
/
Copy pathAverageAngle.cpp
File metadata and controls
68 lines (59 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// FILE: AverageAngle.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// PURPOSE: class for averaging angles
// URL: https://github.com/RobTillaart/AverageAngle
//
// 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
// 0.1.5 2020-12-12 added arduino-CI, unit tests, minor refactor.
#include "AverageAngle.h"
AverageAngle::AverageAngle(const enum AngleType type)
{
_type = type;
reset();
}
uint32_t AverageAngle::add(float alpha, float length)
{
if (_type == AverageAngle::DEGREES )
{
alpha *= DEG_TO_RAD; // (PI / 180.0);
}
_sumx += (cos(alpha) * length);
_sumy += (sin(alpha) * length);
_count++;
return _count;
}
void AverageAngle::reset()
{
_sumx = 0;
_sumy = 0;
_count = 0;
}
float AverageAngle::getAverage()
{
float angle = atan2(_sumy, _sumx);
if (angle < 0) angle += TWO_PI; // (PI * 2);
if (_type == AverageAngle::DEGREES )
{
angle *= RAD_TO_DEG; // (180.0 / PI);
}
return angle;
}
float AverageAngle::getTotalLength()
{
if (_count == 0) return 0;
return hypot(_sumy, _sumx);
}
float AverageAngle::getAverageLength()
{
if (_count == 0) return 0;
return hypot(_sumy, _sumx) / _count;
}
// -- END OF FILE --