From 8458e640894886c571c2d76eee9bdede3c32e526 Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Fri, 27 Dec 2024 19:58:05 +0200 Subject: [PATCH 1/8] Initial commit for RC integration --- .gitmodules | 3 ++ bt-remote | 1 + esp32-cam-webserver.ino | 117 +++++++++++++++++++++++++++------------- platformio.ini | 4 ++ 4 files changed, 88 insertions(+), 37 deletions(-) create mode 100644 .gitmodules create mode 160000 bt-remote diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..827b5ef --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "bt-remote"] + path = bt-remote + url = git@github.com:poconoco/bt-remote.git diff --git a/bt-remote b/bt-remote new file mode 160000 index 0000000..a937d71 --- /dev/null +++ b/bt-remote @@ -0,0 +1 @@ +Subproject commit a937d713b4d13168f4d70f5a5c9fa920643d1cff diff --git a/esp32-cam-webserver.ino b/esp32-cam-webserver.ino index 64d6483..20f26dd 100644 --- a/esp32-cam-webserver.ino +++ b/esp32-cam-webserver.ino @@ -8,6 +8,9 @@ #include "src/parsebytes.h" #include "time.h" #include +#include + +#include "TcpRcClient.h" /* This sketch is a extension/expansion/reork of the 'official' ESP32 Camera example @@ -796,50 +799,90 @@ void setup() { // As a final init step chomp out the serial buffer in case we have recieved mis-keys or garbage during startup while (Serial.available()) Serial.read(); -} -void loop() { - /* - * Just loop forever, reconnecting Wifi As necesscary in client mode - * The stream and URI handler processes initiated by the startCameraServer() call at the - * end of setup() will handle the camera and UI processing from now on. - */ - if (accesspoint) { - // Accespoint is permanently up, so just loop, servicing the captive portal as needed - // Rather than loop forever, follow the watchdog, in case we later add auto re-scan. - unsigned long start = millis(); - while (millis() - start < WIFI_WATCHDOG ) { - delay(100); - if (otaEnabled) ArduinoOTA.handle(); - handleSerial(); - if (captivePortal) dnsServer.processNextRequest(); - } - } else { - // client mode can fail; so reconnect as appropriate - static bool warned = false; - if (WiFi.status() == WL_CONNECTED) { - // We are connected, wait a bit and re-check - if (warned) { - // Tell the user if we have just reconnected - Serial.println("WiFi reconnected"); - warned = false; - } - // loop here for WIFI_WATCHDOG, turning debugData true/false depending on serial input.. + + // Start the RC reading server + TcpRcClient rcClient(9876); + rcClient.init(); + + // Init servos + Servo servo1; + Servo servo2; + servo1.attach(12); + servo2.attach(13); + servo1.write(90); + servo2.write(90); + + float servo1Pos = 90; + float servo2Pos = 90; + + auto clamp = [](float val, float min, float max) { + if (val < min) return min; + if (val > max) return max; + return val; + }; + + auto processRC = [&rcClient, &servo1, &servo2, &servo1Pos, &servo2Pos, clamp]() { + rcClient.read(); + + servo1Pos += (float)rcClient.getX1() * -0.002; + servo2Pos += (float)rcClient.getY1() * -0.002; + servo1Pos = clamp(servo1Pos, 0, 180); + servo2Pos = clamp(servo2Pos, 0, 180); + + setLamp(map(rcClient.getSliderL(), -128, 127, 0, 100)); + servo1.write(servo1Pos); + servo2.write(servo2Pos); + }; + + // Main loop instead of loop() to avoid global vars to interact with it + while (true) { + /* + * Just loop forever, reconnecting Wifi As necesscary in client mode + * The stream and URI handler processes initiated by the startCameraServer() call at the + * end of setup() will handle the camera and UI processing from now on. + */ + if (accesspoint) { + // Accespoint is permanently up, so just loop, servicing the captive portal as needed + // Rather than loop forever, follow the watchdog, in case we later add auto re-scan. unsigned long start = millis(); while (millis() - start < WIFI_WATCHDOG ) { - delay(100); + processRC(); + delay(10); if (otaEnabled) ArduinoOTA.handle(); handleSerial(); + if (captivePortal) dnsServer.processNextRequest(); } } else { - // disconnected; attempt to reconnect - if (!warned) { - // Tell the user if we just disconnected - WiFi.disconnect(); // ensures disconnect is complete, wifi scan cleared - Serial.println("WiFi disconnected, retrying"); - warned = true; + // client mode can fail; so reconnect as appropriate + static bool warned = false; + if (WiFi.status() == WL_CONNECTED) { + // We are connected, wait a bit and re-check + if (warned) { + // Tell the user if we have just reconnected + Serial.println("WiFi reconnected"); + warned = false; + } + // loop here for WIFI_WATCHDOG, turning debugData true/false depending on serial input.. + unsigned long start = millis(); + while (millis() - start < WIFI_WATCHDOG ) { + delay(10); + if (otaEnabled) ArduinoOTA.handle(); + processRC(); + handleSerial(); + } + } else { + // disconnected; attempt to reconnect + if (!warned) { + // Tell the user if we just disconnected + WiFi.disconnect(); // ensures disconnect is complete, wifi scan cleared + Serial.println("WiFi disconnected, retrying"); + warned = true; + } + WifiSetup(); } - WifiSetup(); - } + } } } + +void loop() {} diff --git a/platformio.ini b/platformio.ini index cabb5b4..3867f07 100644 --- a/platformio.ini +++ b/platformio.ini @@ -39,9 +39,13 @@ platform_packages = framework-arduinoespressif32@https://github.com/espressif/ar board = esp32dev board_build.partitions = min_spiffs.csv framework = arduino +monitor_speed = 115200 +lib_deps = + madhephaestus/ESP32Servo build_flags = -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue + -Ibt-remote/receivers/cpp ; For OTA uploading uncomment the next lines and add the IP address or mDNS name of the camera module, and the OTA password ;upload_protocol = espota ;upload_port = From 77cb2e1e9eb91a910ee804cedadce1a025fad2ba Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Fri, 27 Dec 2024 20:50:24 +0200 Subject: [PATCH 2/8] Update bt-remote connector --- bt-remote | 2 +- esp32-cam-webserver.ino | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/bt-remote b/bt-remote index a937d71..f7de43f 160000 --- a/bt-remote +++ b/bt-remote @@ -1 +1 @@ -Subproject commit a937d713b4d13168f4d70f5a5c9fa920643d1cff +Subproject commit f7de43fbfa68907293c0610d1a4dbe08624df5a4 diff --git a/esp32-cam-webserver.ino b/esp32-cam-webserver.ino index 20f26dd..db3dacd 100644 --- a/esp32-cam-webserver.ino +++ b/esp32-cam-webserver.ino @@ -10,7 +10,7 @@ #include #include -#include "TcpRcClient.h" +#include "Esp32TcpRcClient.h" /* This sketch is a extension/expansion/reork of the 'official' ESP32 Camera example @@ -802,7 +802,7 @@ void setup() { // Start the RC reading server - TcpRcClient rcClient(9876); + Esp32TcpRcClient rcClient(9876); rcClient.init(); // Init servos @@ -823,16 +823,17 @@ void setup() { }; auto processRC = [&rcClient, &servo1, &servo2, &servo1Pos, &servo2Pos, clamp]() { - rcClient.read(); + rcClient.tick(); - servo1Pos += (float)rcClient.getX1() * -0.002; - servo2Pos += (float)rcClient.getY1() * -0.002; + servo1Pos += (float)rcClient.getX1() * 0.002; + servo2Pos += (float)rcClient.getY1() * 0.002; servo1Pos = clamp(servo1Pos, 0, 180); servo2Pos = clamp(servo2Pos, 0, 180); setLamp(map(rcClient.getSliderL(), -128, 127, 0, 100)); - servo1.write(servo1Pos); - servo2.write(servo2Pos); + servo1.write(180-servo1Pos); + servo2.write(180-servo2Pos); + rcClient.send("H: "+String((int)servo1Pos)+"\nV: "+String((int)servo2Pos)); }; // Main loop instead of loop() to avoid global vars to interact with it From 61a922234657d3eaf988e5877eccac6f0d343380 Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Sat, 28 Dec 2024 00:12:00 +0200 Subject: [PATCH 3/8] Update bt-remote --- bt-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bt-remote b/bt-remote index f7de43f..60a2adb 160000 --- a/bt-remote +++ b/bt-remote @@ -1 +1 @@ -Subproject commit f7de43fbfa68907293c0610d1a4dbe08624df5a4 +Subproject commit 60a2adbf46ac7affb6b2e35e7288d494293f9d2d From 544acb72ee3163afbde86904915a837357c4061f Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Sat, 28 Dec 2024 19:55:47 +0200 Subject: [PATCH 4/8] Add fork notes to README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 2d87c9d..2e3c519 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ +# poconoco fork notes + +The sole purpose of this fork is to demonstrate how to integrate Esp32TcpRcClient into ESP32-Cam +firmware to remotely control the servos that for example can rotate the camera. Or similar approach +can be used to control 360 degrees servos, or DC motors via PWM driver board for FPV-enabled bot. + + +Original README: +---------------------------------------------- # IMPORTANT! This is a Old and rather Obsolete sketch; it only works with a very old version of the ESP32 Arduino core (2.0.2) and is missing all the improvements that espressif have made since then.. From 7f74ef40909900e4387dff72dbee40e9446942c1 Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Sat, 28 Dec 2024 19:56:13 +0200 Subject: [PATCH 5/8] Update bt-remote submodule --- bt-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bt-remote b/bt-remote index 60a2adb..fbfb831 160000 --- a/bt-remote +++ b/bt-remote @@ -1 +1 @@ -Subproject commit 60a2adbf46ac7affb6b2e35e7288d494293f9d2d +Subproject commit fbfb831448e15a3a395547995227c7433a49ff41 From 592361963f01b9a2fe830560343ee456c9e9c459 Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Sun, 29 Dec 2024 00:48:54 +0200 Subject: [PATCH 6/8] Update bt-remote submodule --- bt-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bt-remote b/bt-remote index fbfb831..0022aed 160000 --- a/bt-remote +++ b/bt-remote @@ -1 +1 @@ -Subproject commit fbfb831448e15a3a395547995227c7433a49ff41 +Subproject commit 0022aed241c1641a1fa2fc0d2e976b5029110c0b From 8cdf10f0d88fd1236317a408843556d082b649fa Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Sat, 11 Jan 2025 12:21:35 +0200 Subject: [PATCH 7/8] Lower default camera resolution --- esp32-cam-webserver.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp32-cam-webserver.ino b/esp32-cam-webserver.ino index db3dacd..bda3d9e 100644 --- a/esp32-cam-webserver.ino +++ b/esp32-cam-webserver.ino @@ -339,7 +339,7 @@ void StartCamera() { config.xclk_freq_hz = xclk * 1000000; config.pixel_format = PIXFORMAT_JPEG; // Low(ish) default framesize and quality - config.frame_size = FRAMESIZE_SVGA; + config.frame_size = FRAMESIZE_QVGA; config.jpeg_quality = 12; config.fb_location = CAMERA_FB_IN_PSRAM; config.fb_count = 2; From 269658eea57f7a6e4945d58c6bc7fcaf24676a62 Mon Sep 17 00:00:00 2001 From: Leonid Yurchenko Date: Sat, 11 Jan 2025 12:21:59 +0200 Subject: [PATCH 8/8] Update bt-remote submodule --- bt-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bt-remote b/bt-remote index 0022aed..f78fc7e 160000 --- a/bt-remote +++ b/bt-remote @@ -1 +1 @@ -Subproject commit 0022aed241c1641a1fa2fc0d2e976b5029110c0b +Subproject commit f78fc7eb79a90fce2936182c189d5caf4488416b