0.2.0 AngleConvertor

This commit is contained in:
Rob Tillaart 2024-01-15 17:23:56 +01:00
parent 6bed0e7b54
commit 6f5ba8c069
10 changed files with 271 additions and 131 deletions

View File

@ -2,7 +2,7 @@
//
// FILE: AngleConvertor.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.2.0
// DATE: 2022-12-01
// PURPOSE: Angle conversion class
// URL: https://github.com/RobTillaart/AngleConvertor
@ -11,7 +11,7 @@
#include "Arduino.h"
#define ANGLECONVERTOR_LIB_VERSION (F("0.1.2"))
#define ANGLECONVERTOR_LIB_VERSION (F("0.2.0"))
/////////////////////////////////////////////////////////////
@ -24,68 +24,68 @@ public:
AngleConvertor() { _v = 0; };
// SETTERS
void setDegrees(float value = 0) { _v = value * (M_PI / 180.0); };
void setRadians(float value = 0) { _v = value; };
void setGradians(float value = 0) { _v = value * (M_PI / 200.0); };
void setAngularMil(float value = 0) { _v = value * (M_PI / 3200.0); };
void setBinaryRadians(float value = 0) { _v = value * (M_PI / 128.0); };
void setCentiTurn(float value = 0) { _v = value * (M_PI / 50.0); };
void setDiameterPart(float value = 0) { _v = value * (M_PI / 60.0); };
void setHexacontade(float value = 0) { _v = value * (M_PI / 30.0); };
void setHourAngle(float value = 0) { _v = value * (M_PI / 12.0); };
void setMilliTurn(float value = 0) { _v = value * (M_PI / 500.0); };
void setDegrees(double value = 0) { _v = value * (180.0 / 180.0); };
void setRadians(double value = 0) { _v = value * (180.0 / M_PI); };
void setGradians(double value = 0) { _v = value * (180.0 / 200.0); };
void setAngularMil(double value = 0) { _v = value * (180.0 / 3200.0); };
void setBinaryRadians(double value = 0) { _v = value * (180.0 / 128.0); };
void setCentiTurn(double value = 0) { _v = value * (180.0 / 50.0); };
void setDiameterPart(double value = 0) { _v = value * (180.0 / 60.0); };
void setHexacontade(double value = 0) { _v = value * (180.0 / 30.0); };
void setHourAngle(double value = 0) { _v = value * (180.0 / 12.0); };
void setMilliTurn(double value = 0) { _v = value * (180.0 / 500.0); };
void setMinuteTime(float value = 0) { _v = value * (M_PI / 720.0); };
void setOctant(float value = 0) { _v = value * (M_PI / 4.0); };
void setPechus(float value = 0) { _v = value * (M_PI / 90.0); }; // assumes 2°
void setPoints(float value = 0) { _v = value * (M_PI / 16.0); };
void setQuadrant(float value = 0) { _v = value * (M_PI / 2.0); };
void setQuarterPoint(float value = 0) { _v = value * (M_PI / 64.0); };
void setSecondsTime(float value = 0) { _v = value * (M_PI / 43200); };
void setSextant(float value = 0) { _v = value * (M_PI / 3.0); };
void setSign(float value = 0) { _v = value * (M_PI / 6.0); };
void setTurn(float value = 0) { _v = value * (M_PI / 0.5); };
void setMinuteTime(double value = 0) { _v = value * (180.0 / 720.0); };
void setOctant(double value = 0) { _v = value * (180.0 / 4.0); };
void setPechus(double value = 0) { _v = value * (180.0 / 90.0); }; // assumes 2°
void setPoints(double value = 0) { _v = value * (180.0 / 16.0); };
void setQuadrant(double value = 0) { _v = value * (180.0 / 2.0); };
void setQuarterPoint(double value = 0) { _v = value * (180.0 / 64.0); };
void setSecondsTime(double value = 0) { _v = value * (180.0 / 43200); };
void setSextant(double value = 0) { _v = value * (180.0 / 3.0); };
void setSign(double value = 0) { _v = value * (180.0 / 6.0); };
void setTurn(double value = 0) { _v = value * (180.0 / 0.5); };
// GETTERS
float getDegrees() { return _v * (180.0 / M_PI); };
float getRadians() { return _v; };
float getGradians() { return _v * (200.0 / M_PI); };
float getAngularMil() { return _v * (3200.0 / M_PI); };
float getBinaryRadians() { return _v * (128.0 / M_PI); };
float getCentiTurn() { return _v * (50.0 / M_PI); };
float getDiameterPart() { return _v * (60.0 / M_PI); };
float getHexacontade() { return _v * (30.0 / M_PI); };
float getHourAngle() { return _v * (12.0 / M_PI); };
float getMilliTurn() { return _v * (500.0 / M_PI); };
double getDegrees() { return _v * (180.0 / 180.0); };
double getRadians() { return _v * (M_PI / 180.0); };
double getGradians() { return _v * (200.0 / 180.0); };
double getAngularMil() { return _v * (3200.0 / 180.0); };
double getBinaryRadians() { return _v * (128.0 / 180.0); };
double getCentiTurn() { return _v * (50.0 / 180.0); };
double getDiameterPart() { return _v * (60.0 / 180.0); };
double getHexacontade() { return _v * (30.0 / 180.0); };
double getHourAngle() { return _v * (12.0 / 180.0); };
double getMilliTurn() { return _v * (500.0 / 180.0); };
float getMinuteTime() { return _v * (720.0 / M_PI); };
float getOctant() { return _v * (4.0 / M_PI); };
float getPechus() { return _v * (90.0 / M_PI); }; // assumes 2°
float getPoints() { return _v * (16.0 / M_PI); };
float getQuadrant() { return _v * (2.0 / M_PI); };
float getQuarterPoint() { return _v * (64.0 / M_PI); };
float getSecondsTime() { return _v * (43200 / M_PI); };
float getSextant() { return _v * (3.0 / M_PI); };
float getSign() { return _v * (6.0 / M_PI); };
float getTurn() { return _v * (0.5 / M_PI); };
double getMinuteTime() { return _v * (720.0 / 180.0); };
double getOctant() { return _v * (4.0 / 180.0); };
double getPechus() { return _v * (90.0 / 180.0); }; // assumes 2°
double getPoints() { return _v * (16.0 / 180.0); };
double getQuadrant() { return _v * (2.0 / 180.0); };
double getQuarterPoint() { return _v * (64.0 / 180.0); };
double getSecondsTime() { return _v * (43200 / 180.0); };
double getSextant() { return _v * (3.0 / 180.0); };
double getSign() { return _v * (6.0 / 180.0); };
double getTurn() { return _v * (0.5 / 180.0); };
// WINDROSE
//
char * windrose()
{
return windrose(_v * (180.0 / M_PI));
return windrose(_v);
}
char * windrose(float degrees)
char * windrose(double degrees)
{
uint8_t idx = (degrees + 11.25) * 0.044444444444444; // 1.0 / 22.5
return _wr2[idx];
}
private:
float _v; // internal use radians.
double _v; // internal use degrees (0.2.0).
char _wr2[17][4] = {
"N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW", "N"};

