From 5fdd48c60d6fe8be2c31d0028d568bc4e522c96e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <niccolo.caruso@skywarder.eu> Date: Wed, 2 Oct 2024 15:17:14 +0200 Subject: [PATCH] [GS] Using now serial on USART4 Now using USART4 for serial. New class even if might not be good, will change to inherit from SerialTransciver. --- cmake/dependencies.cmake | 1 + src/boards/Groundstation/Automated/Hub.cpp | 4 +- src/boards/Groundstation/Automated/Hub.h | 4 +- src/boards/Groundstation/LyraGS/Base/Hub.cpp | 4 +- src/boards/Groundstation/LyraGS/Base/Hub.h | 4 +- src/boards/Groundstation/LyraGS/Buses.h | 1 - .../LyraGS/Ports/SerialLyraGS.cpp | 70 +++++++++++++++++ .../Groundstation/LyraGS/Ports/SerialLyraGS.h | 78 +++++++++++++++++++ .../Groundstation/lyra-gs-entry.cpp | 4 +- 9 files changed, 159 insertions(+), 11 deletions(-) create mode 100644 src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.cpp create mode 100644 src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.h diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index 77faf997e..31cc37817 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -122,4 +122,5 @@ set (LYRA_GS src/boards/Groundstation/Automated/Actuators/Actuators.cpp src/boards/Groundstation/Automated/Sensors/Sensors.cpp src/boards/Groundstation/Automated/PinHandler/PinHandler.cpp + src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.cpp ) \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Hub.cpp b/src/boards/Groundstation/Automated/Hub.cpp index 9158515d3..137982583 100644 --- a/src/boards/Groundstation/Automated/Hub.cpp +++ b/src/boards/Groundstation/Automated/Hub.cpp @@ -25,9 +25,9 @@ #include <Groundstation/Automated/Actuators/Actuators.h> #include <Groundstation/Automated/SMA/SMA.h> #include <Groundstation/Common/Config/GeneralConfig.h> -#include <Groundstation/Common/Ports/Serial.h> #include <Groundstation/LyraGS/BoardStatus.h> #include <Groundstation/LyraGS/Ports/Ethernet.h> +#include <Groundstation/LyraGS/Ports/SerialLyraGS.h> #include <Groundstation/LyraGS/Radio/Radio.h> #include <algorithms/NAS/NASState.h> #include <common/Events.h> @@ -240,7 +240,7 @@ void Hub::dispatchOutgoingMsg(const mavlink_message_t& msg) void Hub::dispatchIncomingMsg(const mavlink_message_t& msg) { - Groundstation::Serial* serial = getModule<Groundstation::Serial>(); + LyraGS::SerialLyraGS* serial = getModule<LyraGS::SerialLyraGS>(); #if !defined(NO_MAVLINK_ON_SERIAL) serial->sendMsg(msg); #else diff --git a/src/boards/Groundstation/Automated/Hub.h b/src/boards/Groundstation/Automated/Hub.h index 32d5563b1..0f7071643 100644 --- a/src/boards/Groundstation/Automated/Hub.h +++ b/src/boards/Groundstation/Automated/Hub.h @@ -24,9 +24,9 @@ #include <Groundstation/Automated/SMA/SMA.h> #include <Groundstation/Common/HubBase.h> -#include <Groundstation/Common/Ports/Serial.h> #include <Groundstation/LyraGS/BoardStatus.h> #include <Groundstation/LyraGS/Ports/Ethernet.h> +#include <Groundstation/LyraGS/Ports/SerialLyraGS.h> #include <Groundstation/LyraGS/Radio/Radio.h> #include <algorithms/NAS/NASState.h> #include <common/Mavlink.h> @@ -47,7 +47,7 @@ namespace Antennas */ class Hub : public Boardcore::InjectableWithDeps< Boardcore::InjectableBase<Groundstation::HubBase>, SMA, - LyraGS::RadioMain, Groundstation::Serial, LyraGS::EthernetGS> + LyraGS::RadioMain, LyraGS::SerialLyraGS, LyraGS::EthernetGS> { public: /** diff --git a/src/boards/Groundstation/LyraGS/Base/Hub.cpp b/src/boards/Groundstation/LyraGS/Base/Hub.cpp index 79f437896..e6ea141ea 100644 --- a/src/boards/Groundstation/LyraGS/Base/Hub.cpp +++ b/src/boards/Groundstation/LyraGS/Base/Hub.cpp @@ -23,9 +23,9 @@ #include "Hub.h" #include <Groundstation/Common/Config/GeneralConfig.h> -#include <Groundstation/Common/Ports/Serial.h> #include <Groundstation/LyraGS/BoardStatus.h> #include <Groundstation/LyraGS/Ports/Ethernet.h> +#include <Groundstation/LyraGS/Ports/SerialLyraGS.h> using namespace Groundstation; using namespace GroundstationBase; @@ -63,7 +63,7 @@ void Hub::dispatchIncomingMsg(const mavlink_message_t& msg) { LyraGS::BoardStatus* status = getModule<LyraGS::BoardStatus>(); - Serial* serial = getModule<Serial>(); + SerialLyraGS* serial = getModule<SerialLyraGS>(); serial->sendMsg(msg); if (status->isEthernetPresent()) diff --git a/src/boards/Groundstation/LyraGS/Base/Hub.h b/src/boards/Groundstation/LyraGS/Base/Hub.h index 00855c32c..0e792bf28 100644 --- a/src/boards/Groundstation/LyraGS/Base/Hub.h +++ b/src/boards/Groundstation/LyraGS/Base/Hub.h @@ -23,8 +23,8 @@ #pragma once #include <Groundstation/Common/HubBase.h> -#include <Groundstation/Common/Ports/Serial.h> #include <Groundstation/LyraGS/Ports/Ethernet.h> +#include <Groundstation/LyraGS/Ports/SerialLyraGS.h> #include <common/Mavlink.h> #include <utils/DependencyManager/DependencyManager.h> @@ -36,7 +36,7 @@ namespace GroundstationBase class Hub : public Boardcore::InjectableWithDeps< Boardcore::InjectableBase<Groundstation::HubBase>, LyraGS::BoardStatus, LyraGS::RadioMain, LyraGS::RadioPayload, - Groundstation::Serial, LyraGS::EthernetGS> + LyraGS::SerialLyraGS, LyraGS::EthernetGS> { public: /** diff --git a/src/boards/Groundstation/LyraGS/Buses.h b/src/boards/Groundstation/LyraGS/Buses.h index 1faf0f846..9d678f612 100644 --- a/src/boards/Groundstation/LyraGS/Buses.h +++ b/src/boards/Groundstation/LyraGS/Buses.h @@ -30,7 +30,6 @@ namespace LyraGS { - class Buses : public Boardcore::Injectable { public: diff --git a/src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.cpp b/src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.cpp new file mode 100644 index 000000000..09f07474f --- /dev/null +++ b/src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.cpp @@ -0,0 +1,70 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Davide Mor + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "SerialLyraGS.h" + +using namespace Groundstation; +using namespace LyraGS; + +bool SerialLyraGS::start() +{ + auto mav_handler = [this](SerialMavDriver* channel, + const mavlink_message_t& msg) { handleMsg(msg); }; + + mav_driver = std::make_unique<SerialMavDriver>(this, mav_handler, 0, 10); + + if (!mav_driver->start()) + { + return false; + } + + return true; +} + +void SerialLyraGS::sendMsg(const mavlink_message_t& msg) +{ + if (mav_driver && mav_driver->isStarted()) + { + mav_driver->enqueueMsg(msg); + } +} + +void SerialLyraGS::handleMsg(const mavlink_message_t& msg) +{ + // Dispatch the message through the hub. + getModule<HubBase>()->dispatchOutgoingMsg(msg); +} + +ssize_t SerialLyraGS::receive(uint8_t* pkt, size_t max_len) +{ + Boardcore::USART& serial = getModule<Buses>()->uart4; + size_t bytesRead = 0; + bool result = serial.readBlocking(pkt, max_len, bytesRead); + return result ? bytesRead : 0; +} + +bool SerialLyraGS::send(uint8_t* pkt, size_t len) +{ + Boardcore::USART& serial = getModule<Buses>()->uart4; + serial.write(pkt, len); + return true; +} \ No newline at end of file diff --git a/src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.h b/src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.h new file mode 100644 index 000000000..5fdece2f3 --- /dev/null +++ b/src/boards/Groundstation/LyraGS/Ports/SerialLyraGS.h @@ -0,0 +1,78 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Davide Mor + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ActiveObject.h> +#include <Groundstation/Common/Ports/EthernetBase.h> +#include <Groundstation/Common/Ports/Serial.h> +#include <Groundstation/LyraGS/BoardStatus.h> +#include <Groundstation/LyraGS/Buses.h> +#include <common/Mavlink.h> +#include <drivers/usart/USART.h> +#include <filesystem/console/console_device.h> +#include <radio/MavlinkDriver/MavlinkDriver.h> +#include <utils/DependencyManager/DependencyManager.h> + +#include <memory> + +namespace LyraGS +{ + +using SerialMavDriver = + Boardcore::MavlinkDriver<1024, 10, MAVLINK_MAX_DIALECT_PAYLOAD_SIZE>; + +/** + * @brief Class responsible for UART communication. + */ +class SerialLyraGS : public Boardcore::InjectableWithDeps< + Boardcore::InjectableBase<Groundstation::Serial>, + Buses, Groundstation::HubBase> +{ +public: + SerialLyraGS() {} + + /** + * @brief Initialize the serial module. + */ + [[nodiscard]] bool start(); + + /** + * @brief Send a mavlink message through this port. + */ + void sendMsg(const mavlink_message_t& msg); + +private: + /** + * @brief Called internally when a message is received. + */ + void handleMsg(const mavlink_message_t& msg); + + ssize_t receive(uint8_t* pkt, size_t max_len) override; + + bool send(uint8_t* pkt, size_t len) override; + + miosix::FastMutex mutex; + std::unique_ptr<SerialMavDriver> mav_driver; +}; + +} // namespace LyraGS \ No newline at end of file diff --git a/src/entrypoints/Groundstation/lyra-gs-entry.cpp b/src/entrypoints/Groundstation/lyra-gs-entry.cpp index bce0c2943..aae2b9775 100644 --- a/src/entrypoints/Groundstation/lyra-gs-entry.cpp +++ b/src/entrypoints/Groundstation/lyra-gs-entry.cpp @@ -26,11 +26,11 @@ #include <Groundstation/Automated/PinHandler/PinHandler.h> #include <Groundstation/Automated/SMA/SMA.h> #include <Groundstation/Automated/Sensors/Sensors.h> -#include <Groundstation/Common/Ports/Serial.h> #include <Groundstation/LyraGS/Base/Hub.h> #include <Groundstation/LyraGS/BoardStatus.h> #include <Groundstation/LyraGS/Buses.h> #include <Groundstation/LyraGS/Ports/Ethernet.h> +#include <Groundstation/LyraGS/Ports/SerialLyraGS.h> #include <Groundstation/LyraGS/Radio/Radio.h> #include <common/Events.h> #include <diagnostic/PrintLogger.h> @@ -138,7 +138,7 @@ int main() TaskScheduler *scheduler_low = new TaskScheduler(0); TaskScheduler *scheduler_high = new TaskScheduler(); Buses *buses = new Buses(); - Serial *serial = new Serial(); + SerialLyraGS *serial = new SerialLyraGS(); LyraGS::RadioMain *radio_main = new LyraGS::RadioMain(dipRead.mainHasBackup, dipRead.mainTXenable); LyraGS::BoardStatus *board_status = new LyraGS::BoardStatus(dipRead.isARP); -- GitLab