/*
 * File: BSER_BattSocCalc.c
 *
 * Code generated for Simulink model 'BSER_BattSocCalc'.
 *
 * Model version                  : 'M1.5.6'
 * Simulink Coder version         : 8.13 (R2017b) 24-Jul-2017
 * C/C++ source code generated on : Mon Aug 16 10:09:47 2021
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Infineon->TriCore
 * Code generation objectives:
 *    1. MISRA C:2012 guidelines
 *    2. Execution efficiency
 * Validation result: Not run
 */

#include "BSER_BattSocCalc.h"
#include "BSER_BattSocCalc_private.h"
#include "look1_iflf_binlcapw.h"
#include "look1_iu32lftf_binlcapw.h"
#include "look2_iflf_binlcapw.h"

/* Named constants for Chart: '<S19>/Chart' */
#define BSER_BattSocCa_IN_SocFulDefault ((uint8_T)1U)
#define BSER_BattSocCalc_IN_SocFulSet  ((uint8_T)2U)

/* Named constants for Chart: '<S41>/Chart' */
#define BSER_BattSocCa_IN_AddCourse_Stt ((uint8_T)1U)
#define BSER_BattSocCalc_IN_Default_Stt ((uint8_T)2U)

/* Named constants for Chart: '<S20>/Chart' */
#define BSER_BattSocCalc_IN_Del_Course ((uint8_T)1U)
#define BSER_BattSocCalc_IN_Del_Default ((uint8_T)2U)

/* Named constants for Chart: '<S35>/Chart' */
#define BSER_BattSocCalc_IN_control    ((uint8_T)1U)
#define BSER_BattSocCalc_IN_int        ((uint8_T)2U)

/* Named constants for Chart: '<S18>/SOC State' */
#define BSER_BattSocCa_IN_littltcurrent ((uint8_T)4U)
#define BSER_BattSocCalc_IN_Fault      ((uint8_T)1U)
#define BSER_BattSocCalc_IN_Intial     ((uint8_T)2U)
#define BSER_BattSocCalc_IN_Run        ((uint8_T)3U)
#define BSER_BattSocCalc_IN_pre_INI    ((uint8_T)5U)

/* Exported data definition */

/* Volatile memory section */
#define ASW1_OEM_START_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: Volatile */
volatile real32_T BSER_cSocDchAhFulRev;/* DchAh Since Full Rev Soc. */
volatile real32_T BSER_rSocAvrg;
volatile real32_T BSER_rSocMax;
volatile real32_T BSER_rSocMin;
volatile uint8_T BSER_stSocFulHld;

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"
#define ASW1_OEM_START_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: OutputSignal */
volatile boolean_T BSER_DispSOCZero_flg;
volatile real32_T BSER_LowSocCorLowCellVoltThres2_C;
volatile real32_T BSER_LowSocCorLowCellVoltThres3_C;
volatile boolean_T BSER_flgSocSave;
volatile real32_T BSER_rRSocTableUpLimit_C;
volatile real32_T BSER_rSoc;
volatile real32_T BSER_rSocDspl;

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block signals (auto storage) */
B_BSER_BattSocCalc_T BSER_BattSocCalc_B;

/* Block states (auto storage) */
DW_BSER_BattSocCalc_T BSER_BattSocCalc_DW;

/* External outputs (root outports fed by signals with auto storage) */
ExtY_BSER_BattSocCalc_T BSER_BattSocCalc_Y;

/* Forward declaration for local functions */
static void BSER_enter_internal_RS_FlipFlop(const boolean_T *DataTypeConversion1,
  const boolean_T *LogicalOperator3);
static void BSER_BattSocCalc_RS_FlipFlop(const boolean_T *DataTypeConversion1,
  const boolean_T *LogicalOperator3);

/* Function for Chart: '<S33>/RS' */
static void BSER_enter_internal_RS_FlipFlop(const boolean_T *DataTypeConversion1,
  const boolean_T *LogicalOperator3)
{
  if (*LogicalOperator3) {
    BSER_BattSocCalc_B.Q = false;
  } else {
    if (*DataTypeConversion1) {
      BSER_BattSocCalc_B.Q = true;
    }
  }
}

/* Function for Chart: '<S33>/RS' */
static void BSER_BattSocCalc_RS_FlipFlop(const boolean_T *DataTypeConversion1,
  const boolean_T *LogicalOperator3)
{
  BSER_enter_internal_RS_FlipFlop(DataTypeConversion1, LogicalOperator3);
}

