Fix and improve pulse width range by rileyjshaw · Pull Request #24 · arduino-libraries/Servo (original) (raw)
Different servo models can accept a wide range of pulse widths. Even different servos of the same model might vary a bit. Currently, the Arduino Servo library has a severely restricted hard limit on the pulse widths that can be sent to servos. Specifically:
- Minimum pulse width must be between [32, 1052].
- Maximum pulse width must be between [1888, 2908].
Many popular servos have min/max pulse widths that fall in that unavailable range between (1052, 1888). For instance, the Parallax Feedback 360° High-Speed Servo operates between [1280, 1720].
Before this commit, each instance of Servo
stored their min
and max
values as int8_t
. Since that only leaves room for values in the range [-128, 127], it can't store meaningful servo pulse widths, which are typically in the ~[1000, 2000]µs range. To compensate, min
and max
store the distance from the default values, divided by 4…
There are two problems with this:
- The first, mentioned above, is that you can never stray more than 512µs from
MIN_PULSE_WIDTH
andMAX_PULSE_WIDTH
. - The second is that unexpected and unnecessary rounding occurs.
Simply storing min
and max
as uint16_t
and using the values directly solves this problem, and reduces the complexity involved in working around it. This commit makes the library faster, and allows it to work with a wider range of servos. It also fixes some subtle bugs where the minimum value was hardcoded to MIN_PULSE_WIDTH
.
Tested on an Arduino Uno with a Tower Pro Micro Servo SG90, and a Parallax Feedback 360° High-Speed Servo.