@@ -27,15 +27,15 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2727uint32_t Servo::_servoMap = 0 ;
2828
2929// 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)
3131int improved_map (int value, int minIn, int maxIn, int minOut, int maxOut)
3232{
3333 const int rangeIn = maxIn - minIn;
3434 const int rangeOut = maxOut - minOut;
3535 const int deltaIn = value - minIn;
3636 // 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 ;
3939
4040 return ((deltaIn * rangeOut * fixedDecimal) / (rangeIn) + fixedHalfDecimal) / fixedDecimal + minOut;
4141}
@@ -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 500-2500 us, these are extreme
73+ // keep the min and max within 200-3000 us, these are extreme
7474 // ranges and should support extreme servos while maintaining
7575 // 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));
7878
7979 write (_valueUs);
8080
@@ -87,21 +87,20 @@ void Servo::detach()
8787 _servoMap &= ~(1 << _pin);
8888 stopWaveform (_pin);
8989 _attached = false ;
90+ _valueUs = DEFAULT_NEUTRAL_PULSE_WIDTH;
9091 digitalWrite (_pin, LOW);
9192 }
9293}
9394
9495void Servo::write (int value)
9596{
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)
9798 if (value < _minUs) {
9899 // assumed to be 0-180 degrees servo
99100 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
102101 value = improved_map (value, 0 , 180 , _minUs, _maxUs);
103102 } else if (value > _maxUs) {
104- value = constrain (value, _minUs, _maxUs) ;
103+ value = _maxUs;
105104 }
106105 writeMicroseconds (value);
107106}
@@ -119,8 +118,7 @@ void Servo::writeMicroseconds(int value)
119118
120119int Servo::read () // return the value as degrees
121120{
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
124122 return improved_map (readMicroseconds (), _minUs, _maxUs, 0 , 180 );
125123}
126124
0 commit comments