From 33fe6916ed6a30c3879872174149bc1a45127017 Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Thu, 30 Mar 2023 22:20:28 +0200 Subject: [PATCH] remove all PS3 things and begin with ControllPad --- lib/ControllPad/controllPad.cpp | 57 +++++++++ lib/ControllPad/controllPad.h | 45 +++++++ lib/ControllPad/controllPadInput.h | 42 +++++++ platformio.ini | 2 - .../menuSysteminformatio.cpp | 22 +--- .../Systeminformation/menuSysteminformation.h | 6 +- src/driveModi/Modi/Autopilot/autopilot.cpp | 6 +- src/driveModi/Modi/Autopilot/autopilot.h | 2 +- .../Modi/CaptureRoute/captureRoute.cpp | 6 +- .../Modi/CaptureRoute/captureRoute.h | 2 +- .../Modi/ManualControl/manualControl.cpp | 8 +- .../Modi/ManualControl/manualControl.h | 5 +- src/driveModi/driveManager.cpp | 10 +- src/driveModi/driveManager.h | 5 +- src/driveModi/driveModi.h | 2 - src/main.cpp | 112 ++---------------- 16 files changed, 182 insertions(+), 150 deletions(-) create mode 100644 lib/ControllPad/controllPad.cpp create mode 100644 lib/ControllPad/controllPad.h create mode 100644 lib/ControllPad/controllPadInput.h diff --git a/lib/ControllPad/controllPad.cpp b/lib/ControllPad/controllPad.cpp new file mode 100644 index 0000000..2df47b5 --- /dev/null +++ b/lib/ControllPad/controllPad.cpp @@ -0,0 +1,57 @@ +/** + * @file controllPad.cpp + * @author Alexander Klein (alex@kleiax.de) + * @brief + * @version 0.1 + * @date 2023-03-30 + * + * @copyright Copyright (c) 2023 + * + */ + +#include "controllPad.h" + +ControllPad::ControllPad() { + +} + +bool ControllPad::loop() { + if (millis() - this->lastLoopMillis < this->loopDelay) + return false; + this->lastLoopMillis = millis(); + + if(!this->connected) + return false; + + if (millis() - this->lastMessageRecive > this->disconnectTime) { + this->connected = false; + return false; + } + + if (this->menuControl && this->controllInput.buttons > 0 && this->updated) + if (!this->firstButtonPress) + this->menuControl->printMenu(); + + if (ControllPadButton::isControllPadButtonPressed(&this->controllInput, ControllPadButton::PadButton::Left)) + this->menuControl->left(); + else if (ControllPadButton::isControllPadButtonPressed(&this->controllInput, ControllPadButton::PadButton::Right)) + this->menuControl->right(); + else if (ControllPadButton::isControllPadButtonPressed(&this->controllInput, ControllPadButton::PadButton::Up)) + this->menuControl->up(); + else if (ControllPadButton::isControllPadButtonPressed(&this->controllInput, ControllPadButton::PadButton::Down)) + this->menuControl->down(); + else if (ControllPadButton::isControllPadButtonPressed(&this->controllInput, ControllPadButton::PadButton::Yes)) + this->menuControl->yes(); + else if (ControllPadButton::isControllPadButtonPressed(&this->controllInput, ControllPadButton::PadButton::No)) + this->menuControl->no(); + + this->menuControl->update(); + + return true; +} + +const ControllPadInput * ControllPad::getControllPadDataPtr(uint16_t maxElapsedTime) const { + if (maxElapsedTime && millis() - this->lastMessageRecive > maxElapsedTime) + return nullptr; + return &this->controllInput; +} diff --git a/lib/ControllPad/controllPad.h b/lib/ControllPad/controllPad.h new file mode 100644 index 0000000..1a38d2c --- /dev/null +++ b/lib/ControllPad/controllPad.h @@ -0,0 +1,45 @@ +/** + * @file controllPad.h + * @author Alexander Klein (alex@kleiax.de) + * @brief + * @version 0.1 + * @date 2023-03-30 + * + * @copyright Copyright (c) 2023 + * + */ + +#pragma once + +#include + +#include "controllPadInput.h" + +class ControllPad { + public: + ControllPad(); + + bool loop(); + + void setMenuControll(MenuControl* menuControll) { this->menuControl = menuControl; } + + const ControllPadInput * getControllPadDataPtr(uint16_t maxElapsedTime = 0) const; + + bool isControllPadConnected() const { return this->connected; } + + private: + MenuControl* menuControl = nullptr; + ControllPadInput controllInput; + + bool connected = false; + bool updated = false; + bool firstButtonPress = false; + + uint16_t disconnectTime = 50; + uint16_t loopDelay = 5; + + uint32_t lastMessageRecive = 0; + uint32_t lastLoopMillis = 0; + + +}; diff --git a/lib/ControllPad/controllPadInput.h b/lib/ControllPad/controllPadInput.h new file mode 100644 index 0000000..800af32 --- /dev/null +++ b/lib/ControllPad/controllPadInput.h @@ -0,0 +1,42 @@ +/** + * @file controllPadInput.h + * @author Alexander Klein (alex@kleiax.de) + * @brief + * @version 0.1 + * @date 2023-03-30 + * + * @copyright Copyright (c) 2023 + * + */ + +#ifndef CONTROLL_PAD_INPUT_H +#define CONTROLL_PAD_INPUT_H + +#include + +struct ControllPadInput { + uint8_t buttons; + uint8_t x; + uint8_t y; + uint8_t counter; +}; + + +class ControllPadButton { + public: + enum PadButton { + Left, + Right, + Up, + Down, + Yes, + No, + Action + }; + + static bool isControllPadButtonPressed(const ControllPadInput *input, PadButton button) { + return false; + } +}; + +#endif // CONTROLL_PAD_INPUT_H diff --git a/platformio.ini b/platformio.ini index 55d53f4..9376e9b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,12 +17,10 @@ monitor_speed = 115200 upload_speed = 921600 monitor_port = COM3 lib_deps = - jvpernis/PS3 Controller Host@^1.1.0 sparkfun/SparkFun u-blox GNSS Arduino Library@^2.2.7 knolleary/PubSubClient@^2.8 br3ttb/PID@^1.2.1 marcoschwartz/LiquidCrystal_I2C@^1.1.4 - marian-craciunescu/ESP32Ping@^1.7 bblanchon/ArduinoJson@^6.20.0 mprograms/QMC5883LCompass@^1.1.1 https://git.kleiax.de/PlatformIO-Libs/Menu.git#v1.0 diff --git a/src/SpecialMenus/Systeminformation/menuSysteminformatio.cpp b/src/SpecialMenus/Systeminformation/menuSysteminformatio.cpp index 87ec269..df745c4 100644 --- a/src/SpecialMenus/Systeminformation/menuSysteminformatio.cpp +++ b/src/SpecialMenus/Systeminformation/menuSysteminformatio.cpp @@ -11,10 +11,9 @@ #include "menuSysteminformation.h" -MenuSysteminformation::MenuSysteminformation(Battery* mainBattery, Ps3Controller* gamepad) +MenuSysteminformation::MenuSysteminformation(Battery* mainBattery) : MenuInformationSites(5) { this->mainBattery = mainBattery; - this->gamepad = gamepad; } void MenuSysteminformation::printPage() const { @@ -44,11 +43,11 @@ void MenuSysteminformation::printPage() const { lineTwo.concat(this->mainBattery->getBatteryVoltage()); break; - case 4: - lineOne = "PS3 battery"; - lineTwo = "is "; - lineTwo.concat(MenuSysteminformation::ps3ToString(this->gamepad->data.status.battery)); - break; + // case 4: + // lineOne = "PS3 battery"; + // lineTwo = "is "; + // lineTwo.concat(MenuSysteminformation::ps3ToString(this->gamepad->data.status.battery)); + // break; default: this->printDefault(); @@ -58,12 +57,3 @@ void MenuSysteminformation::printPage() const { this->print(lineOne, lineTwo); } -String MenuSysteminformation::ps3ToString(uint8_t battery) { - if( battery == ps3_status_battery_charging ) return "LOAD"; - else if( battery == ps3_status_battery_full ) return "FULL"; - else if( battery == ps3_status_battery_high ) return "HIGH"; - else if( battery == ps3_status_battery_low) return "LOW"; - else if( battery == ps3_status_battery_dying ) return "BAD"; - else if( battery == ps3_status_battery_shutdown ) return "OFF"; - else return "UNDEF"; -} diff --git a/src/SpecialMenus/Systeminformation/menuSysteminformation.h b/src/SpecialMenus/Systeminformation/menuSysteminformation.h index 7f214f4..5f6efb1 100644 --- a/src/SpecialMenus/Systeminformation/menuSysteminformation.h +++ b/src/SpecialMenus/Systeminformation/menuSysteminformation.h @@ -13,8 +13,8 @@ #define MENU_SYSTEMINFORMATION_H #include -#include +#include "controllPadInput.h" #include "menuInformationSites.h" #include "battery.h" @@ -31,14 +31,12 @@ class MenuSysteminformation : public MenuInformationSites { * @param mainBattery * @param gamepad */ - MenuSysteminformation(Battery* mainBattery, Ps3Controller* gamepad); + MenuSysteminformation(Battery* mainBattery); private: void printPage() const override; - static String ps3ToString(uint8_t battery); Battery* mainBattery; - Ps3Controller* gamepad; }; #endif // MENU_SYSTEMINFORMATION_H diff --git a/src/driveModi/Modi/Autopilot/autopilot.cpp b/src/driveModi/Modi/Autopilot/autopilot.cpp index 86ba8ad..c71d673 100644 --- a/src/driveModi/Modi/Autopilot/autopilot.cpp +++ b/src/driveModi/Modi/Autopilot/autopilot.cpp @@ -11,8 +11,8 @@ #include "driveModi/Modi/Autopilot/autopilot.h" -Autopilot::Autopilot(MoveControl* moveControl, Ps3Controller *gamepad, Navigation* navigation) - : ManualControl(moveControl, gamepad) { +Autopilot::Autopilot(MoveControl* moveControl, const ControllPadInput *input, Navigation* navigation) + : ManualControl(moveControl, input) { this->navigation = navigation; this->navigationStarted = this->navigation->startNavigation(); this->routeInfo = this->navigation->getRouteInfo(); @@ -45,7 +45,7 @@ void Autopilot::runAutopilot() { this->selfDrivingAvailable = true; if (!this->selfDriving && this->selfDrivingAvailable) - this->setSelfDriving(this->gamepad->data.button.triangle); + this->setSelfDriving(ControllPadButton::isControllPadButtonPressed(this->input, ControllPadButton::PadButton::Action)); if (this->routeInfo.currentPoint == this->routeInfo.totalPoints) { this->navigationEnded = true; diff --git a/src/driveModi/Modi/Autopilot/autopilot.h b/src/driveModi/Modi/Autopilot/autopilot.h index 2d63802..487ceaf 100644 --- a/src/driveModi/Modi/Autopilot/autopilot.h +++ b/src/driveModi/Modi/Autopilot/autopilot.h @@ -35,7 +35,7 @@ class Autopilot : public ManualControl { * @param moveControl for ManualControl * @param navigation for route instructions */ - Autopilot(MoveControl* moveControl, Ps3Controller *gamepad, Navigation* navigation); + Autopilot(MoveControl* moveControl, const ControllPadInput *input, Navigation* navigation); /** * @brief Calls runAutopilot or ManualControl loop diff --git a/src/driveModi/Modi/CaptureRoute/captureRoute.cpp b/src/driveModi/Modi/CaptureRoute/captureRoute.cpp index 80ad7fe..57eb5c3 100644 --- a/src/driveModi/Modi/CaptureRoute/captureRoute.cpp +++ b/src/driveModi/Modi/CaptureRoute/captureRoute.cpp @@ -11,8 +11,8 @@ #include "driveModi/Modi/CaptureRoute/captureRoute.h" -CaptureRoute::CaptureRoute(MoveControl* moveControl, Ps3Controller *gamepad, Navigation* navigation) - : ManualControl(moveControl, gamepad) { +CaptureRoute::CaptureRoute(MoveControl* moveControl, const ControllPadInput *input, Navigation* navigation) + : ManualControl(moveControl, input) { this->navigation = navigation; this->routeInfo = navigation->getRouteInfo(); } @@ -28,7 +28,7 @@ void CaptureRoute::loop() { } void CaptureRoute::runCaptureRoute() { - if (this->gamepad->data.button.triangle) { + if (ControllPadButton::isControllPadButtonPressed(this->input, ControllPadButton::PadButton::Action)) { if (this->navigation->addCurrentPosToRoute()) { this->routeInfo = navigation->getRouteInfo(); this->updateDisplay = true; diff --git a/src/driveModi/Modi/CaptureRoute/captureRoute.h b/src/driveModi/Modi/CaptureRoute/captureRoute.h index efb0f4e..757a9f9 100644 --- a/src/driveModi/Modi/CaptureRoute/captureRoute.h +++ b/src/driveModi/Modi/CaptureRoute/captureRoute.h @@ -33,7 +33,7 @@ class CaptureRoute : public ManualControl { * @param moveControl for ManualControl * @param navigation to add Points */ - CaptureRoute(MoveControl* moveControl, Ps3Controller *gamepad, Navigation* navigation); + CaptureRoute(MoveControl* moveControl, const ControllPadInput *input, Navigation* navigation); /** * @brief Calls runCaptureRoute and ManualControl::loop diff --git a/src/driveModi/Modi/ManualControl/manualControl.cpp b/src/driveModi/Modi/ManualControl/manualControl.cpp index 877cf02..fec6158 100644 --- a/src/driveModi/Modi/ManualControl/manualControl.cpp +++ b/src/driveModi/Modi/ManualControl/manualControl.cpp @@ -10,9 +10,9 @@ */ #include "manualControl.h" -ManualControl::ManualControl(MoveControl *moveControl, Ps3Controller *gamepad) { +ManualControl::ManualControl(MoveControl *moveControl, const ControllPadInput *input) { this->moveControl = moveControl; - this->gamepad = gamepad; + this->input = input; this->moveControl->setDrivingStatus(DrivingStatus::drive); } @@ -31,8 +31,8 @@ void ManualControl::loop() { } void ManualControl::runManualControl() { - int8_t x = this->gamepad->data.analog.stick.lx; - int8_t y = this->gamepad->data.analog.stick.ly; + int8_t x = this->input->x; + int8_t y = this->input->y; double value_per_step = this->max_speed * 2 / 256; this->moveControl->setSpeed((y * -1) * value_per_step); diff --git a/src/driveModi/Modi/ManualControl/manualControl.h b/src/driveModi/Modi/ManualControl/manualControl.h index 60bf312..d470a77 100644 --- a/src/driveModi/Modi/ManualControl/manualControl.h +++ b/src/driveModi/Modi/ManualControl/manualControl.h @@ -13,6 +13,7 @@ #include "moveControl.h" #include "driveModi/driveModi.h" +#include "controllPadInput.h" /** * @brief Drive the Rover with a Joystick @@ -24,7 +25,7 @@ */ class ManualControl : public DriveModi { public: - ManualControl(MoveControl *moveControl, Ps3Controller *gamepad); + ManualControl(MoveControl *moveControl, const ControllPadInput *input); /** * @brief Destroy the Manual Control object @@ -71,7 +72,7 @@ class ManualControl : public DriveModi { protected: MoveControl *moveControl; - Ps3Controller *gamepad; + const ControllPadInput* input; private: uint32_t lastMillis = 0; diff --git a/src/driveModi/driveManager.cpp b/src/driveModi/driveManager.cpp index 653e33b..0fddd4f 100644 --- a/src/driveModi/driveManager.cpp +++ b/src/driveModi/driveManager.cpp @@ -15,9 +15,9 @@ Modi& operator++(Modi& m, int) { return m = (m == Modi::TestMode) ? Modi::Off : static_cast(static_cast(m)+1); } -DriveManager::DriveManager(MoveControl *moveControl, Ps3Controller *gamepad, bool wifi) { +DriveManager::DriveManager(MoveControl *moveControl, const ControllPadInput *input, bool wifi) { this->moveControl = moveControl; - this->gamepad = gamepad; + this->input = input; this->spiPort = new SPIClass(HSPI); spiPort->begin(UBLOX_GNSS_SPI_SCK, UBLOX_GNSS_SPI_CIPO, UBLOX_GNSS_SPI_COPI, UBLOX_GNSS_SPI_CS); @@ -66,17 +66,17 @@ void DriveManager::changeModus(Modi modus) { break; case Modi::ManualControl: { - this->currentModusPtr = new ManualControl(this->moveControl, this->gamepad); + this->currentModusPtr = new ManualControl(this->moveControl, this->input); } break; case Modi::CaptureRoute: { - this->currentModusPtr = new CaptureRoute(this->moveControl, this->gamepad, this->navigation); + this->currentModusPtr = new CaptureRoute(this->moveControl, this->input, this->navigation); } break; case Modi::Autopilot: { - this->currentModusPtr = new Autopilot(this->moveControl, this->gamepad, this->navigation); + this->currentModusPtr = new Autopilot(this->moveControl, this->input, this->navigation); } break; diff --git a/src/driveModi/driveManager.h b/src/driveModi/driveManager.h index 96219c9..50d0266 100644 --- a/src/driveModi/driveManager.h +++ b/src/driveModi/driveManager.h @@ -21,6 +21,7 @@ #include "config.h" #include "networkConfig.h" #include "debugTimes.h" +#include "controllPadInput.h" // All Drive Modi #include "driveModi/Modi/ManualControl/manualControl.h" @@ -51,7 +52,7 @@ class DriveManager { * * @param moveControl */ - DriveManager(MoveControl *moveControl, Ps3Controller *gamepad, bool wifi = false); + DriveManager(MoveControl *moveControl, const ControllPadInput *input, bool wifi = false); /** * @brief Destroy the Drive Manager object @@ -117,7 +118,7 @@ class DriveManager { private: Modi currentModus = Modi::Off; MoveControl *moveControl; - Ps3Controller *gamepad; + const ControllPadInput* input; DriveModi *currentModusPtr = nullptr; Navigation* navigation; SPIClass* spiPort; diff --git a/src/driveModi/driveModi.h b/src/driveModi/driveModi.h index dc36121..8d00c3c 100644 --- a/src/driveModi/driveModi.h +++ b/src/driveModi/driveModi.h @@ -12,8 +12,6 @@ #ifndef DRIVEMODI_H #define DRIVEMODI_H -#include - /** * @brief Baseclass to build DriveModi * diff --git a/src/main.cpp b/src/main.cpp index f4152d7..beb3555 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,13 +17,9 @@ #include "config.h" -#include #include #include #include -#include -#include -#include #include "driveModi/driveManager.h" #include "OutputBuf/outputBuf.h" @@ -33,6 +29,7 @@ #include "debugMqtt.h" #include "battery.h" #include "debugTimes.h" +#include "controllPad.h" #include "menu.h" #include "menuAction.h" @@ -53,21 +50,13 @@ LcdWrapper* lcdWrapper; OutputBuf* outputBuf; DebugMqtt* debugMqtt = nullptr; Battery* mainBattery; -Ps3Controller* gamepad; -BluetoothSerial SerialBT; +ControllPad* controllPad; bool wifiIsActive; -bool disconnectPs3 = false; - -void configureController(void); -void callbackControllerAction(void); -void callbackControllerConnect(void); -void callbackControllerDisconnect(void); void i2cScanner(void); void makeMenu(void); void restart(void); -void disconnectController(void); void setup() { @@ -77,6 +66,8 @@ void setup() { char wifiIndicator = 'X'; mainBattery = new Battery(35, 692000, 218500); + controllPad = new ControllPad(); + controllPad->setMenuControll(main_m); Wire.begin(21, 19); Wire.setClock(400000); @@ -90,14 +81,6 @@ void setup() { Network::setupMQTT(); wifiIsActive = Network::connectWifi(); if (wifiIsActive) { - - // const char* remoteHost = "www.google.com"; - // std::cout << "Pinging host: " << remoteHost << std::endl; - // if (Ping.ping(remoteHost)) - // std::cout << "Ping successful..." << std::endl; - // else - // std::cout << "Ping failed. :/" << std::endl; - if (Network::checkMQTT()) { DebugMqtt::init(Network::getMqttClient(), Loglevel::debug); std::cout << "Activate additional output via MQTT..." << std::endl; @@ -114,9 +97,7 @@ void setup() { std::cout << "Welcome to Kleiax-Rover" << std::endl; std::cout << "All actions from the main program run on Core -> " << xPortGetCoreID() << std::endl; - gamepad = new Ps3Controller; - driveManager = new DriveManager(&moveController, gamepad, wifiIsActive); - configureController(); + driveManager = new DriveManager(&moveController, controllPad->getControllPadDataPtr(), wifiIsActive); outputBuf->activateMqtt(false); @@ -139,6 +120,7 @@ void loop() { wifiTime.stopConsol("WiFi-Time", 10); } driveManager->loop(); + controllPad->loop(); lcdWrapper->loop(); mainBattery->loop(); @@ -156,75 +138,6 @@ void loop() { while (true); } - - 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() { - auto isDelayReached = [](bool reset = false) -> bool { - static uint32_t lastMillis = 0; - if (reset) - lastMillis = millis(); - static const uint16_t delay = 100; - bool res = false; - - if (millis() - lastMillis > delay) - res = true; - return res; - }; - - // Menu controlling - if (isDelayReached()) { - if (gamepad->data.button.up) - main_m->up(); - else if (gamepad->data.button.down) - main_m->down(); - else if (gamepad->data.button.left) - main_m->left(); - else if (gamepad->data.button.right) - main_m->right(); - else if (gamepad->data.button.circle) - main_m->yes(); - else if (gamepad->data.button.cross) - main_m->no(); - else if (gamepad->data.button.select) - std::cout << "main.cpp callbackControllerAction - Here we are!" << std::endl; - - isDelayReached(true); - } - // This have to be stand here because it have to be run on the other Core - main_m->update(); -} - -void callbackControllerConnect() { - std::cout << "Controller connected to ESP32" << std::endl; - std::cout << "All actions from the Controller run on Core -> " << xPortGetCoreID() << std::endl; - - main_m->printMenu(); -} - -void callbackControllerDisconnect() { - std::cout << "Controller disconnected..." << std::endl; - // gamepad->end(); } void i2cScanner() { @@ -274,7 +187,7 @@ void makeMenu() { MenuAutopilot* auto_m = new MenuAutopilot(driveManager); MenuTestMode* testM_m = new MenuTestMode(driveManager); MenuGPS* gps_m = new MenuGPS(driveManager); - MenuSysteminformation* sys_m = new MenuSysteminformation(mainBattery, gamepad); + MenuSysteminformation* sys_m = new MenuSysteminformation(mainBattery); MenuRoute* rout_m = new MenuRoute(driveManager->getNavigation()->getRoute()); // Set Menus on LCD @@ -297,7 +210,6 @@ void makeMenu() { MenuAction* gps_e = new MenuAction("GPS", gps_m); MenuAction* pid_e = new MenuAction("PID", pid_m); MenuAction* restart_e = new MenuAction("Restart", restart); - MenuAction* contr_e = new MenuAction("Remove PS3", disconnectController); MenuAction* sys_e = new MenuAction("Systeminfo", sys_m); MenuAction* rout_e = new MenuAction("Route", rout_m); main_m->addEntry(mode_e); @@ -305,7 +217,6 @@ void makeMenu() { main_m->addEntry(rout_e); main_m->addEntry(pid_e); main_m->addEntry(sys_e); - main_m->addEntry(contr_e); main_m->addEntry(restart_e); // Entry for the mode Menu @@ -344,12 +255,3 @@ void restart(void) { lcdWrapper->print("Rebooting ..."); ESP.restart(); } - -void disconnectController(void) { - disconnectPs3 = true; - lcdWrapper->clear(); - lcdWrapper->setCursor(0, 0); - lcdWrapper->print("Controller is"); - lcdWrapper->setCursor(0, 1); - lcdWrapper->print("disconnected."); -}