From 3fab7c7f83fa1819f0d674f229842eb6d23efff1 Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Tue, 11 Apr 2023 12:47:32 +0200 Subject: [PATCH] working remoteControl via espNow --- .vscode/settings.json | 3 ++ include/networkConfig.h | 2 +- lib/ControllPad/controlPad.cpp | 77 +++++++++++++++++-------------- lib/ControllPad/controlPad.h | 11 ++--- lib/ControllPad/controlPadInput.h | 48 +++++++++++++++---- lib/LcdWrapper/LcdWrapper.cpp | 19 +++++--- lib/LcdWrapper/LcdWrapper.h | 4 ++ src/main.cpp | 39 ++++++++-------- 8 files changed, 125 insertions(+), 78 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 99f1827..6701358 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -85,9 +85,12 @@ "ardui", "blox", "bluedroid", + "CIPO", + "COPI", "gast", "GNSS", "GPGGA", + "HSPI", "Huus", "Keine", "kleiax", diff --git a/include/networkConfig.h b/include/networkConfig.h index 8e06181..6fc8326 100644 --- a/include/networkConfig.h +++ b/include/networkConfig.h @@ -62,7 +62,7 @@ #endif // NTRIP_RTK2GO -#define RHEDE +#define DORO //Network config ESP32 #ifdef ESP32BROKER diff --git a/lib/ControllPad/controlPad.cpp b/lib/ControllPad/controlPad.cpp index 52b4d21..89ae6a1 100644 --- a/lib/ControllPad/controlPad.cpp +++ b/lib/ControllPad/controlPad.cpp @@ -16,58 +16,65 @@ ControlPad::ControlPad() { } bool ControlPad::loop() { - this->receiveData(); - if (millis() - this->lastLoopMillis < this->loopDelay) return false; this->lastLoopMillis = millis(); - // if(!this->connected) - // return false; + if(!this->connected) + return false; - // if (millis() - this->lastMessageReceive > this->disconnectTime) { - // this->connected = false; - // return false; - // } + if (millis() - this->lastMessageReceive > this->disconnectTime) { + this->connected = false; + this->controlInput.buttons = 0; + this->controlInput.x = 127; + this->controlInput.y = 127; + return false; + } - // if (this->menuControl && this->controlInput.buttons > 0 && this->updated) - // if (!this->firstButtonPress) - // this->menuControl->printMenu(); + if (this->lastButtons != controlInput.buttons) + this->updated = true; - // if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Left)) - // this->menuControl->left(); - // else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Right)) - // this->menuControl->right(); - // else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Up)) - // this->menuControl->up(); - // else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Down)) - // this->menuControl->down(); - // else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Yes)) - // this->menuControl->yes(); - // else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::No)) - // this->menuControl->no(); + if (this->menuControl && this->controlInput.buttons > 0 && this->updated) { + if (this->firstButtonPress) { + this->menuControl->printMenu(); + this->firstButtonPress = false; + std::cout << "ControlPad::loop - First menu print" << std::endl; + return true; + } - // this->menuControl->update(); + if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Left)) + this->menuControl->left(); + else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Right)) + this->menuControl->right(); + else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Up)) + this->menuControl->up(); + else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Down)) + this->menuControl->down(); + else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::Yes)) + this->menuControl->yes(); + else if (ControlPadButton::isControlPadButtonPressed(&this->controlInput, ControlPadButton::PadButton::No)) + this->menuControl->no(); + this->updated = false; + this->lastButtons = controlInput.buttons; + } return true; } void ControlPad::insertData(const uint8_t *data) { + this->connected = true; + this->lastMessageReceive = millis(); + uint8_t lastCount = this->controlInput.counter + 1; memcpy(&(this->controlInput), data, sizeof(this->controlInput)); if (lastCount != this->controlInput.counter) std::cout << "ControlPad::insertData counter wrong value" << std::endl; - - this->lastMessageReceive = millis(); - std::cout << "data: " << (int) this->controlInput.counter << std::endl; -} -const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime) const { - if (maxElapsedTime && millis() - this->lastMessageReceive > maxElapsedTime) - return nullptr; - return &this->controlInput; -} - -void ControlPad::receiveData() { + if (this->controlInput.x > 127 - this->deadZoneX + && this->controlInput.x < 127 + this->deadZoneX) + this->controlInput.x = 127; + if (this->controlInput.y > 127 - this->deadZoneY + && this->controlInput.y < 127 + this->deadZoneY) + this->controlInput.y = 127; } diff --git a/lib/ControllPad/controlPad.h b/lib/ControllPad/controlPad.h index b81eef7..db8dc5c 100644 --- a/lib/ControllPad/controlPad.h +++ b/lib/ControllPad/controlPad.h @@ -25,7 +25,7 @@ class ControlPad { void setMenuControl(MenuControl* menuControl) { this->menuControl = menuControl; } - const ControlPadInput * getControlPadDataPtr(uint16_t maxElapsedTime = 0) const; + const ControlPadInput * getControlPadDataPtr() const { return &this->controlInput; } bool isControlPadConnected() const { return this->connected; } @@ -33,13 +33,13 @@ class ControlPad { MenuControl* menuControl = nullptr; ControlPadInput controlInput; - void receiveData(); - bool connected = false; bool updated = false; - bool firstButtonPress = false; + bool firstButtonPress = true; - const uint8_t address[2][6] = { "rover", "ardui" }; + uint8_t deadZoneX = 10; + uint8_t deadZoneY = 10; + uint8_t lastButtons = 0; uint16_t disconnectTime = 50; uint16_t loopDelay = 5; @@ -47,5 +47,4 @@ class ControlPad { uint32_t lastMessageReceive = 0; uint32_t lastLoopMillis = 0; - }; diff --git a/lib/ControllPad/controlPadInput.h b/lib/ControllPad/controlPadInput.h index a6dd4bd..aab44ef 100644 --- a/lib/ControllPad/controlPadInput.h +++ b/lib/ControllPad/controlPadInput.h @@ -25,17 +25,49 @@ struct ControlPadInput { class ControlPadButton { public: enum PadButton { - Left, - Right, - Up, - Down, - Yes, - No, - Action + Left = 4, + Right = 8, + Up = 2, + Down = 1, + Yes = 32, + No = 16, + Action = 64 }; static bool isControlPadButtonPressed(const ControlPadInput *input, PadButton button) { - return false; + uint8_t buttonNum = input->buttons; + switch (button) { + case PadButton::Left : + return buttonNum & (uint8_t) PadButton::Left; + break; + + case PadButton::Right : + return buttonNum & (uint8_t) PadButton::Right; + break; + + case PadButton::Up : + return buttonNum & (uint8_t) PadButton::Up; + break; + + case PadButton::Down : + return buttonNum & (uint8_t) PadButton::Down; + break; + + case PadButton::Yes : + return buttonNum & (uint8_t) PadButton::Yes; + break; + + case PadButton::No : + return buttonNum & (uint8_t) PadButton::No; + break; + + case PadButton::Action : + return buttonNum & (uint8_t) PadButton::Action; + break; + + default: + return false;; + } } }; diff --git a/lib/LcdWrapper/LcdWrapper.cpp b/lib/LcdWrapper/LcdWrapper.cpp index 9823153..50caa37 100644 --- a/lib/LcdWrapper/LcdWrapper.cpp +++ b/lib/LcdWrapper/LcdWrapper.cpp @@ -18,14 +18,19 @@ LcdWrapper::LcdWrapper(LiquidCrystal_I2C* lcd) { } void LcdWrapper::loop() { - if (this->changed) { - this->lcd->clear(); - for (uint8_t i = 0; i < DISPLAY_WRAPPER_LINES; i++) { - this->lcd->setCursor(0, i); - this->lcd->print(this->data[i]); - } - this->changed = false; + if (!this->changed) + return; + + this->lcd->clear(); + for (uint8_t i = 0; i < DISPLAY_WRAPPER_LINES; i++) { + this->lcd->setCursor(0, i); + this->lcd->print(this->data[i]); } + + if (this->callback) + this->callback(this->data, DISPLAY_WRAPPER_LINES, DISPLAY_WRAPPER_ROWS); + + this->changed = false; } void LcdWrapper::clear() { diff --git a/lib/LcdWrapper/LcdWrapper.h b/lib/LcdWrapper/LcdWrapper.h index 40f4408..c0194dd 100644 --- a/lib/LcdWrapper/LcdWrapper.h +++ b/lib/LcdWrapper/LcdWrapper.h @@ -19,6 +19,8 @@ #define DISPLAY_WRAPPER_ROWS 16 #define DISPLAY_WRAPPER_LINES 2 +typedef void (*LcdWrapperCallback) (const char data[][DISPLAY_WRAPPER_ROWS], uint8_t lines, uint8_t rows); + /** * @brief A class for the Menu class to print information * @@ -54,6 +56,7 @@ class LcdWrapper : public DisplayWrapper { * @param line */ void setCursor(uint8_t row, uint8_t line) override; + void setCallback(LcdWrapperCallback callback) { this->callback = callback; } /** * @brief Save the data to be printed @@ -65,6 +68,7 @@ class LcdWrapper : public DisplayWrapper { private: LiquidCrystal_I2C* lcd; + LcdWrapperCallback callback = nullptr; char data[DISPLAY_WRAPPER_LINES][DISPLAY_WRAPPER_ROWS]; uint8_t cursorRow = 0; diff --git a/src/main.cpp b/src/main.cpp index fdc5abb..215ac1c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,8 +59,9 @@ bool wifiIsActive; void i2cScanner(void); void makeMenu(void); void restart(void); -void recieveCallback (const uint8_t * mac, const uint8_t *incomingData, int len); +void receiveCallback (const uint8_t * mac, const uint8_t *incomingData, int len); void sendCallback (const uint8_t *mac_addr, esp_now_send_status_t status); +void lcdWrapperCallback (const char data[][DISPLAY_WRAPPER_ROWS], uint8_t lines, uint8_t rows); void setup() { @@ -78,7 +79,6 @@ void setup() { mainBattery = new Battery(35, 692000, 218500); controlPad = new ControlPad(); - controlPad->setMenuControl(main_m); Wire.begin(21, 19); Wire.setClock(400000); @@ -104,7 +104,7 @@ void setup() { outputBuf = new OutputBuf(); std::cout.rdbuf(outputBuf); - Network::connectEspNow(recieveCallback, sendCallback); + Network::connectEspNow(receiveCallback, sendCallback); std::cout << "Welcome to Kleiax-Rover" << std::endl; std::cout << "All actions from the main program run on Core -> " << xPortGetCoreID() << std::endl; @@ -118,6 +118,7 @@ void setup() { lcd->setCursor(0, 1); lcd->print("Press PS-Button"); lcdWrapper = new LcdWrapper(lcd); + lcdWrapper->setCallback(lcdWrapperCallback); makeMenu(); @@ -125,23 +126,6 @@ void setup() { } void loop() { - static uint32_t lastMillis = 0; - if (millis() - lastMillis > 5000) { - lastMillis = millis(); - char blub[] = {"Du dumme Bla"}; - esp_err_t result = esp_now_send(Network::getBroadcastAddress(), (uint8_t *) blub, sizeof(blub)); - - if (result == ESP_OK) { - Serial.println("Sent with success"); - } - else { - Serial.println("Error sending the data"); - } - // debugMqtt->sendMsg(Loglevel::info, "Alive"); - - } - - if (wifiIsActive) { DebugTimes wifiTime; Network::checkWiFi(); @@ -150,6 +134,7 @@ void loop() { } driveManager->loop(); controlPad->loop(); + main_m->update(); lcdWrapper->loop(); mainBattery->loop(); @@ -276,6 +261,8 @@ void makeMenu() { pidr_m->setEntry(0, "P-Part", (uint8_t) moveController.getPID(1)->GetKp()); pidr_m->setEntry(1, "I-Part", (uint8_t) moveController.getPID(1)->GetKi()); pidr_m->setEntry(2, "D-Part", (uint8_t) moveController.getPID(1)->GetKd()); + + controlPad->setMenuControl(main_m); } void restart(void) { @@ -285,7 +272,7 @@ void restart(void) { ESP.restart(); } -void recieveCallback (const uint8_t * mac, const uint8_t *incomingData, int len) { +void receiveCallback (const uint8_t * mac, const uint8_t *incomingData, int len) { if (len != sizeof(ControlPadInput)) return; controlPad->insertData(incomingData); @@ -295,3 +282,13 @@ void sendCallback (const uint8_t *mac_addr, esp_now_send_status_t status) { if (status != ESP_NOW_SEND_SUCCESS) std::cout << "sendCallback - Delivery Fail" << std::endl; } + +void lcdWrapperCallback (const char data[][DISPLAY_WRAPPER_ROWS], uint8_t lines, uint8_t rows) { + if (!controlPad->isControlPadConnected()) + return; + + esp_err_t result = esp_now_send(Network::getBroadcastAddress(), (uint8_t *) data, lines * rows); + + if (result != ESP_OK) + Serial.println("Error sending the data"); +}