From 344e1e8d1dec3341ea3d2f7ca0bb76d7621bc07a Mon Sep 17 00:00:00 2001 From: mattmcw Date: Fri, 16 Feb 2024 18:57:57 -0500 Subject: [PATCH] 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. --- ino/contact_printer/ContactPrinter.cpp | 27 +++++++++++++------------- ino/contact_printer/ContactPrinter.h | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/ino/contact_printer/ContactPrinter.cpp b/ino/contact_printer/ContactPrinter.cpp index 575b83e..d9e38bf 100644 --- a/ino/contact_printer/ContactPrinter.cpp +++ b/ino/contact_printer/ContactPrinter.cpp @@ -38,7 +38,10 @@ void ContactPrinter::Stop () { //drive_motor.Start(); delay(100); 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) { @@ -63,31 +66,29 @@ void ContactPrinter::RampTakeup(uint16_t start, uint16_t end, uint16_t time) { uint16_t steps = abs(start - end); uint16_t step = round(time / steps); uint16_t pwm = start; - uint8_t takeup_picture_pin; - uint8_t takeup_stock_pin; bool dir = end < start; if (takeup_picture_cw) { - takeup_picture_pin = takeup_picture_pin_cw; - //analogWrite(takeup_picture_pin_ccw, 0); + digitalWrite(takeup_picture_pin_cw, HIGH); + digitalWrite(takeup_picture_pin_ccw, LOW); } else { - takeup_picture_pin = takeup_picture_pin_ccw; - //analogWrite(takeup_picture_pin_cw, 0); + digitalWrite(takeup_picture_pin_cw, LOW); + digitalWrite(takeup_picture_pin_ccw, HIGH); } if (takeup_stock_cw) { - takeup_stock_pin = takeup_stock_pin_cw; - //analogWrite(takeup_stock_pin_ccw, 0); + digitalWrite(takeup_stock_pin_cw, HIGH); + digitalWrite(takeup_stock_pin_ccw, LOW); } else { - takeup_stock_pin = takeup_stock_pin_cw; - //analogWrite(takeup_stock_pin_cw, 0); + digitalWrite(takeup_stock_pin_cw, LOW); + digitalWrite(takeup_stock_pin_ccw, HIGH); } for (uint16_t i = 0; i < steps; i++) { if (pwm <= 0 || pwm >= 256) { break; } - //analogWrite(takeup_picture_pin, pwm); - //analogWrite(takeup_stock_pin, pwm); + ledcWrite(takeup_picture_pwm_channel, pwm); + ledcWrite(takeup_stock_pwm_channel, pwm); delay(step); if (dir) { pwm++; diff --git a/ino/contact_printer/ContactPrinter.h b/ino/contact_printer/ContactPrinter.h index f47f955..466227d 100644 --- a/ino/contact_printer/ContactPrinter.h +++ b/ino/contact_printer/ContactPrinter.h @@ -29,7 +29,7 @@ class ContactPrinter { volatile float drive_speed = 1.0; //calculated 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_ccw = true;