Skip to content

Commit

Permalink
Fix issue with writing inital servo value before min/max is configured
Browse files Browse the repository at this point in the history
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: stm32duino#2308
  • Loading branch information
jan019 authored and fpistm committed Mar 25, 2024
1 parent 8f337e8 commit f67f03b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion libraries/Servo/src/stm32/Servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f67f03b

Please sign in to comment.