/*
 * File: BSER_BattSopCalc.c
 *
 * Code generated for Simulink model 'BSER_BattSopCalc'.
 *
 * Model version                  : A100.2_0
 * Simulink Coder version         : 8.13 (R2017b) 24-Jul-2017
 * C/C++ source code generated on : Wed Aug 25 15:04:45 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_BattSopCalc.h"
#include "BSER_BattSopCalc_private.h"
#include "look2_iflf_binlcapw.h"
#include "look2_iflf_binlxpw.h"

/* Named constants for Chart: '<S1>/SopStatus' */
#define BSER_BattSopCalc_IN_Fault2     ((uint8_T)1U)
#define BSER_BattSopCalc_IN_Fault3     ((uint8_T)2U)
#define BSER_BattSopCalc_IN_Fault4     ((uint8_T)3U)
#define BSER_BattSopCalc_IN_HVon       ((uint8_T)4U)
#define BSER_BattSopCalc_IN_Ini        ((uint8_T)5U)

/* Exported data definition */
#define ASW1_OEM_START_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: OutputSignal */
volatile boolean_T BSER_bDisFloorFlg;
volatile real32_T BSER_iConstCurrChMax;
volatile real32_T BSER_iConstCurrDisChMax;
volatile real32_T BSER_iPeakCurrCh;
volatile real32_T BSER_iPeakCurrDisCh;
volatile real32_T BSER_pConstPwrCh;
volatile real32_T BSER_pConstPwrDisCh;
volatile real32_T BSER_pPeakPwrCh;
volatile real32_T BSER_pPeakPwrDisCh;
volatile real32_T BSER_rLSOCMax;
volatile real32_T BSER_rLSOCMin;
volatile real32_T BSER_uPeakVoltCh;
volatile real32_T BSER_uPeakVoltDisCh;

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block signals (auto storage) */
B_BSER_BattSopCalc_T BSER_BattSopCalc_B;

/* Block states (auto storage) */
DW_BSER_BattSopCalc_T BSER_BattSopCalc_DW;

/*
 * Output and update for atomic system:
 *    '<S14>/CurrentLimit_LUT1'
 *    '<S14>/CurrentLimit_LUT2'
 *    '<S14>/CurrentLimit_LUT3'
 *    '<S14>/CurrentLimit_LUT4'
 *    '<S14>/CurrentLimit_LUT5'
 *    '<S14>/CurrentLimit_LUT6'
 *    '<S14>/CurrentLimit_LUT7'
 *    '<S14>/CurrentLimit_LUT8'
 */
void BSER_BattSopC_CurrentLimit_LUT1(real32_T rtu_T, real32_T rtu_SOC,
  B_CurrentLimit_LUT1_BSER_Batt_T *localB)
{
  localB->SOP_I_10s = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tChrgCurrMapTemp_A[0])), ((const real32_T *)
    &(BSER_rShrtChrgCurrMapSoc_A[0])), ((const real32_T *)
    &(BSER_iShrtChgCurrMap_MAP[0])), rtCP_SOP_I_10s_maxIndex, 20U);
  localB->SOP_I_Conti = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tChrgCurrMapTemp_A[0])), ((const real32_T *)
    &(BSER_rLonChrgCurrMapSoc_A[0])), ((const real32_T *)
    &(BSER_iLonChgCurrMap_MAP[0])), rtCP_SOP_I_Conti_maxIndex, 20U);
}

/*
 * Output and update for atomic system:
 *    '<S15>/CurrentLimit_LUT1'
 *    '<S15>/CurrentLimit_LUT2'
 *    '<S15>/CurrentLimit_LUT3'
 *    '<S15>/CurrentLimit_LUT4'
 *    '<S15>/CurrentLimit_LUT5'
 *    '<S15>/CurrentLimit_LUT6'
 *    '<S15>/CurrentLimit_LUT7'
 *    '<S15>/CurrentLimit_LUT8'
 */
void BSER_BattSo_CurrentLimit_LUT1_m(real32_T rtu_T, real32_T rtu_SOC,
  B_CurrentLimit_LUT1_BSER_Ba_o_T *localB)
{
  localB->SOP_I_10s = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tDisChrgCurrMapTemp_A[0])), ((const real32_T *)
    &(BSER_rShrtCurrMapSoc_A[0])), ((const real32_T *)
    &(BSER_iShrtDischCurrMap_MAP[0])), rtCP_SOP_I_10s_maxIndex_k, 20U);
  localB->SOP_I_Conti = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tDisChrgCurrMapTemp_A[0])), ((const real32_T *)
    &(BSER_rLonCurrMapSoc_A[0])), ((const real32_T *)
    &(BSER_iLonDischCurrMap_MAP[0])), rtCP_SOP_I_Conti_maxIndex_k, 20U);
}

/*
 * Output and update for atomic system:
 *    '<S60>/PowerLimit_LUT1'
 *    '<S60>/PowerLimit_LUT2'
 *    '<S60>/PowerLimit_LUT3'
 *    '<S60>/PowerLimit_LUT4'
 *    '<S60>/PowerLimit_LUT5'
 *    '<S60>/PowerLimit_LUT6'
 *    '<S60>/PowerLimit_LUT7'
 *    '<S60>/PowerLimit_LUT8'
 */
void BSER_BattSopCal_PowerLimit_LUT1(real32_T rtu_T, real32_T rtu_SOC,
  B_PowerLimit_LUT1_BSER_BattSo_T *localB)
{
  localB->SOP_P_10s = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tChrgPwrMapTemp_A[0])), ((const real32_T *)
    &(BSER_rShrtChrgPwrMapSoc_A[0])), ((const real32_T *)
    &(BSER_pShrtChgPwrMap_MAP[0])), rtCP_SOP_P_10s_maxIndex, 20U);
  localB->SOP_P_Conti = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tChrgPwrMapTemp_A[0])), ((const real32_T *)
    &(BSER_rLonChrgPwrMapSoc_A[0])), ((const real32_T *)
    &(BSER_pLonChgPwrMap_MAP[0])), rtCP_SOP_P_Conti_maxIndex, 20U);
}

/*
 * Output and update for atomic system:
 *    '<S61>/PowerLimit_LUT1'
 *    '<S61>/PowerLimit_LUT2'
 *    '<S61>/PowerLimit_LUT3'
 *    '<S61>/PowerLimit_LUT4'
 *    '<S61>/PowerLimit_LUT5'
 *    '<S61>/PowerLimit_LUT6'
 *    '<S61>/PowerLimit_LUT7'
 *    '<S61>/PowerLimit_LUT8'
 */
