From 7e2f7b71023e1e0069da7ce8cc7734c49766e413 Mon Sep 17 00:00:00 2001 From: Andre Date: Wed, 4 Aug 2021 13:54:45 -0300 Subject: [PATCH] firmware: app: tasks: heater_controller: Adding task structure and implementation #142 --- firmware/app/tasks/heater_controller.c | 118 +++++++++++++++++++++++++ firmware/app/tasks/heater_controller.h | 68 ++++++++++++++ firmware/devices/heater/heater.h | 3 +- firmware/version.h | 4 +- 4 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 firmware/app/tasks/heater_controller.c create mode 100644 firmware/app/tasks/heater_controller.h diff --git a/firmware/app/tasks/heater_controller.c b/firmware/app/tasks/heater_controller.c new file mode 100644 index 0000000..301a8b4 --- /dev/null +++ b/firmware/app/tasks/heater_controller.c @@ -0,0 +1,118 @@ +/* + * heater_controller.c + * + * Copyright (C) 2021, SpaceLab. + * + * This file is part of EPS 2.0. + * + * EPS 2.0 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EPS 2.0 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EPS 2.0. If not, see . + * + */ + +/** + * \brief Heater controller task implementation. + * + * \author André M. P. de Mattos + * + * \version 0.2.12 + * + * \date 2021/08/04 + * + * \addtogroup heater_controller + * \{ + */ + +#include +#include + +#include + +#include "heater_controller.h" +#include "startup.h" + +xTaskHandle xTaskHeaterControllerHandle; + +void vTaskHeaterController(void *pvParameters) +{ + uint32_t heater_mode = 0; + uint32_t heater_duty_cyle = 0; + temperature_t temp = 0; + float actuator_output = 0; + + /* Wait startup task to finish */ + xEventGroupWaitBits(task_startup_status, TASK_STARTUP_DONE, pdFALSE, pdTRUE, pdMS_TO_TICKS(TASK_HEATER_CONTROLLER_INIT_TIMEOUT_MS)); + + while(1) + { + TickType_t last_cycle = xTaskGetTickCount(); + + eps_buffer_read(EPS2_PARAM_ID_BAT_HEATER_1_MODE, &heater_mode); + switch(heater_mode) + { + case HEATER_AUTOMATIC_MODE: + if(heater_get_sensor(HEATER_CONTROL_LOOP_CH_0, &temp) != 0) + { + sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_HEATER_CONTROLLER_NAME, "Heater channel 0 failed!"); + sys_log_new_line(); + } + + actuator_output = heater_algorithm(PID_BASE_SET_POINT, temp); + + if(heater_set_actuator(HEATER_CONTROL_LOOP_CH_0, actuator_output) != 0) + { + sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_HEATER_CONTROLLER_NAME, "Heater channel 0 failed!"); + sys_log_new_line(); + } + break; + case HEATER_MANUAL_MODE: + /* TODO: Implemente manual mode */ + break; + default: + sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_HEATER_CONTROLLER_NAME, "Invalid mode!"); + sys_log_new_line(); + break; + } + + eps_buffer_read(EPS2_PARAM_ID_BAT_HEATER_2_MODE, &heater_mode); + switch(heater_mode) + { + case HEATER_AUTOMATIC_MODE: + if(heater_get_sensor(HEATER_CONTROL_LOOP_CH_1, &temp) != 0) + { + sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_HEATER_CONTROLLER_NAME, "Heater channel 1 failed!"); + sys_log_new_line(); + } + + actuator_output = heater_algorithm(PID_BASE_SET_POINT, temp); + + if(heater_set_actuator(HEATER_CONTROL_LOOP_CH_1, actuator_output) != 0) + { + sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_HEATER_CONTROLLER_NAME, "Heater channel 1 failed!"); + sys_log_new_line(); + } + break; + case HEATER_MANUAL_MODE: + /* TODO: Implemente manual mode */ + break; + default: + sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_HEATER_CONTROLLER_NAME, "Invalid mode!"); + sys_log_new_line(); + break; + } + + vTaskDelayUntil(&last_cycle, pdMS_TO_TICKS(TASK_HEATER_CONTROLLER_PERIOD_MS)); + } +} + +/** \} End of heater_controller group */ diff --git a/firmware/app/tasks/heater_controller.h b/firmware/app/tasks/heater_controller.h new file mode 100644 index 0000000..a68d6ed --- /dev/null +++ b/firmware/app/tasks/heater_controller.h @@ -0,0 +1,68 @@ +/* + * heater_controller.h + * + * Copyright (C) 2021, SpaceLab. + * + * This file is part of EPS 2.0. + * + * EPS 2.0 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EPS 2.0 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EPS 2.0. If not, see . + * + */ + +/** + * \brief Heater controller task definition. + * + * \author André M. P. de Mattos + * + * \version 0.2.12 + * + * \date 2021/08/04 + * + * \defgroup heater_controller Heater Controller + * \ingroup tasks + * \{ + */ + +#ifndef HEATER_CONTROLLER_H_ +#define HEATER_CONTROLLER_H_ + +#include +#include + +#define TASK_HEATER_CONTROLLER_NAME "Heater Controller" /**< Task name. */ +#define TASK_HEATER_CONTROLLER_STACK_SIZE 160 /**< Memory stack size in bytes. */ +#define TASK_HEATER_CONTROLLER_PRIORITY 3 /**< Priority. */ +#define TASK_HEATER_CONTROLLER_PERIOD_MS 500 /**< Period in milliseconds. */ +#define TASK_HEATER_CONTROLLER_INIT_TIMEOUT_MS 2000 /**< Wait time to initialize the task in milliseconds. */ + +#define HEATER_AUTOMATIC_MODE 0x00 +#define HEATER_MANUAL_MODE 0x01 + +/** + * \brief Heater controller task handle. + */ +extern xTaskHandle xTaskHeaterControllerHandle; + +/** + * \brief Heater controller task. + * + * \param[in] pvParameters is a value that will passed as the task's parameter. + * + * \return None. + */ +void vTaskHeaterController(void *pvParameters); + +#endif /* HEATER_CONTROLLER_H_ */ + +/** \} End of heater_controller group */ diff --git a/firmware/devices/heater/heater.h b/firmware/devices/heater/heater.h index 46b7c16..9a3eab2 100644 --- a/firmware/devices/heater/heater.h +++ b/firmware/devices/heater/heater.h @@ -49,6 +49,7 @@ /** * \brief PID algorithm constants. */ +#define PID_BASE_SET_POINT 5 /**< TODO. */ #define PID_PROPORTIONAL_CONSTANT 1 /**< Kp: Modulate the proportional actuation influence. */ #define PID_INTEGRATOR_CONSTANT 1 /**< Ki: Modulate the integrator actuation influence. */ #define PID_DIFFERENTIATOR_CONSTANT 1 /**< Kd: Modulate the differentiator actuation influence. */ @@ -62,7 +63,7 @@ #define PID_PREVIOUS_ERROR_INIT 0 /**< TODO. */ #define PID_DIFFERENTIATOR_INIT 0 /**< TODO. */ #define PID_PREVIOUS_MEASUREMENT_INIT 0 /**< TODO. */ -#define PID_OUTPUT_INIT 50 /**< TODO. */ +#define PID_OUTPUT_INIT 50 /**< TODO. */ /** * \brief PWM constants. diff --git a/firmware/version.h b/firmware/version.h index b1f498f..0798f6f 100644 --- a/firmware/version.h +++ b/firmware/version.h @@ -25,7 +25,7 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.2.11 + * \version 0.2.12 * * \date 2020/10/21 * @@ -36,7 +36,7 @@ #ifndef VERSION_H_ #define VERSION_H_ -#define FIRMWARE_VERSION "0.2.11" +#define FIRMWARE_VERSION "0.2.12" #define FIRMWARE_STATUS "Development"