From 2401fcf8e78c14ab3690daca7150d5529a9eb1d2 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Sat, 17 Feb 2024 14:57:26 +0000 Subject: [PATCH] Create the Lamp class which will abstract to different lamp types before one is settled on. --- ino/contact_printer/ContactPrinter.cpp | 31 ++++++++++++++++++++----- ino/contact_printer/ContactPrinter.h | 15 ++++++++++-- ino/contact_printer/Lamp.cpp | 5 ++++ ino/contact_printer/Lamp.h | 17 ++++++++++++++ ino/contact_printer/contact_printer.ino | 4 ++++ 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 ino/contact_printer/Lamp.cpp create mode 100644 ino/contact_printer/Lamp.h diff --git a/ino/contact_printer/ContactPrinter.cpp b/ino/contact_printer/ContactPrinter.cpp index 8aedbae..f645fb5 100644 --- a/ino/contact_printer/ContactPrinter.cpp +++ b/ino/contact_printer/ContactPrinter.cpp @@ -10,6 +10,7 @@ void ContactPrinter::Setup () { pinMode(takeup_picture_pin_ccw, OUTPUT); pinMode(takeup_stock_pin_cw, OUTPUT); pinMode(takeup_stock_pin_ccw, OUTPUT); + pinMode(start_button_pin, INPUT_PULLUP); drive_motor.Setup(); @@ -26,18 +27,22 @@ void ContactPrinter::Setup () { digitalWrite(takeup_picture_pin_ccw, LOW); digitalWrite(takeup_stock_pin_cw, LOW); digitalWrite(takeup_stock_pin_ccw, LOW); + + SetSpeedTakeup(0.4); + //SetSpeedDrive(1.0); } void ContactPrinter::Start () { RampTakeup(0, takeup_pwm_duty_cycle, takeup_ramp_time); delay(100); drive_motor.Start(); + running = true; } void ContactPrinter::Stop () { drive_motor.Start(); 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); @@ -62,11 +67,13 @@ void ContactPrinter::SetDirectionPicture(bool clockwise) { } //linear -void ContactPrinter::RampTakeup(uint16_t start, uint16_t end, uint16_t time) { - takeup_ramp_steps = abs(start - end); +void ContactPrinter::RampTakeup(uint16_t start_pwm, uint16_t end_pwm, uint16_t time) { + takeup_ramp_steps = abs(start_pwm - end_pwm); takeup_ramp_step = round(time / takeup_ramp_steps); - takeup_pwm_duty_cycle = start; - takeup_ramp_dir = end < start; + takeup_pwm_duty_cycle = start_pwm; + takeup_ramp_dir = end_pwm < start_pwm; + takeup_ramp_current_step = 0; + takeup_ramping = true; if (takeup_picture_cw) { digitalWrite(takeup_picture_pin_cw, HIGH); @@ -82,7 +89,6 @@ 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 < takeup_ramp_steps; i++) { if (takeup_pwm_duty_cycle <= 0 || takeup_pwm_duty_cycle >= 255) { @@ -101,6 +107,16 @@ void ContactPrinter::RampTakeup(uint16_t start, uint16_t end, uint16_t time) { takeup_ramping = false; } +void ContactPrinter::RampTakeupLoop () { + +} + +void ContactPrinter::ButtonLoop () { + if (!running && digitalRead(start_button_pin) == LOW) { + Start(); + } +} + bool ContactPrinter::IsRunning () { return running; } @@ -108,4 +124,7 @@ bool ContactPrinter::IsRunning () { void ContactPrinter::Loop () { timer = millis(); drive_motor.Loop(); + if (takeup_ramping) { + RampTakeupLoop(); + } } \ No newline at end of file diff --git a/ino/contact_printer/ContactPrinter.h b/ino/contact_printer/ContactPrinter.h index 31e2660..589d86b 100644 --- a/ino/contact_printer/ContactPrinter.h +++ b/ino/contact_printer/ContactPrinter.h @@ -3,6 +3,7 @@ #include #include "DriveMotor.h" +#include "Lamp.h" class ContactPrinter { @@ -10,6 +11,7 @@ class ContactPrinter { //use default drive motor pins DriveMotor drive_motor; + Lamp lamp; const uint16_t serial_delay = 5; const uint16_t baud = 115200; @@ -22,6 +24,8 @@ class ContactPrinter { const uint8_t takeup_stock_pin_cw = 18; const uint8_t takeup_stock_pin_ccw = 5; + const uint8_t start_button_pin = 34; + const uint32_t pwm_frequency = 30000; const uint8_t takeup_picture_pwm_channel = 1; const uint8_t takeup_stock_pwm_channel = 2; @@ -36,6 +40,12 @@ class ContactPrinter { 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 uint16_t takeup_ramp_time = 500; //default ramp time (ms) + volatile long takeup_ramp_start = 0; //time to start ramping + volatile long takeup_ramp_current_step = 0; + volatile long takeup_ramp_next_step_start = 0; + volatile boolean takeup_ramping = false; volatile bool takeup_picture_cw = false; @@ -44,8 +54,6 @@ class ContactPrinter { volatile bool takeup_stock_cw = true; volatile bool takeup_stock_ccw = true; - volatile uint16_t takeup_ramp_time = 500; - volatile bool running = false; public: @@ -62,6 +70,9 @@ class ContactPrinter { void SetDirectionPicture(bool clockwise); void RampTakeup(uint16_t start, uint16_t end, uint16_t time); + void RampTakeupLoop(); + + void ButtonLoop(); bool IsRunning (); }; diff --git a/ino/contact_printer/Lamp.cpp b/ino/contact_printer/Lamp.cpp new file mode 100644 index 0000000..758e488 --- /dev/null +++ b/ino/contact_printer/Lamp.cpp @@ -0,0 +1,5 @@ +#include "Lamp.h" + +Lamp::Lamp () { + // +} \ No newline at end of file diff --git a/ino/contact_printer/Lamp.h b/ino/contact_printer/Lamp.h new file mode 100644 index 0000000..357fde0 --- /dev/null +++ b/ino/contact_printer/Lamp.h @@ -0,0 +1,17 @@ +#ifndef LAMP +#define LAMP + +#include + +class Lamp { + private: + volatile boolean on = false; + volatile uint8_t lamp_pin_a = 33; + + public: + Lamp(); + void Setup(); + void Loop(); +}; + +#endif \ No newline at end of file diff --git a/ino/contact_printer/contact_printer.ino b/ino/contact_printer/contact_printer.ino index 5fb6ee5..e465cc6 100644 --- a/ino/contact_printer/contact_printer.ino +++ b/ino/contact_printer/contact_printer.ino @@ -23,6 +23,10 @@ * 27 Drive Encoder A * 26 Drive Encoder B * + * 34 Start Button + * + * 33 Lamp + * **/ ContactPrinter contact_printer;