diff --git a/examples/dual_steppers/IteadDualStepperShield.cpp b/examples/dual_steppers/IteadDualStepperShield.cpp new file mode 100644 index 0000000..7e9bfd9 --- /dev/null +++ b/examples/dual_steppers/IteadDualStepperShield.cpp @@ -0,0 +1,81 @@ + +#include "IteadDualStepperShield.h" + +IteadDualStepperShield::IteadDualStepperShield () {} + +void IteadDualStepperShield::setup () { + pinMode(_directionA, OUTPUT); + pinMode(_directionB, OUTPUT); + pinMode(_stepA, OUTPUT); + pinMode(_stepB, OUTPUT); + setDir(0, 1); + setDir(1, 1); +} + +void IteadDualStepperShield::setDir (uint8_t motor, uint8_t dir) { + if (motor == 0) { + directionA = dir; + digitalWrite(_directionA, directionA > 0 ? HIGH : LOW); + } else if (motor == 1) { + directionB = dir; + digitalWrite(_directionB, directionB > 0 ? HIGH : LOW); + } +} + +void IteadDualStepperShield::setSpeed (uint8_t motor, uint16_t rpm) { + uint32_t usPerStep = 60000000 / ((uint32_t) revsteps * (uint32_t) rpm); + if (motor == 0) { + _usStepA = usPerStep; + } else if (motor == 1) { + _usStepB = usPerStep; + } +} + +void IteadDualStepperShield::_micro (uint8_t motor) { + uint8_t stepPin = motor == 0 ? _stepA : _stepB; + uint32_t usStep = motor == 0 ? _usStepA : _usStepB; + digitalWrite(stepPin, HIGH); + delayMicroseconds(usStep); + digitalWrite(stepPin, LOW); + delayMicroseconds(usStep); +} + +//full +void IteadDualStepperShield::_single (uint8_t motor) { + uint8_t stepPin = motor == 0 ? _stepA : _stepB; + uint32_t usStep = motor == 0 ? _usStepA : _usStepB; + for (uint8_t i = 0; i < _microsteps; i++) { + digitalWrite(stepPin, HIGH); + delayMicroseconds(usStep); + digitalWrite(stepPin, LOW); + delayMicroseconds(usStep); + } +} +void IteadDualStepperShield::_both () { + for (uint8_t i = 0; i < _microsteps; i++) { + digitalWrite(_stepA, HIGH); + digitalWrite(_stepB, HIGH); + delayMicroseconds(_usStepA); + digitalWrite(_stepA, LOW); + digitalWrite(_stepB, LOW); + delayMicroseconds(_usStepA); + } +} +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++) { + _single(stepPin); + } +} +void IteadDualStepperShield::onestep (uint8_t motor, uint8_t dir) { + uint8_t stepPin = motor == 0 ? _stepA : _stepB; + setDir(motor, dir); + _single(stepPin); +} + +void IteadDualStepperShield::stepBoth (uint64_t steps) { + for (int i = 0; i < steps; i++) { + _both(); + } +} \ No newline at end of file diff --git a/examples/dual_steppers/IteadDualStepperShield.h b/examples/dual_steppers/IteadDualStepperShield.h new file mode 100644 index 0000000..85f2f7b --- /dev/null +++ b/examples/dual_steppers/IteadDualStepperShield.h @@ -0,0 +1,41 @@ +#ifndef IteadDualStepperShield_h +#define IteadDualStepperShield_h + +#include + +class IteadDualStepperShield { + private: + const uint8_t _microsteps = 8; //8 or 16 + const uint8_t _directionA = 3; + const uint8_t _directionB = 7; + const uint8_t _stepA = 2; + const uint8_t _stepB = 6; + + void _single(uint8_t motor); + void _micro(uint8_t motor); + void _both(); + + uint32_t _usStepA = 300; + uint32_t _usStepB = 300; + + public: + IteadDualStepperShield(); + + uint16_t revsteps = 200; // # steps per revolution + + volatile uint8_t directionA = 1; + volatile uint8_t directionB = 1; + void setup(); + + void setDir(uint8_t motor, uint8_t dir); + void setSpeed(uint8_t motor, uint16_t speed); + + //full + 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(); +}; + +#endif \ No newline at end of file