farmbot-arduino-firmware/src/Command.cpp

172 lines
3.6 KiB
C++
Raw Normal View History

#include "Command.h"
2014-08-06 16:04:40 -06:00
const char axisCodes[3] = { 'X', 'Y', 'Z' };
2014-08-07 15:05:30 -06:00
const char axisSpeedCodes[3] = { 'A', 'B', 'C' };
2014-08-06 16:04:40 -06:00
const char speedCode = 'S';
const char parameterIdCode = 'P';
const char parameterValueCode = 'V';
2014-08-07 15:05:30 -06:00
double axisValue[3] = { 0.0, 0.0, 0.0 };
long axisSpeedValue[3] = { 0, 0, 0 };
double speedValue = 0.0;
long parameterId = 0;
long parameterValue = 0;
2014-08-06 16:04:40 -06:00
2014-05-05 16:15:58 -06:00
CommandCodeEnum commandCodeEnum = CODE_UNDEFINED;
Command::Command(String commandString) {
char charBuf[commandString.length()];
commandString.toCharArray(charBuf, commandString.length());
2014-05-05 16:15:58 -06:00
char* charPointer;
bool invalidCommand = false;
2014-05-05 16:15:58 -06:00
charPointer = strtok(charBuf, " ");
2014-07-23 01:46:21 -06:00
if (charPointer[0] == 'G' || charPointer[0] == 'F') {
2014-05-05 16:15:58 -06:00
commandCodeEnum = getGCodeEnum(charPointer);
} else {
invalidCommand = true;
return;
2014-05-05 16:15:58 -06:00
}
while (charPointer != NULL) {
getParameter(charPointer);
charPointer = strtok(NULL, " \n\r");
}
}
CommandCodeEnum Command::getGCodeEnum(char* code) {
if (strcmp(code, "G0") == 0 || strcmp(code, "G00") == 0) {
return G00;
2014-05-05 16:15:58 -06:00
}
if (strcmp(code, "G1") == 0 || strcmp(code, "G01") == 0) {
return G01;
}
2014-07-23 01:46:21 -06:00
//if (strcmp(code, "F3") == 0 || strcmp(code, "F03") == 0) {
// return F03;
//}
if (strcmp(code, "F11") == 0) {
return F11;
2014-05-05 16:15:58 -06:00
}
2014-07-23 01:46:21 -06:00
if (strcmp(code, "F12") == 0) {
return F12;
}
if (strcmp(code, "F13") == 0) {
return F13;
}
2014-08-06 16:04:40 -06:00
if (strcmp(code, "F20") == 0) {
return F20;
}
if (strcmp(code, "F21") == 0) {
return F21;
}
if (strcmp(code, "F22") == 0) {
return F22;
}
2014-09-04 15:10:47 -06:00
if (strcmp(code, "F31") == 0) {
return F31;
}
if (strcmp(code, "F32") == 0) {
return F32;
}
2014-07-23 01:46:21 -06:00
if (strcmp(code, "F81") == 0) {
return F81;
}
if (strcmp(code, "F82") == 0) {
return F82;
}
if (strcmp(code, "F83") == 0) {
return F83;
}
2014-05-05 16:15:58 -06:00
return CODE_UNDEFINED;
}
double minusNotAllowed(double value) {
if(value < 0) {
return 0;
}
return value;
}
2014-05-05 16:15:58 -06:00
void Command::getParameter(char* charPointer) {
if (charPointer[0] == axisCodes[0]) {
axisValue[0] = atof(charPointer + 1);
} else if (charPointer[0] == axisCodes[1]) {
axisValue[1] = atof(charPointer + 1);
} else if (charPointer[0] == axisCodes[2]) {
axisValue[2] = atof(charPointer + 1);
2014-08-07 15:05:30 -06:00
} else if (charPointer[0] == axisSpeedCodes[0]) {
axisSpeedValue[0] = atof(charPointer + 1);
} else if (charPointer[0] == axisSpeedCodes[1]) {
axisSpeedValue[1] = atof(charPointer + 1);
} else if (charPointer[0] == axisSpeedCodes[2]) {
axisSpeedValue[2] = atof(charPointer + 1);
} else if (charPointer[0] == speedCode) {
2014-08-24 15:52:32 -06:00
speedValue = atof(charPointer + 1);
2014-08-06 16:04:40 -06:00
} else if (charPointer[0] == parameterIdCode) {
2014-08-24 15:52:32 -06:00
parameterId = atof(charPointer + 1);
2014-08-06 16:04:40 -06:00
} else if (charPointer[0] == parameterValueCode) {
2014-08-24 15:52:32 -06:00
parameterValue = atof(charPointer + 1);
2014-05-05 16:15:58 -06:00
}
}
2014-05-05 16:15:58 -06:00
void Command::print() {
2014-08-24 15:52:32 -06:00
Serial.print("R99 Command with code: ");
Serial.print(commandCodeEnum);
2014-08-24 15:52:32 -06:00
Serial.print(", X: ");
2014-05-05 16:15:58 -06:00
Serial.print(axisValue[0]);
2014-08-24 15:52:32 -06:00
Serial.print(", Y: ");
2014-05-05 16:15:58 -06:00
Serial.print(axisValue[1]);
2014-08-24 15:52:32 -06:00
Serial.print(", Z: ");
Serial.print(axisValue[2]);
Serial.print(", S: ");
Serial.print(speedValue);
Serial.print(", P: ");
Serial.print(parameterId);
Serial.print(", V: ");
Serial.print(parameterValue);
Serial.print("\n");
}
CommandCodeEnum Command::getCodeEnum() {
return commandCodeEnum;
}
double Command::getX() {
return axisValue[0];
}
double Command::getY() {
return axisValue[1];
}
double Command::getZ() {
return axisValue[2];
}
2014-08-07 15:05:30 -06:00
long Command::getA() {
return axisValue[0];
}
long Command::getB() {
return axisValue[1];
}
long Command::getC() {
return axisValue[2];
}
2014-08-24 15:52:32 -06:00
long Command::getP() {
2014-08-06 16:04:40 -06:00
return parameterId;
}
2014-08-24 15:52:32 -06:00
long Command::getV() {
2014-08-06 16:04:40 -06:00
return parameterValue;
}