/*
 * Model210_Closedloop.c
 *
 * Real-Time Workshop code generation for Simulink model "Model210_Closedloop.mdl".
 *
 * Model version              : 1.119
 * Real-Time Workshop version : 7.5  (R2010a)  25-Jan-2010
 * C source code generated on : Wed May 04 13:56:01 2011
 *
 * Target selection: rtwin.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: 32-bit Generic
 * Emulation hardware selection:
 *    Differs from embedded hardware (MATLAB Host)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Model210_Closedloop.h"
#include "Model210_Closedloop_private.h"
#include <stdio.h>
#include "Model210_Closedloop_dt.h"

/* Block signals (auto storage) */
BlockIO_Model210_Closedloop Model210_Closedloop_B;

/* Continuous states */
ContinuousStates_Model210_Closedloop Model210_Closedloop_X;

/* Block states (auto storage) */
D_Work_Model210_Closedloop Model210_Closedloop_DWork;

/* Real-time model */
RT_MODEL_Model210_Closedloop Model210_Closedloop_M_;
RT_MODEL_Model210_Closedloop *Model210_Closedloop_M = &Model210_Closedloop_M_;

/*
 * This function updates continuous states using the ODE1 fixed-step
 * solver algorithm
 */
static void rt_ertODEUpdateContinuousStates(RTWSolverInfo *si )
{
  time_T tnew = rtsiGetSolverStopTime(si);
  time_T h = rtsiGetStepSize(si);
  real_T *x = rtsiGetContStates(si);
  ODE1_IntgData *id = (ODE1_IntgData *)rtsiGetSolverData(si);
  real_T *f0 = id->f[0];
  int_T i;
  int_T nXc = 4;
  rtsiSetSimTimeStep(si,MINOR_TIME_STEP);
  rtsiSetdX(si, f0);
  Model210_Closedloop_derivatives();
  rtsiSetT(si, tnew);
  for (i = 0; i < nXc; i++) {
    *x += h * f0[i];
    x++;
  }

  rtsiSetSimTimeStep(si,MAJOR_TIME_STEP);
}