/* Model step function */
void BSER_BattSocCalc_step(void)
{
  boolean_T rtb_LogicalOperator_f;
  real32_T rtb_Product;
  real32_T rtb_Abs1;
  boolean_T rtb_OR7;
  real32_T rtb_Switch;
  real32_T rtb_Switch4;
  boolean_T rtb_Compare_nm;
  boolean_T rtb_bDel;
  real32_T rtb_Product_l;
  uint8_T rtb_DataTypeConversion_h;
  real32_T rtb_Switch3_os;
  real32_T rtb_Product_n;
  real32_T rtb_Product_o;
  real32_T rtb_Switch3;
  boolean_T rtb_LogicalOperator3_b;
  uint8_T rtb_Switch_dw;
  real32_T rtb_Switch_h2;
  real32_T rtb_Switch_fx;
  real32_T rtb_Switch5_a;
  real32_T rtb_Switch1_j3;
  real32_T rtb_Subtract1_f;
  real32_T rtb_Sum;
  enmSocFulHldSts rtb_stSocFulHld;
  int32_T tmp;
  real32_T Sum_tmp;
  BSER_rRSocTableUpLimit_C = look1_iu32lftf_binlcapw(BSPR_TotalOdometer, ((const
    uint32_T *)&(BSER_SOCOdoTableOdo_C[0])), ((const real32_T *)
    &(BSER_SOCOdoTableSOC_C[0])), 9U);
  rtb_Compare_nm = !BSPR_bRSocNvmVldFlg;
  rtb_LogicalOperator_f = (((DSMR_stFaultRank > 2) && DSMR_bFaultRankVldFlg_C) ||
    (rtb_Compare_nm && (!BSPR_bCellVoltVldFlg_C)) || (!BSPR_bCurrVldFlg_C) ||
    (!BSER_bBattCapVldFlg_C));
  if (rtb_LogicalOperator_f) {
    rtb_Product_l = 0.02F;
  } else {
    rtb_Product_l = 0.0F;
  }

  rtb_Product = (rtb_Product_l + fminf(BSER_tiInputFualtFusf_C,
    BSER_BattSocCalc_DW.Delay_DSTATE)) * (real32_T)rtb_LogicalOperator_f;
  rtb_Switch = fabsf(BSPR_iBattCurr);
  if (BSER_BattSocCalc_DW.is_active_c1_BSER_BattSocCalc == 0U) {
    BSER_BattSocCalc_DW.is_active_c1_BSER_BattSocCalc = 1U;
    BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc = BSER_BattSocCalc_IN_pre_INI;
    BSER_BattSocCalc_B.Initial_Enable = true;
    BSER_BattSocCalc_B.bOcvRevHld = false;
  } else {
    switch (BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc) {
     case BSER_BattSocCalc_IN_Fault:
      break;

     case BSER_BattSocCalc_IN_Intial:
      if (((BSER_BattSocCalc_DW.count >= 50.0F) && (BMCR_stBmsModeCtrl > 1) &&
           (BMCR_stBmsModeCtrl < 20)) || ((rtb_Switch > BSER_iRunIniCurrThres_C)
           && (BSER_BattSocCalc_DW.count >= 50.0F))) {
        BSER_BattSocCalc_B.RunINI = false;
        BSER_BattSocCalc_B.bOcvRevHld = false;
        BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc = BSER_BattSocCalc_IN_Run;
        BSER_BattSocCalc_B.Ah_Enable = true;
        BSER_BattSocCalc_B.Initial_Enable = false;
        BSER_BattSocCalc_B.Fault_Enable = false;
        BSER_BattSocCalc_Y.BSER_bSocVldFlg = true;
        BSER_BattSocCalc_DW.counter = 0.0F;
      } else {
        BSER_BattSocCalc_B.Initial_Enable = true;
        BSER_BattSocCalc_DW.count++;
      }
      break;

     case BSER_BattSocCalc_IN_Run:
      if (rtb_Product >= BSER_tiInputFualtFusf_C) {
        BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc = BSER_BattSocCalc_IN_Fault;
        BSER_BattSocCalc_B.Ah_Enable = true;
        BSER_BattSocCalc_B.Initial_Enable = false;
        BSER_BattSocCalc_B.Fault_Enable = false;
        BSER_BattSocCalc_Y.BSER_bSocVldFlg = false;
        BSER_BattSocCalc_DW.counter = 0.0F;
      } else {
        if (rtb_Switch <= BSER_iRunIniCurrThres_C) {
          BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc =
            BSER_BattSocCa_IN_littltcurrent;
          BSER_BattSocCalc_B.RunINI = false;
          BSER_BattSocCalc_DW.counter = 0.0F;
        }
      }
      break;

     case BSER_BattSocCa_IN_littltcurrent:
      if (rtb_Product >= BSER_tiInputFualtFusf_C) {
        BSER_BattSocCalc_B.RunINI = false;
        BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc = BSER_BattSocCalc_IN_Fault;
        BSER_BattSocCalc_B.Ah_Enable = true;
        BSER_BattSocCalc_B.Initial_Enable = false;
        BSER_BattSocCalc_B.Fault_Enable = false;
        BSER_BattSocCalc_Y.BSER_bSocVldFlg = false;
        BSER_BattSocCalc_DW.counter = 0.0F;
      } else if ((real32_T)BSER_BattSocCalc_DW.counter >= BSER_numRunCorrHit_C) {
        BSER_BattSocCalc_B.bOcvRevHld = true;
        BSER_BattSocCalc_B.RunINI = false;
        BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc = BSER_BattSocCalc_IN_Intial;
        BSER_BattSocCalc_B.Initial_Enable = true;
        BSER_BattSocCalc_B.Ah_Enable = false;
        BSER_BattSocCalc_B.Fault_Enable = false;
        BSER_BattSocCalc_Y.BSER_bSocVldFlg = true;
        BSER_BattSocCalc_DW.count = 0.0F;
      } else if (rtb_Switch > BSER_iRunIniCurrThres_C) {
        BSER_BattSocCalc_B.RunINI = false;
        BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc = BSER_BattSocCalc_IN_Run;
        BSER_BattSocCalc_B.Ah_Enable = true;
        BSER_BattSocCalc_B.Initial_Enable = false;
        BSER_BattSocCalc_B.Fault_Enable = false;
        BSER_BattSocCalc_Y.BSER_bSocVldFlg = true;
        BSER_BattSocCalc_DW.counter = 0.0F;
      } else {
        BSER_BattSocCalc_DW.counter++;
        BSER_BattSocCalc_B.Ah_Enable = true;
        BSER_BattSocCalc_B.Initial_Enable = false;
      }
      break;

     default:
      BSER_BattSocCalc_DW.is_c1_BSER_BattSocCalc = BSER_BattSocCalc_IN_Intial;
      BSER_BattSocCalc_B.Initial_Enable = true;
      BSER_BattSocCalc_B.Ah_Enable = false;
      BSER_BattSocCalc_B.Fault_Enable = false;
      BSER_BattSocCalc_Y.BSER_bSocVldFlg = true;
      BSER_BattSocCalc_DW.count = 0.0F;
      break;
    }
  }

  if (BSER_BattSocCalc_B.Initial_Enable) {
    rtb_Abs1 = 100.0F * look2_iflf_binlcapw(BSPR_uCellVoltAvrg,
      BSPR_tCellTempAvrg, ((const real32_T *)&(BSER_uSocOCV_A[0])), ((const
      real32_T *)&(BSER_tSocOCVTemp_A[0])), ((const real32_T *)
      &(BSER_rSocOCV_MAP[0])), rtCP_uDLookupTable_maxIndex, 16U);
    rtb_LogicalOperator_f = (COMR_bStopTimeVldFlg && BSPR_bCellVoltVldFlg_C);
    rtb_OR7 = (((rtb_LogicalOperator_f && (COMR_tiStopTime >=
      BSER_tiStoptimeThres_C)) || rtb_Compare_nm || BSER_BattSocCalc_B.RunINI ||
                ((fabsf(BSPR_rNvmSocAvrg - BSPR_rNvmSocMin) >=
                  BSER_rInitRSocDiffThres_C) || (fabsf(BSPR_rNvmSocMax -
      BSPR_rNvmSocAvrg) >= BSER_rInitRSocDiffThres_C) || (fabsf(BSPR_rNvmSocAvrg
      - rtb_Abs1) >= BSER_rInitRSocDiffThres_C) || BSER_bSocResetCmd_C) ||
                BSER_BattSocCalc_B.bOcvRevHld) && ((BSPR_uCellVoltMin >
      BSER_numSocCorLowCellVoltThres_C) && (BSPR_uCellVoltMin <
      BSER_numSocCorHighCellVoltThres_C)));
    if (!rtb_OR7) {
      rtb_Abs1 = BSPR_rNvmSocAvrg;
    }

    if (rtb_Abs1 > 100.0F) {
      BSER_BattSocCalc_B.Saturation3 = 100.0F;
    } else if (rtb_Abs1 < 0.0F) {
      BSER_BattSocCalc_B.Saturation3 = 0.0F;
    } else {
      BSER_BattSocCalc_B.Saturation3 = rtb_Abs1;
    }

    if (BSER_BattSocCalc_B.bOcvRevHld) {
      rtb_Abs1 = BSER_BattSocCalc_DW.Delay8_DSTATE;
    } else {
      rtb_Abs1 = look2_iflf_binlcapw(BSER_BattSocCalc_B.Saturation3,
        BSPR_tCellTempAvrg, ((const real32_T *)&(BSER_rDsplSocRSoc_A[0])), ((
        const real32_T *)&(BSER_tBattDsplSocTemp_A[0])), ((const real32_T *)
        &(BSER_rDsplSocRSoc_MAP[0])), rtCP_uDLookupTable1_maxIndex_f, 17U);
      if (!(BSER_bSocResetCmd_C || (rtb_LogicalOperator_f && (COMR_tiStopTime >=
             BSER_tiDsplSocStoptimeThres_C)) || rtb_Compare_nm ||
            (rtb_LogicalOperator_f && BSER_BattSocCalc_B.RunINI && (fabsf
             (BSER_BattSocCalc_DW.Delay4_DSTATE - rtb_Abs1) >=
             BSER_rInitDSocDiffThres_C)) || (rtb_LogicalOperator_f && (fabsf
             (rtb_Abs1 - BSPR_rDsplNvmSoc) >= BSER_rInitDSocDiffThres_C)))) {
        rtb_Abs1 = BSPR_rDsplNvmSoc;
      }
    }

    if (rtb_Abs1 > 100.0F) {
      BSER_BattSocCalc_B.Saturation = 100.0F;
    } else if (rtb_Abs1 < 0.0F) {
      BSER_BattSocCalc_B.Saturation = 0.0F;
    } else {
      BSER_BattSocCalc_B.Saturation = rtb_Abs1;
    }

    if (rtb_OR7) {
      rtb_Abs1 = 100.0F * look2_iflf_binlcapw(BSPR_uCellVoltMax,
        BSPR_tCellTempAvrg, ((const real32_T *)&(BSER_uSocOCV_A[0])), ((const
        real32_T *)&(BSER_tSocOCVTemp_A[0])), ((const real32_T *)
        &(BSER_rSocOCV_MAP[0])), rtCP_uDLookupTable2_maxIndex, 16U);
    } else {
      rtb_Abs1 = BSPR_rNvmSocMax;
    }

    if (rtb_Abs1 > 100.0F) {
      BSER_BattSocCalc_B.Saturation1 = 100.0F;
    } else if (rtb_Abs1 < 0.0F) {
      BSER_BattSocCalc_B.Saturation1 = 0.0F;
    } else {
      BSER_BattSocCalc_B.Saturation1 = rtb_Abs1;
    }

    if (rtb_OR7) {
      rtb_Abs1 = 100.0F * look2_iflf_binlcapw(BSPR_uCellVoltMin,
        BSPR_tCellTempAvrg, ((const real32_T *)&(BSER_uSocOCV_A[0])), ((const
        real32_T *)&(BSER_tSocOCVTemp_A[0])), ((const real32_T *)
        &(BSER_rSocOCV_MAP[0])), rtCP_uDLookupTable1_maxIndex_c, 16U);
    } else {
      rtb_Abs1 = BSPR_rNvmSocMin;
    }

    if (rtb_Abs1 > 100.0F) {
      BSER_BattSocCalc_B.Saturation2 = 100.0F;
    } else if (rtb_Abs1 < 0.0F) {
      BSER_BattSocCalc_B.Saturation2 = 0.0F;
    } else {
      BSER_BattSocCalc_B.Saturation2 = rtb_Abs1;
    }
  }

  if (rtb_Switch > BESR_iOcvRevThres_C) {
    rtb_Switch = BSPR_iBattCurr;
  } else {
    rtb_Switch = 0.0F;
  }

  rtb_LogicalOperator_f = BSER_BattSocCalc_DW.Delay3_DSTATE;
  if (BSER_BattSocCalc_B.Ah_Enable) {
    if (rtb_LogicalOperator_f) {
      rtb_Product_l = BSER_BattSocCalc_B.Saturation3;
    } else {
      rtb_Product_l = BSER_BattSocCalc_DW.Delay5_DSTATE;
    }

    rtb_Switch3_os = fabsf(BSER_ahBattCap_C);
    if (rtb_Switch3_os > 0.1F) {
      rtb_Abs1 = BSER_ahBattCap_C;
    } else {
      rtb_Abs1 = 1.0F;
    }

    Sum_tmp = (real32_T)rtb_Switch * BSER_facColmB_C * 0.02777777798473835F *
      BSER_tiSocStep_C;
    BSER_BattSocCalc_B.Sum = rtb_Product_l - (real32_T)(Sum_tmp / rtb_Abs1);
    if (rtb_LogicalOperator_f) {
      rtb_Product_l = BSER_BattSocCalc_B.Saturation1;
    } else {
      rtb_Product_l = BSER_BattSocCalc_DW.Delay7_DSTATE;
    }

    if (rtb_Switch3_os > 0.1F) {
      rtb_Abs1 = BSER_ahBattCap_C;
    } else {
      rtb_Abs1 = 1.0F;
    }

    BSER_BattSocCalc_B.Sum_c = rtb_Product_l - (real32_T)(Sum_tmp / rtb_Abs1);
    if (rtb_LogicalOperator_f) {
      rtb_Product_l = BSER_BattSocCalc_B.Saturation2;
    } else {
      rtb_Product_l = BSER_BattSocCalc_DW.Delay6_DSTATE;
    }

    if (rtb_Switch3_os > 0.1F) {
      rtb_Switch3_os = BSER_ahBattCap_C;
    } else {
      rtb_Switch3_os = 1.0F;
    }

    BSER_BattSocCalc_B.Sum_i = rtb_Product_l - (real32_T)(Sum_tmp /
      rtb_Switch3_os);
    if (fabsf(BSER_ahBattCap) > 0.1F) {
      rtb_Switch1_j3 = BSER_ahBattCap;
    } else {
      rtb_Switch1_j3 = 1.0F;
    }

    if (BSER_BattSocCalc_DW.Delay2_DSTATE > BSER_rRSocTableUpLimit_C) {
      BSER_BattSocCalc_B.Switch2 = 100.0F;
    } else {
      if (BSER_BattSocCalc_DW.Delay2_DSTATE <= BSER_rRSocLowLimit_C) {
        rtb_Abs1 = 0.0F;
      } else {
        if (rtb_LogicalOperator_f) {
          rtb_Abs1 = BSER_BattSocCalc_B.Saturation;
        } else {
          rtb_Abs1 = BSER_BattSocCalc_DW.Delay1_DSTATE;
        }

        rtb_Sum = rtb_Abs1 - look2_iflf_binlcapw
          (BSER_BattSocCalc_DW.Delay2_DSTATE, BSPR_tCellTempAvrg, ((const
             real32_T *)&(BSER_rDsplSocRSoc_A[0])), ((const real32_T *)
            &(BSER_tBattDsplSocTemp_A[0])), ((const real32_T *)
            &(BSER_rDsplSocRSoc_MAP[0])), rtCP_uDLookupTable1_maxIndex, 17U);
        if (fabsf(rtb_Sum) > BSER_rDsplSocDiffAllowThres_C) {
          if (rtb_Switch > 0.0F) {
            if (rtb_Sum > 0.0F) {
              if (rtb_Switch < 0.6F) {
                rtb_Switch1_j3 = rtb_Switch / rtb_Switch1_j3 * BSER_tiSocStep_C *
                  0.027777778F * BSER_rChgDsplSocFast_C * 6.0F;
              } else {
                rtb_Switch1_j3 = rtb_Switch / rtb_Switch1_j3 * BSER_tiSocStep_C *
                  0.027777778F * BSER_rChgDsplSocFast_C;
              }
            } else {
              rtb_Switch1_j3 = rtb_Switch / rtb_Switch1_j3 * BSER_tiSocStep_C *
                0.027777778F * BSER_rChgDsplSocSlow_C;
            }
          } else if (rtb_Sum > 0.0F) {
            rtb_Switch1_j3 = rtb_Switch / rtb_Switch1_j3 * BSER_tiSocStep_C *
              0.027777778F * BSER_rDisChDsplSocSlow_C;
          } else {
            rtb_Switch1_j3 = rtb_Switch / rtb_Switch1_j3 * BSER_tiSocStep_C *
              0.027777778F * BSER_rDisChDsplSocFast_C;
          }
        } else {
          rtb_Switch1_j3 = rtb_Switch / rtb_Switch1_j3 * BSER_tiSocStep_C *
            0.027777778F * BSER_rDsplSocNrm_C;
        }

        rtb_Abs1 -= rtb_Switch1_j3;
      }

      BSER_BattSocCalc_B.Switch2 = rtb_Abs1;
    }
  }

  if (BSER_BattSocCalc_B.Initial_Enable) {
    rtb_Sum = BSER_BattSocCalc_B.Saturation1;
    rtb_Switch4 = BSER_BattSocCalc_B.Saturation2;
  } else {
    rtb_Sum = BSER_BattSocCalc_B.Sum_c;
    rtb_Switch4 = BSER_BattSocCalc_B.Sum_i;
  }

  if (BSER_BattSocCalc_B.Fault_Enable) {
    rtb_Switch1_j3 = BSER_BattSocCalc_DW.Delay1_DSTATE_e;
  } else if (BSER_BattSocCalc_B.Initial_Enable) {
    rtb_Switch1_j3 = BSER_BattSocCalc_B.Saturation;
  } else {
    rtb_Switch1_j3 = BSER_BattSocCalc_B.Switch2;
  }

  if (BSER_BattSocCalc_B.Initial_Enable) {
    rtb_Switch3 = BSER_BattSocCalc_B.Saturation3;
  } else {
    rtb_Switch3 = BSER_BattSocCalc_B.Sum;
  }

  if (rtb_Switch4 < BSER_PackSocCorMinSOC_C) {
    rtb_Abs1 = rtb_Switch4;
  } else if (rtb_Sum > BSER_PackSocCorMaxSOC_C) {
    rtb_Abs1 = rtb_Sum;
  } else {
    rtb_Abs1 = (BSER_rRSocTableUpLimit_C - BSER_rRSocLowLimit_C) - (rtb_Sum -
      rtb_Switch4);
    if (!(fabsf(rtb_Abs1) > 0.1F)) {
      rtb_Abs1 = 1.0F;
    }

    rtb_Abs1 = (BSER_rRSocTableUpLimit_C * rtb_Switch4 - BSER_rRSocLowLimit_C *
                rtb_Sum) / rtb_Abs1;
    rtb_Abs1 = fmaxf(rtb_Abs1, rtb_Switch4);
    rtb_Abs1 = fminf(rtb_Abs1, rtb_Sum);
  }

  BSER_BattSocCalc_DW.Delay1_DSTATE_e = rtb_Switch1_j3;
  rtb_LogicalOperator_f = ((BMCR_stBmsModeCtrl == 5) || (BMCR_stBmsModeCtrl == 6));
  rtb_OR7 = (BSER_BattSocCalc_DW.Delay1_DSTATE_d && (!rtb_LogicalOperator_f));
  BSER_BattSocCalc_DW.Delay1_DSTATE_d = rtb_LogicalOperator_f;
  if (BSER_BattSocCalc_DW.is_active_c4_BSER_BattSocCalc == 0U) {
    BSER_BattSocCalc_DW.is_active_c4_BSER_BattSocCalc = 1U;
    BSER_BattSocCalc_DW.is_c4_BSER_BattSocCalc = BSER_BattSocCalc_IN_Del_Default;
    rtb_bDel = false;
    BSER_BattSocCalc_DW.cnt = 0.0F;
  } else if (BSER_BattSocCalc_DW.is_c4_BSER_BattSocCalc ==
             BSER_BattSocCalc_IN_Del_Course) {
    rtb_bDel = true;
    if (BSER_BattSocCalc_DW.cnt > BSER_cntSocBmsStsDel_C) {
      BSER_BattSocCalc_DW.is_c4_BSER_BattSocCalc =
        BSER_BattSocCalc_IN_Del_Default;
      rtb_bDel = false;
      BSER_BattSocCalc_DW.cnt = 0.0F;
    } else {
      BSER_BattSocCalc_DW.cnt++;
    }
  } else {
    rtb_bDel = false;
    if (rtb_OR7) {
      BSER_BattSocCalc_DW.is_c4_BSER_BattSocCalc =
        BSER_BattSocCalc_IN_Del_Course;
      rtb_bDel = true;
      BSER_BattSocCalc_DW.cnt++;
    }
  }

  rtb_LogicalOperator_f = ((rtb_bDel || rtb_LogicalOperator_f) && (BCMR_bChrgFul
    && BCMR_bChrgFulVldFlg_C));
  if (rtb_LogicalOperator_f) {
    rtb_Product_l = 0.02F;
  } else {
    rtb_Product_l = 0.0F;
  }

  rtb_Product_l = (rtb_Product_l + fminf(BSER_tiFullChgtFusf_C,
    BSER_BattSocCalc_DW.Delay_DSTATE_o)) * (real32_T)rtb_LogicalOperator_f;
  rtb_bDel = (rtb_Product_l >= BSER_tiFullChgtFusf_C);
  rtb_LogicalOperator3_b = !BSER_BattSocCalc_DW.Delay1_DSTATE_l;
  BSER_BattSocCalc_DW.Delay1_DSTATE_l = rtb_bDel;
  rtb_LogicalOperator_f = (BSER_BattSocCalc_DW.Delay_DSTATE_e ||
    BSER_BattSocCalc_DW.Delay2_DSTATE_a);
  if (BSER_BattSocCalc_DW.Delay1_DSTATE_g) {
    rtb_Switch_dw = BSER_BattSocCalc_DW.Delay_DSTATE_a;
  } else {
    rtb_Switch_dw = BSER_stSocFulHld;
  }

  if (BSER_BattSocCalc_DW.is_active_c5_BSER_BattSocCalc == 0U) {
    BSER_BattSocCalc_DW.is_active_c5_BSER_BattSocCalc = 1U;
    BSER_BattSocCalc_DW.is_c5_BSER_BattSocCalc = BSER_BattSocCa_IN_SocFulDefault;
    rtb_stSocFulHld = enmSocFulHldSts_Default;
    rtb_OR7 = false;
  } else if (BSER_BattSocCalc_DW.is_c5_BSER_BattSocCalc ==
             BSER_BattSocCa_IN_SocFulDefault) {
    rtb_stSocFulHld = enmSocFulHldSts_Default;
    rtb_OR7 = false;
    if (rtb_LogicalOperator_f) {
      rtb_DataTypeConversion_h = BSER_BattSocCalc_ConstB.DataTypeConversion1;
    } else {
      rtb_DataTypeConversion_h = rtb_Switch_dw;
    }

    if ((BSPR_bRSocNvmVldFlg && (rtb_DataTypeConversion_h != 0)) ||
        (rtb_LogicalOperator3_b && rtb_bDel)) {
      BSER_BattSocCalc_DW.is_c5_BSER_BattSocCalc = BSER_BattSocCalc_IN_SocFulSet;
      rtb_stSocFulHld = enmSocFulHldSts_FulHld;
      rtb_OR7 = true;
    }
  } else {
    rtb_stSocFulHld = enmSocFulHldSts_FulHld;
    rtb_OR7 = true;
    if (BSER_BattSocCalc_DW.Delay1_DSTATE_n > BSER_cSocAhFulHldThres_C) {
      BSER_BattSocCalc_DW.is_c5_BSER_BattSocCalc =
        BSER_BattSocCa_IN_SocFulDefault;
      rtb_stSocFulHld = enmSocFulHldSts_Default;
      rtb_OR7 = false;
    }
  }

  if (BSER_BattSocCalc_DW.is_active_c6_BSER_BattSocCalc == 0U) {
    BSER_BattSocCalc_DW.is_active_c6_BSER_BattSocCalc = 1U;
    BSER_BattSocCalc_DW.is_c6_BSER_BattSocCalc = BSER_BattSocCalc_IN_Default_Stt;
    BSER_BattSocCalc_DW.i = 0U;
    rtb_bDel = false;
  } else if (BSER_BattSocCalc_DW.is_c6_BSER_BattSocCalc ==
             BSER_BattSocCa_IN_AddCourse_Stt) {
    rtb_bDel = true;
    if (BSER_BattSocCalc_DW.i > BSER_cntSocStbNum_C) {
      BSER_BattSocCalc_DW.is_c6_BSER_BattSocCalc =
        BSER_BattSocCalc_IN_Default_Stt;
      BSER_BattSocCalc_DW.i = 0U;
      rtb_bDel = false;
    } else {
      tmp = BSER_BattSocCalc_DW.i + 1;
      if (tmp > 255) {
        tmp = 255;
      }

      BSER_BattSocCalc_DW.i = (uint8_T)tmp;
    }
  } else {
    rtb_bDel = false;
    if (rtb_stSocFulHld != BSER_BattSocCalc_DW.DelayInput1_DSTATE) {
      BSER_BattSocCalc_DW.is_c6_BSER_BattSocCalc =
        BSER_BattSocCa_IN_AddCourse_Stt;
      rtb_bDel = true;
    }
  }

  if (rtb_bDel) {
    rtb_DataTypeConversion_h = BSER_BattSocCalc_DW.Delay1_DSTATE_ia;
  } else {
    rtb_DataTypeConversion_h = rtb_stSocFulHld;
  }

  rtb_bDel = (BSER_BattSocCalc_DW.Delay1_DSTATE_eo &&
              (!(rtb_DataTypeConversion_h != 0)));
  BSER_BattSocCalc_DW.Delay1_DSTATE_eo = (rtb_DataTypeConversion_h != 0);
  rtb_bDel = (rtb_bDel || BSER_BattSocCalc_DW.Delay2_DSTATE_c);
  if (BSER_BattSocCalc_DW.Delay1_DSTATE_j) {
    rtb_Switch_h2 = BSER_BattSocCalc_DW.Delay_DSTATE_c;
  } else {
    rtb_Switch_h2 = BSER_cSocDchAhFulRev;
  }

  if (rtb_DataTypeConversion_h != 0) {
    if (fabsf(rtb_Switch) > BSER_iSocCurrEff_C) {
      rtb_Switch_fx = rtb_Switch * BSER_tiSocRunT_C / 3.6E+6F;
    } else {
      rtb_Switch_fx = 0.0F;
    }
  } else {
    rtb_Switch_fx = 0.0F;
  }

  if ((rtb_DataTypeConversion_h != 0) != (int32_T)
      BSER_BattSocCalc_DW.DelayInput1_DSTATE_n) {
    rtb_Switch3_os = 0.0F;
  } else {
    rtb_Switch3_os = BSER_BattSocCalc_DW.Delay_DSTATE_i;
  }

  rtb_Switch_fx += rtb_Switch3_os;
  if (rtb_DataTypeConversion_h != 0) {
    if (rtb_Compare_nm || rtb_bDel) {
      rtb_Switch3_os = 0.0F;
    } else {
      rtb_Switch3_os = rtb_Switch_h2;
    }

    rtb_Switch3_os += rtb_Switch_fx;
  } else {
    rtb_Switch3_os = 0.0F;
  }

  BSER_cSocDchAhFulRev = rtb_Switch3_os;
  BSER_stSocFulHld = rtb_DataTypeConversion_h;
  rtb_Compare_nm = (rtb_DataTypeConversion_h != 0);
  rtb_LogicalOperator3_b = ((!(rtb_DataTypeConversion_h != 0)) &&
    (BSER_BattSocCalc_DW.Delay1_DSTATE_i > 99.99F));
  if (BSER_BattSocCalc_DW.is_active_c2_BSER_BattSocCalc == 0U) {
    BSER_BattSocCalc_DW.is_active_c2_BSER_BattSocCalc = 1U;
    BSER_enter_internal_RS_FlipFlop(&rtb_Compare_nm, &rtb_LogicalOperator3_b);
  } else {
    BSER_BattSocCalc_RS_FlipFlop(&rtb_Compare_nm, &rtb_LogicalOperator3_b);
  }

  if (BSER_BattSocCalc_B.Q) {
    rtb_Switch1_j3 = 100.0F;
  } else {
    if ((!BSER_BattSocCalc_B.Initial_Enable) && (rtb_Switch < 0.0F) &&
        (!BSER_BattSocCalc_B.Q)) {
      rtb_Switch1_j3 = fminf(rtb_Switch1_j3, 99.4F);
    }
  }

  if (GbBSW_DispSOCOverWrite_flg) {
    rtb_Switch1_j3 = GcBSW_DispSOCOverWrite_pct;
    rtb_Switch5_a = GcBSW_DispSOCOverWrite_pct;
  } else {
    if (!(BSER_BattSocCalc_B.Initial_Enable || BSER_BattSocCalc_B.Q)) {
      if (fabsf(rtb_Switch) <= BSER_iSocHoldCurrThres_C) {
        rtb_Switch1_j3 = BSER_BattSocCalc_DW.Delay_DSTATE_g;
      } else if ((rtb_Switch > 0.0F) || ((BMCR_stBmsModeCtrl == 4) &&
                  (COMR_YearSet != 2080))) {
        rtb_Switch1_j3 = fminf(BSER_BattSocCalc_DW.Delay_DSTATE_g,
          rtb_Switch1_j3);
      } else {
        rtb_Switch1_j3 = fmaxf(BSER_BattSocCalc_DW.Delay_DSTATE_g,
          rtb_Switch1_j3);
      }
    }

    if (BSER_BattSocCalc_B.Initial_Enable) {
      rtb_Switch5_a = rtb_Switch1_j3;
    } else {
      rtb_Subtract1_f = rtb_Switch1_j3 - BSER_BattSocCalc_DW.Delay_DSTATE_j;
      if (fabsf(rtb_Subtract1_f) <= BSER_rDsplSocFilter_C) {
        rtb_Subtract1_f = rtb_Switch1_j3;
      } else {
        if (rtb_Subtract1_f < 0.0F) {
          rtb_Subtract1_f = -1.0F;
        } else {
          if (rtb_Subtract1_f > 0.0F) {
            rtb_Subtract1_f = 1.0F;
          }
        }

        rtb_Subtract1_f = rtb_Subtract1_f * BSER_rDsplSocFilter_C +
          BSER_BattSocCalc_DW.Delay_DSTATE_j;
      }

      if (rtb_Subtract1_f > 100.0F) {
        rtb_Switch5_a = 100.0F;
      } else if (rtb_Subtract1_f < 0.0F) {
        rtb_Switch5_a = 0.0F;
      } else {
        rtb_Switch5_a = rtb_Subtract1_f;
      }
    }
  }

  BSER_LowSocCorLowCellVoltThres2_C = look1_iflf_binlcapw(BSPR_tCellTempAvrg, ((
    const real32_T *)&(BSER_uMinCellVoltTemp[0])), ((const real32_T *)
    &(BSER_uSOCZeorVolt_MAP[0])), 9U);
  rtb_Compare_nm = ((fabsf(rtb_Switch) <= BSER_LowSocCorCurrThres1_C) &&
                    (BSER_LowSocCorLowCellVoltThres1_C <= BSPR_uCellVoltMin) &&
                    (BSPR_uCellVoltMin <= BSER_LowSocCorLowCellVoltThres2_C));
  if (rtb_Compare_nm) {
    rtb_Switch = 0.02F;
  } else {
    rtb_Switch = 0.0F;
  }

  rtb_Product_n = (rtb_Switch + fminf(BSER_tiLowSOCCor1_C,
    BSER_BattSocCalc_DW.Delay_DSTATE_b)) * (real32_T)rtb_Compare_nm;
  BSER_LowSocCorLowCellVoltThres3_C = look1_iflf_binlcapw(BSPR_tCellTempAvrg, ((
    const real32_T *)&(BSER_uMinCellVoltTemp[0])), ((const real32_T *)
    &(BSER_uSOCOneVolt_MAP[0])), 9U);
  rtb_Compare_nm = (BSPR_uCellVoltMin > BSER_LowSocCorLowCellVoltThres3_C);
  if (rtb_Compare_nm) {
    rtb_Switch = 0.02F;
  } else {
    rtb_Switch = 0.0F;
  }

  rtb_Product_o = (rtb_Switch + fminf(BSER_tiLowSOCCor1_C,
    BSER_BattSocCalc_DW.Delay_DSTATE_od)) * (real32_T)rtb_Compare_nm;
  if (rtb_Product_n >= BSER_tiLowSOCCor1_C) {
    BSER_DispSOCZero_flg = true;
  } else {
    BSER_DispSOCZero_flg = ((!(rtb_Product_o >= BSER_tiLowSOCCor1_C)) &&
      BSER_DispSOCZero_flg);
  }

  if (BSER_DispSOCZero_flg) {
    rtb_Switch = 0.0F;
  } else {
    rtb_Switch = fmaxf(rtb_Switch5_a, BSER_SocCorMinValue_C);
  }

  rtb_Compare_nm = (BCMR_bChrgFul && (!BSER_BattSocCalc_DW.UnitDelay_DSTATE_f));
  BSER_BattSocCalc_DW.UnitDelay_DSTATE_f = BCMR_bChrgFul;
  if (GbBSW_RealSOCOverWrite_flg) {
    rtb_Switch3 = GcBSW_RealSOCOverWrite_pct;
    rtb_Subtract1_f = GcBSW_RealSOCOverWrite_pct;
    rtb_Sum = GcBSW_RealSOCOverWrite_pct;
  } else if ((BSPR_tCellTempAvrg > BSER_RealSocZeroTemp_C) &&
             BSER_DispSOCZero_flg) {
    rtb_Switch3 = (rtb_Switch3 - rtb_Switch4) + BSER_LowSocCorMinSOC1_C;
    rtb_Subtract1_f = BSER_LowSocCorMinSOC1_C;
    rtb_Sum = (rtb_Sum - rtb_Switch4) + BSER_LowSocCorMinSOC1_C;
  } else if (rtb_Compare_nm) {
    rtb_Switch3 = BSER_rRSocTableUpLimit_C - (rtb_Sum - rtb_Switch3);
    rtb_Subtract1_f = BSER_rRSocTableUpLimit_C - (rtb_Sum - rtb_Switch4);
    rtb_Sum = BSER_rRSocTableUpLimit_C;
  } else {
    rtb_Switch3 = fmaxf(rtb_Switch3, BSER_SocCorMinValue_C);
    rtb_Subtract1_f = fmaxf(rtb_Switch4, BSER_SocCorMinValue_C);
    rtb_Sum = fmaxf(rtb_Sum, BSER_SocCorMinValue_C);
  }

  if (BSER_BattSocCalc_DW.Delay2_DSTATE_l || BSER_BattSocCalc_B.Initial_Enable)
  {
    BSER_BattSocCalc_B.In1 = rtb_Switch5_a;
  }

  rtb_Compare_nm = (fabsf(BSER_BattSocCalc_B.In1 - rtb_Switch5_a) >=
                    BESR_numSocSaveStep_C);
  if (BSER_BattSocCalc_DW.is_active_c3_BSER_BattSocCalc == 0U) {
    BSER_BattSocCalc_DW.is_active_c3_BSER_BattSocCalc = 1U;
    BSER_BattSocCalc_DW.is_c3_BSER_BattSocCalc = BSER_BattSocCalc_IN_int;
    BSER_BattSocCalc_DW.conter = 1.0F;
    BSER_BattSocCalc_B.flag = false;
  } else if (BSER_BattSocCalc_DW.is_c3_BSER_BattSocCalc ==
             BSER_BattSocCalc_IN_control) {
    if (BSER_BattSocCalc_DW.conter >= BESR_numSocSaveFlgStep_C) {
      BSER_BattSocCalc_DW.is_c3_BSER_BattSocCalc = BSER_BattSocCalc_IN_int;
      BSER_BattSocCalc_DW.conter = 1.0F;
      BSER_BattSocCalc_B.flag = false;
    } else {
      BSER_BattSocCalc_DW.conter++;
      BSER_BattSocCalc_B.flag = true;
    }
  } else {
    if (rtb_Compare_nm) {
      BSER_BattSocCalc_DW.is_c3_BSER_BattSocCalc = BSER_BattSocCalc_IN_control;
    }
  }

  rtb_LogicalOperator3_b = !BSER_BattSocCalc_DW.UnitDelay_DSTATE;
  BSER_BattSocCalc_DW.UnitDelay_DSTATE = BCMR_bChrgFul;
  BSER_flgSocSave = (BSER_BattSocCalc_B.flag || (BCMR_bChrgFul &&
    rtb_LogicalOperator3_b));
  BSER_BattSocCalc_DW.Delay1_DSTATE_ia = rtb_DataTypeConversion_h;
  BSER_BattSocCalc_DW.Delay1_DSTATE_n = rtb_Switch3_os;
  BSER_BattSocCalc_DW.Delay_DSTATE_o = rtb_Product_l;
  BSER_BattSocCalc_DW.Delay_DSTATE_e = rtb_OR7;
  BSER_BattSocCalc_DW.Delay2_DSTATE_a = rtb_LogicalOperator_f;
  BSER_BattSocCalc_DW.Delay_DSTATE_a = rtb_Switch_dw;
  BSER_BattSocCalc_DW.Delay1_DSTATE_g = true;
  BSER_BattSocCalc_DW.DelayInput1_DSTATE = rtb_stSocFulHld;
  BSER_BattSocCalc_DW.Delay2_DSTATE_c = rtb_bDel;
  BSER_BattSocCalc_DW.Delay_DSTATE_c = rtb_Switch_h2;
  BSER_BattSocCalc_DW.Delay1_DSTATE_j = true;
  BSER_BattSocCalc_DW.DelayInput1_DSTATE_n = (rtb_DataTypeConversion_h != 0);
  BSER_BattSocCalc_DW.Delay_DSTATE_i = rtb_Switch_fx;
  BSER_BattSocCalc_DW.Delay1_DSTATE_i = rtb_Switch5_a;
  BSER_BattSocCalc_DW.Delay_DSTATE_g = rtb_Switch1_j3;
  BSER_BattSocCalc_DW.Delay_DSTATE_j = rtb_Switch5_a;
  BSER_BattSocCalc_DW.Delay_DSTATE_b = rtb_Product_n;
  BSER_BattSocCalc_DW.Delay_DSTATE_od = rtb_Product_o;
  BSER_BattSocCalc_DW.Delay2_DSTATE_l = rtb_Compare_nm;
  if (BSER_rRSocTableUpLimit_C < rtb_Abs1) {
    rtb_Switch1_j3 = BSER_rRSocTableUpLimit_C;
  } else {
    rtb_Switch1_j3 = rtb_Abs1;
  }

  if (rtb_Switch1_j3 > 0.0F) {
    BSER_rSoc = rtb_Switch1_j3;
  } else {
    BSER_rSoc = 0.0F;
  }

  if (BSER_rRSocTableUpLimit_C < rtb_Sum) {
    rtb_Switch1_j3 = BSER_rRSocTableUpLimit_C;
  } else {
    rtb_Switch1_j3 = rtb_Sum;
  }

  if (rtb_Switch1_j3 > 0.0F) {
    BSER_rSocMax = rtb_Switch1_j3;
  } else {
    BSER_rSocMax = 0.0F;
  }

  if (BSER_rRSocTableUpLimit_C < rtb_Subtract1_f) {
    rtb_Switch1_j3 = BSER_rRSocTableUpLimit_C;
  } else {
    rtb_Switch1_j3 = rtb_Subtract1_f;
  }

  if (rtb_Switch1_j3 > 0.0F) {
    BSER_rSocMin = rtb_Switch1_j3;
  } else {
    BSER_rSocMin = 0.0F;
  }

  if (BSER_rRSocTableUpLimit_C < rtb_Switch3) {
    rtb_Switch1_j3 = BSER_rRSocTableUpLimit_C;
  } else {
    rtb_Switch1_j3 = rtb_Switch3;
  }

  if (rtb_Switch1_j3 > 0.0F) {
    BSER_rSocAvrg = rtb_Switch1_j3;
  } else {
    BSER_rSocAvrg = 0.0F;
  }

  if (rtb_Switch >= 99.5F) {
    rtb_Switch1_j3 = 100.0F;
  } else {
    rtb_Switch1_j3 = rtb_Switch;
  }

  if (rtb_Switch1_j3 < 0.0F) {
    BSER_rSocDspl = 0.0F;
  } else {
    BSER_rSocDspl = rtb_Switch1_j3;
  }

  BSER_BattSocCalc_DW.Delay_DSTATE = rtb_Product;
  BSER_BattSocCalc_DW.Delay4_DSTATE = rtb_Switch;
  BSER_BattSocCalc_DW.Delay8_DSTATE = rtb_Switch;
  BSER_BattSocCalc_DW.Delay1_DSTATE = rtb_Switch;
  BSER_BattSocCalc_DW.Delay2_DSTATE = rtb_Abs1;
  BSER_BattSocCalc_DW.Delay3_DSTATE = BSER_BattSocCalc_B.Initial_Enable;
  BSER_BattSocCalc_DW.Delay5_DSTATE = rtb_Switch3;
  BSER_BattSocCalc_DW.Delay6_DSTATE = rtb_Subtract1_f;
  BSER_BattSocCalc_DW.Delay7_DSTATE = rtb_Sum;
}

/* Model initialize function */
void BSER_BattSocCalc_initialize(void)
{
  /* Registration code */

  /* block I/O */

  /* custom signals */
  BSER_rSocDspl = 0.0F;
  BSER_rSocAvrg = 0.0F;
  BSER_rSocMin = 0.0F;
  BSER_rSocMax = 0.0F;
  BSER_rSoc = 0.0F;
  BSER_flgSocSave = false;

  /* external outputs */
  (void) memset((void *)&BSER_BattSocCalc_Y, 0,
                sizeof(ExtY_BSER_BattSocCalc_T));
  BSER_BattSocCalc_DW.Delay1_DSTATE_l = true;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
