Create the Lamp class which will abstract to different lamp types before one is settled on.

This commit is contained in:
Matt McWilliams 2024-02-17 14:57:26 +00:00
parent ce910d66b4
commit 2401fcf8e7
5 changed files with 64 additions and 8 deletions

View File

@ -10,6 +10,7 @@ void ContactPrinter::Setup () {
pinMode(takeup_picture_pin_ccw, OUTPUT); pinMode(takeup_picture_pin_ccw, OUTPUT);
pinMode(takeup_stock_pin_cw, OUTPUT); pinMode(takeup_stock_pin_cw, OUTPUT);
pinMode(takeup_stock_pin_ccw, OUTPUT); pinMode(takeup_stock_pin_ccw, OUTPUT);
pinMode(start_button_pin, INPUT_PULLUP);
drive_motor.Setup(); drive_motor.Setup();
@ -26,12 +27,16 @@ void ContactPrinter::Setup () {
digitalWrite(takeup_picture_pin_ccw, LOW); digitalWrite(takeup_picture_pin_ccw, LOW);
digitalWrite(takeup_stock_pin_cw, LOW); digitalWrite(takeup_stock_pin_cw, LOW);
digitalWrite(takeup_stock_pin_ccw, LOW); digitalWrite(takeup_stock_pin_ccw, LOW);
SetSpeedTakeup(0.4);
//SetSpeedDrive(1.0);
} }
void ContactPrinter::Start () { void ContactPrinter::Start () {
RampTakeup(0, takeup_pwm_duty_cycle, takeup_ramp_time); RampTakeup(0, takeup_pwm_duty_cycle, takeup_ramp_time);
delay(100); delay(100);
drive_motor.Start(); drive_motor.Start();
running = true;
} }
void ContactPrinter::Stop () { void ContactPrinter::Stop () {
@ -62,11 +67,13 @@ void ContactPrinter::SetDirectionPicture(bool clockwise) {
} }
//linear //linear
void ContactPrinter::RampTakeup(uint16_t start, uint16_t end, uint16_t time) { void ContactPrinter::RampTakeup(uint16_t start_pwm, uint16_t end_pwm, uint16_t time) {
takeup_ramp_steps = abs(start - end); takeup_ramp_steps = abs(start_pwm - end_pwm);
takeup_ramp_step = round(time / takeup_ramp_steps); takeup_ramp_step = round(time / takeup_ramp_steps);
takeup_pwm_duty_cycle = start; takeup_pwm_duty_cycle = start_pwm;
takeup_ramp_dir = end < start; takeup_ramp_dir = end_pwm < start_pwm;
takeup_ramp_current_step = 0;
takeup_ramping = true;
if (takeup_picture_cw) { if (takeup_picture_cw) {
digitalWrite(takeup_picture_pin_cw, HIGH); 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_cw, LOW);
digitalWrite(takeup_stock_pin_ccw, HIGH); digitalWrite(takeup_stock_pin_ccw, HIGH);
} }
takeup_ramping = true;
for (uint16_t i = 0; i < takeup_ramp_steps; i++) { for (uint16_t i = 0; i < takeup_ramp_steps; i++) {
if (takeup_pwm_duty_cycle <= 0 || takeup_pwm_duty_cycle >= 255) { 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; takeup_ramping = false;
} }
void ContactPrinter::RampTakeupLoop () {
}
void ContactPrinter::ButtonLoop () {
if (!running && digitalRead(start_button_pin) == LOW) {
Start();
}
}
bool ContactPrinter::IsRunning () { bool ContactPrinter::IsRunning () {
return running; return running;
} }
@ -108,4 +124,7 @@ bool ContactPrinter::IsRunning () {
void ContactPrinter::Loop () { void ContactPrinter::Loop () {
timer = millis(); timer = millis();
drive_motor.Loop(); drive_motor.Loop();
if (takeup_ramping) {
RampTakeupLoop();
}
} }

View File

@ -3,6 +3,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "DriveMotor.h" #include "DriveMotor.h"
#include "Lamp.h"
class ContactPrinter { class ContactPrinter {
@ -10,6 +11,7 @@ class ContactPrinter {
//use default drive motor pins //use default drive motor pins
DriveMotor drive_motor; DriveMotor drive_motor;
Lamp lamp;
const uint16_t serial_delay = 5; const uint16_t serial_delay = 5;
const uint16_t baud = 115200; 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_cw = 18;
const uint8_t takeup_stock_pin_ccw = 5; const uint8_t takeup_stock_pin_ccw = 5;
const uint8_t start_button_pin = 34;
const uint32_t pwm_frequency = 30000; const uint32_t pwm_frequency = 30000;
const uint8_t takeup_picture_pwm_channel = 1; const uint8_t takeup_picture_pwm_channel = 1;
const uint8_t takeup_stock_pwm_channel = 2; 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_steps = 0; //# of steps
volatile uint16_t takeup_ramp_step = 0; //length of step (ms) volatile uint16_t takeup_ramp_step = 0; //length of step (ms)
volatile boolean takeup_ramp_dir = true; //true = up, false = down 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 boolean takeup_ramping = false;
volatile bool takeup_picture_cw = false; volatile bool takeup_picture_cw = false;
@ -44,8 +54,6 @@ class ContactPrinter {
volatile bool takeup_stock_cw = true; volatile bool takeup_stock_cw = true;
volatile bool takeup_stock_ccw = true; volatile bool takeup_stock_ccw = true;
volatile uint16_t takeup_ramp_time = 500;
volatile bool running = false; volatile bool running = false;
public: public:
@ -62,6 +70,9 @@ class ContactPrinter {
void SetDirectionPicture(bool clockwise); void SetDirectionPicture(bool clockwise);
void RampTakeup(uint16_t start, uint16_t end, uint16_t time); void RampTakeup(uint16_t start, uint16_t end, uint16_t time);
void RampTakeupLoop();
void ButtonLoop();
bool IsRunning (); bool IsRunning ();
}; };

View File

@ -0,0 +1,5 @@
#include "Lamp.h"
Lamp::Lamp () {
//
}

View File

@ -0,0 +1,17 @@
#ifndef LAMP
#define LAMP
#include <Arduino.h>
class Lamp {
private:
volatile boolean on = false;
volatile uint8_t lamp_pin_a = 33;
public:
Lamp();
void Setup();
void Loop();
};
#endif

View File

@ -23,6 +23,10 @@
* 27 Drive Encoder A * 27 Drive Encoder A
* 26 Drive Encoder B * 26 Drive Encoder B
* *
* 34 Start Button
*
* 33 Lamp
*
**/ **/
ContactPrinter contact_printer; ContactPrinter contact_printer;