From 1f80548797c2f21564d5fb5dd87166346f98e70e Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Tue, 13 Dec 2022 16:26:28 +0100 Subject: [PATCH] fix reconnect gamepad --- .vscode/settings.json | 1 + doc/Notes and TODOs/TODO allgemein.txt | 2 + doc/Notes and TODOs/ps3 reconnect.txt | 26 ++++++++ include/config.h | 8 +++ lib/Navigation/navigation.cpp | 6 +- lib/Times/debugTimes.cpp | 2 +- src/SpecialMenus/Akku/menuAkku.cpp | 7 ++- src/SpecialMenus/Akku/menuAkku.h | 3 +- src/driveModi/driveManager.cpp | 2 +- src/main.cpp | 85 +++++++++++++++----------- 10 files changed, 99 insertions(+), 43 deletions(-) create mode 100644 doc/Notes and TODOs/ps3 reconnect.txt diff --git a/.vscode/settings.json b/.vscode/settings.json index ee837e7..b9eecf3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -83,6 +83,7 @@ "Ahnung", "akku", "blox", + "bluedroid", "gast", "GNSS", "GPGGA", diff --git a/doc/Notes and TODOs/TODO allgemein.txt b/doc/Notes and TODOs/TODO allgemein.txt index b4899a5..d0853c0 100644 --- a/doc/Notes and TODOs/TODO allgemein.txt +++ b/doc/Notes and TODOs/TODO allgemein.txt @@ -3,4 +3,6 @@ -> Program battery pack tracking -> Add an beeper -> Program the beeper + -> GPS-Menu without Wlan NtripClient + -> CaptureRoute Modus crash when someone try's to save a point diff --git a/doc/Notes and TODOs/ps3 reconnect.txt b/doc/Notes and TODOs/ps3 reconnect.txt new file mode 100644 index 0000000..6b97f40 --- /dev/null +++ b/doc/Notes and TODOs/ps3 reconnect.txt @@ -0,0 +1,26 @@ +ps3.h +void ps3ResetGlobals(); + +ps3.c +void ps3ResetGlobals() { + // Own Code + is_active = false; + ps3_connection_cb = NULL; + ps3_connection_object_cb = NULL; + ps3_connection_object = NULL; + ps3_event_cb = NULL; + ps3_event_object_cb = NULL; + ps3_event_object = NULL; +} + +Ps3Controller.h +private +void resetGlobals(); + +Ps3Controller.cpp +void Ps3Controller::resetGlobals() { + ps3ResetGlobals(); +} + +in -> Ps3Controller::begin +this->resetGlobals(); diff --git a/include/config.h b/include/config.h index 5cd7e31..aa917af 100644 --- a/include/config.h +++ b/include/config.h @@ -15,6 +15,14 @@ * The PS3-Controller only connects to the address which is saved in it. */ #define CONTROLLER_MAC "24:62:AB:F2:4B:3A" + +/** + * @brief Prevents creation of the global object + * + * The PS3-Controller class creates a global object to use. + */ +#define NO_GLOBAL_INSTANCES + #define UBLOX_GNSS_SPI_CS 17 #define UBLOX_GNSS_SPI_COPI 16 #define UBLOX_GNSS_SPI_CIPO 4 diff --git a/lib/Navigation/navigation.cpp b/lib/Navigation/navigation.cpp index 588c2f1..9e74702 100644 --- a/lib/Navigation/navigation.cpp +++ b/lib/Navigation/navigation.cpp @@ -80,13 +80,13 @@ void Navigation::loop() { this->updateCurrentLocation(); std::cout << "duration between: " << millis() - lastTime << std::endl; lastTime = millis(); - } else if (millis() - lastTime > 20000) { - std::cout << "Keine Daten mehr!" << std::endl; + } else if (millis() - lastTime > 20000 && !this->gps->getGnssFixOk()) { + std::cout << "No Data from ZED arrived since a long time. Navigation::loop" << std::endl; this->gps->flushPVT(); this->gps->softwareResetGNSSOnly(); lastTime = millis(); - // std::cout + //std::cout // << "checkUblox: " << this->gps->checkUblox() << " -|- " // << "getGnssFixOk: " << this->gps->getGnssFixOk() << " -|- " // << "getHWstatus: " << this->gps->getHWstatus() << " -|- " diff --git a/lib/Times/debugTimes.cpp b/lib/Times/debugTimes.cpp index 823a678..7490f5d 100644 --- a/lib/Times/debugTimes.cpp +++ b/lib/Times/debugTimes.cpp @@ -16,7 +16,7 @@ bool DebugTimes::printWarning = true; DebugTimes::DebugTimes() { this->startTime = millis(); - if (!DebugTimes::printWarning) { + if (DebugTimes::printWarning) { std::cout << std::endl << "Warning: DebugTimes is muuted, no times are be shown." << std::endl << std::endl; DebugTimes::printWarning = false; } diff --git a/src/SpecialMenus/Akku/menuAkku.cpp b/src/SpecialMenus/Akku/menuAkku.cpp index 527669e..b7aa5e3 100644 --- a/src/SpecialMenus/Akku/menuAkku.cpp +++ b/src/SpecialMenus/Akku/menuAkku.cpp @@ -11,8 +11,9 @@ #include "menuAkku.h" -MenuAkku::MenuAkku(Battery* mainBattery) { +MenuAkku::MenuAkku(Battery* mainBattery, Ps3Controller* gamepad) { this->mainBattery = mainBattery; + this->gamepad = gamepad; } void MenuAkku::right() { @@ -38,8 +39,8 @@ void MenuAkku::printMenu() { uint8_t mainBattery = this->mainBattery->getBatteryPercent(); double mainBatteryVolt = this->mainBattery->getBatteryVoltage(); - if( controllerBattery != Ps3.data.status.battery ){ - controllerBattery = Ps3.data.status.battery; + if( controllerBattery != this->gamepad->data.status.battery ){ + controllerBattery = this->gamepad->data.status.battery; } if( controllerBattery == ps3_status_battery_charging ) sprintf(buf, "LOAD"); diff --git a/src/SpecialMenus/Akku/menuAkku.h b/src/SpecialMenus/Akku/menuAkku.h index b1d8d68..cbdeee5 100644 --- a/src/SpecialMenus/Akku/menuAkku.h +++ b/src/SpecialMenus/Akku/menuAkku.h @@ -30,7 +30,7 @@ class MenuAkku : public MenuControl { * @brief Construct a new Menu Akku object * */ - MenuAkku(Battery* mainBattery); + MenuAkku(Battery* mainBattery, Ps3Controller* gamepad); void down() override {} void up() override {} @@ -58,6 +58,7 @@ class MenuAkku : public MenuControl { uint32_t lastMillis = 0; Battery* mainBattery; + Ps3Controller* gamepad; }; diff --git a/src/driveModi/driveManager.cpp b/src/driveModi/driveManager.cpp index 5269c01..d0ee340 100644 --- a/src/driveModi/driveManager.cpp +++ b/src/driveModi/driveManager.cpp @@ -39,7 +39,7 @@ void DriveManager::loop() { DebugTimes navigationTime; this->navigation->loop(); - navigationTime.stopConsol("NavigationTime", 5); + navigationTime.stopConsol("NavigationTime", 20); if (currentModusPtr) this->currentModusPtr->loop(); diff --git a/src/main.cpp b/src/main.cpp index a56c3e9..de83454 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,14 +14,16 @@ */ #include + +#include "config.h" + #include #include #include #include -#include #include - -#include "config.h" +#include +#include #include "driveModi/driveManager.h" #include "OutputBuf/outputBuf.h" @@ -47,11 +49,15 @@ Menu* main_m; LiquidCrystal_I2C* lcd; OutputBuf* outputBuf; DebugMqtt* debugMqtt = nullptr; -BluetoothSerial* serialBT = nullptr; Battery* mainBattery; +Ps3Controller* gamepad; +BluetoothSerial SerialBT; bool wifiIsActive; +bool disconnectPs3 = false; + +void configureController(void); void callbackControllerAction(void); void callbackControllerConnect(void); void callbackControllerDisconnect(void); @@ -61,7 +67,7 @@ void makeMenu(void); void setup() { Serial.begin(115200); - // DebugTimes::setConsolOutput(true); + DebugTimes::setConsolOutput(true); DebugTimes setupTime; char wifiIndicator = 'X'; @@ -104,18 +110,9 @@ void setup() { std::cout << "Welcome to Kleiax-Rover" << std::endl; std::cout << "All actions from the main program run on Core -> " << xPortGetCoreID() << std::endl; - Ps3.attach(callbackControllerAction); - Ps3.attachOnConnect(callbackControllerConnect); - Ps3.attachOnDisconnect(callbackControllerDisconnect); - std::cout << "\nReady to connect a PS3 Controller... \n"; + gamepad = new Ps3Controller; + configureController(); - // Need to be connected befor an other device connect - Ps3.begin(CONTROLLER_MAC); - - std::cout << "Activate additional output via Bluetooth..." << std::endl; - serialBT = new BluetoothSerial; - serialBT->begin("Kleiax-Rover"); - outputBuf->setSerialBT(serialBT); outputBuf->activateMqtt(false); lcd->backlight(); @@ -141,11 +138,26 @@ void loop() { mainBattery->loop(); mainBatteryTime.stopConsol("MainBatteryTime", 5); - // static uint32_t lastMillis = 0; - // if (millis() - lastMillis > 2000) { - // std::cout << "Is BT-Consol connected: " << serialBT->connected() << std::endl; - // lastMillis = millis(); - // } + if (disconnectPs3) { + DebugTimes disconnectPS3Time; + std::cout << "Disconnect PS3 Controller." << std::endl; + gamepad->end(); + esp_bluedroid_disable(); + esp_bluedroid_deinit(); + configureController(); + disconnectPs3 = false; + disconnectPS3Time.stopConsol("Ps3 Disconnect"); + } +} + +void configureController() { + + gamepad->attach(callbackControllerAction); + gamepad->attachOnConnect(callbackControllerConnect); + gamepad->attachOnDisconnect(callbackControllerDisconnect); + std::cout << "\nReady to connect a PS3 Controller... \n"; + + gamepad->begin(CONTROLLER_MAC); } void callbackControllerAction() { @@ -153,7 +165,7 @@ void callbackControllerAction() { static uint32_t lastMillis = 0; if (reset) lastMillis = millis(); - static const uint16_t delay = 300; + static const uint16_t delay = 100; bool res = false; if (millis() - lastMillis > delay) @@ -163,19 +175,19 @@ void callbackControllerAction() { // Menu controlling if (isDelayReached()) { - if (Ps3.data.button.up) + if (gamepad->data.button.up) main_m->up(); - else if (Ps3.data.button.down) + else if (gamepad->data.button.down) main_m->down(); - else if (Ps3.data.button.left) + else if (gamepad->data.button.left) main_m->left(); - else if (Ps3.data.button.right) + else if (gamepad->data.button.right) main_m->right(); - else if (Ps3.data.button.circle) + else if (gamepad->data.button.circle) main_m->yes(); - else if (Ps3.data.button.cross) + else if (gamepad->data.button.cross) main_m->no(); - else if (Ps3.data.button.select) + else if (gamepad->data.button.select) debugMqtt->sendMsg(Loglevel::info, "Here we are: callbackController.."); isDelayReached(true); @@ -185,18 +197,23 @@ void callbackControllerAction() { } void callbackControllerConnect() { + static bool firstConnect = true; + std::cout << "Controller connected to ESP32" << std::endl; std::cout << "All actions from the Controller run on Core -> " << xPortGetCoreID() << std::endl; // Display is not functional without this seconde init, first init is in setup(), // because after the setup the other core control the display. - lcd->init(); - main_m->printMenu(); + if (firstConnect) { + lcd->init(); + main_m->printMenu(); + firstConnect = false; + } } void callbackControllerDisconnect() { std::cout << "Controller disconnected..." << std::endl; - // Ps3.end(); + // gamepad->end(); } void i2cScanner() { @@ -243,7 +260,7 @@ void makeMenu() { }; auto disconnectController = [&]() { - Ps3.end(); + disconnectPs3 = true; }; // Create Menu @@ -257,7 +274,7 @@ void makeMenu() { MenuAutopilot* auto_m = new MenuAutopilot(driveManager); MenuTestMode* test_m = new MenuTestMode(driveManager); MenuGPS* gps_m = new MenuGPS(driveManager); - MenuAkku* akku_m = new MenuAkku(mainBattery); + MenuAkku* akku_m = new MenuAkku(mainBattery, gamepad); // Set Menus on LCD main_m->setLcd(lcd);