mirror of
https://github.com/usatiuk/EggbotWireless.git
synced 2025-10-26 16:57:48 +01:00
make command a struct
This commit is contained in:
6
Firmware/EggbotWireless/include/Config.h
Normal file
6
Firmware/EggbotWireless/include/Config.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
constexpr int pin12v {5};
|
||||
|
||||
#endif // CONFIG_H
|
||||
@@ -1,9 +1,9 @@
|
||||
#include <Arduino.h>
|
||||
#include "common/Commands.h"
|
||||
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
float *parseGCode(String gcode);
|
||||
#include <Arduino.h>
|
||||
#include "common/Commands.h"
|
||||
|
||||
#endif
|
||||
Command parseGCode(String gcode);
|
||||
|
||||
#endif // PARSER_H
|
||||
15
Firmware/EggbotWireless/include/Power.h
Normal file
15
Firmware/EggbotWireless/include/Power.h
Normal 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
|
||||
@@ -1,13 +1,10 @@
|
||||
#include "GCodeParser.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
float bytecode[4] = {-1, -1, -1, -1};
|
||||
#include "GCodeParser.h"
|
||||
|
||||
float* parseGCode(String gcode) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
bytecode[i] = nanf("");
|
||||
}
|
||||
Command bufcmd;
|
||||
|
||||
Command parseGCode(String gcode) {
|
||||
char commandStringIn[50];
|
||||
char commandString[50];
|
||||
|
||||
@@ -55,28 +52,28 @@ float* parseGCode(String gcode) {
|
||||
floatValue = atof(value);
|
||||
|
||||
if (strcmp(axis, "X") == 0) {
|
||||
bytecode[X] = floatValue;
|
||||
bufcmd.arg1 = floatValue;
|
||||
} else if (strcmp(axis, "Y") == 0) {
|
||||
bytecode[Y] = floatValue;
|
||||
bufcmd.arg2 = floatValue;
|
||||
} else if (strcmp(axis, "Z") == 0) {
|
||||
bytecode[Z] = floatValue;
|
||||
bufcmd.arg3 = floatValue;
|
||||
}
|
||||
}
|
||||
if (strcmp(command, "G00") == 0) {
|
||||
bytecode[0] = G00;
|
||||
return bytecode;
|
||||
bufcmd.type = CommandType::G00;
|
||||
return bufcmd;
|
||||
}
|
||||
if (strcmp(command, "G01") == 0) {
|
||||
bytecode[0] = G01;
|
||||
return bytecode;
|
||||
bufcmd.type = CommandType::G01;
|
||||
return bufcmd;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(command, "M99") == 0) {
|
||||
bytecode[0] = M99;
|
||||
return bytecode;
|
||||
bufcmd.type = CommandType::M99;
|
||||
return bufcmd;
|
||||
}
|
||||
|
||||
bytecode[0] = unk;
|
||||
return bytecode;
|
||||
bufcmd.type = CommandType::unk;
|
||||
return bufcmd;
|
||||
}
|
||||
10
Firmware/EggbotWireless/src/Power.cpp
Normal file
10
Firmware/EggbotWireless/src/Power.cpp
Normal 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); }
|
||||
@@ -8,13 +8,18 @@
|
||||
String inString;
|
||||
bool waitingForNext = false;
|
||||
|
||||
void sendCommand(float *command) {
|
||||
if (command[0] != unk) {
|
||||
void execCommand(Command command) {
|
||||
if (command.type != CommandType::unk) {
|
||||
Wire.beginTransmission(8);
|
||||
byte txBuffer[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
floatToBytes(txBuffer, command[i]);
|
||||
Wire.write(txBuffer, 4);
|
||||
byte buffer[7][sizeof(float)];
|
||||
command.toBytes(buffer[0]);
|
||||
for (int i = 0; i < 7; i++) {
|
||||
/*
|
||||
float dbg;
|
||||
bytesToFloat(&dbg, buffer[i]);
|
||||
Serial.println(dbg);
|
||||
*/
|
||||
Wire.write(buffer[i], sizeof(float));
|
||||
}
|
||||
Wire.endTransmission();
|
||||
} else {
|
||||
@@ -22,24 +27,23 @@ void sendCommand(float *command) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (command[0] == G01 || command[0] == G00) {
|
||||
if (command.type == CommandType::G01 || command.type == CommandType::G00) {
|
||||
Wire.requestFrom(8, 1);
|
||||
waitingForNext = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (command[0] == M99) {
|
||||
if (command.type == CommandType::M99) {
|
||||
Wire.requestFrom(8, 5 * sizeof(float));
|
||||
|
||||
float resp[5];
|
||||
byte buffer[4];
|
||||
byte buffer[sizeof(float)];
|
||||
|
||||
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()) {
|
||||
}
|
||||
char read = Wire.read();
|
||||
buffer[j] = read;
|
||||
buffer[j] = Wire.read();
|
||||
}
|
||||
bytesToFloat(&resp[i], buffer);
|
||||
}
|
||||
@@ -69,7 +73,7 @@ void loop() {
|
||||
|
||||
if (inChar == '\n') {
|
||||
inString.trim();
|
||||
sendCommand(parseGCode(inString));
|
||||
execCommand(parseGCode(inString));
|
||||
unsigned long reqTime = millis();
|
||||
while (waitingForNext) {
|
||||
while (!Wire.available()) {
|
||||
|
||||
@@ -34,44 +34,44 @@ void adjustRPM() {
|
||||
sei();
|
||||
}
|
||||
|
||||
int curFloat = 0;
|
||||
float command[4];
|
||||
Command command;
|
||||
|
||||
bool newCommand = false;
|
||||
bool executing = false;
|
||||
|
||||
int curByte = 0;
|
||||
byte rxBuffer[4];
|
||||
int curFloat = 0;
|
||||
byte rxBuffer[7][sizeof(float)];
|
||||
void receiveEvent(int howMany) {
|
||||
while (Wire.available() > 0) {
|
||||
if (!newCommand) {
|
||||
char c = Wire.read();
|
||||
rxBuffer[curByte] = c;
|
||||
rxBuffer[curFloat][curByte] = c;
|
||||
curByte++;
|
||||
if (curByte == 4) {
|
||||
curByte = 0;
|
||||
bytesToFloat(&command[curFloat], rxBuffer);
|
||||
curFloat++;
|
||||
}
|
||||
if (curFloat == 4) {
|
||||
if (curFloat == 7) {
|
||||
curFloat = 0;
|
||||
command.fromBytes(rxBuffer[0]);
|
||||
newCommand = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte txBuffer[5 * sizeof(float)];
|
||||
byte txBuffer[5][sizeof(float)];
|
||||
void requestEvent() {
|
||||
if (command[0] == M99) {
|
||||
floatToBytes(&txBuffer[0], servoStepper.getPos());
|
||||
floatToBytes(&txBuffer[sizeof(float)], eggStepper.getPos());
|
||||
if (command.type == CommandType::M99) {
|
||||
floatToBytes(txBuffer[0], servoStepper.getPos());
|
||||
floatToBytes(txBuffer[1], eggStepper.getPos());
|
||||
|
||||
floatToBytes(&txBuffer[sizeof(float) * 2], servoStepper.getPosMm());
|
||||
floatToBytes(&txBuffer[sizeof(float) * 3], eggStepper.getPosMm());
|
||||
floatToBytes(txBuffer[2], servoStepper.getPosMm());
|
||||
floatToBytes(txBuffer[3], eggStepper.getPosMm());
|
||||
|
||||
floatToBytes(&txBuffer[sizeof(float) * 4], (float)pen.getEngaged());
|
||||
Wire.write(txBuffer, 5 * sizeof(float));
|
||||
floatToBytes(txBuffer[4], (float)pen.getEngaged());
|
||||
Wire.write(txBuffer[0], 5 * sizeof(float));
|
||||
} else if (executing || newCommand) {
|
||||
Wire.write(WAIT);
|
||||
} else {
|
||||
@@ -81,26 +81,27 @@ void requestEvent() {
|
||||
|
||||
void execCommand() {
|
||||
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;
|
||||
} else {
|
||||
needAdjust = false;
|
||||
}
|
||||
|
||||
if (!isnan(command[X])) {
|
||||
servoStepper.moveTo(command[X]);
|
||||
if (!isnan(command.arg1)) {
|
||||
servoStepper.moveTo(command.arg1);
|
||||
}
|
||||
|
||||
if (!isnan(command[Y])) {
|
||||
eggStepper.moveTo(command[Y]);
|
||||
if (!isnan(command.arg2)) {
|
||||
eggStepper.moveTo(command.arg2);
|
||||
}
|
||||
|
||||
if (!isnan(command[Z])) {
|
||||
if (command[Z] < 0) {
|
||||
if (!isnan(command.arg3)) {
|
||||
if (command.arg3 < 0) {
|
||||
pen.engage();
|
||||
}
|
||||
if (command[Z] >= 0) {
|
||||
if (command.arg3 >= 0) {
|
||||
pen.disengage();
|
||||
}
|
||||
}
|
||||
@@ -127,7 +128,7 @@ void setup() {
|
||||
pen.init();
|
||||
}
|
||||
|
||||
unsigned int ms = 0;
|
||||
volatile unsigned int ms = 0;
|
||||
|
||||
ISR(TIMER0_COMPA_vect) {
|
||||
ms++;
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
#ifndef COMMANDS_H
|
||||
#define COMMANDS_H
|
||||
|
||||
enum command{
|
||||
unk,
|
||||
G00,
|
||||
G01,
|
||||
M99,
|
||||
};
|
||||
|
||||
enum bcAxis {
|
||||
X = 1,
|
||||
Y = 2,
|
||||
@@ -27,6 +20,42 @@ enum StatusMSG {
|
||||
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 floatToBytes(byte *target, float val);
|
||||
|
||||
|
||||
@@ -1,11 +1,53 @@
|
||||
#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) {
|
||||
memcpy(target, val, 4);
|
||||
memcpy(target, val, sizeof(float));
|
||||
return;
|
||||
}
|
||||
|
||||
void floatToBytes(byte *target, float val) {
|
||||
memcpy(target, &val, 4);
|
||||
memcpy(target, &val, sizeof(float));
|
||||
return;
|
||||
}
|
||||
Reference in New Issue
Block a user