implemented PID Loop
This commit is contained in:
@ -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){
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -7,7 +7,7 @@
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// Project: Peltier Controller V2
|
||||
// Author: Noah Piqu<71> (noah.pique@psi.ch)
|
||||
// Author: Noah Piqu<71> (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
|
||||
}
|
||||
|
@ -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 );
|
||||
}
|
||||
@ -230,6 +229,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)
|
||||
{
|
||||
// TODO: check the measurements, everything ok?
|
||||
|
||||
|
||||
// PID Regelung
|
||||
} else if (VARH_uGetVariableData(VARH_eMode) == PECO_eConstVoltage)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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] );
|
||||
|
@ -21,11 +21,13 @@
|
||||
//
|
||||
// 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
|
||||
//{
|
||||
// /* Sections */
|
||||
// SECTIONS
|
||||
// {
|
||||
// /* NOLOAD is required for not ereasing this block */
|
||||
// .user_data (NOLOAD) :
|
||||
// {
|
||||
@ -133,7 +135,7 @@ __attribute__((__section__(".user_data"))) const uint8_t userConfig[8];
|
||||
BOOL USFL_boInitializeModule( VOID )
|
||||
{
|
||||
|
||||
|
||||
// TODO: notes
|
||||
return( TRUE );
|
||||
}
|
||||
|
||||
|
@ -62,6 +62,7 @@ SECTIONS
|
||||
*(.user_data)
|
||||
. = ALIGN(4);
|
||||
} > DATA
|
||||
|
||||
/* The startup code into "FLASH" Rom type memory */
|
||||
.isr_vector :
|
||||
{
|
||||
|
Reference in New Issue
Block a user