Skip to content

Commit 85ded0b

Browse files
committed
🩹 Clarify servo µs min/max
1 parent cb291e8 commit 85ded0b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Marlin/src/HAL/shared/servo.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
ServoInfo_t servo_info[MAX_SERVOS]; // static array of servo info structures
6161
uint8_t ServoCount = 0; // the total number of attached servos
6262

63-
#define SERVO_MIN(v) (MIN_PULSE_WIDTH - (v) * 4) // minimum value in uS for this servo
64-
#define SERVO_MAX(v) (MAX_PULSE_WIDTH - (v) * 4) // maximum value in uS for this servo
63+
#define SERVO_MIN_US(v) (MIN_PULSE_WIDTH - (v) * 4) // minimum value in uS for this servo
64+
#define SERVO_MAX_US(v) (MAX_PULSE_WIDTH - (v) * 4) // maximum value in uS for this servo
6565

6666
/************ static functions common to all instances ***********************/
6767

@@ -117,7 +117,7 @@ void Servo::detach() {
117117

118118
void Servo::write(int value) {
119119
if (value < MIN_PULSE_WIDTH) // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
120-
value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(min), SERVO_MAX(max));
120+
value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN_US(min), SERVO_MAX_US(max));
121121
writeMicroseconds(value);
122122
}
123123

@@ -126,8 +126,8 @@ void Servo::writeMicroseconds(int value) {
126126
byte channel = servoIndex;
127127
if (channel < MAX_SERVOS) { // ensure channel is valid
128128
// ensure pulse width is valid
129-
value = constrain(value, SERVO_MIN(min), SERVO_MAX(max)) - (TRIM_DURATION);
130-
value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
129+
LIMIT(value, SERVO_MIN_US(min), SERVO_MAX_US(max));
130+
value = usToTicks(value - (TRIM_DURATION)); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
131131

132132
CRITICAL_SECTION_START();
133133
servo_info[channel].ticks = value;
@@ -136,7 +136,7 @@ void Servo::writeMicroseconds(int value) {
136136
}
137137

138138
// return the value as degrees
139-
int Servo::read() { return map(readMicroseconds() + 1, SERVO_MIN(min), SERVO_MAX(max), 0, 180); }
139+
int Servo::read() { return map(readMicroseconds() + 1, SERVO_MIN_US(min), SERVO_MAX_US(max), 0, 180); }
140140

141141
int Servo::readMicroseconds() {
142142
return (servoIndex == INVALID_SERVO) ? 0 : ticksToUs(servo_info[servoIndex].ticks) + (TRIM_DURATION);

0 commit comments

Comments
 (0)