From f67f03b544fad52ce16b41264d611205eec53cca Mon Sep 17 00:00:00 2001 From: Jan Grossmann Date: Sun, 24 Mar 2024 11:50:47 +0100 Subject: [PATCH] Fix issue with writing inital servo value before min/max is configured The call to function Servo::write() in Servo::attach() must be done after Servo::min and Servo::max are configured - Servo::write() uses the min/max for mapping the passed value if the value is passed in degrees. Therefore if inital value in attach is set in degress and min/max is not configured when call to Servo::write() is done, the mapping will not be correct. Resolves: #2308 --- libraries/Servo/src/stm32/Servo.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/Servo/src/stm32/Servo.cpp b/libraries/Servo/src/stm32/Servo.cpp index 4c4fc4bbd8..8a7ccc1a1b 100644 --- a/libraries/Servo/src/stm32/Servo.cpp +++ b/libraries/Servo/src/stm32/Servo.cpp @@ -121,10 +121,12 @@ uint8_t Servo::attach(int pin, int min, int max, int value) if (this->servoIndex < MAX_SERVOS) { pinMode(pin, OUTPUT); // set servo pin to output servos[this->servoIndex].Pin.nbr = pin; - write(value); // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS this->max = (MAX_PULSE_WIDTH - max) / 4; + + write(value); // set the initial position + // initialize the timer if it has not already been initialized if (isTimerActive() == false) { TimerServoInit();