diff --git a/include/EggDuino.h b/include/EggDuino.h index 61b57d3..4b3703a 100644 --- a/include/EggDuino.h +++ b/include/EggDuino.h @@ -21,35 +21,39 @@ #ifdef ESP32 // Rotational Stepper -#define dir1 16 -#define enableRotMotor 12 -#define step1 26 +static const int kDefaultRotDirPin = 16; +static const int kDefaultRotEnablePin = 12; +static const int kDefaultRotStepPin = 26; #define rotMicrostep 32 +static const int kDefaultRotMicrostep = rotMicrostep; // Pen Stepper -#define step2 25 -#define dir2 27 -#define enablePenMotor 12 +static const int kDefaultPenStepPin = 25; +static const int kDefaultPenDirPin = 27; +static const int kDefaultPenEnablePin = 12; #define penMicrostep 32 +static const int kDefaultPenMicrostep = penMicrostep; -#define servoPin 17 +static const int kDefaultServoPin = 17; #else // Rotational Stepper -#define step1 2 -#define dir1 5 -#define enableRotMotor 8 +static const int kDefaultRotStepPin = 2; +static const int kDefaultRotDirPin = 5; +static const int kDefaultRotEnablePin = 8; #define rotMicrostep 16 +static const int kDefaultRotMicrostep = rotMicrostep; // Pen Stepper -#define step2 3 -#define dir2 6 -#define enablePenMotor 8 +static const int kDefaultPenStepPin = 3; +static const int kDefaultPenDirPin = 6; +static const int kDefaultPenEnablePin = 8; #define penMicrostep 16 +static const int kDefaultPenMicrostep = penMicrostep; -#define servoPin 4 +static const int kDefaultServoPin = 4; #endif @@ -81,6 +85,15 @@ extern boolean g_bPrgButtonState; extern float fROT_STEP_CORRECTION; extern float fPEN_STEP_CORRECTION; extern boolean g_bMotorsEnabled; +extern int g_iRotDirPin; +extern int g_iRotEnablePin; +extern int g_iRotStepPin; +extern int g_iPenStepPin; +extern int g_iPenDirPin; +extern int g_iPenEnablePin; +extern int g_iServoPin; +extern int g_iRotMicrostep; +extern int g_iPenMicrostep; extern ConfigParameter configParameters[]; extern const size_t configParameterCount; @@ -100,6 +113,7 @@ bool parseSMArgs(uint16_t *duration, int *penStepsEBB, int *rotStepsEBB); void prepareMove(uint16_t duration, int penStepsEBB, int rotStepsEBB); void storePenUpPosInEE(); void storePenDownPosInEE(); +void updateStepCorrectionFactors(); bool initConfigStore(); bool loadConfigFromFile(); diff --git a/src/Config_Web.cpp b/src/Config_Web.cpp index a53a81f..bce888e 100644 --- a/src/Config_Web.cpp +++ b/src/Config_Web.cpp @@ -5,6 +5,7 @@ namespace { const char *kConfigPath = "/config.json"; + const size_t kConfigJsonCapacity = 4096; WebServer server(80); bool configStoreReady = false; @@ -188,7 +189,6 @@ async function pollLogs() { return; } - Log(String("Config gespeichert: penUpPos=") + g_iPenUpPos + ", penDownPos=" + g_iPenDownPos); server.send(200, "application/json", buildConfigJson()); } @@ -214,8 +214,17 @@ async function pollLogs() { } // namespace ConfigParameter configParameters[] = { - // {"penUpPos", &g_iPEN_UP_POS, "Pen Up Position", 5}, - // {"penDownPos", &g_iPEN_DOWN_POS, "Pen Down Position", 20}, + {"penUpPos", &g_iPenUpPos, "Pen Up Position", 40}, + {"penDownPos", &g_iPenDownPos, "Pen Down Position", 10}, + {"rotStepPin", &g_iRotStepPin, "Rotational Stepper Step Pin", kDefaultRotStepPin}, + {"rotDirPin", &g_iRotDirPin, "Rotational Stepper Direction Pin", kDefaultRotDirPin}, + {"rotEnablePin", &g_iRotEnablePin, "Rotational Stepper Enable Pin", kDefaultRotEnablePin}, + {"penStepPin", &g_iPenStepPin, "Pen Stepper Step Pin", kDefaultPenStepPin}, + {"penDirPin", &g_iPenDirPin, "Pen Stepper Direction Pin", kDefaultPenDirPin}, + {"penEnablePin", &g_iPenEnablePin, "Pen Stepper Enable Pin", kDefaultPenEnablePin}, + {"rotMicrostep", &g_iRotMicrostep, "Rotational Stepper Microsteps", kDefaultRotMicrostep}, + {"penMicrostep", &g_iPenMicrostep, "Pen Stepper Microsteps", kDefaultPenMicrostep}, + {"servoPin", &g_iServoPin, "Servo Pin", kDefaultServoPin}, }; const size_t configParameterCount = sizeof(configParameters) / sizeof(configParameters[0]); @@ -242,12 +251,12 @@ bool loadConfigFromFile() return saveConfigToFile(); } - StaticJsonDocument<1024> doc; + StaticJsonDocument doc; DeserializationError err = deserializeJson(doc, file); file.close(); if (err) { - Log("config.json ist ungueltig, defaults werden gespeichert"); + Log(String("config.json ist ungueltig (") + err.c_str() + "), defaults werden gespeichert"); return saveConfigToFile(); } @@ -273,6 +282,7 @@ bool loadConfigFromFile() param->description = item["description"].as(); } } + updateStepCorrectionFactors(); Log(String("Config geladen: penUpPos=") + g_iPenUpPos + ", penDownPos=" + g_iPenDownPos); return true; @@ -287,7 +297,7 @@ bool saveConfigToFile() return false; } - StaticJsonDocument<1024> doc; + StaticJsonDocument doc; JsonArray params = doc.createNestedArray("parameters"); for (size_t i = 0; i < configParameterCount; ++i) { @@ -296,6 +306,12 @@ bool saveConfigToFile() item["value"] = *configParameters[i].value; item["description"] = configParameters[i].description; } + if (doc.overflowed()) + { + Log("Config JSON Dokument zu klein beim Speichern"); + file.close(); + return false; + } bool ok = serializeJsonPretty(doc, file) > 0; file.flush(); @@ -309,7 +325,7 @@ bool saveConfigToFile() String buildConfigJson() { - StaticJsonDocument<1024> doc; + StaticJsonDocument doc; JsonArray params = doc.createNestedArray("parameters"); for (size_t i = 0; i < configParameterCount; ++i) { @@ -318,6 +334,11 @@ String buildConfigJson() item["value"] = *configParameters[i].value; item["description"] = configParameters[i].description; } + if (doc.overflowed()) + { + Log("Config JSON Dokument zu klein beim Lesen"); + return String("{\"parameters\":[]}"); + } String output; serializeJson(doc, output); @@ -326,7 +347,7 @@ String buildConfigJson() bool applyConfigJson(const String &payload, String &errorMessage) { - StaticJsonDocument<1024> doc; + StaticJsonDocument doc; DeserializationError err = deserializeJson(doc, payload); if (err) { @@ -361,6 +382,7 @@ bool applyConfigJson(const String &payload, String &errorMessage) param->description = item["description"].as(); } } + updateStepCorrectionFactors(); return true; } diff --git a/src/Helper_Functions.cpp b/src/Helper_Functions.cpp index e2b316a..77d1cdb 100644 --- a/src/Helper_Functions.cpp +++ b/src/Helper_Functions.cpp @@ -1,5 +1,19 @@ #include "EggDuino.h" +void updateStepCorrectionFactors() +{ + if (g_iRotMicrostep <= 0) + { + g_iRotMicrostep = kDefaultRotMicrostep; + } + if (g_iPenMicrostep <= 0) + { + g_iPenMicrostep = kDefaultPenMicrostep; + } + fROT_STEP_CORRECTION = 16.0f / (float)g_iRotMicrostep; + fPEN_STEP_CORRECTION = 16.0f / (float)g_iPenMicrostep; +} + void initHardware() { if (!initConfigStore()) @@ -7,35 +21,36 @@ void initHardware() g_iPenUpPos = 5; g_iPenDownPos = 20; } + updateStepCorrectionFactors(); g_iPenState = g_iPenUpPos; g_stepEngine.init(); - g_pStepperRotate = g_stepEngine.stepperConnectToPin(step1); + g_pStepperRotate = g_stepEngine.stepperConnectToPin(g_iRotStepPin); if (g_pStepperRotate) { // rotMotor.setMaxSpeed(2000.0); // rotMotor.setAcceleration(10000.0); - g_pStepperRotate->setDirectionPin(dir1); - g_pStepperRotate->setEnablePin(enableRotMotor); + g_pStepperRotate->setDirectionPin(g_iRotDirPin); + g_pStepperRotate->setEnablePin(g_iRotEnablePin); g_pStepperRotate->setAcceleration(10000); g_pStepperRotate->setAutoEnable(false); } // Stepper pen init - g_pStepperPen = g_stepEngine.stepperConnectToPin(step2); + g_pStepperPen = g_stepEngine.stepperConnectToPin(g_iPenStepPin); if (g_pStepperPen) { // penMotor.setMaxSpeed(2000.0); // penMotor.setAcceleration(10000.0); - g_pStepperPen->setDirectionPin(dir2); - g_pStepperPen->setEnablePin(enablePenMotor); + g_pStepperPen->setDirectionPin(g_iPenDirPin); + g_pStepperPen->setEnablePin(g_iPenEnablePin); g_pStepperPen->setAcceleration(10000); g_pStepperPen->setAutoEnable(false); } motorsOff(); - penServo.attach(servoPin); + penServo.attach(g_iServoPin); penServo.write(g_iPenState); } @@ -51,19 +66,19 @@ void storePenDownPosInEE() void sendAck() { - Log(__FUNCTION__); + Log(__FUNCTION__); Serial.print("OK\r\n"); } void sendError() { - Log(__FUNCTION__); + Log(__FUNCTION__); Serial.print("unknown CMD\r\n"); } void motorsOff() { - Log(__FUNCTION__); + Log(__FUNCTION__); g_pStepperPen->disableOutputs(); g_pStepperRotate->disableOutputs(); g_bMotorsEnabled = 0; @@ -71,7 +86,7 @@ void motorsOff() void motorsOn() { - Log(__FUNCTION__); + Log(__FUNCTION__); g_pStepperPen->enableOutputs(); g_pStepperRotate->enableOutputs(); g_bMotorsEnabled = 1; @@ -79,7 +94,7 @@ void motorsOn() void toggleMotors() { - Log(__FUNCTION__); + Log(__FUNCTION__); if (g_bMotorsEnabled) { motorsOff(); @@ -123,16 +138,15 @@ void prepareMove(uint16_t duration, int penStepsEBB, int rotStepsEBB) motorsOn(); } + if ((1 == fROT_STEP_CORRECTION) && (1 == fPEN_STEP_CORRECTION)) + { // if coordinatessystems are identical + // set Coordinates and Speed + g_pStepperRotate->setSpeedInTicks(abs((float)rotStepsEBB * (float)1000 / (float)duration)); + g_pStepperRotate->move(rotStepsEBB); - if ((1 == fROT_STEP_CORRECTION) && (1 == fPEN_STEP_CORRECTION)) - { // if coordinatessystems are identical - // set Coordinates and Speed - g_pStepperRotate->setSpeedInTicks(abs((float)rotStepsEBB * (float)1000 / (float)duration)); - g_pStepperRotate->move(rotStepsEBB); - - g_pStepperPen->setSpeedInTicks(abs((float)penStepsEBB * (float)1000 / (float)duration)); - g_pStepperPen->move(penStepsEBB); - } + g_pStepperPen->setSpeedInTicks(abs((float)penStepsEBB * (float)1000 / (float)duration)); + g_pStepperPen->move(penStepsEBB); + } else { // incoming EBB-Steps will be multiplied by 16, then Integer-maths is done, result will be divided by 16 @@ -150,25 +164,27 @@ void prepareMove(uint16_t duration, int penStepsEBB, int rotStepsEBB) long temp_penSpeed = ((long)penStepsToGo * (long)1000 / (long)duration); float rotSpeed = (float)abs(temp_rotSpeed); // type cast - float penSpeed = (float)abs(temp_penSpeed); + float penSpeed = (float)abs(temp_penSpeed); - // set Coordinates and Speed - g_pStepperRotate->setSpeedInTicks(rotSpeed); - g_pStepperRotate->move(rotStepsToGo); + // set Coordinates and Speed + g_pStepperRotate->setSpeedInTicks(rotSpeed); + g_pStepperRotate->move(rotStepsToGo); - g_pStepperPen->setSpeedInTicks(penSpeed); - g_pStepperPen->move(penStepsToGo); - } + g_pStepperPen->setSpeedInTicks(penSpeed); + g_pStepperPen->move(penStepsToGo); } +} void moveOneStep() { - while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning()); + while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning()) + ; } void moveToDestination() { - while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning()); + while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning()) + ; } void setprgButtonState() diff --git a/src/main.cpp b/src/main.cpp index ce6d084..b631998 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,9 +59,18 @@ int g_iPenState = g_iPenUpPos; uint32_t g_uiNodeCount = 0; unsigned int g_uiLayer = 0; boolean g_bPrgButtonState = 0; -float fROT_STEP_CORRECTION = 16.0 / rotMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps -float fPEN_STEP_CORRECTION = 16.0 / penMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps boolean g_bMotorsEnabled = 0; +int g_iRotDirPin = kDefaultRotDirPin; +int g_iRotEnablePin = kDefaultRotEnablePin; +int g_iRotStepPin = kDefaultRotStepPin; +int g_iPenStepPin = kDefaultPenStepPin; +int g_iPenDirPin = kDefaultPenDirPin; +int g_iPenEnablePin = kDefaultPenEnablePin; +int g_iServoPin = kDefaultServoPin; +int g_iRotMicrostep = kDefaultRotMicrostep; +int g_iPenMicrostep = kDefaultPenMicrostep; +float fROT_STEP_CORRECTION = 16.0 / kDefaultRotMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps +float fPEN_STEP_CORRECTION = 16.0 / kDefaultPenMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps // Stepper Test #ifdef TEST @@ -75,9 +84,9 @@ void setup() Serial.begin(115200); Serial.println("Starting..."); Log("Starting..."); + startWebInterface(); makeComInterface(); initHardware(); - startWebInterface(); } uint8_t g_uiState = 0;