From 1c1978107df78adeb58e444eea2a11c2f82660f7 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Sat, 17 Feb 2024 12:17:26 +0000 Subject: [PATCH] Move all ramp variables to class members. To move the ramp behavior to the Loop() method there will need to be a global timer. --- ino/contact_printer/ContactPrinter.cpp | 33 ++++++++++++++------------ ino/contact_printer/ContactPrinter.h | 4 ++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ino/contact_printer/ContactPrinter.cpp b/ino/contact_printer/ContactPrinter.cpp index d9e38bf..f904086 100644 --- a/ino/contact_printer/ContactPrinter.cpp +++ b/ino/contact_printer/ContactPrinter.cpp @@ -31,15 +31,15 @@ void ContactPrinter::Setup () { void ContactPrinter::Start () { RampTakeup(0, takeup_pwm_duty_cycle, takeup_ramp_time); delay(100); - //drive_motor.Start(); + drive_motor.Start(); } void ContactPrinter::Stop () { - //drive_motor.Start(); + 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_picture_pin_ccw, LOW); digitalWrite(takeup_stock_pin_cw, LOW); digitalWrite(takeup_stock_pin_ccw, LOW); } @@ -63,10 +63,10 @@ void ContactPrinter::SetDirectionPicture(bool clockwise) { //linear 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; - bool dir = end < start; + takeup_ramp_steps = abs(start - end); + takeup_ramp_step = round(time / takeup_ramp_steps); + takeup_pwm_duty_cycle = start; + takeup_ramp_dir = end < start; if (takeup_picture_cw) { digitalWrite(takeup_picture_pin_cw, HIGH); @@ -82,20 +82,23 @@ void ContactPrinter::RampTakeup(uint16_t start, uint16_t end, uint16_t time) { digitalWrite(takeup_stock_pin_cw, LOW); digitalWrite(takeup_stock_pin_ccw, HIGH); } + takeup_ramping = true; - for (uint16_t i = 0; i < steps; i++) { - if (pwm <= 0 || pwm >= 256) { + for (uint16_t i = 0; i < takeup_ramp_steps; i++) { + if (takeup_pwm_duty_cycle <= 0 || takeup_pwm_duty_cycle >= 255) { + takeup_ramping = false; break; } - ledcWrite(takeup_picture_pwm_channel, pwm); - ledcWrite(takeup_stock_pwm_channel, pwm); - delay(step); - if (dir) { - pwm++; + ledcWrite(takeup_picture_pwm_channel, takeup_pwm_duty_cycle); + ledcWrite(takeup_stock_pwm_channel, takeup_pwm_duty_cycle); + delay(takeup_ramp_step); + if (takeup_ramp_dir) { + takeup_pwm_duty_cycle++; } else { - pwm--; + takeup_pwm_duty_cycle--; } } + takeup_ramping = false; } bool ContactPrinter::IsRunning () { diff --git a/ino/contact_printer/ContactPrinter.h b/ino/contact_printer/ContactPrinter.h index 466227d..99fc1ef 100644 --- a/ino/contact_printer/ContactPrinter.h +++ b/ino/contact_printer/ContactPrinter.h @@ -30,6 +30,10 @@ class ContactPrinter { volatile float takeup_speed = 1.0; //estimated rpm volatile uint16_t takeup_pwm_duty_cycle = 0; + volatile uint16_t takeup_ramp_steps = 0; //# of steps + volatile uint16_t takeup_ramp_step = 0; //length of step (ms) + volatile boolean takeup_ramp_dir = true; //true = up, false = down + volatile boolean takeup_ramping = false; volatile bool takeup_picture_cw = false; volatile bool takeup_picture_ccw = true;