diff --git a/CMakeLists.txt b/CMakeLists.txt index 478d2431d078007fc009c4968ffc7ae1dee97d31..cbd46c1eb554e1898822d969ec8dbf91b5f42a18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,6 +109,14 @@ add_executable(automated-antennas-entry src/entrypoints/Groundstation/AutomatedA target_include_directories(automated-antennas-entry PRIVATE ${OBSW_INCLUDE_DIRS}) sbs_target(automated-antennas-entry stm32f407vg_stm32f4discovery) +add_executable(test-automated-radio + src/entrypoints/Groundstation/Automated/test-automated-radio.cpp + ${ANTENNAS} ${GROUNDSTATION_COMMON} ${GROUNDSTATION_AUTOMATED} +) +target_include_directories(test-automated-radio PRIVATE ${OBSW_INCLUDE_DIRS}) +# target_compile_definitions(test-automated-radio PRIVATE NO_SD_LOGGING) +sbs_target(test-automated-radio stm32f767zi_automated_antennas) + add_executable(test-steps src/entrypoints/Groundstation/Automated/test-steps.cpp ${ANTENNAS}) target_include_directories(test-steps PRIVATE ${OBSW_INCLUDE_DIRS}) # target_compile_definitions(test-steps PRIVATE NO_SD_LOGGING) diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index b24fa9aa3e7fd8622d36ea62d6575630760db944..8819a4ddc2eb234eee5fd2dde61afd01c5b49cf0 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -139,6 +139,12 @@ set(GROUNDSTATION_BASE src/boards/Groundstation/Common/HubBase.cpp ) +set(GROUNDSTATION_AUTOMATED + src/boards/Groundstation/Automated/Radio/Radio.cpp + src/boards/Groundstation/Automated/Radio/RadioStatus.cpp + src/boards/Groundstation/Automated/Hub.cpp +) + set(ANTENNAS src/boards/Groundstation/Automated/Actuators/Actuators.cpp src/boards/Groundstation/Automated/Sensors/Sensors.cpp diff --git a/src/boards/Groundstation/Automated/Buses.h b/src/boards/Groundstation/Automated/Buses.h index ea59ac3f98a0f4717837c03cd479f1b4920e72b9..cf35a02e4bd2894ccda7b22e82fe87163f126c79 100644 --- a/src/boards/Groundstation/Automated/Buses.h +++ b/src/boards/Groundstation/Automated/Buses.h @@ -21,6 +21,7 @@ */ #pragma once +#include <drivers/spi/SPIBus.h> #include <drivers/usart/USART.h> #include <utils/ModuleManager/ModuleManager.hpp> @@ -33,7 +34,13 @@ class Buses : public Boardcore::Module public: Boardcore::USART usart2; Boardcore::USART uart4; + Boardcore::SPIBus radio_bus; + Boardcore::SPIBus ethernet_bus; - Buses() : usart2(USART2, 115200), uart4(UART4, 115200) {} + Buses() + : usart2(USART2, 115200), uart4(UART4, 115200), radio_bus(SPI1), + ethernet_bus(SPI4) + { + } }; } // namespace Antennas \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Hub.cpp b/src/boards/Groundstation/Automated/Hub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5420ace00690f2a7ad3f3b41f8f1578f705f2eb --- /dev/null +++ b/src/boards/Groundstation/Automated/Hub.cpp @@ -0,0 +1,56 @@ +/* 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 "Hub.h" + +#include <Groundstation/Automated/Radio/Radio.h> +#include <Groundstation/Automated/Radio/RadioStatus.h> +#include <Groundstation/Common/Config/GeneralConfig.h> +#include <Groundstation/Common/Ports/Serial.h> + +using namespace Antennas; +using namespace Boardcore; +using namespace Groundstation; + +void Hub::dispatchOutgoingMsg(const mavlink_message_t& msg) +{ + // TODO: Dispatch to correct radio using mavlink ids + + bool send_ok = false; + + RadioMain* radio = ModuleManager::getInstance().get<RadioMain>(); + send_ok |= radio->sendMsg(msg); + + // If both of the sends went wrong, just send a nack + if (!send_ok) + { + sendNack(msg); + } +} + +void Hub::dispatchIncomingMsg(const mavlink_message_t& msg) +{ + Serial* serial = ModuleManager::getInstance().get<Serial>(); + serial->sendMsg(msg); + + // TODO: Add UDP dispatch +} \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Hub.h b/src/boards/Groundstation/Automated/Hub.h new file mode 100644 index 0000000000000000000000000000000000000000..ebb323667fe73c17f36839a005961c19be451c9a --- /dev/null +++ b/src/boards/Groundstation/Automated/Hub.h @@ -0,0 +1,54 @@ +/* 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 <Groundstation/Common/HubBase.h> +#include <common/Mavlink.h> + +#include <utils/ModuleManager/ModuleManager.hpp> + +namespace Antennas +{ + +/** + * @brief Central hub connecting all outgoing and ingoing modules. + */ +class Hub : public Groundstation::HubBase +{ +public: + Hub() {} + + /** + * @brief Dispatch to the correct interface and outgoing packet (gs -> + * rocket). + */ + void dispatchOutgoingMsg(const mavlink_message_t& msg) override; + + /** + * @brief Dispatch to the correct interface and incoming packet (rocket -> + * gs). + */ + void dispatchIncomingMsg(const mavlink_message_t& msg) override; +}; + +} // namespace Antennas \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Radio/Radio.cpp b/src/boards/Groundstation/Automated/Radio/Radio.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee543417a9155c7234664ce932862ab1d6b88134 --- /dev/null +++ b/src/boards/Groundstation/Automated/Radio/Radio.cpp @@ -0,0 +1,97 @@ +/* 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 "Radio.h" + +#include <Groundstation/Automated/Buses.h> +#include <Groundstation/Automated/Hub.h> +#include <Groundstation/Automated/Radio/RadioStatus.h> +#include <Groundstation/Common/Ports/Serial.h> +#include <interfaces-impl/hwmapping.h> +#include <radio/SX1278/SX1278Frontends.h> + +using namespace Antennas; +using namespace Boardcore; +using namespace miosix; + +void __attribute__((used)) EXTI6_IRQHandlerImpl() +{ + ModuleManager::getInstance().get<RadioMain>()->handleDioIRQ(); +} + +void __attribute__((used)) EXTI4_IRQHandlerImpl() +{ + ModuleManager::getInstance().get<RadioMain>()->handleDioIRQ(); +} + +void __attribute__((used)) EXTI5_IRQHandlerImpl() +{ + ModuleManager::getInstance().get<RadioMain>()->handleDioIRQ(); +} + +namespace Antennas +{ + +bool RadioMain::start() +{ +#ifdef SKYWARD_GS_MAIN_USE_BACKUP_RF + std::unique_ptr<SX1278::ISX1278Frontend> frontend = + std::make_unique<EbyteFrontend>(radio::txen::getPin(), + radio::rxen::getPin()); +#else + std::unique_ptr<SX1278::ISX1278Frontend> frontend = + std::make_unique<Skyward433Frontend>(); +#endif + + std::unique_ptr<Boardcore::SX1278Fsk> sx1278 = + std::make_unique<Boardcore::SX1278Fsk>( + ModuleManager::getInstance().get<Antennas::Buses>()->radio_bus, + radio::cs::getPin(), radio::dio0::getPin(), radio::dio1::getPin(), + radio::dio3::getPin(), SPI::ClockDivider::DIV_64, + std::move(frontend)); + + // First check if the device is even connected + bool present = sx1278->checkVersion(); + + ModuleManager::getInstance().get<RadioStatus>()->setMainRadioPresent( + present); + + if (present) + { + // Configure the radio + if (sx1278->configure(Common::MAIN_RADIO_CONFIG) != + SX1278Fsk::Error::NONE) + { + return false; + } + + // Initialize if only if present + if (!RadioBase::start(std::move(sx1278))) + { + return false; + } + } + + return true; +} + +} // namespace Antennas \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Radio/Radio.h b/src/boards/Groundstation/Automated/Radio/Radio.h new file mode 100644 index 0000000000000000000000000000000000000000..4feaa87380a4cec0bfcc8ab73778a6ba2f730dd5 --- /dev/null +++ b/src/boards/Groundstation/Automated/Radio/Radio.h @@ -0,0 +1,36 @@ +/* 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 <Groundstation/Common/Radio/RadioBase.h> + +namespace Antennas +{ + +class RadioMain : public Groundstation::RadioBase, public Boardcore::Module +{ +public: + [[nodiscard]] bool start(); +}; + +} // namespace Antennas \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Radio/RadioStatus.cpp b/src/boards/Groundstation/Automated/Radio/RadioStatus.cpp new file mode 100644 index 0000000000000000000000000000000000000000..164e2a5905bf045bc1968992c49ed56c6ec01c06 --- /dev/null +++ b/src/boards/Groundstation/Automated/Radio/RadioStatus.cpp @@ -0,0 +1,89 @@ +/* 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 "RadioStatus.h" + +#include <Groundstation/Automated/Radio/Radio.h> +#include <Groundstation/Common/Config/GeneralConfig.h> +#include <Groundstation/Common/HubBase.h> +#include <common/Mavlink.h> +#include <drivers/timer/TimestampTimer.h> + +using namespace Boardcore; +using namespace Groundstation; +using namespace Antennas; + +bool RadioStatus::isMainRadioPresent() { return main_radio_present; } + +bool RadioStatus::start() +{ + if (!ActiveObject::start()) + { + return false; + } + + return true; +} + +void RadioStatus::setMainRadioPresent(bool present) +{ + main_radio_present = present; +} + +void RadioStatus::setPayloadRadioPresent(bool present) +{ + payload_radio_present = present; +} + +void RadioStatus::run() +{ + while (!shouldStop()) + { + miosix::Thread::sleep(RADIO_STATUS_PERIOD); + + mavlink_receiver_tm_t tm = {0}; + tm.timestamp = TimestampTimer::getTimestamp(); + + if (main_radio_present) + { + tm.main_radio_present = 1; + + auto stats = + ModuleManager::getInstance().get<RadioMain>()->getStats(); + tm.main_packet_tx_error_count = stats.send_errors; + tm.main_tx_bitrate = main_tx_bitrate.update(stats.bits_tx_count); + tm.main_packet_rx_success_count = stats.packet_rx_success_count; + tm.main_packet_rx_drop_count = stats.packet_rx_drop_count; + tm.main_rx_bitrate = main_rx_bitrate.update(stats.bits_rx_count); + tm.main_rx_rssi = stats.rx_rssi; + tm.main_rx_fei = stats.rx_fei; + + last_main_stats = stats; + } + + mavlink_message_t msg; + mavlink_msg_receiver_tm_encode(GS_SYSTEM_ID, GS_COMPONENT_ID, &msg, + &tm); + + ModuleManager::getInstance().get<HubBase>()->dispatchIncomingMsg(msg); + } +} \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Radio/RadioStatus.h b/src/boards/Groundstation/Automated/Radio/RadioStatus.h new file mode 100644 index 0000000000000000000000000000000000000000..81282807e8a68da137e49a0fac73f038db4c67fd --- /dev/null +++ b/src/boards/Groundstation/Automated/Radio/RadioStatus.h @@ -0,0 +1,110 @@ +/* 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/Config/RadioConfig.h> +#include <Groundstation/Common/Radio/RadioBase.h> +#include <utils/collections/CircularBuffer.h> + +#include <utils/ModuleManager/ModuleManager.hpp> + +namespace Antennas +{ + +/** + * @brief Utility to calculate the bitrate + */ +template <unsigned int WINDOW_SIZE, unsigned int PERIOD> +class BitrateCalculator +{ +public: + BitrateCalculator() {} + + /** + * @brief Update the calculator, should be called every PERIOD ms + */ + uint16_t update(uint32_t sample) + { + if (window.isFull()) + { + uint32_t last = window.pop(); + window.put(sample); + + uint16_t delta = sample - last; + + // window size is in ms, we want the result in s + return delta * 1000 / Groundstation::RADIO_BITRATE_WINDOW_SIZE; + } + else + { + window.put(sample); + return 0; + } + } + +private: + Boardcore::CircularBuffer<uint32_t, WINDOW_SIZE / PERIOD> window; +}; + +/** + * @brief Class responsible for keeping track of radio status and metrics. + */ +class RadioStatus : public Boardcore::Module, private Boardcore::ActiveObject +{ +public: + RadioStatus() {} + + bool start(); + + /** + * @brief Check wether the main radio was found during boot. + */ + bool isMainRadioPresent(); + + /** + * @brief Check wether the payload radio was found during boot. + */ + bool isPayloadRadioPresent(); + + void setMainRadioPresent(bool present); + void setPayloadRadioPresent(bool present); + +private: + void run() override; + + Groundstation::RadioStats last_main_stats = {0}; + Groundstation::RadioStats last_payload_stats = {0}; + + BitrateCalculator<Groundstation::RADIO_BITRATE_WINDOW_SIZE, + Groundstation::RADIO_STATUS_PERIOD> + main_tx_bitrate; + BitrateCalculator<Groundstation::RADIO_BITRATE_WINDOW_SIZE, + Groundstation::RADIO_STATUS_PERIOD> + main_rx_bitrate; + + bool main_radio_present = false; + bool payload_radio_present = false; +}; + +} // namespace Antennas \ No newline at end of file diff --git a/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp b/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp index 2dbc76a2bd5d0975f05271ef57edf264bb9ef72e..025c181a47aa4448e41ab8cb5f3b512bf5815f37 100644 --- a/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp +++ b/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp @@ -1,5 +1,5 @@ /* Copyright (c) 2023 Skyward Experimental Rocketry - * Author: Riccardo Musso + * Authors: Riccardo Musso, Emilio Corigliano * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,6 +23,11 @@ #include <drivers/timer/TimestampTimer.h> #include <miosix.h> +#include <Groundstation/Base/Buses.h> +#include <Groundstation/Base/Hub.h> +#include <Groundstation/Base/Radio/Radio.h> +#include <Groundstation/Base/Radio/RadioStatus.h> +#include <Groundstation/Common/Ports/Serial.h> #include "Converter.h" using namespace miosix; diff --git a/src/entrypoints/Groundstation/Automated/test-automated-radio.cpp b/src/entrypoints/Groundstation/Automated/test-automated-radio.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11b9451e4fdc43d58764440fb8c910ee38be7f8b --- /dev/null +++ b/src/entrypoints/Groundstation/Automated/test-automated-radio.cpp @@ -0,0 +1,114 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Mor, Emilio Corigliano + * + * 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 <Groundstation/Automated/Buses.h> +#include <Groundstation/Automated/Hub.h> +#include <Groundstation/Automated/Radio/Radio.h> +#include <Groundstation/Automated/Radio/RadioStatus.h> +#include <Groundstation/Common/Ports/Serial.h> +#include <miosix.h> + +using namespace Groundstation; +using namespace Antennas; +using namespace Boardcore; +using namespace miosix; + +void spinLoop() +{ + while (1) + { + Thread::sleep(1000); + } +} + +void errorLoop() +{ + while (1) + { + led1On(); + Thread::sleep(100); + led1Off(); + Thread::sleep(100); + } +} + +int main() +{ + ledOff(); + + Hub *hub = new Hub(); + Buses *buses = new Buses(); + Serial *serial = new Serial(); + RadioMain *radio_main = new RadioMain(); + RadioStatus *radio_status = new RadioStatus(); + + ModuleManager &modules = ModuleManager::getInstance(); + + bool ok = true; + + ok &= modules.insert<HubBase>(hub); + ok &= modules.insert(buses); + ok &= modules.insert(serial); + ok &= modules.insert(radio_main); + ok &= modules.insert(radio_status); + + // If insertion failed, stop right here + if (!ok) + { + printf("[error] Failed to insert all modules!\n"); + errorLoop(); + } + + // Ok now start them + + ok &= serial->start(); + if (!ok) + { + printf("[error] Failed to start serial!\n"); + } + + ok &= radio_main->start(); + if (!ok) + { + printf("[error] Failed to start main radio!\n"); + } + + ok &= radio_status->start(); + if (!ok) + { + printf("[error] Failed to start radio status!\n"); + } + + if (radio_status->isMainRadioPresent()) + { + led2On(); + } + + if (!ok) + { + errorLoop(); + } + + led1On(); + spinLoop(); + return 0; +} \ No newline at end of file