From 24fe1e877353d48df17d1848a4a5fe5b393f75dc Mon Sep 17 00:00:00 2001 From: mattmcw Date: Fri, 25 Aug 2023 16:44:28 -0400 Subject: [PATCH] Work on projector: Still having issues with IteadDualStepperShield (addresses wrong motor, wrong number of steps). --- .../IteadDualStepperShield.cpp | 4 +- .../IteadDualStepperShield.h | 4 +- .../McopyProjector.cpp | 42 ++++++++++++------- ino/mcopy_projector_firmware/McopyProjector.h | 16 ++++--- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/ino/mcopy_projector_firmware/IteadDualStepperShield.cpp b/ino/mcopy_projector_firmware/IteadDualStepperShield.cpp index f564e9b..7e9bfd9 100644 --- a/ino/mcopy_projector_firmware/IteadDualStepperShield.cpp +++ b/ino/mcopy_projector_firmware/IteadDualStepperShield.cpp @@ -61,7 +61,7 @@ void IteadDualStepperShield::_both () { delayMicroseconds(_usStepA); } } -void IteadDualStepperShield::step (uint8_t motor, uint16_t steps, uint8_t dir) { +void IteadDualStepperShield::step (uint8_t motor, uint64_t steps, uint8_t dir) { uint8_t stepPin = motor == 0 ? _stepA : _stepB; setDir(motor, dir); for (int i = 0; i < steps; i++) { @@ -74,7 +74,7 @@ void IteadDualStepperShield::onestep (uint8_t motor, uint8_t dir) { _single(stepPin); } -void IteadDualStepperShield::stepBoth (uint16_t steps) { +void IteadDualStepperShield::stepBoth (uint64_t steps) { for (int i = 0; i < steps; i++) { _both(); } diff --git a/ino/mcopy_projector_firmware/IteadDualStepperShield.h b/ino/mcopy_projector_firmware/IteadDualStepperShield.h index 76fca75..85f2f7b 100644 --- a/ino/mcopy_projector_firmware/IteadDualStepperShield.h +++ b/ino/mcopy_projector_firmware/IteadDualStepperShield.h @@ -31,8 +31,8 @@ class IteadDualStepperShield { void setSpeed(uint8_t motor, uint16_t speed); //full - void step(uint8_t motor, uint16_t steps, uint8_t dir); - void stepBoth(uint16_t steps); + void step(uint8_t motor, uint64_t steps, uint8_t dir); + void stepBoth(uint64_t steps); void onestep(uint8_t motor, uint8_t dir); void release(); diff --git a/ino/mcopy_projector_firmware/McopyProjector.cpp b/ino/mcopy_projector_firmware/McopyProjector.cpp index 6a88497..4e66aca 100644 --- a/ino/mcopy_projector_firmware/McopyProjector.cpp +++ b/ino/mcopy_projector_firmware/McopyProjector.cpp @@ -16,11 +16,11 @@ void McopyProjector::begin () { void McopyProjector::setDirection (bool dir) { _dir = dir; if (_dir) { - steppers.setDir(0, 1); - steppers.setDir(1, 1); + steppers.setDir(0, FORWARD); + steppers.setDir(1, FORWARD); } else { - steppers.setDir(0, 0); - steppers.setDir(1, 0); + steppers.setDir(0, BACKWARD); + steppers.setDir(1, BACKWARD); } } @@ -28,34 +28,46 @@ void McopyProjector::frame (bool dir) { if (dir != _dir) { setDirection(dir); } - steppers.stepBoth(_stepsPerFrame); + steppers.step(FEED, _stepsPerFrame, _dir ? FORWARD : BACKWARD); _posTakeup += dir ? _stepsPerFrame : -_stepsPerFrame; _posFeed += dir ? _stepsPerFrame : -_stepsPerFrame; } -void McopyProjector::adjust(uint8_t motor, int32_t steps) { +void McopyProjector::adjust(uint8_t motor, int64_t steps) { + uint64_t s = abs(steps); if (steps < 0) { - steppers.setDir(motor, 0); + steppers.setDir(motor, BACKWARD); } else { - steppers.setDir(motor, 1); + steppers.setDir(motor, FORWARD); } - steppers.step(motor, abs(steps)); + steppers.step(motor, s, _dir ? FORWARD : BACKWARD); if (motor == 0) { _posTakeup += steps; } else if (motor == 1) { _posFeed += steps; } + //restore set direction after adjustment + steppers.setDir(motor, _dir ? FORWARD : BACKWARD); } -void McopyProjector::adjustBoth(int32_t steps) { +void McopyProjector::adjustBoth(int64_t steps) { + uint64_t s = abs(steps); if (steps < 0) { - steppers.setDir(0, 0); - steppers.setDir(1, 0); + steppers.setDir(TAKEUP, BACKWARD); + steppers.setDir(FEED, BACKWARD); } else { - steppers.setDir(0, 1); - steppers.setDir(1, 1); + steppers.setDir(TAKEUP, FORWARD); + steppers.setDir(FEED, FORWARD); } - steppers.stepBoth(abs(steps)); + steppers.stepBoth(s); _posTakeup += steps; _posFeed += steps; + + //restore set direction after adjustment + steppers.setDir(TAKEUP, _dir ? FORWARD : BACKWARD); + steppers.setDir(FEED, _dir ? FORWARD : BACKWARD); +} + +void McopyProjector::frames(bool dir, uint64_t count) { + } diff --git a/ino/mcopy_projector_firmware/McopyProjector.h b/ino/mcopy_projector_firmware/McopyProjector.h index 58c8b6d..2ad719f 100644 --- a/ino/mcopy_projector_firmware/McopyProjector.h +++ b/ino/mcopy_projector_firmware/McopyProjector.h @@ -12,12 +12,18 @@ class McopyProjector { uint8_t _motorSteps = 200; uint8_t _frames = 8; - uint8_t _stepsPerFrame = round(_motorSteps / _frames); - uint16_t _speed = 500; + uint8_t _stepsPerFrame = 25; //round(_motorSteps / _frames); + uint16_t _speed = 300; int64_t _posTakeup = 0; int64_t _posFeed = 0; + const uint8_t FORWARD = 1; //CW + const uint8_t BACKWARD = 0; //CCW + + const uint8_t TAKEUP = 0; + const uint8_t FEED = 0; + bool _dir = true; public: @@ -25,11 +31,11 @@ class McopyProjector { McopyProjector(); void begin(); //0 = takeup, 1 = feed - void adjust(uint8_t motor, int32_t steps); - void adjustBoth(int32_t steps); + void adjust(uint8_t motor, int64_t steps); + void adjustBoth(int64_t steps); //true = forward, false = back void frame(bool dir); - void frames(bool dir, uint32_t count); + void frames(bool dir, uint64_t count); void setDirection(bool dir); };