diff --git a/.cproject b/.cproject index f6da38a..721d02a 100644 --- a/.cproject +++ b/.cproject @@ -55,6 +55,15 @@ + + + + + + + + + @@ -98,6 +107,15 @@ + + + + + + + + + @@ -141,6 +159,15 @@ + + + + + + + + + @@ -296,6 +307,15 @@ + + + + + + + + + @@ -338,6 +358,15 @@ + + + + + + + + + diff --git a/.gitignore b/.gitignore index 77aa44f..f607ab2 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,7 @@ STM32L152-Discovery.elf.launch # VS2022 .vs/ +.vscode/ # OS Specific **/.DS_Store diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ea88dd5..c4767f4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,32 +1,34 @@ -repos: - - - repo: https://github.com/pre-commit/mirrors-clang-format - rev: 7d85583be209cb547946c82fbe51f4bc5dd1d017 - hooks: - - id: clang-format - args: ['--style={BasedOnStyle: Google, SortIncludes: false}'] - files: \.(cpp|hpp)$ - stages: [commit] - - - repo: https://github.com/pre-commit/mirrors-prettier - rev: v2.5.1 - hooks: - - id: prettier - files: \.(js|ts|jsx|tsx|css|less|html|json|markdown|md|yaml|yml)$ - - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.4.1 - hooks: - - id: ruff - types_or: [ python, pyi ] - args: [ --fix ] - stages: [commit] - - id: ruff-format - stages: [commit] - - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml +repos: + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: 7d85583be209cb547946c82fbe51f4bc5dd1d017 + hooks: + - id: clang-format + args: + [ + "--style={BasedOnStyle: Google, SortIncludes: false, ColumnLimit: 120}", + ] + files: \.(cpp|hpp)$ + stages: [commit] + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v2.5.1 + hooks: + - id: prettier + files: \.(js|ts|jsx|tsx|css|less|html|json|markdown|md|yaml|yml)$ + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.4.1 + hooks: + - id: ruff + types_or: [python, pyi] + args: [--fix] + stages: [commit] + - id: ruff-format + stages: [commit] + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml diff --git a/Components/PubSubTest/Inc/PubSubReceive.hpp b/Components/PubSubTest/Inc/PubSubReceive.hpp new file mode 100644 index 0000000..17a5165 --- /dev/null +++ b/Components/PubSubTest/Inc/PubSubReceive.hpp @@ -0,0 +1,58 @@ +/** + ******************************************************************************** + * @file PubSubReceive.hpp + * @author Shivam Desai + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef PUBSUB_RECEIEVE_HPP_ +#define PUBSUB_RECEIEVE_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include "Task.hpp" +#include "SystemDefines.hpp" + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class PubSubReceive : public Task { + public: + static PubSubReceive& Inst() { + static PubSubReceive inst; + return inst; + } + + void InitTask(); + + protected: + static void RunTask(void* pvParams) { + PubSubReceive::Inst().Run(pvParams); + } // Static Task Interface, passes control to the instance Run(); + void Run(void* pvParams); // Main run code + void HandleCommand(Command& cm); + void HandleDataBrokerCommand(const Command& cm); + + private: + // Private Functions + PubSubReceive(); // Private constructor + PubSubReceive(const PubSubReceive&); // Prevent copy-construction + PubSubReceive& operator=(const PubSubReceive&); // Prevent assignment +}; + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* PUBSUB_RECEIEVE_HPP_ */ diff --git a/Components/PubSubTest/Inc/PubSubSend.hpp b/Components/PubSubTest/Inc/PubSubSend.hpp new file mode 100644 index 0000000..ef09d94 --- /dev/null +++ b/Components/PubSubTest/Inc/PubSubSend.hpp @@ -0,0 +1,57 @@ +/** + ******************************************************************************** + * @file PubSubSend.hpp + * @author Shivam Desai + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef PUBSUB_SEND_HPP_ +#define PUBSUB_SEND_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include "Task.hpp" +#include "SystemDefines.hpp" + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class PubSubSend : public Task { + public: + static PubSubSend& Inst() { + static PubSubSend inst; + return inst; + } + + void InitTask(); + + protected: + static void RunTask(void* pvParams) { + PubSubSend::Inst().Run(pvParams); + } // Static Task Interface, passes control to the instance Run(); + void Run(void* pvParams); // Main run code + void HandleCommand(Command& cm); + + private: + // Private Functions + PubSubSend(); // Private constructor + PubSubSend(const PubSubSend&); // Prevent copy-construction + PubSubSend& operator=(const PubSubSend&); // Prevent assignment +}; + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* PUBSUB_SEND_HPP_ */ diff --git a/Components/PubSubTest/PubSubReceive.cpp b/Components/PubSubTest/PubSubReceive.cpp new file mode 100644 index 0000000..5574d7b --- /dev/null +++ b/Components/PubSubTest/PubSubReceive.cpp @@ -0,0 +1,119 @@ +/** + ******************************************************************************** + * @file PubSubReceive.cpp + * @author Shivam Desai + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +/************************************ + * INCLUDES + ************************************/ +#include "PubSubReceive.hpp" +#include "SystemDefines.hpp" +#include "SensorDataTypes.hpp" +#include "DataBroker.hpp" + +/************************************ + * PRIVATE MACROS AND DEFINES + ************************************/ + +/************************************ + * VARIABLES + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +/************************************ + * FUNCTION DEFINITIONS + ************************************/ + +/** + * @brief Constructor for PubSubReceive + */ +PubSubReceive::PubSubReceive() : Task(PUBSUB_RECEIVE_TASK_QUEUE_DEPTH_OBJS) {} + +/** + * @brief Initialize the PubSubReceive + * Do not modify this function aside from adding the task name + */ +void PubSubReceive::InitTask() { + // Make sure the task is not already initialized + SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice"); + + BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubReceive::RunTask, (const char*)"PubSubReceive", + (uint16_t)PUBSUB_RECEIVE_TASK_STACK_DEPTH_WORDS, (void*)this, + (UBaseType_t)PUBSUB_RECEIVE_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle); + + SOAR_ASSERT(rtValue == pdPASS, "PubSubReceive::InitTask() - xTaskCreate() failed"); +} + +/** + * @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized. + * @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used + */ +void PubSubReceive::Run(void* pvParams) { + // SOAR_PRINT("PUBSUB RECIEVE STARTED\n"); + DataBroker::Subscribe(this); + // DataBroker::Unsubscribe(this); + while (1) { + /* Process commands in blocking mode */ + Command cm; + bool res = qEvtQueue->ReceiveWait(cm); + if (res) { + HandleCommand(cm); + } + } +} + +/** + * @brief Handles a command + * @param cm Command reference to handle + */ +void PubSubReceive::HandleCommand(Command& cm) { + switch (cm.GetCommand()) { + case DATA_BROKER_COMMAND: + HandleDataBrokerCommand(cm); + break; + + default: + SOAR_PRINT("PubSubReceive - Received Unsupported Command {%d}\n", cm.GetCommand()); + break; + } + + // No matter what we happens, we must reset allocated data + cm.Reset(); +} + +/** + * @brief Handle all data broker commands + * @param cm The command object with the data + * Use cm.GetTaskCommand() to get the message type + * Message types must be cast back into DataBrokerMessageTypes enum + * Use cm.GetDataPointer() to get the pointer to the data + */ +void PubSubReceive::HandleDataBrokerCommand(const Command& cm) { + DataBrokerMessageTypes messageType = DataBroker::getMessageType(cm); + switch (messageType) { + case DataBrokerMessageTypes::IMU_DATA: { + IMUData imu_data = DataBroker::ExtractData(cm); + SOAR_PRINT("\n IMU DATA : \n"); + SOAR_PRINT(" X -> %d \n", imu_data.accelX); + SOAR_PRINT(" Y -> %d \n", imu_data.accelY); + SOAR_PRINT(" Z -> %d \n", imu_data.accelZ); + SOAR_PRINT("--DATA_END--\n\n"); + break; + } + + case DataBrokerMessageTypes::THERMOCOUPLE_DATA: + break; + + case DataBrokerMessageTypes::INVALID: + [[fallthrough]]; + default: + break; + } +} diff --git a/Components/PubSubTest/PubSubSend.cpp b/Components/PubSubTest/PubSubSend.cpp new file mode 100644 index 0000000..9fb3091 --- /dev/null +++ b/Components/PubSubTest/PubSubSend.cpp @@ -0,0 +1,94 @@ +/** + ******************************************************************************** + * @file PubSubSend.cpp + * @author Shivam Desai + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +/************************************ + * INCLUDES + ************************************/ +#include "PubSubSend.hpp" +#include "SystemDefines.hpp" +#include "SensorDataTypes.hpp" +#include "DataBroker.hpp" + +/************************************ + * PRIVATE MACROS AND DEFINES + ************************************/ + +/************************************ + * VARIABLES + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +/************************************ + * FUNCTION DEFINITIONS + ************************************/ + +/** + * @brief Constructor for PubSubSend + */ +PubSubSend::PubSubSend() : Task(PUBSUB_SEND_TASK_QUEUE_DEPTH_OBJS) {} + +/** + * @brief Initialize the PubSubSend + * Do not modify this function aside from adding the task name + */ +void PubSubSend::InitTask() { + // Make sure the task is not already initialized + SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice"); + + BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubSend::RunTask, (const char*)"PubSubSend", + (uint16_t)PUBSUB_SEND_TASK_STACK_DEPTH_WORDS, (void*)this, + (UBaseType_t)PUBSUB_SEND_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle); + + SOAR_ASSERT(rtValue == pdPASS, "PubSubSend::InitTask() - xTaskCreate() failed"); +} + +/** + * @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized. + * @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used + */ +void PubSubSend::Run(void* pvParams) { + // SOAR_PRINT("\nPUBSUB SEND STARTED\n"); + + while (1) { + Command cm; + if (qEvtQueue->Receive(cm, 5000)) { + HandleCommand(cm); + } else { + IMUData imuData = { + .accelX = 1, + .accelY = 2, + .accelZ = 3, + }; + DataBroker::Publish(&imuData); + + ThermocoupleData thermData = { + .temperature = -52, + }; + DataBroker::Publish(&thermData); + } + } +} + +/** + * @brief Handles a command + * @param cm Command reference to handle + */ +void PubSubSend::HandleCommand(Command& cm) { + switch (cm.GetCommand()) { + default: + SOAR_PRINT("PubSubSend - Received Unsupported Command {%d}\n", cm.GetCommand()); + break; + } + + // No matter what we happens, we must reset allocated data + cm.Reset(); +} diff --git a/Components/SystemDefines.hpp b/Components/SystemDefines.hpp index 11200f2..7a2ba55 100644 --- a/Components/SystemDefines.hpp +++ b/Components/SystemDefines.hpp @@ -28,13 +28,13 @@ /* Cube++ Required Configuration * ------------------------------------------------------------------*/ #include "CubeDefines.hpp" -constexpr UARTDriver* const DEFAULT_DEBUG_UART_DRIVER = - UART::Debug; // UART Handle that ASSERT messages are sent over +constexpr UARTDriver* const DEFAULT_DEBUG_UART_DRIVER = UART::Debug; // UART Handle that ASSERT messages are sent over enum GLOBAL_COMMANDS : uint8_t { COMMAND_NONE = 0, // No command, packet can probably be ignored TASK_SPECIFIC_COMMAND, // Runs a task specific command when given this object - DATA_COMMAND, // Data command, used to send data to a task. Target is stored - // in taskCommand + DATA_COMMAND, // Data command, used to send data to a task. Target is stored + // in taskCommand + DATA_BROKER_COMMAND, }; /* Cube++ Optional Code Configuration @@ -46,17 +46,23 @@ enum GLOBAL_COMMANDS : uint8_t { * ---------------------------------*/ // UART TASK -constexpr uint8_t UART_TASK_RTOS_PRIORITY = 2; // Priority of the uart task -constexpr uint8_t UART_TASK_QUEUE_DEPTH_OBJS = - 10; // Size of the uart task queue -constexpr uint16_t UART_TASK_STACK_DEPTH_WORDS = - 512; // Size of the uart task stack +constexpr uint8_t UART_TASK_RTOS_PRIORITY = 2; // Priority of the uart task +constexpr uint8_t UART_TASK_QUEUE_DEPTH_OBJS = 10; // Size of the uart task queue +constexpr uint16_t UART_TASK_STACK_DEPTH_WORDS = 512; // Size of the uart task stack // DEBUG TASK -constexpr uint8_t TASK_DEBUG_PRIORITY = 2; // Priority of the debug task -constexpr uint8_t TASK_DEBUG_QUEUE_DEPTH_OBJS = - 10; // Size of the debug task queue -constexpr uint16_t TASK_DEBUG_STACK_DEPTH_WORDS = - 512; // Size of the debug task stack +constexpr uint8_t TASK_DEBUG_PRIORITY = 2; // Priority of the debug task +constexpr uint8_t TASK_DEBUG_QUEUE_DEPTH_OBJS = 10; // Size of the debug task queue +constexpr uint16_t TASK_DEBUG_STACK_DEPTH_WORDS = 512; // Size of the debug task stack + +// PUBSUB SEND Task +constexpr uint8_t PUBSUB_SEND_TASK_RTOS_PRIORITY = 1; // Priority of the pubsub send task +constexpr uint8_t PUBSUB_SEND_TASK_QUEUE_DEPTH_OBJS = 10; // Size of the pubsub send task queue +constexpr uint16_t PUBSUB_SEND_TASK_STACK_DEPTH_WORDS = 512; // Size of the pubsub send task stack + +// PUBSUB RECEIVE Task +constexpr uint8_t PUBSUB_RECEIVE_TASK_RTOS_PRIORITY = 1; // Priority of the pubsub receive task +constexpr uint8_t PUBSUB_RECEIVE_TASK_QUEUE_DEPTH_OBJS = 10; // Size of the pubsub receive task queue +constexpr uint16_t PUBSUB_RECEIVE_TASK_STACK_DEPTH_WORDS = 512; // Size of the pubsub receive task stack #endif // CUBE_MAIN_SYSTEM_DEFINES_H diff --git a/Components/SystemTypes/SensorDataTypes.hpp b/Components/SystemTypes/SensorDataTypes.hpp deleted file mode 100644 index fade876..0000000 --- a/Components/SystemTypes/SensorDataTypes.hpp +++ /dev/null @@ -1,22 +0,0 @@ -/** - ******************************************************************************** - * @file SensorDataTypes.hpp - * @author Shivam Desai - * @date Nov 3, 2024 - * @brief General sensor data structure to pass around in the system or - *log it to flash memory - ******************************************************************************** - */ - -#ifndef SENSORDATATYPES_HPP_ -#define SENSORDATATYPES_HPP_ - -/************************************ - * MACROS AND DEFINES - ************************************/ - -/************************************ - * TYPEDEFS - ************************************/ - -#endif /* SENSORDATATYPES_HPP_ */ diff --git a/Components/SystemTypes/SystemCommunicationTypes.hpp b/Components/SystemTypes/SystemCommunicationTypes.hpp deleted file mode 100644 index e3d046b..0000000 --- a/Components/SystemTypes/SystemCommunicationTypes.hpp +++ /dev/null @@ -1,22 +0,0 @@ -/** - ******************************************************************************** - * @file SystemCommunicationTypes.hpp - * @author Shivam Desai - * @date Nov 3, 2024 - * @brief Data structures to pass around other system information such as - * commands, events or state information - ******************************************************************************** - */ - -#ifndef SYSTEMCOMMUNICATIONTYPES_HPP_ -#define SYSTEMCOMMUNICATIONTYPES_HPP_ - -/************************************ - * MACROS AND DEFINES - ************************************/ - -/************************************ - * TYPEDEFS - ************************************/ - -#endif /* SYSTEMCOMMUNICATIONTYPES_HPP_ */ diff --git a/Components/SystemTypes/SystemConfigTypes.hpp b/Components/SystemTypes/SystemConfigTypes.hpp deleted file mode 100644 index 578f097..0000000 --- a/Components/SystemTypes/SystemConfigTypes.hpp +++ /dev/null @@ -1,18 +0,0 @@ -/** - ******************************************************************************** - * @file SystemConfigTypes.hpp - * @author Shivam Desai - * @date Nov 3, 2024 - * @brief Defines that allow the system to build with different - *functionality by changing the value of the define in this file - ******************************************************************************** - */ - -#ifndef SYSTEMCONFIGTYPES_HPP_ -#define SYSTEMCONFIGTYPES_HPP_ - -/************************************ - * MACROS AND DEFINES - ************************************/ - -#endif /* SYSTEMCONFIGTYPES_HPP_ */ diff --git a/Components/main_avionics.cpp b/Components/main_avionics.cpp index fa0164b..9d1a497 100644 --- a/Components/main_avionics.cpp +++ b/Components/main_avionics.cpp @@ -10,6 +10,8 @@ #include "SystemDefines.hpp" #include "UARTDriver.hpp" #include "CubeTask.hpp" +#include "PubSubReceive.hpp" +#include "PubSubSend.hpp" /* Drivers ------------------------------------------------------------------*/ namespace Driver { @@ -26,16 +28,15 @@ void run_main() { // Init Tasks CubeTask::Inst().InitTask(); DebugTask::Inst().InitTask(); + PubSubReceive::Inst().InitTask(); + PubSubSend::Inst().InitTask(); // Print System Boot Info : Warning, don't queue more than 10 prints before // scheduler starts SOAR_PRINT("\n-- CUBE SYSTEM --\n"); - SOAR_PRINT( - "System Reset Reason: [TODO]\n"); // TODO: System reset reason can be - // implemented via. Flash storage + SOAR_PRINT("System Reset Reason: [TODO]\n"); // TODO: System reset reason can be implemented via. Flash storage SOAR_PRINT("Current System Free Heap: %d Bytes\n", xPortGetFreeHeapSize()); - SOAR_PRINT("Lowest Ever Free Heap: %d Bytes\n\n", - xPortGetMinimumEverFreeHeapSize()); + SOAR_PRINT("Lowest Ever Free Heap: %d Bytes\n\n", xPortGetMinimumEverFreeHeapSize()); // Start the Scheduler // Guidelines: diff --git a/SoarOS b/SoarOS index 038a687..2796fa8 160000 --- a/SoarOS +++ b/SoarOS @@ -1 +1 @@ -Subproject commit 038a687ec48f894996084122b590058398fffc7d +Subproject commit 2796fa848c102683b4b35c48c7ba8e3ea3872d6d