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.21 KB
/
Copy pathAverageAngle.cpp
File metadata and controls
68 lines (59 loc) · 1.21 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.2
// PURPOSE: class for averaging angles
// URL: https://github.com/RobTillaart/Arduino
//
// 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
#include "AverageAngle.h"
/////////////////////////////////////////////////////
//
// PUBLIC
//
AverageAngle::AverageAngle(const enum AngleType type)
{
_type = type;
reset();
}
void AverageAngle::add(float alpha, float length)
{
if (_type == AverageAngle::DEGREES )
{
alpha *= (PI / 180.0);
}
_sumx += (cos(alpha) * length);
_sumy += (sin(alpha) * length);
_count++;
}
void AverageAngle::reset()
{
_sumx = 0;
_sumy = 0;
_count = 0;
}
float AverageAngle::getAverage()
{
float angle = atan2(_sumy, _sumx);
if (angle < 0) angle += (PI*2);
if (_type == AverageAngle::DEGREES )
{
angle *= (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