From e0b30f942cfd9584ae58e49e7e1392d92a876aca Mon Sep 17 00:00:00 2001 From: Pablo Date: Mon, 19 Jun 2023 09:31:11 +0200 Subject: [PATCH 01/10] APP_CtrlCheckErrors implemented --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 149 +++++++++++++++++++++++ firmware/Sources/APP/APP_CTRL/app_ctrl.h | 73 +++++++++++ 2 files changed, 222 insertions(+) create mode 100644 firmware/Sources/APP/APP_CTRL/app_ctrl.c create mode 100644 firmware/Sources/APP/APP_CTRL/app_ctrl.h diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c new file mode 100644 index 0000000..c668a0c --- /dev/null +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -0,0 +1,149 @@ +/********************************************************************************* +* @file : app_ctrl.c +* @brief : Implementation of APP CTRL +**********************************************************************************/ + +/**********************************************************************************/ +/* Include common and project definition header */ +/**********************************************************************************/ + +#include "app_ctrl.h" + +/**********************************************************************************/ +/* Include other headers */ +/**********************************************************************************/ + +#include "mid_reg.h" //Import MID_REG_info to check type +#include "epc_conf.h" +//#include "mid_pwr.h" +/**********************************************************************************/ +/* Definition of imported constant data */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local symbolic constants */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local function like macros */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local types (typedef, enum, struct, union) */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local variables */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of exported variables */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of exported constant data */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of imported constant data */ +/**********************************************************************************/ +extern const MID_REG_info_s EPC_CONF_info; + +/**********************************************************************************/ +/* Declaration of local function prototypes */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local constant data */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local functions */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of exported functions */ +/**********************************************************************************/ + +APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, + const MID_REG_meas_property_s * meas, const MID_REG_limit_s * limits){ + + res = APP_CTRL_RESULT_ERROR_INT; + + //Inverse priority check to set lastErrVal to error with the biggest priority + + // Check body temp + if ((EPC_CONF_info.hwVer%2 == 0) && // TODO: Use function to know if there is sensor present + ((meas->tempBody > limits->tempMax) || (meas->tempBody < limits->tempMin))){ + errors->tempErr = MID_REG_ERROR_RAISED; + errors->lastErrVal = (uint16_t)meas->tempBody; + res = APP_CTRL_RESULT_ERROR_FOUND; + }else{ + errors->tempErr = MID_REG_ERROR_NONE; + } + + // Check anode temp + if ((EPC_CONF_info.hwVer%2 == 0) && // TODO: Use function to know if there is sensor present + ((meas->tempAnod > limits->tempMax) || (meas->tempAnod < limits->tempMin))){ + errors->tempErr = MID_REG_ERROR_RAISED; + errors->lastErrVal = (uint16_t)meas->tempBody; + res = APP_CTRL_RESULT_ERROR_FOUND; + }else{ + errors->tempErr = MID_REG_ERROR_NONE; + } + + // Check amb temp + if ((EPC_CONF_info.hwVer%2 == 0) && // TODO: Use function to know if there is sensor present + ((meas->tempBody > limits->tempMax) || (meas->tempBody < limits->tempMin))){ + errors->tempErr = MID_REG_ERROR_RAISED; + errors->lastErrVal = (uint16_t)meas->tempBody; + res = APP_CTRL_RESULT_ERROR_FOUND; + }else{ + errors->tempErr = MID_REG_ERROR_NONE; + } + + // Check ls current + if (meas->lsCurr > limits->lsCurrMax || meas->lsCurr < limits->lsCurrMin){ + errors->lsCurrErr = MID_REG_ERROR_RAISED; + errors->lastErrVal = (uint16_t)meas->lsCurr; + res = APP_CTRL_RESULT_ERROR_FOUND; + }else{ + errors->lsCurrErr = MID_REG_ERROR_NONE; + } + + // Check ls voltage + if (meas->lsVolt > limits->lsVoltMax || meas->lsVolt < limits->lsVoltMin){ + errors->lsVoltErr = MID_REG_ERROR_RAISED; + errors->lastErrVal = meas->lsVolt; + res = APP_CTRL_RESULT_ERROR_FOUND; + }else{ + errors->lsVoltErr = MID_REG_ERROR_NONE; + } + + // Check hs voltage + if (meas->hsVolt > limits->hsVoltMax || meas->hsVolt < limits->hsVoltMin){ + errors->hsVoltErr = MID_REG_ERROR_RAISED; + errors->lastErrVal = meas->hsVolt; + res = APP_CTRL_RESULT_ERROR_FOUND; + }else{ + errors->hsVoltErr = MID_REG_ERROR_NONE; + } + + + // Check internal error flag && comm flag to stop conversion if raised in other modules + if (errors->commErr == MID_REG_ERROR_RAISED || errors->intErr == MID_REG_ERROR_RAISED){ + res = APP_CTRL_RESULT_ERROR_FOUND; + } + + // Disable power conversion if any error is raised + if (res == APP_CTRL_RESULT_ERROR_FOUND){ + //TODO: Disable PWM -> MID_Pwr if APP_CTRL_RESULT_ERROR_FOUND + }else{ + res = APP_CTRL_RESULT_SUCCESS; + } + + return res; +} + + + diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.h b/firmware/Sources/APP/APP_CTRL/app_ctrl.h new file mode 100644 index 0000000..755ea1a --- /dev/null +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.h @@ -0,0 +1,73 @@ +/********************************************************************************* +* @file : app_ctrl.h +* @brief : APP header file for CONTROL of EPC +**********************************************************************************/ + +#ifndef APP_CTRL_H_ +#define APP_CTRL_H_ + +/**********************************************************************************/ +/* Project Includes */ +/**********************************************************************************/ + +#include "mid_reg.h" +/**********************************************************************************/ +/* Include other headers */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local symbolic constants */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local function like macros */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of exported types (typedef, enum, struct, union) */ +/**********************************************************************************/ + +/** + * @enum APP_CTRL_result_e + * @brief Structure for the result of the APP CTRL operations. + */ +typedef enum +{ + APP_CTRL_RESULT_SUCCESS = 0x0U, /**< APP_CTRL success operation result **/ + APP_CTRL_RESULT_ERROR_FOUND = 0x01U, /**< APP_CTRL error found**/ + APP_CTRL_RESULT_ERROR_INT = 0x02U /**< APP_CTRL error in calculus **/ +} APP_CTRL_result_e; + +/**********************************************************************************/ +/* Definition of exported variables */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of local constant data */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of exported variables */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Definition of exported constant data */ +/**********************************************************************************/ + +/**********************************************************************************/ +/* Declaration of exported function prototypes */ +/**********************************************************************************/ + + +/** + * @fn APP_CTRL_result_e APP_CtrlCheckErrors(MID_REG_errorStatus_s, + * MID_REG_meas_property_s, MID_REG_limit_s ) + * @brief Check if there is some value in measures above or below limits and updates + * the error register + * @return @ref APP_CTRL_RESULT_SUCCESS if no errors, APP_CTRL_RESULT_ERROR_FOUND if + * errors found and APP_CTRL_RESULT_ERROR_INT if any error in comparision + */ +APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s *, const MID_REG_meas_property_s *, const MID_REG_limit_s *); + + +#endif /* APP_APP_CTRL_APP_CTRL_H_ */ From eaa45b495a94f35c00796246510953d64a628405 Mon Sep 17 00:00:00 2001 From: Pablo Date: Mon, 19 Jun 2023 14:40:14 +0200 Subject: [PATCH 02/10] APP_CtrlUpdate structure added --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 124 ++++++++++++++++++++++- firmware/Sources/APP/APP_CTRL/app_ctrl.h | 14 ++- 2 files changed, 135 insertions(+), 3 deletions(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index c668a0c..78a70ca 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -23,6 +23,8 @@ /**********************************************************************************/ /* Definition of local symbolic constants */ /**********************************************************************************/ +#define to_dW 100000 +#define steps_to_ms 10 //10kHz to 1kHz (1ms) /**********************************************************************************/ /* Definition of local function like macros */ @@ -31,11 +33,27 @@ /**********************************************************************************/ /* Definition of local types (typedef, enum, struct, union) */ /**********************************************************************************/ +typedef enum +{ + limit_reached = 0x0U, + limit_not_reached = 0x01U, + limit_error +} limit_status_e; + +typedef enum +{ + action_charge = 0x0U, + action_discharge = 0x01U, + action_wait = 0x02 +} action_e; + /**********************************************************************************/ /* Definition of local variables */ /**********************************************************************************/ +static limit_status_e ctrl_status = limit_reached; + /**********************************************************************************/ /* Definition of exported variables */ /**********************************************************************************/ @@ -61,6 +79,64 @@ extern const MID_REG_info_s EPC_CONF_info; /* Definition of local functions */ /**********************************************************************************/ +void updateCtrlTime(uint32_t * ctrl_time){ + static uint8_t steps = 0; + steps +=1; + if (steps >= steps_to_ms){ + steps = 0; + ctrl_time +=1; + } +} + +limit_status_e checkLimit(MID_REG_control_s * mode, const MID_REG_meas_property_s * meas, action_e ctrl_action, uint32_t ctrl_time){ + limit_status_e res = limit_not_reached; + //check mode + + // voltage limit, charge + if (mode->limitType == MID_REG_LIMIT_VOLT && ctrl_action == action_charge){ + if (meas->lsVolt >= (uint16_t)mode->limRef){ + res = limit_reached; + } + } // voltage limit, discharge + else if(mode->limitType == MID_REG_LIMIT_VOLT && ctrl_action == action_discharge){ + if (meas->lsVolt <= (uint16_t)mode->limRef){ + res = limit_reached; + } + } // current limit, charge + else if(mode->limitType == MID_REG_LIMIT_CURR && ctrl_action == action_charge){ + if (meas->lsCurr <= (int16_t)mode->limRef){ + res = limit_reached; + } + } // current limit, discharge + else if(mode->limitType == MID_REG_LIMIT_CURR && ctrl_action == action_discharge){ + if (meas->lsCurr >= (int16_t)mode->limRef){ + res = limit_reached; + } + } // power, charge + else if(mode->limitType == MID_REG_LIMIT_PWR && ctrl_action == action_charge){ + int16_t power = (int16_t)((int32_t)(meas->lsCurr * (int16_t)meas->lsVolt) / to_dW); + if (power <= (int16_t)mode->limRef){ + res = limit_reached; + } + } // power, discharge + else if(mode->limitType == MID_REG_LIMIT_PWR && ctrl_action == action_discharge){ + int16_t power = (int16_t)((int32_t)(meas->lsCurr * (int16_t)meas->lsVolt) / to_dW); + if (power >= (int16_t)mode->limRef){ + res = limit_reached; + } + } // time + else if(mode->limitType == MID_REG_LIMIT_TIME){ + if (ctrl_time >= mode->limRef){ + res = limit_reached; + } + }else{ + res = limit_error; + } + + return res; +} + + /**********************************************************************************/ /* Definition of exported functions */ /**********************************************************************************/ @@ -68,7 +144,7 @@ extern const MID_REG_info_s EPC_CONF_info; APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, const MID_REG_meas_property_s * meas, const MID_REG_limit_s * limits){ - res = APP_CTRL_RESULT_ERROR_INT; + APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; //Inverse priority check to set lastErrVal to error with the biggest priority @@ -129,7 +205,6 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, errors->hsVoltErr = MID_REG_ERROR_NONE; } - // Check internal error flag && comm flag to stop conversion if raised in other modules if (errors->commErr == MID_REG_ERROR_RAISED || errors->intErr == MID_REG_ERROR_RAISED){ res = APP_CTRL_RESULT_ERROR_FOUND; @@ -146,4 +221,49 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, } +APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_property_s * meas, + const MID_REG_limit_s * limits){ + APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; + static action_e ctrl_action = action_wait; + static uint32_t ctrl_time = 0; + + updateCtrlTime(&ctrl_time); + ctrl_status = checkLimit(mode, meas, ctrl_action, ctrl_time); + + if (ctrl_status == limit_not_reached){ + //check if limit is reached + switch(mode->mode) + { + case MID_REG_MODE_IDLE: + //TODO: Disable PWM + //change limit to reached + break; + case MID_REG_MODE_WAIT: + //TODO: Disable PWM + break; + case MID_REG_MODE_CV: + //call to MID_PWR_PI + break; + case MID_REG_MODE_CC: + //call to MID_PWR_PI + break; + case MID_REG_MODE_CP: + //call to MID_PWR_PI + break; + default: + //TODO: Disable PWM + res = APP_CTRL_RESULT_ERROR_INT; + } + + }else if(ctrl_status == limit_reached){ + //change mode to IDLE + + res = APP_CTRL_RESULT_SUCCESS; + }else{ + res = APP_CTRL_RESULT_ERROR_INT; + } + + return res; + +} diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.h b/firmware/Sources/APP/APP_CTRL/app_ctrl.h index 755ea1a..2569d6e 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.h +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.h @@ -65,9 +65,21 @@ typedef enum * @brief Check if there is some value in measures above or below limits and updates * the error register * @return @ref APP_CTRL_RESULT_SUCCESS if no errors, APP_CTRL_RESULT_ERROR_FOUND if - * errors found and APP_CTRL_RESULT_ERROR_INT if any error in comparision + * errors found and APP_CTRL_RESULT_ERROR_INT if any error in comparison */ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s *, const MID_REG_meas_property_s *, const MID_REG_limit_s *); +/** + * @fn APP_CTRL_result_e APP_CtrlUpdate(MID_REG_control_s, + * MID_REG_meas_property_s, MID_REG_limit_s ) + * @brief Update internal PIs and apply control to PWM. If limit is reached, stop PWM + * @return @ref APP_CTRL_RESULT_SUCCESS if no errors, APP_CTRL_RESULT_ERROR_INT if + * errors in calculus or MID_PWR results + */ +APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s *, const MID_REG_meas_property_s *, const MID_REG_limit_s *); + + + + #endif /* APP_APP_CTRL_APP_CTRL_H_ */ From 6e303a3558163fd3ee48b3baccde2bd3608de026 Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 20 Jun 2023 11:00:26 +0200 Subject: [PATCH 03/10] APP_CTRL integrated with MID_PWR calls. Not tested --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 110 ++++++++++++++++------- firmware/Sources/APP/APP_CTRL/app_ctrl.h | 13 ++- 2 files changed, 88 insertions(+), 35 deletions(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index 78a70ca..66152e0 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -35,8 +35,9 @@ /**********************************************************************************/ typedef enum { - limit_reached = 0x0U, - limit_not_reached = 0x01U, + limit_reached = 0x0U, + limit_not_reached = 0x01U, + limit_already_reached = 0x02U, limit_error } limit_status_e; @@ -44,7 +45,8 @@ typedef enum { action_charge = 0x0U, action_discharge = 0x01U, - action_wait = 0x02 + action_wait = 0x02U, + action_error = 0x03U } action_e; @@ -52,7 +54,9 @@ typedef enum /* Definition of local variables */ /**********************************************************************************/ -static limit_status_e ctrl_status = limit_reached; +static limit_status_e ctrl_status = limit_already_reached; +static action_e ctrl_action = action_wait; +static uint32_t ctrl_time = 0; /**********************************************************************************/ /* Definition of exported variables */ @@ -88,6 +92,26 @@ void updateCtrlTime(uint32_t * ctrl_time){ } } +action_e calcNewAction(const MID_REG_control_s * mode, const MID_REG_meas_property_s * meas){ + action_e res = action_error; + switch(mode->mode) + { + case MID_REG_MODE_IDLE: + res = action_wait; + case MID_REG_MODE_WAIT: + res = action_wait; + case MID_REG_MODE_CC: + res = (mode->modeRef >= 0) ? action_charge : action_discharge; + case MID_REG_MODE_CV: + res = (mode->modeRef <= meas->lsVolt) ? action_charge : action_discharge; + case MID_REG_MODE_CP: + res = (mode->modeRef >= 0) ? action_charge : action_discharge; + default: + res = action_wait; + } + return res; +} + limit_status_e checkLimit(MID_REG_control_s * mode, const MID_REG_meas_property_s * meas, action_e ctrl_action, uint32_t ctrl_time){ limit_status_e res = limit_not_reached; //check mode @@ -153,7 +177,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, ((meas->tempBody > limits->tempMax) || (meas->tempBody < limits->tempMin))){ errors->tempErr = MID_REG_ERROR_RAISED; errors->lastErrVal = (uint16_t)meas->tempBody; - res = APP_CTRL_RESULT_ERROR_FOUND; + res = APP_CTRL_RESULT_ERROR_RAISED; }else{ errors->tempErr = MID_REG_ERROR_NONE; } @@ -163,7 +187,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, ((meas->tempAnod > limits->tempMax) || (meas->tempAnod < limits->tempMin))){ errors->tempErr = MID_REG_ERROR_RAISED; errors->lastErrVal = (uint16_t)meas->tempBody; - res = APP_CTRL_RESULT_ERROR_FOUND; + res = APP_CTRL_RESULT_ERROR_RAISED; }else{ errors->tempErr = MID_REG_ERROR_NONE; } @@ -173,7 +197,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, ((meas->tempBody > limits->tempMax) || (meas->tempBody < limits->tempMin))){ errors->tempErr = MID_REG_ERROR_RAISED; errors->lastErrVal = (uint16_t)meas->tempBody; - res = APP_CTRL_RESULT_ERROR_FOUND; + res = APP_CTRL_RESULT_ERROR_RAISED; }else{ errors->tempErr = MID_REG_ERROR_NONE; } @@ -182,7 +206,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, if (meas->lsCurr > limits->lsCurrMax || meas->lsCurr < limits->lsCurrMin){ errors->lsCurrErr = MID_REG_ERROR_RAISED; errors->lastErrVal = (uint16_t)meas->lsCurr; - res = APP_CTRL_RESULT_ERROR_FOUND; + res = APP_CTRL_RESULT_ERROR_RAISED; }else{ errors->lsCurrErr = MID_REG_ERROR_NONE; } @@ -191,7 +215,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, if (meas->lsVolt > limits->lsVoltMax || meas->lsVolt < limits->lsVoltMin){ errors->lsVoltErr = MID_REG_ERROR_RAISED; errors->lastErrVal = meas->lsVolt; - res = APP_CTRL_RESULT_ERROR_FOUND; + res = APP_CTRL_RESULT_ERROR_RAISED; }else{ errors->lsVoltErr = MID_REG_ERROR_NONE; } @@ -200,19 +224,19 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, if (meas->hsVolt > limits->hsVoltMax || meas->hsVolt < limits->hsVoltMin){ errors->hsVoltErr = MID_REG_ERROR_RAISED; errors->lastErrVal = meas->hsVolt; - res = APP_CTRL_RESULT_ERROR_FOUND; + res = APP_CTRL_RESULT_ERROR_RAISED; }else{ errors->hsVoltErr = MID_REG_ERROR_NONE; } // Check internal error flag && comm flag to stop conversion if raised in other modules if (errors->commErr == MID_REG_ERROR_RAISED || errors->intErr == MID_REG_ERROR_RAISED){ - res = APP_CTRL_RESULT_ERROR_FOUND; + res = APP_CTRL_RESULT_ERROR_RAISED; } // Disable power conversion if any error is raised - if (res == APP_CTRL_RESULT_ERROR_FOUND){ - //TODO: Disable PWM -> MID_Pwr if APP_CTRL_RESULT_ERROR_FOUND + if (res == APP_CTRL_RESULT_ERROR_RAISED){ + //TODO: Disable PWM -> MID_Pwr if APP_CTRL_RESULT_ERROR_RAISED }else{ res = APP_CTRL_RESULT_SUCCESS; } @@ -220,12 +244,10 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, return res; } - APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_property_s * meas, const MID_REG_limit_s * limits){ APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; - static action_e ctrl_action = action_wait; - static uint32_t ctrl_time = 0; + MID_PWR_result_e internalRes = MID_PWR_RESULT_TIMEOUT; updateCtrlTime(&ctrl_time); ctrl_status = checkLimit(mode, meas, ctrl_action, ctrl_time); @@ -234,36 +256,60 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p //check if limit is reached switch(mode->mode) { - case MID_REG_MODE_IDLE: - //TODO: Disable PWM - //change limit to reached - break; case MID_REG_MODE_WAIT: - //TODO: Disable PWM - break; + internalRes = MID_PwrSetOutput(MID_PWR_Disable); + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; case MID_REG_MODE_CV: - //call to MID_PWR_PI - break; + internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CV, limits); + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; case MID_REG_MODE_CC: - //call to MID_PWR_PI - break; + internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CC, limits); + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; case MID_REG_MODE_CP: - //call to MID_PWR_PI - break; + internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CP, limits); + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; default: - //TODO: Disable PWM + internalRes = MID_PwrSetOutput(MID_PWR_Disable); res = APP_CTRL_RESULT_ERROR_INT; } }else if(ctrl_status == limit_reached){ - //change mode to IDLE - - res = APP_CTRL_RESULT_SUCCESS; + MID_REG_control_s newMode = { + MID_REG_DISABLED, // outStatus + MID_REG_MODE_IDLE, // mode + MID_REG_LIMIT_TIME, // limitType + 0, // modeRef + 0 // limRef + }; + + internalRes = MID_PwrSetOutput(MID_PWR_Disable); + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; + + if (res == APP_CTRL_RESULT_SUCCESS){ + res = APP_CtrlApplyNewMode(newMode, mode, meas); + ctrl_status = limit_already_reached; + } }else{ res = APP_CTRL_RESULT_ERROR_INT; } return res; +} +APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s * newMode, MID_REG_control_s * mode, + const MID_REG_meas_property_s * meas){ + APP_CTRL_result_e res = APP_CTRL_RESULT_SUCCESS; + MID_PWR_result_e internalRes = MID_PWR_RESULT_TIMEOUT; + ctrl_action = calcNewAction(newMode, meas); + ctrl_status = limit_not_reached; + ctrl_time = 0; + mode = newMode; //TODO: esto es deepcopy o iguala punteros? + //Enable PWR output for the modes that require power transfer + if (newMode->mode == MID_REG_MODE_CV || newMode->mode == MID_REG_MODE_CP || newMode->mode == MID_REG_MODE_CC){ + internalRes = MID_PwrSetOutput(MID_PWR_Disable); + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; + } + + return res; } diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.h b/firmware/Sources/APP/APP_CTRL/app_ctrl.h index 2569d6e..37545fb 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.h +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.h @@ -34,7 +34,7 @@ typedef enum { APP_CTRL_RESULT_SUCCESS = 0x0U, /**< APP_CTRL success operation result **/ - APP_CTRL_RESULT_ERROR_FOUND = 0x01U, /**< APP_CTRL error found**/ + APP_CTRL_RESULT_ERROR_RAISED = 0x01U, /**< APP_CTRL error found**/ APP_CTRL_RESULT_ERROR_INT = 0x02U /**< APP_CTRL error in calculus **/ } APP_CTRL_result_e; @@ -64,12 +64,11 @@ typedef enum * MID_REG_meas_property_s, MID_REG_limit_s ) * @brief Check if there is some value in measures above or below limits and updates * the error register - * @return @ref APP_CTRL_RESULT_SUCCESS if no errors, APP_CTRL_RESULT_ERROR_FOUND if + * @return @ref APP_CTRL_RESULT_SUCCESS if no errors, APP_CTRL_RESULT_ERROR_RAISED if * errors found and APP_CTRL_RESULT_ERROR_INT if any error in comparison */ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s *, const MID_REG_meas_property_s *, const MID_REG_limit_s *); - /** * @fn APP_CTRL_result_e APP_CtrlUpdate(MID_REG_control_s, * MID_REG_meas_property_s, MID_REG_limit_s ) @@ -79,6 +78,14 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s *, const MID_REG_me */ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s *, const MID_REG_meas_property_s *, const MID_REG_limit_s *); +/** + * @fn APP_CTRL_result_e APP_CtrlApplyNewMode(MID_REG_control_s, + * MID_REG_control_s) + * @brief Reset ctrl internal variables and set the module to apply the new control mode + * @return @ref APP_CTRL_RESULT_SUCCESS if no errors, APP_CTRL_RESULT_ERROR_INT if + * any error + */ +APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s *, MID_REG_control_s *, const MID_REG_meas_property_s *); From fcf0728878f6ecabdee3eaa0f1abd710fe946026 Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 20 Jun 2023 11:02:24 +0200 Subject: [PATCH 04/10] pointer added to call --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index 66152e0..93d3cf6 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -286,7 +286,7 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; if (res == APP_CTRL_RESULT_SUCCESS){ - res = APP_CtrlApplyNewMode(newMode, mode, meas); + res = APP_CtrlApplyNewMode(&newMode, mode, meas); ctrl_status = limit_already_reached; } }else{ From d17c6f058a73672551ba33c17659eb4c5fb74ea8 Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 20 Jun 2023 13:22:44 +0200 Subject: [PATCH 05/10] MID_REG names changed --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 4 +++- firmware/Sources/APP/APP_CTRL/app_ctrl.h | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index 93d3cf6..74c1eea 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -289,6 +289,8 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p res = APP_CtrlApplyNewMode(&newMode, mode, meas); ctrl_status = limit_already_reached; } + }else if(ctrl_status == limit_already_reached){ + //NOP }else{ res = APP_CTRL_RESULT_ERROR_INT; } @@ -303,7 +305,7 @@ APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s * newMode, MID_R ctrl_action = calcNewAction(newMode, meas); ctrl_status = limit_not_reached; ctrl_time = 0; - mode = newMode; //TODO: esto es deepcopy o iguala punteros? + memcpy(newMode, mode, sizeof(mode)); //Enable PWR output for the modes that require power transfer if (newMode->mode == MID_REG_MODE_CV || newMode->mode == MID_REG_MODE_CP || newMode->mode == MID_REG_MODE_CC){ internalRes = MID_PwrSetOutput(MID_PWR_Disable); diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.h b/firmware/Sources/APP/APP_CTRL/app_ctrl.h index 37545fb..2e1b69a 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.h +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.h @@ -60,14 +60,14 @@ typedef enum /** - * @fn APP_CTRL_result_e APP_CtrlCheckErrors(MID_REG_errorStatus_s, + * @fn APP_CTRL_result_e APP_CtrlCheckErrors(MID_REG_error_status_s, * MID_REG_meas_property_s, MID_REG_limit_s ) * @brief Check if there is some value in measures above or below limits and updates * the error register * @return @ref APP_CTRL_RESULT_SUCCESS if no errors, APP_CTRL_RESULT_ERROR_RAISED if * errors found and APP_CTRL_RESULT_ERROR_INT if any error in comparison */ -APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s *, const MID_REG_meas_property_s *, const MID_REG_limit_s *); +APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_error_status_s *, const MID_REG_meas_property_s *, const MID_REG_limit_s *); /** * @fn APP_CTRL_result_e APP_CtrlUpdate(MID_REG_control_s, From 98b6e3db82491847df5f8cfb750b7b45040c70e4 Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 20 Jun 2023 15:24:49 +0200 Subject: [PATCH 06/10] bug in pwm activation call --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index 74c1eea..2c3aa96 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -308,7 +308,7 @@ APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s * newMode, MID_R memcpy(newMode, mode, sizeof(mode)); //Enable PWR output for the modes that require power transfer if (newMode->mode == MID_REG_MODE_CV || newMode->mode == MID_REG_MODE_CP || newMode->mode == MID_REG_MODE_CC){ - internalRes = MID_PwrSetOutput(MID_PWR_Disable); + internalRes = MID_PwrSetOutput(MID_PWR_Enable); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; } From 12d991e5608b518380209f64a4134b309c3c6732 Mon Sep 17 00:00:00 2001 From: Pablo Date: Wed, 21 Jun 2023 13:51:09 +0200 Subject: [PATCH 07/10] Pending TODO solved --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index 2c3aa96..189aecc 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -169,6 +169,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, const MID_REG_meas_property_s * meas, const MID_REG_limit_s * limits){ APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; + MID_PWR_result_e internalRes = MID_PWR_RESULT_TIMEOUT; //Inverse priority check to set lastErrVal to error with the biggest priority @@ -236,7 +237,8 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, // Disable power conversion if any error is raised if (res == APP_CTRL_RESULT_ERROR_RAISED){ - //TODO: Disable PWM -> MID_Pwr if APP_CTRL_RESULT_ERROR_RAISED + internalRes = MID_PwrSetOutput(MID_PWR_Enable); + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_ERROR_RAISED : APP_CTRL_RESULT_ERROR_INT; }else{ res = APP_CTRL_RESULT_SUCCESS; } From 60c1b3904e3d92197614ba45fbe61f2aec813082 Mon Sep 17 00:00:00 2001 From: Roberto Aldea Date: Tue, 27 Jun 2023 10:57:53 +0200 Subject: [PATCH 08/10] Update with changes from MID_PWR interface --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index 189aecc..1f9cd45 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -237,7 +237,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, // Disable power conversion if any error is raised if (res == APP_CTRL_RESULT_ERROR_RAISED){ - internalRes = MID_PwrSetOutput(MID_PWR_Enable); + internalRes = MID_PwrSetOutput(MID_PWR_Enable, meas->hsVolt, meas->lsVolt); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_ERROR_RAISED : APP_CTRL_RESULT_ERROR_INT; }else{ res = APP_CTRL_RESULT_SUCCESS; @@ -259,7 +259,7 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p switch(mode->mode) { case MID_REG_MODE_WAIT: - internalRes = MID_PwrSetOutput(MID_PWR_Disable); + internalRes = MID_PwrSetOutput(MID_PWR_Disable, meas->hsVolt, meas->lsVolt); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; case MID_REG_MODE_CV: internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CV, limits); @@ -271,7 +271,7 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CP, limits); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; default: - internalRes = MID_PwrSetOutput(MID_PWR_Disable); + internalRes = MID_PwrSetOutput(MID_PWR_Disable, meas->hsVolt, meas->lsVolt); res = APP_CTRL_RESULT_ERROR_INT; } @@ -284,7 +284,7 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p 0 // limRef }; - internalRes = MID_PwrSetOutput(MID_PWR_Disable); + internalRes = MID_PwrSetOutput(MID_PWR_Disable, meas->hsVolt, meas->lsVolt); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; if (res == APP_CTRL_RESULT_SUCCESS){ @@ -310,7 +310,7 @@ APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s * newMode, MID_R memcpy(newMode, mode, sizeof(mode)); //Enable PWR output for the modes that require power transfer if (newMode->mode == MID_REG_MODE_CV || newMode->mode == MID_REG_MODE_CP || newMode->mode == MID_REG_MODE_CC){ - internalRes = MID_PwrSetOutput(MID_PWR_Enable); + internalRes = MID_PwrSetOutput(MID_PWR_Enable, meas->hsVolt, meas->lsVolt); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; } From 9e2803cb76ff0283e24c00164310c05a34c7471e Mon Sep 17 00:00:00 2001 From: Roberto Aldea Date: Wed, 28 Jun 2023 14:12:49 +0200 Subject: [PATCH 09/10] Update with changes from MID_PWR --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 64 +++++++++++++++--------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index 1f9cd45..be4267f 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -15,7 +15,8 @@ #include "mid_reg.h" //Import MID_REG_info to check type #include "epc_conf.h" -//#include "mid_pwr.h" +#include "mid_pwr.h" +#include /**********************************************************************************/ /* Definition of imported constant data */ /**********************************************************************************/ @@ -23,7 +24,6 @@ /**********************************************************************************/ /* Definition of local symbolic constants */ /**********************************************************************************/ -#define to_dW 100000 #define steps_to_ms 10 //10kHz to 1kHz (1ms) /**********************************************************************************/ @@ -103,7 +103,7 @@ action_e calcNewAction(const MID_REG_control_s * mode, const MID_REG_meas_proper case MID_REG_MODE_CC: res = (mode->modeRef >= 0) ? action_charge : action_discharge; case MID_REG_MODE_CV: - res = (mode->modeRef <= meas->lsVolt) ? action_charge : action_discharge; + res = (mode->modeRef >= meas->lsVolt) ? action_charge : action_discharge; case MID_REG_MODE_CP: res = (mode->modeRef >= 0) ? action_charge : action_discharge; default: @@ -138,13 +138,13 @@ limit_status_e checkLimit(MID_REG_control_s * mode, const MID_REG_meas_property_ } } // power, charge else if(mode->limitType == MID_REG_LIMIT_PWR && ctrl_action == action_charge){ - int16_t power = (int16_t)((int32_t)(meas->lsCurr * (int16_t)meas->lsVolt) / to_dW); + int16_t power = (int16_t)((int32_t)(meas->lsCurr * (int16_t)meas->lsVolt) / MID_PWR_TO_dW); if (power <= (int16_t)mode->limRef){ res = limit_reached; } } // power, discharge else if(mode->limitType == MID_REG_LIMIT_PWR && ctrl_action == action_discharge){ - int16_t power = (int16_t)((int32_t)(meas->lsCurr * (int16_t)meas->lsVolt) / to_dW); + int16_t power = (int16_t)((int32_t)(meas->lsCurr * (int16_t)meas->lsVolt) / MID_PWR_TO_dW); if (power >= (int16_t)mode->limRef){ res = limit_reached; } @@ -153,6 +153,8 @@ limit_status_e checkLimit(MID_REG_control_s * mode, const MID_REG_meas_property_ if (ctrl_time >= mode->limRef){ res = limit_reached; } + }else if (ctrl_action==action_wait){ + res = limit_not_reached; }else{ res = limit_error; } @@ -165,7 +167,7 @@ limit_status_e checkLimit(MID_REG_control_s * mode, const MID_REG_meas_property_ /* Definition of exported functions */ /**********************************************************************************/ -APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, +APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_error_status_s * errors, const MID_REG_meas_property_s * meas, const MID_REG_limit_s * limits){ APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; @@ -237,7 +239,7 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, // Disable power conversion if any error is raised if (res == APP_CTRL_RESULT_ERROR_RAISED){ - internalRes = MID_PwrSetOutput(MID_PWR_Enable, meas->hsVolt, meas->lsVolt); + internalRes = MID_PwrSetOutput(MID_PWR_Disable); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_ERROR_RAISED : APP_CTRL_RESULT_ERROR_INT; }else{ res = APP_CTRL_RESULT_SUCCESS; @@ -249,33 +251,39 @@ APP_CTRL_result_e APP_CtrlCheckErrors (MID_REG_errorStatus_s * errors, APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_property_s * meas, const MID_REG_limit_s * limits){ APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; - MID_PWR_result_e internalRes = MID_PWR_RESULT_TIMEOUT; - + MID_PWR_result_e internalRes = MID_PWR_RESULT_SUCCESS; +// internalRes = MID_PwrCalculateD0(meas->hsVolt, meas->lsVolt); updateCtrlTime(&ctrl_time); ctrl_status = checkLimit(mode, meas, ctrl_action, ctrl_time); - if (ctrl_status == limit_not_reached){ + if (ctrl_status == limit_not_reached && internalRes == MID_PWR_RESULT_SUCCESS){ //check if limit is reached switch(mode->mode) { case MID_REG_MODE_WAIT: - internalRes = MID_PwrSetOutput(MID_PWR_Disable, meas->hsVolt, meas->lsVolt); + internalRes = MID_PwrSetOutput(MID_PWR_Disable); + mode->outStatus = MID_PWR_Disable; res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; + break; case MID_REG_MODE_CV: - internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CV, limits); + internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CV, *limits); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; + break; case MID_REG_MODE_CC: - internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CC, limits); + internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CC, *limits); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; + break; case MID_REG_MODE_CP: - internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CP, limits); + internalRes = MID_PwrApplyCtrl(mode->modeRef, meas->lsVolt, meas->lsCurr, MID_PWR_MODE_CP, *limits); res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; + break; default: - internalRes = MID_PwrSetOutput(MID_PWR_Disable, meas->hsVolt, meas->lsVolt); + internalRes = MID_PwrSetOutput(MID_PWR_Disable); + mode->outStatus = MID_PWR_Disable; res = APP_CTRL_RESULT_ERROR_INT; + break; } - - }else if(ctrl_status == limit_reached){ + }else if(ctrl_status == limit_reached && internalRes == MID_PWR_RESULT_SUCCESS){ MID_REG_control_s newMode = { MID_REG_DISABLED, // outStatus MID_REG_MODE_IDLE, // mode @@ -284,7 +292,8 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p 0 // limRef }; - internalRes = MID_PwrSetOutput(MID_PWR_Disable, meas->hsVolt, meas->lsVolt); + internalRes = MID_PwrSetOutput(MID_PWR_Disable); + mode->outStatus = MID_PWR_Disable; res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; if (res == APP_CTRL_RESULT_SUCCESS){ @@ -292,7 +301,7 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p ctrl_status = limit_already_reached; } }else if(ctrl_status == limit_already_reached){ - //NOP + res = APP_CTRL_RESULT_SUCCESS; }else{ res = APP_CTRL_RESULT_ERROR_INT; } @@ -302,17 +311,24 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s * newMode, MID_REG_control_s * mode, const MID_REG_meas_property_s * meas){ - APP_CTRL_result_e res = APP_CTRL_RESULT_SUCCESS; + APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; MID_PWR_result_e internalRes = MID_PWR_RESULT_TIMEOUT; + if (newMode->outStatus == MID_REG_DISABLED){ + internalRes = MID_PwrSetOutput(MID_PWR_Disable); + } ctrl_action = calcNewAction(newMode, meas); ctrl_status = limit_not_reached; ctrl_time = 0; - memcpy(newMode, mode, sizeof(mode)); + memcpy(mode, newMode, sizeof(MID_REG_control_s)); //Enable PWR output for the modes that require power transfer - if (newMode->mode == MID_REG_MODE_CV || newMode->mode == MID_REG_MODE_CP || newMode->mode == MID_REG_MODE_CC){ - internalRes = MID_PwrSetOutput(MID_PWR_Enable, meas->hsVolt, meas->lsVolt); - res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; + if ((newMode->mode == MID_REG_MODE_CV || newMode->mode == MID_REG_MODE_CP || newMode->mode == MID_REG_MODE_CC) && + newMode->outStatus == MID_REG_ENABLED){ + internalRes = MID_PwrCalculateD0(meas->hsVolt, meas->lsVolt); + if (internalRes == MID_PWR_RESULT_SUCCESS){ + internalRes = MID_PwrSetOutput(MID_PWR_Enable); + } } + res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; return res; } From fd894b043046b9d97265498fa9f0549b27d9a8cc Mon Sep 17 00:00:00 2001 From: Roberto Aldea Date: Wed, 28 Jun 2023 14:41:33 +0200 Subject: [PATCH 10/10] Update minor changes and with comments in code --- firmware/Sources/APP/APP_CTRL/app_ctrl.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/firmware/Sources/APP/APP_CTRL/app_ctrl.c b/firmware/Sources/APP/APP_CTRL/app_ctrl.c index be4267f..7bc37c1 100644 --- a/firmware/Sources/APP/APP_CTRL/app_ctrl.c +++ b/firmware/Sources/APP/APP_CTRL/app_ctrl.c @@ -252,15 +252,15 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p const MID_REG_limit_s * limits){ APP_CTRL_result_e res = APP_CTRL_RESULT_ERROR_INT; MID_PWR_result_e internalRes = MID_PWR_RESULT_SUCCESS; -// internalRes = MID_PwrCalculateD0(meas->hsVolt, meas->lsVolt); updateCtrlTime(&ctrl_time); ctrl_status = checkLimit(mode, meas, ctrl_action, ctrl_time); if (ctrl_status == limit_not_reached && internalRes == MID_PWR_RESULT_SUCCESS){ - //check if limit is reached + //check if limit is reached, if not apply control switch(mode->mode) { case MID_REG_MODE_WAIT: + // If change output to disable change also register to disable internalRes = MID_PwrSetOutput(MID_PWR_Disable); mode->outStatus = MID_PWR_Disable; res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; @@ -278,12 +278,14 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; break; default: + // If change output to disable change also register to disable internalRes = MID_PwrSetOutput(MID_PWR_Disable); mode->outStatus = MID_PWR_Disable; res = APP_CTRL_RESULT_ERROR_INT; break; } }else if(ctrl_status == limit_reached && internalRes == MID_PWR_RESULT_SUCCESS){ + MID_REG_control_s newMode = { MID_REG_DISABLED, // outStatus MID_REG_MODE_IDLE, // mode @@ -291,16 +293,18 @@ APP_CTRL_result_e APP_CtrlUpdate (MID_REG_control_s * mode, const MID_REG_meas_p 0, // modeRef 0 // limRef }; - + //First change output + // If change output to disable change also register to disable internalRes = MID_PwrSetOutput(MID_PWR_Disable); mode->outStatus = MID_PWR_Disable; res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT; - + //Then change the mode if (res == APP_CTRL_RESULT_SUCCESS){ res = APP_CtrlApplyNewMode(&newMode, mode, meas); ctrl_status = limit_already_reached; } }else if(ctrl_status == limit_already_reached){ + //If no operation needed is success res = APP_CTRL_RESULT_SUCCESS; }else{ res = APP_CTRL_RESULT_ERROR_INT; @@ -316,6 +320,7 @@ APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s * newMode, MID_R if (newMode->outStatus == MID_REG_DISABLED){ internalRes = MID_PwrSetOutput(MID_PWR_Disable); } + //ctrl_action will receive if mode is charging or discharging ctrl_action = calcNewAction(newMode, meas); ctrl_status = limit_not_reached; ctrl_time = 0; @@ -323,10 +328,14 @@ APP_CTRL_result_e APP_CtrlApplyNewMode (const MID_REG_control_s * newMode, MID_R //Enable PWR output for the modes that require power transfer if ((newMode->mode == MID_REG_MODE_CV || newMode->mode == MID_REG_MODE_CP || newMode->mode == MID_REG_MODE_CC) && newMode->outStatus == MID_REG_ENABLED){ + //Update DO when new mode is applied internalRes = MID_PwrCalculateD0(meas->hsVolt, meas->lsVolt); if (internalRes == MID_PWR_RESULT_SUCCESS){ internalRes = MID_PwrSetOutput(MID_PWR_Enable); } + }else { + //If not in control mode, the output is always disabled + mode->outStatus == MID_REG_DISABLED; } res = (internalRes == MID_PWR_RESULT_SUCCESS) ? APP_CTRL_RESULT_SUCCESS : APP_CTRL_RESULT_ERROR_INT;