madhephaestus / ESP32Servo

Arduino-compatible servo library for the ESP32
138 stars 53 forks source link

read() does not return last write() accurately #39

Open bigedian opened 1 year ago

bigedian commented 1 year ago

There is a loss of precision when reading back the angle that was last written to the servo. This prevents doing something like this: servo.write(servo.read()+1); to increment a servo by 1 degree from it's current position.

Possible cause is the value being mangled by the (integer) map() macros in the read() & write() functions.

Example test code:

for (int16_t i = 0; i <= 30; i++) { x_axis.write(i); Serial.printf("Wrote: %u Read: %u\n", i, x_axis.read()); }

Output: Wrote: 0 Read: 0 Wrote: 1 Read: 0 Wrote: 2 Read: 1 Wrote: 3 Read: 1 Wrote: 4 Read: 3 Wrote: 5 Read: 3 Wrote: 6 Read: 5 Wrote: 7 Read: 5 Wrote: 8 Read: 7 Wrote: 9 Read: 7 Wrote: 10 Read: 9 Wrote: 11 Read: 10 Wrote: 12 Read: 10 Wrote: 13 Read: 12 Wrote: 14 Read: 12 Wrote: 15 Read: 14 Wrote: 16 Read: 14 Wrote: 17 Read: 16 Wrote: 18 Read: 16 Wrote: 19 Read: 18 Wrote: 20 Read: 18 Wrote: 21 Read: 20 Wrote: 22 Read: 20 Wrote: 23 Read: 22 Wrote: 24 Read: 22 Wrote: 25 Read: 24 Wrote: 26 Read: 24 Wrote: 27 Read: 26 Wrote: 28 Read: 26 Wrote: 29 Read: 28 Wrote: 30 Read: 29

Clearly this is not very useful