Skip to content

Commit a2a1e0d

Browse files
committed
Change timing limits to safe values. With previous default timings and safety limits, popular servos
could force against internal physical endstops, which could overload and destroy them.
1 parent 0827917 commit a2a1e0d

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

libraries/Servo/src/Servo.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ int improved_map(int value, int minIn, int maxIn, int minOut, int maxOut)
4646
Servo::Servo()
4747
{
4848
_attached = false;
49-
_valueUs = DEFAULT_PULSE_WIDTH;
50-
_minUs = MIN_PULSE_WIDTH;
51-
_maxUs = MAX_PULSE_WIDTH;
49+
_valueUs = DEFAULT_NEUTRAL_PULSE_WIDTH;
50+
_minUs = DEFAULT_MIN_PULSE_WIDTH;
51+
_maxUs = DEFAULT_MAX_PULSE_WIDTH;
5252
}
5353

5454
Servo::~Servo() {
@@ -58,7 +58,7 @@ Servo::~Servo() {
5858

5959
uint8_t Servo::attach(int pin)
6060
{
61-
return attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
61+
return attach(pin, DEFAULT_MIN_PULSE_WIDTH, DEFAULT_MAX_PULSE_WIDTH);
6262
}
6363

6464
uint8_t Servo::attach(int pin, uint16_t minUs, uint16_t maxUs)
@@ -70,11 +70,11 @@ uint8_t Servo::attach(int pin, uint16_t minUs, uint16_t maxUs)
7070
_attached = true;
7171
}
7272

73-
// keep the min and max within 200-3000 us, these are extreme
73+
// keep the min and max within 500-2500 us, these are extreme
7474
// ranges and should support extreme servos while maintaining
7575
// reasonable ranges
76-
_maxUs = max((uint16_t)250, min((uint16_t)3000, maxUs));
77-
_minUs = max((uint16_t)200, min(_maxUs, minUs));
76+
_maxUs = max((uint16_t)550, min((uint16_t)2500, maxUs));
77+
_minUs = max((uint16_t)500, min(_maxUs, minUs));
7878

7979
write(_valueUs);
8080

@@ -97,9 +97,11 @@ void Servo::write(int value)
9797
if (value < _minUs) {
9898
// assumed to be 0-180 degrees servo
9999
value = constrain(value, 0, 180);
100-
// writeMicroseconds will contrain the calculated value for us
100+
// writeMicroseconds will contain the calculated value for us
101101
// for any user defined min and max, but we must use default min max
102102
value = improved_map(value, 0, 180, _minUs, _maxUs);
103+
} else if (value > _maxUs) {
104+
value = constrain(value, _minUs, _maxUs);
103105
}
104106
writeMicroseconds(value);
105107
}

libraries/Servo/src/Servo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646

4747
// the following are in us (microseconds)
4848
//
49-
#define MIN_PULSE_WIDTH 800 // the shortest duty cycle sent to a servo
50-
#define MAX_PULSE_WIDTH 2190 // the longest duty cycle sent to a servo
51-
#define DEFAULT_PULSE_WIDTH 1500 // default duty cycle when servo is attached
52-
#define REFRESH_INTERVAL 20000 // classic default period to refresh servos in microseconds
49+
#define DEFAULT_MIN_PULSE_WIDTH 800 // the shortest duty cycle sent to a servo
50+
#define DEFAULT_MAX_PULSE_WIDTH 2190 // the longest duty cycle sent to a servo
51+
#define DEFAULT_NEUTRAL_PULSE_WIDTH 1500 // default duty cycle when servo is attached
52+
#define REFRESH_INTERVAL 20000 // classic default period to refresh servos in microseconds
5353
#define MAX_SERVOS 12
5454

5555
#if !defined(ESP8266)

0 commit comments

Comments
 (0)