- Added endstop support

- Movement changed to straight line (nearly straight - division rest is
not handled)
- Added G28 - home all axis
- Negative axis values are changed to 0
pull/1/head
mateo 2014-07-01 13:00:03 +02:00
parent 21d6fe20d7
commit deed0bf626
11 changed files with 114 additions and 76 deletions

View File

@ -33,21 +33,23 @@ CommandCodeEnum Command::getGCodeEnum(char* code) {
if (strcmp(code, "G1") == 0 || strcmp(code, "G01") == 0) {
return G01;
}
if (strcmp(code, "G2") == 0 || strcmp(code, "G02") == 0) {
return G02;
}
if (strcmp(code, "G3") == 0 || strcmp(code, "G03") == 0) {
return G03;
}
if (strcmp(code, "G4") == 0 || strcmp(code, "G04") == 0) {
return G04;
if (strcmp(code, "G28") == 0) {
return G28;
}
return CODE_UNDEFINED;
}
double minusNotAllowed(double value) {
if(value < 0) {
return 0;
}
return value;
}
void Command::getParameter(char* charPointer) {
if (charPointer[0] == axisCodes[0]) {
axisValue[0] = atof(charPointer + 1);
axisValue[0] = minusNotAllowed(axisValue[0]);
} else if (charPointer[0] == axisCodes[1]) {
axisValue[1] = atof(charPointer + 1);
} else if (charPointer[0] == axisCodes[2]) {

View File

@ -9,9 +9,7 @@ enum CommandCodeEnum
CODE_UNDEFINED = -1,
G00 = 0,
G01,
G02,
G03,
G04
G28
};
#define NULL 0

View File

@ -12,5 +12,7 @@ const int LOGGING = 0;
const unsigned int MAX_STEPS_PER_SECOND = 100;
const unsigned int MAX_ACCELERATION_STEPS_PER_SECOND = 2;
const unsigned int HOME_MOVEMENT_SPEED_S_P_S = 200;
const unsigned int INVERT_ENDSTOPS = 1;
#endif /* CONFIG_H_ */

View File

@ -21,7 +21,7 @@ G00Handler * G00Handler::getInstance() {
G00Handler::G00Handler() {
}
int GCodeHandler::execute(Command* command) {
int G00Handler::execute(Command* command) {
StepperControl::getInstance()->moveAbsoluteConstant(command->getX(),
command->getY(), command->getZ(), command->getS());
if (LOGGING) {

View File

@ -17,6 +17,7 @@
class G00Handler : public GCodeHandler {
public:
static G00Handler* getInstance();
int execute(Command*);
private:
G00Handler();
G00Handler(G00Handler const&);

30
src/G28Handler.cpp 100644
View File

@ -0,0 +1,30 @@
/*
* G00Handler.cpp
*
* Created on: 15 maj 2014
* Author: MattLech
*/
#include "G28Handler.h"
static G28Handler* instance;
G28Handler * G28Handler::getInstance() {
if (!instance) {
instance = new G28Handler();
};
return instance;
}
;
G28Handler::G28Handler() {
}
int G28Handler::execute(Command* command) {
StepperControl::getInstance()->moveAbsoluteConstant(0,0,0,HOME_MOVEMENT_SPEED_S_P_S);
if (LOGGING) {
CurrentState::getInstance()->print();
}
return 0;
}

29
src/G28Handler.h 100644
View File

@ -0,0 +1,29 @@
/*
* G28Handler.h
*
* Created on: 15 maj 2014
* Author: MattLech
*/
#ifndef G28HANDLER_H_
#define G28HANDLER_H_
#include "GCodeHandler.h"
#include "Config.h"
#include "CurrentState.h"
#include "pins.h"
#include "Config.h"
#include "StepperControl.h"
class G28Handler : public GCodeHandler {
public:
static G28Handler* getInstance();
int execute(Command*);
private:
G28Handler();
G28Handler(G28Handler const&);
void operator=(G28Handler const&);
long adjustStepAmount(long);
long getNumberOfSteps(double, double);
};
#endif /* G28HANDLER_H_ */

View File

@ -14,3 +14,6 @@ GCodeHandler::GCodeHandler() {
GCodeHandler::~GCodeHandler() {
}
int GCodeHandler::execute(Command*) {
return -1;
}

View File

@ -33,13 +33,20 @@ int GCodeProcessor::execute(Command* command) {
Serial.println("This is false: handler == NULL");
return -1;
}
return handler->execute(command);
Serial.println("R01");
int execution = handler->execute(command);
if(execution == 0) {
Serial.println("R02");
}
return execution;
};
GCodeHandler* GCodeProcessor::getGCodeHandler(CommandCodeEnum codeEnum) {
switch(codeEnum) {
case G00:
return G00Handler::getInstance();
case G28:
return G28Handler::getInstance();
}
return NULL;
}

View File

@ -9,6 +9,7 @@
#define GCODEPROCESSOR_H_
#include "GCodeHandler.h"
#include "G00Handler.h"
#include "G28Handler.h"
#include "Command.h"
#include "Config.h"

View File

@ -48,14 +48,6 @@ void step(int axis, unsigned int &currentPoint, unsigned int steps,
bool pointReached(unsigned int currentPoint[3],
unsigned int destinationPoint[3]) {
if (LOGGING) {
Serial.print("Current point:");
Serial.print(currentPoint[0]);
Serial.print(", ");
Serial.print(currentPoint[1]);
Serial.print(", ");
Serial.println(currentPoint[2]);
}
for (int i = 0; i < 3; i++) {
if (destinationPoint[i] != currentPoint[i]) {
return false;
@ -241,6 +233,31 @@ int StepperControl::moveAbsolute(unsigned int xDest, unsigned int yDest,
return 0;
}
int endStopsReached() {
bool x_min_endstop=(digitalRead(X_MIN_PIN) == INVERT_ENDSTOPS);
bool x_max_endstop=(digitalRead(X_MAX_PIN) == INVERT_ENDSTOPS);
bool y_min_endstop=(digitalRead(Y_MIN_PIN) == INVERT_ENDSTOPS);
bool y_max_endstop=(digitalRead(Y_MAX_PIN) == INVERT_ENDSTOPS);
bool z_min_endstop=(digitalRead(Z_MIN_PIN) == INVERT_ENDSTOPS);
bool z_max_endstop=(digitalRead(Z_MAX_PIN) == INVERT_ENDSTOPS);
if(x_min_endstop || x_max_endstop || y_min_endstop || y_max_endstop || z_min_endstop || z_max_endstop) {
Serial.print("R03 ");
Serial.print(x_min_endstop);
Serial.print(" ");
Serial.print(x_max_endstop);
Serial.print(" ");
Serial.print(y_min_endstop);
Serial.print(" ");
Serial.print(y_max_endstop);
Serial.print(" ");
Serial.print(z_min_endstop);
Serial.print(" ");
Serial.println(z_max_endstop);
return 1;
}
return 0;
}
/**
* xDest - destination X in steps
* yDest - destination Y in steps
@ -255,50 +272,12 @@ int StepperControl::moveAbsoluteConstant(unsigned int xDest, unsigned int yDest,
CurrentState::getInstance()->getZ() };
unsigned int destinationPoint[3] = { xDest, yDest, zDest };
Serial.print("Destination point:");
Serial.print(destinationPoint[0]);
Serial.print(", ");
Serial.print(destinationPoint[1]);
Serial.print(", ");
Serial.println(destinationPoint[2]);
Serial.print("Current point:");
Serial.print(currentPoint[0]);
Serial.print(", ");
Serial.print(currentPoint[1]);
Serial.print(", ");
Serial.println(currentPoint[2]);
unsigned int movementLength[3] = { getLength(destinationPoint[0], currentPoint[0]),
getLength(destinationPoint[1], currentPoint[1]),
getLength(destinationPoint[2], currentPoint[2])};
unsigned int maxLenth = getMaxLength(movementLength);
double lengthRatio[3] = { 1.0 * movementLength[0] / maxLenth, 1.0
* movementLength[1] / maxLenth, 1.0 * movementLength[2] / maxLenth };
Serial.print("Max length:");
Serial.print(maxLenth);
Serial.print("Length ratio:");
Serial.print(lengthRatio[0]);
Serial.print(", ");
Serial.print(lengthRatio[1]);
Serial.print(", ");
Serial.println(lengthRatio[2]);
Serial.print(", step per= ");
double sp = 1000 / (1.0*maxStepsPerSecond * lengthRatio[0] - 1);
Serial.print(sp);
Serial.print(", ");
sp = 1000 / (1.0*maxStepsPerSecond * lengthRatio[1] - 1);
Serial.print(sp);
Serial.print(", ");
sp = 1000 / (1.0*maxStepsPerSecond * lengthRatio[2] - 1);
Serial.println(sp);
Serial.print("MaxStepsPerSec==== ");
Serial.println(maxStepsPerSecond);
Serial.print("Calculation==== ");
sp = (1.0*maxStepsPerSecond * lengthRatio[0] - 1);
Serial.println(sp);
unsigned int currentMillis = 0;
unsigned int currentStepsPerSecond = maxStepsPerSecond;
@ -310,6 +289,9 @@ int StepperControl::moveAbsoluteConstant(unsigned int xDest, unsigned int yDest,
setDirections(currentPoint, destinationPoint);
while (!pointReached(currentPoint, destinationPoint)) {
if(endStopsReached()) {
return -1;
}
bool stepMade = false;
for (int i = 0; i < 3; i++) {
if (currentPoint[i] != destinationPoint[i] && currentMillis - lastStepMillis[i]
@ -322,19 +304,8 @@ int StepperControl::moveAbsoluteConstant(unsigned int xDest, unsigned int yDest,
delayMicroseconds(500);
if (stepMade) {
currentSteps++;
if (LOGGING) {
Serial.print("Step made:");
Serial.print(", Current step= ");
Serial.print(currentSteps);
Serial.print(", Current millis= ");
Serial.print(currentMillis);
Serial.print(", Current steps per sec= ");
Serial.println(currentStepsPerSecond);
}
}
currentMillis++;
//delay(1);
//delayMicroseconds(500);
if (stepMade) {
digitalWrite(X_STEP_PIN, LOW);
digitalWrite(Y_STEP_PIN, LOW);
@ -344,12 +315,6 @@ int StepperControl::moveAbsoluteConstant(unsigned int xDest, unsigned int yDest,
}
//enableMotors(false);
Serial.print("FINISHED at: ");
Serial.print(currentPoint[0]);
Serial.print(", ");
Serial.print(currentPoint[1]);
Serial.print(", ");
Serial.println(currentPoint[2]);
CurrentState::getInstance()->setX(currentPoint[0]);
CurrentState::getInstance()->setY(currentPoint[1]);
CurrentState::getInstance()->setZ(currentPoint[2]);