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,
"profiles": {
"mcopy": {

2
app/package-lock.json generated
View File

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

View File

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

View File

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

View File

@ -2,7 +2,11 @@
#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;
_feed = feed;
@ -10,6 +14,10 @@ McopyProjector::McopyProjector (AccelStepper takeup, AccelStepper feed, uint8_t
_takeupSettingB = takeupSettingB;
_feedSettingA = feedSettingA;
_feedSettingB = feedSettingB;
_takeupEmitter = takeupEmitter;
_takeupReceiver = takeupReceiver;
_feedEmitter = feedEmitter;
_feedReceiver = feedReceiver;
}
void McopyProjector::begin () {
@ -25,7 +33,12 @@ void McopyProjector::begin () {
pinMode(_takeupSettingB, OUTPUT);
pinMode(_feedSettingA, OUTPUT);
pinMode(_feedSettingB, OUTPUT);
pinMode(_takeupEmitter, OUTPUT);
pinMode(_feedEmitter, OUTPUT);
pinMode(_takeupReceiver, INPUT);
pinMode(_feedReceiver, INPUT);
//keep at 1 for now
setStepperMode(1);
}
@ -53,7 +66,6 @@ void McopyProjector::frame (bool dir) {
while (running) {
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
//frame done
running = false;
_posTakeup = takeupGoal;
_posFeed += feedGoal;
@ -95,9 +107,11 @@ void McopyProjector::loop () {
_running = false;
_posTakeup += _dir ? _stepsPerFrame : -_stepsPerFrame;
_posFeed += _dir ? _stepsPerFrame : -_stepsPerFrame;
Serial.println(_count);
} else {
_takeup.run();
_feed.run();
_count++;
}
} else if (_adjusting) {
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
@ -107,7 +121,8 @@ void McopyProjector::loop () {
_takeup.run();
_feed.run();
}
}*/
}
*/
}
//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 result;
// Read 1.1V reference against AVcc
@ -154,18 +221,30 @@ long McopyProjector::readVcc() {
return result;
}
long McopyProjector::analogReadAccurate (int pin) {
long McopyProjector::analogReadAccurate (uint8_t &pin) {
double Vcc = readVcc() / 1000.0;
double ADCValue = analogRead(pin);
return (ADCValue / 1024.0) * Vcc;
}
long McopyProjector::analogReadAccurateAverage (int pin) {
int count = 3;
long McopyProjector::analogReadAccurateAverage (uint8_t &pin) {
uint8_t count = 3;
double sum = 0.0;
for (int i = 0; i < count; i++) {
for (uint8_t i = 0; i < count; i++) {
sum += analogReadAccurate(pin);
delay(1);
}
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 _feed;
uint16_t _motorSteps = 1600; //microstepped
uint8_t _frames = 8;
uint16_t _stepsPerFrame = 25; //round(_motorSteps / _frames);
uint8_t _mode = 1;
float _speed = 2000.0;
const uint16_t _motorSteps = 200; //full steps
const uint8_t _frames = 8;
const uint16_t _stepsPerFrame = 25; //round(_motorSteps / _frames);
const float _speed = 2000.0;
volatile uint8_t _mode = 1;
int64_t _posTakeup = 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;
uint8_t _takeupSettingA = 4;
uint8_t _takeupSettingB = 5;
uint8_t _feedSettingA = 8;
uint8_t _feedSettingB = 9;
long _feedSamples[200];
long _takeupSamples[200];
bool _dir = true;
@ -49,12 +59,17 @@ class McopyProjector {
bool _adjusting = false;
long readVcc();
long analogReadAccurate (int pin);
long analogReadAccurateAverage (int pin);
long analogReadAccurate (uint8_t &pin);
long analogReadAccurateAverage (uint8_t &pin);
uint16_t findPeak(long (&arr)[200], uint16_t &steps);
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();
//0 = takeup, 1 = feed
void adjust(uint8_t motor, int64_t steps);
@ -65,6 +80,8 @@ class McopyProjector {
void setStepperMode(uint8_t mode);
void loop();
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);
//CAMERA CONSTANTS
const int BUTTON = 7;
const int LED_FWD = 8;
const int LED_BWD = 9;
const int BUTTON = 19;
const int LED_FWD = 20;
const int LED_BWD = 21;
const int PROJECTOR_MOMENT = 240;
const int PROJECTOR_STEPS = 25;
//VARIABLES
volatile int projectorFrame = -1;
@ -61,7 +60,11 @@ volatile bool direction = true;
volatile long start;
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 () {
pins();
@ -72,6 +75,7 @@ void setup () {
delay(42);
digitalWrite(LED_FWD, LOW);
digitalWrite(LED_BWD, LOW);
projector.home();
}
void loop () {
@ -81,7 +85,7 @@ void loop () {
if (digitalRead(BUTTON) == LOW) {
projector_frame();
}
//projector.loop();
projector.loop();
}
void pins () {
@ -134,6 +138,7 @@ void projector_timing_end () {
void projector_frame () {
int LED = direction ? LED_FWD : LED_BWD;
mcopy.log("projector_direction(false)");
digitalWrite(LED, HIGH);
projector.frame(direction);
mcopy.confirm(mcopy.PROJECTOR);
@ -146,7 +151,7 @@ void state () {
stateString += String(mcopy.STATE);
mcopy.print(stateString);
}
/*
long readVcc() {
long result;
// Read 1.1V reference against AVcc
@ -175,3 +180,4 @@ long analogReadAccurateAverage (int pin) {
}
return sum / (double) count;
}
*/

4
package-lock.json generated
View File

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

View File

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

View File

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