From e4e3e5e00a5d2103c73450ba65b95ceb7cb2943a Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Sat, 13 Jul 2019 23:49:15 +0300 Subject: [PATCH] make command a struct --- Firmware/EggbotWireless/include/Config.h | 6 +++ Firmware/EggbotWireless/include/GCodeParser.h | 10 ++-- Firmware/EggbotWireless/include/Power.h | 15 ++++++ Firmware/EggbotWireless/src/GCodeParser.cpp | 31 +++++------ Firmware/EggbotWireless/src/Power.cpp | 10 ++++ Firmware/EggbotWireless/src/main.cpp | 30 ++++++----- Firmware/MotorControl/src/main.cpp | 51 ++++++++++--------- Firmware/common/Commands.h | 45 +++++++++++++--- Firmware/commonSrc/Commands.cpp | 46 ++++++++++++++++- 9 files changed, 174 insertions(+), 70 deletions(-) create mode 100644 Firmware/EggbotWireless/include/Config.h create mode 100644 Firmware/EggbotWireless/include/Power.h create mode 100644 Firmware/EggbotWireless/src/Power.cpp diff --git a/Firmware/EggbotWireless/include/Config.h b/Firmware/EggbotWireless/include/Config.h new file mode 100644 index 0000000..92afee3 --- /dev/null +++ b/Firmware/EggbotWireless/include/Config.h @@ -0,0 +1,6 @@ +#ifndef CONFIG_H +#define CONFIG_H + +constexpr int pin12v {5}; + +#endif // CONFIG_H \ No newline at end of file diff --git a/Firmware/EggbotWireless/include/GCodeParser.h b/Firmware/EggbotWireless/include/GCodeParser.h index a692701..5e35591 100644 --- a/Firmware/EggbotWireless/include/GCodeParser.h +++ b/Firmware/EggbotWireless/include/GCodeParser.h @@ -1,9 +1,9 @@ -#include -#include "common/Commands.h" - #ifndef PARSER_H #define PARSER_H -float *parseGCode(String gcode); +#include +#include "common/Commands.h" -#endif \ No newline at end of file +Command parseGCode(String gcode); + +#endif // PARSER_H \ No newline at end of file diff --git a/Firmware/EggbotWireless/include/Power.h b/Firmware/EggbotWireless/include/Power.h new file mode 100644 index 0000000..532327e --- /dev/null +++ b/Firmware/EggbotWireless/include/Power.h @@ -0,0 +1,15 @@ +#ifndef POWER_H +#define POWER_H + +#include + +#include "Config.h" + +class Power { + public: + Power(); + void disable12v(); + void enable12v(); +}; + +#endif // POWER_H \ No newline at end of file diff --git a/Firmware/EggbotWireless/src/GCodeParser.cpp b/Firmware/EggbotWireless/src/GCodeParser.cpp index 1179cc4..08d9657 100644 --- a/Firmware/EggbotWireless/src/GCodeParser.cpp +++ b/Firmware/EggbotWireless/src/GCodeParser.cpp @@ -1,13 +1,10 @@ -#include "GCodeParser.h" #include -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; } \ No newline at end of file diff --git a/Firmware/EggbotWireless/src/Power.cpp b/Firmware/EggbotWireless/src/Power.cpp new file mode 100644 index 0000000..4682933 --- /dev/null +++ b/Firmware/EggbotWireless/src/Power.cpp @@ -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); } \ No newline at end of file diff --git a/Firmware/EggbotWireless/src/main.cpp b/Firmware/EggbotWireless/src/main.cpp index ada497a..56fdaab 100644 --- a/Firmware/EggbotWireless/src/main.cpp +++ b/Firmware/EggbotWireless/src/main.cpp @@ -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()) { diff --git a/Firmware/MotorControl/src/main.cpp b/Firmware/MotorControl/src/main.cpp index 3ccc73f..e02ca18 100644 --- a/Firmware/MotorControl/src/main.cpp +++ b/Firmware/MotorControl/src/main.cpp @@ -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,11 +128,11 @@ void setup() { pen.init(); } -unsigned int ms = 0; +volatile unsigned int ms = 0; ISR(TIMER0_COMPA_vect) { ms++; - if (ms % adjustDelay == 0) { + if (ms % adjustDelay == 0) { adjustRPM(); } if (ms % eggStepperDelay == 0) { diff --git a/Firmware/common/Commands.h b/Firmware/common/Commands.h index 1b7bda4..b241d28 100644 --- a/Firmware/common/Commands.h +++ b/Firmware/common/Commands.h @@ -1,14 +1,7 @@ #ifndef COMMANDS_H #define COMMANDS_H -enum command{ - unk, - G00, - G01, - M99, -}; - -enum bcAxis{ +enum bcAxis { X = 1, Y = 2, Z = 3, @@ -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); diff --git a/Firmware/commonSrc/Commands.cpp b/Firmware/commonSrc/Commands.cpp index 6a36771..88798d1 100644 --- a/Firmware/commonSrc/Commands.cpp +++ b/Firmware/commonSrc/Commands.cpp @@ -1,11 +1,53 @@ #include +#include "common/Commands.h" + +Command::Command(float *floats) { fromFloats(floats); } + +int Command::fromFloats(float *floats) { + type = static_cast(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(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; } \ No newline at end of file