From b26d8ec771b5d545d148e0148fc421abff143f4c Mon Sep 17 00:00:00 2001 From: BOTAlex Date: Mon, 20 Jan 2025 20:53:54 +0100 Subject: [PATCH] still errors but im falling asleep :( --- .vscode/settings.json | 52 ++++++++++++++++++++++++++- platformio.ini | 3 +- src/ClawMachineOverhaul.ino | 6 ++-- src/ZCodeParser.h | 71 ++++++++++++++++++++++++++----------- 4 files changed, 107 insertions(+), 25 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 53ddc4a..899ce3d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,56 @@ "files.associations": { "*.js": "javascript", "*.h": "cpp", - "string": "cpp" + "string": "cpp", + "vector": "cpp", + "new": "cpp", + "algorithm": "cpp", + "associative_base": "cpp", + "basic_definitions": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "char_traits": "cpp", + "cmath": "cpp", + "complex": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "exception": "cpp", + "func_exception": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "istream_helpers": "cpp", + "iterator": "cpp", + "iterator_base": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "numeric": "cpp", + "ostream": "cpp", + "ostream_helpers": "cpp", + "queue": "cpp", + "serstream": "cpp", + "set": "cpp", + "sstream": "cpp", + "stack": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string_iostream": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "valarray": "cpp" } } \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index eb1bbc1..aa82a86 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,9 +10,10 @@ [env:mega2560] -platform = atmelave +platform = atmelavr board = megaatmega2560 framework = arduino lib_extra_dirs = ~/Documents/Arduino/libraries lib_deps = FreeRTOS +upload_speed = 115200 \ No newline at end of file diff --git a/src/ClawMachineOverhaul.ino b/src/ClawMachineOverhaul.ino index 35e9c80..3cabc56 100644 --- a/src/ClawMachineOverhaul.ino +++ b/src/ClawMachineOverhaul.ino @@ -1,16 +1,18 @@ #include -#include +// #include #include "MotorControlPart.h" #include "ZCodeParser.h" void setup() { Serial.begin(115200); + delayMicroseconds(1000); + // setupMotor(); // Create tasks // xTaskCreate(MotorControlTask, "Motor", 200, NULL, 10, NULL); - ZCodeParser::ParseString("G0 Z2 ", void, void); + ZCodeParser::ParseString("G0 E20 X40; Comment test\nG1 Y50 X69\nG0 Y235 E5532"); } void loop() { diff --git a/src/ZCodeParser.h b/src/ZCodeParser.h index 2a6d283..77eaa30 100644 --- a/src/ZCodeParser.h +++ b/src/ZCodeParser.h @@ -2,44 +2,73 @@ #include typedef struct { - char Axis; - int time; -} MoveCommand; + String command; + std::vector params; +} GCommand; -class ZCodeParser +class ZCodeParser // Variant of GCode, but made by Zhen. Lol { private: + static std::vector splitString(const String &input, char delimiter); public: - static void ParseString(String inString, MoveCommand *outMovecommands, int *numOfCommands); + static std::vector ParseString(const String inString); }; -void ZCodeParser::ParseString(String inString, MoveCommand *outMovecommands, int *numOfCommands){ - // G0 = move, no block - // G1 = move, block - // ; = comments - // \n = new command - - std::vector stringCommands; +std::vector ZCodeParser::splitString(const String &input, char delimiter) { + std::vector result; int startIndex = 0; int endIndex; - while (true) - { - endIndex = inString.indexOf('\n', startIndex); - if (endIndex == -1){ + while (true) { + endIndex = input.indexOf(delimiter, startIndex); + if (endIndex == -1) { + result.push_back(input.substring(startIndex)); // Get the rest of the string break; } + result.push_back(input.substring(startIndex, endIndex)); + startIndex = endIndex + 1; + } - stringCommands.push_back(inString.substring(startIndex, endIndex)); - startIndex = endIndex; + return result; +} + +std::vector ZCodeParser::ParseString(const String inString){ + // G0 = move, no block + // G1 = move, block + // M17 = Enable Stepper + // M18 = Disable Stepper + // ; = comments + // \n = new command + + std::vector stringCommands = ZCodeParser::splitString(inString, '\n'); + + // Remove comments + for (size_t i = 0; i < stringCommands.size(); i++) + { + int commentIndex = stringCommands[i].indexOf(';'); + if (commentIndex == -1) continue; // Continue if no comments + + stringCommands[i] = stringCommands[i].substring(0, commentIndex); + } + + // Extract function name and params + std::vector commnads; + for (size_t i = 0; i < stringCommands.size(); i++) + { + std::vector seperatedStrings = ZCodeParser::splitString(stringCommands[i], ' '); + GCommand commnad; + commnad.command = seperatedStrings[0]; + stringCommands.erase(stringCommands.begin()); + commnad.params = stringCommands; } Serial.println("Parsed: "); - for (size_t i = 0; i < stringCommands.size(); i++) + for (size_t i = 0; i < commnads.size(); i++) { - Serial.print(stringCommands[i]); + Serial.print(commnads[i].command); Serial.print(", "); } Serial.println("\n"); - + + // return ; } \ No newline at end of file