diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index 77faf997eb10b734de934e880d475b53380189a3..31cc37817df31c90a24f2c721dcb7b5bb5b0c186 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 9158515d319e61cfc99f4eaa7c1984bff5c61edb..137982583afbacdfdedd8fca606b158f4d2e515c 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 32d5563b18e2ca7ed606820dd2af7bf6a1341162..0f70716439012c960ccd16ef1a6029bc51fad28b 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 79f437896f134ad48125bef3b16f6e78ad60f424..e6ea141eadbc17520b4f0279c5b12ad6e94b7fe3 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 00855c32cfb04b5e49c0b1b9341eea2c9ae51c0d..0e792bf28945767a75f4f64f12eb619dae84a2bb 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 1faf0f84611b36b9e182c38852a26a1cb4a05c33..9d678f612e13d9cc128d229d9e1956f9705aed71 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 0000000000000000000000000000000000000000..09f07474f57ca0b8a5c22805aa711413cf380f78 --- /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 0000000000000000000000000000000000000000..5fdece2f3765d49eb50c824e759a0a817fb25771 --- /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 bce0c2943bcbd270f111983b2ce6b7ff7002afb8..aae2b97758d65807736c8d536338a4258746ab19 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);