McopyProjector lib for mcopy_projector_firmware rewrite in progress.

This commit is contained in:
Matt McWilliams 2023-08-23 15:21:50 -04:00
parent 86c88c4ac2
commit 141d27599f
4 changed files with 110 additions and 16 deletions

View File

@ -1,7 +1,6 @@
#ifndef IteadDualStepperShield_h #ifndef IteadDualStepperShield_h
#define IteadDualStepperShield_h #define IteadDualStepperShield_h
#include <Arduino.h> #include <Arduino.h>
class IteadDualStepperShield { class IteadDualStepperShield {

View File

@ -0,0 +1,61 @@
/// Mcopy Projector Class
#include "McopyProjector.h"
#include "IteadDualStepperShield.h"
McopyProjector::McopyProjector () {
}
void McopyProjector::begin () {
steppers.setup();
steppers.setSpeed(0, _speed);
steppers.setSpeed(1, _speed);
}
void McopyProjector::setDirection (bool dir) {
_dir = dir;
if (_dir) {
steppers.setDir(0, 1);
steppers.setDir(1, 1);
} else {
steppers.setDir(0, 0);
steppers.setDir(1, 0);
}
}
void McopyProjector::frame (bool dir) {
if (dir != _dir) {
setDirection(dir);
}
steppers.stepBoth(_stepsPerFrame);
_posTakeup += dir ? _stepsPerFrame : -_stepsPerFrame;
_posFeed += dir ? _stepsPerFrame : -_stepsPerFrame;
}
void McopyProjector::adjust(uint8_t motor, int32_t steps) {
if (steps < 0) {
steppers.setDir(motor, 0);
} else {
steppers.setDir(motor, 1);
}
steppers.step(motor, abs(steps));
if (motor == 0) {
_posTakeup += steps;
} else if (motor == 1) {
_posFeed += steps;
}
}
void McopyProjector::adjustBoth(int32_t steps) {
if (steps < 0) {
steppers.setDir(0, 0);
steppers.setDir(1, 0);
} else {
steppers.setDir(0, 1);
steppers.setDir(1, 1);
}
steppers.stepBoth(abs(steps));
_posTakeup += steps;
_posFeed += steps;
}

View File

@ -0,0 +1,36 @@
#ifndef MCOPY_PROJECTOR
#define MCOPY_PROJECTOR
#include <Arduino.h>
#include "IteadDualStepperShield.h"
class McopyProjector {
private:
IteadDualStepperShield steppers;
uint8_t _motorSteps = 200;
uint8_t _frames = 8;
uint8_t _stepsPerFrame = round(_motorSteps / _frames);
uint16_t _speed = 500;
int64_t _posTakeup = 0;
int64_t _posFeed = 0;
bool _dir = true;
public:
McopyProjector();
void begin();
//0 = takeup, 1 = feed
void adjust(uint8_t motor, int32_t steps);
void adjustBoth(int32_t steps);
//true = forward, false = back
void frame(bool dir);
void frames(bool dir, uint32_t count);
void setDirection(bool dir);
};
#endif

View File

@ -21,10 +21,11 @@
Y02A - Blue Y02A - Blue
Y02B - Red Y02B - Red
*/ */
#include "McopyProjector.h"
#include "McopySerial.h" #include "McopySerial.h"
#include "IteadDualStepperShield.h" #include "IteadDualStepperShield.h"
//CAMERA CONSTANTS //CAMERA CONSTANTS
const int BUTTON = 7; const int BUTTON = 7;
const int LED_FWD = 8; const int LED_FWD = 8;
@ -41,17 +42,16 @@ volatile bool direction = true;
volatile long start; volatile long start;
McopySerial mcopy; McopySerial mcopy;
IteadDualStepperShield steppers; McopyProjector projector;
void setup () { void setup () {
steppers.setup();
steppers.setSpeed(0, 500);
steppers.setSpeed(1, 500);
pins(); pins();
digitalWrite(LED_FWD, HIGH); digitalWrite(LED_FWD, HIGH);
digitalWrite(LED_BWD, HIGH); digitalWrite(LED_BWD, HIGH);
mcopy.begin(mcopy.PROJECTOR_IDENTIFIER); mcopy.begin(mcopy.PROJECTOR_IDENTIFIER);
projector.begin();
delay(42); delay(42);
digitalWrite(LED_FWD, LOW); digitalWrite(LED_FWD, LOW);
digitalWrite(LED_BWD, LOW); digitalWrite(LED_BWD, LOW);
@ -62,7 +62,7 @@ void loop () {
cmdChar = mcopy.loop(); cmdChar = mcopy.loop();
cmd(cmdChar); cmd(cmdChar);
if (digitalRead(BUTTON) == LOW) { if (digitalRead(BUTTON) == LOW) {
projector(); projector_frame();
} }
} }
@ -76,12 +76,12 @@ void pins () {
} }
void cmd (char val) { void cmd (char val) {
if (val == mcopy.CAMERA_FORWARD) { if (val == mcopy.PROJECTOR_FORWARD) {
projector_direction(true); projector_direction(true);
} else if (val == mcopy.CAMERA_BACKWARD) { } else if (val == mcopy.PROJECTOR_BACKWARD) {
projector_direction(false); projector_direction(false);
} else if (val == mcopy.PROJECTOR) { } else if (val == mcopy.PROJECTOR) {
projector(); projector_frame();
} else if (val == mcopy.STATE) { } else if (val == mcopy.STATE) {
state(); state();
} }
@ -92,13 +92,11 @@ void projector_direction (boolean state) {
if (state) { if (state) {
mcopy.confirm(mcopy.PROJECTOR_FORWARD); mcopy.confirm(mcopy.PROJECTOR_FORWARD);
mcopy.log("projector_direction(true)"); mcopy.log("projector_direction(true)");
steppers.setDir(0, 1); projector.setDirection(true);
steppers.setDir(1, 1);
} else { } else {
mcopy.confirm(mcopy.PROJECTOR_BACKWARD); mcopy.confirm(mcopy.PROJECTOR_BACKWARD);
mcopy.log("projector_direction(false)"); mcopy.log("projector_direction(false)");
steppers.setDir(0, 0); projector.setDirection(false);
steppers.setDir(1, 0);
} }
} }
@ -116,10 +114,10 @@ void projector_timing_end () {
} }
} }
void projector () { void projector_frame () {
int LED = direction ? LED_FWD : LED_BWD; int LED = direction ? LED_FWD : LED_BWD;
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
steppers.stepBoth(PROJECTOR_STEPS); projector.frame(direction);
mcopy.confirm(mcopy.PROJECTOR); mcopy.confirm(mcopy.PROJECTOR);
digitalWrite(LED, LOW); digitalWrite(LED, LOW);
} }