Pages: [1]   Go Down
Author Topic: Servo library handling speed  (Read 1279 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 - downloaded 8 times.)
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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.
Code:
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
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

when updated I will review it again
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: