Tweaks to Servo classes

This commit is contained in:
Scott Lahteine 2017-09-27 03:33:29 -05:00
parent f582f1bf4e
commit 0cb4d25431
4 changed files with 21 additions and 24 deletions

View File

@ -89,11 +89,11 @@
this->servoIndex = INVALID_SERVO; // too many servos this->servoIndex = INVALID_SERVO; // too many servos
} }
int8_t Servo::attach(int pin) { int8_t Servo::attach(const int pin) {
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
} }
int8_t Servo::attach(int pin, int min, int max) { int8_t Servo::attach(const int pin, const int min, const int max) {
if (this->servoIndex >= MAX_SERVOS) return -1; if (this->servoIndex >= MAX_SERVOS) return -1;
@ -113,7 +113,7 @@
servo_info[this->servoIndex].Pin.isActive = false; servo_info[this->servoIndex].Pin.isActive = false;
} }
void Servo::write(int value) { void Servo::write(const int value) {
if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX()); value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX());
// odd - this sets zero degrees to 544 and 180 degrees to 2400 microseconds but the literature says // odd - this sets zero degrees to 544 and 180 degrees to 2400 microseconds but the literature says
@ -122,7 +122,7 @@
this->writeMicroseconds(value); this->writeMicroseconds(value);
} }
void Servo::writeMicroseconds(int value) { void Servo::writeMicroseconds(const int value) {
// calculate and store the values for the given channel // calculate and store the values for the given channel
byte channel = this->servoIndex; byte channel = this->servoIndex;
if (channel < MAX_SERVOS) { // ensure channel is valid if (channel < MAX_SERVOS) { // ensure channel is valid
@ -146,7 +146,7 @@
bool Servo::attached() { return servo_info[this->servoIndex].Pin.isActive; } bool Servo::attached() { return servo_info[this->servoIndex].Pin.isActive; }
void Servo::move(int value) { void Servo::move(const int value) {
if (this->attach(0) >= 0) { // notice the pin number is zero here if (this->attach(0) >= 0) { // notice the pin number is zero here
this->write(value); this->write(value);
delay(SERVO_DELAY); delay(SERVO_DELAY);

View File

@ -39,12 +39,12 @@
class Servo { class Servo {
public: public:
Servo(); Servo();
int8_t attach(int pin); // attach the given pin to the next free channel, set pinMode, return channel number (-1 on fail) int8_t attach(const int pin); // attach the given pin to the next free channel, set pinMode, return channel number (-1 on fail)
int8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. int8_t attach(const int pin, const int min, const 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 it is treated as an angle, otherwise as pulse width in microseconds void write(const 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 void writeMicroseconds(const int value); // write pulse width in microseconds
void move(int value); // attach the servo, then move to value void move(const 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 value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
// if DEACTIVATE_SERVOS_AFTER_MOVE wait SERVO_DELAY, then detach // if DEACTIVATE_SERVOS_AFTER_MOVE wait SERVO_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

View File

@ -1,20 +1,18 @@
#if defined(__MK64FX512__) || defined(__MK66FX1M0__) #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
#include "HAL_Servo_Teensy.h" #include "HAL_Servo_Teensy.h"
#include "../../inc/MarlinConfig.h" #include "../../inc/MarlinConfig.h"
int8_t libServo::attach(const int pin) {
int8_t libServo::attach(int pin) {
if (this->servoIndex >= MAX_SERVOS) return -1; if (this->servoIndex >= MAX_SERVOS) return -1;
return Servo::attach(pin); return Servo::attach(pin);
} }
int8_t libServo::attach(int pin, int min, int max) { int8_t libServo::attach(const int pin, const int min, const int max) {
return Servo::attach(pin, min, max); return Servo::attach(pin, min, max);
} }
void libServo::move(int value) { void libServo::move(const int value) {
if (this->attach(0) >= 0) { if (this->attach(0) >= 0) {
this->write(value); this->write(value);
delay(SERVO_DELAY); delay(SERVO_DELAY);
@ -24,5 +22,4 @@ void libServo::move(int value) {
} }
} }
#endif // __MK64FX512__ || __MK66FX1M0__
#endif

View File

@ -6,13 +6,13 @@
// Inherit and expand on the official library // Inherit and expand on the official library
class libServo : public Servo { class libServo : public Servo {
public: public:
int8_t attach(int pin); int8_t attach(const int pin);
int8_t attach(int pin, int min, int max); int8_t attach(const int pin, const int min, const int max);
void move(int value); void move(const int value);
private: private:
uint16_t min_ticks; uint16_t min_ticks;
uint16_t max_ticks; uint16_t max_ticks;
uint8_t servoIndex; // index into the channel data for this servo uint8_t servoIndex; // index into the channel data for this servo
}; };
#endif #endif // HAL_Servo_Teensy_h