2020-11-27 05:10:47 -05:00
|
|
|
#pragma once
|
2017-12-09 12:23:18 -05:00
|
|
|
//
|
|
|
|
// FILE: AverageAngle.h
|
2021-10-18 15:44:20 -04:00
|
|
|
// AUTHOR: Rob Tillaart
|
2024-04-09 04:32:29 -04:00
|
|
|
// VERSION: 0.2.2
|
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.
|
2024-04-09 04:32:29 -04:00
|
|
|
// URL: https://github.com/RobTillaart/AverageAngle
|
2017-12-09 12:23:18 -05:00
|
|
|
|
2021-10-18 15:44:20 -04:00
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
#include "math.h"
|
2017-12-09 12:23:18 -05:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
2024-04-09 04:32:29 -04:00
|
|
|
#define AVERAGE_ANGLE_LIB_VERSION (F("0.2.2"))
|
2021-10-18 15:44:20 -04:00
|
|
|
|
2021-12-13 14:51:57 -05:00
|
|
|
#define GRAD_TO_RAD (PI / 200.0)
|
|
|
|
#define RAD_TO_GRAD (200.0 / PI)
|
2021-10-18 15:44:20 -04:00
|
|
|
|
2017-12-09 12:23:18 -05:00
|
|
|
|
2023-02-01 07:53:15 -05:00
|
|
|
#define AVERAGE_ANGLE_OK 0
|
|
|
|
#define AVERAGE_ANGLE_OVERFLOW -10
|
|
|
|
#define AVERAGE_ANGLE_SINGULARITY -20
|
|
|
|
|
|
|
|
|
2017-12-09 12:23:18 -05:00
|
|
|
class AverageAngle
|
|
|
|
{
|
|
|
|
public:
|
2021-10-18 15:44:20 -04:00
|
|
|
enum AngleType { DEGREES = 0, RADIANS = 1, GRADIANS = 2 };
|
2017-12-09 12:23:18 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
AverageAngle(const enum AngleType type = DEGREES);
|
2017-12-09 12:23:18 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint32_t add(float alpha, float length = 1.0);
|
|
|
|
void reset();
|
2022-10-29 07:59:29 -04:00
|
|
|
uint32_t count();
|
2023-02-01 07:53:15 -05:00
|
|
|
|
2022-12-07 14:30:13 -05: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 06:31:58 -05:00
|
|
|
float getAverage();
|
2018-04-02 12:51:08 -04:00
|
|
|
|
2022-12-07 14:30:13 -05:00
|
|
|
// if count == 0 total length == 0.
|
2021-01-29 06:31:58 -05:00
|
|
|
float getTotalLength();
|
2022-12-07 14:30:13 -05:00
|
|
|
// if count == 0 average length == 0.
|
2021-01-29 06:31:58 -05:00
|
|
|
float getAverageLength();
|
2024-04-09 04:32:29 -04:00
|
|
|
float getSumX();
|
|
|
|
float getSumY();
|
2018-04-02 12:51:08 -04:00
|
|
|
|
2022-10-29 07:59:29 -04:00
|
|
|
AngleType type();
|
|
|
|
bool setType(AngleType type);
|
2021-10-18 15:44:20 -04:00
|
|
|
|
2023-02-01 07:53:15 -05:00
|
|
|
int lastError();
|
2017-12-09 12:23:18 -05:00
|
|
|
|
|
|
|
private:
|
2021-01-29 06:31:58 -05:00
|
|
|
AngleType _type;
|
|
|
|
float _sumx;
|
|
|
|
float _sumy;
|
|
|
|
uint32_t _count;
|
2023-02-01 07:53:15 -05:00
|
|
|
int _error;
|
2017-12-09 12:23:18 -05:00
|
|
|
};
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2021-12-13 14:51:57 -05:00
|
|
|
|
2022-12-07 14:30:13 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-13 14:51:57 -05:00
|
|
|
|