make command a struct

This commit is contained in:
2019-07-13 23:49:15 +03:00
parent f139333bba
commit e4e3e5e00a
9 changed files with 174 additions and 70 deletions

View File

@@ -0,0 +1,6 @@
#ifndef CONFIG_H
#define CONFIG_H
constexpr int pin12v {5};
#endif // CONFIG_H

View File

@@ -1,9 +1,9 @@
#include <Arduino.h>
#include "common/Commands.h"
#ifndef PARSER_H #ifndef PARSER_H
#define PARSER_H #define PARSER_H
float *parseGCode(String gcode); #include <Arduino.h>
#include "common/Commands.h"
#endif Command parseGCode(String gcode);
#endif // PARSER_H

View File

@@ -0,0 +1,15 @@
#ifndef POWER_H
#define POWER_H
#include <Arduino.h>
#include "Config.h"
class Power {
public:
Power();
void disable12v();
void enable12v();
};
#endif // POWER_H

View File

@@ -1,13 +1,10 @@
#include "GCodeParser.h"
#include <Arduino.h> #include <Arduino.h>
float bytecode[4] = {-1, -1, -1, -1}; #include "GCodeParser.h"
float* parseGCode(String gcode) { Command bufcmd;
for (int i = 0; i < 4; i++) {
bytecode[i] = nanf("");
}
Command parseGCode(String gcode) {
char commandStringIn[50]; char commandStringIn[50];
char commandString[50]; char commandString[50];
@@ -55,28 +52,28 @@ float* parseGCode(String gcode) {
floatValue = atof(value); floatValue = atof(value);
if (strcmp(axis, "X") == 0) { if (strcmp(axis, "X") == 0) {
bytecode[X] = floatValue; bufcmd.arg1 = floatValue;
} else if (strcmp(axis, "Y") == 0) { } else if (strcmp(axis, "Y") == 0) {
bytecode[Y] = floatValue; bufcmd.arg2 = floatValue;
} else if (strcmp(axis, "Z") == 0) { } else if (strcmp(axis, "Z") == 0) {
bytecode[Z] = floatValue; bufcmd.arg3 = floatValue;
} }
} }
if (strcmp(command, "G00") == 0) { if (strcmp(command, "G00") == 0) {
bytecode[0] = G00; bufcmd.type = CommandType::G00;
return bytecode; return bufcmd;
} }
if (strcmp(command, "G01") == 0) { if (strcmp(command, "G01") == 0) {
bytecode[0] = G01; bufcmd.type = CommandType::G01;
return bytecode; return bufcmd;
} }
} }
if (strcmp(command, "M99") == 0) { if (strcmp(command, "M99") == 0) {
bytecode[0] = M99; bufcmd.type = CommandType::M99;
return bytecode; return bufcmd;
} }
bytecode[0] = unk; bufcmd.type = CommandType::unk;
return bytecode; return bufcmd;
} }

View File

@@ -0,0 +1,10 @@
#include "Power.h"
Power::Power() {
pinMode(pin12v, OUTPUT);
enable12v();
}
void Power::disable12v() { digitalWrite(pin12v, false); }
void Power::enable12v() { digitalWrite(pin12v, true); }

View File

