From cf87923609daa4cd564429d253d93684da051097 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sat, 18 Dec 2021 13:14:58 +0100 Subject: [PATCH] 0.1.9 FastTrig --- libraries/FastTrig/.arduino-ci.yml | 10 ++-- .../.github/workflows/arduino_test_runner.yml | 10 ++-- libraries/FastTrig/FastTrig.h | 14 ++++- libraries/FastTrig/LICENSE | 2 +- libraries/FastTrig/README.md | 52 +++++++++++++------ .../fastTrig_generate_tables.ino | 13 +++-- .../fastTrig_optimize/fastTrig_optimize.ino | 6 ++- .../examples/fastTrig_plot/fastTrig_plot.ino | 5 +- .../fastTrig_test1/fastTrig_test1.ino | 3 +- .../fastTrig_test2/fastTrig_test2.ino | 7 ++- .../fastTrig_test_arc/fastTrig_test_arc.ino | 4 ++ .../fatTrig_playground/fatTrig_playground.ino | 7 ++- libraries/FastTrig/keywords.txt | 7 ++- libraries/FastTrig/library.json | 5 +- libraries/FastTrig/library.properties | 2 +- libraries/FastTrig/test/unit_test_001.cpp | 6 +++ 16 files changed, 113 insertions(+), 40 deletions(-) diff --git a/libraries/FastTrig/.arduino-ci.yml b/libraries/FastTrig/.arduino-ci.yml index ff5659b9..e7cb4633 100644 --- a/libraries/FastTrig/.arduino-ci.yml +++ b/libraries/FastTrig/.arduino-ci.yml @@ -2,6 +2,10 @@ compile: # Choosing to run compilation tests on 2 different Arduino platforms platforms: - uno - - leonardo - - due - - zero + # - due + # - zero + # - leonardo + - m4 + - esp32 + # - esp8266 + # - mega2560 \ No newline at end of file diff --git a/libraries/FastTrig/.github/workflows/arduino_test_runner.yml b/libraries/FastTrig/.github/workflows/arduino_test_runner.yml index 476456bb..096b975b 100644 --- a/libraries/FastTrig/.github/workflows/arduino_test_runner.yml +++ b/libraries/FastTrig/.github/workflows/arduino_test_runner.yml @@ -4,10 +4,14 @@ name: Arduino CI on: [push, pull_request] jobs: - arduino_ci: + runTest: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: Arduino-CI/action@master - # Arduino-CI/action@v0.1.1 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.6 + - run: | + gem install arduino_ci + arduino_ci.rb diff --git a/libraries/FastTrig/FastTrig.h b/libraries/FastTrig/FastTrig.h index 9eaeff56..dfc5a2d8 100644 --- a/libraries/FastTrig/FastTrig.h +++ b/libraries/FastTrig/FastTrig.h @@ -2,7 +2,7 @@ // // FILE: FastTrig.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.8 +// VERSION: 0.1.9 // PURPOSE: Arduino library for a faster approximation of sin() and cos() // DATE: 2011-08-18 // URL: https://github.com/RobTillaart/FastTrig @@ -23,10 +23,15 @@ // 0.1.7 2021-04-23 fix for PlatformIO // 0.1.8 2021-08-10 made % 180 conditional in itan() => performance gain // added icot() cotangent. +// 0.1.9 2021-12-18 update Arduino-CI, badges, +// update library.json, minor edits #include "Arduino.h" +#define FAST_TRIG_LIB_VERSION (F("0.1.9")) + + // 91 x 2 bytes ==> 182 bytes // use 65535.0 as divider uint16_t isinTable16[] = { @@ -75,6 +80,7 @@ uint8_t isinTable8[] = { 255 }; + /////////////////////////////////////////////////////// // // GONIO LOOKUP @@ -124,12 +130,14 @@ float isin(float f) return -g; } + float icos(float x) { // prevent modulo math if x in 0..360 return isin(x - 270.0); // better than x + 90; } + float itan(float f) { // reference @@ -175,6 +183,7 @@ float itan(float f) return ta; } + // some problem at 0 but at least we have a icot(x) cotangent. float icot(float f) { @@ -225,15 +234,18 @@ float iasin(float f) return (lo + delta); } + float iacos(float f) { return 90 - iasin(f); } + // PLACEHOLDER float iatan(float f) { return 0 * f; } + // -- END OF FILE -- diff --git a/libraries/FastTrig/LICENSE b/libraries/FastTrig/LICENSE index cfc58f98..8a10ac68 100644 --- a/libraries/FastTrig/LICENSE +++ b/libraries/FastTrig/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2011-2021 Rob Tillaart +Copyright (c) 2011-2022 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 diff --git a/libraries/FastTrig/README.md b/libraries/FastTrig/README.md index 3bd4cd56..dbb1feae 100644 --- a/libraries/FastTrig/README.md +++ b/libraries/FastTrig/README.md @@ -1,5 +1,7 @@ [![Arduino CI](https://github.com/RobTillaart/FastTrig/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![Arduino-lint](https://github.com/RobTillaart/FastTrig/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/FastTrig/actions/workflows/arduino-lint.yml) +[![JSON check](https://github.com/RobTillaart/FastTrig/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/FastTrig/actions/workflows/jsoncheck.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/FastTrig/blob/master/LICENSE) [![GitHub release](https://img.shields.io/github/release/RobTillaart/FastTrig.svg?maxAge=3600)](https://github.com/RobTillaart/FastTrig/releases) @@ -14,19 +16,32 @@ Arduino library with interpolated lookup for sin() and cos(). Trades speed for a **Warning: The library trades speed for accuracy so use at own risk** The library provides one lookup table that is used for -**isin(degrees)** and **icos(degrees)** and **itan(degrees)**. This lookup table is optimized for interpolation so the values for whole degrees are not optimal. Furthermore the **itan()** on AVR has almost no performance gain over the regular **tan()** so on AVR one is adviced to use **tan()**. On ESP32 the **itan(degrees)** does have a serious performance gain so use it if you need speed. +**isin(degrees)** and **icos(degrees)** and **itan(degrees)**. +This lookup table is optimized for interpolation so the values for whole degrees are not optimal. +Furthermore the **itan()** on AVR has almost no performance gain over the regular **tan()** so on AVR one is advised to use **tan()**. +On ESP32 the **itan(degrees)** does have a serious performance gain so use it if you need speed. -These functions are to be used as replacements for **sin(radians)**, **cos(radians)**and **tan(radians)**. Important to know is that they are NOT direct replaceable as the parameter differs a factor (PI/180.0) or its inverse. +These functions are to be used as replacements for **sin(radians)**, **cos(radians)** and **tan(radians)**. +Important to know is that they are NOT direct replaceable as the parameter differs a factor (PI/180.0) or its inverse. -Similar to ```cos(x) == sin(x + PI)``` it is also true that ```icos(x) == isin(x + 90)```, so **icos()** can use the very same lookup table at the cost of a single addition. In fact it uses ```icos(x) == isin(x - 270)``` as that performs better, -due to the folding. +Similar to ```cos(x) == sin(x + PI)``` it is also true that ```icos(x) == isin(x + 90)```, +so **icos()** can use the very same lookup table at the cost of a single addition. +In fact it uses ```icos(x) == isin(x - 270)``` as that performs better, due to the folding. -The **i** in the names stands for **int** and **interpolated** as the core is using integer math and lookuptable of 91 uint16_t = 182 bytes. By folding and mirroring the whole 360 degrees and beyond can be handled. When **isin(x)** is called and ```x == int(x)``` then the library will not interpolate and this will improve performance. When x is not a whole number the library will linear interpolate between **isin(int(x))** and **isin(int(x+1))**. Of course this introduces an error but it is quite fast (which was the goal). +The **i** in the names stands for **int** and **interpolated** as the core is using integer math and lookup table of 91 uint16_t = 182 bytes. +By folding and mirroring the whole 360 degrees and beyond can be handled. +When **isin(x)** is called and ```x == int(x)``` then the library will not interpolate and this will improve performance. +When x is not a whole number the library will linear interpolate between **isin(int(x))** and **isin(int(x+1))**. +Of course this introduces an error but it is quite fast (which was the goal). #### Lookup tables -The lookup tables are optimized (sketch provided) to minimize the error when using the interpolation, this implies that the points in the table might not be optimal when you use only wholde degrees. A sketch that generates lookup tables is in the examples folder. This generator sketch can also generate tables with different resolution e.g. 24, 14, 12 or even 6, 5 or 4 bit lookup tables. So depending on the application these tables can be ideal, but verify they meet your requirements. +The lookup tables are optimized (sketch provided) to minimize the error when using the interpolation, +this implies that the points in the table might not be optimal when you use only whole degrees. +A sketch that generates lookup tables is in the examples folder. +This generator sketch can also generate tables with different resolution e.g. 24, 14, 12 or even 6, 5 or 4 bit lookup tables. +So depending on the application these tables can be ideal, but verify they meet your requirements. The lookup tables used by **isin()** can be used directly in your program, the names are: - **isinTable16\[\]** index 0..90, values need to be (float) divided by 65535.0 @@ -70,6 +85,7 @@ values outside the 0..360 range. Please, verify the performance to see if it meets your requirements. + ## Accuracy isin icos itan errors - based upon example sketch - lib version 0.1.5 @@ -93,10 +109,11 @@ UNO calls 0.0 - 360.0 step 0.1 degree *Note: 0.1.3 for AVR was bad: 17.41900634 , 0.02249339 , 0.02953807 for itan() * Strange that the **itan()** on UNO and ESP32 differs (OK same order of magnitude). -Different implementation of gonio / float math? +Different implementation of goniometry / float maths? Please, verify the performance to see if it meets your requirements. + ## Performance iasin iacos iatan (added 0.1.5) @@ -113,10 +130,11 @@ time in us - calls -1 ..+1 step 0.001 degree | iatan | NI | NI | - the interpolated reverse lookup is around 30% faster on UNO an 80+% on ESP32 -- iatan is Not Implemented. +- **iatan()** is **Not** Implemented. Please, verify the accuracy to see if it meets your requirements. + ## Accuracy iasin iacos iatan (added 0.1.5) @@ -131,7 +149,7 @@ ESP32 calls -1 ..+1 step 0.001 degree | iatan | NI | NI | NI | NI | - largest error at 0.999981 - second largest error 0.052841 at -0.999000 -- iatan is Not Implemented +- **iatan()** is **Not** Implemented @@ -144,8 +162,8 @@ UNO calls -1 ..+1 step 0.001 degree | iatan | NI | NI | NI | NI | - largest error at 0.999981 - second largest error 0.052841 at -0.999000 -- max rel error is high as it occured near zero. -- iatan is Not Implemented +- max relative error is high as it occurred near zero. +- **iatan()** is **Not** Implemented Please, verify the accuracy to see if it meets your requirements. @@ -186,14 +204,18 @@ There is no **atan()** or **atan2()** replacement. - Made the % 180 in the **itan()** conditional. - added **icot(f)** +## 0.1.9 - -## TODO - -- How to improve the accuracy of the whole degrees, as now the table is optimized for interpolation. +- update library.json, badges, version string, minor edits. ## Operation See examples + +## Future + +- How to improve the accuracy of the whole degrees, as now the table is optimized for interpolation. +- version info in release_notes.md file. + diff --git a/libraries/FastTrig/examples/fastTrig_generate_tables/fastTrig_generate_tables.ino b/libraries/FastTrig/examples/fastTrig_generate_tables/fastTrig_generate_tables.ino index df7eb481..d97a6b2d 100644 --- a/libraries/FastTrig/examples/fastTrig_generate_tables/fastTrig_generate_tables.ino +++ b/libraries/FastTrig/examples/fastTrig_generate_tables/fastTrig_generate_tables.ino @@ -1,14 +1,14 @@ // // FILE: fastTrig_generate_tables.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: generate look up tables for gonio functions (and others) // these are not optimized for interpolation. // DATE: 2020-09-08 // TODO -// tables might have some trouble at "max values" CHECK +// tables might have some trouble at "max values" CHECK + #include "Arduino.h" @@ -19,7 +19,7 @@ void setup() Serial.println(__FILE__); // generate any # bits you want. - generate_bit_sin(32); // works is possible + generate_bit_sin(32); // works is possible generate_bit_sin(24); // TODO is this better than 16 bit? generate_bit_sin(20); // for serious math 1 digit better than 16 bit. generate_bit_sin(16); // for serious math @@ -38,13 +38,16 @@ void setup() generate_bit_tan(16); generate_bit_tan(8); + + Serial.println("\n//done..."); } + void loop() { - } + void generate_header() { Serial.println(); @@ -146,4 +149,6 @@ void generate_bit_tan(int t) Serial.println(); } + // -- END OF FILE -- + diff --git a/libraries/FastTrig/examples/fastTrig_optimize/fastTrig_optimize.ino b/libraries/FastTrig/examples/fastTrig_optimize/fastTrig_optimize.ino index f8a0808b..34d546c0 100644 --- a/libraries/FastTrig/examples/fastTrig_optimize/fastTrig_optimize.ino +++ b/libraries/FastTrig/examples/fastTrig_optimize/fastTrig_optimize.ino @@ -1,7 +1,6 @@ // // FILE: fastTrig_optimize.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.2 // PURPOSE: sketch to optimize the table for interpolation // DATE: 2020-09-06 @@ -9,8 +8,10 @@ // TODO make a python script for this ? + #include "FastTrig.h" + float getError(int i) { float error = 0; @@ -120,9 +121,10 @@ void test_isin_error_1(bool show) } - void loop() { } + // -- END OF FILE -- + diff --git a/libraries/FastTrig/examples/fastTrig_plot/fastTrig_plot.ino b/libraries/FastTrig/examples/fastTrig_plot/fastTrig_plot.ino index b6f948c9..21389123 100644 --- a/libraries/FastTrig/examples/fastTrig_plot/fastTrig_plot.ino +++ b/libraries/FastTrig/examples/fastTrig_plot/fastTrig_plot.ino @@ -1,7 +1,6 @@ // // FILE: fastTrig_plot.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.1 // PURPOSE: testing the fastTrigonio functions // DATE: 2020-09-07 @@ -15,6 +14,7 @@ volatile float x; int i; + void setup() { Serial.begin(115200); @@ -62,8 +62,11 @@ void setup() Serial.println("done...\n"); } + void loop() { } + // -- END OF FILE -- + diff --git a/libraries/FastTrig/examples/fastTrig_test1/fastTrig_test1.ino b/libraries/FastTrig/examples/fastTrig_test1/fastTrig_test1.ino index f4c3ecf2..972ad196 100644 --- a/libraries/FastTrig/examples/fastTrig_test1/fastTrig_test1.ino +++ b/libraries/FastTrig/examples/fastTrig_test1/fastTrig_test1.ino @@ -1,7 +1,6 @@ // // FILE: fastTrig_test1.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.2 // PURPOSE: testing the fastTrigonio functions // DATE: 2020-08-30 // (c) : MIT @@ -15,6 +14,7 @@ uint32_t start, d1, d2; volatile float x; int i; + void setup() { Serial.begin(115200); @@ -305,4 +305,5 @@ void loop() { } + // -- END OF FILE -- diff --git a/libraries/FastTrig/examples/fastTrig_test2/fastTrig_test2.ino b/libraries/FastTrig/examples/fastTrig_test2/fastTrig_test2.ino index 81308a15..8106a34b 100644 --- a/libraries/FastTrig/examples/fastTrig_test2/fastTrig_test2.ino +++ b/libraries/FastTrig/examples/fastTrig_test2/fastTrig_test2.ino @@ -4,7 +4,7 @@ // PURPOSE: testing the itan functions // DATE: 2021-08-10 // (c) : MIT -// + #include "FastTrig.h" @@ -14,6 +14,7 @@ uint32_t start, d1, d2; volatile float x; int i; + void setup() { Serial.begin(115200); @@ -26,7 +27,6 @@ void setup() } - void test_tan(int n) { Serial.println(__FUNCTION__); @@ -57,6 +57,7 @@ void test_tan(int n) delay(10); } + void test_itan_error_1(bool show) { Serial.println(__FUNCTION__); @@ -119,4 +120,6 @@ void loop() { } + // -- END OF FILE -- + diff --git a/libraries/FastTrig/examples/fastTrig_test_arc/fastTrig_test_arc.ino b/libraries/FastTrig/examples/fastTrig_test_arc/fastTrig_test_arc.ino index 6aa239a9..83265cf2 100644 --- a/libraries/FastTrig/examples/fastTrig_test_arc/fastTrig_test_arc.ino +++ b/libraries/FastTrig/examples/fastTrig_test_arc/fastTrig_test_arc.ino @@ -15,6 +15,7 @@ volatile float x; int i; float f; + void setup() { Serial.begin(115200); @@ -32,6 +33,7 @@ void setup() Serial.println("done...\n"); } + ///////////////////////////////////////////////////////////// // // @@ -287,4 +289,6 @@ void loop() { } + // -- END OF FILE -- + diff --git a/libraries/FastTrig/examples/fatTrig_playground/fatTrig_playground.ino b/libraries/FastTrig/examples/fatTrig_playground/fatTrig_playground.ino index d554b373..20536510 100644 --- a/libraries/FastTrig/examples/fatTrig_playground/fatTrig_playground.ino +++ b/libraries/FastTrig/examples/fatTrig_playground/fatTrig_playground.ino @@ -1,7 +1,6 @@ // // FILE: fastTrig_playground.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: playground to play with tables. // DATE: 2020-09-08 @@ -31,6 +30,7 @@ // tables generated with other sketch. + uint32_t isinTable24[] = { 0, 292803, 585516, 878052, 1170319, 1462231, 1753697, 2044628, 2334937, 2624535, 2913333, @@ -44,6 +44,7 @@ uint32_t isinTable24[] = { 16570660, 16613940, 16652160, 16685308, 16713372, 16736348, 16754222, 16766994, 16774660, 16777215, }; + uint32_t isinTable20[] = { 0, 18300, 36595, 54878, 73145, 91389, 109606, 127789, 145933, 164033, 182083, @@ -57,6 +58,7 @@ uint32_t isinTable20[] = { 1035665, 1038370, 1040759, 1042831, 1044585, 1046021, 1047138, 1047936, 1048415, 1048575, }; + uint16_t isinTable16[] = { 0, 1144, 2287, 3430, 4571, 5712, 6850, 7987, 9121, 10252, 11380, @@ -160,10 +162,11 @@ void test_performance() sum = 0; } + void loop() { - } // -- END OF FILE -- + diff --git a/libraries/FastTrig/keywords.txt b/libraries/FastTrig/keywords.txt index 54a3abaf..1d0a6fcf 100644 --- a/libraries/FastTrig/keywords.txt +++ b/libraries/FastTrig/keywords.txt @@ -1,17 +1,20 @@ -# Syntax Coloring Map For FastTrig +# Syntax Colouring Map For FastTrig -# Datatypes (KEYWORD1) +# Data types (KEYWORD1) # Methods and Functions (KEYWORD2) isin KEYWORD2 icos KEYWORD2 itan KEYWORD2 icot KEYWORD2 + iasin KEYWORD2 iacos KEYWORD2 + # Instances (KEYWORD2) + # Constants (LITERAL1) isinTable16 LITERAL1 isinTable8 LITERAL1 diff --git a/libraries/FastTrig/library.json b/libraries/FastTrig/library.json index 5b6cf27d..ec7b380b 100644 --- a/libraries/FastTrig/library.json +++ b/libraries/FastTrig/library.json @@ -15,8 +15,9 @@ "type": "git", "url": "https://github.com/RobTillaart/FastTrig" }, - "version": "0.1.8", + "version": "0.1.9", "license": "MIT", "frameworks": "*", - "platforms": "*" + "platforms": "*", + "headers": "FastTrig.h" } diff --git a/libraries/FastTrig/library.properties b/libraries/FastTrig/library.properties index f3b853b4..b32fc67b 100644 --- a/libraries/FastTrig/library.properties +++ b/libraries/FastTrig/library.properties @@ -1,5 +1,5 @@ name=FastTrig -version=0.1.8 +version=0.1.9 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library with interpolated lookup for sin() and cos() diff --git a/libraries/FastTrig/test/unit_test_001.cpp b/libraries/FastTrig/test/unit_test_001.cpp index dc8ed6f1..79cca147 100644 --- a/libraries/FastTrig/test/unit_test_001.cpp +++ b/libraries/FastTrig/test/unit_test_001.cpp @@ -30,8 +30,10 @@ unittest_setup() { + fprintf(stderr, "FAST_TRIG_LIB_VERSION: %s\n", (char *) FAST_TRIG_LIB_VERSION); } + unittest_teardown() { } @@ -47,6 +49,7 @@ unittest(test_isinTable16) } } + unittest(test_isinTable8) { fprintf(stderr,"Table8 error is < 1%% \n"); @@ -57,6 +60,7 @@ unittest(test_isinTable8) } } + unittest(test_max_error_table16) { fprintf(stderr,"Table16 max error: "); @@ -71,6 +75,7 @@ unittest(test_max_error_table16) assertEqualFloat(0, m, 0.001); } + unittest(test_max_error_table8) { fprintf(stderr,"Table8 max error: "); @@ -85,6 +90,7 @@ unittest(test_max_error_table8) assertEqualFloat(0, m, 0.01); } + unittest_main() // --------