void BSER_BattSopC_PowerLimit_LUT1_c(real32_T rtu_T, real32_T rtu_SOC,
  B_PowerLimit_LUT1_BSER_Batt_d_T *localB)
{
  localB->SOP_P_10s = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tDisChrgPwrMapTemp_A[0])), ((const real32_T *)
    &(BSER_rShrtPwrMapSoc_A[0])), ((const real32_T *)
    &(BSER_pShrtDischPwrMap_MAP[0])), rtCP_SOP_P_10s_maxIndex_p3, 20U);
  localB->SOP_P_Conti = look2_iflf_binlcapw(rtu_T, rtu_SOC, ((const real32_T *)
    &(BSER_tDisChrgPwrMapTemp_A[0])), ((const real32_T *)&(BSER_rLonPwrMapSoc_A
    [0])), ((const real32_T *)&(BSER_pLonDischPwrMap_MAP[0])),
    rtCP_SOP_P_Conti_maxIndex_bi, 20U);
}

/* Model step function */
void BSER_BattSopCalc_step(void)
{
  real32_T rtb_BSER_Soh;
  real32_T rtb_Delay_i;
  boolean_T rtb_RelationalOperator1_c;
  boolean_T rtb_RelationalOperator;
  boolean_T rtb_RelationalOperator_j;
  boolean_T rtb_RelationalOperator_f1;
  real32_T rtb_Gain1_f;
  real32_T rtb_Saturation;
  real32_T rtb_Saturation1;
  boolean_T rtb_RelationalOperator_a;
  real32_T rtb_Saturation4;
  real32_T rtb_Saturation5;
  real32_T rtb_Gain1;
  real32_T rtb_Saturation3;
  real32_T rtb_Switch1_g;
  real32_T rtb_Product2_d;
  real32_T rtb_Product2_l;
  int32_T rtb_Switch_o;
  real32_T rtb_Switch2_l;
  real32_T rtb_uDLookupTable1;
  real32_T rtb_uDLookupTable2;
  real32_T rtb_uDLookupTable3;
  real32_T rtb_uDLookupTable4;
  if (BSER_BattSopCalc_DW.is_active_c3_BSER_BattSopCalc == 0U) {
    BSER_BattSopCalc_DW.is_active_c3_BSER_BattSopCalc = 1U;
    BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_Ini;
    BSER_BattSopCalc_DW.counter = 0.0F;
    BSER_BattSopCalc_B.Ini_enable = true;
    BSER_BattSopCalc_B.Fault2_enable = false;
    BSER_BattSopCalc_B.Fault3_enable = false;
    BSER_BattSopCalc_B.Fault4_enable = false;
  } else {
    switch (BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc) {
     case BSER_BattSopCalc_IN_Fault2:
      if (DSMR_stFaultRank == 4) {
        BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_Fault4;
        BSER_BattSopCalc_B.Ini_enable = false;
        BSER_BattSopCalc_B.Fault2_enable = false;
        BSER_BattSopCalc_B.Fault3_enable = false;
        BSER_BattSopCalc_B.Fault4_enable = true;
      } else if (DSMR_stFaultRank == 3) {
        BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_Fault3;
        BSER_BattSopCalc_B.Ini_enable = false;
        BSER_BattSopCalc_B.Fault2_enable = false;
        BSER_BattSopCalc_B.Fault3_enable = true;
        BSER_BattSopCalc_B.Fault4_enable = false;
      } else {
        if (DSMR_stFaultRank < 2) {
          BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_HVon;
          BSER_BattSopCalc_B.Ini_enable = false;
          BSER_BattSopCalc_B.Fault2_enable = false;
          BSER_BattSopCalc_B.Fault3_enable = false;
          BSER_BattSopCalc_B.Fault4_enable = false;
        }
      }
      break;

     case BSER_BattSopCalc_IN_Fault3:
      if (DSMR_stFaultRank == 4) {
        BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_Fault4;
        BSER_BattSopCalc_B.Ini_enable = false;
        BSER_BattSopCalc_B.Fault2_enable = false;
        BSER_BattSopCalc_B.Fault3_enable = false;
        BSER_BattSopCalc_B.Fault4_enable = true;
      }
      break;

     case BSER_BattSopCalc_IN_Fault4:
      break;

     case BSER_BattSopCalc_IN_HVon:
      if (DSMR_stFaultRank == 4) {
        BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_Fault4;
        BSER_BattSopCalc_B.Ini_enable = false;
        BSER_BattSopCalc_B.Fault2_enable = false;
        BSER_BattSopCalc_B.Fault3_enable = false;
        BSER_BattSopCalc_B.Fault4_enable = true;
      } else if (DSMR_stFaultRank == 3) {
        BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_Fault3;
        BSER_BattSopCalc_B.Ini_enable = false;
        BSER_BattSopCalc_B.Fault2_enable = false;
        BSER_BattSopCalc_B.Fault3_enable = true;
        BSER_BattSopCalc_B.Fault4_enable = false;
      } else {
        if (DSMR_stFaultRank == 2) {
          BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc =
            BSER_BattSopCalc_IN_Fault2;
          BSER_BattSopCalc_B.Ini_enable = false;
          BSER_BattSopCalc_B.Fault2_enable = true;
          BSER_BattSopCalc_B.Fault3_enable = false;
          BSER_BattSopCalc_B.Fault4_enable = false;
        }
      }
      break;

     default:
      if (BSER_BattSopCalc_DW.counter >= 1.0F) {
        BSER_BattSopCalc_DW.is_c3_BSER_BattSopCalc = BSER_BattSopCalc_IN_HVon;
        BSER_BattSopCalc_B.Ini_enable = false;
        BSER_BattSopCalc_B.Fault2_enable = false;
        BSER_BattSopCalc_B.Fault3_enable = false;
        BSER_BattSopCalc_B.Fault4_enable = false;
      } else {
        BSER_BattSopCalc_DW.counter++;
      }
      break;
    }
  }

  rtb_BSER_Soh = 0.01F * BSER_rSoh;
  rtb_uDLookupTable1 = look2_iflf_binlcapw(BSER_rSocMin, BSPR_tCellTempMin, ((
    const real32_T *)&(BSER_rLSocRSoc_A[0])), ((const real32_T *)
    &(BSER_tBattLSocTemp_A[0])), ((const real32_T *)&(BSER_rLSocRSoc_MAP[0])),
    rtCP_uDLookupTable1_maxIndex, 17U);
  rtb_uDLookupTable2 = look2_iflf_binlcapw(BSER_rSocMin, BSPR_tCellTempMax, ((
    const real32_T *)&(BSER_rLSocRSoc_A[0])), ((const real32_T *)
    &(BSER_tBattLSocTemp_A[0])), ((const real32_T *)&(BSER_rLSocRSoc_MAP[0])),
    rtCP_uDLookupTable2_maxIndex, 17U);
  rtb_uDLookupTable3 = look2_iflf_binlcapw(BSER_rSocMax, BSPR_tCellTempMin, ((
    const real32_T *)&(BSER_rLSocRSoc_A[0])), ((const real32_T *)
    &(BSER_tBattLSocTemp_A[0])), ((const real32_T *)&(BSER_rLSocRSoc_MAP[0])),
    rtCP_uDLookupTable3_maxIndex, 17U);
  rtb_uDLookupTable4 = look2_iflf_binlcapw(BSER_rSocMax, BSPR_tCellTempMax, ((
    const real32_T *)&(BSER_rLSocRSoc_A[0])), ((const real32_T *)
    &(BSER_tBattLSocTemp_A[0])), ((const real32_T *)&(BSER_rLSocRSoc_MAP[0])),
    rtCP_uDLookupTable4_maxIndex, 17U);
  if (BSER_bLSocCrrCls_C) {
    rtb_Delay_i = BSER_rSocMin;
  } else {
    rtb_Delay_i = fminf(fminf(fminf(rtb_uDLookupTable1, rtb_uDLookupTable2),
      rtb_uDLookupTable3), rtb_uDLookupTable4);
  }

  if (rtb_Delay_i > 100.0F) {
    BSER_rLSOCMin = 100.0F;
  } else if (rtb_Delay_i < 0.0F) {
    BSER_rLSOCMin = 0.0F;
  } else {
    BSER_rLSOCMin = rtb_Delay_i;
  }

  if (BSER_bLSocCrrCls_C) {
    rtb_Delay_i = BSER_rSocMax;
  } else {
    rtb_Delay_i = fmaxf(fmaxf(fmaxf(rtb_uDLookupTable1, rtb_uDLookupTable2),
      rtb_uDLookupTable3), rtb_uDLookupTable4);
  }

  if (rtb_Delay_i > 100.0F) {
    BSER_rLSOCMax = 100.0F;
  } else if (rtb_Delay_i < 0.0F) {
    BSER_rLSOCMax = 0.0F;
  } else {
    BSER_rLSOCMax = rtb_Delay_i;
  }

  rtb_RelationalOperator1_c = (BSPR_uCellVoltMax >= BSER_uCellOverVoltThres_C);
  if (rtb_RelationalOperator1_c) {
    rtb_Delay_i = 0.01F;
  } else {
    rtb_Delay_i = 0.0F;
  }

  rtb_uDLookupTable1 = (rtb_Delay_i + fminf(BSER_tiCellOverVoltFusf_C,
    BSER_BattSopCalc_DW.Delay_DSTATE)) * (real32_T)rtb_RelationalOperator1_c;
  rtb_RelationalOperator = (rtb_uDLookupTable1 >= BSER_tiCellOverVoltFusf_C);
  rtb_uDLookupTable4 = look2_iflf_binlxpw(BSER_rSoh, BSPR_tCellTempMin, ((const
    real32_T *)&(BSER_perSohCellVoltRng_A[0])), ((const real32_T *)
    &(BSER_tCellVoltRng_A[0])), ((const real32_T *)&(BSER_uCellVoltRngLo_MAP[0])),
    rtCP_BSER_uCellMinLtbLmt_maxInd, 5U);
  rtb_RelationalOperator1_c = (BSPR_uCellVoltMin <= rtb_uDLookupTable4);
  if (rtb_RelationalOperator1_c) {
    rtb_Delay_i = 0.01F;
  } else {
    rtb_Delay_i = 0.0F;
  }

  rtb_uDLookupTable2 = (rtb_Delay_i + fminf(BSER_tiCellUndrVoltFusf_C,
    BSER_BattSopCalc_DW.Delay_DSTATE_d)) * (real32_T)rtb_RelationalOperator1_c;
  rtb_RelationalOperator_j = (rtb_uDLookupTable2 >= BSER_tiCellUndrVoltFusf_C);
  rtb_RelationalOperator1_c = (BSPR_uCellVoltMin <= rtb_uDLookupTable4 +
    BSER_uCellVoltRngDiff_C);
  if (rtb_RelationalOperator1_c) {
    rtb_Delay_i = 0.01F;
  } else {
    rtb_Delay_i = 0.0F;
  }

  rtb_uDLookupTable3 = (rtb_Delay_i + fminf(BSER_tiCellOverVoltLv1_C,
    BSER_BattSopCalc_DW.Delay_DSTATE_dv)) * (real32_T)rtb_RelationalOperator1_c;
  rtb_RelationalOperator_f1 = (rtb_uDLookupTable3 >= BSER_tiCellOverVoltLv1_C);
  rtb_RelationalOperator1_c = (BSPR_uCellVoltMin >= rtb_uDLookupTable4 +
    BSER_uCellVoltRngDiffHeal_C);
  if (rtb_RelationalOperator1_c) {
    rtb_Delay_i = 0.01F;
  } else {
    rtb_Delay_i = 0.0F;
  }

  rtb_uDLookupTable4 = (rtb_Delay_i + fminf(BSER_tiCellOverVoltLv1_C,
    BSER_BattSopCalc_DW.Delay_DSTATE_a)) * (real32_T)rtb_RelationalOperator1_c;
  rtb_RelationalOperator1_c = (rtb_uDLookupTable4 >= BSER_tiCellOverVoltLv1_C);
  if (BSER_BattSopCalc_DW.Delay_DSTATE_o) {
    rtb_Switch1_g = BSPR_uCellVoltMin;
  } else {
    rtb_Switch1_g = (BSPR_uCellVoltMin - BSER_BattSopCalc_DW.UnitDelay_DSTATE) *
      BSER_uCellVoltFilter_C + BSER_BattSopCalc_DW.UnitDelay_DSTATE;
  }

  BSER_BattSopCalc_DW.UnitDelay_DSTATE = rtb_Switch1_g;
  BSER_BattSopCalc_DW.Delay_DSTATE_o = false;
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMin, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT1);
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMax, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT5);
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMin, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT2);
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMax, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT6);
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMin, BSER_rSocMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT3);
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMax, BSER_rSocMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT7);
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMin, BSER_rSocMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT4);
  BSER_BattSopC_CurrentLimit_LUT1(BSPR_tCellTempMax, BSER_rSocMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT8);
  if (BSER_bLSocCrrChrgSta_C) {
    rtb_Gain1 = fmaxf(fmaxf(fmaxf(fmaxf(fmaxf(fmaxf(fmaxf
      (BSER_BattSopCalc_B.CurrentLimit_LUT1.SOP_I_10s,
       BSER_BattSopCalc_B.CurrentLimit_LUT5.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT2.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT6.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT3.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT7.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT4.SOP_I_10s),
                      BSER_BattSopCalc_B.CurrentLimit_LUT8.SOP_I_10s);
  } else {
    rtb_Gain1 = fmaxf(fmaxf(fmaxf(BSER_BattSopCalc_B.CurrentLimit_LUT4.SOP_I_10s,
      BSER_BattSopCalc_B.CurrentLimit_LUT3.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT7.SOP_I_10s),
                      BSER_BattSopCalc_B.CurrentLimit_LUT8.SOP_I_10s);
  }

  rtb_Gain1_f = (real32_T)BSER_numParlCellNum_C * rtb_Gain1;
  if (rtb_Gain1_f > 0.0F) {
    rtb_Saturation = 0.0F;
  } else if (rtb_Gain1_f < BSER_iCellPeakChCurrThresTbl_C) {
    rtb_Saturation = BSER_iCellPeakChCurrThresTbl_C;
  } else {
    rtb_Saturation = rtb_Gain1_f;
  }

  if (BSER_bLSocCrrChrgSta_C) {
    rtb_Gain1 = fmaxf(fmaxf(fmaxf(fmaxf(fmaxf(fmaxf(fmaxf
      (BSER_BattSopCalc_B.CurrentLimit_LUT1.SOP_I_Conti,
       BSER_BattSopCalc_B.CurrentLimit_LUT5.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT2.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT6.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT3.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT7.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT4.SOP_I_Conti),
                      BSER_BattSopCalc_B.CurrentLimit_LUT8.SOP_I_Conti);
  } else {
    rtb_Gain1 = fmaxf(fmaxf(fmaxf
      (BSER_BattSopCalc_B.CurrentLimit_LUT8.SOP_I_Conti,
       BSER_BattSopCalc_B.CurrentLimit_LUT7.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT3.SOP_I_Conti),
                      BSER_BattSopCalc_B.CurrentLimit_LUT4.SOP_I_Conti);
  }

  rtb_Gain1_f = (real32_T)BSER_numParlCellNum_C * rtb_Gain1;
  if (rtb_Gain1_f > 0.0F) {
    rtb_Saturation1 = 0.0F;
  } else if (rtb_Gain1_f < BSER_iCellConstChCurrtThres_C) {
    rtb_Saturation1 = BSER_iCellConstChCurrtThres_C;
  } else {
    rtb_Saturation1 = rtb_Gain1_f;
  }

  rtb_Gain1_f = BSPR_iBattCurr - rtb_Saturation1;
  rtb_Gain1 = BSER_rSopShrtTimDef_C * BSER_rSopTimLimtDef_C;
  if (!((rtb_Gain1_f < 0.0F) || (BSER_BattSopCalc_DW.Delay_DSTATE_a0 <=
        (rtb_Saturation1 - rtb_Saturation) * rtb_Gain1))) {
    rtb_Gain1_f = 0.0F;
  }

  rtb_Delay_i = 0.01F * rtb_Gain1_f + BSER_BattSopCalc_DW.Delay_DSTATE_a0;
  if (rtb_Delay_i > 10000.0F) {
    rtb_Delay_i = 10000.0F;
  } else {
    if (rtb_Delay_i < -10000.0F) {
      rtb_Delay_i = -10000.0F;
    }
  }

  rtb_Gain1_f = (real32_T)!rtb_RelationalOperator;
  rtb_Product2_d = fminf(fmaxf(rtb_Saturation, rtb_Saturation - rtb_Delay_i /
    BSER_rSopShrtTimDef_C), rtb_Saturation1) * rtb_BSER_Soh * rtb_Gain1_f;
  rtb_Saturation1 = rtb_BSER_Soh * rtb_Saturation1 * rtb_Gain1_f;
  BSER_BattSopCalc_DW.Delay_DSTATE_a0 = rtb_Delay_i;
  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Saturation4 = BSER_iFault4ChCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Saturation4 = BSER_iFault3ChCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Saturation4 = BSER_iFault2ChCurrLimt_C;
  } else {
    rtb_Saturation4 = rtb_Product2_d;
  }

  rtb_Delay_i = fmaxf(rtb_Saturation4, rtb_Product2_d);
  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Saturation4 = BSER_iFault4ChCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Saturation4 = BSER_iFault3ChCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Saturation4 = BSER_iFault2ChCurrLimt_C;
  } else {
    rtb_Saturation4 = rtb_Saturation1;
  }

  rtb_Saturation1 = fmaxf(rtb_Saturation4, rtb_Saturation1);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMin, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT1_m);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMax, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT5_p);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMin, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT2_m);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMax, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT6_d);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMin, BSER_rSocMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT3_d);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMax, BSER_rSocMin,
    &BSER_BattSopCalc_B.CurrentLimit_LUT7_o);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMin, BSER_rSocMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT4_h);
  BSER_BattSo_CurrentLimit_LUT1_m(BSPR_tCellTempMax, BSER_rSocMax,
    &BSER_BattSopCalc_B.CurrentLimit_LUT8_a);
  rtb_RelationalOperator_a = (BSER_rSocMin > BSER_rSocMinCellLmt_C);
  if (rtb_RelationalOperator_a) {
    rtb_Saturation5 = fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.CurrentLimit_LUT1_m.SOP_I_10s,
       BSER_BattSopCalc_B.CurrentLimit_LUT5_p.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT2_m.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT6_d.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT3_d.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT7_o.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT4_h.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT8_a.SOP_I_10s);
  } else {
    rtb_Saturation5 = fminf(fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.CurrentLimit_LUT1_m.SOP_I_10s,
       BSER_BattSopCalc_B.CurrentLimit_LUT5_p.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT2_m.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT6_d.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT3_d.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT7_o.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT4_h.SOP_I_10s),
      BSER_BattSopCalc_B.CurrentLimit_LUT8_a.SOP_I_10s), look2_iflf_binlcapw
      (BSPR_tCellTempMin, rtb_Switch1_g, ((const real32_T *)
      &(BSER_tDisChrgCurrMapTemp_A[0])), ((const real32_T *)
      &(BSER_uCellUndrVoltLmt_A[0])), ((const real32_T *)
      &(BSER_iShrtDisChCurrVlmtMap_MAP[0])), rtCP_SOP_I_10s_maxIndex_l, 20U));
  }

  rtb_Gain1_f = (real32_T)BSER_numParlCellNum_C * rtb_Saturation5;
  if (rtb_Gain1_f > BSER_iCellPeakDisChCurrThresTbl_C) {
    rtb_Saturation4 = BSER_iCellPeakDisChCurrThresTbl_C;
  } else if (rtb_Gain1_f < 0.0F) {
    rtb_Saturation4 = 0.0F;
  } else {
    rtb_Saturation4 = rtb_Gain1_f;
  }

  if (rtb_RelationalOperator_a) {
    rtb_Saturation5 = fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.CurrentLimit_LUT1_m.SOP_I_Conti,
       BSER_BattSopCalc_B.CurrentLimit_LUT5_p.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT2_m.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT6_d.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT3_d.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT7_o.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT4_h.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT8_a.SOP_I_Conti);
  } else {
    rtb_Saturation5 = fminf(fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.CurrentLimit_LUT1_m.SOP_I_Conti,
       BSER_BattSopCalc_B.CurrentLimit_LUT5_p.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT2_m.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT6_d.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT3_d.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT7_o.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT4_h.SOP_I_Conti),
      BSER_BattSopCalc_B.CurrentLimit_LUT8_a.SOP_I_Conti), look2_iflf_binlcapw
      (BSPR_tCellTempMin, rtb_Switch1_g, ((const real32_T *)
      &(BSER_tDisChrgCurrMapTemp_A[0])), ((const real32_T *)
      &(BSER_uCellUndrVoltLmt_A[0])), ((const real32_T *)
      &(BSER_iLonDisChCurrVlmtMap_MAP[0])), rtCP_SOP_I_Conti_maxIndex_fd, 20U));
  }

  rtb_Gain1_f = (real32_T)BSER_numParlCellNum_C * rtb_Saturation5;
  if (rtb_Gain1_f > BSER_iCellConstDisChCurrtThres_C) {
    rtb_Saturation5 = BSER_iCellConstDisChCurrtThres_C;
  } else if (rtb_Gain1_f < 0.0F) {
    rtb_Saturation5 = 0.0F;
  } else {
    rtb_Saturation5 = rtb_Gain1_f;
  }

  rtb_Switch2_l = BSPR_iBattCurr - rtb_Saturation5;
  if (!((rtb_Switch2_l > 0.0F) || (BSER_BattSopCalc_DW.Delay_DSTATE_e >=
        (rtb_Saturation5 - rtb_Saturation4) * rtb_Gain1))) {
    rtb_Switch2_l = 0.0F;
  }

  rtb_Gain1_f = 0.01F * rtb_Switch2_l + BSER_BattSopCalc_DW.Delay_DSTATE_e;
  if (rtb_Gain1_f > 10000.0F) {
    rtb_Gain1_f = 10000.0F;
  } else {
    if (rtb_Gain1_f < -10000.0F) {
      rtb_Gain1_f = -10000.0F;
    }
  }

  if (rtb_RelationalOperator_j) {
    rtb_Switch2_l = BSER_rPwrLimtDet_C;
  } else if (rtb_RelationalOperator1_c) {
    rtb_Switch2_l = BSER_rPwrLimtHeal_C;
  } else if (rtb_RelationalOperator_f1) {
    rtb_Switch2_l = BSER_rPwrLimtLv1_C;
  } else {
    rtb_Switch2_l = BSER_rPwrLimtLv2_C;
  }

  rtb_Product2_l = fmaxf(fminf(rtb_Saturation4, rtb_Saturation4 - rtb_Gain1_f /
    BSER_rSopShrtTimDef_C), rtb_Saturation5) * rtb_BSER_Soh * rtb_Switch2_l;
  rtb_Saturation5 = rtb_BSER_Soh * rtb_Saturation5 * rtb_Switch2_l;
  BSER_BattSopCalc_DW.Delay_DSTATE_e = rtb_Gain1_f;
  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Gain1 = BSER_iFault4DisCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Gain1 = BSER_iFault3DisCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Gain1 = BSER_iFault2DisCurrLimt_C;
  } else {
    rtb_Gain1 = rtb_Product2_l;
  }

  rtb_Gain1_f = fminf(rtb_Gain1, rtb_Product2_l);
  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Gain1 = BSER_iFault4DisCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Gain1 = BSER_iFault3DisCurrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Gain1 = BSER_iFault2DisCurrLimt_C;
  } else {
    rtb_Gain1 = rtb_Saturation5;
  }

  rtb_Saturation5 = fminf(rtb_Gain1, rtb_Saturation5);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMin, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT1);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMax, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT5);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMin, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT2);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMax, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT6);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMin, BSER_rSocMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT3);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMax, BSER_rSocMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT7);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMin, BSER_rSocMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT4);
  BSER_BattSopCal_PowerLimit_LUT1(BSPR_tCellTempMax, BSER_rSocMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT8);
  if (BSER_bLSocCrrChrgSta_C) {
    rtb_Gain1 = fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.PowerLimit_LUT1.SOP_P_Conti,
       BSER_BattSopCalc_B.PowerLimit_LUT5.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT2.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT6.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT3.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT7.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT4.SOP_P_Conti),
                      BSER_BattSopCalc_B.PowerLimit_LUT8.SOP_P_Conti);
  } else {
    rtb_Gain1 = fminf(fminf(fminf(BSER_BattSopCalc_B.PowerLimit_LUT8.SOP_P_Conti,
      BSER_BattSopCalc_B.PowerLimit_LUT7.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT3.SOP_P_Conti),
                      BSER_BattSopCalc_B.PowerLimit_LUT4.SOP_P_Conti);
  }

  rtb_Switch2_l = (real32_T)BSER_numParlCellNum_C * rtb_Gain1;
  if (BSER_bLSocCrrChrgSta_C) {
    rtb_Gain1 = fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.PowerLimit_LUT1.SOP_P_10s,
       BSER_BattSopCalc_B.PowerLimit_LUT5.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT2.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT6.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT3.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT7.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT4.SOP_P_10s),
                      BSER_BattSopCalc_B.PowerLimit_LUT8.SOP_P_10s);
  } else {
    rtb_Gain1 = fminf(fminf(fminf(BSER_BattSopCalc_B.PowerLimit_LUT4.SOP_P_10s,
      BSER_BattSopCalc_B.PowerLimit_LUT3.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT7.SOP_P_10s),
                      BSER_BattSopCalc_B.PowerLimit_LUT8.SOP_P_10s);
  }

  rtb_Gain1 *= (real32_T)BSER_numParlCellNum_C;
  if (rtb_Gain1 > BSER_pCellPeakChPwrThresTbl_C) {
    rtb_Gain1 = BSER_pCellPeakChPwrThresTbl_C;
  } else {
    if (rtb_Gain1 < 0.0F) {
      rtb_Gain1 = 0.0F;
    }
  }

  if (rtb_Switch2_l > BSER_pCellConstChPwrThres_C) {
    rtb_Saturation3 = BSER_pCellConstChPwrThres_C;
  } else if (rtb_Switch2_l < 0.0F) {
    rtb_Saturation3 = 0.0F;
  } else {
    rtb_Saturation3 = rtb_Switch2_l;
  }

  rtb_Switch_o = !rtb_RelationalOperator;
  rtb_Switch2_l = rtb_BSER_Soh * rtb_Saturation3 * (real32_T)rtb_Switch_o;
  if (!(rtb_Saturation != 0.0F)) {
    rtb_Saturation = BSER_numDivPrtNum_C;
  }

  rtb_Saturation = fmaxf(fminf(rtb_Gain1, (real32_T)((real32_T)(real32_T)((real32_T)
    rtb_Product2_d / rtb_Saturation) * rtb_Gain1)), rtb_Saturation3) *
    rtb_BSER_Soh * (real32_T)rtb_Switch_o;
  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Gain1 = BSER_pFault4ChPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Gain1 = BSER_pFault3ChPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Gain1 = BSER_pFault2ChPwrLimt_C;
  } else {
    rtb_Gain1 = rtb_Saturation;
  }

  rtb_Saturation = fminf(rtb_Gain1, rtb_Saturation);
  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Gain1 = BSER_pFault4ChPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Gain1 = BSER_pFault3ChPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Gain1 = BSER_pFault2ChPwrLimt_C;
  } else {
    rtb_Gain1 = rtb_Switch2_l;
  }

  rtb_Product2_d = fminf(rtb_Gain1, rtb_Switch2_l);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMin, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT1_c);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMax, BSER_rLSOCMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT5_c);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMin, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT2_n);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMax, BSER_rLSOCMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT6_h);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMin, BSER_rSocMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT3_h);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMax, BSER_rSocMin,
    &BSER_BattSopCalc_B.PowerLimit_LUT7_h);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMin, BSER_rSocMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT4_n);
  BSER_BattSopC_PowerLimit_LUT1_c(BSPR_tCellTempMax, BSER_rSocMax,
    &BSER_BattSopCalc_B.PowerLimit_LUT8_k);
  rtb_RelationalOperator = (BSER_rSocMin > BSER_rSocMinCellLmt_C);
  if (rtb_RelationalOperator) {
    rtb_Switch2_l = fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.PowerLimit_LUT1_c.SOP_P_10s,
       BSER_BattSopCalc_B.PowerLimit_LUT5_c.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT2_n.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT6_h.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT3_h.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT7_h.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT4_n.SOP_P_10s),
                          BSER_BattSopCalc_B.PowerLimit_LUT8_k.SOP_P_10s);
  } else {
    rtb_Switch2_l = fminf(fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.PowerLimit_LUT1_c.SOP_P_10s,
       BSER_BattSopCalc_B.PowerLimit_LUT5_c.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT2_n.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT6_h.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT3_h.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT7_h.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT4_n.SOP_P_10s),
      BSER_BattSopCalc_B.PowerLimit_LUT8_k.SOP_P_10s), look2_iflf_binlcapw
                          (BSPR_tCellTempMin, rtb_Switch1_g, ((const real32_T *)
      &(BSER_tDisChrgPwrMapTemp_A[0])), ((const real32_T *)
      &(BSER_uCellUndrVoltLmt_A[0])), ((const real32_T *)
      &(BSER_pShrtDischPwrVlmtMap_MAP[0])), rtCP_SOP_P_10s1_maxIndex, 20U));
  }

  rtb_Gain1 = (real32_T)BSER_numParlCellNum_C * rtb_Switch2_l;
  if (rtb_RelationalOperator) {
    rtb_Switch2_l = fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.PowerLimit_LUT1_c.SOP_P_Conti,
       BSER_BattSopCalc_B.PowerLimit_LUT5_c.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT2_n.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT6_h.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT3_h.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT7_h.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT4_n.SOP_P_Conti),
                          BSER_BattSopCalc_B.PowerLimit_LUT8_k.SOP_P_Conti);
  } else {
    rtb_Switch2_l = fminf(fminf(fminf(fminf(fminf(fminf(fminf(fminf
      (BSER_BattSopCalc_B.PowerLimit_LUT1_c.SOP_P_Conti,
       BSER_BattSopCalc_B.PowerLimit_LUT5_c.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT2_n.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT6_h.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT3_h.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT7_h.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT4_n.SOP_P_Conti),
      BSER_BattSopCalc_B.PowerLimit_LUT8_k.SOP_P_Conti), look2_iflf_binlcapw
                          (BSPR_tCellTempMin, rtb_Switch1_g, ((const real32_T *)
      &(BSER_tDisChrgPwrMapTemp_A[0])), ((const real32_T *)
      &(BSER_uCellUndrVoltLmt_A[0])), ((const real32_T *)
      &(BSER_pLonDischPwrVlmtMap_MAP[0])), rtCP_SOP_P_Conti_maxIndex_np, 20U));
  }

  rtb_Switch2_l *= (real32_T)BSER_numParlCellNum_C;
  if (rtb_Gain1 > BSER_pCellPeakDisChPwrThresTbl_C) {
    rtb_Gain1 = BSER_pCellPeakDisChPwrThresTbl_C;
  } else {
    if (rtb_Gain1 < 0.0F) {
      rtb_Gain1 = 0.0F;
    }
  }

  if (rtb_Switch2_l > BSER_pCellConstDisChPwrThres_C) {
    rtb_Switch2_l = BSER_pCellConstDisChPwrThres_C;
  } else {
    if (rtb_Switch2_l < 0.0F) {
      rtb_Switch2_l = 0.0F;
    }
  }

  if (rtb_RelationalOperator_j) {
    rtb_Switch1_g = fminf(rtb_BSER_Soh * rtb_Switch2_l,
                          BSER_pDischgFloorDisPwrLimt_C);
    if (!(rtb_Saturation4 != 0.0F)) {
      rtb_Saturation4 = BSER_numDivPrtNum_C;
    }

    rtb_BSER_Soh = fminf(BSER_pDischgFloorDisPwrLimt_C, fmaxf(fminf(rtb_Gain1,
      (real32_T)((real32_T)(real32_T)((real32_T)rtb_Product2_l / rtb_Saturation4) *
                 rtb_Gain1)), rtb_Switch2_l) * rtb_BSER_Soh);
  } else {
    rtb_Switch1_g = rtb_BSER_Soh * rtb_Switch2_l;
    if (!(rtb_Saturation4 != 0.0F)) {
      rtb_Saturation4 = BSER_numDivPrtNum_C;
    }

    rtb_BSER_Soh *= fmaxf(fminf(rtb_Gain1, (real32_T)((real32_T)(real32_T)((real32_T)
      rtb_Product2_l / rtb_Saturation4) * rtb_Gain1)), rtb_Switch2_l);
  }

  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Gain1 = BSER_pFault4DisPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Gain1 = BSER_pFault3DisPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Gain1 = BSER_pFault2DisPwrLimt_C;
  } else {
    rtb_Gain1 = rtb_BSER_Soh;
  }

  rtb_BSER_Soh = fminf(rtb_Gain1, rtb_BSER_Soh);
  if (BSER_BattSopCalc_B.Fault4_enable) {
    rtb_Gain1 = BSER_pFault4DisPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault3_enable) {
    rtb_Gain1 = BSER_pFault3DisPwrLimt_C;
  } else if (BSER_BattSopCalc_B.Fault2_enable) {
    rtb_Gain1 = BSER_pFault2DisPwrLimt_C;
  } else {
    rtb_Gain1 = rtb_Switch1_g;
  }

  rtb_Switch1_g = fminf(rtb_Gain1, rtb_Switch1_g);
  if (!BSER_BattSopCalc_B.Ini_enable) {
    rtb_Saturation4 = rtb_Saturation1 - BSER_BattSopCalc_DW.Delay1_DSTATE;
    if (!(fabsf(rtb_Saturation4) <= BSER_iSopCurrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_Saturation4 = -1.0F;
      } else {
        if (rtb_Saturation4 > 0.0F) {
          rtb_Saturation4 = 1.0F;
        }
      }

      rtb_Saturation1 = rtb_Saturation4 * BSER_iSopCurrFilter_C +
        BSER_BattSopCalc_DW.Delay1_DSTATE;
    }

    rtb_Saturation4 = rtb_Delay_i - BSER_BattSopCalc_DW.Delay_DSTATE_b;
    if (!(fabsf(rtb_Saturation4) <= BSER_iSopCurrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_Saturation4 = -1.0F;
      } else {
        if (rtb_Saturation4 > 0.0F) {
          rtb_Saturation4 = 1.0F;
        }
      }

      rtb_Delay_i = rtb_Saturation4 * BSER_iSopCurrFilter_C +
        BSER_BattSopCalc_DW.Delay_DSTATE_b;
    }

    rtb_Saturation4 = rtb_Saturation - BSER_BattSopCalc_DW.Delay2_DSTATE;
    if (!(fabsf(rtb_Saturation4) <= BSER_pSopPwrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_Saturation4 = -1.0F;
      } else {
        if (rtb_Saturation4 > 0.0F) {
          rtb_Saturation4 = 1.0F;
        }
      }

      rtb_Saturation = rtb_Saturation4 * BSER_pSopPwrFilter_C +
        BSER_BattSopCalc_DW.Delay2_DSTATE;
    }

    rtb_Saturation4 = rtb_Product2_d - BSER_BattSopCalc_DW.Delay3_DSTATE;
    if (!(fabsf(rtb_Saturation4) <= BSER_pSopPwrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_Saturation4 = -1.0F;
      } else {
        if (rtb_Saturation4 > 0.0F) {
          rtb_Saturation4 = 1.0F;
        }
      }

      rtb_Product2_d = rtb_Saturation4 * BSER_pSopPwrFilter_C +
        BSER_BattSopCalc_DW.Delay3_DSTATE;
    }

    rtb_Saturation4 = rtb_Saturation5 - BSER_BattSopCalc_DW.Delay5_DSTATE;
    if (!(fabsf(rtb_Saturation4) <= BSER_iSopCurrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_Saturation4 = -1.0F;
      } else {
        if (rtb_Saturation4 > 0.0F) {
          rtb_Saturation4 = 1.0F;
        }
      }

      rtb_Saturation5 = rtb_Saturation4 * BSER_iSopCurrFilter_C +
        BSER_BattSopCalc_DW.Delay5_DSTATE;
    }

    rtb_Saturation4 = rtb_Gain1_f - BSER_BattSopCalc_DW.Delay4_DSTATE;
    if (!(fabsf(rtb_Saturation4) <= BSER_iSopCurrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_Saturation4 = -1.0F;
      } else {
        if (rtb_Saturation4 > 0.0F) {
          rtb_Saturation4 = 1.0F;
        }
      }

      if (rtb_RelationalOperator_j) {
        rtb_Gain1_f = BSER_iSopCurrFilter_C;
      } else if (rtb_RelationalOperator1_c) {
        rtb_Gain1_f = BSER_iSopCurrFilter_C;
      } else {
        rtb_Gain1_f = BSER_iDecLv1CurrFilter_C;
      }

      rtb_Gain1_f = rtb_Saturation4 * rtb_Gain1_f +
        BSER_BattSopCalc_DW.Delay4_DSTATE;
    }

    rtb_Saturation4 = rtb_BSER_Soh - BSER_BattSopCalc_DW.Delay6_DSTATE;
    rtb_Gain1 = fabsf(rtb_Saturation4);
    if (!(rtb_Gain1 <= BSER_pSopPwrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_BSER_Soh = -1.0F;
      } else if (rtb_Saturation4 > 0.0F) {
        rtb_BSER_Soh = 1.0F;
      } else {
        rtb_BSER_Soh = rtb_Saturation4;
      }

      if (rtb_Saturation4 > 0.0F) {
        rtb_Saturation4 = BSER_pSopPwrHealLv1Filter_C;
      } else if (rtb_RelationalOperator_j) {
        rtb_Saturation4 = fminf(rtb_Gain1 * BSER_PwrDownDisFloorFactor,
          BSER_pSopDischgFloorPwrFilter_C);
      } else if (rtb_RelationalOperator_f1) {
        rtb_Saturation4 = BSER_pSopPwrDetLv1Filter_C;
      } else if (rtb_RelationalOperator1_c) {
        rtb_Saturation4 = BSER_pSopPwrHealLv2Filter_C;
      } else {
        rtb_Saturation4 = BSER_pSopPwrHealLv1Filter_C;
      }

      rtb_BSER_Soh = rtb_BSER_Soh * rtb_Saturation4 +
        BSER_BattSopCalc_DW.Delay6_DSTATE;
    }

    rtb_Saturation4 = rtb_Switch1_g - BSER_BattSopCalc_DW.Delay7_DSTATE;
    if (!(fabsf(rtb_Saturation4) <= BSER_pSopPwrFilter_C)) {
      if (rtb_Saturation4 < 0.0F) {
        rtb_Saturation4 = -1.0F;
      } else {
        if (rtb_Saturation4 > 0.0F) {
          rtb_Saturation4 = 1.0F;
        }
      }

      rtb_Switch1_g = rtb_Saturation4 * BSER_pSopPwrFilter_C +
        BSER_BattSopCalc_DW.Delay7_DSTATE;
    }
  }

  rtb_RelationalOperator_f1 = (BMCR_stBmsModeCtrl >= BSER_stSopBgnCnt_C);
  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_Saturation;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > BSER_pCellPeakChPwrThres_C) {
    rtb_Saturation4 = BSER_pCellPeakChPwrThres_C;
  } else {
    if (rtb_Saturation4 < 0.0F) {
      rtb_Saturation4 = 0.0F;
    }
  }

  BSER_pPeakPwrCh = (real32_T)((real32_T)BSER_numSeriesCells_C * rtb_Saturation4 *
    0.001F);
  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_Product2_d;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > BSER_pCellConstChPwrThres_C) {
    rtb_Saturation4 = BSER_pCellConstChPwrThres_C;
  } else {
    if (rtb_Saturation4 < 0.0F) {
      rtb_Saturation4 = 0.0F;
    }
  }

  BSER_pConstPwrCh = (real32_T)((real32_T)BSER_numSeriesCells_C * rtb_Saturation4 *
    0.001F);
  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_BSER_Soh;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > BSER_pCellPeakDisChPwrThres_C) {
    rtb_Saturation4 = BSER_pCellPeakDisChPwrThres_C;
  } else {
    if (rtb_Saturation4 < 0.0F) {
      rtb_Saturation4 = 0.0F;
    }
  }

  BSER_pPeakPwrDisCh = (real32_T)((real32_T)BSER_numSeriesCells_C *
    rtb_Saturation4 * 0.001F);
  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_Switch1_g;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > BSER_pCellConstDisChPwrThres_C) {
    rtb_Saturation4 = BSER_pCellConstDisChPwrThres_C;
  } else {
    if (rtb_Saturation4 < 0.0F) {
      rtb_Saturation4 = 0.0F;
    }
  }

  BSER_pConstPwrDisCh = (real32_T)((real32_T)BSER_numSeriesCells_C *
    rtb_Saturation4 * 0.001F);
  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_Delay_i;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > 0.0F) {
    BSER_iPeakCurrCh = 0.0F;
  } else if (rtb_Saturation4 < BSER_iCellPeakChCurrThres_C) {
    BSER_iPeakCurrCh = BSER_iCellPeakChCurrThres_C;
  } else {
    BSER_iPeakCurrCh = rtb_Saturation4;
  }

  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_Saturation1;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > 0.0F) {
    BSER_iConstCurrChMax = 0.0F;
  } else if (rtb_Saturation4 < BSER_iCellConstChCurrtThres_C) {
    BSER_iConstCurrChMax = BSER_iCellConstChCurrtThres_C;
  } else {
    BSER_iConstCurrChMax = rtb_Saturation4;
  }

  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_Gain1_f;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > BSER_iCellPeakDisChCurrThres_C) {
    BSER_iPeakCurrDisCh = BSER_iCellPeakDisChCurrThres_C;
  } else if (rtb_Saturation4 < 0.0F) {
    BSER_iPeakCurrDisCh = 0.0F;
  } else {
    BSER_iPeakCurrDisCh = rtb_Saturation4;
  }

  if (rtb_RelationalOperator_f1) {
    rtb_Saturation4 = rtb_Saturation5;
  } else {
    rtb_Saturation4 = 0.0F;
  }

  if (rtb_Saturation4 > BSER_iCellConstDisChCurrtThres_C) {
    BSER_iConstCurrDisChMax = BSER_iCellConstDisChCurrtThres_C;
  } else if (rtb_Saturation4 < 0.0F) {
    BSER_iConstCurrDisChMax = 0.0F;
  } else {
    BSER_iConstCurrDisChMax = rtb_Saturation4;
  }

  BSER_BattSopCalc_DW.Delay_DSTATE_b = rtb_Delay_i;
  BSER_BattSopCalc_DW.Delay1_DSTATE = rtb_Saturation1;
  BSER_BattSopCalc_DW.Delay2_DSTATE = rtb_Saturation;
  BSER_BattSopCalc_DW.Delay3_DSTATE = rtb_Product2_d;
  BSER_BattSopCalc_DW.Delay4_DSTATE = rtb_Gain1_f;
  BSER_BattSopCalc_DW.Delay5_DSTATE = rtb_Saturation5;
  BSER_BattSopCalc_DW.Delay6_DSTATE = rtb_BSER_Soh;
  BSER_BattSopCalc_DW.Delay7_DSTATE = rtb_Switch1_g;
  BSER_uPeakVoltCh = BSER_uPeakVoltCh_C;
  BSER_uPeakVoltDisCh = BSER_uPeakVoltDisCh_C;
  BSER_bDisFloorFlg = rtb_RelationalOperator_j;
  BSER_BattSopCalc_DW.Delay_DSTATE = rtb_uDLookupTable1;
  BSER_BattSopCalc_DW.Delay_DSTATE_d = rtb_uDLookupTable2;
  BSER_BattSopCalc_DW.Delay_DSTATE_dv = rtb_uDLookupTable3;
  BSER_BattSopCalc_DW.Delay_DSTATE_a = rtb_uDLookupTable4;
}

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

  /* block I/O */

  /* custom signals */
  BSER_uPeakVoltCh = 0.0F;
  BSER_uPeakVoltDisCh = 0.0F;
  BSER_pPeakPwrCh = 0.0F;
  BSER_pConstPwrCh = 0.0F;
  BSER_pPeakPwrDisCh = 0.0F;
  BSER_pConstPwrDisCh = 0.0F;
  BSER_iPeakCurrCh = 0.0F;
  BSER_iConstCurrChMax = 0.0F;
  BSER_iPeakCurrDisCh = 0.0F;
  BSER_iConstCurrDisChMax = 0.0F;
  BSER_bDisFloorFlg = false;
  BSER_BattSopCalc_DW.Delay_DSTATE_o = true;
}

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