@@ -8,13 +8,18 @@
String inString; String inString;
bool waitingForNext = false; bool waitingForNext = false;
void sendCommand(float *command) { void execCommand(Command command) {
if (command[0] != unk) { if (command.type != CommandType::unk) {
Wire.beginTransmission(8); Wire.beginTransmission(8);
byte txBuffer[4]; byte buffer[7][sizeof(float)];
for (int i = 0; i < 4; i++) { command.toBytes(buffer[0]);
floatToBytes(txBuffer, command[i]); for (int i = 0; i < 7; i++) {
Wire.write(txBuffer, 4); /*
float dbg;
bytesToFloat(&dbg, buffer[i]);
Serial.println(dbg);
*/
Wire.write(buffer[i], sizeof(float));
} }
Wire.endTransmission(); Wire.endTransmission();
} else { } else {
@@ -22,24 +27,23 @@ void sendCommand(float *command) {
return; return;
} }
if (command[0] == G01 || command[0] == G00) { if (command.type == CommandType::G01 || command.type == CommandType::G00) {
Wire.requestFrom(8, 1); Wire.requestFrom(8, 1);
waitingForNext = true; waitingForNext = true;
return; return;
} }
if (command[0] == M99) { if (command.type == CommandType::M99) {
Wire.requestFrom(8, 5 * sizeof(float)); Wire.requestFrom(8, 5 * sizeof(float));
float resp[5]; float resp[5];
byte buffer[4]; byte buffer[sizeof(float)];
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) { for (int j = 0; j < sizeof(float); j++) {
while (!Wire.available()) { while (!Wire.available()) {
} }
char read = Wire.read(); buffer[j] = Wire.read();
buffer[j] = read;
} }
bytesToFloat(&resp[i], buffer); bytesToFloat(&resp[i], buffer);
} }
@@ -69,7 +73,7 @@ void loop() {
if (inChar == '\n') { if (inChar == '\n') {
inString.trim(); inString.trim();
sendCommand(parseGCode(inString)); execCommand(parseGCode(inString));
unsigned long reqTime = millis(); unsigned long reqTime = millis();
while (waitingForNext) { while (waitingForNext) {
while (!Wire.available()) { while (!Wire.available()) {

View File

@@ -34,44 +34,44 @@ void adjustRPM() {
sei(); sei();
} }
int curFloat = 0; Command command;
float command[4];
bool newCommand = false; bool newCommand = false;
bool executing = false; bool executing = false;
int curByte = 0; int curByte = 0;
byte rxBuffer[4]; int curFloat = 0;
byte rxBuffer[7][sizeof(float)];
void receiveEvent(int howMany) { void receiveEvent(int howMany) {
while (Wire.available() > 0) { while (Wire.available() > 0) {
if (!newCommand) { if (!newCommand) {
char c = Wire.read(); char c = Wire.read();
rxBuffer[curByte] = c; rxBuffer[curFloat][curByte] = c;
curByte++; curByte++;
if (curByte == 4) { if (curByte == 4) {
curByte = 0; curByte = 0;
bytesToFloat(&command[curFloat], rxBuffer);
curFloat++; curFloat++;
} }
if (curFloat == 4) { if (curFloat == 7) {
curFloat = 0; curFloat = 0;
command.fromBytes(rxBuffer[0]);
newCommand = true; newCommand = true;
} }
} }
} }
} }
byte txBuffer[5 * sizeof(float)]; byte txBuffer[5][sizeof(float)];
void requestEvent() { void requestEvent() {
if (command[0] == M99) { if (command.type == CommandType::M99) {
floatToBytes(&txBuffer[0], servoStepper.getPos()); floatToBytes(txBuffer[0], servoStepper.getPos());
floatToBytes(&txBuffer[sizeof(float)], eggStepper.getPos()); floatToBytes(txBuffer[1], eggStepper.getPos());
floatToBytes(&txBuffer[sizeof(float) * 2], servoStepper.getPosMm()); floatToBytes(txBuffer[2], servoStepper.getPosMm());
floatToBytes(&txBuffer[sizeof(float) * 3], eggStepper.getPosMm()); floatToBytes(txBuffer[3], eggStepper.getPosMm());
floatToBytes(&txBuffer[sizeof(float) * 4], (float)pen.getEngaged()); floatToBytes(txBuffer[4], (float)pen.getEngaged());
Wire.write(txBuffer, 5 * sizeof(float)); Wire.write(txBuffer[0], 5 * sizeof(float));
} else if (executing || newCommand) { } else if (executing || newCommand) {
Wire.write(WAIT); Wire.write(WAIT);
} else { } else {
@@ -81,26 +81,27 @@ void requestEvent() {
void execCommand() { void execCommand() {
executing = true; executing = true;
if (command[0] == G01 || command[0] == G00) {
if (command[0] == G01) { if (command.type == CommandType::G01 || command.type == CommandType::G00) {
if (command.type == CommandType::G01) {
needAdjust = true; needAdjust = true;
} else { } else {
needAdjust = false; needAdjust = false;
} }
if (!isnan(command[X])) { if (!isnan(command.arg1)) {
servoStepper.moveTo(command[X]); servoStepper.moveTo(command.arg1);
} }
if (!isnan(command[Y])) { if (!isnan(command.arg2)) {
eggStepper.moveTo(command[Y]); eggStepper.moveTo(command.arg2);
} }
if (!isnan(command[Z])) { if (!isnan(command.arg3)) {
if (command[Z] < 0) { if (command.arg3 < 0) {
pen.engage(); pen.engage();
} }
if (command[Z] >= 0) { if (command.arg3 >= 0) {
pen.disengage(); pen.disengage();
} }
} }
@@ -127,11 +128,11 @@ void setup() {
pen.init(); pen.init();
} }
unsigned int ms = 0; volatile unsigned int ms = 0;
ISR(TIMER0_COMPA_vect) { ISR(TIMER0_COMPA_vect) {
ms++; ms++;
if (ms % adjustDelay == 0) { if (ms % adjustDelay == 0) {
adjustRPM(); adjustRPM();
} }
if (ms % eggStepperDelay == 0) { if (ms % eggStepperDelay == 0) {

View File

@@ -1,14 +1,7 @@
#ifndef COMMANDS_H #ifndef COMMANDS_H
#define COMMANDS_H #define COMMANDS_H
enum command{ enum bcAxis {
unk,
G00,
G01,
M99,
};
enum bcAxis{
X = 1, X = 1,
Y = 2, Y = 2,
Z = 3, Z = 3,
@@ -27,6 +20,42 @@ enum StatusMSG {
penPos, penPos,
}; };
enum class CommandType {
unk = 0,
G00,
G01,
M99,
};
struct Command {
CommandType type = CommandType::unk;
float arg1 = NAN;
float arg2 = NAN;
float arg3 = NAN;
float arg4 = NAN;
float arg5 = NAN;
float arg6 = NAN;
Command(CommandType type = CommandType::unk, float arg1 = NAN,
float arg2 = NAN, float arg3 = NAN, float arg4 = NAN,
float arg5 = NAN, float arg6 = NAN)
: type(type),
arg1(arg1),
arg2(arg2),
arg3(arg3),
arg4(arg4),
arg5(arg5),
arg6(arg6){};
Command(float *floats);
int fromFloats(float *floats);
int toFloats(float *floats);
Command(byte *bytes);
int fromBytes(byte *bytes);
int toBytes(byte *bytes);
};
void bytesToFloat(float *target, byte *val); void bytesToFloat(float *target, byte *val);
void floatToBytes(byte *target, float val); void floatToBytes(byte *target, float val);

View File

@@ -1,11 +1,53 @@
#include <Arduino.h> #include <Arduino.h>
#include "common/Commands.h"
Command::Command(float *floats) { fromFloats(floats); }
int Command::fromFloats(float *floats) {
type = static_cast<CommandType>(floats[0]);
arg1 = floats[1];
arg2 = floats[2];
arg3 = floats[3];
arg4 = floats[4];
arg5 = floats[5];
arg6 = floats[6];
}
int Command::toFloats(float *floats) {
floats[0] = static_cast<float>(type);
floats[1] = arg1;
floats[2] = arg2;
floats[3] = arg3;
floats[4] = arg4;
floats[5] = arg5;
floats[6] = arg6;
}
Command::Command(byte *bytes) { fromBytes(bytes); }
int Command::fromBytes(byte *bytes) {
float floats[7];
for (int i = 0; i < 7; i++) {
bytesToFloat(&floats[i], &bytes[i * sizeof(float)]);
}
fromFloats(floats);
}
int Command::toBytes(byte *bytes) {
float floats[7];
toFloats(floats);
for (int i = 0; i < 7; i++) {
floatToBytes(&bytes[i * sizeof(float)], floats[i]);
}
}
void bytesToFloat(float *target, byte *val) { void bytesToFloat(float *target, byte *val) {
memcpy(target, val, 4); memcpy(target, val, sizeof(float));
return; return;
} }
void floatToBytes(byte *target, float val) { void floatToBytes(byte *target, float val) {
memcpy(target, &val, 4); memcpy(target, &val, sizeof(float));
return; return;
} }