/*
 * File: BSER_BattSoeCalc.c
 *
 * Code generated for Simulink model 'BSER_BattSoeCalc'.
 *
 * Model version                  : A100.3_0
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Sat Oct 10 17:30:29 2020
 *
 * 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_BattSoeCalc.h"
#include "BSER_BattSoeCalc_private.h"

/* Named constants for Chart: '<S1>/SoeControl' */
#define BSER_BattSoeCalc_IN_Ini        ((uint8_T)1U)
#define BSER_BattSoeCalc_IN_Run        ((uint8_T)2U)

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

/* Definition for custom storage class: OutputSignal */
volatile real32_T BSER_ahBattCap;
volatile real32_T BSER_eBattNrmEnrg;   /* Battary Pack Nominal Engergy */
volatile real32_T BSER_eSoe;           /* Battary Pack State of Engergy */
volatile real32_T BSER_eSoeOcv;        /* SOE based on battery OCV */
volatile real32_T BSER_eSoeUbatt;      /* SOE based on battery voltage */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block signals (auto storage) */
B_BSER_BattSoeCalc_T BSER_BattSoeCalc_B;

/* Block states (auto storage) */
DW_BSER_BattSoeCalc_T BSER_BattSoeCalc_DW;

/* Model step function */
void BSER_BattSoeCalc_step(void)
{
  real32_T rtb_Gain1;
  real32_T rtb_Product_p;
  real32_T rtb_Subtract1;
  real32_T rtb_Subtract1_j;
  if (BSER_BattSoeCalc_DW.temporalCounter_i1 < MAX_uint32_T) {
    BSER_BattSoeCalc_DW.temporalCounter_i1++;
  }

  if (BSER_BattSoeCalc_DW.is_active_c3_BSER_BattSoeCalc == 0U) {
    BSER_BattSoeCalc_DW.is_active_c3_BSER_BattSoeCalc = 1U;
    BSER_BattSoeCalc_DW.is_c3_BSER_BattSoeCalc = BSER_BattSoeCalc_IN_Ini;
    BSER_BattSoeCalc_DW.temporalCounter_i1 = 0U;
    BSER_BattSoeCalc_B.IniEna = true;
  } else {
    if ((BSER_BattSoeCalc_DW.is_c3_BSER_BattSoeCalc == BSER_BattSoeCalc_IN_Ini) &&
        (BSER_BattSoeCalc_DW.temporalCounter_i1 >= (uint32_T)ceil
         (BSER_tiSoeIniTime_C / 0.02F - 2.0E-10F))) {
      BSER_BattSoeCalc_DW.is_c3_BSER_BattSoeCalc = BSER_BattSoeCalc_IN_Run;
      BSER_BattSoeCalc_B.IniEna = false;
    }
  }

  rtb_Product_p = 0.01F * BSER_rSoh * fminf(look1_iflf_binlcapw
    (BSPR_tCellTempMin, ((const real32_T *)&(BSER_tBattCapTemp_A[0])), ((const
    real32_T *)&(BSER_ahBattCapTemp_CUR[0])), 8U), look1_iflf_binlcapw
    (BSPR_tCellTempMax, ((const real32_T *)&(BSER_tBattCapTemp_A[0])), ((const
    real32_T *)&(BSER_ahBattCapTemp_CUR[0])), 8U));
  if (!BSER_BattSoeCalc_B.IniEna) {
    rtb_Subtract1_j = rtb_Product_p - BSER_BattSoeCalc_DW.Delay1_DSTATE_c;
    if (!(fabsf(rtb_Subtract1_j) <= BSER_ahBattCapFilter_C)) {
      if (rtb_Subtract1_j < 0.0F) {
        rtb_Subtract1_j = -1.0F;
      } else {
        if (rtb_Subtract1_j > 0.0F) {
          rtb_Subtract1_j = 1.0F;
        }
      }

      rtb_Product_p = rtb_Subtract1_j * BSER_ahBattCapFilter_C +
        BSER_BattSoeCalc_DW.Delay1_DSTATE_c;
    }

    if (rtb_Product_p > 200.0F) {
      rtb_Product_p = 200.0F;
    } else {
      if (rtb_Product_p < 0.0F) {
        rtb_Product_p = 0.0F;
      }
    }
  }

  if (rtb_Product_p > 500.0F) {
    BSER_ahBattCap = 500.0F;
  } else if (rtb_Product_p < 1.0F) {
    BSER_ahBattCap = 1.0F;
  } else {
    BSER_ahBattCap = rtb_Product_p;
  }

  BSER_BattSoeCalc_DW.Delay1_DSTATE_c = rtb_Product_p;
  BSER_eBattNrmEnrg = BSER_uBattNrmVolt_C * BSER_ahBattNrmCap_C * 0.001F;
  rtb_Gain1 = 0.01F * BSER_rSocDspl;
  rtb_Subtract1_j = BSER_rSoeUsableRate_C * BSER_ahBattCap * rtb_Gain1;
  rtb_Product_p = rtb_Subtract1_j * BSPR_uBattVolt * 0.001F;
  rtb_Subtract1_j = look1_iflf_binlcapw(0.01F * BSER_rSoc, ((const real32_T *)
    &(BSER_rOCVSoc_A[0])), ((const real32_T *)&(BSER_uOCVSoc_CUR[0])), 20U) *
    (real32_T)BSER_numSoeSeriesCells_C * rtb_Subtract1_j * 0.001F;
  rtb_Gain1 *= BSER_rSoeUsableRate_C * BSER_eBattNrmEnrg;
  if (!BSER_BattSoeCalc_B.IniEna) {
    rtb_Subtract1 = rtb_Gain1 - BSER_BattSoeCalc_DW.Delay2_DSTATE;
    if (!(fabsf(rtb_Subtract1) <= BSER_eBattSoeFilter_C)) {
      if (rtb_Subtract1 < 0.0F) {
        rtb_Subtract1 = -1.0F;
      } else {
        if (rtb_Subtract1 > 0.0F) {
          rtb_Subtract1 = 1.0F;
        }
      }

      rtb_Gain1 = rtb_Subtract1 * BSER_eBattSoeFilter_C +
        BSER_BattSoeCalc_DW.Delay2_DSTATE;
    }

    if (rtb_Gain1 > 200.0F) {
      rtb_Gain1 = 200.0F;
    } else {
      if (rtb_Gain1 < 0.0F) {
        rtb_Gain1 = 0.0F;
      }
    }

    rtb_Subtract1 = rtb_Product_p - BSER_BattSoeCalc_DW.Delay3_DSTATE;
    if (!(fabsf(rtb_Subtract1) <= BSER_eBattSoeFilter_C)) {
      if (rtb_Subtract1 < 0.0F) {
        rtb_Subtract1 = -1.0F;
      } else {
        if (rtb_Subtract1 > 0.0F) {
          rtb_Subtract1 = 1.0F;
        }
      }

      rtb_Product_p = rtb_Subtract1 * BSER_eBattSoeFilter_C +
        BSER_BattSoeCalc_DW.Delay3_DSTATE;
    }

    if (rtb_Product_p > 200.0F) {
      rtb_Product_p = 200.0F;
    } else {
      if (rtb_Product_p < 0.0F) {
        rtb_Product_p = 0.0F;
      }
    }
  }

  if (rtb_Gain1 > 500.0F) {
    BSER_eSoe = 500.0F;
  } else if (rtb_Gain1 < 0.0F) {
    BSER_eSoe = 0.0F;
  } else {
    BSER_eSoe = rtb_Gain1;
  }

  if (rtb_Product_p > 500.0F) {
    BSER_eSoeUbatt = 500.0F;
  } else if (rtb_Product_p < 0.0F) {
    BSER_eSoeUbatt = 0.0F;
  } else {
    BSER_eSoeUbatt = rtb_Product_p;
  }

  if (!BSER_BattSoeCalc_B.IniEna) {
    rtb_Subtract1 = rtb_Subtract1_j - BSER_BattSoeCalc_DW.Delay1_DSTATE;
    if (!(fabsf(rtb_Subtract1) <= BSER_eBattSoeFilter_C)) {
      if (rtb_Subtract1 < 0.0F) {
        rtb_Subtract1 = -1.0F;
      } else {
        if (rtb_Subtract1 > 0.0F) {
          rtb_Subtract1 = 1.0F;
        }
      }

      rtb_Subtract1_j = rtb_Subtract1 * BSER_eBattSoeFilter_C +
        BSER_BattSoeCalc_DW.Delay1_DSTATE;
    }

    if (rtb_Subtract1_j > 200.0F) {
      rtb_Subtract1_j = 200.0F;
    } else {
      if (rtb_Subtract1_j < 0.0F) {
        rtb_Subtract1_j = 0.0F;
      }
    }
  }

  if (rtb_Subtract1_j > 500.0F) {
    BSER_eSoeOcv = 500.0F;
  } else if (rtb_Subtract1_j < 0.0F) {
    BSER_eSoeOcv = 0.0F;
  } else {
    BSER_eSoeOcv = rtb_Subtract1_j;
  }

  BSER_BattSoeCalc_DW.Delay1_DSTATE = rtb_Subtract1_j;
  BSER_BattSoeCalc_DW.Delay2_DSTATE = rtb_Gain1;
  BSER_BattSoeCalc_DW.Delay3_DSTATE = rtb_Product_p;
}

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

  /* block I/O */

  /* custom signals */
  BSER_eBattNrmEnrg = 1.0F;
  BSER_eSoe = 1.0F;
  BSER_eSoeUbatt = 1.0F;
  BSER_eSoeOcv = 1.0F;
  BSER_ahBattCap = 1.0F;
}

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