diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index da0f6041c7baedef9891ea0b83645b72e1c2d5f3..77faf997eb10b734de934e880d475b53380189a3 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -121,4 +121,5 @@ set (LYRA_GS src/boards/Groundstation/Automated/SMA/SMA.cpp src/boards/Groundstation/Automated/Actuators/Actuators.cpp src/boards/Groundstation/Automated/Sensors/Sensors.cpp + src/boards/Groundstation/Automated/PinHandler/PinHandler.cpp ) \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/Config/PinHandlerConfig.h b/src/boards/Groundstation/Automated/Config/PinHandlerConfig.h new file mode 100644 index 0000000000000000000000000000000000000000..fe783f7a644acfbc363f34a31e1431b3e10f01f2 --- /dev/null +++ b/src/boards/Groundstation/Automated/Config/PinHandlerConfig.h @@ -0,0 +1,35 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Author: Nicolò Caruso + * + * 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 <utils/PinObserver/PinObserver.h> + +namespace Antennas +{ +namespace PinHandlerConfig +{ +constexpr unsigned int ARM_SWITCH_THRESHOLD = 20; +constexpr unsigned int ACTIVE_SWITCH_THRESHOLD = 20; +} // namespace PinHandlerConfig + +} // namespace Antennas \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/PinHandler/PinData.h b/src/boards/Groundstation/Automated/PinHandler/PinData.h new file mode 100644 index 0000000000000000000000000000000000000000..10be191d6a38e3c5761afd1153e32388d485ff77 --- /dev/null +++ b/src/boards/Groundstation/Automated/PinHandler/PinData.h @@ -0,0 +1,54 @@ +/* Copyright (c) 2019-2024 Skyward Experimental Rocketry + * Author: Matteo Pignataro, Nicolò Caruso + * + * 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 <stdint.h> + +#include <iostream> + +namespace Antennas +{ + +struct PinChangeData +{ + uint64_t timestamp = 0; + uint8_t pinID = 0; + uint32_t changesCount = 0; + + PinChangeData(uint64_t timestamp, uint8_t pinID, uint32_t changesCount) + : timestamp(timestamp), pinID(pinID), changesCount(changesCount) + { + } + + PinChangeData() : PinChangeData{0, 0, 0} {} + + static std::string header() { return "timestamp,pinID,changesCount\n"; } + + void print(std::ostream& os) const + { + os << timestamp << "," << (int)pinID << "," << (int)changesCount + << "\n"; + } +}; + +} // namespace Antennas \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/PinHandler/PinHandler.cpp b/src/boards/Groundstation/Automated/PinHandler/PinHandler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba6c53feedeb44f27bc9b1a91d0ff506128cde05 --- /dev/null +++ b/src/boards/Groundstation/Automated/PinHandler/PinHandler.cpp @@ -0,0 +1,120 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Authors: Nicolò Caruso + * + * 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/Config/PinHandlerConfig.h> +#include <Groundstation/Automated/PinHandler/PinHandler.h> +#include <common/Events.h> +#include <common/Topics.h> +#include <drivers/timer/TimestampTimer.h> +#include <events/EventBroker.h> +#include <interfaces-impl/hwmapping.h> + +#include <functional> + +using namespace Boardcore; +using namespace std; +using namespace std::placeholders; +using namespace Common; + +namespace Antennas +{ +PinHandler::PinHandler() : scheduler(), pin_observer(scheduler) +{ + pin_observer.registerPinCallback( + miosix::commBox::switchArm::getPin(), + bind(&PinHandler::onArmTransition, this, _1), + PinHandlerConfig::ARM_SWITCH_THRESHOLD); + + pin_observer.registerPinCallback( + miosix::commBox::switchActive::getPin(), + bind(&PinHandler::onActiveTransition, this, _1), + PinHandlerConfig::ACTIVE_SWITCH_THRESHOLD); +} + +bool PinHandler::start() { return scheduler.start(); } + +bool PinHandler::isStarted() { return scheduler.isRunning(); } + +void PinHandler::onArmTransition(PinTransition transition) +{ + if (transition == Boardcore::PinTransition::RISING_EDGE) + { + EventBroker::getInstance().post(Common::Events::TMTC_ARP_ARM, + Common::Topics::TOPIC_ARP); + + // Log the event + PinData data = getPinData(PinList::ARM_SWITCH); + PinChangeData log{TimestampTimer::getTimestamp(), PinList::ARM_SWITCH, + data.changesCount}; + Logger::getInstance().log(log); + } + else + { + EventBroker::getInstance().post(Common::Events::TMTC_ARP_DISARM, + Common::Topics::TOPIC_ARP); + + // Log the event + PinData data = getPinData(PinList::ARM_SWITCH); + PinChangeData log{TimestampTimer::getTimestamp(), PinList::ARM_SWITCH, + data.changesCount}; + Logger::getInstance().log(log); + } +} + +void PinHandler::onActiveTransition(PinTransition transition) +{ + if (transition == Boardcore::PinTransition::RISING_EDGE) + { + EventBroker::getInstance().post(Common::Events::TMTC_ARP_FOLLOW, + Common::Topics::TOPIC_ARP); + + // Log the event + PinData data = getPinData(PinList::ACTIVE_SWITCH); + PinChangeData log{TimestampTimer::getTimestamp(), + PinList::ACTIVE_SWITCH, data.changesCount}; + Logger::getInstance().log(log); + } + // TODO: Is needed an exiting transition with the command box? +} + +PinData PinHandler::getPinData(PinList pin) +{ + switch (pin) + { + case PinList::ARM_SWITCH: + { + return pin_observer.getPinData( + miosix::commBox::switchArm::getPin()); + } + case PinList::ACTIVE_SWITCH: + { + return pin_observer.getPinData( + miosix::commBox::switchActive::getPin()); + } + default: + { + LOG_ERR(logger, "Requested non valid pin"); + return PinData{}; + } + } +} +} // namespace Antennas \ No newline at end of file diff --git a/src/boards/Groundstation/Automated/PinHandler/PinHandler.h b/src/boards/Groundstation/Automated/PinHandler/PinHandler.h new file mode 100644 index 0000000000000000000000000000000000000000..9497a5ca752be463feefd4b3ad14711e3735081c --- /dev/null +++ b/src/boards/Groundstation/Automated/PinHandler/PinHandler.h @@ -0,0 +1,76 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Authors: Nicolò Caruso + * + * 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/Automated/PinHandler/PinData.h> +#include <diagnostic/PrintLogger.h> +#include <scheduler/TaskScheduler.h> +#include <utils/PinObserver/PinObserver.h> + +#include <utils/ModuleManager/ModuleManager.hpp> + +namespace Antennas +{ +class PinHandler : public Boardcore::Module +{ +public: + enum PinList : uint8_t + { + ARM_SWITCH, + ACTIVE_SWITCH + }; + + PinHandler(); + + /** + * @brief Starts the PinObserver module thread + */ + bool start(); + + /** + * @brief Checks if the module has started correctly + */ + bool isStarted(); + + /** + * @brief Called when the arm switch has been flipped + */ + void onArmTransition(Boardcore::PinTransition transition); + + /** + * @brief Called when the active switch has been flipped + */ + void onActiveTransition(Boardcore::PinTransition transition); + + /** + * @brief Returns the status of the requested pin + */ + Boardcore::PinData getPinData(PinList pin); + +private: + Boardcore::PrintLogger logger = Boardcore::Logging::getLogger("PinHandler"); + + Boardcore::TaskScheduler scheduler; + Boardcore::PinObserver pin_observer; +}; +} // namespace Antennas \ 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 b827c8ce8bbee9c8580d01578450821b8f294033..d659500f58817a0ed131a964b3355c66251777f7 100644 --- a/src/entrypoints/Groundstation/lyra-gs-entry.cpp +++ b/src/entrypoints/Groundstation/lyra-gs-entry.cpp @@ -23,6 +23,7 @@ #include <Groundstation/Automated/Actuators/Actuators.h> #include <Groundstation/Automated/Hub.h> #include <Groundstation/Automated/Leds/Leds.h> +#include <Groundstation/Automated/PinHandler/PinHandler.h> #include <Groundstation/Automated/SMA/SMA.h> #include <Groundstation/Automated/Sensors/Sensors.h> #include <Groundstation/Common/Ports/Serial.h> @@ -104,10 +105,11 @@ int main() DipSwitch dip(sh, clk, qh, microSecClk); DipStatus dipRead = dip.read(); - // TODO: Pass to DependencyManager when rebased + // TODO: Pass to DependencyManager when rebased! ModuleManager &modules = ModuleManager::getInstance(); PrintLogger logger = Logging::getLogger("lyra_gs"); + // TODO: Board scheduler for the schedulers TaskScheduler *scheduler_low = new TaskScheduler(0); TaskScheduler *scheduler_high = new TaskScheduler(); Buses *buses = new Buses(); @@ -132,17 +134,19 @@ int main() if (dipRead.isARP) { LOG_DEBUG(logger, "[debug] Starting as ARP Ground Station\n"); - Antennas::Leds *leds = new Antennas::Leds(scheduler_low); - HubBase *hub = new Antennas::Hub(); //< TODO: Could it be this??? NAH! - Antennas::Actuators *actuators = new Antennas::Actuators(); - Antennas::Sensors *sensors = new Antennas::Sensors(); - Antennas::SMA *sma = new Antennas::SMA(scheduler_high); + Antennas::Leds *leds = new Antennas::Leds(scheduler_low); + HubBase *hub = new Antennas::Hub(); + Antennas::Actuators *actuators = new Antennas::Actuators(); + Antennas::Sensors *sensors = new Antennas::Sensors(); + Antennas::SMA *sma = new Antennas::SMA(scheduler_high); + Antennas::PinHandler *pinHandler = new Antennas::PinHandler(); ok &= modules.insert(sma); ok &= modules.insert<HubBase>(hub); ok &= modules.insert(actuators); ok &= modules.insert(sensors); ok &= modules.insert(leds); + ok &= modules.insert(pinHandler); } // Ground station module insertion else @@ -226,6 +230,14 @@ int main() { LOG_ERR(logger, "[error] Failed to start sma!\n"); } + + ok &= ModuleManager::getInstance().get<Antennas::PinHandler>()->start(); + + if (!modules.get<Antennas::PinHandler>()->start()) + if (!ok) + { + LOG_ERR(logger, "[error] Failed to start PinHandler!\n"); + } } if (!ok)