60 lines
1.3 KiB
C
Raw Normal View History

2020-11-27 11:10:47 +01:00
#pragma once
2017-12-09 18:23:18 +01:00
//
// FILE: AverageAngle.h
2021-10-18 21:44:20 +02:00
// AUTHOR: Rob Tillaart
2023-10-17 19:38:03 +02:00
// VERSION: 0.2.1
2022-12-07 20:30:13 +01:00
// DATE: 2017-11-21
2023-10-17 19:38:03 +02:00
// PURPOSE: Arduino library to calculate correctly the average of multiple angles.
2017-12-09 18:23:18 +01:00
2021-10-18 21:44:20 +02:00
2020-11-27 11:10:47 +01:00
#include "math.h"
2017-12-09 18:23:18 +01:00
#include "Arduino.h"
2023-02-01 13:53:15 +01:00
#define AVERAGE_ANGLE_LIB_VERSION (F("0.2.0"))
2021-10-18 21:44:20 +02:00
2021-12-13 20:51:57 +01:00
#define GRAD_TO_RAD (PI / 200.0)
#define RAD_TO_GRAD (200.0 / PI)
2021-10-18 21:44:20 +02:00
2017-12-09 18:23:18 +01:00
2023-02-01 13:53:15 +01:00
#define AVERAGE_ANGLE_OK 0
#define AVERAGE_ANGLE_OVERFLOW -10
#define AVERAGE_ANGLE_SINGULARITY -20
2017-12-09 18:23:18 +01:00
class AverageAngle
{
public:
2021-10-18 21:44:20 +02:00
enum AngleType { DEGREES = 0, RADIANS = 1, GRADIANS = 2 };
2017-12-09 18:23:18 +01:00
2021-01-29 12:31:58 +01:00
AverageAngle(const enum AngleType type = DEGREES);
2017-12-09 18:23:18 +01:00
2021-01-29 12:31:58 +01:00
uint32_t add(float alpha, float length = 1.0);
void reset();
2022-10-29 13:59:29 +02:00
uint32_t count();
2023-02-01 13:53:15 +01:00
2022-12-07 20:30:13 +01:00
// if there are no angles added (count == 0) or
// if the sum == (0,0) there is a singularity ==> no angle. NAN
2021-01-29 12:31:58 +01:00
float getAverage();
2022-12-07 20:30:13 +01:00
// if count == 0 total length == 0.
2021-01-29 12:31:58 +01:00
float getTotalLength();
2022-12-07 20:30:13 +01:00
// if count == 0 average length == 0.
2021-01-29 12:31:58 +01:00
float getAverageLength();
2022-10-29 13:59:29 +02:00
AngleType type();
bool setType(AngleType type);
2021-10-18 21:44:20 +02:00
2023-02-01 13:53:15 +01:00
int lastError();
2017-12-09 18:23:18 +01:00
private:
2021-01-29 12:31:58 +01:00
AngleType _type;
float _sumx;
float _sumy;
uint32_t _count;
2023-02-01 13:53:15 +01:00
int _error;
2017-12-09 18:23:18 +01:00
};
2020-11-27 11:10:47 +01:00
2021-12-13 20:51:57 +01:00
2022-12-07 20:30:13 +01:00
// -- END OF FILE --
2021-12-13 20:51:57 +01:00