View File

@ -7,10 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.0] - 2024-01-15
- Fix #4, use **degrees** as internal format to improve precision (a bit)
- set internal size to **double** for those board that support 8 bytes double.
- improved **AngleConverter_demo.ino**
----
## [0.1.2] - 2023-10-17
- update readme.md
## [0.1.1] - 2023-01-23
- update GitHub actions
- update license 2023

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2022-2023 Rob Tillaart
Copyright (c) 2022-2024 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -18,6 +18,11 @@ Arduino library for converting angles (degrees/radians) to less known formats.
AngleConvertor is an Arduino class to convert an angle from and to less known formats.
Since 0.2.0 the class uses degrees as internal format as that improved precision a bit
compared to the pre-0.2.0 version that used radians.
Furthermore to improve precision the class uses doubles, so platforms that support these
can gain extra precision.
#### Formats
@ -68,61 +73,61 @@ AngleConvertor is an Arduino class to convert an angle from and to less known fo
#### Setters
- **void setDegrees(float value = 0)**
- **void setRadians(float value = 0)**
- **void setGradians(float value = 0)**
- **void setAngularMil(float value = 0)**
- **void setBinaryRadians(float value = 0)**
- **void setCentiTurn(float value = 0)**
- **void setDiameterPart(float value = 0)**
- **void setHexacontade(float value = 0)**
- **void setHourAngle(float value = 0)**
- **void setMilliTurn(float value = 0)**
- **void setMinuteTime(float value = 0)**
- **void setOctant(float value = 0)**
- **void setPechus(float value = 0)**
- **void setPoints(float value = 0)**
- **void setQuadrant(float value = 0)**
- **void setQuarterPoint(float value = 0)**
- **void setSecondsTime(float value = 0)**
- **void setSextant(float value = 0)**
- **void setSign(float value = 0)**
- **void setTurn(float value = 0)**
- **void setDegrees(double value = 0)**
- **void setRadians(double value = 0)**
- **void setGradians(double value = 0)**
- **void setAngularMil(double value = 0)**
- **void setBinaryRadians(double value = 0)**
- **void setCentiTurn(double value = 0)**
- **void setDiameterPart(double value = 0)**
- **void setHexacontade(double value = 0)**
- **void setHourAngle(double value = 0)**
- **void setMilliTurn(double value = 0)**
- **void setMinuteTime(double value = 0)**
- **void setOctant(double value = 0)**
- **void setPechus(double value = 0)**
- **void setPoints(double value = 0)**
- **void setQuadrant(double value = 0)**
- **void setQuarterPoint(double value = 0)**
- **void setSecondsTime(double value = 0)**
- **void setSextant(double value = 0)**
- **void setSign(double value = 0)**
- **void setTurn(double value = 0)**
#### Getters
- **float getDegrees()**
- **float getRadians()**
- **float getGradians()**
- **float getAngularMil()**
- **float getBinaryRadians()**
- **float getCentiTurn()**
- **float getDiameterPart()**
- **float getHexacontade()**
- **float getHourAngle()**
- **float getMilliTurn()**
- **float getMinuteTime()**
- **float getOctant()**
- **float getPechus()**
- **float getPoints()**
- **float getQuadrant()**
- **float getQuarterPoint()**
- **float getSecondsTime()**
- **float getSextant()**
- **float getSign()**
- **float getTurn()**
- **double getDegrees()**
- **double getRadians()**
- **double getGradians()**
- **double getAngularMil()**
- **double getBinaryRadians()**
- **double getCentiTurn()**
- **double getDiameterPart()**
- **double getHexacontade()**
- **double getHourAngle()**
- **double getMilliTurn()**
- **double getMinuteTime()**
- **double getOctant()**
- **double getPechus()**
- **double getPoints()**
- **double getQuadrant()**
- **double getQuarterPoint()**
- **double getSecondsTime()**
- **double getSextant()**
- **double getSign()**
- **double getTurn()**
#### WindRose
From: https://forum.arduino.cc/t/function-optimization-wind-direction-open-for-ideas/92493/10
Converts an angle in degrees to a char array like "WSW".
0 and 360 degrees is considered North.
- **char \* windrose()** converter version.
- **char \* windrose(float degrees)** stand alone version.
- **char \* windrose(double degrees)** stand alone version.
degrees should be between 0 and 360, as function does no normalization.

