From e039c7f5018f365e3b2486cdb47a076c100fc9ce Mon Sep 17 00:00:00 2001 From: EmilioCorigliano <emilio.corigliano@mail.polimi.it> Date: Mon, 3 Apr 2023 09:17:44 +0200 Subject: [PATCH] [UBXGPSSerial] Correctly using the USART bus instead of creating it --- src/shared/sensors/UBXGPS/UBXGPSSerial.cpp | 30 +++++++++---------- src/shared/sensors/UBXGPS/UBXGPSSerial.h | 11 ++++--- .../algorithms/NAS/test-nas-parafoil.cpp | 12 ++++++-- src/tests/sensors/test-ubxgps-serial.cpp | 11 +++++-- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp b/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp index 0cfb238df..02ef13b4c 100644 --- a/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp +++ b/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp @@ -31,15 +31,12 @@ using namespace miosix; namespace Boardcore { -UBXGPSSerial::UBXGPSSerial(USARTInterface::Baudrate baudrate, - uint8_t sampleRate, USARTType* usartNumber, +UBXGPSSerial::UBXGPSSerial(USART& usart, USARTInterface::Baudrate baudrate, + uint8_t sampleRate, USARTInterface::Baudrate defaultBaudrate) + : usart(usart), baudrate(baudrate), defaultBaudrate(defaultBaudrate), + sampleRate(sampleRate) { - this->usart = nullptr; - this->baudrate = baudrate; - this->defaultBaudrate = defaultBaudrate; - this->sampleRate = sampleRate; - this->usartNumber = usartNumber; } bool UBXGPSSerial::init() @@ -173,8 +170,8 @@ bool UBXGPSSerial::setBaudrateAndUBX(bool safe) bool UBXGPSSerial::setSerialCommunication() { - usart = new USART(usartNumber, defaultBaudrate); - usart->init(); + usart.setBaudrate(defaultBaudrate); + usart.init(); // Change the baudrate only if it is different than the default if (baudrate != defaultBaudrate) { @@ -187,7 +184,7 @@ bool UBXGPSSerial::setSerialCommunication() } miosix::Thread::sleep(100); - usart->setBaudrate(baudrate); + usart.setBaudrate(baudrate); return true; } @@ -256,8 +253,9 @@ bool UBXGPSSerial::readUBXFrame(UBXFrame& frame) size_t i = 0; while (i < 2) { + // TODO: Check if this can deadlock uint8_t c; - if (usart->read(&c, 1) <= 0) // No more data available + if (usart.read(&c, 1) <= 0) // No more data available return false; if (c == UBXFrame::PREAMBLE[i]) @@ -277,10 +275,10 @@ bool UBXGPSSerial::readUBXFrame(UBXFrame& frame) } } - if (usart->read(&frame.message, 2) <= 0 || - usart->read(&frame.payloadLength, 2) <= 0 || - usart->read(frame.payload, frame.getRealPayloadLength()) <= 0 || - usart->read(frame.checksum, 2) <= 0) + if (usart.read(&frame.message, 2) <= 0 || + usart.read(&frame.payloadLength, 2) <= 0 || + usart.read(frame.payload, frame.getRealPayloadLength()) <= 0 || + usart.read(frame.checksum, 2) <= 0) return false; if (!frame.isValid()) @@ -303,7 +301,7 @@ bool UBXGPSSerial::writeUBXFrame(const UBXFrame& frame) uint8_t packedFrame[frame.getLength()]; frame.writePacked(packedFrame); - if (usart->write(packedFrame, frame.getLength()) < 0) + if (usart.write(packedFrame, frame.getLength()) < 0) { LOG_ERR(logger, "Failed to write ubx message"); return false; diff --git a/src/shared/sensors/UBXGPS/UBXGPSSerial.h b/src/shared/sensors/UBXGPS/UBXGPSSerial.h index 55cba451c..02fb535e8 100644 --- a/src/shared/sensors/UBXGPS/UBXGPSSerial.h +++ b/src/shared/sensors/UBXGPS/UBXGPSSerial.h @@ -56,16 +56,16 @@ public: /** * @brief Construct a new UBXGPSSerial object. * + * @param usart USART bus to be used for communication. * @param baudrate Baudrate to communicate with the device (max: 921600, * min: 4800 for NEO-M9N). * @param sampleRate GPS sample rate (max: 25 for NEO-M9N). - * @param serialPortNumber Number of the serial port connected to the GPS. - * @param serialPortName Name of the file for the gps device. * @param defaultBaudrate Startup baudrate (38400 for NEO-M9N). */ - UBXGPSSerial( + explicit UBXGPSSerial( + USART &usart, USARTInterface::Baudrate baudrate = USARTInterface::Baudrate::B921600, - uint8_t sampleRate = 10, USARTType *usartNumber = USART2, + uint8_t sampleRate = 10, USARTInterface::Baudrate defaultBaudrate = USARTInterface::Baudrate::B38400); @@ -161,11 +161,10 @@ private: void run() override; + USART &usart; // The usart interface Boardcore::USARTInterface::Baudrate baudrate; Boardcore::USARTInterface::Baudrate defaultBaudrate; uint8_t sampleRate; // [Hz] - USARTType *usartNumber; - USART *usart; // The usart interface mutable miosix::FastMutex mutex; UBXGPSData threadSample{}; diff --git a/src/tests/algorithms/NAS/test-nas-parafoil.cpp b/src/tests/algorithms/NAS/test-nas-parafoil.cpp index 7af7e41c3..96af56025 100644 --- a/src/tests/algorithms/NAS/test-nas-parafoil.cpp +++ b/src/tests/algorithms/NAS/test-nas-parafoil.cpp @@ -48,11 +48,17 @@ Vector2f startPos = Vector2f(45.501141, 9.156281); NAS* nas; SPIBus spi1(SPI1); -MPU9250* imu = nullptr; +MPU9250* imu = nullptr; +USART usart(USART2, USARTInterface::Baudrate::B38400); UBXGPSSerial* gps = nullptr; int main() { + u2rx1::getPin().alternateFunction(7); + u2rx1::getPin().mode(Mode::ALTERNATE); + u2tx1::getPin().alternateFunction(7); + u2tx1::getPin().mode(Mode::ALTERNATE); + init(); nas = new NAS(getEKConfig()); @@ -109,10 +115,12 @@ void setInitialOrientation() void init() { + usart.init(); + imu = new MPU9250(spi1, sensors::mpu9250::cs::getPin()); imu->init(); - gps = new UBXGPSSerial(USARTInterface::Baudrate::B38400, 10, USART2, + gps = new UBXGPSSerial(usart, USARTInterface::Baudrate::B38400, 10, USARTInterface::Baudrate::B38400); gps->init(); gps->start(); diff --git a/src/tests/sensors/test-ubxgps-serial.cpp b/src/tests/sensors/test-ubxgps-serial.cpp index 063bf6df8..ef44cf5c8 100644 --- a/src/tests/sensors/test-ubxgps-serial.cpp +++ b/src/tests/sensors/test-ubxgps-serial.cpp @@ -36,8 +36,15 @@ int main() { printf("Welcome to the ublox test\n"); + u2rx1::getPin().alternateFunction(7); + u2rx1::getPin().mode(Mode::ALTERNATE); + u2tx1::getPin().alternateFunction(7); + u2tx1::getPin().mode(Mode::ALTERNATE); + + USART usart(USART2, USARTInterface::Baudrate::B38400); + // Keep GPS baud rate at default for easier testing - UBXGPSSerial gps(USARTInterface::Baudrate::B38400, RATE, USART2, + UBXGPSSerial gps(usart, USARTInterface::Baudrate::B38400, RATE, USARTInterface::Baudrate::B9600); UBXGPSData dataGPS; printf("Gps allocated\n"); @@ -68,13 +75,11 @@ int main() while (true) { - printf("a\n"); // Give time to the thread Thread::sleep(1000 / RATE); // Sample gps.sample(); - printf("b\n"); dataGPS = gps.getLastSample(); // Print out the latest sample -- GitLab