diff --git a/app/data/cfg.json b/app/data/cfg.json index 45e7735..fbcc6f8 100644 --- a/app/data/cfg.json +++ b/app/data/cfg.json @@ -1,5 +1,5 @@ { - "version": "1.8.34", + "version": "1.8.35", "ext_port": 1111, "profiles": { "mcopy": { diff --git a/app/package-lock.json b/app/package-lock.json index 526fc61..7844b5d 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -1,6 +1,6 @@ { "name": "mcopy-app", - "version": "1.8.34", + "version": "1.8.35", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/app/package.json b/app/package.json index 3a95cdd..20fe108 100644 --- a/app/package.json +++ b/app/package.json @@ -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": { diff --git a/data/cfg.json b/data/cfg.json index 45e7735..fbcc6f8 100644 --- a/data/cfg.json +++ b/data/cfg.json @@ -1,5 +1,5 @@ { - "version": "1.8.34", + "version": "1.8.35", "ext_port": 1111, "profiles": { "mcopy": { diff --git a/ino/mcopy_projector_firmware/McopyProjector.cpp b/ino/mcopy_projector_firmware/McopyProjector.cpp index 1a47c70..e22c027 100644 --- a/ino/mcopy_projector_firmware/McopyProjector.cpp +++ b/ino/mcopy_projector_firmware/McopyProjector.cpp @@ -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,23 +14,32 @@ 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 () { - _takeup.setMaxSpeed(_speed); - _takeup.setSpeed(_speed); - _takeup.setAcceleration(1000.0); - - _feed.setMaxSpeed(_speed); - _feed.setSpeed(_speed); - _feed.setAcceleration(1000.0); + _takeup.setMaxSpeed(_speed); + _takeup.setSpeed(_speed); + _takeup.setAcceleration(1000.0); + + _feed.setMaxSpeed(_speed); + _feed.setSpeed(_speed); + _feed.setAcceleration(1000.0); - pinMode(_takeupSettingA, OUTPUT); + pinMode(_takeupSettingA, OUTPUT); pinMode(_takeupSettingB, OUTPUT); pinMode(_feedSettingA, 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) { @@ -53,13 +66,12 @@ void McopyProjector::frame (bool dir) { while (running) { if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) { - //frame done running = false; _posTakeup = takeupGoal; _posFeed += feedGoal; } else { _takeup.run(); - _feed.run(); + _feed.run(); } } @@ -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; -} \ No newline at end of file +} + +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; +} diff --git a/ino/mcopy_projector_firmware/McopyProjector.h b/ino/mcopy_projector_firmware/McopyProjector.h index 3184820..846e7d7 100644 --- a/ino/mcopy_projector_firmware/McopyProjector.h +++ b/ino/mcopy_projector_firmware/McopyProjector.h @@ -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(); }; diff --git a/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino b/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino index ae6de7e..9802c4e 100644 --- a/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino +++ b/ino/mcopy_projector_firmware/mcopy_projector_firmware.ino @@ -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; } +*/ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1bac742..c5a1cb8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 9fc3608..2061f42 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/processing/mcopy/cfg.json b/processing/mcopy/cfg.json index 45e7735..fbcc6f8 100644 --- a/processing/mcopy/cfg.json +++ b/processing/mcopy/cfg.json @@ -1,5 +1,5 @@ { - "version": "1.8.34", + "version": "1.8.35", "ext_port": 1111, "profiles": { "mcopy": {