/* Model output function */
void Model210_Closedloop_output(int_T tid)
{
  /* local block i/o variables */
  real_T rtb_scale;
  real_T currentTime;
  real_T tmp[2];
  int32_T tmp_0;
  int32_T tmp_1;
  if (rtmIsMajorTimeStep(Model210_Closedloop_M)) {
    /* set solver stop time */
    if (!(Model210_Closedloop_M->Timing.clockTick0+1)) {
      rtsiSetSolverStopTime(&Model210_Closedloop_M->solverInfo,
                            ((Model210_Closedloop_M->Timing.clockTickH0 + 1) *
        Model210_Closedloop_M->Timing.stepSize0 * 4294967296.0));
    } else {
      rtsiSetSolverStopTime(&Model210_Closedloop_M->solverInfo,
                            ((Model210_Closedloop_M->Timing.clockTick0 + 1) *
        Model210_Closedloop_M->Timing.stepSize0 +
        Model210_Closedloop_M->Timing.clockTickH0 *
        Model210_Closedloop_M->Timing.stepSize0 * 4294967296.0));
    }
  }                                    /* end MajorTimeStep */

  /* Update absolute time of base rate at minor time step */
  if (rtmIsMinorTimeStep(Model210_Closedloop_M)) {
    Model210_Closedloop_M->Timing.t[0] = rtsiGetT
      (&Model210_Closedloop_M->solverInfo);
  }

  /* Clock: '<Root>/Clock' */
  Model210_Closedloop_B.Clock = Model210_Closedloop_M->Timing.t[0];
  if (rtmIsMajorTimeStep(Model210_Closedloop_M)) {
    /* Level2 S-Function Block: '<S3>/S-Function' (ECPDSPDriver) */
    {
      SimStruct *rts = Model210_Closedloop_M->childSfunctions[0];
      sfcnOutputs(rts, 1);
    }

    /* Gain: '<S2>/scale1' incorporates:
     *  Gain: '<S2>/get positions'
     */
    for (tmp_0 = 0; tmp_0 < 2; tmp_0++) {
      tmp[tmp_0] = 0.0;
      for (tmp_1 = 0; tmp_1 < 5; tmp_1++) {
        tmp[tmp_0] += Model210_Closedloop_P.getpositions_Gain[(tmp_1 << 1) +
          tmp_0] * Model210_Closedloop_B.SFunction[tmp_1];
      }

      Model210_Closedloop_B.scale1[tmp_0] = Model210_Closedloop_P.scale1_Gain *
        tmp[tmp_0];
    }
  }

  /* TransferFcn: '<Root>/Prefilter' */
  rtb_scale = Model210_Closedloop_P.Prefilter_C[0]*
    Model210_Closedloop_X.Prefilter_CSTATE[0]
    + Model210_Closedloop_P.Prefilter_C[1]*
    Model210_Closedloop_X.Prefilter_CSTATE[1];

  /* Sum: '<Root>/Sum' */
  Model210_Closedloop_B.Sum = rtb_scale - Model210_Closedloop_B.scale1[1];

  /* TransferFcn: '<Root>/Controller' */
  rtb_scale = Model210_Closedloop_P.Controller_D*Model210_Closedloop_B.Sum;
  rtb_scale += Model210_Closedloop_P.Controller_C[0]*
    Model210_Closedloop_X.Controller_CSTATE[0]
    + Model210_Closedloop_P.Controller_C[1]*
    Model210_Closedloop_X.Controller_CSTATE[1];

  /* Gain: '<S1>/scale' */
  rtb_scale *= Model210_Closedloop_P.scale_Gain;

  /* Saturate: '<S1>/Saturation' */
  Model210_Closedloop_B.Saturation = rt_SATURATE(rtb_scale,
    Model210_Closedloop_P.Saturation_LowerSat,
    Model210_Closedloop_P.Saturation_UpperSat);

  /* Step: '<Root>/Step' */
  currentTime = Model210_Closedloop_M->Timing.t[0];
  if (currentTime < Model210_Closedloop_P.Step_Time) {
    Model210_Closedloop_B.Step = Model210_Closedloop_P.Step_Y0;
  } else {
    Model210_Closedloop_B.Step = Model210_Closedloop_P.Step_YFinal;
  }

  /* tid is required for a uniform function interface.
   * Argument tid is not used in the function. */
  UNUSED_PARAMETER(tid);
}

/* Model update function */
void Model210_Closedloop_update(int_T tid)
{
  if (rtmIsMajorTimeStep(Model210_Closedloop_M)) {
    rt_ertODEUpdateContinuousStates(&Model210_Closedloop_M->solverInfo);
  }

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The absolute time is the multiplication of "clockTick0"
   * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
   * overflow during the application lifespan selected.
   * Timer of this task consists of two 32 bit unsigned integers.
   * The two integers represent the low bits Timing.clockTick0 and the high bits
   * Timing.clockTickH0. When the low bit overflows to 0, the high bits increment.
   */
  if (!(++Model210_Closedloop_M->Timing.clockTick0)) {
    ++Model210_Closedloop_M->Timing.clockTickH0;
  }

  Model210_Closedloop_M->Timing.t[0] = rtsiGetSolverStopTime
    (&Model210_Closedloop_M->solverInfo);
  if (rtmIsMajorTimeStep(Model210_Closedloop_M)) {
    /* Update absolute timer for sample time: [0.004s, 0.0s] */
    /* The "clockTick1" counts the number of times the code of this task has
     * been executed. The absolute time is the multiplication of "clockTick1"
     * and "Timing.stepSize1". Size of "clockTick1" ensures timer will not
     * overflow during the application lifespan selected.
     * Timer of this task consists of two 32 bit unsigned integers.
     * The two integers represent the low bits Timing.clockTick1 and the high bits
     * Timing.clockTickH1. When the low bit overflows to 0, the high bits increment.
     */
    if (!(++Model210_Closedloop_M->Timing.clockTick1)) {
      ++Model210_Closedloop_M->Timing.clockTickH1;
    }

    Model210_Closedloop_M->Timing.t[1] =
      Model210_Closedloop_M->Timing.clockTick1 *
      Model210_Closedloop_M->Timing.stepSize1 +
      Model210_Closedloop_M->Timing.clockTickH1 *
      Model210_Closedloop_M->Timing.stepSize1 * 4294967296.0;
  }

  /* tid is required for a uniform function interface.
   * Argument tid is not used in the function. */
  UNUSED_PARAMETER(tid);
}

