Rewrite arri_s_firmware to use McopySerial

This commit is contained in:
Matt McWilliams 2023-04-16 16:42:00 -04:00
parent ef54e61494
commit ca22a3ba4b
1 changed files with 34 additions and 63 deletions

View File

@ -1,6 +1,6 @@
#include <Adafruit_MotorShield.h>
#include "McopySerial.h"
volatile boolean debug_state = true;
volatile boolean cam_dir = true;
volatile boolean running = true;
@ -9,90 +9,56 @@ const int fullRotation = 3 * stepsPerRevolution;
const int openRotationForward = 300;
const int openRotationBackward = 300;
//CAMERA COMMANDS
const char cmd_camera = 'c';
const char cmd_cam_forward = 'e';
const char cmd_cam_backward = 'f';
const char cmd_debug = 'd';
const char cmd_connect = 'i';
volatile char cmd_char = 'z';
const char cmd_mcopy_identifier = 'm';
const char cmd_cam_identifier = 'k';
const int serialDelay = 5;
volatile char cmdChar = 'z';
volatile long now;
volatile long cameraFrame = -1;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
//Set up for a 200step motor (NEMA 17)
Adafruit_StepperMotor *stepper = AFMS.getStepper(stepsPerRevolution, 2);
McopySerial mc;
void setupMotor () {
//TWBR = ((F_CPU /400000l) - 16) / 2; // Change the i2c clock to 400KHz
if (!AFMS.begin()) { // default frequency 1.6KHz
log("Could not find Motor Shield. Check wiring.");
mc.log("Could not find Motor Shield. Check wiring.");
while (1);
}
log("Motor Shield found.");
mc.log("Motor Shield found.");
stepper->setSpeed(600);
}
void setup() {
Serial.begin(57600);
mc.begin(mc.CAMERA_IDENTIFIER);
setupMotor();
}
void loop() {
if (Serial.available()) {
// read the most recent byte
cmd_char = (char)Serial.read();
}
if (cmd_char != 'z') {
cmd(cmd_char);
cmd_char = 'z';
}
now = millis();
cmdChar = mc.loop();
cmd(cmdChar);
}
void cmd (char val) {
if (val == cmd_debug) {
debug();
} else if (val == cmd_connect) {
connect();
} else if (val == cmd_mcopy_identifier) {
identify();
} else if (val == cmd_cam_forward) {
setDir(true); //explicit
} else if (val == cmd_cam_backward) {
if (val == mc.CAMERA_FORWARD) {
setDir(true);
} else if (val == mc.CAMERA_BACKWARD) {
setDir(false);
} else if (val == cmd_camera) {
} else if (val == mc.CAMERA) {
cam();
}
}
void debug () {
debug_state = true;
Serial.println(cmd_debug);
log("debugging enabled");
}
void connect () {
Serial.println(cmd_connect);
log("connect()");
}
void identify () {
Serial.println(cmd_cam_identifier);
log("identify()");
}
void setDir (boolean dir) {
cam_dir = dir;
if (cam_dir) {
Serial.println(cmd_cam_forward);
log("setDir = true");
mc.confirm(mc.CAMERA_FORWARD);
mc.log("setDir = true");
} else {
Serial.println(cmd_cam_backward);
log("setDir -> false");
mc.confirm(mc.CAMERA_BACKWARD);
mc.log("setDir -> false");
}
}
@ -100,18 +66,23 @@ void cam () {
long startTime = millis();
if (cam_dir) {
stepper->step(fullRotation, FORWARD, DOUBLE);
Serial.println(cmd_cam_forward);
log("cam -> forward");
mc.log("cam() -> forward");
} else {
stepper->step(fullRotation, BACKWARD, DOUBLE);
Serial.println(cmd_cam_backward);
log("cam -> backward");
mc.log("cam() -> backward");
}
log(String(millis() - startTime));
mc.confirm(mc.CAMERA);
if (cameraFrame == -1) {
cameraFrame = millis() - startTime;
} else {
cameraFrame = round((cameraFrame + (millis() - startTime)) / 2);
}
mc.log(String(cameraFrame));
}
void log (String msg) {
if (debug_state) {
Serial.println(msg);
}
void state () {
String stateString = String(mc.CAMERA_EXPOSURE);
stateString += String(cameraFrame);
stateString += String(mc.STATE);
mc.print(stateString);
}