View File

@ -12,6 +12,7 @@
AngleConvertor conv;
void setup()
{
Serial.begin(115200);
@ -20,105 +21,149 @@ void setup()
Serial.println(ANGLECONVERTOR_LIB_VERSION);
Serial.println();
// if double = 8 bytes adjust #decimals
uint8_t decimals = 7;
if (sizeof(double) == 8) decimals = 15;
conv.setDegrees(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getDegrees(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getDegrees(), decimals);
conv.setRadians(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getRadians(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getRadians(), decimals);
conv.setGradians(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getGradians(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getGradians(), decimals);
conv.setAngularMil(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getAngularMil(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getAngularMil(), decimals);
conv.setBinaryRadians(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getBinaryRadians(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getBinaryRadians(), decimals);
conv.setCentiTurn(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getCentiTurn(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getCentiTurn(), decimals);
conv.setDiameterPart(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getDiameterPart(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getDiameterPart(), decimals);
conv.setHexacontade(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getHexacontade(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getHexacontade(), decimals);
conv.setHourAngle(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getHourAngle(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getHourAngle(), decimals);
conv.setMilliTurn(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getMilliTurn(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getMilliTurn(), decimals);
conv.setMinuteTime(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getMinuteTime(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getMinuteTime(), decimals);
conv.setOctant(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getOctant(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getOctant(), decimals);
conv.setPechus(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getPechus(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getPechus(), decimals);
conv.setPoints(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getPoints(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getPoints(), decimals);
conv.setQuadrant(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getQuadrant(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getQuadrant(), decimals);
conv.setQuarterPoint(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getQuarterPoint(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getQuarterPoint(), decimals);
conv.setSecondsTime(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getSecondsTime(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getSecondsTime(), decimals);
conv.setSextant(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getSextant(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getSextant(), decimals);
conv.setSign(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getSign(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getSign(), decimals);
conv.setTurn(1.0);
Serial.print(conv.getRadians(), 7);
Serial.print(conv.getDegrees(), decimals);
Serial.print("\t");
Serial.println(conv.getTurn(), 7);
Serial.print(conv.getRadians(), decimals);
Serial.print("\t");
Serial.println(conv.getTurn(), decimals);
Serial.println("\nDone...");
}

View File

@ -0,0 +1,23 @@
lib version: 0.1.2
1.0000000 0.0174533 1.0000000
57.2957763 1.0000000 1.0000000
0.9000000 0.0157080 1.0000000
0.0562500 0.0009817 1.0000000
1.4062500 0.0245437 1.0000000
3.5999999 0.0628319 1.0000000 <<<<<<<<<<<
3.0000000 0.0523599 1.0000000
6.0000000 0.1047198 1.0000000
15.0000000 0.2617994 1.0000000
0.3600000 0.0062832 1.0000000
0.2500000 0.0043633 1.0000000
45.0000000 0.7853982 1.0000000
1.9999999 0.0349066 1.0000000 <<<<<<<<<<<
11.2500000 0.1963496 1.0000000
90.0000000 1.5707963 1.0000000
2.8125000 0.0490874 1.0000000
0.0041667 0.0000727 1.0000000
60.0000000 1.0471975 1.0000000
30.0000000 0.5235988 1.0000000
360.0000000 6.2831854 1.0000000

View File

@ -0,0 +1,49 @@
lib version: 0.2.0 (UNO double == 4 bytes == float)
1.0000000 0.0174533 1.0000000
57.2957763 1.0000000 1.0000000
0.9000000 0.0157080 1.0000000
0.0562500 0.0009817 1.0000000
1.4062500 0.0245437 1.0000000
3.5999999 0.0628319 1.0000000
3.0000000 0.0523599 1.0000000
6.0000000 0.1047198 1.0000000
15.0000000 0.2617994 1.0000000
0.3600000 0.0062832 1.0000000
0.2500000 0.0043633 1.0000000
45.0000000 0.7853982 1.0000000
2.0000000 0.0349066 1.0000000
11.2500000 0.1963496 1.0000000
90.0000000 1.5707963 1.0000000
2.8125000 0.0490874 1.0000000
0.0041667 0.0000727 1.0000000
60.0000000 1.0471975 1.0000000
30.0000000 0.5235988 1.0000000
360.0000000 6.2831854 1.0000000
lib version: 0.2.0 (ESP32 double == 8 bytes != float)
1.000000000000000 0.017453292519943 1.000000000000000
57.295779513082322 1.000000000000000 1.000000000000000
0.900000000000000 0.015707963267949 1.000000000000000
0.056250000000000 0.000981747704247 1.000000000000000
1.406250000000000 0.024543692606170 1.000000000000000
3.600000000000000 0.062831853071796 1.000000000000000
3.000000000000000 0.052359877559830 1.000000000000000
6.000000000000000 0.104719755119660 1.000000000000000
15.000000000000000 0.261799387799149 1.000000000000000
0.360000000000000 0.006283185307180 1.000000000000000
0.250000000000000 0.004363323129986 1.000000000000000
45.000000000000000 0.785398163397448 1.000000000000000
2.000000000000000 0.034906585039887 1.000000000000000
11.250000000000000 0.196349540849362 1.000000000000000
90.000000000000000 1.570796326794897 1.000000000000000
2.812500000000000 0.049087385212341 1.000000000000000
0.004166666666667 0.000072722052166 1.000000000000000
60.000000000000000 1.047197551196598 1.000000000000000
30.000000000000000 0.523598775598299 1.000000000000000
360.000000000000000 6.283185307179587 1.000000000000000

View File

@ -0,0 +1,12 @@
lib version: 0.1.2 UNO
TIME: 4
Done...
lib version: 0.1.2 ESP32
TIME: 3
Done...

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/AngleConvertor.git"
},
"version": "0.1.2",
"version": "0.2.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=AngleConvertor
version=0.1.2
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library to convert between different less known angle formats.