GY-63_MS5611/libraries/FastMap/readme.md

134 lines
5.4 KiB
Markdown
Raw Normal View History

2021-01-29 06:31:58 -05:00
[![Arduino CI](https://github.com/RobTillaart/FastMap/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/FastMap/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/FastMap/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/FastMap/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/FastMap/actions/workflows/jsoncheck.yml)
2021-01-29 06:31:58 -05:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/FastMap/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/FastMap.svg?maxAge=3600)](https://github.com/RobTillaart/FastMap/releases)
2020-11-27 05:16:22 -05:00
# FastMap
2020-04-16 06:59:37 -04:00
Fast mapping and constraining.
2020-11-27 05:16:22 -05:00
## Description
FastMap is an object that pre-calculates (internal) floats to make a mapping function especially for floats.
2022-11-05 05:57:25 -04:00
The FastMap also provides a **back()** function to reverse the mapping.
This only works well with floats, and less with integers, so use with care.
2020-11-27 05:16:22 -05:00
An important difference with the traditional **map()** function is that both **init()** and **map()**
2022-11-05 05:57:25 -04:00
accepts floats as parameters.
This allows mapping that would be hard to achieve with the normal **map()** function.
2020-11-27 05:16:22 -05:00
2022-11-05 05:57:25 -04:00
Since 0.4.0 the **init()** function will not accept zero range defining input or output parameters.
2020-11-27 05:16:22 -05:00
## Performance notes
(based upon tests https://github.com/RobTillaart/FastMap/issues/4 )
2022-11-05 05:57:25 -04:00
- On AVR (UNO and MEGA) no gain is made mapping integers with fastMap, 130% slower = substantial.
- On AVR the gain for float is limited, 10% faster.
- On ESP32 the gain for integers and float is both in the order of 25%.
2020-11-27 05:16:22 -05:00
To see the actual gain in your project on your hardware you should test and compare.
FastMap is faster when mapping floats as it uses less float operations than the standard map formula does.
2022-11-05 05:57:25 -04:00
The performance results from pre-calculating values in the **init()** function.
An actual mapping therefore needs only one multiply and one add operation where the standard **map()** function
uses four adds, a multiplication and a division.
The pre-calculation in **init()** should be taken in account and if every **map()** call needs an **init()**
2020-11-27 05:16:22 -05:00
there will be no gain, on contrary.
2022-11-05 05:57:25 -04:00
## Precision notes
The implementation of **fastMap()** uses floats (typical 32 bits) which might result in more memory usage
2022-11-05 05:57:25 -04:00
and loss of precision for mapping of larger values, especially 32 and 64 bit integers.
This is caused by the limits of the mantissa (~23 bits) of the standard 4 byte float.
2022-11-05 05:57:25 -04:00
To solve this a **FastMapDouble** class is added which uses the **double** type for the platforms
that support 8 byte floats.
If your platform does not support double it will often be mapped to float, so no gain.
Furthermore using double might imply a performance penalty on some platforms.
2020-11-27 05:16:22 -05:00
## Interface
2022-11-05 05:57:25 -04:00
- **bool init(float in_min, float in_max, float out_min, float out_max)** defines the linear mapping parameters.
2020-11-27 05:16:22 -05:00
The **init()** function calculates all needed values for the **map()**, the **back()** call and the **constrainXX()** functions.
2022-11-05 05:57:25 -04:00
The **init()** function can be called again with new values when needed to do other mapping,
although it will give less overhead if you create an fastMap object per conversion needed.
Returns false if (out_max == out_min) or (in_max == in_min). (breaking change in 0.4.0).
2020-11-27 05:16:22 -05:00
- **float map(float value)** maps the parameter.
2022-11-05 05:57:25 -04:00
- **float back(float value)** does the inverse mapping.
2020-11-27 05:16:22 -05:00
2021-01-29 06:31:58 -05:00
### Constrains
2022-11-05 05:57:25 -04:00
FastMap supports three versions of constraining the map function, based upon the parameters of **init()**.
2020-11-27 05:16:22 -05:00
- **float constrainedMap(float value);** returns a value between outMin .. outMax
2022-11-05 05:57:25 -04:00
- **float lowerConstrainedMap(float value);** returns a value between outMin .. infinity, ==> no upper limit.
- **float upperConstrainedMap(float value);** returns a value between -infinity .. outMax ==> no lower limit.
2020-11-27 05:16:22 -05:00
2022-11-05 05:57:25 -04:00
To change the constrain values call **init()** with new limits, or use the standard **constrain()**.
2020-11-27 05:16:22 -05:00
Note there are **NO** constrain-versions for **back(value)** function.
## FastMapDouble
Version 3.0 adds **fastMapDouble** which has the same interface as **fastMap()**.
2020-11-27 05:16:22 -05:00
This class is meant to support 8 bytes doubles in their native accuracy and precision.
To display doubles one might need the **sci()** function of my **printHelpers** class.
2021-01-29 06:31:58 -05:00
https://github.com/RobTillaart/printHelpers
2020-11-27 05:16:22 -05:00
2022-11-05 05:57:25 -04:00
Note that on most embedded platforms the performance of doubles is less than floats.
#### boards supporting double
| board | float | double |
|:---------------|:-------:|:--------:|
| UNO | yes | no |
| ATMEGA | yes | no |
| MKR1000 | yes | yes |
| Zero | yes | yes |
| Teensy | yes | ? |
| ESP32 | yes | yes |
| RP2040 | yes | ? |
to elaborate table. (if someone has a good link, please let me know).
test code.
```cpp
void setup() {
Serial.begin(115200);
Serial.print("size of double:\t");
Serial.println(sizeof(double));
Serial.print("size of float: \t");
Serial.println(sizeof(float));
}
void loop() {}
```
2020-11-27 05:16:22 -05:00
## Usage
See examples.
## Future
2022-11-05 05:57:25 -04:00
#### must
#### should
- update documentation
2022-11-05 05:57:25 -04:00
- test performance fastMapDouble on ESP32.
#### could
- investigate map function for complex numbers? / coordinates?
- can fastMap and fastMapDouble be in a class hierarchy? gain?
- Template class?
2022-11-05 05:57:25 -04:00