Projector firmware progress: currently the homing function will not compile due to errors with reference and so the sketch method needs to be broken into workable smaller methods

This commit is contained in:
Matt McWilliams 2023-09-25 21:54:31 -04:00
parent d2c7781d79
commit b1c8192625
10 changed files with 148 additions and 46 deletions

View File

@ -1,5 +1,5 @@
{ {
"version": "1.8.34", "version": "1.8.35",
"ext_port": 1111, "ext_port": 1111,
"profiles": { "profiles": {
"mcopy": { "mcopy": {

2
app/package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy-app", "name": "mcopy-app",
"version": "1.8.34", "version": "1.8.35",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {

View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy-app", "name": "mcopy-app",
"version": "1.8.34", "version": "1.8.35",
"description": "GUI for the mcopy small gauge film optical printer platform", "description": "GUI for the mcopy small gauge film optical printer platform",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {

View File

@ -1,5 +1,5 @@
{ {
"version": "1.8.34", "version": "1.8.35",
"ext_port": 1111, "ext_port": 1111,
"profiles": { "profiles": {
"mcopy": { "mcopy": {

View File

@ -2,7 +2,11 @@
#include "McopyProjector.h" #include "McopyProjector.h"
McopyProjector::McopyProjector (AccelStepper takeup, AccelStepper feed, uint8_t takeupSettingA, uint8_t takeupSettingB, uint8_t feedSettingA, uint8_t feedSettingB) { McopyProjector::McopyProjector (AccelStepper takeup, AccelStepper feed,
uint8_t takeupSettingA, uint8_t takeupSettingB,
uint8_t feedSettingA, uint8_t feedSettingB,
uint8_t takeupEmitter, uint8_t takeupReceiver,
uint8_t feedEmitter, uint8_t feedReceiver) {
_takeup = takeup; _takeup = takeup;
_feed = feed; _feed = feed;
@ -10,23 +14,32 @@ McopyProjector::McopyProjector (AccelStepper takeup, AccelStepper feed, uint8_t
_takeupSettingB = takeupSettingB; _takeupSettingB = takeupSettingB;
_feedSettingA = feedSettingA; _feedSettingA = feedSettingA;
_feedSettingB = feedSettingB; _feedSettingB = feedSettingB;
_takeupEmitter = takeupEmitter;
_takeupReceiver = takeupReceiver;
_feedEmitter = feedEmitter;
_feedReceiver = feedReceiver;
} }
void McopyProjector::begin () { void McopyProjector::begin () {
_takeup.setMaxSpeed(_speed); _takeup.setMaxSpeed(_speed);
_takeup.setSpeed(_speed); _takeup.setSpeed(_speed);
_takeup.setAcceleration(1000.0); _takeup.setAcceleration(1000.0);
_feed.setMaxSpeed(_speed); _feed.setMaxSpeed(_speed);
_feed.setSpeed(_speed); _feed.setSpeed(_speed);
_feed.setAcceleration(1000.0); _feed.setAcceleration(1000.0);
pinMode(_takeupSettingA, OUTPUT); pinMode(_takeupSettingA, OUTPUT);
pinMode(_takeupSettingB, OUTPUT); pinMode(_takeupSettingB, OUTPUT);
pinMode(_feedSettingA, OUTPUT); pinMode(_feedSettingA, OUTPUT);
pinMode(_feedSettingB, OUTPUT); pinMode(_feedSettingB, OUTPUT);
pinMode(_takeupEmitter, OUTPUT);
pinMode(_feedEmitter, OUTPUT);
pinMode(_takeupReceiver, INPUT);
pinMode(_feedReceiver, INPUT);
setStepperMode(1); //keep at 1 for now
setStepperMode(1);
} }
void McopyProjector::setDirection (bool dir) { void McopyProjector::setDirection (bool dir) {
@ -53,13 +66,12 @@ void McopyProjector::frame (bool dir) {
while (running) { while (running) {
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) { if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
//frame done
running = false; running = false;
_posTakeup = takeupGoal; _posTakeup = takeupGoal;
_posFeed += feedGoal; _posFeed += feedGoal;
} else { } else {
_takeup.run(); _takeup.run();
_feed.run(); _feed.run();
} }
} }
@ -95,9 +107,11 @@ void McopyProjector::loop () {
_running = false; _running = false;
_posTakeup += _dir ? _stepsPerFrame : -_stepsPerFrame; _posTakeup += _dir ? _stepsPerFrame : -_stepsPerFrame;
_posFeed += _dir ? _stepsPerFrame : -_stepsPerFrame; _posFeed += _dir ? _stepsPerFrame : -_stepsPerFrame;
Serial.println(_count);
} else { } else {
_takeup.run(); _takeup.run();
_feed.run(); _feed.run();
_count++;
} }
} else if (_adjusting) { } else if (_adjusting) {
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) { if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
@ -107,7 +121,8 @@ void McopyProjector::loop () {
_takeup.run(); _takeup.run();
_feed.run(); _feed.run();
} }
}*/ }
*/
} }
//https://wiki.iteadstudio.com/Arduino_Dual_Step_Motor_Driver_Shield //https://wiki.iteadstudio.com/Arduino_Dual_Step_Motor_Driver_Shield
@ -141,6 +156,58 @@ void McopyProjector::setStepperMode (uint8_t mode) {
} }
} }
void McopyProjector::home () {
uint16_t steps = _motorSteps * _mode;
long reading;
//
for (uint16_t i = 0; i < steps; i++) {
reading = analogReadAccurateAverage(_takeupReceiver);
_takeupSamples[i] = reading;
if (i < steps - 1) {
_takeup.move(1);
_takeup.runToPosition();
}
}
//
for (uint16_t i = 0; i < steps; i++) {
Serial.print(i);
Serial.print(", ");
Serial.println(_takeupSamples[i]);
}
uint16_t peak = findPeak(_takeupSamples, steps);
Serial.print("peak: ");
Serial.println(peak);
uint16_t offset = abs(200 - peak);
Serial.print("offset: ");
Serial.println(offset);
if (offset != 0) {
for (uint16_t i = 0; i < offset; i++) {
_takeup.move(-1);
_takeup.runToPosition();
}
for (uint16_t i = 0; i < 25; i++) {
_takeup.move(-1);
_takeup.runToPosition();
}
}
for (uint16_t i = 0; i < 50; i++) {
reading = analogReadAccurateAverage(_takeupReceiver);
_takeupSamples[i] = reading;
if (i < steps - 1) {
_takeup.move(1);
_takeup.runToPosition();
}
}
uint16_t peak2 = findPeak(_takeupSamples, 50);
uint16_t offset2 = abs(50 - peak2);
if (offset2 != 0) {
for (uint16_t i = 0; i < offset2; i++) {
_takeup.move(-1);
_takeup.runToPosition();
}
}
}
long McopyProjector::readVcc() { long McopyProjector::readVcc() {
long result; long result;
// Read 1.1V reference against AVcc // Read 1.1V reference against AVcc
@ -154,18 +221,30 @@ long McopyProjector::readVcc() {
return result; return result;
} }
long McopyProjector::analogReadAccurate (int pin) { long McopyProjector::analogReadAccurate (uint8_t &pin) {
double Vcc = readVcc() / 1000.0; double Vcc = readVcc() / 1000.0;
double ADCValue = analogRead(pin); double ADCValue = analogRead(pin);
return (ADCValue / 1024.0) * Vcc; return (ADCValue / 1024.0) * Vcc;
} }
long McopyProjector::analogReadAccurateAverage (int pin) { long McopyProjector::analogReadAccurateAverage (uint8_t &pin) {
int count = 3; uint8_t count = 3;
double sum = 0.0; double sum = 0.0;
for (int i = 0; i < count; i++) { for (uint8_t i = 0; i < count; i++) {
sum += analogReadAccurate(pin); sum += analogReadAccurate(pin);
delay(1); delay(1);
} }
return sum / (double) count; return sum / (double) count;
} }
uint16_t McopyProjector::findPeak(long (&arr)[200], uint16_t &steps) {
uint16_t maxI = 0;
long max = 0;
for (uint16_t i = 0; i < steps; i++) {
if (arr[i] > max) {
maxI = i;
max = arr[i];
}
}
return maxI;
}

View File

@ -28,20 +28,30 @@ class McopyProjector {
AccelStepper _takeup; AccelStepper _takeup;
AccelStepper _feed; AccelStepper _feed;
uint16_t _motorSteps = 1600; //microstepped const uint16_t _motorSteps = 200; //full steps
uint8_t _frames = 8; const uint8_t _frames = 8;
uint16_t _stepsPerFrame = 25; //round(_motorSteps / _frames); const uint16_t _stepsPerFrame = 25; //round(_motorSteps / _frames);
uint8_t _mode = 1; const float _speed = 2000.0;
float _speed = 2000.0;
volatile uint8_t _mode = 1;
int64_t _posTakeup = 0; int64_t _posTakeup = 0;
int64_t _posFeed = 0; int64_t _posFeed = 0;
//X
uint8_t _takeupSettingA;
uint8_t _takeupSettingB;
//Y
uint8_t _feedSettingA;
uint8_t _feedSettingB;
//X
uint8_t _takeupEmitter;
uint8_t _takeupReceiver;
//Y
uint8_t _feedEmitter;
uint8_t _feedReceiver;
long _feedSamples[200];
uint8_t _takeupSettingA = 4; long _takeupSamples[200];
uint8_t _takeupSettingB = 5;
uint8_t _feedSettingA = 8;
uint8_t _feedSettingB = 9;
bool _dir = true; bool _dir = true;
@ -49,12 +59,17 @@ class McopyProjector {
bool _adjusting = false; bool _adjusting = false;
long readVcc(); long readVcc();
long analogReadAccurate (int pin); long analogReadAccurate (uint8_t &pin);
long analogReadAccurateAverage (int pin); long analogReadAccurateAverage (uint8_t &pin);
uint16_t findPeak(long (&arr)[200], uint16_t &steps);
public: public:
McopyProjector(AccelStepper takeup, AccelStepper feed, uint8_t takeupSettingA, uint8_t takeupSettingB, uint8_t feedSettingA, uint8_t feedSettingB); McopyProjector(AccelStepper takeup, AccelStepper feed,
uint8_t takeupSettingA, uint8_t takeupSettingB,
uint8_t feedSettingA, uint8_t feedSettingB,
uint8_t takeupEmitter, uint8_t takeupReceiver,
uint8_t feedEmitter, uint8_t feedReceiver);
void begin(); void begin();
//0 = takeup, 1 = feed //0 = takeup, 1 = feed
void adjust(uint8_t motor, int64_t steps); void adjust(uint8_t motor, int64_t steps);
@ -65,6 +80,8 @@ class McopyProjector {
void setStepperMode(uint8_t mode); void setStepperMode(uint8_t mode);
void loop(); void loop();
void home(); void home();
void firstPass();
void centerPass();
}; };

View File

@ -46,12 +46,11 @@ AccelStepper takeup(AccelStepper::DRIVER, TAKEUP_STEP_PIN, TAKEUP_DIR_PIN);
AccelStepper feed(AccelStepper::DRIVER, FEED_STEP_PIN, FEED_DIR_PIN); AccelStepper feed(AccelStepper::DRIVER, FEED_STEP_PIN, FEED_DIR_PIN);
//CAMERA CONSTANTS //CAMERA CONSTANTS
const int BUTTON = 7; const int BUTTON = 19;
const int LED_FWD = 8; const int LED_FWD = 20;
const int LED_BWD = 9; const int LED_BWD = 21;
const int PROJECTOR_MOMENT = 240; const int PROJECTOR_MOMENT = 240;
const int PROJECTOR_STEPS = 25;
//VARIABLES //VARIABLES
volatile int projectorFrame = -1; volatile int projectorFrame = -1;
@ -61,7 +60,11 @@ volatile bool direction = true;
volatile long start; volatile long start;
McopySerial mcopy; McopySerial mcopy;
McopyProjector projector(takeup, feed, TAKEUP_SETTINGS_A, TAKEUP_SETTINGS_B, FEED_SETTINGS_A, FEED_SETTINGS_B); McopyProjector projector(takeup, feed,
TAKEUP_SETTINGS_A, TAKEUP_SETTINGS_B,
FEED_SETTINGS_A, FEED_SETTINGS_B,
TAKEUP_EMITTER, TAKEUP_RECEIVER,
FEED_EMITTER, FEED_RECEIVER);
void setup () { void setup () {
pins(); pins();
@ -72,6 +75,7 @@ void setup () {
delay(42); delay(42);
digitalWrite(LED_FWD, LOW); digitalWrite(LED_FWD, LOW);
digitalWrite(LED_BWD, LOW); digitalWrite(LED_BWD, LOW);
projector.home();
} }
void loop () { void loop () {
@ -81,7 +85,7 @@ void loop () {
if (digitalRead(BUTTON) == LOW) { if (digitalRead(BUTTON) == LOW) {
projector_frame(); projector_frame();
} }
//projector.loop(); projector.loop();
} }
void pins () { void pins () {
@ -134,6 +138,7 @@ void projector_timing_end () {
void projector_frame () { void projector_frame () {
int LED = direction ? LED_FWD : LED_BWD; int LED = direction ? LED_FWD : LED_BWD;
mcopy.log("projector_direction(false)");
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
projector.frame(direction); projector.frame(direction);
mcopy.confirm(mcopy.PROJECTOR); mcopy.confirm(mcopy.PROJECTOR);
@ -146,7 +151,7 @@ void state () {
stateString += String(mcopy.STATE); stateString += String(mcopy.STATE);
mcopy.print(stateString); mcopy.print(stateString);
} }
/*
long readVcc() { long readVcc() {
long result; long result;
// Read 1.1V reference against AVcc // Read 1.1V reference against AVcc
@ -175,3 +180,4 @@ long analogReadAccurateAverage (int pin) {
} }
return sum / (double) count; return sum / (double) count;
} }
*/

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "mcopy", "name": "mcopy",
"version": "1.8.34", "version": "1.8.35",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "mcopy", "name": "mcopy",
"version": "1.8.34", "version": "1.8.35",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"arduino": "file:app/lib/arduino", "arduino": "file:app/lib/arduino",

View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy", "name": "mcopy",
"version": "1.8.34", "version": "1.8.35",
"description": "Small gauge film optical printer platform", "description": "Small gauge film optical printer platform",
"main": "build.js", "main": "build.js",
"directories": { "directories": {

View File

@ -1,5 +1,5 @@
{ {
"version": "1.8.34", "version": "1.8.35",
"ext_port": 1111, "ext_port": 1111,
"profiles": { "profiles": {
"mcopy": { "mcopy": {