/* Derivatives for root system: '<Root>' */
void Model210_Closedloop_derivatives(void)
{
  /* Derivatives for TransferFcn: '<Root>/Prefilter' */
  {
    ((StateDerivatives_Model210_Closedloop *)
      Model210_Closedloop_M->ModelData.derivs)->Prefilter_CSTATE[0] =
      Model210_Closedloop_B.Step;
    ((StateDerivatives_Model210_Closedloop *)
      Model210_Closedloop_M->ModelData.derivs)->Prefilter_CSTATE[0] +=
      (Model210_Closedloop_P.Prefilter_A[0])*
      Model210_Closedloop_X.Prefilter_CSTATE[0]
      + (Model210_Closedloop_P.Prefilter_A[1])*
      Model210_Closedloop_X.Prefilter_CSTATE[1];
    ((StateDerivatives_Model210_Closedloop *)
      Model210_Closedloop_M->ModelData.derivs)->Prefilter_CSTATE[1]=
      Model210_Closedloop_X.Prefilter_CSTATE[0];
  }

  /* Derivatives for TransferFcn: '<Root>/Controller' */
  {
    ((StateDerivatives_Model210_Closedloop *)
      Model210_Closedloop_M->ModelData.derivs)->Controller_CSTATE[0] =
      Model210_Closedloop_B.Sum;
    ((StateDerivatives_Model210_Closedloop *)
      Model210_Closedloop_M->ModelData.derivs)->Controller_CSTATE[0] +=
      (Model210_Closedloop_P.Controller_A[0])*
      Model210_Closedloop_X.Controller_CSTATE[0]
      + (Model210_Closedloop_P.Controller_A[1])*
      Model210_Closedloop_X.Controller_CSTATE[1];
    ((StateDerivatives_Model210_Closedloop *)
      Model210_Closedloop_M->ModelData.derivs)->Controller_CSTATE[1]=
      Model210_Closedloop_X.Controller_CSTATE[0];
  }
}

