From 26522970e7234e15e797c812b7b0bd704d27a8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20Piqu=C3=A9?= Date: Sun, 9 Jan 2022 14:23:22 +0100 Subject: [PATCH] implemented PID Loop --- Core/Application/MAIN_MainApplication.c | 4 +- Core/Application/VARH_VariableHandler.h | 2 +- Core/Drivers/ADCD_AdcDriver.c | 4 +- Core/Drivers/ADCD_AdcDriver.h | 4 +- Core/Drivers/PECO_PeltierController.c | 58 +++++++++++++++++++++---- Core/Drivers/TEMP_Temperature.c | 4 +- Core/Drivers/USFL_UserFlash.c | 24 +++++----- STM32L432KBUX_FLASH.ld | 1 + 8 files changed, 74 insertions(+), 27 deletions(-) diff --git a/Core/Application/MAIN_MainApplication.c b/Core/Application/MAIN_MainApplication.c index 2c3a7ba..ece7e65 100644 --- a/Core/Application/MAIN_MainApplication.c +++ b/Core/Application/MAIN_MainApplication.c @@ -227,6 +227,8 @@ PRIVATE VOID vMainTask( PVOID arg ) if( u32Flags & EVENT_NEW_MESSAGE ) { + // TODO: implement new protocol + CAND_Message stMessage; osMessageQueueGet( m_pstCANRxMsgQueueID, &stMessage, NULL, 0 ); @@ -234,7 +236,7 @@ PRIVATE VOID vMainTask( PVOID arg ) S16 s16Voltage = stMessage.au8Data[0] << 8; s16Voltage |= stMessage.au8Data[1]; - PECO_boSetTemperature( s16Voltage ); + VARH_vSetVariableData(VARH_eControlVoltage, s16Voltage); } else if(stMessage.u8Id == 0x13){ diff --git a/Core/Application/VARH_VariableHandler.h b/Core/Application/VARH_VariableHandler.h index 99fe2aa..ad15c4d 100644 --- a/Core/Application/VARH_VariableHandler.h +++ b/Core/Application/VARH_VariableHandler.h @@ -71,7 +71,7 @@ typedef enum VARH_ePID_kp, VARH_ePID_ki, - VARH_ePID_ki, + VARH_ePID_kd, VARH_ePID_Temp, VARH_ePID_Max, VARH_ePID_Min, diff --git a/Core/Drivers/ADCD_AdcDriver.c b/Core/Drivers/ADCD_AdcDriver.c index 8b17172..ea1e87a 100644 --- a/Core/Drivers/ADCD_AdcDriver.c +++ b/Core/Drivers/ADCD_AdcDriver.c @@ -234,13 +234,13 @@ BOOL ADCD_boInitializeModule( VOID ) } //------------------------------------------------------------------------------------------------- -// Function: ADCD_dReadData +// Function: ADCD_boReadData // Description: Reads the conversion data form the ADC // Parameters: PU8 pu8Error error // Returns: DOUBLE conversion data // //------------------------------------------------------------------------------------------------- -BOOL ADCD_dReadData(ADCD_EnTemps eChannel, PU8 pu8Error, PU16 pu16Data) +BOOL ADCD_boReadData(ADCD_EnTemps eChannel, PU8 pu8Error, PU16 pu16Data) { BOOL boOK = TRUE; *pu8Error = 0; // reset error state diff --git a/Core/Drivers/ADCD_AdcDriver.h b/Core/Drivers/ADCD_AdcDriver.h index 3d54b8a..a104272 100644 --- a/Core/Drivers/ADCD_AdcDriver.h +++ b/Core/Drivers/ADCD_AdcDriver.h @@ -7,7 +7,7 @@ //------------------------------------------------------------------------------------------------- // // Project: Peltier Controller V2 -// Author: Noah Piqué (noah.pique@psi.ch) +// Author: Noah Piqu� (noah.pique@psi.ch) // //------------------------------------------------------------------------------------------------- // @@ -89,7 +89,7 @@ typedef enum { BOOL ADCD_boInitializeModule( VOID ); //BOOL ADCD_boConfig( BOOL boFast, ADCD_pfnCallback pfnDataReadyCallback, PVOID pvCallbackArg ); -BOOL ADCD_dReadData(ADCD_EnTemps eChannel, PU8 pu8Error, PU16 pu16Data); +BOOL ADCD_boReadData(ADCD_EnTemps eChannel, PU8 pu8Error, PU16 pu16Data); #ifdef __cplusplus } diff --git a/Core/Drivers/PECO_PeltierController.c b/Core/Drivers/PECO_PeltierController.c index dc2d0fe..a4cc3ee 100644 --- a/Core/Drivers/PECO_PeltierController.c +++ b/Core/Drivers/PECO_PeltierController.c @@ -53,6 +53,8 @@ // Description: Definition of local constants (visible by this module only). //================================================================================================= +#define REFRESH 1000 // Refresh rate in ms + #define EVENT_TIMER_UPDATE ((U32)(1<<0)) #define EVENT_FLAGS_ALL ( EVENT_TIMER_UPDATE ) @@ -91,7 +93,6 @@ LOCAL osEventFlagsId_t m_pstEventID = NULL; BOOL boEnableOutput = FALSE; - //================================================================================================= // Section: LOCAL CONSTANTS // Description: Definition of local constants (visible by this module only). @@ -142,13 +143,11 @@ PRIVATE VOID vEventCallback( PVOID pvData ); //================================================================================================= - //================================================================================================= // Section: EXTERNAL VARIABLES // Description: Definition of external (global) variables. //================================================================================================= -extern DAC_HandleTypeDef hdac1; //================================================================================================= // Section: GLOBAL FUNCTIONS @@ -172,7 +171,7 @@ BOOL PECO_boInitializeModule( VOID ) boSetPeltierVoltage(0); - boOK &= (osTimerStart( m_pstUpdateTimer, 1000 ) == osOK ) ? TRUE : FALSE; + boOK &= (osTimerStart( m_pstUpdateTimer, REFRESH ) == osOK ) ? TRUE : FALSE; return( boOK ); } @@ -229,6 +228,10 @@ VOID PECO_vTask( PVOID arg ) UNUSED( arg ); U32 u32Flags; + + FLOAT last_error = 0; + FLOAT integral = 0; + FLOAT dT = REFRESH; // 1s while ( TRUE ) { @@ -238,12 +241,51 @@ VOID PECO_vTask( PVOID arg ) if( u32Flags & EVENT_TIMER_UPDATE ) { - if (VARH_uGetVariableData(VARH_eMode) == PECO_eConstTemp) + if (VARH_uGetVariableData(VARH_eMode).u32Val == PECO_eConstTemp) { - // PID Regelung - } else if (VARH_uGetVariableData(VARH_eMode) == PECO_eConstVoltage) + // TODO: check the measurements, everything ok? + + + // PID Regelung + + FLOAT kp = VARH_uGetVariableData(VARH_ePID_kp).flVal; + FLOAT ki = VARH_uGetVariableData(VARH_ePID_ki).flVal; + FLOAT kd = VARH_uGetVariableData(VARH_ePID_kd).flVal; + FLOAT max = VARH_uGetVariableData(VARH_ePID_Max).flVal; + FLOAT min = VARH_uGetVariableData(VARH_ePID_Min).flVal; + + FLOAT Tnow = VARH_uGetVariableData(VARH_eTemp_C).flVal; + FLOAT Tset = VARH_uGetVariableData(VARH_ePID_Temp).flVal; + + // -error + FLOAT error = Tnow - Tset; + + // 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; + } + + boSetPeltierVoltage(output); // set the output + + } else if (VARH_uGetVariableData(VARH_eMode).u32Val == PECO_eConstVoltage) { - boSetPeltierVoltage(VARH_uGetVariableData(VARH_eControlVoltage)) + boSetPeltierVoltage(VARH_uGetVariableData(VARH_eControlVoltage).flVal) // set the output } } diff --git a/Core/Drivers/TEMP_Temperature.c b/Core/Drivers/TEMP_Temperature.c index 45f7fd3..d4e3b19 100644 --- a/Core/Drivers/TEMP_Temperature.c +++ b/Core/Drivers/TEMP_Temperature.c @@ -156,8 +156,8 @@ PRIVATE VOID vTempTask( PVOID arg ) while( TRUE ) { - boOK &= ADCD_dReadData(ADCD_eHot, &error, &u16ADC_data[ADCD_eHot]); - boOK &= ADCD_dReadData(ADCD_eCold, &error, &u16ADC_data[ADCD_eCold]); + boOK &= ADCD_boReadData(ADCD_eHot, &error, &u16ADC_data[ADCD_eHot]); + boOK &= ADCD_boReadData(ADCD_eCold, &error, &u16ADC_data[ADCD_eCold]); if( boOK ) { flTempData[ADCD_eHot] = flConvertADCData( u16ADC_data[ADCD_eHot] ); diff --git a/Core/Drivers/USFL_UserFlash.c b/Core/Drivers/USFL_UserFlash.c index 4ec5020..7a10f0e 100644 --- a/Core/Drivers/USFL_UserFlash.c +++ b/Core/Drivers/USFL_UserFlash.c @@ -21,18 +21,20 @@ // // Description: This source file contains all functions dealing with internal flash for User Settings // +// STM32L432KBUX_FLASH.ld +// // DATA (rwx) : ORIGIN = 0x801F800, LENGTH = 2K // -///* Sections */ -//SECTIONS -//{ -// /* NOLOAD is required for not ereasing this block */ -// .user_data (NOLOAD) : -// { -// . = ALIGN(4); -// *(.user_data) -// . = ALIGN(4); -// } > DATA*/ +// /* Sections */ +// SECTIONS +// { +// /* NOLOAD is required for not ereasing this block */ +// .user_data (NOLOAD) : +// { +// . = ALIGN(4); +// *(.user_data) +// . = ALIGN(4); +// } > DATA*/ // //================================================================================================= @@ -133,7 +135,7 @@ __attribute__((__section__(".user_data"))) const uint8_t userConfig[8]; BOOL USFL_boInitializeModule( VOID ) { - + // TODO: notes return( TRUE ); } diff --git a/STM32L432KBUX_FLASH.ld b/STM32L432KBUX_FLASH.ld index d767154..fa85954 100644 --- a/STM32L432KBUX_FLASH.ld +++ b/STM32L432KBUX_FLASH.ld @@ -62,6 +62,7 @@ SECTIONS *(.user_data) . = ALIGN(4); } > DATA + /* The startup code into "FLASH" Rom type memory */ .isr_vector : {