Rewrite mcopy_projector_firmware with AccelStepper, replacing the custom Itead firmware (not working)

This commit is contained in:
Matt McWilliams 2023-08-29 15:33:51 -04:00
parent 24452794a9
commit dcbb57f732
5 changed files with 68 additions and 170 deletions

View File

@ -1,81 +0,0 @@
#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();
}
}

View File

@ -1,41 +0,0 @@
#ifndef IteadDualStepperShield_h
#define IteadDualStepperShield_h
#include <Arduino.h>
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

View File

@ -1,73 +1,82 @@
/// Mcopy Projector Class
#include "McopyProjector.h"
#include "IteadDualStepperShield.h"
McopyProjector::McopyProjector () {
McopyProjector::McopyProjector (AccelStepper takeup, AccelStepper feed) {
_takeup = takeup;
_feed = feed;
}
void McopyProjector::begin () {
steppers.setup();
steppers.setSpeed(0, _speed);
steppers.setSpeed(1, _speed);
_takeup.setMaxSpeed(_speed);
_takeup.setSpeed(_speed);
_takeup.setAcceleration(1000.0);
_feed.setMaxSpeed(_speed);
_feed.setSpeed(_speed);
_feed.setAcceleration(1000.0);
}
void McopyProjector::setDirection (bool dir) {
_dir = dir;
if (_dir) {
steppers.setDir(0, FORWARD);
steppers.setDir(1, FORWARD);
} else {
steppers.setDir(0, BACKWARD);
steppers.setDir(1, BACKWARD);
}
}
void McopyProjector::frame (bool dir) {
if (dir != _dir) {
setDirection(dir);
}
steppers.step(FEED, _stepsPerFrame, _dir ? FORWARD : BACKWARD);
_posTakeup += dir ? _stepsPerFrame : -_stepsPerFrame;
_posFeed += dir ? _stepsPerFrame : -_stepsPerFrame;
int64_t takeupGoal = _takeup.currentPosition();
int64_t feedGoal = _feed.currentPosition();
takeupGoal += _dir ? _stepsPerFrame : -_stepsPerFrame;
feedGoal += _dir ? _stepsPerFrame : -_stepsPerFrame;
_takeup.moveTo(takeupGoal);
_feed.moveTo(feedGoal);
_running = true;
}
void McopyProjector::adjust(uint8_t motor, int64_t steps) {
uint64_t s = abs(steps);
if (steps < 0) {
steppers.setDir(motor, BACKWARD);
} else {
steppers.setDir(motor, FORWARD);
}
steppers.step(motor, s, _dir ? FORWARD : BACKWARD);
//moveTo
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(int64_t steps) {
void McopyProjector::adjustBoth (int64_t steps) {
uint64_t s = abs(steps);
if (steps < 0) {
steppers.setDir(TAKEUP, BACKWARD);
steppers.setDir(FEED, BACKWARD);
} else {
steppers.setDir(TAKEUP, FORWARD);
steppers.setDir(FEED, FORWARD);
}
steppers.stepBoth(s);
//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) {
void McopyProjector::loop () {
if (_running) {
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
//frame done
_running = false;
_posTakeup += _dir ? _stepsPerFrame : -_stepsPerFrame;
_posFeed += _dir ? _stepsPerFrame : -_stepsPerFrame;
} else {
_takeup.run();
_feed.run();
}
} else if (_adjusting) {
if (_takeup.distanceToGo() == 0 && _feed.distanceToGo() == 0) {
//adjustment done
_adjusting = false;
} else {
_takeup.run();
_feed.run();
}
}
}

View File

@ -2,18 +2,19 @@
#define MCOPY_PROJECTOR
#include <Arduino.h>
#include "IteadDualStepperShield.h"
#include <AccelStepper.h>
class McopyProjector {
private:
IteadDualStepperShield steppers;
AccelStepper _takeup;
AccelStepper _feed;
uint8_t _motorSteps = 200;
uint8_t _motorSteps = 1600; //microstepped
uint8_t _frames = 8;
uint8_t _stepsPerFrame = 25; //round(_motorSteps / _frames);
uint16_t _speed = 300;
float _speed = 500.0;
int64_t _posTakeup = 0;
int64_t _posFeed = 0;
@ -26,17 +27,20 @@ class McopyProjector {
bool _dir = true;
bool _running = false;
bool _adjusting = false;
public:
McopyProjector();
McopyProjector(AccelStepper takeup, AccelStepper feed);
void begin();
//0 = takeup, 1 = feed
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, uint64_t count);
void setDirection(bool dir);
void loop();
};
#endif

View File

@ -23,8 +23,16 @@
*/
#include "McopyProjector.h"
#include "McopySerial.h"
#include "IteadDualStepperShield.h"
#include <AccelStepper.h>
#define TAKEUP_DIR_PIN 3
#define TAKEUP_STEP_PIN 2
#define FEED_DIR_PIN 7
#define FEED_STEP_PIN 6
AccelStepper takeup(AccelStepper::DRIVER, TAKEUP_STEP_PIN, TAKEUP_DIR_PIN);
AccelStepper feed(AccelStepper::DRIVER, FEED_STEP_PIN, FEED_DIR_PIN);
//CAMERA CONSTANTS
const int BUTTON = 7;
@ -42,11 +50,9 @@ volatile bool direction = true;
volatile long start;
McopySerial mcopy;
McopyProjector projector;
McopyProjector projector(takeup, feed);
void setup () {
pins();
digitalWrite(LED_FWD, HIGH);
digitalWrite(LED_BWD, HIGH);
@ -64,6 +70,7 @@ void loop () {
if (digitalRead(BUTTON) == LOW) {
projector_frame();
}
projector.loop();
}
void pins () {