implemented PID Loop

This commit is contained in:
2022-01-09 14:23:22 +01:00
parent ff5a05d32b
commit 26522970e7
8 changed files with 74 additions and 27 deletions

View File

@ -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
}
}