/* Model initialize function */
void Model210_Closedloop_initialize(boolean_T firstTime)
{
  (void)firstTime;

  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* initialize real-time model */
  (void) memset((void *)Model210_Closedloop_M, 0,
                sizeof(RT_MODEL_Model210_Closedloop));

  {
    /* Setup solver object */
    rtsiSetSimTimeStepPtr(&Model210_Closedloop_M->solverInfo,
                          &Model210_Closedloop_M->Timing.simTimeStep);
    rtsiSetTPtr(&Model210_Closedloop_M->solverInfo, &rtmGetTPtr
                (Model210_Closedloop_M));
    rtsiSetStepSizePtr(&Model210_Closedloop_M->solverInfo,
                       &Model210_Closedloop_M->Timing.stepSize0);
    rtsiSetdXPtr(&Model210_Closedloop_M->solverInfo,
                 &Model210_Closedloop_M->ModelData.derivs);
    rtsiSetContStatesPtr(&Model210_Closedloop_M->solverInfo,
                         &Model210_Closedloop_M->ModelData.contStates);
    rtsiSetNumContStatesPtr(&Model210_Closedloop_M->solverInfo,
      &Model210_Closedloop_M->Sizes.numContStates);
    rtsiSetErrorStatusPtr(&Model210_Closedloop_M->solverInfo,
                          (&rtmGetErrorStatus(Model210_Closedloop_M)));
    rtsiSetRTModelPtr(&Model210_Closedloop_M->solverInfo, Model210_Closedloop_M);
  }

  rtsiSetSimTimeStep(&Model210_Closedloop_M->solverInfo, MAJOR_TIME_STEP);
  Model210_Closedloop_M->ModelData.intgData.f[0] =
    Model210_Closedloop_M->ModelData.odeF[0];
  Model210_Closedloop_M->ModelData.contStates = ((real_T *)
    &Model210_Closedloop_X);
  rtsiSetSolverData(&Model210_Closedloop_M->solverInfo, (void *)
                    &Model210_Closedloop_M->ModelData.intgData);
  rtsiSetSolverName(&Model210_Closedloop_M->solverInfo,"ode1");
  Model210_Closedloop_M->solverInfoPtr = (&Model210_Closedloop_M->solverInfo);

  /* Initialize timing info */
  {
    int_T *mdlTsMap = Model210_Closedloop_M->Timing.sampleTimeTaskIDArray;
    mdlTsMap[0] = 0;
    mdlTsMap[1] = 1;
    Model210_Closedloop_M->Timing.sampleTimeTaskIDPtr = (&mdlTsMap[0]);
    Model210_Closedloop_M->Timing.sampleTimes =
      (&Model210_Closedloop_M->Timing.sampleTimesArray[0]);
    Model210_Closedloop_M->Timing.offsetTimes =
      (&Model210_Closedloop_M->Timing.offsetTimesArray[0]);

    /* task periods */
    Model210_Closedloop_M->Timing.sampleTimes[0] = (0.0);
    Model210_Closedloop_M->Timing.sampleTimes[1] = (0.004);

    /* task offsets */
    Model210_Closedloop_M->Timing.offsetTimes[0] = (0.0);
    Model210_Closedloop_M->Timing.offsetTimes[1] = (0.0);
  }

  rtmSetTPtr(Model210_Closedloop_M, &Model210_Closedloop_M->Timing.tArray[0]);

  {
    int_T *mdlSampleHits = Model210_Closedloop_M->Timing.sampleHitArray;
    mdlSampleHits[0] = 1;
    mdlSampleHits[1] = 1;
    Model210_Closedloop_M->Timing.sampleHits = (&mdlSampleHits[0]);
  }

  rtmSetTFinal(Model210_Closedloop_M, 2.0);
  Model210_Closedloop_M->Timing.stepSize0 = 0.004;
  Model210_Closedloop_M->Timing.stepSize1 = 0.004;

  /* external mode info */
  Model210_Closedloop_M->Sizes.checksums[0] = (1426332317U);
  Model210_Closedloop_M->Sizes.checksums[1] = (400780918U);
  Model210_Closedloop_M->Sizes.checksums[2] = (4000400524U);
  Model210_Closedloop_M->Sizes.checksums[3] = (1417380925U);

  {
    static const sysRanDType rtAlwaysEnabled = SUBSYS_RAN_BC_ENABLE;
    static RTWExtModeInfo rt_ExtModeInfo;
    static const sysRanDType *systemRan[1];
    Model210_Closedloop_M->extModeInfo = (&rt_ExtModeInfo);
    rteiSetSubSystemActiveVectorAddresses(&rt_ExtModeInfo, systemRan);
    systemRan[0] = &rtAlwaysEnabled;
    rteiSetModelMappingInfoPtr(Model210_Closedloop_M->extModeInfo,
      &Model210_Closedloop_M->SpecialInfo.mappingInfo);
    rteiSetChecksumsPtr(Model210_Closedloop_M->extModeInfo,
                        Model210_Closedloop_M->Sizes.checksums);
    rteiSetTPtr(Model210_Closedloop_M->extModeInfo, rtmGetTPtr
                (Model210_Closedloop_M));
  }

  Model210_Closedloop_M->solverInfoPtr = (&Model210_Closedloop_M->solverInfo);
  Model210_Closedloop_M->Timing.stepSize = (0.004);
  rtsiSetFixedStepSize(&Model210_Closedloop_M->solverInfo, 0.004);
  rtsiSetSolverMode(&Model210_Closedloop_M->solverInfo,
                    SOLVER_MODE_SINGLETASKING);

  /* block I/O */
  Model210_Closedloop_M->ModelData.blockIO = ((void *) &Model210_Closedloop_B);

  {
    int_T i;
    for (i = 0; i < 5; i++) {
      Model210_Closedloop_B.SFunction[i] = 0.0;
    }

    Model210_Closedloop_B.Clock = 0.0;
    Model210_Closedloop_B.scale1[0] = 0.0;
    Model210_Closedloop_B.scale1[1] = 0.0;
    Model210_Closedloop_B.Sum = 0.0;
    Model210_Closedloop_B.Saturation = 0.0;
    Model210_Closedloop_B.Step = 0.0;
  }

  /* parameters */
  Model210_Closedloop_M->ModelData.defaultParam = ((real_T *)
    &Model210_Closedloop_P);

  /* states (continuous) */
  {
    real_T *x = (real_T *) &Model210_Closedloop_X;
    Model210_Closedloop_M->ModelData.contStates = (x);
    (void) memset((void *)&Model210_Closedloop_X, 0,
                  sizeof(ContinuousStates_Model210_Closedloop));
  }

  /* states (dwork) */
  Model210_Closedloop_M->Work.dwork = ((void *) &Model210_Closedloop_DWork);
  (void) memset((void *)&Model210_Closedloop_DWork, 0,
                sizeof(D_Work_Model210_Closedloop));

  /* data type transition information */
  {
    static DataTypeTransInfo dtInfo;
    (void) memset((char_T *) &dtInfo, 0,
                  sizeof(dtInfo));
    Model210_Closedloop_M->SpecialInfo.mappingInfo = (&dtInfo);
    dtInfo.numDataTypes = 14;
    dtInfo.dataTypeSizes = &rtDataTypeSizes[0];
    dtInfo.dataTypeNames = &rtDataTypeNames[0];

    /* Block I/O transition table */
    dtInfo.B = &rtBTransTable;

    /* Parameters transition table */
    dtInfo.P = &rtPTransTable;
  }

  /* child S-Function registration */
  {
    RTWSfcnInfo *sfcnInfo = &Model210_Closedloop_M->NonInlinedSFcns.sfcnInfo;
    Model210_Closedloop_M->sfcnInfo = (sfcnInfo);
    rtssSetErrorStatusPtr(sfcnInfo, (&rtmGetErrorStatus(Model210_Closedloop_M)));
    rtssSetNumRootSampTimesPtr(sfcnInfo,
      &Model210_Closedloop_M->Sizes.numSampTimes);
    rtssSetTPtrPtr(sfcnInfo, &rtmGetTPtr(Model210_Closedloop_M));
    rtssSetTStartPtr(sfcnInfo, &rtmGetTStart(Model210_Closedloop_M));
    rtssSetTFinalPtr(sfcnInfo, &rtmGetTFinal(Model210_Closedloop_M));
    rtssSetTimeOfLastOutputPtr(sfcnInfo, &rtmGetTimeOfLastOutput
      (Model210_Closedloop_M));
    rtssSetStepSizePtr(sfcnInfo, &Model210_Closedloop_M->Timing.stepSize);
    rtssSetStopRequestedPtr(sfcnInfo, &rtmGetStopRequested(Model210_Closedloop_M));
    rtssSetDerivCacheNeedsResetPtr(sfcnInfo,
      &Model210_Closedloop_M->ModelData.derivCacheNeedsReset);
    rtssSetZCCacheNeedsResetPtr(sfcnInfo,
      &Model210_Closedloop_M->ModelData.zCCacheNeedsReset);
    rtssSetBlkStateChangePtr(sfcnInfo,
      &Model210_Closedloop_M->ModelData.blkStateChange);
    rtssSetSampleHitsPtr(sfcnInfo, &Model210_Closedloop_M->Timing.sampleHits);
    rtssSetPerTaskSampleHitsPtr(sfcnInfo,
      &Model210_Closedloop_M->Timing.perTaskSampleHits);
    rtssSetSimModePtr(sfcnInfo, &Model210_Closedloop_M->simMode);
    rtssSetSolverInfoPtr(sfcnInfo, &Model210_Closedloop_M->solverInfoPtr);
  }

  Model210_Closedloop_M->Sizes.numSFcns = (1);

  /* register each child */
  {
    (void) memset((void *)
                  &Model210_Closedloop_M->NonInlinedSFcns.childSFunctions[0], 0,
                  1*sizeof(SimStruct));
    Model210_Closedloop_M->childSfunctions =
      (&Model210_Closedloop_M->NonInlinedSFcns.childSFunctionPtrs[0]);
    Model210_Closedloop_M->childSfunctions[0] =
      (&Model210_Closedloop_M->NonInlinedSFcns.childSFunctions[0]);

    /* Level2 S-Function Block: Model210_Closedloop/<S3>/S-Function (ECPDSPDriver) */
    {
      SimStruct *rts = Model210_Closedloop_M->childSfunctions[0];

      /* timing info */
      time_T *sfcnPeriod =
        Model210_Closedloop_M->NonInlinedSFcns.Sfcn0.sfcnPeriod;
      time_T *sfcnOffset =
        Model210_Closedloop_M->NonInlinedSFcns.Sfcn0.sfcnOffset;
      int_T *sfcnTsMap = Model210_Closedloop_M->NonInlinedSFcns.Sfcn0.sfcnTsMap;
      (void) memset((void*)sfcnPeriod, 0,
                    sizeof(time_T)*1);
      (void) memset((void*)sfcnOffset, 0,
                    sizeof(time_T)*1);
      ssSetSampleTimePtr(rts, &sfcnPeriod[0]);
      ssSetOffsetTimePtr(rts, &sfcnOffset[0]);
      ssSetSampleTimeTaskIDPtr(rts, sfcnTsMap);

      /* Set up the mdlInfo pointer */
      {
        ssSetBlkInfo2Ptr(rts, &Model210_Closedloop_M->NonInlinedSFcns.blkInfo2[0]);
      }

      ssSetRTWSfcnInfo(rts, Model210_Closedloop_M->sfcnInfo);

      /* Allocate memory of model methods 2 */
      {
        ssSetModelMethods2(rts, &Model210_Closedloop_M->
                           NonInlinedSFcns.methods2[0]);
      }

      /* Allocate memory of model methods 3 */
      {
        ssSetModelMethods3(rts, &Model210_Closedloop_M->
                           NonInlinedSFcns.methods3[0]);
      }

      /* inputs */
      {
        _ssSetNumInputPorts(rts, 1);
        ssSetPortInfoForInputs(rts,
          &Model210_Closedloop_M->NonInlinedSFcns.Sfcn0.inputPortInfo[0]);

        /* port 0 */
        {
          real_T const **sfcnUPtrs = (real_T const **)
            &Model210_Closedloop_M->NonInlinedSFcns.Sfcn0.UPtrs0;
          sfcnUPtrs[0] = &Model210_Closedloop_B.Saturation;
          sfcnUPtrs[1] = &Model210_Closedloop_P.Dac2Value_Value;
          ssSetInputPortSignalPtrs(rts, 0, (InputPtrsType)&sfcnUPtrs[0]);
          _ssSetInputPortNumDimensions(rts, 0, 1);
          ssSetInputPortWidth(rts, 0, 2);
        }
      }

      /* outputs */
      {
        ssSetPortInfoForOutputs(rts,
          &Model210_Closedloop_M->NonInlinedSFcns.Sfcn0.outputPortInfo[0]);
        _ssSetNumOutputPorts(rts, 1);

        /* port 0 */
        {
          _ssSetOutputPortNumDimensions(rts, 0, 1);
          ssSetOutputPortWidth(rts, 0, 5);
          ssSetOutputPortSignal(rts, 0, ((real_T *)
            Model210_Closedloop_B.SFunction));
        }
      }

      /* path info */
      ssSetModelName(rts, "S-Function");
      ssSetPath(rts, "Model210_Closedloop/ECP Model 210/Subsystem/S-Function");
      ssSetRTModel(rts,Model210_Closedloop_M);
      ssSetParentSS(rts, (NULL));
      ssSetRootSS(rts, rts);
      ssSetVersion(rts, SIMSTRUCT_VERSION_LEVEL2);

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &Model210_Closedloop_M->NonInlinedSFcns.Sfcn0.params;
        ssSetSFcnParamsCount(rts, 4);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)
                       &Model210_Closedloop_P.SFunction_P1_Size[0]);
        ssSetSFcnParam(rts, 1, (mxArray*)
                       &Model210_Closedloop_P.SFunction_P2_Size[0]);
        ssSetSFcnParam(rts, 2, (mxArray*)
                       &Model210_Closedloop_P.SFunction_P3_Size[0]);
        ssSetSFcnParam(rts, 3, (mxArray*)
                       &Model210_Closedloop_P.SFunction_P4_Size[0]);
      }

      /* registration */
      ECPDSPDriver(rts);
      sfcnInitializeSizes(rts);
      sfcnInitializeSampleTimes(rts);

      /* adjust sample time */
      ssSetSampleTime(rts, 0, 0.004);
      ssSetOffsetTime(rts, 0, 0.0);
      sfcnTsMap[0] = 1;

      /* set compiled values of dynamic vector attributes */
      ssSetNumNonsampledZCs(rts, 0);

      /* Update connectivity flags for each port */
      _ssSetInputPortConnected(rts, 0, 1);
      _ssSetOutputPortConnected(rts, 0, 1);
      _ssSetOutputPortBeingMerged(rts, 0, 0);

      /* Update the BufferDstPort flags for each input port */
      ssSetInputPortBufferDstPort(rts, 0, -1);
    }
  }
}

