83 lines
2.7 KiB
Markdown
Raw Normal View History

2021-01-29 12:31:58 +01:00
[![Arduino CI](https://github.com/RobTillaart/AverageAngle/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AverageAngle/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/AverageAngle.svg?maxAge=3600)](https://github.com/RobTillaart/AverageAngle/releases)
2020-11-27 11:10:47 +01:00
# AverageAngle
2021-01-29 12:31:58 +01:00
2020-11-27 11:10:47 +01:00
Arduino library to calculate correctly the average of multiple angles
2021-10-18 21:44:20 +02:00
2020-11-27 11:10:47 +01:00
## Description
AverageAngle is an class to calculate the average of angles.
This is especially useful when angles are around 0 degrees,
e.g. from a compass sensor or the resultant of a track.
Example, the average angle of 359 and 1 is 0, not 179 (most of the time)
Furthermore the AverageAngle can also include the **length (weight)** of the angle
as if it is a vector. Default this length is set to 1 so all angles are by default of
equal weight.
2021-10-18 21:44:20 +02:00
Example: The average angle of 359 (length = 2) and 1(length = 1) is 359.something not zero.
2020-11-27 11:10:47 +01:00
2021-01-29 12:31:58 +01:00
**See also runningAngle class**.
2021-10-18 21:44:20 +02:00
2021-01-29 12:31:58 +01:00
## Interface
2021-10-18 21:44:20 +02:00
- **enum AngleType { DEGREES, RADIANS, GRADIANS } ** idem. 100 GRADIANS == 90 DEGREES.
2021-01-29 12:31:58 +01:00
- **AverageAngle(AngleType type = DEGREES)** constructor defaults to degrees.
2021-10-18 21:44:20 +02:00
- **uint32_t add(float alpha, float length = 1.0)** add a new angle, optional with length. **add()** returns the number of elements (count).
- **void reset()** clears internal buffers.
- **uint32_t count()** the amount of angles added.
- **float getAverage() ** returns the average.
- **float getTotalLength() ** the length of the resulting 'angle' when we see them as vectors.
- **float getAverageLength()** returns the average length of the angles added.
- **AngleType type()** returns DEGREES = 0, RADIANS = 1 or GRADIANS = 2.
- **void setType(AngleType type)** changes type DEGREES, RADIANS or GRADIANS.
## Gradians
Gradians a.k.a. gon, is a less often used unit for angles. There are 100 gradians in a right angle. A full circle = 400 gradians.
https://en.wikipedia.org/wiki/Gradian
2021-01-29 12:31:58 +01:00
2020-11-27 11:10:47 +01:00
## Operation
If you want to average 5 compass readings you can just add the angles and
do not use the length parameter.
```
AA.reset();
for (int i = 0; i < 5; i++)
{
AA.add(compass.readHeading());
2021-10-18 21:44:20 +02:00
delay(100); // e.g. compass read needs some time
2020-11-27 11:10:47 +01:00
}
Serial.println(AA.getAverage());
```
2021-10-18 21:44:20 +02:00
If you want to average a track, e.g. 5 steps North, 3 steps west etc,
you need to include the length of each step.
2020-11-27 11:10:47 +01:00
```
AA.reset();
2021-10-18 21:44:20 +02:00
AA.add(90, 5); // 5 steps north
AA.add(180, 3); // 3 steps west
2020-11-27 11:10:47 +01:00
Serial.println(AA.getAverage());
Serial.println(AA.getTotalLength());
```
2021-10-18 21:44:20 +02:00
## Future
- check if other units exist to support.
2020-11-27 11:10:47 +01:00