Added logging enable/disable.

Removed feed rate and extruder parameters - not used in farmbot
pull/1/head
mateo 2014-05-16 22:27:16 +02:00
parent 66a0515d88
commit a7bf3dfb25
9 changed files with 89 additions and 83 deletions

View File

@ -2,10 +2,6 @@
const char axisCodes[3] = { 'X', 'Y', 'Z' }; const char axisCodes[3] = { 'X', 'Y', 'Z' };
double axisValue[3] = { 0.0, 0.0, 0.0 }; 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; CommandCodeEnum commandCodeEnum = CODE_UNDEFINED;
Command::Command(String commandString) { Command::Command(String commandString) {
@ -29,20 +25,20 @@ Command::Command(String commandString) {
} }
CommandCodeEnum Command::getGCodeEnum(char* code) { CommandCodeEnum Command::getGCodeEnum(char* code) {
if (strcmp(code, "G0") == 0) { if (strcmp(code, "G0") == 0 || strcmp(code, "G00") == 0) {
return G0; return G00;
} }
if (strcmp(code, "G1") == 0) { if (strcmp(code, "G1") == 0 || strcmp(code, "G01") == 0) {
return G1; return G01;
} }
if (strcmp(code, "G2") == 0) { if (strcmp(code, "G2") == 0 || strcmp(code, "G02") == 0) {
return G2; return G02;
} }
if (strcmp(code, "G3") == 0) { if (strcmp(code, "G3") == 0 || strcmp(code, "G03") == 0) {
return G3; return G03;
} }
if (strcmp(code, "G4") == 0) { if (strcmp(code, "G4") == 0 || strcmp(code, "G04") == 0) {
return G4; return G04;
} }
return CODE_UNDEFINED; return CODE_UNDEFINED;
} }
@ -54,10 +50,6 @@ void Command::getParameter(char* charPointer) {
axisValue[1] = atof(charPointer + 1); axisValue[1] = atof(charPointer + 1);
} else if (charPointer[0] == axisCodes[2]) { } else if (charPointer[0] == axisCodes[2]) {
axisValue[2] = atof(charPointer + 1); 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(axisValue[1]);
Serial.print(", Z:"); Serial.print(", Z:");
Serial.println(axisValue[2]); Serial.println(axisValue[2]);
Serial.print("Extrusion motor:");
Serial.println(extrusionMotor);
Serial.print("Feed rate:");
Serial.println(feedRate);
} }
CommandCodeEnum Command::getCodeEnum() { CommandCodeEnum Command::getCodeEnum() {
@ -89,13 +77,6 @@ double Command::getY() {
} }
double Command::getZ() { double Command::getZ() {
return axisValue[1]; return axisValue[2];
} }
double Command::getE() {
return extrusionMotor;
}
double Command::getFeedRate() {
return feedRate;
}

View File

@ -7,11 +7,11 @@
enum CommandCodeEnum enum CommandCodeEnum
{ {
CODE_UNDEFINED = -1, CODE_UNDEFINED = -1,
G0 = 0, G00 = 0,
G1, G01,
G2, G02,
G3, G03,
G4 G04
}; };
#define NULL 0 #define NULL 0
@ -25,8 +25,6 @@ public:
double getX(); double getX();
double getY(); double getY();
double getZ(); double getZ();
double getFeedRate();
double getE();
private: private:
CommandCodeEnum getGCodeEnum(char* code); CommandCodeEnum getGCodeEnum(char* code);
void getParameter(char* charPointer); void getParameter(char* charPointer);

13
Config.h 100644
View File

@ -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_ */

View File

@ -11,8 +11,6 @@ static CurrentState* instance;
static double x = 0.0; static double x = 0.0;
static double y = 0.0; static double y = 0.0;
static double z = 0.0; static double z = 0.0;
static double e = 0.0;
static double feedRate = 0.0;
CurrentState * CurrentState::getInstance() { CurrentState * CurrentState::getInstance() {
if (!instance) { if (!instance) {
@ -36,14 +34,6 @@ double CurrentState::getZ() {
return z; return z;
} }
double CurrentState::getE() {
return e;
}
double CurrentState::getFeedRate() {
return feedRate;
}
void CurrentState::setX(double newX) { void CurrentState::setX(double newX) {
x = newX; x = newX;
} }
@ -64,10 +54,6 @@ void CurrentState::print() {
Serial.print(y); Serial.print(y);
Serial.print(", Z:"); Serial.print(", Z:");
Serial.println(z); Serial.println(z);
Serial.print("Extrusion motor:");
Serial.println(e);
Serial.print("Feed rate:");
Serial.println(feedRate);
} }

View File

@ -15,8 +15,6 @@ public:
double getX(); double getX();
double getY(); double getY();
double getZ(); double getZ();
double getFeedRate();
double getE();
void setX(double); void setX(double);
void setY(double); void setY(double);
void setZ(double); void setZ(double);

View File

@ -5,9 +5,11 @@
* Author: MattLech * Author: MattLech
*/ */
#include "GCodeHandler.h"
#include "G00Handler.h" #include "G00Handler.h"
#include "CurrentState.h" #include "CurrentState.h"
#include "pins.h" #include "pins.h"
#include "Config.h"
static G00Handler* instance; static G00Handler* instance;
@ -16,15 +18,16 @@ G00Handler * G00Handler::getInstance() {
instance = new G00Handler(); instance = new G00Handler();
}; };
return instance; return instance;
}; }
;
G00Handler::G00Handler() { G00Handler::G00Handler() {
} }
long adjustStepAmount(long steps) { long adjustStepAmount(long steps) {
if(steps < 0) { if (steps < 0) {
return steps + 1; return steps + 1;
} else if(steps > 0) { } else if (steps > 0) {
return steps - 1; return steps - 1;
} else { } else {
return 0; return 0;
@ -35,61 +38,75 @@ long getNumberOfSteps(double destinationNumber, double currentNumber) {
return destinationNumber - currentNumber; return destinationNumber - currentNumber;
} }
int GCodeHandler::execute(Command* command) { int GCodeHandler::execute(Command* command) {
long xStepsNeeded = getNumberOfSteps(command->getX(), CurrentState::getInstance()->getX()); long xStepsNeeded = getNumberOfSteps(command->getX(),
long yStepsNeeded = getNumberOfSteps(command->getY(), CurrentState::getInstance()->getY()); CurrentState::getInstance()->getX());
long zStepsNeeded = getNumberOfSteps(command->getZ(), CurrentState::getInstance()->getZ()); long yStepsNeeded = getNumberOfSteps(command->getY(),
CurrentState::getInstance()->getY());
if(xStepsNeeded > 0) { 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); digitalWrite(X_DIR_PIN, LOW);
} else { } else {
digitalWrite(X_DIR_PIN, HIGH); digitalWrite(X_DIR_PIN, HIGH);
} }
if(yStepsNeeded > 0) { if (yStepsNeeded > 0) {
digitalWrite(Y_DIR_PIN, LOW); digitalWrite(Y_DIR_PIN, LOW);
} else { } else {
digitalWrite(Y_DIR_PIN, HIGH); digitalWrite(Y_DIR_PIN, HIGH);
} }
if(zStepsNeeded > 0) { if (zStepsNeeded > 0) {
digitalWrite(Z_DIR_PIN, LOW); digitalWrite(Z_DIR_PIN, LOW);
} else { } else {
digitalWrite(Z_DIR_PIN, HIGH); digitalWrite(Z_DIR_PIN, HIGH);
} }
CurrentState::getInstance()->setX(CurrentState::getInstance()->getX() + xStepsNeeded); CurrentState::getInstance()->setX(
CurrentState::getInstance()->setY(CurrentState::getInstance()->getY() + yStepsNeeded); CurrentState::getInstance()->getX() + xStepsNeeded);
CurrentState::getInstance()->setZ(CurrentState::getInstance()->getZ() + zStepsNeeded); CurrentState::getInstance()->setY(
CurrentState::getInstance()->getY() + yStepsNeeded);
CurrentState::getInstance()->setZ(
CurrentState::getInstance()->getZ() + zStepsNeeded);
digitalWrite(X_ENABLE_PIN, LOW); digitalWrite(X_ENABLE_PIN, LOW);
digitalWrite(Y_ENABLE_PIN, LOW); digitalWrite(Y_ENABLE_PIN, LOW);
digitalWrite(Z_ENABLE_PIN, LOW); digitalWrite(Z_ENABLE_PIN, LOW);
while(xStepsNeeded != 0 || yStepsNeeded != 0 || zStepsNeeded != 0) { while (xStepsNeeded != 0 || yStepsNeeded != 0 || zStepsNeeded != 0) {
if(xStepsNeeded != 0) { if (xStepsNeeded != 0) {
digitalWrite(X_STEP_PIN, HIGH); digitalWrite(X_STEP_PIN, HIGH);
} }
if(yStepsNeeded != 0) { if (yStepsNeeded != 0) {
digitalWrite(Y_STEP_PIN, HIGH); digitalWrite(Y_STEP_PIN, HIGH);
} }
if(zStepsNeeded != 0) { if (zStepsNeeded != 0) {
digitalWrite(Z_STEP_PIN, HIGH); digitalWrite(Z_STEP_PIN, HIGH);
} }
delay(5); delay(5);
if(xStepsNeeded != 0) { if (xStepsNeeded != 0) {
digitalWrite(X_STEP_PIN, LOW); digitalWrite(X_STEP_PIN, LOW);
xStepsNeeded = adjustStepAmount(xStepsNeeded); xStepsNeeded = adjustStepAmount(xStepsNeeded);
} }
if(yStepsNeeded != 0) { if (yStepsNeeded != 0) {
digitalWrite(Y_STEP_PIN, LOW); digitalWrite(Y_STEP_PIN, LOW);
yStepsNeeded = adjustStepAmount(yStepsNeeded); yStepsNeeded = adjustStepAmount(yStepsNeeded);
} }
if(zStepsNeeded != 0) { if (zStepsNeeded != 0) {
digitalWrite(Z_STEP_PIN, LOW); digitalWrite(Z_STEP_PIN, LOW);
zStepsNeeded = adjustStepAmount(zStepsNeeded); zStepsNeeded = adjustStepAmount(zStepsNeeded);
} }
} }
if(LOGGING) {
CurrentState::getInstance()->print(); CurrentState::getInstance()->print();
}
return 0;
} }

View File

@ -14,12 +14,18 @@ GCodeProcessor::GCodeProcessor() {
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) { int GCodeProcessor::execute(Command* command) {
if(command == NULL || command->getCodeEnum() == CODE_UNDEFINED) { if(command == NULL || command->getCodeEnum() == CODE_UNDEFINED) {
Serial.print("command == NULL:"); if(LOGGING) {
Serial.println(command == NULL); printCommandLog(command);
Serial.println("command->getCodeEnum() == CODE_UNDEFINED:"); }
Serial.println(command->getCodeEnum() == CODE_UNDEFINED);
return -1; return -1;
} }
GCodeHandler* handler = getGCodeHandler(command->getCodeEnum()); GCodeHandler* handler = getGCodeHandler(command->getCodeEnum());
@ -32,8 +38,10 @@ int GCodeProcessor::execute(Command* command) {
GCodeHandler* GCodeProcessor::getGCodeHandler(CommandCodeEnum codeEnum) { GCodeHandler* GCodeProcessor::getGCodeHandler(CommandCodeEnum codeEnum) {
switch(codeEnum) { switch(codeEnum) {
case G0: case G00:
return G00Handler::getInstance(); return G00Handler::getInstance();
} }
return NULL; return NULL;
} }

View File

@ -10,6 +10,7 @@
#include "GCodeHandler.h" #include "GCodeHandler.h"
#include "G00Handler.h" #include "G00Handler.h"
#include "Command.h" #include "Command.h"
#include "Config.h"
class GCodeProcessor { class GCodeProcessor {
public: public:
@ -18,6 +19,8 @@ public:
int execute(Command* command); int execute(Command* command);
protected: protected:
GCodeHandler* getGCodeHandler(CommandCodeEnum); GCodeHandler* getGCodeHandler(CommandCodeEnum);
private:
void printCommandLog(Command*);
}; };
#endif /* GCODEPROCESSOR_H_ */ #endif /* GCODEPROCESSOR_H_ */

View File

@ -1,6 +1,7 @@
// Do not remove the include below // Do not remove the include below
#include "farmbot_arduino_controller.h" #include "farmbot_arduino_controller.h"
#include "pins.h" #include "pins.h"
#include "Config.h"
static char commandEndChar = 0x0A; static char commandEndChar = 0x0A;
static GCodeProcessor* gCodeProcessor = new GCodeProcessor(); static GCodeProcessor* gCodeProcessor = new GCodeProcessor();
@ -34,8 +35,9 @@ void loop() {
String commandString = Serial.readStringUntil(commandEndChar); String commandString = Serial.readStringUntil(commandEndChar);
if (commandString && commandString.length() > 0) { if (commandString && commandString.length() > 0) {
Command* command = new Command(commandString); Command* command = new Command(commandString);
command->print(); if(LOGGING) {
command->print();
}
gCodeProcessor->execute(command); gCodeProcessor->execute(command);
} }
} }