/* Model terminate function */
void Model210_Closedloop_terminate(void)
{
  /* Level2 S-Function Block: '<S3>/S-Function' (ECPDSPDriver) */
  {
    SimStruct *rts = Model210_Closedloop_M->childSfunctions[0];
    sfcnTerminate(rts);
  }
}

/*========================================================================*
 * Start of GRT compatible call interface                                 *
 *========================================================================*/

/* Solver interface called by GRT_Main */
#ifndef USE_GENERATED_SOLVER

void rt_ODECreateIntegrationData(RTWSolverInfo *si)
{
  UNUSED_PARAMETER(si);
  return;
}                                      /* do nothing */

void rt_ODEDestroyIntegrationData(RTWSolverInfo *si)
{
  UNUSED_PARAMETER(si);
  return;
}                                      /* do nothing */

void rt_ODEUpdateContinuousStates(RTWSolverInfo *si)
{
  UNUSED_PARAMETER(si);
  return;
}                                      /* do nothing */

#endif

void MdlOutputs(int_T tid)
{
  Model210_Closedloop_output(tid);
}

void MdlUpdate(int_T tid)
{
  Model210_Closedloop_update(tid);
}

void MdlInitializeSizes(void)
{
  Model210_Closedloop_M->Sizes.numContStates = (4);/* Number of continuous states */
  Model210_Closedloop_M->Sizes.numY = (0);/* Number of model outputs */
  Model210_Closedloop_M->Sizes.numU = (0);/* Number of model inputs */
  Model210_Closedloop_M->Sizes.sysDirFeedThru = (0);/* The model is not direct feedthrough */
  Model210_Closedloop_M->Sizes.numSampTimes = (2);/* Number of sample times */
  Model210_Closedloop_M->Sizes.numBlocks = (14);/* Number of blocks */
  Model210_Closedloop_M->Sizes.numBlockIO = (6);/* Number of block outputs */
  Model210_Closedloop_M->Sizes.numBlockPrms = (44);/* Sum of parameter "widths" */
}

void MdlInitializeSampleTimes(void)
{
}

void MdlInitialize(void)
{
  /* InitializeConditions for TransferFcn: '<Root>/Prefilter' */
  Model210_Closedloop_X.Prefilter_CSTATE[0] = 0.0;
  Model210_Closedloop_X.Prefilter_CSTATE[1] = 0.0;

  /* InitializeConditions for TransferFcn: '<Root>/Controller' */
  Model210_Closedloop_X.Controller_CSTATE[0] = 0.0;
  Model210_Closedloop_X.Controller_CSTATE[1] = 0.0;
}

void MdlStart(void)
{
  /* Level2 S-Function Block: '<S3>/S-Function' (ECPDSPDriver) */
  {
    SimStruct *rts = Model210_Closedloop_M->childSfunctions[0];
    sfcnStart(rts);
    if (ssGetErrorStatus(rts) != (NULL))
      return;
  }

  MdlInitialize();
}

void MdlTerminate(void)
{
  Model210_Closedloop_terminate();
}

RT_MODEL_Model210_Closedloop *Model210_Closedloop(void)
{
  Model210_Closedloop_initialize(1);
  return Model210_Closedloop_M;
}

/*========================================================================*
 * End of GRT compatible call interface                                   *
 *========================================================================*/
