From 39607106d24b901e5d42ff0032ea4f101dfbf3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20Piqu=C3=A9?= Date: Wed, 19 Oct 2022 14:12:32 +0200 Subject: [PATCH] PECO update --- Core/Application/PECO_PeltierController.c | 111 +++++++++++----------- Core/Application/PECO_PeltierController.h | 1 - Makefile | 2 +- 3 files changed, 54 insertions(+), 60 deletions(-) diff --git a/Core/Application/PECO_PeltierController.c b/Core/Application/PECO_PeltierController.c index dbad36f..4c3b663 100644 --- a/Core/Application/PECO_PeltierController.c +++ b/Core/Application/PECO_PeltierController.c @@ -52,7 +52,7 @@ // Description: Definition of local constants (visible by this module only). //================================================================================================= -#define REFRESH 1000 // Refresh rate in ms +#define REFRESH 100 // Refresh rate in ms #define EVENT_TIMER_UPDATE ((U32)(1<<0)) @@ -105,7 +105,7 @@ LOCAL CONST osThreadAttr_t stTaskAttribute = 0, // size of provided memory for control block NULL, // memory for stack 1024, // size of stack - osPriorityNormal, // initial thread priority (default: osPriorityNormal) + osPriorityHigh, // initial thread priority (default: osPriorityNormal) 0, // TrustZone module identifier 0, // reserved (must be 0) }; @@ -176,21 +176,6 @@ BOOL PECO_boInitializeModule( VOID ) return( boOK ); } - -//------------------------------------------------------------------------------------------------- -// Function: PECO_boSetTemperature -// Description: Sets the Peltier Controller to a Specific Temperature -// Parameters: None -// Returns: Boolean TRUE if successful -//------------------------------------------------------------------------------------------------- -BOOL PECO_boSetTemperature( S16 Temperature ){ - BOOL boOK = TRUE; - - boOK &= boSetPeltierVoltage( Temperature ); - - return( boOK ); -} - //------------------------------------------------------------------------------------------------- // Function: PECO_Enable // Description: Enables the Peltier Controller Output @@ -231,7 +216,7 @@ VOID vTask( PVOID arg ) FLOAT last_error = 0; FLOAT integral = 0; - FLOAT dT = REFRESH; // 1s + FLOAT dT = REFRESH / 1000.0f; // 1s while ( TRUE ) { @@ -243,53 +228,63 @@ VOID vTask( PVOID arg ) HAL_IWDG_Refresh(&hiwdg); - if ( VARH_u32GetVariableData( VARH_eMode ) == PECO_eConstTemp ) + if ( PECO_isEnabled() ) { - // TODO: check the measurements, everything ok? - - - // PID Regelung - FLOAT kp = VARH_flGetVariableData( VARH_ePID_kp ); - FLOAT ki = VARH_flGetVariableData( VARH_ePID_ki ); - FLOAT kd = VARH_flGetVariableData( VARH_ePID_kd ); - FLOAT max = VARH_flGetVariableData( VARH_ePID_Max ); - FLOAT min = VARH_flGetVariableData( VARH_ePID_Min ); - - FLOAT Tnow = VARH_flGetVariableData( VARH_eTemp_C ); - FLOAT Tset = VARH_flGetVariableData( VARH_ePID_Temp ); + if ( VARH_u32GetVariableData( VARH_eMode ) == PECO_eConstTemp ) + { + // TODO: check the measurements, everything ok? + + + // PID Regelung - // -error - FLOAT error = Tnow - Tset; + FLOAT kp = VARH_flGetVariableData( VARH_ePID_kp ); + FLOAT ki = VARH_flGetVariableData( VARH_ePID_ki ); + FLOAT kd = VARH_flGetVariableData( VARH_ePID_kd ); + FLOAT max = VARH_flGetVariableData( VARH_ePID_Max ); + FLOAT min = VARH_flGetVariableData( VARH_ePID_Min ); + + FLOAT Tnow = VARH_flGetVariableData( VARH_eTemp_C ); + FLOAT Tset = VARH_flGetVariableData( VARH_ePID_Temp ); - // proportional term - FLOAT P = kp * error; - - // integral term - integral += error * dT; - FLOAT I = ki * integral; - - // derivative term - FLOAT D = kd * ( error - last_error ) / dT; - last_error = error; - - // total - FLOAT output = P + I + D; - - // limitter - if ( output > max ){ - output = max; - } else if ( output < min ){ - output = min; - } + // -error + FLOAT error = Tnow - Tset; - boSetPeltierVoltage( output ); // set the output + // proportional term + FLOAT P = kp * error; + + // integral term + integral += error * dT; + FLOAT I = ki * integral; + + // derivative term + FLOAT D = kd * ( error - last_error ) / dT; + last_error = error; + + // total + FLOAT output = P + I + D; + + // limitter + if ( output > max ){ + output = max; + integral = 0; + } else if ( output < min ){ + output = min; + integral = 0; + } - } else if ( VARH_u32GetVariableData( VARH_eMode ) == PECO_eConst_Voltage ) - { - boSetPeltierVoltage( VARH_flGetVariableData( VARH_eControlVoltage ) ); // set the output - } + boSetPeltierVoltage( output ); // set the output + } else if ( VARH_u32GetVariableData( VARH_eMode ) == PECO_eConst_Voltage ) + { + boSetPeltierVoltage( VARH_flGetVariableData( VARH_eControlVoltage ) ); // set the output + last_error = 0; + integral = 0; + } + } else { + last_error = 0; + integral = 0; + } } diff --git a/Core/Application/PECO_PeltierController.h b/Core/Application/PECO_PeltierController.h index 55b9201..960971a 100644 --- a/Core/Application/PECO_PeltierController.h +++ b/Core/Application/PECO_PeltierController.h @@ -88,7 +88,6 @@ typedef enum { //================================================================================================= BOOL PECO_boInitializeModule( VOID ); -BOOL PECO_boSetTemperature( S16 Temperature ); VOID PECO_Enable( BOOL Enable ); BOOL PECO_isEnabled( VOID ); diff --git a/Makefile b/Makefile index b9c4562..52738e5 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [3.15.2] date: [Thu Jul 07 18:17:37 CEST 2022] +# File automatically-generated by tool: [projectgenerator] version: [3.15.2] date: [Tue Sep 06 08:17:33 CEST 2022] ########################################################################################################################## # ------------------------------------------------