Commit 9d4d7643 authored by MagoKimbra's avatar MagoKimbra

Fix Servo

parent 6bb5dd51
...@@ -299,13 +299,10 @@ void Servo::writeMicroseconds(int value) ...@@ -299,13 +299,10 @@ void Servo::writeMicroseconds(int value)
} }
} }
int Servo::read() // return the value as degrees // return the value as degrees
{ int Servo::read() { return map(this->readMicroseconds() + 1, SERVO_MIN(), SERVO_MAX(), 0, 180); }
return map(readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180);
}
int Servo::readMicroseconds() int Servo::readMicroseconds() {
{
unsigned int pulsewidth; unsigned int pulsewidth;
if (this->servoIndex != INVALID_SERVO) if (this->servoIndex != INVALID_SERVO)
pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION; pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION;
...@@ -315,9 +312,16 @@ int Servo::readMicroseconds() ...@@ -315,9 +312,16 @@ int Servo::readMicroseconds()
return pulsewidth; return pulsewidth;
} }
bool Servo::attached() bool Servo::attached() { return servos[this->servoIndex].Pin.isActive; }
{
return servos[this->servoIndex].Pin.isActive; void Servo::move(int value) {
if (this->attach(0) >= 0) {
this->write(value);
#if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
delay(SERVO_DEACTIVATION_DELAY);
this->detach();
#endif
}
} }
#endif #endif
...@@ -83,19 +83,21 @@ typedef struct { ...@@ -83,19 +83,21 @@ typedef struct {
volatile unsigned int ticks; volatile unsigned int ticks;
} servo_t; } servo_t;
class Servo class Servo {
{ public:
public:
Servo(); Servo();
uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
void detach(); void detach();
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
void writeMicroseconds(int value); // Write pulse width in microseconds void writeMicroseconds(int value); // Write pulse width in microseconds
void move(int value); // attach the servo, then move to value
// if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
// if DEACTIVATE_SERVOS_AFTER_MOVE wait SERVO_DEACTIVATION_DELAY, then detach
int read(); // returns current pulse width as an angle between 0 and 180 degrees 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) 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 bool attached(); // return true if this servo is attached, otherwise false
private: private:
uint8_t servoIndex; // index into the channel data for this servo 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 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 int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment