Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated TemperatureControl class #18

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions .settings/language.settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<provider-reference id="org.eclipse.cdt.ui.UserLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-1044504862420830438" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-560329967557635101" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand All @@ -16,7 +16,7 @@
<provider-reference id="org.eclipse.cdt.ui.UserLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-1044506437813815085" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-560327486725857206" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand Down
78 changes: 78 additions & 0 deletions Components/FlightControl/Inc/TemperatureControl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
******************************************************************************
* File Name : TemperatureControl.hpp
* Description : Functions defined, macros and etc for Temperature Control
* Task
******************************************************************************
*/
#ifndef SOAR_TEMPERATURECONTROL_HPP_
#define SOAR_TEMPERATURECONTROL_HPP_

/* INCLUDES */
#include "Task.hpp"
#include "GPIO.hpp"
#include "SystemDefines.hpp"

/* DEFINES */
#define ERROR_TEMPERATURE_VALUE 9999
#define TEMPERATURE_OFFSET -4.0 //in degrees Celsius
#define THERMOCOUPLE_SPI_TIMEOUT 100 //in ms

/* Macros/Enums ------------------------------------------------------------*/
enum THERMOCOUPLE_TASK_COMMANDS {
THERMOCOUPLE_NULL = 0,
SET_TARGET_TEMP,
SET_TARGET_STATE,
SET_CURRENT_TEMP
};

enum class TARGET_CONTROLS : uint8_t {
AC1 = 0,
AC2,
AC3,
NUMBER_OF_CONTROLS, // NOTE: Always keep this as the last item
};

//diff functions for each in the struct and and to be able to store them
struct Temp_Control {
TARGET_CONTROLS acUnit;
uint8_t targetTemperature;
bool isOn;
uint8_t currTemperature; //will be updated as temp being passed through from GUI
GPIO_TypeDef * targetPinPort;
uint16_t targetPin;

};

class TemperatureControl : public Task
{
public:
static TemperatureControl& Inst() {
static TemperatureControl inst;
return inst;
}

void InitTask();

protected:

static void RunTask(void* pvParams) {
TemperatureControl::Inst().Run(pvParams);
} // Static Task Interface, passes control to the instance Run();

void Run(void* pvParams); // Main run code
void HandleCommand(Command& cm);
void HandleTaskCommand(uint16_t taskCommand);

void SetTargetTemp(TARGET_CONTROLS Target, uint8_t Target_Temp);
void SetCurrentTemp(TARGET_CONTROLS Target, uint8_t tempReceived);
void SetTargetState(TARGET_CONTROLS Target, bool currentState);

private:
TemperatureControl();
TemperatureControl(const TemperatureControl&);
TemperatureControl& operator=(const TemperatureControl&);
bool acStatus;
};

#endif // SOAR_TEMPERATURECONTROL_HPP_
176 changes: 176 additions & 0 deletions Components/FlightControl/TemperatureControl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/**
**********************************************************************************
* File Name : TemperatureControl.cpp
* Description : This file is able to control the AC/Cooling unit by
* reading the temperature from the thermocouples and when
* it reaches a specified temperature that is too high, the
* cooling unit is turned on, and turned off when the desired
* temperature is reached.
**********************************************************************************
*/
#include <TemperatureControl.hpp>
#include "GPIO.hpp"
#include "SystemDefines.hpp"
#include "Task.hpp"

//GPIO initialize


// Initialize the TempControl array with AC units and target temperatures - make static array
static Temp_Control tempControl[2] = {
{TARGET_CONTROLS::AC1, 10, false, 0, LED_1_GPIO_Port, LED_1_Pin}, // AC1, target temperature 10, initially off, currentTemp
{TARGET_CONTROLS::AC2, 20, false, 0, LED_1_GPIO_Port, LED_1_Pin} // AC2, target temperature 20, initially off, currentTemp
};
/**
* @brief Constructor for TemperatureControl
*/

TemperatureControl::TemperatureControl() : Task(){

}

/**
* @brief Initialize the TemperatureControl
*/

void TemperatureControl::InitTask()
{
//Make sure the task is not already initialized
SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize flight task twice");

BaseType_t rtValue =
xTaskCreate((TaskFunction_t)TemperatureControl::RunTask,
(const char*)"TemperatureControl",
(uint16_t)TEMPERATURE_TASK_STACK_DEPTH_WORDS,
(void*)this,
(UBaseType_t)TEMPERATURE_TASK_RTOS_PRIORITY,
(TaskHandle_t*)&rtTaskHandle);

SOAR_ASSERT(rtValue == pdPASS, "TemperatureTask::InitTask() - xTaskCreate() failed");

}

/*
* @brief Runs loop, waits for a command
*
*/
void TemperatureControl::Run(void* pvParams){

while (1) {
Command cm;

//Wait forever for a command
if (qEvtQueue->Receive(cm, 1000)) {
HandleCommand(cm);
}
else {
// Update target state depending on temp
for (Temp_Control target : tempControl){

if (target.currTemperature > target.targetTemperature){
HAL_GPIO_WritePin(target.targetPinPort, target.targetPin, GPIO_PIN_SET);
}
else{
HAL_GPIO_WritePin(target.targetPinPort, target.targetPin, GPIO_PIN_RESET);
}
}
}
}
}

/*
* @brief Handles command
*
*/
void TemperatureControl::HandleCommand(Command& cm)
{
//Switch for the GLOBAL_COMMAND
switch (cm.GetCommand()) {

case TASK_SPECIFIC_COMMAND: {
HandleTaskCommand(cm.GetTaskCommand());
break; //No task specific commands need
}
default:
SOAR_PRINT("ThermocoupleTask - Received Unsupported Command {%d}\n", cm.GetCommand());//change
break;
}

cm.Reset();
}

/*
* @brief Handles a Request Command
*
*/
void TemperatureControl::HandleTaskCommand(uint16_t taskCommand)
{
//
switch (taskCommand) {
case SET_TARGET_TEMP: {
for (Temp_Control& target : tempControl) {
// The for loop iterates over all instances of Temp_Control.
// It ensures the SetTargetTemp function is called for each air conditioning unit (e.g., AC1, AC2, etc.),
// dynamically updating their respective target temperatures as needed.
SetTargetTemp(target.acUnit, target.targetTemperature);
}
}

case SET_TARGET_STATE: {
for(Temp_Control& target : tempControl){
SetTargetState(target.acUnit, target.currTemperature);
}
}

case SET_CURRENT_TEMP: {
for(Temp_Control& target : tempControl){
SetCurrentTemp(target.acUnit, target.isOn);
}
}
default:
SOAR_PRINT("UARTTask - Received Unsupported REQUEST_COMMAND {%d}\n", taskCommand);
break;
}
}

void TemperatureControl::SetTargetTemp(TARGET_CONTROLS Target, uint8_t Target_Temp){ // Data_command, setcurrenttemp into the Command IMUData -> sent as a struct then you would destruct it
// Iterate through each element in the target array.
// If the current target matches the specified target, update its value accordingly

for (Temp_Control targetSettings : tempControl) {
if (targetSettings.acUnit == Target) {
targetSettings.targetTemperature = Target_Temp;
}
}
}

void TemperatureControl::SetCurrentTemp(TARGET_CONTROLS Target, uint8_t tempReceived){
// Iterate through each element in the target array.
// If the received temperature matches the current temperature, update the corresponding value.


for (Temp_Control targetSettings : tempControl) {
if (targetSettings.acUnit == Target) {
targetSettings.currTemperature = tempReceived;
}
}

//Command;
}

void TemperatureControl::SetTargetState(TARGET_CONTROLS Target, bool currentState){
// Iterate through each element in the target array.
// If the current target matches the specified target, set its state to "on." Otherwise, set its state to "off."


for (Temp_Control targetSettings : tempControl) {
if (targetSettings.acUnit == Target) {
targetSettings.isOn = true;
}

else{
targetSettings.isOn = false;
}
}

}
2 changes: 1 addition & 1 deletion Components/SoarProtocol/SoarProto
Submodule SoarProto updated 0 files
5 changes: 5 additions & 0 deletions Components/SystemDefines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ constexpr uint8_t TASK_THERMOCOUPLE_PRIORITY = 2; // Priority of the thermocou
constexpr uint8_t TASK_THERMOCOUPLE_QUEUE_DEPTH_OBJS = 10; // Size of the thermocouple task queue
constexpr uint16_t TASK_THERMOCOUPLE_STACK_DEPTH_WORDS = 384; // Size of the thermocouple task stack

// TEMPERATURE CONTROL TASK
constexpr uint8_t TEMPERATURE_TASK_RTOS_PRIORITY = 2; // Priority of the temperature control task
constexpr uint8_t TEMPERATURE_TASK_QUEUE_DEPTH_OBJS = 10; // Size of the temperature control task queue
constexpr uint16_t TEMPERATURE_TASK_STACK_DEPTH_WORDS = 384; // Size of the temperature control task stack

constexpr uint32_t TELEMETRY_DEFAULT_LOGGING_RATE_MS = 400; // Default logging delay for telemetry task


Expand Down