diff --git a/Firmware/EggbotWireless/include/Config.h b/Firmware/EggbotWireless/include/Config.h index 92afee3..f6bae29 100644 --- a/Firmware/EggbotWireless/include/Config.h +++ b/Firmware/EggbotWireless/include/Config.h @@ -1,6 +1,9 @@ #ifndef CONFIG_H #define CONFIG_H -constexpr int pin12v {5}; +constexpr int pin12v{5}; +constexpr int i2cTimeout{10}, i2cTimeoutTries{10}; +constexpr int lastStsTTL{1}; +constexpr unsigned long powerTimeout{20000}, powerStartupDelay{100}; -#endif // CONFIG_H \ No newline at end of file +#endif // CONFIG_H \ No newline at end of file diff --git a/Firmware/EggbotWireless/include/ConfigManager.h b/Firmware/EggbotWireless/include/ConfigManager.h new file mode 100644 index 0000000..7b9843f --- /dev/null +++ b/Firmware/EggbotWireless/include/ConfigManager.h @@ -0,0 +1,6 @@ +#ifndef CONFIG_MANAGER_H +#define CONFIG_MANAGER_H + + + +#endif // CONFIG_MANAGER_H \ No newline at end of file diff --git a/Firmware/EggbotWireless/include/Executor.h b/Firmware/EggbotWireless/include/Executor.h index cd96034..2a9c043 100644 --- a/Firmware/EggbotWireless/include/Executor.h +++ b/Firmware/EggbotWireless/include/Executor.h @@ -12,7 +12,6 @@ class Executor private: unsigned long lastStsTime; I2CStatusMsg lastSts; - static constexpr int lastStsTTL = 1; public: Executor(/* args */); void execCommand(Command command); diff --git a/Firmware/EggbotWireless/src/Config.cpp b/Firmware/EggbotWireless/src/ConfigManager.cpp similarity index 100% rename from Firmware/EggbotWireless/src/Config.cpp rename to Firmware/EggbotWireless/src/ConfigManager.cpp diff --git a/Firmware/EggbotWireless/src/Executor.cpp b/Firmware/EggbotWireless/src/Executor.cpp index 0751265..6129a01 100644 --- a/Firmware/EggbotWireless/src/Executor.cpp +++ b/Firmware/EggbotWireless/src/Executor.cpp @@ -1,36 +1,36 @@ #include "Executor.h" +#include "Config.h" +#include "common/Commands.h" Executor executor; Executor::Executor() {} void Executor::execCommand(Command command) { - if (command.type != CommandType::unk) { - Wire.beginTransmission(8); - byte buffer[7][sizeof(float)]; - command.toBytes(buffer[0]); - for (int i = 0; i < 7; i++) { - Wire.write(buffer[i], sizeof(float)); - } - Wire.endTransmission(); - } else { + if (command.type == CommandType::unk) { Serial.println("OK"); return; } + Wire.beginTransmission(8); + byte buffer[i2cCmdBytes]; + command.toBytes(buffer); + Wire.write(buffer, i2cCmdBytes); + Wire.endTransmission(); + if (command.type == CommandType::G01 || command.type == CommandType::G00) { return; } if (command.type == CommandType::M99) { delay(10); - Wire.requestFrom(8, 5 * sizeof(float)); + Wire.requestFrom(8, 5 * i2cFloatSize); float resp[5]; - byte buffer[sizeof(float)]; + byte buffer[i2cFloatSize]; for (int i = 0; i < 5; i++) { - for (unsigned int j = 0; j < sizeof(float); j++) { + for (unsigned int j = 0; j < i2cFloatSize; j++) { while (!Wire.available()) { } buffer[j] = Wire.read(); @@ -68,7 +68,7 @@ I2CStatusMsg Executor::status() { Wire.requestFrom(8, 1); while (!Wire.available()) { - if (millis() - reqTime > 10 && tries < 10) { + if (millis() - reqTime > i2cTimeout && tries < i2cTimeoutTries) { Wire.requestFrom(8, 1); tries++; reqTime = millis(); diff --git a/Firmware/EggbotWireless/src/Power.cpp b/Firmware/EggbotWireless/src/Power.cpp index 4a60772..68d7541 100644 --- a/Firmware/EggbotWireless/src/Power.cpp +++ b/Firmware/EggbotWireless/src/Power.cpp @@ -1,10 +1,9 @@ #include "Power.h" #include "Globals.h" +#include "Config.h" Power power; -constexpr unsigned long commandTimeout = 20000; - Power::Power() { pinMode(pin12v, OUTPUT); disable12v(); @@ -12,7 +11,7 @@ Power::Power() { } void Power::tickerRoutine() { - if (millis() - lastCmdTime > commandTimeout) { + if (millis() - lastCmdTime > powerTimeout) { disable12v(); } } @@ -33,6 +32,6 @@ void Power::commandHook() { lastCmdTime = millis(); if (!isEnabled12v()) { enable12v(); - delay(100); + delay(powerStartupDelay); } } \ No newline at end of file diff --git a/Firmware/MotorControl/src/main.cpp b/Firmware/MotorControl/src/main.cpp index e86d0c3..6a2cd2b 100644 --- a/Firmware/MotorControl/src/main.cpp +++ b/Firmware/MotorControl/src/main.cpp @@ -41,39 +41,34 @@ Command command; bool newCommand = false; bool executing = false; -byte rxBuffer[7][sizeof(float)]; +byte rxBuffer[i2cCmdBytes]; void receiveEvent(int howMany) { static int curByte = 0; - static int curFloat = 0; while (Wire.available() > 0) { if (!newCommand) { char c = Wire.read(); - rxBuffer[curFloat][curByte] = c; + rxBuffer[curByte] = c; curByte++; - if (curByte == 4) { + if (curByte == i2cCmdBytes) { curByte = 0; - curFloat++; - } - if (curFloat == 7) { - curFloat = 0; - command.fromBytes(rxBuffer[0]); + command.fromBytes(rxBuffer); newCommand = true; } } } } -byte txBuffer[5][sizeof(float)]; +byte txBuffer[5 * i2cFloatSize]; void requestEvent() { if (command.type == CommandType::M99 && newCommand) { - floatToBytes(txBuffer[0], servoStepper.getPos()); - floatToBytes(txBuffer[1], eggStepper.getPos()); + floatToBytes(&txBuffer[0 * i2cFloatSize], servoStepper.getPos()); + floatToBytes(&txBuffer[1 * i2cFloatSize], eggStepper.getPos()); - floatToBytes(txBuffer[2], servoStepper.getPosMm()); - floatToBytes(txBuffer[3], eggStepper.getPosMm()); + floatToBytes(&txBuffer[2 * i2cFloatSize], servoStepper.getPosMm()); + floatToBytes(&txBuffer[3 * i2cFloatSize], eggStepper.getPosMm()); - floatToBytes(txBuffer[4], (float)pen.getEngaged()); - Wire.write(txBuffer[0], 5 * sizeof(float)); + floatToBytes(&txBuffer[4 * i2cFloatSize], (float)pen.getEngaged()); + Wire.write(txBuffer, 5 * i2cFloatSize); newCommand = false; } else if (executing || newCommand) { Wire.write(static_cast(I2CStatusMsg::WAIT)); diff --git a/Firmware/common/Commands.h b/Firmware/common/Commands.h index 10973b3..d2481d1 100644 --- a/Firmware/common/Commands.h +++ b/Firmware/common/Commands.h @@ -1,6 +1,9 @@ #ifndef COMMANDS_H #define COMMANDS_H +constexpr int i2cFloatSize{4}, i2cCmdFloats{7}, + i2cCmdBytes{i2cFloatSize * i2cCmdFloats}; + enum bcAxis { X = 1, Y = 2, @@ -50,12 +53,12 @@ struct Command { arg6(arg6){}; Command(float *floats); - int fromFloats(float *floats); - int toFloats(float *floats); + void fromFloats(float *floats); + void toFloats(float *floats); Command(byte *bytes); - int fromBytes(byte *bytes); - int toBytes(byte *bytes); + void fromBytes(byte *bytes); + void toBytes(byte *bytes); }; void bytesToFloat(float *target, byte *val); diff --git a/Firmware/commonSrc/Commands.cpp b/Firmware/commonSrc/Commands.cpp index 88798d1..9f152fc 100644 --- a/Firmware/commonSrc/Commands.cpp +++ b/Firmware/commonSrc/Commands.cpp @@ -4,7 +4,7 @@ Command::Command(float *floats) { fromFloats(floats); } -int Command::fromFloats(float *floats) { +void Command::fromFloats(float *floats) { type = static_cast(floats[0]); arg1 = floats[1]; arg2 = floats[2]; @@ -14,7 +14,7 @@ int Command::fromFloats(float *floats) { arg6 = floats[6]; } -int Command::toFloats(float *floats) { +void Command::toFloats(float *floats) { floats[0] = static_cast(type); floats[1] = arg1; floats[2] = arg2; @@ -26,28 +26,28 @@ int Command::toFloats(float *floats) { 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)]); +void Command::fromBytes(byte *bytes) { + float floats[i2cCmdFloats]; + for (int i = 0; i < i2cCmdFloats; i++) { + bytesToFloat(&floats[i], &bytes[i * i2cFloatSize]); } fromFloats(floats); } -int Command::toBytes(byte *bytes) { - float floats[7]; +void Command::toBytes(byte *bytes) { + float floats[i2cCmdFloats]; toFloats(floats); for (int i = 0; i < 7; i++) { - floatToBytes(&bytes[i * sizeof(float)], floats[i]); + floatToBytes(&bytes[i * i2cFloatSize], floats[i]); } } void bytesToFloat(float *target, byte *val) { - memcpy(target, val, sizeof(float)); + memcpy(target, val, i2cFloatSize); return; } void floatToBytes(byte *target, float val) { - memcpy(target, &val, sizeof(float)); + memcpy(target, &val, i2cFloatSize); return; } \ No newline at end of file