From c0e7422920673c513e8225c66f55119c15100a5c Mon Sep 17 00:00:00 2001 From: mattmcw Date: Fri, 5 Jul 2024 23:56:45 -0400 Subject: [PATCH] Work on oxberry camera firmware. Basics are there. Will add button features to mitchell. --- app/data/cfg.json | 2 +- app/package-lock.json | 2 +- app/package.json | 2 +- data/cfg.json | 2 +- .../EndstopCameraShield.cpp | 160 +++++++++++++++++- .../EndstopCameraShield/EndstopCameraShield.h | 49 ++++-- .../EndstopCameraShield.cpp | 160 +++++++++++++++++- .../EndstopCameraShield.h | 49 ++++-- .../TB6600MotorDriver.cpp | 4 +- ino/mcopy_mitchell_camera/TB6600MotorDriver.h | 3 +- .../EndstopCameraShield.cpp | 160 +++++++++++++++++- .../EndstopCameraShield.h | 49 ++++-- .../TB6600MotorDriver.cpp | 4 +- ino/mcopy_oxberry_camera/TB6600MotorDriver.h | 3 +- .../mcopy_oxberry_camera.ino | 108 +++++++++++- package-lock.json | 4 +- package.json | 2 +- processing/mcopy/cfg.json | 2 +- 18 files changed, 706 insertions(+), 59 deletions(-) diff --git a/app/data/cfg.json b/app/data/cfg.json index 37baf68..4c4c61d 100644 --- a/app/data/cfg.json +++ b/app/data/cfg.json @@ -1,5 +1,5 @@ { - "version": "1.8.120", + "version": "1.8.121", "ext_port": 1111, "profiles": { "mcopy": { diff --git a/app/package-lock.json b/app/package-lock.json index f791c97..cc80c15 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -1,6 +1,6 @@ { "name": "mcopy-app", - "version": "1.8.120", + "version": "1.8.121", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/app/package.json b/app/package.json index f57e3e0..d1035bf 100644 --- a/app/package.json +++ b/app/package.json @@ -1,6 +1,6 @@ { "name": "mcopy-app", - "version": "1.8.120", + "version": "1.8.121", "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 37baf68..4c4c61d 100644 --- a/data/cfg.json +++ b/data/cfg.json @@ -1,5 +1,5 @@ { - "version": "1.8.120", + "version": "1.8.121", "ext_port": 1111, "profiles": { "mcopy": { diff --git a/ino/lib/EndstopCameraShield/EndstopCameraShield.cpp b/ino/lib/EndstopCameraShield/EndstopCameraShield.cpp index 980c93b..96ad745 100644 --- a/ino/lib/EndstopCameraShield/EndstopCameraShield.cpp +++ b/ino/lib/EndstopCameraShield/EndstopCameraShield.cpp @@ -1,7 +1,12 @@ #include "EndstopCameraShield.h" -EndstopCameraShield::EndstopCameraShield (uint32_t usPulse, uint8_t microsteps) : motorUsPulse(usPulse), motorMicrosteps(microsteps), motor(motorEnablePin, motorDirectionPin, motorPulsePin, motorUsPulse, motorMicrosteps) { +volatile bool EndstopCameraShield::_direction = true; //true = forward, false = backward +volatile bool EndstopCameraShield::_enabled = false; +volatile bool EndstopCameraShield::_isClosed = false; +volatile bool EndstopCameraShield::_isOpened = false; +EndstopCameraShield::EndstopCameraShield (uint32_t usPulse, uint8_t microsteps) : _motorUsPulse(usPulse), _motorMicrosteps(microsteps), _motor(_motorEnablePin, _motorDirectionPin, _motorPulsePin, _motorUsPulse, _motorMicrosteps) { + _stepAngle = (double) 360 / ((double) microsteps * (double) 200); } void EndstopCameraShield::loop () { @@ -9,7 +14,154 @@ void EndstopCameraShield::loop () { } void EndstopCameraShield::setup () { - motor.setup(); - pinMode(emitterOpenPin, OUTPUT); - pinMode(emitterClosePin, OUTPUT); + _motor.setup(); + pinMode(_emitterOpenPin, OUTPUT); + pinMode(_emitterClosePin, OUTPUT); + + _checkState(); + _enableMotor(); +} + +void EndstopCameraShield::_enableCloseInterrupt() { + attachInterrupt(digitalPinToInterrupt(_receiverClosePin), EndstopCameraShield::_handleCloseInterrupt, FALLING); +} + +void EndstopCameraShield::_enableOpenInterrupt() { + attachInterrupt(digitalPinToInterrupt(_receiverOpenPin), EndstopCameraShield::_handleOpenInterrupt, FALLING); +} + +void EndstopCameraShield::_enableCloseEmitter() { + digitalWrite(_emitterClosePin, HIGH); +} + +void EndstopCameraShield::_enableOpenEmitter() { + digitalWrite(_emitterOpenPin, HIGH); +} + +void EndstopCameraShield::_enableMotor() { + _enabled = true; + _motor.enable(); +} + +void EndstopCameraShield::_disableCloseInterrupt() { + detachInterrupt(digitalPinToInterrupt(_receiverClosePin)); +} + +void EndstopCameraShield::_disableOpenInterrupt() { + detachInterrupt(digitalPinToInterrupt(_receiverOpenPin)); +} + +void EndstopCameraShield::_disableCloseEmitter() { + digitalWrite(_emitterClosePin, LOW); +} + +void EndstopCameraShield::_disableOpenEmitter() { + digitalWrite(_emitterOpenPin, LOW); +} + +void EndstopCameraShield::_disableMotor() { + _enabled = false; + _motor.release(); +} + +void EndstopCameraShield::_handleCloseInterrupt() { + _isClosed = true; +} + +void EndstopCameraShield::_handleOpenInterrupt() { + _isOpened = true; +} + +void EndstopCameraShield::_checkState() { + _enableCloseEmitter(); + _enableOpenEmitter(); + if (digitalRead(_receiverClosePin) == LOW) { + _isClosed = true; + } else if (digitalRead(_receiverOpenPin) == LOW) { + _isOpened = true; + } + _disableCloseEmitter(); + _disableOpenEmitter(); +} + +uint32_t EndstopCameraShield::frame() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isClosed = false; + _enableMotor(); + + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableCloseEmitter(); + _enableCloseInterrupt(); + primed = true; + } + if (primed && _isClosed) { + running = false; + } + i++; + } + _disableCloseInterrupt(); + _disableCloseEmitter(); + return i; +} + +uint32_t EndstopCameraShield::toOpen() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isOpened = false; + _enableMotor(); + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableOpenEmitter(); + _enableOpenInterrupt(); + primed = true; + } + if (primed && _isOpened) { + running = false; + } + i++; + } + _disableOpenInterrupt(); + _disableOpenEmitter(); + return i; +} + +uint32_t EndstopCameraShield::toClosed() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isClosed = false; + _enableMotor(); + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableCloseEmitter(); + _enableCloseInterrupt(); + primed = true; + } + if (primed && _isClosed) { + running = false; + } + i++; + } + _disableCloseInterrupt(); + _disableCloseEmitter(); + return i; +} + +void EndstopCameraShield::setDirection(bool direction) { + if (direction != _direction) { + _direction = direction; + _motor.setDirection(_direction); + } +} + +bool EndstopCameraShield::isOpened() { + return _isOpened; +} + +bool EndstopCameraShield::isClosed() { + return _isClosed; } \ No newline at end of file diff --git a/ino/lib/EndstopCameraShield/EndstopCameraShield.h b/ino/lib/EndstopCameraShield/EndstopCameraShield.h index 880d384..c24d828 100644 --- a/ino/lib/EndstopCameraShield/EndstopCameraShield.h +++ b/ino/lib/EndstopCameraShield/EndstopCameraShield.h @@ -14,19 +14,42 @@ class EndstopCameraShield { private: - const uint8_t receiverClosePin = 2; - const uint8_t receiverOpenPin = 3; - const uint8_t emitterOpenPin = 4; - const uint8_t emitterClosePin = 5; + const uint8_t _receiverClosePin = 2; + const uint8_t _receiverOpenPin = 3; + const uint8_t _emitterOpenPin = 4; + const uint8_t _emitterClosePin = 5; - const uint8_t motorEnablePin = 6; - const uint8_t motorDirectionPin = 7; - const uint8_t motorPulsePin = 8; + const uint8_t _motorEnablePin = 6; + const uint8_t _motorDirectionPin = 7; + const uint8_t _motorPulsePin = 8; + + const double _ledAngle = 7.0; - volatile uint32_t motorUsPulse = 300; - volatile uint8_t motorMicrosteps = 2; //half stepping + volatile uint32_t _motorUsPulse = 300; + volatile uint8_t _motorMicrosteps = 2; //half stepping + volatile double _stepAngle = (double) 360 / ((double) 2 * (double) 200); - TB6600MotorDriver motor; + TB6600MotorDriver _motor; + + static volatile bool _direction; //true = forward, false = backward + static volatile bool _enabled; + static volatile bool _isClosed; + static volatile bool _isOpened; + + void _checkState(); + void _enableCloseInterrupt(); + void _enableOpenInterrupt(); + void _enableCloseEmitter(); + void _enableOpenEmitter(); + void _enableMotor(); + void _disableCloseInterrupt(); + void _disableOpenInterrupt(); + void _disableCloseEmitter(); + void _disableOpenEmitter(); + void _disableMotor(); + + static void _handleCloseInterrupt(); + static void _handleOpenInterrupt(); public: @@ -34,6 +57,12 @@ class EndstopCameraShield { void setup(); void loop(); + uint32_t frame(); + uint32_t toOpen(); + uint32_t toClosed(); + void setDirection(bool direction); + bool isOpened(); + bool isClosed(); }; #endif \ No newline at end of file diff --git a/ino/mcopy_mitchell_camera/EndstopCameraShield.cpp b/ino/mcopy_mitchell_camera/EndstopCameraShield.cpp index 980c93b..96ad745 100644 --- a/ino/mcopy_mitchell_camera/EndstopCameraShield.cpp +++ b/ino/mcopy_mitchell_camera/EndstopCameraShield.cpp @@ -1,7 +1,12 @@ #include "EndstopCameraShield.h" -EndstopCameraShield::EndstopCameraShield (uint32_t usPulse, uint8_t microsteps) : motorUsPulse(usPulse), motorMicrosteps(microsteps), motor(motorEnablePin, motorDirectionPin, motorPulsePin, motorUsPulse, motorMicrosteps) { +volatile bool EndstopCameraShield::_direction = true; //true = forward, false = backward +volatile bool EndstopCameraShield::_enabled = false; +volatile bool EndstopCameraShield::_isClosed = false; +volatile bool EndstopCameraShield::_isOpened = false; +EndstopCameraShield::EndstopCameraShield (uint32_t usPulse, uint8_t microsteps) : _motorUsPulse(usPulse), _motorMicrosteps(microsteps), _motor(_motorEnablePin, _motorDirectionPin, _motorPulsePin, _motorUsPulse, _motorMicrosteps) { + _stepAngle = (double) 360 / ((double) microsteps * (double) 200); } void EndstopCameraShield::loop () { @@ -9,7 +14,154 @@ void EndstopCameraShield::loop () { } void EndstopCameraShield::setup () { - motor.setup(); - pinMode(emitterOpenPin, OUTPUT); - pinMode(emitterClosePin, OUTPUT); + _motor.setup(); + pinMode(_emitterOpenPin, OUTPUT); + pinMode(_emitterClosePin, OUTPUT); + + _checkState(); + _enableMotor(); +} + +void EndstopCameraShield::_enableCloseInterrupt() { + attachInterrupt(digitalPinToInterrupt(_receiverClosePin), EndstopCameraShield::_handleCloseInterrupt, FALLING); +} + +void EndstopCameraShield::_enableOpenInterrupt() { + attachInterrupt(digitalPinToInterrupt(_receiverOpenPin), EndstopCameraShield::_handleOpenInterrupt, FALLING); +} + +void EndstopCameraShield::_enableCloseEmitter() { + digitalWrite(_emitterClosePin, HIGH); +} + +void EndstopCameraShield::_enableOpenEmitter() { + digitalWrite(_emitterOpenPin, HIGH); +} + +void EndstopCameraShield::_enableMotor() { + _enabled = true; + _motor.enable(); +} + +void EndstopCameraShield::_disableCloseInterrupt() { + detachInterrupt(digitalPinToInterrupt(_receiverClosePin)); +} + +void EndstopCameraShield::_disableOpenInterrupt() { + detachInterrupt(digitalPinToInterrupt(_receiverOpenPin)); +} + +void EndstopCameraShield::_disableCloseEmitter() { + digitalWrite(_emitterClosePin, LOW); +} + +void EndstopCameraShield::_disableOpenEmitter() { + digitalWrite(_emitterOpenPin, LOW); +} + +void EndstopCameraShield::_disableMotor() { + _enabled = false; + _motor.release(); +} + +void EndstopCameraShield::_handleCloseInterrupt() { + _isClosed = true; +} + +void EndstopCameraShield::_handleOpenInterrupt() { + _isOpened = true; +} + +void EndstopCameraShield::_checkState() { + _enableCloseEmitter(); + _enableOpenEmitter(); + if (digitalRead(_receiverClosePin) == LOW) { + _isClosed = true; + } else if (digitalRead(_receiverOpenPin) == LOW) { + _isOpened = true; + } + _disableCloseEmitter(); + _disableOpenEmitter(); +} + +uint32_t EndstopCameraShield::frame() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isClosed = false; + _enableMotor(); + + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableCloseEmitter(); + _enableCloseInterrupt(); + primed = true; + } + if (primed && _isClosed) { + running = false; + } + i++; + } + _disableCloseInterrupt(); + _disableCloseEmitter(); + return i; +} + +uint32_t EndstopCameraShield::toOpen() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isOpened = false; + _enableMotor(); + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableOpenEmitter(); + _enableOpenInterrupt(); + primed = true; + } + if (primed && _isOpened) { + running = false; + } + i++; + } + _disableOpenInterrupt(); + _disableOpenEmitter(); + return i; +} + +uint32_t EndstopCameraShield::toClosed() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isClosed = false; + _enableMotor(); + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableCloseEmitter(); + _enableCloseInterrupt(); + primed = true; + } + if (primed && _isClosed) { + running = false; + } + i++; + } + _disableCloseInterrupt(); + _disableCloseEmitter(); + return i; +} + +void EndstopCameraShield::setDirection(bool direction) { + if (direction != _direction) { + _direction = direction; + _motor.setDirection(_direction); + } +} + +bool EndstopCameraShield::isOpened() { + return _isOpened; +} + +bool EndstopCameraShield::isClosed() { + return _isClosed; } \ No newline at end of file diff --git a/ino/mcopy_mitchell_camera/EndstopCameraShield.h b/ino/mcopy_mitchell_camera/EndstopCameraShield.h index 880d384..c24d828 100644 --- a/ino/mcopy_mitchell_camera/EndstopCameraShield.h +++ b/ino/mcopy_mitchell_camera/EndstopCameraShield.h @@ -14,19 +14,42 @@ class EndstopCameraShield { private: - const uint8_t receiverClosePin = 2; - const uint8_t receiverOpenPin = 3; - const uint8_t emitterOpenPin = 4; - const uint8_t emitterClosePin = 5; + const uint8_t _receiverClosePin = 2; + const uint8_t _receiverOpenPin = 3; + const uint8_t _emitterOpenPin = 4; + const uint8_t _emitterClosePin = 5; - const uint8_t motorEnablePin = 6; - const uint8_t motorDirectionPin = 7; - const uint8_t motorPulsePin = 8; + const uint8_t _motorEnablePin = 6; + const uint8_t _motorDirectionPin = 7; + const uint8_t _motorPulsePin = 8; + + const double _ledAngle = 7.0; - volatile uint32_t motorUsPulse = 300; - volatile uint8_t motorMicrosteps = 2; //half stepping + volatile uint32_t _motorUsPulse = 300; + volatile uint8_t _motorMicrosteps = 2; //half stepping + volatile double _stepAngle = (double) 360 / ((double) 2 * (double) 200); - TB6600MotorDriver motor; + TB6600MotorDriver _motor; + + static volatile bool _direction; //true = forward, false = backward + static volatile bool _enabled; + static volatile bool _isClosed; + static volatile bool _isOpened; + + void _checkState(); + void _enableCloseInterrupt(); + void _enableOpenInterrupt(); + void _enableCloseEmitter(); + void _enableOpenEmitter(); + void _enableMotor(); + void _disableCloseInterrupt(); + void _disableOpenInterrupt(); + void _disableCloseEmitter(); + void _disableOpenEmitter(); + void _disableMotor(); + + static void _handleCloseInterrupt(); + static void _handleOpenInterrupt(); public: @@ -34,6 +57,12 @@ class EndstopCameraShield { void setup(); void loop(); + uint32_t frame(); + uint32_t toOpen(); + uint32_t toClosed(); + void setDirection(bool direction); + bool isOpened(); + bool isClosed(); }; #endif \ No newline at end of file diff --git a/ino/mcopy_mitchell_camera/TB6600MotorDriver.cpp b/ino/mcopy_mitchell_camera/TB6600MotorDriver.cpp index 38e5936..3057cc6 100644 --- a/ino/mcopy_mitchell_camera/TB6600MotorDriver.cpp +++ b/ino/mcopy_mitchell_camera/TB6600MotorDriver.cpp @@ -23,10 +23,10 @@ void TB6600MotorDriver::setup() { pinMode(_pulsePin, OUTPUT); digitalWrite(_directionPin, LOW); - digitalWrite(_enablePin, HIGH); + digitalWrite(_enablePin, _enabled ? HIGH : LOW); } -void TB6600MotorDriver::setDirection(uint8_t direction) { +void TB6600MotorDriver::setDirection(bool direction) { if (direction != _direction) { _direction = direction; _setDirection(); diff --git a/ino/mcopy_mitchell_camera/TB6600MotorDriver.h b/ino/mcopy_mitchell_camera/TB6600MotorDriver.h index f13c487..8e44cd3 100644 --- a/ino/mcopy_mitchell_camera/TB6600MotorDriver.h +++ b/ino/mcopy_mitchell_camera/TB6600MotorDriver.h @@ -35,10 +35,9 @@ class TB6600MotorDriver { TB6600MotorDriver(); TB6600MotorDriver(uint8_t enablePin, uint8_t directionPin, uint8_t pulsePin, uint32_t usPulse, uint8_t microsteps); - void setup(); - void setDirection(uint8_t direction); + void setDirection(bool direction); void setSpeed(uint16_t rpm); //full diff --git a/ino/mcopy_oxberry_camera/EndstopCameraShield.cpp b/ino/mcopy_oxberry_camera/EndstopCameraShield.cpp index 980c93b..96ad745 100644 --- a/ino/mcopy_oxberry_camera/EndstopCameraShield.cpp +++ b/ino/mcopy_oxberry_camera/EndstopCameraShield.cpp @@ -1,7 +1,12 @@ #include "EndstopCameraShield.h" -EndstopCameraShield::EndstopCameraShield (uint32_t usPulse, uint8_t microsteps) : motorUsPulse(usPulse), motorMicrosteps(microsteps), motor(motorEnablePin, motorDirectionPin, motorPulsePin, motorUsPulse, motorMicrosteps) { +volatile bool EndstopCameraShield::_direction = true; //true = forward, false = backward +volatile bool EndstopCameraShield::_enabled = false; +volatile bool EndstopCameraShield::_isClosed = false; +volatile bool EndstopCameraShield::_isOpened = false; +EndstopCameraShield::EndstopCameraShield (uint32_t usPulse, uint8_t microsteps) : _motorUsPulse(usPulse), _motorMicrosteps(microsteps), _motor(_motorEnablePin, _motorDirectionPin, _motorPulsePin, _motorUsPulse, _motorMicrosteps) { + _stepAngle = (double) 360 / ((double) microsteps * (double) 200); } void EndstopCameraShield::loop () { @@ -9,7 +14,154 @@ void EndstopCameraShield::loop () { } void EndstopCameraShield::setup () { - motor.setup(); - pinMode(emitterOpenPin, OUTPUT); - pinMode(emitterClosePin, OUTPUT); + _motor.setup(); + pinMode(_emitterOpenPin, OUTPUT); + pinMode(_emitterClosePin, OUTPUT); + + _checkState(); + _enableMotor(); +} + +void EndstopCameraShield::_enableCloseInterrupt() { + attachInterrupt(digitalPinToInterrupt(_receiverClosePin), EndstopCameraShield::_handleCloseInterrupt, FALLING); +} + +void EndstopCameraShield::_enableOpenInterrupt() { + attachInterrupt(digitalPinToInterrupt(_receiverOpenPin), EndstopCameraShield::_handleOpenInterrupt, FALLING); +} + +void EndstopCameraShield::_enableCloseEmitter() { + digitalWrite(_emitterClosePin, HIGH); +} + +void EndstopCameraShield::_enableOpenEmitter() { + digitalWrite(_emitterOpenPin, HIGH); +} + +void EndstopCameraShield::_enableMotor() { + _enabled = true; + _motor.enable(); +} + +void EndstopCameraShield::_disableCloseInterrupt() { + detachInterrupt(digitalPinToInterrupt(_receiverClosePin)); +} + +void EndstopCameraShield::_disableOpenInterrupt() { + detachInterrupt(digitalPinToInterrupt(_receiverOpenPin)); +} + +void EndstopCameraShield::_disableCloseEmitter() { + digitalWrite(_emitterClosePin, LOW); +} + +void EndstopCameraShield::_disableOpenEmitter() { + digitalWrite(_emitterOpenPin, LOW); +} + +void EndstopCameraShield::_disableMotor() { + _enabled = false; + _motor.release(); +} + +void EndstopCameraShield::_handleCloseInterrupt() { + _isClosed = true; +} + +void EndstopCameraShield::_handleOpenInterrupt() { + _isOpened = true; +} + +void EndstopCameraShield::_checkState() { + _enableCloseEmitter(); + _enableOpenEmitter(); + if (digitalRead(_receiverClosePin) == LOW) { + _isClosed = true; + } else if (digitalRead(_receiverOpenPin) == LOW) { + _isOpened = true; + } + _disableCloseEmitter(); + _disableOpenEmitter(); +} + +uint32_t EndstopCameraShield::frame() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isClosed = false; + _enableMotor(); + + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableCloseEmitter(); + _enableCloseInterrupt(); + primed = true; + } + if (primed && _isClosed) { + running = false; + } + i++; + } + _disableCloseInterrupt(); + _disableCloseEmitter(); + return i; +} + +uint32_t EndstopCameraShield::toOpen() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isOpened = false; + _enableMotor(); + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableOpenEmitter(); + _enableOpenInterrupt(); + primed = true; + } + if (primed && _isOpened) { + running = false; + } + i++; + } + _disableOpenInterrupt(); + _disableOpenEmitter(); + return i; +} + +uint32_t EndstopCameraShield::toClosed() { + bool primed = false; + bool running = true; + uint32_t i = 0; + _isClosed = false; + _enableMotor(); + while (running) { + if (!primed && (double) i * _stepAngle > _ledAngle) { + _enableCloseEmitter(); + _enableCloseInterrupt(); + primed = true; + } + if (primed && _isClosed) { + running = false; + } + i++; + } + _disableCloseInterrupt(); + _disableCloseEmitter(); + return i; +} + +void EndstopCameraShield::setDirection(bool direction) { + if (direction != _direction) { + _direction = direction; + _motor.setDirection(_direction); + } +} + +bool EndstopCameraShield::isOpened() { + return _isOpened; +} + +bool EndstopCameraShield::isClosed() { + return _isClosed; } \ No newline at end of file diff --git a/ino/mcopy_oxberry_camera/EndstopCameraShield.h b/ino/mcopy_oxberry_camera/EndstopCameraShield.h index 880d384..c24d828 100644 --- a/ino/mcopy_oxberry_camera/EndstopCameraShield.h +++ b/ino/mcopy_oxberry_camera/EndstopCameraShield.h @@ -14,19 +14,42 @@ class EndstopCameraShield { private: - const uint8_t receiverClosePin = 2; - const uint8_t receiverOpenPin = 3; - const uint8_t emitterOpenPin = 4; - const uint8_t emitterClosePin = 5; + const uint8_t _receiverClosePin = 2; + const uint8_t _receiverOpenPin = 3; + const uint8_t _emitterOpenPin = 4; + const uint8_t _emitterClosePin = 5; - const uint8_t motorEnablePin = 6; - const uint8_t motorDirectionPin = 7; - const uint8_t motorPulsePin = 8; + const uint8_t _motorEnablePin = 6; + const uint8_t _motorDirectionPin = 7; + const uint8_t _motorPulsePin = 8; + + const double _ledAngle = 7.0; - volatile uint32_t motorUsPulse = 300; - volatile uint8_t motorMicrosteps = 2; //half stepping + volatile uint32_t _motorUsPulse = 300; + volatile uint8_t _motorMicrosteps = 2; //half stepping + volatile double _stepAngle = (double) 360 / ((double) 2 * (double) 200); - TB6600MotorDriver motor; + TB6600MotorDriver _motor; + + static volatile bool _direction; //true = forward, false = backward + static volatile bool _enabled; + static volatile bool _isClosed; + static volatile bool _isOpened; + + void _checkState(); + void _enableCloseInterrupt(); + void _enableOpenInterrupt(); + void _enableCloseEmitter(); + void _enableOpenEmitter(); + void _enableMotor(); + void _disableCloseInterrupt(); + void _disableOpenInterrupt(); + void _disableCloseEmitter(); + void _disableOpenEmitter(); + void _disableMotor(); + + static void _handleCloseInterrupt(); + static void _handleOpenInterrupt(); public: @@ -34,6 +57,12 @@ class EndstopCameraShield { void setup(); void loop(); + uint32_t frame(); + uint32_t toOpen(); + uint32_t toClosed(); + void setDirection(bool direction); + bool isOpened(); + bool isClosed(); }; #endif \ No newline at end of file diff --git a/ino/mcopy_oxberry_camera/TB6600MotorDriver.cpp b/ino/mcopy_oxberry_camera/TB6600MotorDriver.cpp index 38e5936..3057cc6 100644 --- a/ino/mcopy_oxberry_camera/TB6600MotorDriver.cpp +++ b/ino/mcopy_oxberry_camera/TB6600MotorDriver.cpp @@ -23,10 +23,10 @@ void TB6600MotorDriver::setup() { pinMode(_pulsePin, OUTPUT); digitalWrite(_directionPin, LOW); - digitalWrite(_enablePin, HIGH); + digitalWrite(_enablePin, _enabled ? HIGH : LOW); } -void TB6600MotorDriver::setDirection(uint8_t direction) { +void TB6600MotorDriver::setDirection(bool direction) { if (direction != _direction) { _direction = direction; _setDirection(); diff --git a/ino/mcopy_oxberry_camera/TB6600MotorDriver.h b/ino/mcopy_oxberry_camera/TB6600MotorDriver.h index f13c487..8e44cd3 100644 --- a/ino/mcopy_oxberry_camera/TB6600MotorDriver.h +++ b/ino/mcopy_oxberry_camera/TB6600MotorDriver.h @@ -35,10 +35,9 @@ class TB6600MotorDriver { TB6600MotorDriver(); TB6600MotorDriver(uint8_t enablePin, uint8_t directionPin, uint8_t pulsePin, uint32_t usPulse, uint8_t microsteps); - void setup(); - void setDirection(uint8_t direction); + void setDirection(bool direction); void setSpeed(uint16_t rpm); //full diff --git a/ino/mcopy_oxberry_camera/mcopy_oxberry_camera.ino b/ino/mcopy_oxberry_camera/mcopy_oxberry_camera.ino index a6cf122..a4ba366 100644 --- a/ino/mcopy_oxberry_camera/mcopy_oxberry_camera.ino +++ b/ino/mcopy_oxberry_camera/mcopy_oxberry_camera.ino @@ -1,15 +1,121 @@ #include "EndstopCameraShield.h" #include "McopySerial.h" +const bool DEBUG = false; + +const uint8_t enableButtonPin = 9; +const uint8_t enableButtonPin = 10; +const uint8_t enableButtonPin = 11; +const uint8_t enableButtonPin = 12; +const uint8_t LEDPin = 13; + const uint32_t usPulse = 300; const uint8_t microsteps = 2; +volatile char cmdChar = 'z'; +volatile long now; + +volatile long exposureAvg = -1; //pre-fill +volatile String exposureString; +volatile long exposureTarget = -1; + +volatile bool direction = true; +volatile bool directionSwitch = true; EndstopCameraShield cam(usPulse, microsteps); +McopySerial mc; void setup () { + mc.begin(mc.CAMERA_IDENTIFIER); + mc.debug(DEBUG); cam.setup(); + if (cam.isOpened()) { + mc.log("Camera is OPENED"); + } else if (cam.isClosed()) { + mc.log("Camera is CLOSED"); + } } void loop () { + now = millis(); + cmdChar = mc.loop(); + cmd(cmdChar); cam.loop(); -} \ No newline at end of file +} + +void cmd (char val) { + if (val == mc.CAMERA_FORWARD) { + camera_direction(true); + } else if (val == mc.CAMERA_BACKWARD) { + camera_direction(false); + } else if (val == mc.CAMERA) { + camera(); + } else if (val == mc.CAMERA_EXPOSURE) { + exposure(); + } else if (val == mc.STATE) { + state(); + } +} + +void exposure () { + exposureString = mc.getString(); + parseExposureString(); + cameraFrame = exposureTarget; + mc.confirm(mc.CAMERA_EXPOSURE); +} + +void parseExposureString () { + exposureTarget = exposureString.toInt(); +} + +void camera_direction (boolean state) { + direction = state; + cam.setDirection(direction); + if (state) { + mc.confirm(mc.CAMERA_FORWARD); + mc.log("camera_direction(true)"); + } else { + mc.confirm(mc.CAMERA_BACKWARD); + mc.log("camera_direction(false)"); + } +} + +void camera () { + long start, half, pause; + if (exposureTarget > -1) { + half = exposureAvg / 2; //assume a 180 shutter + pause = exposureTarget - half; + if (pause < exposureAvg) { + cam.frame(); + } else { + cam.toOpen(); + delay(pause); + cam.toClose(); + } + + } else{ + millis(); + cam.frame(); + updateAvg(millis() - start); + } + mc.confirm(mc.CAMERA); +} + +void state () { + String stateString = String(mc.STATE); + stateString += String(mc.CAMERA_EXPOSURE); + if (exposureTarget > -1) { + stateString += String(exposureTarget); + } else { + stateString += String(exposureAvg); + } + stateString += String(mc.STATE); + mc.sendString(stateString); +} + +void updateAvg (long value) { + exposureAvg = round((exposureAvg + value) / 2); +} + +/** + * Button logic + **/ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 68ee361..ce775f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mcopy", - "version": "1.8.120", + "version": "1.8.121", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mcopy", - "version": "1.8.120", + "version": "1.8.121", "license": "MIT", "dependencies": { "alert": "file:app/lib/alert", diff --git a/package.json b/package.json index f7149d3..6ec41e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mcopy", - "version": "1.8.120", + "version": "1.8.121", "description": "Small gauge film optical printer platform", "main": "build.js", "directories": { diff --git a/processing/mcopy/cfg.json b/processing/mcopy/cfg.json index 37baf68..4c4c61d 100644 --- a/processing/mcopy/cfg.json +++ b/processing/mcopy/cfg.json @@ -1,5 +1,5 @@ { - "version": "1.8.120", + "version": "1.8.121", "ext_port": 1111, "profiles": { "mcopy": {