Fix WMath's map() implementation for inverse/round-trip mapping#7027
Merged
earlephilhower merged 2 commits intoesp8266:masterfrom Feb 9, 2020
Merged
Fix WMath's map() implementation for inverse/round-trip mapping#7027earlephilhower merged 2 commits intoesp8266:masterfrom
earlephilhower merged 2 commits intoesp8266:masterfrom
Conversation
Contributor
Author
|
Here's the sketch for testing correctness and timing: uint32_t expired;
uint32_t iterations;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("check map");
expired = 0;
iterations = 0;
}
constexpr int imin = 0;
constexpr int imax = 180;
constexpr int omin = 1000;
constexpr int omax = 2000;
int i = 0;
int fails = 0;
void loop()
{
++iterations;
i = (i + 1) % (imax + 1);
#if defined(ESP8266) || defined(ESP32)
cli();
volatile auto start = ESP.getCycleCount();
#else
// Set Timer 1 to normal mode at F_CPU.
TCCR1A = 0;
TCCR1B = 1;
cli();
volatile uint16_t start = TCNT1;
#endif
volatile auto o = map(i, imin, imax, omin, omax);
#if defined(ESP8266) || defined(ESP32)
sei();
volatile auto cycles = ESP.getCycleCount() - start;
#else
sei();
volatile uint16_t finish = TCNT1;
uint16_t cycles = finish - start;
#endif
expired += cycles;
if (i != map(o, omin, omax, imin, imax))
{
++fails;
}
if (iterations > 100000)
{
Serial.print("Cycles per map(...) = "); Serial.print(expired / iterations);
Serial.print(", fails = "); Serial.print(fails);
Serial.println();
expired = 0;
iterations = 0;
fails = 0;
}
} |
Contributor
Author
…ion. Greatly reduces error rate (half, or 0 zero errors, depends on in/out ranges) for round-trip mapping at the same performance.
devyte
reviewed
Feb 9, 2020
| const long divisor = in_max - in_min; | ||
| const long delta = x - in_min; | ||
|
|
||
| return (delta * dividend + (divisor / 2)) / divisor + out_min; |
Collaborator
There was a problem hiding this comment.
This doesn't handle divisor == 0.
Contributor
Author
|
This today got merged into Arduino Core for ESP32, espressif/arduino-esp32#3655 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Inspired by Servo.cpp's
improved_map, this superseded that code, and has better performance (identical to prior WMath implementation).I expect this is better used to replace the core implementation, then drop "improved_map" from Servo.cpp, than keep it local and have a map() function that may be compatible but works horribly ;-)