Servo library handling speed

Hello!

I have made a modification on the servo library to handle speed of the servo.

I dont know if i can propose it somewere. If someone is interested, feel free to contact me

By

Robin

i would surely be interested in something like that. Could you share the code here or somewhere else for a glimpse.

Here it is!

I haven’t take too much time to made a clear and good code, but it is working.
I didnt change the name of the class, so I can change back to the older library just by changing the include.

I wrote an example.

The new function available are:

void writeMicrosecondsWithSpeed(int value);
void writeSpeed(int value);
bool moving();

The others are working without modifications.

By,

Robin

ServoSpeed.zip (7.59 KB)

A quick look at your library made me wonder if values can become negative? It seems that pulseWidths and speeds are always 0 or positive.
If not, there is quite some improvement to be made by using unsigned ints.

something like this:

class Servo
{
public:
  Servo();
  uint8_t attach(uint8_t pin);           // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
  uint8_t attach(uint8_t pin, uint16_t min, uint16_t max); // as above but also sets min and max values for writes. 
  void detach();
  void write(uint16_t value);                   // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds 
  void writeMicroseconds(uint16_t value);   // Write pulse width in microseconds 
  void writeWithSpeed(uint16_t value);
  void writeMicrosecondsWithSpeed(uint16_t value);
  void writeSpeed(uint16_t value);
  bool moving();
  uint8_t read();                        // returns current pulse width as an angle between 0 and 180 degrees
  uint16_t readMicroseconds();            // returns current pulse width in microseconds for this servo (was read_us() in first release)
  bool attached();                   // return true if this servo is attached, otherwise false 
  
private:
   uint8_t servoIndex;               // index into the channel data for this servo
   int8_t min;                       // minimum is this value times 4 added to MIN_PULSE_WIDTH    
   int8_t max;                       // maximum is this value times 4 added to MAX_PULSE_WIDTH   
};

write microSeconds() has a potential bug??: // if value == SERVO MIN subtracting - TRIM_DURATION makes it even smaller? is that correct?

furthermore you could use the constrain and skip the channel >=0 test.

void Servo::writeMicroseconds(int value)
{
  // calculate and store the values for the given channel
  byte channel = this->servoIndex;
  if( channel < MAX_SERVOS )                      // channel is byte so always >= 0
  {
    value = constrain( value, SERVO_MIN(), SERVO_MAX() );  // shorter code
    value = value - TRIM_DURATION;     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ?
    ...

further a smaller bug in readMicroseconds() the signature should be unsigend int Servo::readMicroseconds() or uint16_t Servo::readMicroseconds()

Not reviewed all code but some of the remarks are applicable in multiple places of the lib

my 2 cents

many thanks rob for the code. works as said. robtillaart, thanks for pointing that out. i will post the updated file soon.

when updated I will review it again