2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: runningAngle.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-11-21 10:07:14 -05:00
|
|
|
// VERSION: 0.2.1
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: Library to average angles by means of low pass filtering with wrapping.
|
2022-05-29 05:19:11 -04:00
|
|
|
// URL: https://github.com/RobTillaart/runningAngle
|
|
|
|
// RELATED: https://github.com/RobTillaart/AverageAngle
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "math.h"
|
|
|
|
|
|
|
|
|
2023-11-21 10:07:14 -05:00
|
|
|
#define RUNNING_ANGLE_LIB_VERSION (F("0.2.1"))
|
2023-02-22 06:50:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
const float RA_DEFAULT_WEIGHT = 0.80;
|
|
|
|
const float RA_MIN_WEIGHT = 0.001;
|
|
|
|
const float RA_MAX_WEIGHT = 1.0;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
class runningAngle
|
|
|
|
{
|
|
|
|
public:
|
2022-05-29 05:19:11 -04:00
|
|
|
enum AngleType { DEGREES = 0, RADIANS = 1, GRADIANS = 2 };
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
runningAngle(const enum AngleType type = DEGREES);
|
|
|
|
|
2022-11-23 11:07:12 -05:00
|
|
|
// first value added will not use the weight to set the initial value.
|
2023-02-22 06:50:23 -05:00
|
|
|
float add(float angle); // returns new average
|
|
|
|
void reset();
|
|
|
|
float getAverage();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2023-02-22 06:50:23 -05:00
|
|
|
bool setWeight(float w = RA_DEFAULT_WEIGHT);
|
|
|
|
float getWeight();
|
|
|
|
enum AngleType type();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-11-23 11:07:12 -05:00
|
|
|
// reformat angle to -180..+180 (degrees) or -PI..PI (radians)
|
2023-02-22 06:50:23 -05:00
|
|
|
float wrap(float angle);
|
|
|
|
|
|
|
|
// select the output
|
|
|
|
void setMode0(); // -180..180
|
|
|
|
void setMode1(); // 0..360
|
|
|
|
uint8_t getMode();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum AngleType _type;
|
|
|
|
float _average = 0;
|
|
|
|
float _weight;
|
|
|
|
bool _reset;
|
2023-02-22 06:50:23 -05:00
|
|
|
uint16_t _mode = 0;
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
2021-05-28 07:43:31 -04:00
|
|
|
|
2022-11-23 11:07:12 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-28 03:50:06 -05:00
|
|
|
|