From 71615ead86f6392b51ba97f18fae0b41b6fb3a69 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 22 Jan 2026 23:15:20 +0300 Subject: [PATCH] init --- .gitignore | 5 ++ .vscode/extensions.json | 10 +++ README.md | 0 include/README | 37 ++++++++++ lib/README | 46 +++++++++++++ platformio.ini | 19 ++++++ src/actuators.cpp | 59 ++++++++++++++++ src/actuators.h | 11 +++ src/config.h | 23 +++++++ src/controller.cpp | 39 +++++++++++ src/controller.h | 9 +++ src/main.cpp | 20 ++++++ src/robot_state.cpp | 18 +++++ src/robot_state.h | 22 ++++++ src/web_server.cpp | 109 ++++++++++++++++++++++++++++++ src/web_server.h | 3 + src/web_ui.cpp | 146 ++++++++++++++++++++++++++++++++++++++++ src/web_ui.h | 4 ++ test/README | 11 +++ 19 files changed, 591 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 README.md create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/actuators.cpp create mode 100644 src/actuators.h create mode 100644 src/config.h create mode 100644 src/controller.cpp create mode 100644 src/controller.h create mode 100644 src/main.cpp create mode 100644 src/robot_state.cpp create mode 100644 src/robot_state.h create mode 100644 src/web_server.cpp create mode 100644 src/web_server.h create mode 100644 src/web_ui.cpp create mode 100644 src/web_ui.h create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..d9d88c8 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +monitor_speed = 115200 + +lib_deps = + adafruit/Adafruit GFX Library + adafruit/Adafruit SSD1306 \ No newline at end of file diff --git a/src/actuators.cpp b/src/actuators.cpp new file mode 100644 index 0000000..15774a6 --- /dev/null +++ b/src/actuators.cpp @@ -0,0 +1,59 @@ +#include +#include "config.h" +#include "actuators.h" + +void actuatorsInit() { + pinMode(PIN_LB, OUTPUT); + pinMode(PIN_LF, OUTPUT); + pinMode(PIN_RB, OUTPUT); + pinMode(PIN_RF, OUTPUT); + + ledcSetup(PWM_CH_L, PWM_FREQ, PWM_RES); + ledcSetup(PWM_CH_R, PWM_FREQ, PWM_RES); + + ledcAttachPin(L_PWM_PIN, PWM_CH_L); + ledcAttachPin(R_PWM_PIN, PWM_CH_R); + + actuatorsSetSpeed(150, 150); + actuatorsStop(); +} + +void actuatorsStop() { + digitalWrite(PIN_LB, HIGH); + digitalWrite(PIN_LF, HIGH); + digitalWrite(PIN_RB, HIGH); + digitalWrite(PIN_RF, HIGH); +} + +void actuatorsForward() { + digitalWrite(PIN_LB, LOW); + digitalWrite(PIN_LF, HIGH); + digitalWrite(PIN_RB, LOW); + digitalWrite(PIN_RF, HIGH); +} + +void actuatorsBack() { + digitalWrite(PIN_LB, HIGH); + digitalWrite(PIN_LF, LOW); + digitalWrite(PIN_RB, HIGH); + digitalWrite(PIN_RF, LOW); +} + +void actuatorsLeft() { + digitalWrite(PIN_LB, HIGH); + digitalWrite(PIN_LF, LOW); + digitalWrite(PIN_RB, LOW); + digitalWrite(PIN_RF, HIGH); +} + +void actuatorsRight() { + digitalWrite(PIN_LB, LOW); + digitalWrite(PIN_LF, HIGH); + digitalWrite(PIN_RB, HIGH); + digitalWrite(PIN_RF, LOW); +} + +void actuatorsSetSpeed(int l, int r) { + ledcWrite(PWM_CH_L, constrain(l, 0, 255)); + ledcWrite(PWM_CH_R, constrain(r, 0, 255)); +} diff --git a/src/actuators.h b/src/actuators.h new file mode 100644 index 0000000..8d37c8c --- /dev/null +++ b/src/actuators.h @@ -0,0 +1,11 @@ +#pragma once + +void actuatorsInit(); +void actuatorsStop(); + +void actuatorsForward(); +void actuatorsBack(); +void actuatorsLeft(); +void actuatorsRight(); + +void actuatorsSetSpeed(int l, int r); diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..91437fc --- /dev/null +++ b/src/config.h @@ -0,0 +1,23 @@ +#pragma once + +// ===== Wi-Fi ===== +#define WIFI_SSID "Capybara" +#define WIFI_PASS "qq1234567890" + +// ===== Motors ===== +#define L_PWM_PIN 25 +#define R_PWM_PIN 26 + +#define PIN_LB 16 +#define PIN_LF 17 +#define PIN_RB 18 +#define PIN_RF 19 + +#define PWM_CH_L 0 +#define PWM_CH_R 1 + +#define PWM_FREQ 1000 +#define PWM_RES 8 // 0..255 + +// ===== Timing ===== +#define COMMAND_TIMEOUT_MS 600 diff --git a/src/controller.cpp b/src/controller.cpp new file mode 100644 index 0000000..724ffcc --- /dev/null +++ b/src/controller.cpp @@ -0,0 +1,39 @@ +#include "controller.h" +#include "actuators.h" +#include "config.h" + +void controllerSetMode(RobotMode m) { + robot.mode = m; + controllerStop(); + robot.lastCmdMs = millis(); +} + +void controllerStop() { + actuatorsStop(); +} + +bool controllerMove(const String& cmd) { + if (robot.mode != MODE_MANUAL) return false; + + if (cmd == "FWD") actuatorsForward(); + else if (cmd == "BACK") actuatorsBack(); + else if (cmd == "LEFT") actuatorsLeft(); + else if (cmd == "RIGHT") actuatorsRight(); + else return false; + + return true; +} + +void controllerSetSpeed(int l, int r) { + robot.speedL = l; + robot.speedR = r; + actuatorsSetSpeed(l, r); + robot.lastCmdMs = millis(); +} + +void controllerUpdate() { + if (robot.mode == MODE_MANUAL && + millis() - robot.lastCmdMs > COMMAND_TIMEOUT_MS) { + controllerStop(); + } +} diff --git a/src/controller.h b/src/controller.h new file mode 100644 index 0000000..eade9b0 --- /dev/null +++ b/src/controller.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include "robot_state.h" + +void controllerSetMode(RobotMode m); +bool controllerMove(const String& cmd); +void controllerStop(); +void controllerSetSpeed(int l, int r); +void controllerUpdate(); diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..7a04a87 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,20 @@ +#include + +#include "actuators.h" +#include "web_server.h" +#include "controller.h" +#include "robot_state.h" + +void setup() { + Serial.begin(115200); + + actuatorsInit(); + webServerInit(); + + robot.lastCmdMs = millis(); +} + +void loop() { + webServerLoop(); + controllerUpdate(); +} diff --git a/src/robot_state.cpp b/src/robot_state.cpp new file mode 100644 index 0000000..47abd93 --- /dev/null +++ b/src/robot_state.cpp @@ -0,0 +1,18 @@ +#include "robot_state.h" + +RobotState robot = { + MODE_IDLE, + 0, + 150, + 150 +}; + +const char* modeToStr(RobotMode m) { + switch (m) { + case MODE_IDLE: return "IDLE"; + case MODE_MANUAL: return "MANUAL"; + case MODE_AUTO: return "AUTO"; + case MODE_SERVICE: return "SERVICE"; + default: return "?"; + } +} diff --git a/src/robot_state.h b/src/robot_state.h new file mode 100644 index 0000000..f78d9cd --- /dev/null +++ b/src/robot_state.h @@ -0,0 +1,22 @@ +#pragma once +#include + +enum RobotMode { + MODE_IDLE, + MODE_MANUAL, + MODE_AUTO, + MODE_SERVICE +}; + +struct RobotState { + RobotMode mode; + uint32_t lastCmdMs; + int speedL; + int speedR; +}; + +// глобальное состояние +extern RobotState robot; + +// удобный вывод +const char* modeToStr(RobotMode m); diff --git a/src/web_server.cpp b/src/web_server.cpp new file mode 100644 index 0000000..6755229 --- /dev/null +++ b/src/web_server.cpp @@ -0,0 +1,109 @@ +#include +#include + +#include "config.h" +#include "web_ui.h" +#include "controller.h" +#include "robot_state.h" + +static WebServer server(80); + +void handleRoot() { + server.send_P(200, "text/html; charset=utf-8", INDEX_HTML); +} + +void handleCmd() { + if (!server.hasArg("c")) { + server.send(400, "text/plain", "Missing c"); + return; + } + + String c = server.arg("c"); + c.toUpperCase(); + + robot.lastCmdMs = millis(); + + if (c == "STOP") { + controllerStop(); + server.send(200, "text/plain", "OK STOP"); + return; + } + + if (!controllerMove(c)) { + server.send(403, "text/plain", + String("DENIED MODE=") + modeToStr(robot.mode)); + return; + } + + server.send(200, "text/plain", "OK " + c); +} + +void handleSpeed() { + int l = server.hasArg("l") ? server.arg("l").toInt() : robot.speedL; + int r = server.hasArg("r") ? server.arg("r").toInt() : robot.speedR; + + controllerSetSpeed(l, r); + server.send(200, "text/plain", "OK SPEED"); +} + +void handleMode() { + if (!server.hasArg("m")) { + server.send(400, "text/plain", "Missing m"); + return; + } + + String m = server.arg("m"); + m.toUpperCase(); + + if (m == "IDLE") controllerSetMode(MODE_IDLE); + else if (m == "MANUAL") controllerSetMode(MODE_MANUAL); + else if (m == "AUTO") controllerSetMode(MODE_AUTO); + else { + server.send(400, "text/plain", "Unknown mode"); + return; + } + + server.send(200, "text/plain", + String("OK MODE=") + modeToStr(robot.mode)); +} + +void handleStatus() { + String json = "{"; + + json += "\"mode\":\"" + String(modeToStr(robot.mode)) + "\","; + json += "\"speedL\":" + String(robot.speedL) + ","; + json += "\"speedR\":" + String(robot.speedR) + ","; + json += "\"rssi\":" + String(WiFi.RSSI()) + ","; + json += "\"uptime\":" + String(millis()); + + json += "}"; + + server.send(200, "application/json", json); +} + +void webServerInit() { + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + Serial.print("\nConnecting to Wi-Fi"); + + while (WiFi.status() != WL_CONNECTED) { + Serial.print("\nRetry connecting to Wi-Fi"); + delay(300); + } + + if (WiFi.status() == WL_CONNECTED) { + Serial.print("\nConnected. IP: "); + Serial.println(WiFi.localIP()); + } + + server.on("/", handleRoot); + server.on("/cmd", handleCmd); + server.on("/speed", handleSpeed); + server.on("/mode", handleMode); + server.on("/status", handleStatus); + server.begin(); +} + +void webServerLoop() { + server.handleClient(); +} diff --git a/src/web_server.h b/src/web_server.h new file mode 100644 index 0000000..884d14e --- /dev/null +++ b/src/web_server.h @@ -0,0 +1,3 @@ +#pragma once +void webServerInit(); +void webServerLoop(); diff --git a/src/web_ui.cpp b/src/web_ui.cpp new file mode 100644 index 0000000..1ce7f49 --- /dev/null +++ b/src/web_ui.cpp @@ -0,0 +1,146 @@ +#include +#include "web_ui.h" + +const char INDEX_HTML[] PROGMEM = R"HTML( + + + + + + ESP32 Robot + + + +

ESP32 Robot

+ +
+
+ +
+ + + + + +
+ +
+ + +
+ +
+ + +
Tip: удерживай стрелку — робот едет. Отпустил — STOP.
+
+
+ +
+ + + +
+ + + + +)HTML"; diff --git a/src/web_ui.h b/src/web_ui.h new file mode 100644 index 0000000..4d05348 --- /dev/null +++ b/src/web_ui.h @@ -0,0 +1,4 @@ +#pragma once +#include + +extern const char INDEX_HTML[]; diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html