|
|
@ -56,52 +56,50 @@ uint8_t ServoCount = 0; |
|
|
|
#define SERVO_OVERFLOW ((uint16_t)round((double)TAU_CYC / SERVO_PRESCALER)) |
|
|
|
|
|
|
|
// Unit conversions
|
|
|
|
#define US_TO_COMPARE(us) ((uint16_t)map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW)) |
|
|
|
#define COMPARE_TO_US(c) ((uint32_t)map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC)) |
|
|
|
#define ANGLE_TO_US(a) ((uint16_t)(map((a), this->minAngle, this->maxAngle, \ |
|
|
|
SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW))) |
|
|
|
#define US_TO_ANGLE(us) ((int16_t)(map((us), SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW, \ |
|
|
|
this->minAngle, this->maxAngle))) |
|
|
|
|
|
|
|
void libServo::servoWrite(uint8_t pin, uint16_t duty_cycle) { |
|
|
|
#define US_TO_COMPARE(us) uint16_t(map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW)) |
|
|
|
#define COMPARE_TO_US(c) uint32_t(map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC)) |
|
|
|
#define ANGLE_TO_US(a) uint16_t(map((a), minAngle, maxAngle, SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW)) |
|
|
|
#define US_TO_ANGLE(us) int16_t(map((us), SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW, minAngle, maxAngle)) |
|
|
|
|
|
|
|
void libServo::servoWrite(uint8_t inPin, uint16_t duty_cycle) { |
|
|
|
#ifdef SERVO0_TIMER_NUM |
|
|
|
if (this->servoIndex == 0) { |
|
|
|
this->pwmSetDuty(duty_cycle); |
|
|
|
if (servoIndex == 0) { |
|
|
|
pwmSetDuty(duty_cycle); |
|
|
|
return; |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
timer_dev *tdev = PIN_MAP[pin].timer_device; |
|
|
|
uint8_t tchan = PIN_MAP[pin].timer_channel; |
|
|
|
timer_dev *tdev = PIN_MAP[inPin].timer_device; |
|
|
|
uint8_t tchan = PIN_MAP[inPin].timer_channel; |
|
|
|
if (tdev) timer_set_compare(tdev, tchan, duty_cycle); |
|
|
|
} |
|
|
|
|
|
|
|
libServo::libServo() { |
|
|
|
this->servoIndex = ServoCount < MAX_SERVOS ? ServoCount++ : INVALID_SERVO; |
|
|
|
servoIndex = ServoCount < MAX_SERVOS ? ServoCount++ : INVALID_SERVO; |
|
|
|
} |
|
|
|
|
|
|
|
bool libServo::attach(const int32_t pin, const int32_t minAngle, const int32_t maxAngle) { |
|
|
|
if (this->servoIndex >= MAX_SERVOS) return false; |
|
|
|
if (pin >= BOARD_NR_GPIO_PINS) return false; |
|
|
|
bool libServo::attach(const int32_t inPin, const int32_t inMinAngle, const int32_t inMaxAngle) { |
|
|
|
if (servoIndex >= MAX_SERVOS) return false; |
|
|
|
if (inPin >= BOARD_NR_GPIO_PINS) return false; |
|
|
|
|
|
|
|
this->minAngle = minAngle; |
|
|
|
this->maxAngle = maxAngle; |
|
|
|
this->angle = -1; |
|
|
|
minAngle = inMinAngle; |
|
|
|
maxAngle = inMaxAngle; |
|
|
|
angle = -1; |
|
|
|
|
|
|
|
#ifdef SERVO0_TIMER_NUM |
|
|
|
if (this->servoIndex == 0 && this->setupSoftPWM(pin)) { |
|
|
|
this->pin = pin; // set attached()
|
|
|
|
if (servoIndex == 0 && setupSoftPWM(inPin)) { |
|
|
|
pin = inPin; // set attached()
|
|
|
|
return true; |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
if (!PWM_PIN(pin)) return false; |
|
|
|
if (!PWM_PIN(inPin)) return false; |
|
|
|
|
|
|
|
timer_dev *tdev = PIN_MAP[pin].timer_device; |
|
|
|
//uint8_t tchan = PIN_MAP[pin].timer_channel;
|
|
|
|
timer_dev *tdev = PIN_MAP[inPin].timer_device; |
|
|
|
//uint8_t tchan = PIN_MAP[inPin].timer_channel;
|
|
|
|
|
|
|
|
SET_PWM(pin); |
|
|
|
servoWrite(pin, 0); |
|
|
|
SET_PWM(inPin); |
|
|
|
servoWrite(inPin, 0); |
|
|
|
|
|
|
|
timer_pause(tdev); |
|
|
|
timer_set_prescaler(tdev, SERVO_PRESCALER - 1); // prescaler is 1-based
|
|
|
@ -109,25 +107,24 @@ bool libServo::attach(const int32_t pin, const int32_t minAngle, const int32_t m |
|
|
|
timer_generate_update(tdev); |
|
|
|
timer_resume(tdev); |
|
|
|
|
|
|
|
this->pin = pin; // set attached()
|
|
|
|
|
|
|
|
pin = inPin; // set attached()
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
bool libServo::detach() { |
|
|
|
if (!this->attached()) return false; |
|
|
|
this->angle = -1; |
|
|
|
servoWrite(this->pin, 0); |
|
|
|
if (!attached()) return false; |
|
|
|
angle = -1; |
|
|
|
servoWrite(pin, 0); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t libServo::read() const { |
|
|
|
if (this->attached()) { |
|
|
|
if (attached()) { |
|
|
|
#ifdef SERVO0_TIMER_NUM |
|
|
|
if (this->servoIndex == 0) return this->angle; |
|
|
|
if (servoIndex == 0) return angle; |
|
|
|
#endif |
|
|
|
timer_dev *tdev = PIN_MAP[this->pin].timer_device; |
|
|
|
uint8_t tchan = PIN_MAP[this->pin].timer_channel; |
|
|
|
timer_dev *tdev = PIN_MAP[pin].timer_device; |
|
|
|
uint8_t tchan = PIN_MAP[pin].timer_channel; |
|
|
|
return US_TO_ANGLE(COMPARE_TO_US(timer_get_compare(tdev, tchan))); |
|
|
|
} |
|
|
|
return 0; |
|
|
@ -137,12 +134,12 @@ void libServo::move(const int32_t value) { |
|
|
|
constexpr uint16_t servo_delay[] = SERVO_DELAY; |
|
|
|
static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long."); |
|
|
|
|
|
|
|
if (this->attached()) { |
|
|
|
this->angle = constrain(value, this->minAngle, this->maxAngle); |
|
|
|
servoWrite(this->pin, US_TO_COMPARE(ANGLE_TO_US(this->angle))); |
|
|
|
safe_delay(servo_delay[this->servoIndex]); |
|
|
|
if (attached()) { |
|
|
|
angle = constrain(value, minAngle, maxAngle); |
|
|
|
servoWrite(pin, US_TO_COMPARE(ANGLE_TO_US(angle))); |
|
|
|
safe_delay(servo_delay[servoIndex]); |
|
|
|
#if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE) |
|
|
|
this->detach(); |
|
|
|
detach(); |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
@ -169,13 +166,13 @@ void libServo::move(const int32_t value) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
bool libServo::setupSoftPWM(const int32_t pin) { |
|
|
|
bool libServo::setupSoftPWM(const int32_t inPin) { |
|
|
|
timer_dev *tdev = get_timer_dev(SERVO0_TIMER_NUM); |
|
|
|
if (!tdev) return false; |
|
|
|
#ifdef SERVO0_PWM_OD |
|
|
|
OUT_WRITE_OD(pin, 1); |
|
|
|
OUT_WRITE_OD(inPin, 1); |
|
|
|
#else |
|
|
|
OUT_WRITE(pin, 0); |
|
|
|
OUT_WRITE(inPin, 0); |
|
|
|
#endif |
|
|
|
|
|
|
|
timer_pause(tdev); |
|
|
@ -206,9 +203,9 @@ void libServo::move(const int32_t value) { |
|
|
|
timer_disable_irq(tdev, 1); |
|
|
|
timer_disable_irq(tdev, 2); |
|
|
|
#ifdef SERVO0_PWM_OD |
|
|
|
OUT_WRITE_OD(this->pin, 1); // off
|
|
|
|
OUT_WRITE_OD(pin, 1); // off
|
|
|
|
#else |
|
|
|
OUT_WRITE(this->pin, 0); |
|
|
|
OUT_WRITE(pin, 0); |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
@ -221,7 +218,7 @@ void libServo::move(const int32_t value) { |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
bool libServo::setupSoftPWM(const int32_t pin) { return false; } |
|
|
|
bool libServo::setupSoftPWM(const int32_t inPin) { return false; } |
|
|
|
void libServo::pwmSetDuty(const uint16_t duty_cycle) {} |
|
|
|
void libServo::pauseSoftPWM() {} |
|
|
|
|
|
|
|