diff --git a/.vscode/settings.json b/.vscode/settings.json index b9eecf3..99f1827 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -82,6 +82,7 @@ "cSpell.words": [ "Ahnung", "akku", + "ardui", "blox", "bluedroid", "gast", diff --git a/include/config.h b/include/config.h index 863d79a..4b52d94 100644 --- a/include/config.h +++ b/include/config.h @@ -33,3 +33,6 @@ #define UBLOX_GNSS_SPI_CIPO 4 #define UBLOX_GNSS_SPI_SCK 5 ///@} + +#define RF24_CSN_PIN 18 +#define RF24_CE_PIN 2 diff --git a/lib/ControllPad/controlPad.cpp b/lib/ControllPad/controlPad.cpp index 7b8aa9c..377b96a 100644 --- a/lib/ControllPad/controlPad.cpp +++ b/lib/ControllPad/controlPad.cpp @@ -11,41 +11,69 @@ #include "controlPad.h" -ControlPad::ControlPad() { +ControlPad::ControlPad(SPIClass *spiPort, uint8_t cePin, uint8_t csnPin) { + this->radio = new RF24(cePin, csnPin); + std::cout << "ControlPad::ControlPad: sizeof ControlPadInput in byte: " << sizeof(ControlPadInput) << std::endl; + + + if (!this->radio->begin(spiPort)) { + std::cout << "ControlPad::ControlPad: Error radio->begin!" << std::endl; + while(true); + } + + // this->radio->setAddressWidth(5); + // this->radio->setChannel(7); + // this->radio->setDataRate(RF24_1MBPS); + // this->radio->setAutoAck(true); + // this->radio->enableDynamicPayloads(); + // this->radio->enableAckPayload(); + // this->radio->setCRCLength(RF24_CRC_16); + + this->radio->setPALevel(RF24_PA_LOW); + + // this->radio->openWritingPipe(this->address[1]); + this->radio->openReadingPipe(0, this->address[0]); + + this->radio->startListening(); + + // this->radio->printPrettyDetails(); + this->radio->printPrettyDetails(); } 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; + // return false; + // } - if (this->menuControl && this->controlInput.buttons > 0 && this->updated) - if (!this->firstButtonPress) - this->menuControl->printMenu(); + // if (this->menuControl && this->controlInput.buttons > 0 && this->updated) + // if (!this->firstButtonPress) + // this->menuControl->printMenu(); - 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 (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->menuControl->update(); + // this->menuControl->update(); return true; } @@ -55,3 +83,12 @@ const ControlPadInput * ControlPad::getControlPadDataPtr(uint16_t maxElapsedTime return nullptr; return &this->controlInput; } + +void ControlPad::receiveData() { + this->radio->startListening(); + if (this->radio->available()) { + ControlPadInput lastInput = this->controlInput; + this->radio->read(&this->controlInput, sizeof(ControlPadInput)); + std::cout << "Data RF24: " << this->controlInput.counter << std::endl; + } +} diff --git a/lib/ControllPad/controlPad.h b/lib/ControllPad/controlPad.h index 06ffdf5..24a57ec 100644 --- a/lib/ControllPad/controlPad.h +++ b/lib/ControllPad/controlPad.h @@ -12,12 +12,16 @@ #pragma once #include +#include +#include +#include +// #include #include "controlPadInput.h" class ControlPad { public: - ControlPad(); + ControlPad(SPIClass *spiPort, uint8_t cePin, uint8_t csnPin); bool loop(); @@ -29,12 +33,17 @@ class ControlPad { private: MenuControl* menuControl = nullptr; + RF24* radio; ControlPadInput controlInput; + void receiveData(); + bool connected = false; bool updated = false; bool firstButtonPress = false; + const uint8_t address[2][6] = { "rover", "ardui" }; + uint16_t disconnectTime = 50; uint16_t loopDelay = 5; diff --git a/platformio.ini b/platformio.ini index 9376e9b..d6ca304 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,6 +24,7 @@ lib_deps = bblanchon/ArduinoJson@^6.20.0 mprograms/QMC5883LCompass@^1.1.1 https://git.kleiax.de/PlatformIO-Libs/Menu.git#v1.0 + nrf24/RF24@^1.4.5 upload_port = COM3 [env:release] diff --git a/src/driveModi/driveManager.cpp b/src/driveModi/driveManager.cpp index acd61c4..17a8391 100644 --- a/src/driveModi/driveManager.cpp +++ b/src/driveModi/driveManager.cpp @@ -15,12 +15,11 @@ Modi& operator++(Modi& m, int) { return m = (m == Modi::TestMode) ? Modi::Off : static_cast(static_cast(m)+1); } -DriveManager::DriveManager(MoveControl *moveControl, const ControlPadInput *input, bool wifi) { +DriveManager::DriveManager(MoveControl *moveControl, SPIClass *spiPort, const ControlPadInput *input, bool wifi) { this->moveControl = moveControl; 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); + this->spiPort = spiPort; this->navigation = new Navigation(spiPort, UBLOX_GNSS_SPI_CS); if (wifi) this->navigation->initNtrip(NTRIP_HOST, NTRIP_PORT, NTRIP_MOUNT_POINT, NTRIP_USER, NTRIP_PASSWORD); diff --git a/src/driveModi/driveManager.h b/src/driveModi/driveManager.h index d27edf9..6dbaec7 100644 --- a/src/driveModi/driveManager.h +++ b/src/driveModi/driveManager.h @@ -52,7 +52,7 @@ class DriveManager { * * @param moveControl */ - DriveManager(MoveControl *moveControl, const ControlPadInput *input, bool wifi = false); + DriveManager(MoveControl *moveControl, SPIClass *spiPort, const ControlPadInput *input, bool wifi = false); /** * @brief Destroy the Drive Manager object diff --git a/src/main.cpp b/src/main.cpp index 45c62c1..dc91c48 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "driveModi/driveManager.h" #include "OutputBuf/outputBuf.h" @@ -50,6 +51,7 @@ LcdWrapper* lcdWrapper; OutputBuf* outputBuf; DebugMqtt* debugMqtt = nullptr; Battery* mainBattery; +SPIClass* spiPort; ControlPad* controlPad; bool wifiIsActive; @@ -65,8 +67,11 @@ void setup() { DebugTimes setupTime; char wifiIndicator = 'X'; + spiPort = new SPIClass(HSPI); + spiPort->begin(UBLOX_GNSS_SPI_SCK, UBLOX_GNSS_SPI_CIPO, UBLOX_GNSS_SPI_COPI, UBLOX_GNSS_SPI_CS); + mainBattery = new Battery(35, 692000, 218500); - controlPad = new ControlPad(); + controlPad = new ControlPad(spiPort, RF24_CE_PIN, RF24_CSN_PIN); controlPad->setMenuControl(main_m); Wire.begin(21, 19); @@ -97,7 +102,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; - driveManager = new DriveManager(&moveController, controlPad->getControlPadDataPtr(), wifiIsActive); + driveManager = new DriveManager(&moveController, spiPort, controlPad->getControlPadDataPtr(), wifiIsActive); outputBuf->activateMqtt(false);