2024-06-30 15:50:02 +00:00
|
|
|
#include "EndstopCameraShield.h"
|
|
|
|
|
2024-07-06 03:56:45 +00:00
|
|
|
volatile bool EndstopCameraShield::_direction = true; //true = forward, false = backward
|
|
|
|
volatile bool EndstopCameraShield::_enabled = false;
|
|
|
|
volatile bool EndstopCameraShield::_isClosed = false;
|
|
|
|
volatile bool EndstopCameraShield::_isOpened = false;
|
2024-06-30 15:50:02 +00:00
|
|
|
|
2024-07-06 03:56:45 +00:00
|
|
|
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);
|
2024-06-30 15:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EndstopCameraShield::loop () {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndstopCameraShield::setup () {
|
2024-07-06 03:56:45 +00:00
|
|
|
_motor.setup();
|
|
|
|
pinMode(_emitterOpenPin, OUTPUT);
|
|
|
|
pinMode(_emitterClosePin, OUTPUT);
|
|
|
|
|
|
|
|
_checkState();
|
|
|
|
_enableMotor();
|
2024-10-22 21:33:18 +00:00
|
|
|
//Serial.println(_minSteps);
|
2024-07-06 03:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2024-07-31 00:00:09 +00:00
|
|
|
delay(3);
|
2024-07-06 03:56:45 +00:00
|
|
|
if (digitalRead(_receiverClosePin) == LOW) {
|
|
|
|
_isClosed = true;
|
|
|
|
} else if (digitalRead(_receiverOpenPin) == LOW) {
|
|
|
|
_isOpened = true;
|
|
|
|
}
|
|
|
|
_disableCloseEmitter();
|
|
|
|
_disableOpenEmitter();
|
|
|
|
}
|
|
|
|
|
2024-07-31 00:00:09 +00:00
|
|
|
void EndstopCameraShield::_checkClose() {
|
|
|
|
if (digitalRead(_receiverClosePin) == LOW) {
|
|
|
|
_isClosed = true;
|
|
|
|
} else {
|
|
|
|
_isClosed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndstopCameraShield::_checkOpen() {
|
|
|
|
if (digitalRead(_receiverOpenPin) == LOW) {
|
|
|
|
_isOpened = true;
|
|
|
|
} else {
|
|
|
|
_isOpened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 03:56:45 +00:00
|
|
|
uint32_t EndstopCameraShield::frame() {
|
|
|
|
bool primed = false;
|
|
|
|
bool running = true;
|
|
|
|
uint32_t i = 0;
|
|
|
|
_isClosed = false;
|
|
|
|
_enableMotor();
|
|
|
|
|
|
|
|
while (running) {
|
2024-07-31 00:00:09 +00:00
|
|
|
if (!primed && i > _minSteps) {
|
2024-07-06 03:56:45 +00:00
|
|
|
_enableCloseEmitter();
|
2024-07-31 00:00:09 +00:00
|
|
|
//_enableCloseInterrupt();
|
2024-07-06 03:56:45 +00:00
|
|
|
primed = true;
|
|
|
|
}
|
2024-07-31 00:00:09 +00:00
|
|
|
_checkClose();
|
2024-07-06 03:56:45 +00:00
|
|
|
if (primed && _isClosed) {
|
|
|
|
running = false;
|
2024-07-31 00:00:09 +00:00
|
|
|
break;
|
2024-07-06 03:56:45 +00:00
|
|
|
}
|
2024-07-31 00:00:09 +00:00
|
|
|
_motor.step();
|
2024-07-06 03:56:45 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
_disableCloseEmitter();
|
2024-08-03 12:46:46 +00:00
|
|
|
//_disableCloseInterrupt();
|
2024-07-06 03:56:45 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t EndstopCameraShield::toOpen() {
|
|
|
|
bool primed = false;
|
|
|
|
bool running = true;
|
|
|
|
uint32_t i = 0;
|
|
|
|
_isOpened = false;
|
|
|
|
_enableMotor();
|
|
|
|
while (running) {
|
2024-07-31 00:00:09 +00:00
|
|
|
if (!primed && i > _minSteps) {
|
2024-07-06 03:56:45 +00:00
|
|
|
_enableOpenEmitter();
|
2024-07-31 00:00:09 +00:00
|
|
|
//_enableOpenInterrupt();
|
2024-07-06 03:56:45 +00:00
|
|
|
primed = true;
|
|
|
|
}
|
2024-07-31 00:00:09 +00:00
|
|
|
_checkOpen();
|
2024-07-06 03:56:45 +00:00
|
|
|
if (primed && _isOpened) {
|
|
|
|
running = false;
|
2024-07-31 00:00:09 +00:00
|
|
|
break;
|
2024-07-06 03:56:45 +00:00
|
|
|
}
|
2024-07-31 00:00:09 +00:00
|
|
|
_motor.step();
|
2024-07-06 03:56:45 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
_disableOpenEmitter();
|
2024-08-03 12:46:46 +00:00
|
|
|
//_disableOpenInterrupt();
|
2024-07-06 03:56:45 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2024-07-08 02:17:48 +00:00
|
|
|
uint32_t EndstopCameraShield::toClose() {
|
2024-07-06 03:56:45 +00:00
|
|
|
bool primed = false;
|
|
|
|
bool running = true;
|
|
|
|
uint32_t i = 0;
|
|
|
|
_isClosed = false;
|
|
|
|
_enableMotor();
|
|
|
|
while (running) {
|
2024-07-31 00:00:09 +00:00
|
|
|
if (!primed && i > _minSteps) {
|
2024-07-06 03:56:45 +00:00
|
|
|
_enableCloseEmitter();
|
2024-07-31 00:00:09 +00:00
|
|
|
//_enableCloseInterrupt();
|
2024-07-06 03:56:45 +00:00
|
|
|
primed = true;
|
|
|
|
}
|
2024-07-31 00:00:09 +00:00
|
|
|
_checkClose();
|
2024-07-06 03:56:45 +00:00
|
|
|
if (primed && _isClosed) {
|
|
|
|
running = false;
|
2024-07-31 00:00:09 +00:00
|
|
|
break;
|
2024-07-06 03:56:45 +00:00
|
|
|
}
|
2024-07-31 00:00:09 +00:00
|
|
|
_motor.step();
|
2024-07-06 03:56:45 +00:00
|
|
|
i++;
|
|
|
|
}
|
2024-08-03 12:46:46 +00:00
|
|
|
_disableCloseEmitter();
|
|
|
|
//_disableCloseInterrupt();
|
2024-07-06 03:56:45 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndstopCameraShield::setDirection(bool direction) {
|
|
|
|
if (direction != _direction) {
|
|
|
|
_direction = direction;
|
|
|
|
_motor.setDirection(_direction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 12:46:46 +00:00
|
|
|
bool EndstopCameraShield::getDirection() {
|
|
|
|
return _direction;
|
|
|
|
}
|
|
|
|
|
2024-07-06 03:56:45 +00:00
|
|
|
bool EndstopCameraShield::isOpened() {
|
|
|
|
return _isOpened;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EndstopCameraShield::isClosed() {
|
|
|
|
return _isClosed;
|
2024-07-31 00:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EndstopCameraShield::test () {
|
|
|
|
_enableCloseEmitter();
|
|
|
|
_enableOpenEmitter();
|
|
|
|
delay(1000);
|
|
|
|
for (uint32_t i = 0; i < _motorMicrosteps * 200; i++) {
|
|
|
|
_motor.step();
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(" ");
|
|
|
|
if (digitalRead(_receiverOpenPin) == HIGH) {
|
|
|
|
Serial.print("OPEN _ ");
|
|
|
|
} else {
|
|
|
|
Serial.print("OPEN x ");
|
|
|
|
}
|
|
|
|
if (digitalRead(_receiverClosePin) == HIGH) {
|
|
|
|
Serial.print("CLOSE _");
|
|
|
|
} else {
|
|
|
|
Serial.print("CLOSE x");
|
|
|
|
}
|
|
|
|
Serial.println("");
|
|
|
|
}
|
|
|
|
_enableCloseEmitter();
|
|
|
|
_enableOpenEmitter();
|
2024-06-30 15:50:02 +00:00
|
|
|
}
|