2023-08-23 19:21:50 +00:00
|
|
|
/// Mcopy Projector Class
|
|
|
|
|
|
|
|
#include "McopyProjector.h"
|
|
|
|
|
2023-09-26 01:54:31 +00:00
|
|
|
McopyProjector::McopyProjector (AccelStepper takeup, AccelStepper feed,
|
|
|
|
uint8_t takeupSettingA, uint8_t takeupSettingB,
|
|
|
|
uint8_t feedSettingA, uint8_t feedSettingB,
|
|
|
|
uint8_t takeupEmitter, uint8_t takeupReceiver,
|
2023-10-09 13:59:19 +00:00
|
|
|
uint8_t feedEmitter, uint8_t feedReceiver,
|
|
|
|
uint8_t servoPin) {
|
2023-08-29 19:33:51 +00:00
|
|
|
_takeup = takeup;
|
|
|
|
_feed = feed;
|
2023-08-29 23:48:49 +00:00
|
|
|
|
|
|
|
_takeupSettingA = takeupSettingA;
|
|
|
|
_takeupSettingB = takeupSettingB;
|
|
|
|
_feedSettingA = feedSettingA;
|
|
|
|
_feedSettingB = feedSettingB;
|
2023-09-26 01:54:31 +00:00
|
|
|
_takeupEmitter = takeupEmitter;
|
|
|
|
_takeupReceiver = takeupReceiver;
|
|
|
|
_feedEmitter = feedEmitter;
|
|
|
|
_feedReceiver = feedReceiver;
|
2023-10-09 13:59:19 +00:00
|
|
|
_servoPin = servoPin;
|
2023-08-23 19:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void McopyProjector::begin () {
|
2023-09-26 01:54:31 +00:00
|
|
|
_takeup.setMaxSpeed(_speed);
|
|
|
|
_takeup.setSpeed(_speed);
|
|
|
|
_takeup.setAcceleration(1000.0);
|
|
|
|
|
|
|
|
_feed.setMaxSpeed(_speed);
|
|
|
|
_feed.setSpeed(_speed);
|
|
|
|
_feed.setAcceleration(1000.0);
|
|
|
|
|
|
|
|
pinMode(_takeupSettingA, OUTPUT);
|
2023-08-30 05:18:10 +00:00
|
|
|
pinMode(_takeupSettingB, OUTPUT);
|
|
|
|
pinMode(_feedSettingA, OUTPUT);
|
|
|
|
pinMode(_feedSettingB, OUTPUT);
|
2023-09-26 01:54:31 +00:00
|
|
|
pinMode(_takeupEmitter, OUTPUT);
|
|
|
|
pinMode(_feedEmitter, OUTPUT);
|
|
|
|
pinMode(_takeupReceiver, INPUT);
|
|
|
|
pinMode(_feedReceiver, INPUT);
|
2023-08-30 05:18:10 +00:00
|
|
|
|
2023-09-26 01:54:31 +00:00
|
|
|
//keep at 1 for now
|
|
|
|
setStepperMode(1);
|
2023-10-09 13:59:19 +00:00
|
|
|
|
|
|
|
_servo.attach(_servoPin);
|
|
|
|
_servo.write(_servoHome);
|
2023-08-23 19:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void McopyProjector::setDirection (bool dir) {
|
|
|
|
_dir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void McopyProjector::frame (bool dir) {
|
2023-09-29 00:46:45 +00:00
|
|
|
int16_t spf = _stepsPerFrame * _mode; //scaled
|
2023-09-22 01:06:31 +00:00
|
|
|
bool running = true;
|
2023-08-23 19:21:50 +00:00
|
|
|
if (dir != _dir) {
|
|
|
|
setDirection(dir);
|
|
|
|
}
|
2023-08-29 19:33:51 +00:00
|
|
|
|
|
|
|
int64_t takeupGoal = _takeup.currentPosition();
|
|
|
|
int64_t feedGoal = _feed.currentPosition();
|
|
|
|
|
2023-09-22 01:06:31 +00:00
|
|
|
takeupGoal += _dir ? spf : -spf;
|
|
|
|
feedGoal += _dir ? spf : -spf;
|
2023-08-29 19:33:51 +00:00
|
|
|
|
|
|
|
_takeup.moveTo(takeupGoal);
|
|
|
|
_feed.moveTo(feedGoal);
|
|
|
|
|
|
|
|
_running = true;
|
|
|
|
|
2023-10-09 13:59:19 +00:00
|
|
|
_servo.write(_servoAway);
|
|
|
|
delay(20);
|
|
|
|
|
2023-09-22 01:06:31 +00:00
|
|
|
while (running) {
|
|
|
|
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
|
|
|
|
running = false;
|
|
|
|
_posTakeup = takeupGoal;
|
|
|
|
_posFeed += feedGoal;
|
|
|
|
} else {
|
|
|
|
_takeup.run();
|
2023-09-26 01:54:31 +00:00
|
|
|
_feed.run();
|
2023-09-22 01:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-09 13:59:19 +00:00
|
|
|
|
|
|
|
delay(20);
|
|
|
|
_servo.write(_servoHome);
|
2023-09-22 01:06:31 +00:00
|
|
|
|
|
|
|
_running = false;
|
|
|
|
|
2023-08-23 19:21:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 20:44:28 +00:00
|
|
|
void McopyProjector::adjust(uint8_t motor, int64_t steps) {
|
|
|
|
uint64_t s = abs(steps);
|
2023-08-29 19:33:51 +00:00
|
|
|
|
|
|
|
//moveTo
|
2023-08-23 19:21:50 +00:00
|
|
|
if (motor == 0) {
|
|
|
|
_posTakeup += steps;
|
|
|
|
} else if (motor == 1) {
|
|
|
|
_posFeed += steps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:33:51 +00:00
|
|
|
void McopyProjector::adjustBoth (int64_t steps) {
|
2023-08-25 20:44:28 +00:00
|
|
|
uint64_t s = abs(steps);
|
2023-08-29 19:33:51 +00:00
|
|
|
|
|
|
|
//steppers.stepBoth(s);
|
2023-08-23 19:21:50 +00:00
|
|
|
_posTakeup += steps;
|
|
|
|
_posFeed += steps;
|
2023-08-25 20:44:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:33:51 +00:00
|
|
|
void McopyProjector::loop () {
|
2023-09-22 01:06:31 +00:00
|
|
|
/*
|
2023-08-29 19:33:51 +00:00
|
|
|
if (_running) {
|
|
|
|
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
|
|
|
|
//frame done
|
|
|
|
_running = false;
|
|
|
|
_posTakeup += _dir ? _stepsPerFrame : -_stepsPerFrame;
|
|
|
|
_posFeed += _dir ? _stepsPerFrame : -_stepsPerFrame;
|
2023-09-26 01:54:31 +00:00
|
|
|
Serial.println(_count);
|
2023-08-29 19:33:51 +00:00
|
|
|
} else {
|
|
|
|
_takeup.run();
|
|
|
|
_feed.run();
|
2023-09-26 01:54:31 +00:00
|
|
|
_count++;
|
2023-08-29 19:33:51 +00:00
|
|
|
}
|
|
|
|
} else if (_adjusting) {
|
|
|
|
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
|
|
|
|
//adjustment done
|
|
|
|
_adjusting = false;
|
|
|
|
} else {
|
|
|
|
_takeup.run();
|
|
|
|
_feed.run();
|
|
|
|
}
|
2023-09-26 01:54:31 +00:00
|
|
|
}
|
|
|
|
*/
|
2023-08-23 19:21:50 +00:00
|
|
|
}
|
2023-08-29 23:48:49 +00:00
|
|
|
|
|
|
|
//https://wiki.iteadstudio.com/Arduino_Dual_Step_Motor_Driver_Shield
|
|
|
|
void McopyProjector::setStepperMode (uint8_t mode) {
|
2023-09-22 01:06:31 +00:00
|
|
|
_mode = mode;
|
2023-08-29 23:48:49 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 1 :
|
|
|
|
digitalWrite(_takeupSettingA, LOW);
|
|
|
|
digitalWrite(_takeupSettingB, LOW);
|
|
|
|
digitalWrite(_feedSettingA, LOW);
|
|
|
|
digitalWrite(_feedSettingB, LOW);
|
|
|
|
break;
|
|
|
|
case 2 :
|
|
|
|
digitalWrite(_takeupSettingA, HIGH);
|
|
|
|
digitalWrite(_takeupSettingB, LOW);
|
|
|
|
digitalWrite(_feedSettingA, HIGH);
|
|
|
|
digitalWrite(_feedSettingB, LOW);
|
|
|
|
break;
|
|
|
|
case 4 :
|
|
|
|
digitalWrite(_takeupSettingA, LOW);
|
|
|
|
digitalWrite(_takeupSettingB, HIGH);
|
|
|
|
digitalWrite(_feedSettingA, LOW);
|
|
|
|
digitalWrite(_feedSettingB, HIGH);
|
|
|
|
break;
|
|
|
|
case 8 :
|
|
|
|
digitalWrite(_takeupSettingA, HIGH);
|
|
|
|
digitalWrite(_takeupSettingB, HIGH);
|
|
|
|
digitalWrite(_feedSettingA, HIGH);
|
|
|
|
digitalWrite(_feedSettingB, HIGH);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 01:06:31 +00:00
|
|
|
|
2023-09-26 01:54:31 +00:00
|
|
|
void McopyProjector::home () {
|
|
|
|
uint16_t steps = _motorSteps * _mode;
|
2023-10-03 17:44:04 +00:00
|
|
|
uint16_t quarter = steps / 4;
|
|
|
|
uint16_t eighth = quarter / 2;
|
|
|
|
uint16_t takeupPeak = 0;
|
|
|
|
uint16_t feedPeak = 0;
|
|
|
|
uint16_t takeupOffset = 0;
|
|
|
|
uint16_t feedOffset = 0;
|
|
|
|
long takeupReading = 0.0;
|
|
|
|
long feedReading = 0.0;
|
|
|
|
|
|
|
|
emitters(true);
|
|
|
|
delay(10);
|
|
|
|
|
2023-09-26 01:54:31 +00:00
|
|
|
for (uint16_t i = 0; i < steps; i++) {
|
2023-10-03 17:44:04 +00:00
|
|
|
takeupReading = analogReadAccurateAverage(_takeupReceiver);
|
|
|
|
feedReading = analogReadAccurateAverage(_feedReceiver);
|
|
|
|
_takeupSamples[i] = takeupReading;
|
|
|
|
_feedSamples[i] = feedReading;
|
2023-09-26 01:54:31 +00:00
|
|
|
if (i < steps - 1) {
|
|
|
|
_takeup.move(1);
|
2023-10-03 17:44:04 +00:00
|
|
|
_feed.move(1);
|
2023-09-26 01:54:31 +00:00
|
|
|
_takeup.runToPosition();
|
2023-10-03 17:44:04 +00:00
|
|
|
_feed.runToPosition();
|
2023-09-26 01:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 17:44:04 +00:00
|
|
|
|
2023-09-26 01:54:31 +00:00
|
|
|
for (uint16_t i = 0; i < steps; i++) {
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(", ");
|
2023-10-03 17:44:04 +00:00
|
|
|
Serial.print(_takeupSamples[i]);
|
|
|
|
Serial.print(", ");
|
|
|
|
Serial.println(_feedSamples[i]);
|
2023-09-26 01:54:31 +00:00
|
|
|
}
|
2023-10-03 17:44:04 +00:00
|
|
|
|
|
|
|
takeupPeak = findPeak(_takeupSamples, steps);
|
|
|
|
feedPeak = findPeak(_feedSamples, steps);
|
|
|
|
Serial.print(" takeup peak: ");
|
|
|
|
Serial.println(takeupPeak);
|
|
|
|
Serial.print(" feed peak: ");
|
|
|
|
Serial.println(feedPeak);
|
|
|
|
|
|
|
|
takeupOffset = abs(steps - takeupPeak);
|
|
|
|
feedOffset = abs(steps - takeupPeak);
|
|
|
|
|
|
|
|
Serial.print("takeup offset: ");
|
|
|
|
Serial.println(takeupOffset);
|
|
|
|
Serial.print(" feed offset: ");
|
|
|
|
Serial.println(feedOffset);
|
|
|
|
|
|
|
|
if (takeupOffset > 0) {
|
|
|
|
for (uint16_t i = 0; i < takeupOffset; i++) {
|
2023-09-26 01:54:31 +00:00
|
|
|
_takeup.move(-1);
|
|
|
|
_takeup.runToPosition();
|
|
|
|
}
|
2023-10-03 17:44:04 +00:00
|
|
|
}
|
|
|
|
if (feedOffset > 0) {
|
|
|
|
for (uint16_t i = 0; i < feedOffset; i++) {
|
|
|
|
_feed.move(-1);
|
|
|
|
_feed.runToPosition();
|
2023-09-26 01:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 17:44:04 +00:00
|
|
|
|
|
|
|
for (uint16_t i = 0; i < eighth; i++) {
|
|
|
|
_takeup.move(-1);
|
|
|
|
_feed.move(-1);
|
|
|
|
_takeup.runToPosition();
|
|
|
|
_feed.runToPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < quarter; i++) {
|
|
|
|
takeupReading = analogReadAccurateAverage(_takeupReceiver);
|
|
|
|
feedReading = analogReadAccurateAverage(_feedReceiver);
|
|
|
|
_takeupSamples[i] = takeupReading;
|
|
|
|
_feedSamples[i] = feedReading;
|
2023-09-26 01:54:31 +00:00
|
|
|
if (i < steps - 1) {
|
|
|
|
_takeup.move(1);
|
2023-10-03 17:44:04 +00:00
|
|
|
_feed.move(1);
|
2023-09-26 01:54:31 +00:00
|
|
|
_takeup.runToPosition();
|
2023-10-03 17:44:04 +00:00
|
|
|
_feed.runToPosition();
|
2023-09-26 01:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 17:44:04 +00:00
|
|
|
|
|
|
|
emitters(false);
|
|
|
|
|
|
|
|
takeupPeak = findPeak(_takeupSamples, quarter);
|
|
|
|
feedPeak = findPeak(_feedSamples, quarter);
|
|
|
|
takeupOffset = abs(quarter - takeupPeak);
|
|
|
|
feedOffset = abs(quarter - feedPeak);
|
|
|
|
|
|
|
|
if (takeupOffset > 0) {
|
|
|
|
for (uint16_t i = 0; i < takeupOffset; i++) {
|
2023-09-26 01:54:31 +00:00
|
|
|
_takeup.move(-1);
|
|
|
|
_takeup.runToPosition();
|
|
|
|
}
|
2023-10-03 17:44:04 +00:00
|
|
|
}
|
|
|
|
if (feedOffset > 0) {
|
|
|
|
for (uint16_t i = 0; i < feedOffset; i++) {
|
|
|
|
_feed.move(-1);
|
|
|
|
_feed.runToPosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_posTakeup = 0;
|
|
|
|
_posFeed = 0;
|
2023-09-26 01:54:31 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 01:06:31 +00:00
|
|
|
long McopyProjector::readVcc() {
|
|
|
|
long result;
|
|
|
|
// Read 1.1V reference against AVcc
|
|
|
|
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
|
|
|
|
delay(2); // Wait for Vref to settle
|
|
|
|
ADCSRA |= _BV(ADSC); // Convert
|
|
|
|
while (bit_is_set(ADCSRA,ADSC));
|
|
|
|
result = ADCL;
|
|
|
|
result |= ADCH<<8;
|
|
|
|
result = 1125300L / result; // Back-calculate AVcc in mV
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-26 01:54:31 +00:00
|
|
|
long McopyProjector::analogReadAccurate (uint8_t &pin) {
|
2023-09-22 01:06:31 +00:00
|
|
|
double Vcc = readVcc() / 1000.0;
|
|
|
|
double ADCValue = analogRead(pin);
|
|
|
|
return (ADCValue / 1024.0) * Vcc;
|
|
|
|
}
|
|
|
|
|
2023-09-26 01:54:31 +00:00
|
|
|
long McopyProjector::analogReadAccurateAverage (uint8_t &pin) {
|
|
|
|
uint8_t count = 3;
|
2023-09-22 01:06:31 +00:00
|
|
|
double sum = 0.0;
|
2023-09-26 01:54:31 +00:00
|
|
|
for (uint8_t i = 0; i < count; i++) {
|
2023-09-22 01:06:31 +00:00
|
|
|
sum += analogReadAccurate(pin);
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
return sum / (double) count;
|
2023-09-26 01:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-10-03 17:44:04 +00:00
|
|
|
|
|
|
|
void McopyProjector::emitters (bool enabled) {
|
|
|
|
digitalWrite(_takeupEmitter, enabled ? HIGH : LOW);
|
|
|
|
digitalWrite(_feedEmitter, enabled ? HIGH : LOW);
|
|
|
|
}
|