Add Servo::move() to servo.cpp

move(pin, angel) - Sequence of attach(pin), write(angel),
                   if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.

As we have jitter on the servos during moves of the steppers, and detaching them improves this behaviour,
the usual sequence to handle a servo movement is:
attach(pin)
write(angel)
delay(until the servo finished the move)
detache()

Here a function to handle the complete sequence.
This commit is contained in:
AnHardt 2015-07-12 23:02:47 +02:00 committed by Richard Wackerbarth
parent 8a01e5fb7a
commit 2ddb2a2be9
2 changed files with 21 additions and 1 deletions

View file

@ -35,6 +35,8 @@
write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
writeMicroseconds() - Sets the servo pulse width in microseconds
move(pin, angel) - Sequence of attach(pin), write(angel),
if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.
read() - Gets the last written servo pulse width as an angle between 0 and 180.
readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
attached() - Returns true if there is a servo attached.
@ -301,4 +303,17 @@ int Servo::readMicroseconds() {
bool Servo::attached() { return servos[this->servoIndex].Pin.isActive; }
uint8_t Servo::move(int pin, int value) {
uint8_t ret;
ret = this->attach(pin);
if (ret) {
this->write(value);
#ifdef DEACTIVATE_SERVOS_AFTER_MOVE && (SERVO_DEACTIVATION_DELAY > 0)
delay(SERVO_DEACTIVATION_DELAY);
this->detach();
#endif
}
return ret;
}
#endif

View file

@ -40,6 +40,8 @@
readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
attached() - Returns true if there is a servo attached.
detach() - Stops an attached servos from pulsing its i/o pin.
move(pin, angel) - Sequence of attach(pin), write(angel),
if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.
*/
#ifndef servo_h
@ -120,6 +122,9 @@ class Servo {
void detach();
void write(int value); // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
void writeMicroseconds(int value); // Write pulse width in microseconds
uint8_t move(int pin, int value); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure.
// if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds.
// if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.
int read(); // returns current pulse width as an angle between 0 and 180 degrees
int 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