Skip to content

Commit 931445b

Browse files
committed
Fix arduino-libraries#3: Update min and max to sensible defaults
R/C servos have a standard pulse width range of 1000 to 2000µs<sup name="a1">[1](#f1)</sup>, with the zero point between the two at 1500µs. Currently, Arduino's Servo library sets: - [`#define MIN_PULSE_WIDTH 544`](https://github.com/arduino-libraries/Servo/blob/4970d615a13f4c0d08026ee361cc8a01974924a2/src/Servo.h#L80) - [`#define MAX_PULSE_WIDTH 2400`](https://github.com/arduino-libraries/Servo/blob/4970d615a13f4c0d08026ee361cc8a01974924a2/src/Servo.h#L81) - [`#define DEFAULT_PULSE_WIDTH 1500`](https://github.com/arduino-libraries/Servo/blob/4970d615a13f4c0d08026ee361cc8a01974924a2/src/Servo.h#L82) This causes a lot of confusion<sup name="a2">[2](#f2)</sup>, especially since [the docs say `write(90)` should correspond to the mid-point] (https://www.arduino.cc/en/Reference/ServoWrite); in actuality, it results in a call to `writeMicroseconds(1472)`<sup name="a3">[3](#f3)</sup>. This change adjusts the defaults to align with R/C standards. Specifically, - `write(0)` now corresponds to the standard min pulse width of 1000µs. - `write(90)` now corresponds to the standard zero point pulse width, and aligns with the library's `DEFAULT_PULSE_WIDTH` variable. - `write(180)` now corresponds to the standard max pulse width of 2000µs. Tested on an Arduino Uno with a [Tower Pro Micro Servo SG90](http://www.ee.ic.ac.uk/pcheung/teaching/DE1_EE/stores/sg90_datasheet.pdf), and a [Parallax Feedback 360° High-Speed Servo](https://parallax.com/sites/default/files/downloads/900-00360-Feedback-360-HS-Servo-v1.2.pdf). --- <a name="f1" href="#a1">1</a>: For example, http://www.ee.ic.ac.uk/pcheung/teaching/DE1_EE/stores/sg90_datasheet.pdf <a name="f2" href="#a2">2</a>: For instance: - julianduque/beaglebone-io#54 - arduino-libraries#3 - https://toolguyd.com/oscilloscope-arduino-servo-pwm-signal-mistakes/ - https://makezine.com/2014/04/23/arduinos-servo-library-angles-microseconds-and-optional-command-parameters/ I also see a _lot_ of posts on https://forum.arduino.cc about this. <a name="f3" href="#a3">3</a>: There is actually no way to set a standard servo to the zero-point using `write(angle)`; the closest you can get is `write(92)`, for a pulse of 1504µs.
1 parent 3c1373b commit 931445b

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

Diff for: src/Servo.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777

7878
#define Servo_VERSION 2 // software version of this library
7979

80-
#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
81-
#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
80+
#define MIN_PULSE_WIDTH 1000 // the shortest pulse (in us) sent to a servo
81+
#define MAX_PULSE_WIDTH 2000 // the longest pulse (in us) sent to a servo
8282
#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
8383
#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds
8484

@@ -106,8 +106,8 @@ class Servo
106106
uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
107107
uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
108108
void detach();
109-
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
110-
void writeMicroseconds(int value); // Write pulse width in microseconds
109+
void write(int value); // if value is < the minimum pulse width it's treated as an angle, otherwise as pulse width in microseconds
110+
void writeMicroseconds(int value); // Write pulse width in microseconds
111111
int read(); // returns current pulse width as an angle between 0 and 180 degrees
112112
int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release)
113113
bool attached(); // return true if this servo is attached, otherwise false

Diff for: src/avr/Servo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void Servo::detach()
264264
void Servo::write(int value)
265265
{
266266
if(value < MIN_PULSE_WIDTH)
267-
{ // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
267+
{ // treat values less than the minimum pulse width as angles in degrees (valid values in microseconds are handled as microseconds)
268268
if(value < 0) value = 0;
269269
if(value > 180) value = 180;
270270
value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());

Diff for: src/megaavr/Servo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void Servo::detach()
157157

158158
void Servo::write(int value)
159159
{
160-
// treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
160+
// treat values less than the minimum pulse width as angles in degrees (valid values in microseconds are handled as microseconds)
161161
if (value < MIN_PULSE_WIDTH)
162162
{
163163
if (value < 0)

Diff for: src/sam/Servo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void Servo::detach()
228228

229229
void Servo::write(int value)
230230
{
231-
// treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
231+
// treat values less than the minimum pulse width as angles in degrees (valid values in microseconds are handled as microseconds)
232232
if (value < MIN_PULSE_WIDTH)
233233
{
234234
if (value < 0)

Diff for: src/samd/Servo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ void Servo::detach()
243243

244244
void Servo::write(int value)
245245
{
246-
// treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
246+
// treat values less than the minimum pulse width as angles in degrees (valid values in microseconds are handled as microseconds)
247247
if (value < MIN_PULSE_WIDTH)
248248
{
249249
if (value < 0)

Diff for: src/stm32f4/ServoTimers.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
#define MIN_ANGLE 0
7676
#define MAX_ANGLE 180
7777

78-
#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
79-
#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
78+
#define MIN_PULSE_WIDTH 1000 // the shortest pulse (in us) sent to a servo
79+
#define MAX_PULSE_WIDTH 2000 // the longest pulse (in us) sent to a servo
8080

8181
/** Class for interfacing with RC servomotors. */
8282
class Servo {
@@ -103,12 +103,12 @@ class Servo {
103103
* @param minPulseWidth Minimum pulse width to write to pin, in
104104
* microseconds. This will be associated
105105
* with a minAngle degree angle. Defaults to
106-
* SERVO_DEFAULT_MIN_PW = 544.
106+
* MIN_PULSE_WIDTH.
107107
*
108108
* @param maxPulseWidth Maximum pulse width to write to pin, in
109109
* microseconds. This will be associated
110110
* with a maxAngle degree angle. Defaults to
111-
* SERVO_DEFAULT_MAX_PW = 2400.
111+
* MAX_PULSE_WIDTH.
112112
*
113113
* @param minAngle Target angle (in degrees) associated with
114114
* minPulseWidth. Defaults to

0 commit comments

Comments
 (0)