2014-05-25 17:20:00 -06:00
|
|
|
#include "StepperControl.h"
|
2015-06-14 15:08:57 -06:00
|
|
|
#include "TimerOne.h"
|
2014-05-25 17:20:00 -06:00
|
|
|
|
|
|
|
static StepperControl* instance;
|
|
|
|
|
|
|
|
StepperControl * StepperControl::getInstance() {
|
|
|
|
if (!instance) {
|
|
|
|
instance = new StepperControl();
|
|
|
|
};
|
|
|
|
return instance;
|
2014-05-28 17:02:22 -06:00
|
|
|
}
|
|
|
|
;
|
2014-05-25 17:20:00 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
const int MOVEMENT_INTERRUPT_SPEED = 100; // Interrupt cycle in micro seconds
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2015-06-21 15:39:45 -06:00
|
|
|
int test = 0;
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
long sourcePoint[3] = {0,0,0};
|
|
|
|
long currentPoint[3] = {0,0,0};
|
|
|
|
long destinationPoint[3] = {0,0,0};
|
|
|
|
unsigned long movementLength[3] = {0,0,0};
|
|
|
|
unsigned long maxLenth = 0;
|
|
|
|
unsigned long moveTicks[3] = {0,0,0};
|
|
|
|
unsigned long stepOnTick[3] = {0,0,0};
|
|
|
|
unsigned long stepOffTick[3] = {0,0,0};
|
2015-06-15 15:16:33 -06:00
|
|
|
unsigned long axisSpeed[3] = {0,0,0};
|
2015-06-14 15:08:57 -06:00
|
|
|
double lengthRatio[3] = {0,0,0};
|
|
|
|
long speedMax[3] = {0,0,0};
|
|
|
|
long speedMin[3] = {0,0,0};
|
|
|
|
long stepsAcc[3] = {0,0,0};
|
|
|
|
long timeOut[3] = {0,0,0};
|
|
|
|
bool homeIsUp[3] = {false,false,false};
|
|
|
|
bool motorInv[3] = {false,false,false};
|
|
|
|
bool endStInv[3] = {false,false,false};
|
|
|
|
bool homeAxis[3] = {false,false,false};
|
|
|
|
bool axisActive[3] = {false,false,false};
|
2015-06-15 15:16:33 -06:00
|
|
|
bool movementUp[3] = {false,false,false};
|
|
|
|
bool movementToHome[3] = {false,false,false};
|
2015-06-14 15:08:57 -06:00
|
|
|
bool home = false;
|
|
|
|
|
|
|
|
|
|
|
|
// ** test code
|
|
|
|
|
|
|
|
// The interrupt will blink the LED, and keep
|
|
|
|
// track of how many times it has blinked.
|
|
|
|
int ledState = LOW;
|
|
|
|
//volatile unsigned long blinkCount = 0; // use volatile for shared variables
|
|
|
|
|
|
|
|
void blinkLed() {
|
|
|
|
if (ledState == LOW) {
|
|
|
|
ledState = HIGH;
|
2015-06-21 15:39:45 -06:00
|
|
|
//Serial.print("R99 led on");
|
2015-06-14 15:08:57 -06:00
|
|
|
} else {
|
|
|
|
ledState = LOW;
|
2015-06-21 15:39:45 -06:00
|
|
|
//Serial.print("R99 led off");
|
2015-06-14 15:08:57 -06:00
|
|
|
}
|
|
|
|
digitalWrite(LED_PIN, ledState);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-25 17:20:00 -06:00
|
|
|
StepperControl::StepperControl() {
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2014-05-25 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-07-15 16:19:06 -06:00
|
|
|
unsigned long getMaxLength(unsigned long lengths[3]) {
|
|
|
|
unsigned long max = lengths[0];
|
2014-05-25 17:20:00 -06:00
|
|
|
for (int i = 1; i < 3; i++) {
|
|
|
|
if (lengths[i] > max) {
|
|
|
|
max = lengths[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
bool endStopMin(int axis_nr) {
|
|
|
|
|
|
|
|
long stepPin[3] = { X_MIN_PIN,
|
|
|
|
Y_MIN_PIN,
|
|
|
|
Z_MIN_PIN };
|
|
|
|
|
|
|
|
return digitalRead(stepPin[axis_nr]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool endStopMax(int axis_nr) {
|
|
|
|
|
|
|
|
long stepPin[3] = { X_MAX_PIN,
|
|
|
|
Y_MAX_PIN,
|
|
|
|
Z_MAX_PIN };
|
|
|
|
|
|
|
|
return digitalRead(stepPin[axis_nr]);
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:32:37 -06:00
|
|
|
int endStopAxisReached(int axis_nr, bool movement_forward) {
|
|
|
|
|
2014-08-07 15:05:30 -06:00
|
|
|
bool endStInv[3] = { ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_X),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_Y),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_Z) };
|
|
|
|
|
2014-07-25 14:32:37 -06:00
|
|
|
bool min_endstop = false;
|
|
|
|
bool max_endstop = false;
|
2014-08-17 14:15:05 -06:00
|
|
|
bool invert = false;
|
|
|
|
|
|
|
|
if (endStInv[axis_nr]) {
|
|
|
|
invert = true;
|
|
|
|
}
|
2014-07-25 14:32:37 -06:00
|
|
|
|
|
|
|
// for the axis to check, retrieve the end stop status
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
if (!invert) {
|
|
|
|
min_endstop = endStopMin(axis_nr);
|
|
|
|
max_endstop = endStopMax(axis_nr);
|
|
|
|
} else {
|
|
|
|
min_endstop = endStopMax(axis_nr);
|
|
|
|
max_endstop = endStopMin(axis_nr);
|
|
|
|
}
|
|
|
|
/*
|
2014-07-25 14:32:37 -06:00
|
|
|
if (axis_nr == 0) {
|
2014-08-17 14:15:05 -06:00
|
|
|
min_endstop=(digitalRead(X_MIN_PIN) ^ invert);
|
|
|
|
max_endstop=(digitalRead(X_MAX_PIN) ^ invert);
|
2014-07-25 14:32:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (axis_nr == 1) {
|
2014-08-17 14:15:05 -06:00
|
|
|
min_endstop=(digitalRead(Y_MIN_PIN) ^ invert);
|
|
|
|
max_endstop=(digitalRead(Y_MAX_PIN) ^ invert);
|
2014-07-25 14:32:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (axis_nr == 2) {
|
2014-08-17 14:15:05 -06:00
|
|
|
min_endstop=(digitalRead(Z_MIN_PIN) ^ invert);
|
|
|
|
max_endstop=(digitalRead(Z_MAX_PIN) ^ invert);
|
2014-07-25 14:32:37 -06:00
|
|
|
}
|
2014-09-21 14:42:44 -06:00
|
|
|
*/
|
2014-07-25 14:32:37 -06:00
|
|
|
|
|
|
|
// if moving forward, only check the end stop max
|
|
|
|
// for moving backwards, check only the end stop min
|
|
|
|
|
|
|
|
if((!movement_forward && min_endstop) || (movement_forward && max_endstop)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void step(int axis, long ¤tPoint, unsigned long steps,
|
2014-07-15 16:19:06 -06:00
|
|
|
long destinationPoint) {
|
2014-08-17 14:15:05 -06:00
|
|
|
|
2014-05-25 17:20:00 -06:00
|
|
|
if (currentPoint < destinationPoint) {
|
|
|
|
currentPoint += steps;
|
|
|
|
} else if (currentPoint > destinationPoint) {
|
|
|
|
currentPoint -= steps;
|
|
|
|
}
|
2014-08-17 14:15:05 -06:00
|
|
|
|
2014-05-25 17:20:00 -06:00
|
|
|
switch (axis) {
|
|
|
|
case 0:
|
|
|
|
digitalWrite(X_STEP_PIN, HIGH);
|
2014-05-28 17:02:22 -06:00
|
|
|
//digitalWrite(X_STEP_PIN, LOW);
|
2014-05-25 17:20:00 -06:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
digitalWrite(Y_STEP_PIN, HIGH);
|
2014-05-28 17:02:22 -06:00
|
|
|
//digitalWrite(Y_STEP_PIN, LOW);
|
2014-05-25 17:20:00 -06:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
digitalWrite(Z_STEP_PIN, HIGH);
|
2014-05-28 17:02:22 -06:00
|
|
|
//digitalWrite(Z_STEP_PIN, LOW);
|
2014-05-25 17:20:00 -06:00
|
|
|
break;
|
|
|
|
}
|
2014-07-25 14:32:37 -06:00
|
|
|
|
|
|
|
// if the home end stop is reached, set the current position
|
|
|
|
if (endStopAxisReached(axis, false))
|
|
|
|
{
|
|
|
|
currentPoint = 0;
|
|
|
|
}
|
2014-05-25 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
void resetStep(int axis) {
|
|
|
|
switch (axis) {
|
|
|
|
case 0:
|
|
|
|
digitalWrite(X_STEP_PIN, LOW);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
digitalWrite(Y_STEP_PIN, LOW);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
digitalWrite(Z_STEP_PIN, LOW);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-15 16:19:06 -06:00
|
|
|
bool pointReached(long currentPoint[3],
|
|
|
|
long destinationPoint[3]) {
|
2014-05-25 17:20:00 -06:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (destinationPoint[i] != currentPoint[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-02 10:51:01 -06:00
|
|
|
/*
|
2014-05-25 17:20:00 -06:00
|
|
|
unsigned int calculateSpeed(unsigned int ¤tStepsPerSecond,
|
|
|
|
unsigned int maxStepsPerSecond, unsigned int currentSteps,
|
|
|
|
unsigned int currentMillis, unsigned int maxLength,
|
|
|
|
unsigned int &accelerationSteps,
|
|
|
|
unsigned int maxAccelerationStepsPerSecond) {
|
2014-08-02 10:51:01 -06:00
|
|
|
|
2014-05-25 17:20:00 -06:00
|
|
|
if (currentStepsPerSecond < maxStepsPerSecond) {
|
|
|
|
if (currentSteps < maxLength / 2) {
|
2014-08-02 10:51:01 -06:00
|
|
|
accelerationSteps = currentSteps;
|
2014-05-25 17:20:00 -06:00
|
|
|
if (currentMillis % 100 == 0) {
|
|
|
|
currentStepsPerSecond += maxAccelerationStepsPerSecond / 10;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (currentMillis % 100 == 0
|
|
|
|
&& currentStepsPerSecond
|
|
|
|
> maxAccelerationStepsPerSecond / 10) {
|
|
|
|
currentStepsPerSecond -= maxAccelerationStepsPerSecond / 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (currentSteps > maxLength - accelerationSteps
|
|
|
|
&& currentStepsPerSecond > maxAccelerationStepsPerSecond / 10) {
|
|
|
|
if (currentMillis % 100 == 0) {
|
|
|
|
currentStepsPerSecond -= maxAccelerationStepsPerSecond / 10;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentStepsPerSecond = maxStepsPerSecond;
|
|
|
|
}
|
2014-05-28 17:02:22 -06:00
|
|
|
if (currentStepsPerSecond < maxAccelerationStepsPerSecond) {
|
|
|
|
currentStepsPerSecond = maxAccelerationStepsPerSecond;
|
|
|
|
}
|
2014-05-25 17:20:00 -06:00
|
|
|
return currentStepsPerSecond;
|
|
|
|
}
|
2014-08-02 10:51:01 -06:00
|
|
|
*/
|
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
|
|
|
|
unsigned int lastCalcLog = 0;
|
|
|
|
|
2014-08-02 10:51:01 -06:00
|
|
|
unsigned int calculateSpeed(long sourcePosition, long currentPosition, long destinationPosition, long minSpeed, long maxSpeed, long stepsAccDec) {
|
|
|
|
|
|
|
|
int newSpeed = 0;
|
|
|
|
|
|
|
|
long curPos = abs(currentPosition);
|
|
|
|
|
|
|
|
long staPos;
|
|
|
|
long endPos;
|
|
|
|
|
|
|
|
if (abs(sourcePosition) < abs(destinationPosition)) {
|
|
|
|
staPos = abs(sourcePosition);
|
|
|
|
endPos = abs(destinationPosition);;
|
|
|
|
} else {
|
|
|
|
staPos = abs(destinationPosition);;
|
|
|
|
endPos = abs(sourcePosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long halfway = ((endPos - staPos) / 2) + staPos;
|
|
|
|
|
|
|
|
// Set the minimum speed if the position would be out of bounds
|
|
|
|
if (curPos < staPos || curPos > endPos) {
|
|
|
|
newSpeed = minSpeed;
|
|
|
|
} else {
|
|
|
|
if (curPos >= staPos && curPos <= halfway) {
|
|
|
|
// accelerating
|
|
|
|
if (curPos > (staPos + stepsAccDec)) {
|
|
|
|
// now beyond the accelleration point to go full speed
|
|
|
|
newSpeed = maxSpeed + 1;
|
|
|
|
} else {
|
|
|
|
// speeding up, increase speed linear within the first period
|
|
|
|
newSpeed = (1.0 * (curPos - staPos) / stepsAccDec * (maxSpeed - minSpeed)) + minSpeed;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// decelerating
|
|
|
|
if (curPos < (endPos - stepsAccDec)) {
|
|
|
|
// still before the deceleration point so keep going at full speed
|
|
|
|
newSpeed = maxSpeed + 2;
|
|
|
|
} else {
|
|
|
|
// speeding up, increase speed linear within the first period
|
|
|
|
newSpeed = (1.0 * (endPos - curPos) / stepsAccDec * (maxSpeed - minSpeed)) + minSpeed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 09:42:58 -06:00
|
|
|
/*
|
|
|
|
if (millis() - lastCalcLog > 1000) {
|
|
|
|
|
|
|
|
lastCalcLog = millis();
|
|
|
|
|
|
|
|
Serial.print("R99");
|
|
|
|
|
|
|
|
// Serial.print(" a ");
|
|
|
|
// Serial.print(endPos);
|
|
|
|
// Serial.print(" b ");
|
|
|
|
// Serial.print((endPos - stepsAccDec));
|
|
|
|
// Serial.print(" c ");
|
|
|
|
// Serial.print(curPos < (endPos - stepsAccDec));
|
|
|
|
|
|
|
|
|
|
|
|
Serial.print(" sta ");
|
|
|
|
Serial.print(staPos);
|
|
|
|
Serial.print(" cur ");
|
|
|
|
Serial.print(curPos);
|
|
|
|
Serial.print(" end ");
|
|
|
|
Serial.print(endPos);
|
|
|
|
Serial.print(" half ");
|
|
|
|
Serial.print(halfway);
|
|
|
|
Serial.print(" len ");
|
|
|
|
Serial.print(stepsAccDec);
|
|
|
|
Serial.print(" min ");
|
|
|
|
Serial.print(minSpeed);
|
|
|
|
Serial.print(" max ");
|
|
|
|
Serial.print(maxSpeed);
|
|
|
|
Serial.print(" spd ");
|
|
|
|
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(newSpeed);
|
|
|
|
|
|
|
|
Serial.print("\n");
|
2014-08-27 15:11:09 -06:00
|
|
|
}
|
2015-07-15 09:42:58 -06:00
|
|
|
*/
|
2014-08-02 10:51:01 -06:00
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
// Return the calculated speed, in steps per second
|
2014-08-02 10:51:01 -06:00
|
|
|
return newSpeed;
|
|
|
|
}
|
2014-05-25 17:20:00 -06:00
|
|
|
|
|
|
|
void enableMotors(bool enable) {
|
|
|
|
if (enable) {
|
|
|
|
digitalWrite(X_ENABLE_PIN, LOW);
|
|
|
|
digitalWrite(Y_ENABLE_PIN, LOW);
|
|
|
|
digitalWrite(Z_ENABLE_PIN, LOW);
|
2014-07-15 14:23:01 -06:00
|
|
|
delay(100);
|
2014-05-25 17:20:00 -06:00
|
|
|
} else {
|
|
|
|
digitalWrite(X_ENABLE_PIN, HIGH);
|
|
|
|
digitalWrite(Y_ENABLE_PIN, HIGH);
|
|
|
|
digitalWrite(Z_ENABLE_PIN, HIGH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 16:25:24 -06:00
|
|
|
void setDirectionAxis(int dirPin, long currentPoint, long destinationPoint, bool goHome, bool homeIsUp, bool motorInv) {
|
2014-09-04 15:10:47 -06:00
|
|
|
|
|
|
|
if (((!goHome && currentPoint < destinationPoint) || (goHome && homeIsUp)) ^ motorInv) {
|
|
|
|
digitalWrite(dirPin, HIGH);
|
|
|
|
} else {
|
|
|
|
digitalWrite(dirPin, LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDirections(long* currentPoint, long* destinationPoint, bool* homeAxis, bool* motorInv) {
|
2014-08-07 15:05:30 -06:00
|
|
|
|
|
|
|
bool homeIsUp[3] = { ParameterList::getInstance()->getValue(MOVEMENT_HOME_UP_X),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_HOME_UP_Y),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_HOME_UP_Z) };
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
setDirectionAxis(X_DIR_PIN, currentPoint[0], destinationPoint[0], homeAxis[0], homeIsUp[0], motorInv[0]);
|
|
|
|
setDirectionAxis(Y_DIR_PIN, currentPoint[1], destinationPoint[1], homeAxis[1], homeIsUp[1], motorInv[1]);
|
|
|
|
setDirectionAxis(Z_DIR_PIN, currentPoint[2], destinationPoint[2], homeAxis[2], homeIsUp[2], motorInv[2]);
|
2014-05-25 17:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-07-15 16:19:06 -06:00
|
|
|
unsigned long getLength(long l1, long l2) {
|
2014-05-28 17:02:22 -06:00
|
|
|
if(l1 > l2) {
|
|
|
|
return l1 - l2;
|
|
|
|
} else {
|
|
|
|
return l2 - l1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 05:00:03 -06:00
|
|
|
int endStopsReached() {
|
2014-08-07 15:05:30 -06:00
|
|
|
|
|
|
|
bool endStInv[3] = { ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_X),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_Y),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_Z) };
|
|
|
|
|
|
|
|
bool x_min_endstop=(digitalRead(X_MIN_PIN) == endStInv[0]);
|
|
|
|
bool x_max_endstop=(digitalRead(X_MAX_PIN) == endStInv[0]);
|
|
|
|
bool y_min_endstop=(digitalRead(Y_MIN_PIN) == endStInv[1]);
|
|
|
|
bool y_max_endstop=(digitalRead(Y_MAX_PIN) == endStInv[1]);
|
|
|
|
bool z_min_endstop=(digitalRead(Z_MIN_PIN) == endStInv[2]);
|
|
|
|
bool z_max_endstop=(digitalRead(Z_MAX_PIN) == endStInv[2]);
|
2014-07-01 05:00:03 -06:00
|
|
|
if(x_min_endstop || x_max_endstop || y_min_endstop || y_max_endstop || z_min_endstop || z_max_endstop) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-23 01:46:21 -06:00
|
|
|
void reportEndStops() {
|
|
|
|
|
|
|
|
CurrentState::getInstance()->printEndStops();
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:32:37 -06:00
|
|
|
void reportPosition(){
|
|
|
|
CurrentState::getInstance()->printPosition();
|
|
|
|
}
|
|
|
|
|
2014-07-23 01:46:21 -06:00
|
|
|
void storeEndStops() {
|
2014-07-15 14:23:01 -06:00
|
|
|
|
2014-07-24 15:04:19 -06:00
|
|
|
CurrentState::getInstance()->storeEndStops();
|
|
|
|
}
|
2014-07-15 14:23:01 -06:00
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
/**
|
|
|
|
* water is dosed by setting the pin for the water high for a number of miliseconds
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void doseWaterByTime(long time) {
|
|
|
|
digitalWrite(HEATER_1_PIN, HIGH);
|
|
|
|
delay(time);
|
|
|
|
digitalWrite(HEATER_1_PIN, LOW);
|
|
|
|
}
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
void checkAxisDirection(int i) {
|
|
|
|
if (homeAxis[i]){
|
|
|
|
// When home is active, the direction is fixed
|
2015-06-15 15:16:33 -06:00
|
|
|
movementUp[i] = homeIsUp[i];
|
|
|
|
movementToHome[i] = true;
|
2015-07-15 09:42:58 -06:00
|
|
|
} else {
|
|
|
|
// For normal movement, move in direction of destination
|
|
|
|
movementUp[i] = ( currentPoint[i] < destinationPoint[i] );
|
|
|
|
movementToHome[i] = (abs(currentPoint[i]) >= abs(destinationPoint[i]));
|
2015-06-14 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
2015-07-15 09:42:58 -06:00
|
|
|
if (currentPoint[i] == 0) {
|
|
|
|
// Go slow when theoretical end point reached but there is no end stop siganl
|
|
|
|
axisSpeed[i] = speedMin[i];
|
|
|
|
}
|
2015-06-14 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
void stepAxis(int i, bool state) {
|
|
|
|
|
|
|
|
if (homeAxis[i] && currentPoint[i] == 0) {
|
|
|
|
|
|
|
|
// Keep moving toward end stop even when position is zero
|
|
|
|
// but end stop is not yet active
|
|
|
|
if (homeIsUp[i]) {
|
|
|
|
currentPoint[i] = -1;
|
|
|
|
} else {
|
|
|
|
currentPoint[i] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set a step on the motors
|
|
|
|
step(i, currentPoint[i], 1, destinationPoint[i]);
|
2015-06-21 15:39:45 -06:00
|
|
|
blinkLed();
|
2015-06-15 15:16:33 -06:00
|
|
|
//stepMade = true;
|
|
|
|
//lastStepMillis[i] = currentMillis;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
void checkAxis(int i) {
|
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
//moveTicks[i]++;
|
2015-07-15 09:42:58 -06:00
|
|
|
checkAxisDirection(i);
|
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
/*
|
|
|
|
Serial.print("R99 check axis ");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(" axis active ");
|
|
|
|
Serial.print(axisActive[i]);
|
|
|
|
Serial.print(" current point ");
|
|
|
|
Serial.print(currentPoint[i]);
|
|
|
|
Serial.print(" destination point ");
|
|
|
|
Serial.print(destinationPoint[i]);
|
2015-07-15 09:42:58 -06:00
|
|
|
|
|
|
|
Serial.print(" home stop reached ");
|
|
|
|
Serial.print(endStopAxisReached(i, false));
|
|
|
|
Serial.print(" axis stop reached ");
|
|
|
|
Serial.print(endStopAxisReached(i, true));
|
|
|
|
Serial.print(" home axis ");
|
|
|
|
Serial.print(homeAxis[i]);
|
|
|
|
Serial.print(" movement to home ");
|
|
|
|
Serial.print(movementToHome[i]);
|
2015-07-13 15:22:43 -06:00
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
2015-06-21 15:39:45 -06:00
|
|
|
|
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
//if ((!pointReached(currentPoint, destinationPoint) || home) && axisActive[i]) {
|
2015-07-15 09:42:58 -06:00
|
|
|
if (((currentPoint[i] != destinationPoint[i]) || homeAxis[i]) && axisActive[i]) {
|
2015-06-24 14:58:45 -06:00
|
|
|
//Serial.print("R99 point not reached yet\n");
|
2015-06-14 15:08:57 -06:00
|
|
|
// home or destination not reached, keep moving
|
2015-06-24 14:58:45 -06:00
|
|
|
/*
|
|
|
|
Serial.print("R99 calc axis speed");
|
|
|
|
Serial.print(" speed max ");
|
|
|
|
Serial.print(speedMax[i]);
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
// Get the axis speed, in steps per second
|
2015-06-15 15:16:33 -06:00
|
|
|
axisSpeed[i] = calculateSpeed( sourcePoint[i],currentPoint[i],destinationPoint[i],
|
2015-06-14 15:08:57 -06:00
|
|
|
speedMin[i], speedMax[i], stepsAcc[i]);
|
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
//Serial.print("R99 axis speed ");
|
|
|
|
//Serial.print(axisSpeed[i]);
|
|
|
|
//Serial.print("\n");
|
2015-06-21 15:39:45 -06:00
|
|
|
|
2015-07-15 09:42:58 -06:00
|
|
|
/*
|
|
|
|
Serial.print("R99 check axis b ");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(" home part true ");
|
|
|
|
Serial.print(homeAxis[i] && !endStopAxisReached(i, false));
|
|
|
|
Serial.print(" !homeAxis[i] ");
|
|
|
|
Serial.print(!homeAxis[i]);
|
|
|
|
Serial.print(" !endStopAxisReached(i, !movementToHome[i]) ");
|
|
|
|
Serial.print(!endStopAxisReached(i, !movementToHome[i]));
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
2015-06-14 15:08:57 -06:00
|
|
|
// If end stop reached, don't move anymore
|
2015-07-15 09:42:58 -06:00
|
|
|
if ((homeAxis[i] && !endStopAxisReached(i, false)) || (!homeAxis[i] && !endStopAxisReached(i, !movementToHome[i]))) {
|
2015-06-14 15:08:57 -06:00
|
|
|
|
|
|
|
// Set the moments when the step is set to true and false
|
2015-06-24 14:58:45 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
if (axisSpeed[i] > 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Serial.print("R99 calculated steps ");
|
|
|
|
Serial.print(" ** additional ticks steps ");
|
|
|
|
Serial.print(1000.0 * 1000.0 / MOVEMENT_INTERRUPT_SPEED / axisSpeed[i]);
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Take the requested speed (steps / second) and divide by the interrupt speed (interrupts per seconde)
|
|
|
|
// This gives the number of interrupts (called ticks here) before the pulse needs to be set for the next step
|
|
|
|
stepOnTick[i] = moveTicks[i] + (1000.0 * 1000.0 / MOVEMENT_INTERRUPT_SPEED / axisSpeed[i] / 2);
|
|
|
|
stepOffTick[i] = moveTicks[i] + (1000.0 * 1000.0 / MOVEMENT_INTERRUPT_SPEED / axisSpeed[i] );
|
|
|
|
//stepOnTick[i] = moveTicks[i] + (axisSpeed[i] * 1000 / MOVEMENT_INTERRUPT_SPEED / 2);
|
|
|
|
//stepOffTick[i] = moveTicks[i] + (axisSpeed[i] * 1000 / MOVEMENT_INTERRUPT_SPEED );
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
/*
|
2015-07-13 15:22:43 -06:00
|
|
|
Serial.print("R99 calculated steps ");
|
|
|
|
Serial.print(" interrupt speed ");
|
|
|
|
Serial.print(MOVEMENT_INTERRUPT_SPEED);
|
|
|
|
Serial.print(" axisSpeed ");
|
|
|
|
Serial.print(axisSpeed[i]);
|
|
|
|
Serial.print(" moveTicks ");
|
|
|
|
Serial.print(moveTicks[i]);
|
|
|
|
Serial.print(" stepOnTick ");
|
|
|
|
Serial.print(stepOnTick[i]);
|
|
|
|
Serial.print(" stepOffTick ");
|
|
|
|
Serial.print(stepOffTick[i]);
|
|
|
|
Serial.print("\n");
|
2015-06-24 14:58:45 -06:00
|
|
|
*/
|
2015-07-13 15:22:43 -06:00
|
|
|
}
|
2015-06-14 15:08:57 -06:00
|
|
|
}
|
2015-07-15 09:42:58 -06:00
|
|
|
else {
|
|
|
|
axisActive[i] = false;
|
2015-06-14 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Destination or home reached. Deactivate the axis.
|
2015-06-15 15:16:33 -06:00
|
|
|
axisActive[i] = false;
|
2015-06-14 15:08:57 -06:00
|
|
|
}
|
2015-07-15 09:42:58 -06:00
|
|
|
|
|
|
|
// If end stop for home is active, set the position to zero
|
|
|
|
if (endStopAxisReached(i, false)) {
|
|
|
|
currentPoint[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void checkTicksAxis(int i) {
|
|
|
|
|
2015-06-21 15:39:45 -06:00
|
|
|
moveTicks[i]++;
|
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
//Serial.print("R99 checkTicksAxis ");
|
|
|
|
//Serial.print(" destination point ");
|
|
|
|
//Serial.print(destinationPoint[0]);
|
|
|
|
//Serial.print(" moveTicks ");
|
|
|
|
//Serial.print(moveTicks[i]);
|
|
|
|
//Serial.print(" stepOnTick ");
|
|
|
|
//Serial.print(stepOnTick[i]);
|
|
|
|
//Serial.print(" stepOffTick ");
|
|
|
|
//Serial.print(stepOffTick[i]);
|
|
|
|
//Serial.print("\n");
|
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
if (axisActive[i]) {
|
2015-06-24 14:58:45 -06:00
|
|
|
if (moveTicks[i] >= stepOffTick[i]) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.print("R99 reset step ");
|
|
|
|
Serial.print(" moveTicks ");
|
|
|
|
Serial.print(moveTicks[i]);
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
|
|
|
// Negative flank for the steps
|
2015-06-15 15:16:33 -06:00
|
|
|
resetStep(i);
|
2015-06-14 15:08:57 -06:00
|
|
|
checkAxis(i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
if (moveTicks[i] == stepOnTick[i]) {
|
|
|
|
/*
|
|
|
|
Serial.print("R99 set step ");
|
|
|
|
Serial.print(" moveTicks ");
|
|
|
|
Serial.print(moveTicks[i]);
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
|
|
|
// Positive flank for the steps
|
2015-06-14 15:08:57 -06:00
|
|
|
stepAxis(i, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle movement by checking each axis
|
2015-06-21 15:39:45 -06:00
|
|
|
void StepperControl::handleMovementInterrupt(void){
|
2015-06-24 14:58:45 -06:00
|
|
|
//Serial.print("R99 interrupt\n");
|
|
|
|
|
|
|
|
//Serial.print("R99 interrupt data ");
|
|
|
|
//Serial.print(" destination point ");
|
|
|
|
//Serial.print(destinationPoint[0]);
|
|
|
|
//Serial.print(" ");
|
|
|
|
//Serial.print("test ");
|
|
|
|
//Serial.print(test);
|
|
|
|
//Serial.print("\n");
|
2015-06-21 15:39:45 -06:00
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
checkTicksAxis(0);
|
|
|
|
checkTicksAxis(1);
|
|
|
|
checkTicksAxis(2);
|
|
|
|
|
|
|
|
// blinkLed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:39:45 -06:00
|
|
|
bool interruptBusy = false;
|
|
|
|
void handleMovementInterruptTest(void) {
|
|
|
|
if (interruptBusy == false) {
|
|
|
|
interruptBusy = true;
|
2015-06-24 14:58:45 -06:00
|
|
|
StepperControl::getInstance()->handleMovementInterrupt();
|
|
|
|
//blinkLed();
|
2015-06-21 15:39:45 -06:00
|
|
|
interruptBusy = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
// Start the interrupt used for moviing
|
|
|
|
// Interrupt management code library written by Paul Stoffregen
|
|
|
|
void StepperControl::initInterrupt() {
|
2015-06-21 15:39:45 -06:00
|
|
|
//Timer1.attachInterrupt(StepperControl::getInstance()->handleMovementInterrupt);
|
|
|
|
Timer1.attachInterrupt(handleMovementInterruptTest);
|
2015-06-14 15:08:57 -06:00
|
|
|
Timer1.initialize(MOVEMENT_INTERRUPT_SPEED);
|
|
|
|
Timer1.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-25 14:22:52 -06:00
|
|
|
/**
|
|
|
|
* xDest - destination X in steps
|
|
|
|
* yDest - destination Y in steps
|
|
|
|
* zDest - destination Z in steps
|
|
|
|
* maxStepsPerSecond - maximum number of steps per second
|
|
|
|
* maxAccelerationStepsPerSecond - maximum number of acceleration in steps per second
|
|
|
|
*/
|
2014-08-06 16:04:40 -06:00
|
|
|
int StepperControl::moveAbsoluteConstant( long xDest, long yDest, long zDest,
|
|
|
|
unsigned int xMaxSpd, unsigned int yMaxSpd, unsigned int zMaxSpd,
|
|
|
|
bool xHome, bool yHome, bool zHome) {
|
2014-07-15 14:23:01 -06:00
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
|
|
|
|
//Serial.print("R99 ");
|
|
|
|
//Serial.print(" xMaxSpd ");
|
|
|
|
//Serial.print(xMaxSpd);
|
|
|
|
//Serial.print("\n");
|
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
sourcePoint[0] = CurrentState::getInstance()->getX();
|
|
|
|
sourcePoint[1] = CurrentState::getInstance()->getY();
|
|
|
|
sourcePoint[2] = CurrentState::getInstance()->getZ();
|
|
|
|
|
|
|
|
currentPoint[0] = CurrentState::getInstance()->getX();
|
|
|
|
currentPoint[1] = CurrentState::getInstance()->getY();
|
|
|
|
currentPoint[2] = CurrentState::getInstance()->getZ();
|
2014-08-02 10:51:01 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
destinationPoint[0] = xDest;
|
|
|
|
destinationPoint[1] = yDest;
|
|
|
|
destinationPoint[2] = zDest;
|
2014-08-02 10:51:01 -06:00
|
|
|
|
2015-06-21 15:39:45 -06:00
|
|
|
movementLength[0] = getLength(destinationPoint[0], currentPoint[0]);
|
|
|
|
movementLength[1] = getLength(destinationPoint[1], currentPoint[1]);
|
|
|
|
movementLength[2] = getLength(destinationPoint[2], currentPoint[2]);
|
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
maxLenth = getMaxLength(movementLength);
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
lengthRatio[0] = 1.0 * movementLength[0] / maxLenth;
|
|
|
|
lengthRatio[1] = 1.0 * movementLength[1] / maxLenth;
|
|
|
|
lengthRatio[2] = 1.0 * movementLength[2] / maxLenth;
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
homeIsUp[0] = ParameterList::getInstance()->getValue(MOVEMENT_HOME_UP_X);
|
|
|
|
homeIsUp[1] = ParameterList::getInstance()->getValue(MOVEMENT_HOME_UP_Y);
|
|
|
|
homeIsUp[2] = ParameterList::getInstance()->getValue(MOVEMENT_HOME_UP_Z);
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
speedMax[0] = ParameterList::getInstance()->getValue(MOVEMENT_MAX_SPD_X);
|
|
|
|
speedMax[1] = ParameterList::getInstance()->getValue(MOVEMENT_MAX_SPD_Y);
|
|
|
|
speedMax[2] = ParameterList::getInstance()->getValue(MOVEMENT_MAX_SPD_Z);
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
speedMin[0] = ParameterList::getInstance()->getValue(MOVEMENT_MIN_SPD_X);
|
|
|
|
speedMin[1] = ParameterList::getInstance()->getValue(MOVEMENT_MIN_SPD_Y);
|
|
|
|
speedMin[2] = ParameterList::getInstance()->getValue(MOVEMENT_MIN_SPD_Z);
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
stepsAcc[0] = ParameterList::getInstance()->getValue(MOVEMENT_STEPS_ACC_DEC_X);
|
|
|
|
stepsAcc[1] = ParameterList::getInstance()->getValue(MOVEMENT_STEPS_ACC_DEC_Y);
|
|
|
|
stepsAcc[2] = ParameterList::getInstance()->getValue(MOVEMENT_STEPS_ACC_DEC_Z);
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
motorInv[0] = ParameterList::getInstance()->getValue(MOVEMENT_INVERT_MOTOR_X);
|
|
|
|
motorInv[1] = ParameterList::getInstance()->getValue(MOVEMENT_INVERT_MOTOR_Y);
|
|
|
|
motorInv[2] = ParameterList::getInstance()->getValue(MOVEMENT_INVERT_MOTOR_Z);
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
endStInv[0] = ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_X);
|
|
|
|
endStInv[1] = ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_Y);
|
|
|
|
endStInv[2] = ParameterList::getInstance()->getValue(MOVEMENT_INVERT_ENDPOINTS_Z);
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
timeOut[0] = ParameterList::getInstance()->getValue(MOVEMENT_TIMEOUT_X);
|
|
|
|
timeOut[1] = ParameterList::getInstance()->getValue(MOVEMENT_TIMEOUT_X);
|
|
|
|
timeOut[2] = ParameterList::getInstance()->getValue(MOVEMENT_TIMEOUT_X);
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
homeAxis[0] = xHome;
|
|
|
|
homeAxis[1] = yHome;
|
|
|
|
homeAxis[2] = zHome;
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
moveTicks[0] = 0;
|
|
|
|
moveTicks[1] = 0;
|
|
|
|
moveTicks[2] = 0;
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
home = xHome || yHome || zHome;
|
2014-07-23 01:46:21 -06:00
|
|
|
|
2015-06-21 15:39:45 -06:00
|
|
|
//Serial.print("R99 ");
|
|
|
|
//Serial.print(" after vars ");
|
|
|
|
//Serial.print(destinationPoint[0]);
|
|
|
|
//Serial.print("\n");
|
|
|
|
|
|
|
|
|
2014-07-29 16:12:19 -06:00
|
|
|
unsigned long currentMillis = 0;
|
2014-10-12 15:57:07 -06:00
|
|
|
|
2014-07-25 14:32:37 -06:00
|
|
|
unsigned long timeStart = millis();
|
2014-07-15 14:23:01 -06:00
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2014-07-15 14:23:01 -06:00
|
|
|
bool movementDone = false;
|
2014-07-29 16:12:19 -06:00
|
|
|
bool movementUp = false;
|
|
|
|
bool movementToHome = false;
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2014-07-15 14:23:01 -06:00
|
|
|
bool moving = false;
|
2015-06-14 15:08:57 -06:00
|
|
|
|
2014-07-15 14:23:01 -06:00
|
|
|
bool stepMade = false;
|
2014-10-24 16:02:50 -06:00
|
|
|
int incomingByte = 0;
|
2015-07-13 15:22:43 -06:00
|
|
|
//int axisSpeed = 0;
|
2014-07-25 14:32:37 -06:00
|
|
|
int error = 0;
|
|
|
|
|
2014-08-06 16:04:40 -06:00
|
|
|
// if a speed is given in the command, use that instead of the config speed
|
|
|
|
|
|
|
|
if (xMaxSpd > 0 && xMaxSpd < speedMax[0]) {
|
|
|
|
speedMax[0] = xMaxSpd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (yMaxSpd > 0 && yMaxSpd < speedMax[1]) {
|
|
|
|
speedMax[1] = yMaxSpd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zMaxSpd > 0 && zMaxSpd < speedMax[2]) {
|
|
|
|
speedMax[2] = zMaxSpd;
|
2014-07-29 16:12:19 -06:00
|
|
|
}
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
// Prepare for movement
|
2015-06-24 14:58:45 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
setDirections(currentPoint, destinationPoint, homeAxis, motorInv);
|
2015-06-24 14:58:45 -06:00
|
|
|
|
2014-08-06 16:04:40 -06:00
|
|
|
|
2014-07-23 01:46:21 -06:00
|
|
|
storeEndStops();
|
2014-07-15 14:23:01 -06:00
|
|
|
reportEndStops();
|
2014-07-13 15:12:01 -06:00
|
|
|
enableMotors(true);
|
2014-07-29 16:12:19 -06:00
|
|
|
|
|
|
|
// Limit normal movmement to the home position
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (!homeIsUp[i] && destinationPoint[i] < 0) {
|
|
|
|
destinationPoint[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( homeIsUp[i] && destinationPoint[i] > 0) {
|
|
|
|
destinationPoint[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 14:22:52 -06:00
|
|
|
|
2014-08-06 16:04:40 -06:00
|
|
|
// Start movement
|
|
|
|
|
2015-06-15 15:16:33 -06:00
|
|
|
axisActive[0] = true;
|
|
|
|
axisActive[1] = true;
|
|
|
|
axisActive[2] = true;
|
2015-06-14 15:08:57 -06:00
|
|
|
|
|
|
|
checkAxis(0);
|
2015-06-24 14:58:45 -06:00
|
|
|
checkAxis(1);
|
|
|
|
checkAxis(2);
|
2015-06-21 15:39:45 -06:00
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
Timer1.start();
|
|
|
|
|
2015-06-21 15:39:45 -06:00
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
// Let the interrupt handle all the movements
|
2015-06-15 15:16:33 -06:00
|
|
|
while (axisActive[0] || axisActive[1] || axisActive[2]) {
|
2015-06-21 15:39:45 -06:00
|
|
|
/**///Serial.print("R99 while loop\n");
|
2015-07-13 15:22:43 -06:00
|
|
|
//delay(50);
|
|
|
|
delayMicroseconds(100);
|
2014-07-15 14:23:01 -06:00
|
|
|
|
2015-06-24 14:58:45 -06:00
|
|
|
//Serial.print("R99 ");
|
|
|
|
//Serial.print(" maximim speed ");
|
|
|
|
//Serial.print(speedMax[0]);
|
|
|
|
//Serial.print("\n");
|
|
|
|
|
|
|
|
|
2015-07-15 09:42:58 -06:00
|
|
|
checkTicksAxis(0);
|
|
|
|
checkTicksAxis(1);
|
|
|
|
checkTicksAxis(2);
|
2015-06-21 15:39:45 -06:00
|
|
|
|
|
|
|
|
|
|
|
//axisActive[0] = false;
|
2015-06-24 14:58:45 -06:00
|
|
|
//axisActive[1] = false;
|
|
|
|
//axisActive[2] = false;
|
2015-06-21 15:39:45 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
/*
|
|
|
|
Serial.print("R99");
|
|
|
|
Serial.print(" cur ");
|
|
|
|
Serial.print(currentPoint[0]);
|
|
|
|
Serial.print(" end ");
|
|
|
|
Serial.print(destinationPoint[0]);
|
|
|
|
Serial.print(" max spd ");
|
|
|
|
Serial.print(speedMax[0]);
|
|
|
|
Serial.print(" axis spd ");
|
|
|
|
Serial.print(axisSpeed[0]);
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
|
|
|
|
2014-07-23 01:46:21 -06:00
|
|
|
storeEndStops();
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
// Check timeouts
|
2015-07-13 15:22:43 -06:00
|
|
|
if (axisActive[0] == true && ((millis() >= timeStart && millis() - timeStart > timeOut[0] * 1000) || (millis() < timeStart && millis() > timeOut[0] * 1000))) {
|
2015-06-14 15:08:57 -06:00
|
|
|
error = 1;
|
|
|
|
}
|
2015-07-13 15:22:43 -06:00
|
|
|
if (axisActive[1] == true && ((millis() >= timeStart && millis() - timeStart > timeOut[0] * 1000) || (millis() < timeStart && millis() > timeOut[0] * 1000))) {
|
2015-06-14 15:08:57 -06:00
|
|
|
error = 1;
|
|
|
|
}
|
2015-07-13 15:22:43 -06:00
|
|
|
if (axisActive[2] == true && ((millis() >= timeStart && millis() - timeStart > timeOut[0] * 1000) || (millis() < timeStart && millis() > timeOut[0] * 1000))) {
|
2015-06-14 15:08:57 -06:00
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
|
2014-10-24 16:02:50 -06:00
|
|
|
// Check if there is an emergency stop command
|
|
|
|
if (Serial.available() > 0) {
|
|
|
|
incomingByte = Serial.read();
|
|
|
|
if (incomingByte == 69 || incomingByte == 101) {
|
2015-06-14 15:08:57 -06:00
|
|
|
error = 1;
|
|
|
|
}
|
2014-10-24 16:02:50 -06:00
|
|
|
}
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
if (error == 1) {
|
2015-06-21 15:39:45 -06:00
|
|
|
Serial.print("R99 error\n");
|
2015-06-14 15:08:57 -06:00
|
|
|
Timer1.stop();
|
2015-06-15 15:16:33 -06:00
|
|
|
axisActive[0] = false;
|
|
|
|
axisActive[1] = false;
|
|
|
|
axisActive[2] = false;
|
2014-06-25 14:22:52 -06:00
|
|
|
}
|
2014-10-12 15:57:07 -06:00
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
// Periodically send message still active
|
2014-06-25 14:22:52 -06:00
|
|
|
currentMillis++;
|
2015-06-14 15:08:57 -06:00
|
|
|
if (currentMillis % 2500 == 0)
|
2014-10-12 15:57:07 -06:00
|
|
|
{
|
|
|
|
Serial.print("R04\n");
|
|
|
|
}
|
|
|
|
|
2014-06-25 14:22:52 -06:00
|
|
|
}
|
|
|
|
|
2015-06-21 15:39:45 -06:00
|
|
|
/**/Serial.print("R99 stopped\n");
|
|
|
|
|
2015-06-14 15:08:57 -06:00
|
|
|
Timer1.stop();
|
2014-08-17 14:15:05 -06:00
|
|
|
enableMotors(false);
|
2014-07-23 01:46:21 -06:00
|
|
|
|
2014-05-28 17:02:22 -06:00
|
|
|
CurrentState::getInstance()->setX(currentPoint[0]);
|
|
|
|
CurrentState::getInstance()->setY(currentPoint[1]);
|
|
|
|
CurrentState::getInstance()->setZ(currentPoint[2]);
|
2014-07-23 01:46:21 -06:00
|
|
|
|
|
|
|
storeEndStops();
|
|
|
|
reportEndStops();
|
2014-07-25 14:32:37 -06:00
|
|
|
reportPosition();
|
2014-08-27 15:11:09 -06:00
|
|
|
|
2014-07-25 14:32:37 -06:00
|
|
|
return error;
|
2014-05-25 17:20:00 -06:00
|
|
|
}
|
2014-09-04 15:10:47 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Calibration
|
|
|
|
//
|
|
|
|
|
|
|
|
int StepperControl::calibrateAxis(int axis) {
|
|
|
|
|
|
|
|
|
2014-09-22 14:35:01 -06:00
|
|
|
int parMotInv[3] = { 31, 32, 33};
|
|
|
|
int parEndInv[3] = { 21, 22, 23};
|
2014-10-09 15:35:26 -06:00
|
|
|
int parNbrStp[3] = {801,802,803};
|
2014-09-22 14:35:01 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
bool motorInv[3] = { ParameterList::getInstance()->getValue(MOVEMENT_INVERT_MOTOR_X),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_INVERT_MOTOR_Y),
|
|
|
|
ParameterList::getInstance()->getValue(MOVEMENT_INVERT_MOTOR_Z) };
|
|
|
|
|
|
|
|
bool homeAxis[3] = {false,false,false};
|
|
|
|
|
|
|
|
bool homeIsUp[3] = {false,false,false};
|
|
|
|
|
|
|
|
long speedMin[3] = {200,200,200};
|
|
|
|
|
|
|
|
bool endStInv[3] = {false,false,false};
|
|
|
|
|
|
|
|
long timeOut[3] = {5000,5000,5000};
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
long stepPin[3] = { X_STEP_PIN,
|
|
|
|
Y_STEP_PIN,
|
|
|
|
Z_STEP_PIN };
|
|
|
|
|
|
|
|
long dirPin[3] = { X_DIR_PIN,
|
|
|
|
Y_DIR_PIN,
|
|
|
|
Z_DIR_PIN };
|
|
|
|
|
|
|
|
// Set the coordinate variables for homing, so the default functions can be used for settign direction
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
long sourcePoint[3] = { 0, 0, 0 };
|
|
|
|
long destinationPoint[3] = { 0, 0, 0 };
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
unsigned long currentMillis = 0;
|
|
|
|
|
|
|
|
unsigned long currentSteps = 0;
|
|
|
|
unsigned long lastStepMillis = 0;
|
|
|
|
|
|
|
|
unsigned long timeStart = millis();
|
|
|
|
|
|
|
|
bool movementDone = false;
|
|
|
|
|
2014-09-22 14:35:01 -06:00
|
|
|
int paramValueInt = 0;
|
2014-09-21 14:42:44 -06:00
|
|
|
bool invertEndStops = false;
|
|
|
|
int stepsCount = 0;
|
2015-06-24 14:58:45 -06:00
|
|
|
int incomingByte = 0;
|
|
|
|
int error = 0;
|
2014-09-04 15:10:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
// Prepare for movement
|
|
|
|
|
|
|
|
storeEndStops();
|
|
|
|
reportEndStops();
|
2014-09-21 14:42:44 -06:00
|
|
|
|
|
|
|
// Preliminary checks
|
|
|
|
|
|
|
|
if (endStopMin(axis) || endStopMax(axis)) {
|
2014-10-12 15:57:07 -06:00
|
|
|
Serial.print("R99 Calibration error: end stop active before start\n");
|
2014-09-21 14:42:44 -06:00
|
|
|
return 1;
|
|
|
|
}
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-10-12 15:57:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.print("R99");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("calibration start");
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
// Move towards home
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
enableMotors(true);
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
movementDone = false;
|
2014-09-21 14:42:44 -06:00
|
|
|
setDirectionAxis(dirPin[0], 0, -1, homeAxis[axis], homeIsUp[axis], motorInv[axis]);
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
while (!movementDone && error == 0) {
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-10-24 16:02:50 -06:00
|
|
|
// Check if there is an emergency stop command
|
|
|
|
if (Serial.available() > 0) {
|
|
|
|
incomingByte = Serial.read();
|
|
|
|
if (incomingByte == 69 || incomingByte == 101) {
|
|
|
|
movementDone = true;
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
// Move until the end stop for home position is reached
|
2014-10-24 16:02:50 -06:00
|
|
|
if ((!endStopMin(axis) && !endStopMax(axis)) && !movementDone) {
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2015-07-13 15:22:43 -06:00
|
|
|
if ((millis() >= timeStart && millis() - timeStart > timeOut[axis] * 1000) || (millis() < timeStart && millis() > timeOut[axis] * 1000)) {
|
2014-09-04 15:10:47 -06:00
|
|
|
movementDone = true;
|
|
|
|
error = 1;
|
|
|
|
} else {
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
// Only do a step every x milliseconds (x is calculated above)
|
|
|
|
if (currentMillis - lastStepMillis >= MOVEMENT_SPEED_BASE_TIME / speedMin[axis]) {
|
|
|
|
digitalWrite(X_STEP_PIN, HIGH);
|
2014-10-12 15:57:07 -06:00
|
|
|
lastStepMillis = currentMillis;
|
2014-09-04 15:10:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 15:57:07 -06:00
|
|
|
delayMicroseconds(MOVEMENT_DELAY);
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
currentMillis++;
|
2014-10-12 15:57:07 -06:00
|
|
|
if (currentMillis % 10000 == 0)
|
|
|
|
{
|
|
|
|
// Periodically send message still active
|
|
|
|
Serial.print("R04\n");
|
|
|
|
}
|
|
|
|
|
2014-09-04 15:10:47 -06:00
|
|
|
digitalWrite(X_STEP_PIN, LOW);
|
2014-10-12 15:57:07 -06:00
|
|
|
delayMicroseconds(MOVEMENT_DELAY);
|
2014-09-04 15:10:47 -06:00
|
|
|
|
|
|
|
} else {
|
|
|
|
movementDone = true;
|
2014-09-21 14:42:44 -06:00
|
|
|
|
|
|
|
// If end stop for home is active, set the position to zero
|
|
|
|
if (endStopMax(axis))
|
|
|
|
{
|
|
|
|
invertEndStops = true;
|
|
|
|
}
|
2014-09-04 15:10:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 15:57:07 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.print("R99");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("calibration halfway");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("end stop invert");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(invertEndStops);
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("error");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(error);
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-09-22 14:35:01 -06:00
|
|
|
// Report back the end stop setting
|
|
|
|
|
|
|
|
if (error == 0) {
|
|
|
|
if (invertEndStops) {
|
|
|
|
paramValueInt = 1;
|
|
|
|
} else {
|
|
|
|
paramValueInt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.print("R23");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("P");
|
|
|
|
Serial.print(parEndInv[axis]);
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("V");
|
2014-10-09 15:35:26 -06:00
|
|
|
Serial.print(paramValueInt);
|
|
|
|
Serial.print("\n");
|
2014-09-22 14:35:01 -06:00
|
|
|
}
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-22 14:35:01 -06:00
|
|
|
// Store the status of the system
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
storeEndStops();
|
|
|
|
reportEndStops();
|
2014-09-22 14:35:01 -06:00
|
|
|
//reportPosition();
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
// Move into the other direction now, and measure the number of steps
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-10-12 15:57:07 -06:00
|
|
|
stepsCount = 0;
|
2014-09-21 14:42:44 -06:00
|
|
|
movementDone = false;
|
|
|
|
setDirectionAxis(dirPin[0], 0, 1, homeAxis[axis], homeIsUp[axis], motorInv[axis]);
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
while (!movementDone && error == 0) {
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-10-24 16:02:50 -06:00
|
|
|
// Check if there is an emergency stop command
|
|
|
|
if (Serial.available() > 0) {
|
|
|
|
incomingByte = Serial.read();
|
|
|
|
if (incomingByte == 69 || incomingByte == 101) {
|
|
|
|
movementDone = true;
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
// Move until the end stop at the other side of the axis is reached
|
2014-10-24 16:02:50 -06:00
|
|
|
if (((!invertEndStops && !endStopMax(axis)) || (invertEndStops && !endStopMin(axis))) && !movementDone) {
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
if (millis() - timeStart > timeOut[axis] * MOVEMENT_SPEED_BASE_TIME) {
|
|
|
|
movementDone = true;
|
|
|
|
error = 1;
|
|
|
|
} else {
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
// Only do a step every x milliseconds (x is calculated above)
|
|
|
|
if (currentMillis - lastStepMillis >= MOVEMENT_SPEED_BASE_TIME / speedMin[axis]) {
|
|
|
|
digitalWrite(X_STEP_PIN, HIGH);
|
|
|
|
stepsCount++;
|
2014-10-12 15:57:07 -06:00
|
|
|
lastStepMillis = currentMillis;
|
2014-09-21 14:42:44 -06:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-10-12 15:57:07 -06:00
|
|
|
delayMicroseconds(MOVEMENT_DELAY);
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
currentMillis++;
|
2014-10-12 15:57:07 -06:00
|
|
|
if (currentMillis % 10000 == 0)
|
|
|
|
{
|
|
|
|
// Periodically send message still active
|
|
|
|
Serial.print("R04\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
digitalWrite(X_STEP_PIN, LOW);
|
2014-10-12 15:57:07 -06:00
|
|
|
delayMicroseconds(MOVEMENT_DELAY);
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-21 14:42:44 -06:00
|
|
|
} else {
|
2014-09-04 15:10:47 -06:00
|
|
|
movementDone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 15:57:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.print("R99");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("calibration done");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("nr steps");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(stepsCount);
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("error");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(error);
|
|
|
|
Serial.print("\n");
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-22 14:35:01 -06:00
|
|
|
// Report back the end stop setting
|
|
|
|
|
|
|
|
if (error == 0) {
|
|
|
|
Serial.print("R23");
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("P");
|
|
|
|
Serial.print(parNbrStp[axis]);
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print("V");
|
2014-10-12 15:57:07 -06:00
|
|
|
Serial.print(stepsCount);
|
2014-10-09 15:35:26 -06:00
|
|
|
Serial.print("\n");
|
2014-09-22 14:35:01 -06:00
|
|
|
}
|
2014-09-04 15:10:47 -06:00
|
|
|
|
2014-09-22 14:35:01 -06:00
|
|
|
enableMotors(false);
|
2014-09-04 15:10:47 -06:00
|
|
|
|
|
|
|
storeEndStops();
|
|
|
|
reportEndStops();
|
2014-10-12 15:57:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
switch (axis) {
|
|
|
|
case 0:
|
|
|
|
CurrentState::getInstance()->setX(stepsCount);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
CurrentState::getInstance()->setY(stepsCount);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
CurrentState::getInstance()->setZ(stepsCount);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
reportPosition();
|
2014-09-04 15:10:47 -06:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2015-06-14 15:08:57 -06:00
|
|
|
|