@@ -27,15 +27,15 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
27
uint32_t Servo::_servoMap = 0 ;
28
28
29
29
// similiar to map but will have increased accuracy that provides a more
30
- // symetric api (call it and use result to reverse will provide the original value)
30
+ // symmetrical api (call it and use result to reverse will provide the original value)
31
31
int improved_map (int value, int minIn, int maxIn, int minOut, int maxOut)
32
32
{
33
33
const int rangeIn = maxIn - minIn;
34
34
const int rangeOut = maxOut - minOut;
35
35
const int deltaIn = value - minIn;
36
36
// fixed point math constants to improve accurancy of divide and rounding
37
- const int fixedHalfDecimal = 1 ;
38
- const int fixedDecimal = fixedHalfDecimal * 2 ;
37
+ constexpr int fixedHalfDecimal = 1 ;
38
+ constexpr int fixedDecimal = fixedHalfDecimal * 2 ;
39
39
40
40
return ((deltaIn * rangeOut * fixedDecimal) / (rangeIn) + fixedHalfDecimal) / fixedDecimal + minOut;
41
41
}
@@ -70,11 +70,11 @@ uint8_t Servo::attach(int pin, uint16_t minUs, uint16_t maxUs)
70
70
_attached = true ;
71
71
}
72
72
73
- // keep the min and max within 500-2500 us, these are extreme
73
+ // keep the min and max within 200-3000 us, these are extreme
74
74
// ranges and should support extreme servos while maintaining
75
75
// reasonable ranges
76
- _maxUs = max ((uint16_t )550 , min ((uint16_t )2500 , maxUs));
77
- _minUs = max ((uint16_t )500 , min (_maxUs, minUs));
76
+ _maxUs = max ((uint16_t )250 , min ((uint16_t )3000 , maxUs));
77
+ _minUs = max ((uint16_t )200 , min (_maxUs, minUs));
78
78
79
79
write (_valueUs);
80
80
@@ -87,21 +87,20 @@ void Servo::detach()
87
87
_servoMap &= ~(1 << _pin);
88
88
stopWaveform (_pin);
89
89
_attached = false ;
90
+ _valueUs = DEFAULT_NEUTRAL_PULSE_WIDTH;
90
91
digitalWrite (_pin, LOW);
91
92
}
92
93
}
93
94
94
95
void Servo::write (int value)
95
96
{
96
- // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
97
+ // treat values less than _minUs as angles in degrees (values equal or larger are handled as microseconds)
97
98
if (value < _minUs) {
98
99
// assumed to be 0-180 degrees servo
99
100
value = constrain (value, 0 , 180 );
100
- // writeMicroseconds will contain the calculated value for us
101
- // for any user defined min and max, but we must use default min max
102
101
value = improved_map (value, 0 , 180 , _minUs, _maxUs);
103
102
} else if (value > _maxUs) {
104
- value = constrain (value, _minUs, _maxUs) ;
103
+ value = _maxUs;
105
104
}
106
105
writeMicroseconds (value);
107
106
}
@@ -119,8 +118,7 @@ void Servo::writeMicroseconds(int value)
119
118
120
119
int Servo::read () // return the value as degrees
121
120
{
122
- // read returns the angle for an assumed 0-180, so we calculate using
123
- // the normal min/max constants and not user defined ones
121
+ // read returns the angle for an assumed 0-180
124
122
return improved_map (readMicroseconds (), _minUs, _maxUs, 0 , 180 );
125
123
}
126
124
0 commit comments