Added logging enable/disable.
Removed feed rate and extruder parameters - not used in farmbotpull/1/head
parent
66a0515d88
commit
a7bf3dfb25
41
Command.cpp
41
Command.cpp
|
@ -2,10 +2,6 @@
|
|||
|
||||
const char axisCodes[3] = { 'X', 'Y', 'Z' };
|
||||
double axisValue[3] = { 0.0, 0.0, 0.0 };
|
||||
const char feedRateCode = 'F';
|
||||
double feedRate = 0.0;
|
||||
const char extrusionMotorCode = 'E';
|
||||
double extrusionMotor = 0.0;
|
||||
CommandCodeEnum commandCodeEnum = CODE_UNDEFINED;
|
||||
|
||||
Command::Command(String commandString) {
|
||||
|
@ -29,20 +25,20 @@ Command::Command(String commandString) {
|
|||
}
|
||||
|
||||
CommandCodeEnum Command::getGCodeEnum(char* code) {
|
||||
if (strcmp(code, "G0") == 0) {
|
||||
return G0;
|
||||
if (strcmp(code, "G0") == 0 || strcmp(code, "G00") == 0) {
|
||||
return G00;
|
||||
}
|
||||
if (strcmp(code, "G1") == 0) {
|
||||
return G1;
|
||||
if (strcmp(code, "G1") == 0 || strcmp(code, "G01") == 0) {
|
||||
return G01;
|
||||
}
|
||||
if (strcmp(code, "G2") == 0) {
|
||||
return G2;
|
||||
if (strcmp(code, "G2") == 0 || strcmp(code, "G02") == 0) {
|
||||
return G02;
|
||||
}
|
||||
if (strcmp(code, "G3") == 0) {
|
||||
return G3;
|
||||
if (strcmp(code, "G3") == 0 || strcmp(code, "G03") == 0) {
|
||||
return G03;
|
||||
}
|
||||
if (strcmp(code, "G4") == 0) {
|
||||
return G4;
|
||||
if (strcmp(code, "G4") == 0 || strcmp(code, "G04") == 0) {
|
||||
return G04;
|
||||
}
|
||||
return CODE_UNDEFINED;
|
||||
}
|
||||
|
@ -54,10 +50,6 @@ void Command::getParameter(char* charPointer) {
|
|||
axisValue[1] = atof(charPointer + 1);
|
||||
} else if (charPointer[0] == axisCodes[2]) {
|
||||
axisValue[2] = atof(charPointer + 1);
|
||||
} else if (charPointer[0] == feedRateCode) {
|
||||
feedRate = atof(charPointer + 1);
|
||||
} else if (charPointer[0] == extrusionMotorCode) {
|
||||
extrusionMotor = atof(charPointer + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,10 +62,6 @@ void Command::print() {
|
|||
Serial.print(axisValue[1]);
|
||||
Serial.print(", Z:");
|
||||
Serial.println(axisValue[2]);
|
||||
Serial.print("Extrusion motor:");
|
||||
Serial.println(extrusionMotor);
|
||||
Serial.print("Feed rate:");
|
||||
Serial.println(feedRate);
|
||||
}
|
||||
|
||||
CommandCodeEnum Command::getCodeEnum() {
|
||||
|
@ -89,13 +77,6 @@ double Command::getY() {
|
|||
}
|
||||
|
||||
double Command::getZ() {
|
||||
return axisValue[1];
|
||||
return axisValue[2];
|
||||
}
|
||||
|
||||
double Command::getE() {
|
||||
return extrusionMotor;
|
||||
}
|
||||
|
||||
double Command::getFeedRate() {
|
||||
return feedRate;
|
||||
}
|
||||
|
|
12
Command.h
12
Command.h
|
@ -7,11 +7,11 @@
|
|||
enum CommandCodeEnum
|
||||
{
|
||||
CODE_UNDEFINED = -1,
|
||||
G0 = 0,
|
||||
G1,
|
||||
G2,
|
||||
G3,
|
||||
G4
|
||||
G00 = 0,
|
||||
G01,
|
||||
G02,
|
||||
G03,
|
||||
G04
|
||||
};
|
||||
|
||||
#define NULL 0
|
||||
|
@ -25,8 +25,6 @@ public:
|
|||
double getX();
|
||||
double getY();
|
||||
double getZ();
|
||||
double getFeedRate();
|
||||
double getE();
|
||||
private:
|
||||
CommandCodeEnum getGCodeEnum(char* code);
|
||||
void getParameter(char* charPointer);
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
/*
|
||||
* Config.h
|
||||
*
|
||||
* Created on: 16 maj 2014
|
||||
* Author: MattLech
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_H_
|
||||
#define CONFIG_H_
|
||||
|
||||
const int LOGGING = 1;
|
||||
|
||||
#endif /* CONFIG_H_ */
|
|
@ -11,8 +11,6 @@ static CurrentState* instance;
|
|||
static double x = 0.0;
|
||||
static double y = 0.0;
|
||||
static double z = 0.0;
|
||||
static double e = 0.0;
|
||||
static double feedRate = 0.0;
|
||||
|
||||
CurrentState * CurrentState::getInstance() {
|
||||
if (!instance) {
|
||||
|
@ -36,14 +34,6 @@ double CurrentState::getZ() {
|
|||
return z;
|
||||
}
|
||||
|
||||
double CurrentState::getE() {
|
||||
return e;
|
||||
}
|
||||
|
||||
double CurrentState::getFeedRate() {
|
||||
return feedRate;
|
||||
}
|
||||
|
||||
void CurrentState::setX(double newX) {
|
||||
x = newX;
|
||||
}
|
||||
|
@ -64,10 +54,6 @@ void CurrentState::print() {
|
|||
Serial.print(y);
|
||||
Serial.print(", Z:");
|
||||
Serial.println(z);
|
||||
Serial.print("Extrusion motor:");
|
||||
Serial.println(e);
|
||||
Serial.print("Feed rate:");
|
||||
Serial.println(feedRate);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,8 +15,6 @@ public:
|
|||
double getX();
|
||||
double getY();
|
||||
double getZ();
|
||||
double getFeedRate();
|
||||
double getE();
|
||||
void setX(double);
|
||||
void setY(double);
|
||||
void setZ(double);
|
||||
|
|
|
@ -5,9 +5,11 @@
|
|||
* Author: MattLech
|
||||
*/
|
||||
|
||||
#include "GCodeHandler.h"
|
||||
#include "G00Handler.h"
|
||||
#include "CurrentState.h"
|
||||
#include "pins.h"
|
||||
#include "Config.h"
|
||||
|
||||
static G00Handler* instance;
|
||||
|
||||
|
@ -16,15 +18,16 @@ G00Handler * G00Handler::getInstance() {
|
|||
instance = new G00Handler();
|
||||
};
|
||||
return instance;
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
G00Handler::G00Handler() {
|
||||
}
|
||||
|
||||
long adjustStepAmount(long steps) {
|
||||
if(steps < 0) {
|
||||
if (steps < 0) {
|
||||
return steps + 1;
|
||||
} else if(steps > 0) {
|
||||
} else if (steps > 0) {
|
||||
return steps - 1;
|
||||
} else {
|
||||
return 0;
|
||||
|
@ -35,61 +38,75 @@ long getNumberOfSteps(double destinationNumber, double currentNumber) {
|
|||
return destinationNumber - currentNumber;
|
||||
}
|
||||
|
||||
|
||||
int GCodeHandler::execute(Command* command) {
|
||||
long xStepsNeeded = getNumberOfSteps(command->getX(), CurrentState::getInstance()->getX());
|
||||
long yStepsNeeded = getNumberOfSteps(command->getY(), CurrentState::getInstance()->getY());
|
||||
long zStepsNeeded = getNumberOfSteps(command->getZ(), CurrentState::getInstance()->getZ());
|
||||
|
||||
if(xStepsNeeded > 0) {
|
||||
long xStepsNeeded = getNumberOfSteps(command->getX(),
|
||||
CurrentState::getInstance()->getX());
|
||||
long yStepsNeeded = getNumberOfSteps(command->getY(),
|
||||
CurrentState::getInstance()->getY());
|
||||
long zStepsNeeded = getNumberOfSteps(command->getZ(),
|
||||
CurrentState::getInstance()->getZ());
|
||||
if(LOGGING) {
|
||||
Serial.print("Steps X:");
|
||||
Serial.print(xStepsNeeded);
|
||||
Serial.print(", Steps Y:");
|
||||
Serial.print(yStepsNeeded);
|
||||
Serial.print(", Steps Z:");
|
||||
Serial.println(zStepsNeeded);
|
||||
}
|
||||
if (xStepsNeeded > 0) {
|
||||
digitalWrite(X_DIR_PIN, LOW);
|
||||
} else {
|
||||
digitalWrite(X_DIR_PIN, HIGH);
|
||||
}
|
||||
if(yStepsNeeded > 0) {
|
||||
if (yStepsNeeded > 0) {
|
||||
digitalWrite(Y_DIR_PIN, LOW);
|
||||
} else {
|
||||
digitalWrite(Y_DIR_PIN, HIGH);
|
||||
}
|
||||
if(zStepsNeeded > 0) {
|
||||
if (zStepsNeeded > 0) {
|
||||
digitalWrite(Z_DIR_PIN, LOW);
|
||||
} else {
|
||||
digitalWrite(Z_DIR_PIN, HIGH);
|
||||
}
|
||||
|
||||
CurrentState::getInstance()->setX(CurrentState::getInstance()->getX() + xStepsNeeded);
|
||||
CurrentState::getInstance()->setY(CurrentState::getInstance()->getY() + yStepsNeeded);
|
||||
CurrentState::getInstance()->setZ(CurrentState::getInstance()->getZ() + zStepsNeeded);
|
||||
CurrentState::getInstance()->setX(
|
||||
CurrentState::getInstance()->getX() + xStepsNeeded);
|
||||
CurrentState::getInstance()->setY(
|
||||
CurrentState::getInstance()->getY() + yStepsNeeded);
|
||||
CurrentState::getInstance()->setZ(
|
||||
CurrentState::getInstance()->getZ() + zStepsNeeded);
|
||||
|
||||
digitalWrite(X_ENABLE_PIN, LOW);
|
||||
digitalWrite(Y_ENABLE_PIN, LOW);
|
||||
digitalWrite(Z_ENABLE_PIN, LOW);
|
||||
|
||||
while(xStepsNeeded != 0 || yStepsNeeded != 0 || zStepsNeeded != 0) {
|
||||
if(xStepsNeeded != 0) {
|
||||
while (xStepsNeeded != 0 || yStepsNeeded != 0 || zStepsNeeded != 0) {
|
||||
if (xStepsNeeded != 0) {
|
||||
digitalWrite(X_STEP_PIN, HIGH);
|
||||
}
|
||||
if(yStepsNeeded != 0) {
|
||||
if (yStepsNeeded != 0) {
|
||||
digitalWrite(Y_STEP_PIN, HIGH);
|
||||
}
|
||||
if(zStepsNeeded != 0) {
|
||||
if (zStepsNeeded != 0) {
|
||||
digitalWrite(Z_STEP_PIN, HIGH);
|
||||
}
|
||||
delay(5);
|
||||
if(xStepsNeeded != 0) {
|
||||
if (xStepsNeeded != 0) {
|
||||
digitalWrite(X_STEP_PIN, LOW);
|
||||
xStepsNeeded = adjustStepAmount(xStepsNeeded);
|
||||
}
|
||||
if(yStepsNeeded != 0) {
|
||||
if (yStepsNeeded != 0) {
|
||||
digitalWrite(Y_STEP_PIN, LOW);
|
||||
yStepsNeeded = adjustStepAmount(yStepsNeeded);
|
||||
}
|
||||
if(zStepsNeeded != 0) {
|
||||
if (zStepsNeeded != 0) {
|
||||
digitalWrite(Z_STEP_PIN, LOW);
|
||||
zStepsNeeded = adjustStepAmount(zStepsNeeded);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
CurrentState::getInstance()->print();
|
||||
if(LOGGING) {
|
||||
CurrentState::getInstance()->print();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -14,12 +14,18 @@ GCodeProcessor::GCodeProcessor() {
|
|||
GCodeProcessor::~GCodeProcessor() {
|
||||
}
|
||||
|
||||
void GCodeProcessor::printCommandLog(Command* command) {
|
||||
Serial.print("command == NULL:");
|
||||
Serial.println(command == NULL);
|
||||
Serial.println("command->getCodeEnum() == CODE_UNDEFINED:");
|
||||
Serial.println(command->getCodeEnum() == CODE_UNDEFINED);
|
||||
}
|
||||
|
||||
int GCodeProcessor::execute(Command* command) {
|
||||
if(command == NULL || command->getCodeEnum() == CODE_UNDEFINED) {
|
||||
Serial.print("command == NULL:");
|
||||
Serial.println(command == NULL);
|
||||
Serial.println("command->getCodeEnum() == CODE_UNDEFINED:");
|
||||
Serial.println(command->getCodeEnum() == CODE_UNDEFINED);
|
||||
if(LOGGING) {
|
||||
printCommandLog(command);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
GCodeHandler* handler = getGCodeHandler(command->getCodeEnum());
|
||||
|
@ -32,8 +38,10 @@ int GCodeProcessor::execute(Command* command) {
|
|||
|
||||
GCodeHandler* GCodeProcessor::getGCodeHandler(CommandCodeEnum codeEnum) {
|
||||
switch(codeEnum) {
|
||||
case G0:
|
||||
case G00:
|
||||
return G00Handler::getInstance();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "GCodeHandler.h"
|
||||
#include "G00Handler.h"
|
||||
#include "Command.h"
|
||||
#include "Config.h"
|
||||
|
||||
class GCodeProcessor {
|
||||
public:
|
||||
|
@ -18,6 +19,8 @@ public:
|
|||
int execute(Command* command);
|
||||
protected:
|
||||
GCodeHandler* getGCodeHandler(CommandCodeEnum);
|
||||
private:
|
||||
void printCommandLog(Command*);
|
||||
};
|
||||
|
||||
#endif /* GCODEPROCESSOR_H_ */
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Do not remove the include below
|
||||
#include "farmbot_arduino_controller.h"
|
||||
#include "pins.h"
|
||||
#include "Config.h"
|
||||
|
||||
static char commandEndChar = 0x0A;
|
||||
static GCodeProcessor* gCodeProcessor = new GCodeProcessor();
|
||||
|
@ -34,8 +35,9 @@ void loop() {
|
|||
String commandString = Serial.readStringUntil(commandEndChar);
|
||||
if (commandString && commandString.length() > 0) {
|
||||
Command* command = new Command(commandString);
|
||||
command->print();
|
||||
|
||||
if(LOGGING) {
|
||||
command->print();
|
||||
}
|
||||
gCodeProcessor->execute(command);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue