Implement pwm ramp up with ledcWrite(), not analogWrite(). Use directional pins to start in particular direction and stop in both. Ramp behavior should move to loop so that it is not blocking loop behavior.

This commit is contained in:
Matt McWilliams 2024-02-16 18:57:57 -05:00
parent 899b52777a
commit 344e1e8d1d
2 changed files with 15 additions and 14 deletions

View File

@ -38,7 +38,10 @@ void ContactPrinter::Stop () {
//drive_motor.Start(); //drive_motor.Start();
delay(100); delay(100);
RampTakeup( takeup_pwm_duty_cycle, 0, takeup_ramp_time); RampTakeup( takeup_pwm_duty_cycle, 0, takeup_ramp_time);
digitalWrite(takeup_picture_pin_cw, LOW);
digitalWrite(takeup_picture_pin_ccw, LOW)
digitalWrite(takeup_stock_pin_cw, LOW);
digitalWrite(takeup_stock_pin_ccw, LOW);
} }
void ContactPrinter::SetSpeedTakeup(float speed) { void ContactPrinter::SetSpeedTakeup(float speed) {
@ -63,31 +66,29 @@ void ContactPrinter::RampTakeup(uint16_t start, uint16_t end, uint16_t time) {
uint16_t steps = abs(start - end); uint16_t steps = abs(start - end);
uint16_t step = round(time / steps); uint16_t step = round(time / steps);
uint16_t pwm = start; uint16_t pwm = start;
uint8_t takeup_picture_pin;
uint8_t takeup_stock_pin;
bool dir = end < start; bool dir = end < start;
if (takeup_picture_cw) { if (takeup_picture_cw) {
takeup_picture_pin = takeup_picture_pin_cw; digitalWrite(takeup_picture_pin_cw, HIGH);
//analogWrite(takeup_picture_pin_ccw, 0); digitalWrite(takeup_picture_pin_ccw, LOW);
} else { } else {
takeup_picture_pin = takeup_picture_pin_ccw; digitalWrite(takeup_picture_pin_cw, LOW);
//analogWrite(takeup_picture_pin_cw, 0); digitalWrite(takeup_picture_pin_ccw, HIGH);
} }
if (takeup_stock_cw) { if (takeup_stock_cw) {
takeup_stock_pin = takeup_stock_pin_cw; digitalWrite(takeup_stock_pin_cw, HIGH);
//analogWrite(takeup_stock_pin_ccw, 0); digitalWrite(takeup_stock_pin_ccw, LOW);
} else { } else {
takeup_stock_pin = takeup_stock_pin_cw; digitalWrite(takeup_stock_pin_cw, LOW);
//analogWrite(takeup_stock_pin_cw, 0); digitalWrite(takeup_stock_pin_ccw, HIGH);
} }
for (uint16_t i = 0; i < steps; i++) { for (uint16_t i = 0; i < steps; i++) {
if (pwm <= 0 || pwm >= 256) { if (pwm <= 0 || pwm >= 256) {
break; break;
} }
//analogWrite(takeup_picture_pin, pwm); ledcWrite(takeup_picture_pwm_channel, pwm);
//analogWrite(takeup_stock_pin, pwm); ledcWrite(takeup_stock_pwm_channel, pwm);
delay(step); delay(step);
if (dir) { if (dir) {
pwm++; pwm++;

View File

@ -29,7 +29,7 @@ class ContactPrinter {
volatile float drive_speed = 1.0; //calculated rpm volatile float drive_speed = 1.0; //calculated rpm
volatile float takeup_speed = 1.0; //estimated rpm volatile float takeup_speed = 1.0; //estimated rpm
volatile uint16_t takeup_pwm_duty_cycle = 255; volatile uint16_t takeup_pwm_duty_cycle = 0;
volatile bool takeup_picture_cw = false; volatile bool takeup_picture_cw = false;
volatile bool takeup_picture_ccw = true; volatile bool takeup_picture_ccw = true;