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/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.. diff --git a/bt-remote b/bt-remote new file mode 160000 index 0000000..f78fc7e --- /dev/null +++ b/bt-remote @@ -0,0 +1 @@ +Subproject commit f78fc7eb79a90fce2936182c189d5caf4488416b diff --git a/esp32-cam-webserver.ino b/esp32-cam-webserver.ino index 64d6483..bda3d9e 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 "Esp32TcpRcClient.h" /* This sketch is a extension/expansion/reork of the 'official' ESP32 Camera example @@ -336,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; @@ -796,50 +799,91 @@ 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 + Esp32TcpRcClient 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.tick(); + + 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(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 + 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 =