Skip to content

Servo.attach() call to Servo::write() before min/max are configured leads to issue when writting angles in degrees #2308

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jan019 opened this issue Mar 19, 2024 · 2 comments · Fixed by #2318
Labels
bug 🐛 Something isn't working
Milestone

Comments

@jan019
Copy link
Contributor

jan019 commented Mar 19, 2024

Describe the bug
The Servo library calls the write() function within the attach() function

before configuring this->min and this->max values
this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS
this->max = (MAX_PULSE_WIDTH - max) / 4;
, which however need to be configured for write() function to work correctly when the servo position is set in degrees. The function write() uses the this->min and this->max values to map the passed value in degrees between min and max https://github.com/stm32duino/Arduino_Core_STM32/blob/641f3d8883e9a7220f65dded355dc32e218a990d/libraries/Servo/src/stm32/Servo.cpp#L156/

To Reproduce

int min = 40;
int max = 70;
int value = 40;

// on first call to attach, the write is called with a different value in this->min and this->max than on following calls
Servo.attach(pin, min, max, value)
  • STM32 core version: 2.7.1

The write() function should either not be called in the attach() function at all, or it must be called after this->min and this->max are configured.

@fpistm
Copy link
Member

fpistm commented Mar 20, 2024

Hi @jan019
Thanks for the detailed report.
It seems logical. Could you submit a PR ? I guess configuring the values before the write call instead of after is enough.

@fpistm fpistm added the bug 🐛 Something isn't working label Mar 20, 2024
@fpistm fpistm added this to the 2.8.0 milestone Mar 20, 2024
@jan019
Copy link
Contributor Author

jan019 commented Mar 21, 2024

Hi @fpistm, thanks for checking this out. I will submit a PR later today with configuring the values before the write call instead of after (I have tested this and it works well).

jan019 added a commit to jan019/Arduino_Core_STM32 that referenced this issue Mar 24, 2024
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
fpistm pushed a commit that referenced this issue Mar 25, 2024
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
tadtruex pushed a commit to TadAtThayer/Arduino_Core_STM32 that referenced this issue Apr 23, 2024
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
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug 🐛 Something isn't working
Projects
Development

Successfully merging a pull request may close this issue.

2 participants