/*
 * Model210_DT_sv1.c
 *
 * Real-Time Workshop code generation for Simulink model "Model210_DT_sv1.mdl".
 *
 * Model version              : 1.152
 * Real-Time Workshop version : 7.5  (R2010a)  25-Jan-2010
 * C source code generated on : Mon May 09 14:54:45 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_DT_sv1.h"
#include "Model210_DT_sv1_private.h"
#include <stdio.h>
#include "Model210_DT_sv1_dt.h"

/* Block signals (auto storage) */
BlockIO_Model210_DT_sv1 Model210_DT_sv1_B;

/* Block states (auto storage) */
D_Work_Model210_DT_sv1 Model210_DT_sv1_DWork;

/* Real-time model */
RT_MODEL_Model210_DT_sv1 Model210_DT_sv1_M_;
RT_MODEL_Model210_DT_sv1 *Model210_DT_sv1_M = &Model210_DT_sv1_M_;

/* Model output function */
void Model210_DT_sv1_output(int_T tid)
{
  /* local block i/o variables */
  real_T rtb_TmpSignalConversionAtCInpor[3];
  real_T rtb_scale;
  real_T currentTime;
  real_T tmp[3];
  int32_T tmp_0;
  real_T tmp_1[8];
  real_T tmp_2[8];
  int32_T tmp_3;

  /* Level2 S-Function Block: '<S3>/S-Function' (ECPDSPDriver) */
  {
    SimStruct *rts = Model210_DT_sv1_M->childSfunctions[0];
    sfcnOutputs(rts, 1);
  }

  /* Gain: '<S2>/scale1' incorporates:
   *  Gain: '<S2>/get positions'
   */
  for (tmp_0 = 0; tmp_0 < 3; tmp_0++) {
    tmp[tmp_0] = 0.0;
    for (tmp_3 = 0; tmp_3 < 5; tmp_3++) {
      tmp[tmp_0] += Model210_DT_sv1_P.getpositions_Gain[3 * tmp_3 + tmp_0] *
        Model210_DT_sv1_B.SFunction[tmp_3];
    }

    Model210_DT_sv1_B.scale1[tmp_0] = Model210_DT_sv1_P.scale1_Gain * tmp[tmp_0];
  }

  /* Derivative: '<S2>/Derivative' */
  {
    real_T t = Model210_DT_sv1_M->Timing.t[0];
    real_T timeStampA = Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampA;
    real_T timeStampB = Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampB;
    real_T *lastU = &Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeA[0];
    if (timeStampA >= t && timeStampB >= t) {
      rtb_TmpSignalConversionAtCInpor[0] = 0.0;
      rtb_TmpSignalConversionAtCInpor[1] = 0.0;
      rtb_TmpSignalConversionAtCInpor[2] = 0.0;
    } else {
      real_T deltaT;
      real_T lastTime = timeStampA;
      if (timeStampA < timeStampB) {
        if (timeStampB < t) {
          lastTime = timeStampB;
          lastU = &Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeB[0];
        }
      } else if (timeStampA >= t) {
        lastTime = timeStampB;
        lastU = &Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeB[0];
      }

      deltaT = t - lastTime;
      rtb_TmpSignalConversionAtCInpor[0] = (Model210_DT_sv1_B.scale1[0] - *lastU
        ++) / deltaT;
      rtb_TmpSignalConversionAtCInpor[1] = (Model210_DT_sv1_B.scale1[1] - *lastU
        ++) / deltaT;
      rtb_TmpSignalConversionAtCInpor[2] = (Model210_DT_sv1_B.scale1[2] - *lastU
        ++) / deltaT;
    }
  }

  /* Gain: '<S2>/scale2' incorporates:
   *  Gain: '<S2>/get angles'
   */
  currentTime = 0.0;
  for (tmp_0 = 0; tmp_0 < 5; tmp_0++) {
    currentTime += Model210_DT_sv1_P.getangles_Gain[tmp_0] *
      Model210_DT_sv1_B.SFunction[tmp_0];
  }

  Model210_DT_sv1_B.scale2 = Model210_DT_sv1_P.scale2_Gain * currentTime;

  /* Derivative: '<S2>/Derivative2' */
  {
    real_T t = Model210_DT_sv1_M->Timing.t[0];
    real_T timeStampA = Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampA;
    real_T timeStampB = Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampB;
    real_T *lastU = &Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeA;
    if (timeStampA >= t && timeStampB >= t) {
      rtb_scale = 0.0;
    } else {
      real_T deltaT;
      real_T lastTime = timeStampA;
      if (timeStampA < timeStampB) {
        if (timeStampB < t) {
          lastTime = timeStampB;
          lastU = &Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeB;
        }
      } else if (timeStampA >= t) {
        lastTime = timeStampB;
        lastU = &Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeB;
      }

      deltaT = t - lastTime;
      rtb_scale = (Model210_DT_sv1_B.scale2 - *lastU++) / deltaT;
    }
  }

  /* Gain: '<S2>/get  desired states' incorporates:
   *  Gain: '<S2>/get all  states'
   *  SignalConversion: '<S2>/TmpSignal ConversionAtget all  statesInport1'
   */
  tmp_1[0] = Model210_DT_sv1_B.scale1[0];
  tmp_1[1] = Model210_DT_sv1_B.scale1[1];
  tmp_1[2] = Model210_DT_sv1_B.scale1[2];
  tmp_1[3] = rtb_TmpSignalConversionAtCInpor[0];
  tmp_1[4] = rtb_TmpSignalConversionAtCInpor[1];
  tmp_1[5] = rtb_TmpSignalConversionAtCInpor[2];
  tmp_1[6] = Model210_DT_sv1_B.scale2;
  tmp_1[7] = rtb_scale;
  for (tmp_0 = 0; tmp_0 < 8; tmp_0++) {
    tmp_2[tmp_0] = 0.0;
    for (tmp_3 = 0; tmp_3 < 8; tmp_3++) {
      tmp_2[tmp_0] += Model210_DT_sv1_P.getallstates_Gain[(tmp_3 << 3) + tmp_0] *
        tmp_1[tmp_3];
    }
  }

  /* UnitDelay: '<S1>/Unit Delay' */
  Model210_DT_sv1_B.UnitDelay = Model210_DT_sv1_DWork.UnitDelay_DSTATE;

  /* SignalConversion: '<Root>/TmpSignal ConversionAtCInport1' */
  for (tmp_0 = 0; tmp_0 < 2; tmp_0++) {
    Model210_DT_sv1_B.getdesiredstates[tmp_0] = 0.0;
    for (tmp_3 = 0; tmp_3 < 8; tmp_3++) {
      Model210_DT_sv1_B.getdesiredstates[tmp_0] =
        Model210_DT_sv1_P.getdesiredstates_Gain[(tmp_3 << 1) + tmp_0] *
        tmp_2[tmp_3] + Model210_DT_sv1_B.getdesiredstates[tmp_0];
    }

    rtb_TmpSignalConversionAtCInpor[tmp_0] =
      Model210_DT_sv1_B.getdesiredstates[tmp_0];
  }

  rtb_TmpSignalConversionAtCInpor[2] = Model210_DT_sv1_B.UnitDelay;

  /* Gain: '<Root>/C' */
  Model210_DT_sv1_B.C = (Model210_DT_sv1_P.C_Gain[0] *
    rtb_TmpSignalConversionAtCInpor[0] + Model210_DT_sv1_P.C_Gain[1] *
    rtb_TmpSignalConversionAtCInpor[1]) + Model210_DT_sv1_P.C_Gain[2] *
    rtb_TmpSignalConversionAtCInpor[2];

  /* Step: '<Root>/Input' */
  currentTime = Model210_DT_sv1_M->Timing.t[1];
  if (currentTime < Model210_DT_sv1_P.Input_Time) {
    currentTime = Model210_DT_sv1_P.Input_Y0;
  } else {
    currentTime = Model210_DT_sv1_P.Input_YFinal;
  }

  /* Gain: '<Root>/Gpf' */
  Model210_DT_sv1_B.Gpf = Model210_DT_sv1_P.Gpf_Gain * currentTime;

  /* Gain: '<Root>/K' */
  rtb_scale = (Model210_DT_sv1_P.K_Gain[0] * rtb_TmpSignalConversionAtCInpor[0]
               + Model210_DT_sv1_P.K_Gain[1] * rtb_TmpSignalConversionAtCInpor[1])
    + Model210_DT_sv1_P.K_Gain[2] * rtb_TmpSignalConversionAtCInpor[2];

  /* Sum: '<Root>/Sum' */
  Model210_DT_sv1_B.Sum = Model210_DT_sv1_B.Gpf - rtb_scale;

  /* Gain: '<S1>/scale' */
  rtb_scale = Model210_DT_sv1_P.scale_Gain * Model210_DT_sv1_B.Sum;

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

  /* DigitalClock: '<Root>/Digital Clock' */
  Model210_DT_sv1_B.DigitalClock = Model210_DT_sv1_M->Timing.t[1];

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

/* Model update function */
void Model210_DT_sv1_update(int_T tid)
{
  /* Update for Derivative: '<S2>/Derivative' */
  {
    real_T timeStampA = Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampA;
    real_T timeStampB = Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampB;
    real_T* lastTime = &Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampA;
    real_T* lastU = &Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeA[0];
    if (timeStampA != rtInf) {
      if (timeStampB == rtInf) {
        lastTime = &Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampB;
        lastU = &Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeB[0];
      } else if (timeStampA >= timeStampB) {
        lastTime = &Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampB;
        lastU = &Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeB[0];
      }
    }

    *lastTime = Model210_DT_sv1_M->Timing.t[0];
    *lastU++ = Model210_DT_sv1_B.scale1[0];
    *lastU++ = Model210_DT_sv1_B.scale1[1];
    *lastU++ = Model210_DT_sv1_B.scale1[2];
  }

  /* Update for Derivative: '<S2>/Derivative2' */
  {
    real_T timeStampA = Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampA;
    real_T timeStampB = Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampB;
    real_T* lastTime = &Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampA;
    real_T* lastU = &Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeA;
    if (timeStampA != rtInf) {
      if (timeStampB == rtInf) {
        lastTime = &Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampB;
        lastU = &Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeB;
      } else if (timeStampA >= timeStampB) {
        lastTime = &Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampB;
        lastU = &Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeB;
      }
    }

    *lastTime = Model210_DT_sv1_M->Timing.t[0];
    *lastU++ = Model210_DT_sv1_B.scale2;
  }

  /* Update for UnitDelay: '<S1>/Unit Delay' */
  Model210_DT_sv1_DWork.UnitDelay_DSTATE = Model210_DT_sv1_B.Sum;

  /* 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_DT_sv1_M->Timing.clockTick0)) {
    ++Model210_DT_sv1_M->Timing.clockTickH0;
  }

  Model210_DT_sv1_M->Timing.t[0] = Model210_DT_sv1_M->Timing.clockTick0 *
    Model210_DT_sv1_M->Timing.stepSize0 + Model210_DT_sv1_M->Timing.clockTickH0 *
    Model210_DT_sv1_M->Timing.stepSize0 * 4294967296.0;

  {
    /* Update absolute timer for sample time: [0.02s, 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_DT_sv1_M->Timing.clockTick1)) {
      ++Model210_DT_sv1_M->Timing.clockTickH1;
    }

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

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

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

  /* Registration code */

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

  /* initialize real-time model */
  (void) memset((void *)Model210_DT_sv1_M, 0,
                sizeof(RT_MODEL_Model210_DT_sv1));

  {
    /* Setup solver object */
    rtsiSetSimTimeStepPtr(&Model210_DT_sv1_M->solverInfo,
                          &Model210_DT_sv1_M->Timing.simTimeStep);
    rtsiSetTPtr(&Model210_DT_sv1_M->solverInfo, &rtmGetTPtr(Model210_DT_sv1_M));
    rtsiSetStepSizePtr(&Model210_DT_sv1_M->solverInfo,
                       &Model210_DT_sv1_M->Timing.stepSize0);
    rtsiSetErrorStatusPtr(&Model210_DT_sv1_M->solverInfo, (&rtmGetErrorStatus
      (Model210_DT_sv1_M)));
    rtsiSetRTModelPtr(&Model210_DT_sv1_M->solverInfo, Model210_DT_sv1_M);
  }

  rtsiSetSimTimeStep(&Model210_DT_sv1_M->solverInfo, MAJOR_TIME_STEP);
  rtsiSetSolverName(&Model210_DT_sv1_M->solverInfo,"FixedStepDiscrete");
  Model210_DT_sv1_M->solverInfoPtr = (&Model210_DT_sv1_M->solverInfo);

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

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

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

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

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

  rtmSetTFinal(Model210_DT_sv1_M, 1.5);
  Model210_DT_sv1_M->Timing.stepSize0 = 0.02;
  Model210_DT_sv1_M->Timing.stepSize1 = 0.02;

  /* external mode info */
  Model210_DT_sv1_M->Sizes.checksums[0] = (2515227681U);
  Model210_DT_sv1_M->Sizes.checksums[1] = (2459980648U);
  Model210_DT_sv1_M->Sizes.checksums[2] = (1684864911U);
  Model210_DT_sv1_M->Sizes.checksums[3] = (3589337226U);

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

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

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

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

    Model210_DT_sv1_B.scale1[0] = 0.0;
    Model210_DT_sv1_B.scale1[1] = 0.0;
    Model210_DT_sv1_B.scale1[2] = 0.0;
    Model210_DT_sv1_B.scale2 = 0.0;
    Model210_DT_sv1_B.getdesiredstates[0] = 0.0;
    Model210_DT_sv1_B.getdesiredstates[1] = 0.0;
    Model210_DT_sv1_B.UnitDelay = 0.0;
    Model210_DT_sv1_B.C = 0.0;
    Model210_DT_sv1_B.Gpf = 0.0;
    Model210_DT_sv1_B.Sum = 0.0;
    Model210_DT_sv1_B.Saturation = 0.0;
    Model210_DT_sv1_B.DigitalClock = 0.0;
  }

  /* parameters */
  Model210_DT_sv1_M->ModelData.defaultParam = ((real_T *)&Model210_DT_sv1_P);

  /* states (dwork) */
  Model210_DT_sv1_M->Work.dwork = ((void *) &Model210_DT_sv1_DWork);
  (void) memset((void *)&Model210_DT_sv1_DWork, 0,
                sizeof(D_Work_Model210_DT_sv1));
  Model210_DT_sv1_DWork.UnitDelay_DSTATE = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampA = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeA[0] = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeA[1] = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeA[2] = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampB = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeB[0] = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeB[1] = 0.0;
  Model210_DT_sv1_DWork.Derivative_RWORK.LastUAtTimeB[2] = 0.0;
  Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampA = 0.0;
  Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeA = 0.0;
  Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampB = 0.0;
  Model210_DT_sv1_DWork.Derivative2_RWORK.LastUAtTimeB = 0.0;

  /* data type transition information */
  {
    static DataTypeTransInfo dtInfo;
    (void) memset((char_T *) &dtInfo, 0,
                  sizeof(dtInfo));
    Model210_DT_sv1_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_DT_sv1_M->NonInlinedSFcns.sfcnInfo;
    Model210_DT_sv1_M->sfcnInfo = (sfcnInfo);
    rtssSetErrorStatusPtr(sfcnInfo, (&rtmGetErrorStatus(Model210_DT_sv1_M)));
    rtssSetNumRootSampTimesPtr(sfcnInfo, &Model210_DT_sv1_M->Sizes.numSampTimes);
    rtssSetTPtrPtr(sfcnInfo, &rtmGetTPtr(Model210_DT_sv1_M));
    rtssSetTStartPtr(sfcnInfo, &rtmGetTStart(Model210_DT_sv1_M));
    rtssSetTFinalPtr(sfcnInfo, &rtmGetTFinal(Model210_DT_sv1_M));
    rtssSetTimeOfLastOutputPtr(sfcnInfo, &rtmGetTimeOfLastOutput
      (Model210_DT_sv1_M));
    rtssSetStepSizePtr(sfcnInfo, &Model210_DT_sv1_M->Timing.stepSize);
    rtssSetStopRequestedPtr(sfcnInfo, &rtmGetStopRequested(Model210_DT_sv1_M));
    rtssSetDerivCacheNeedsResetPtr(sfcnInfo,
      &Model210_DT_sv1_M->ModelData.derivCacheNeedsReset);
    rtssSetZCCacheNeedsResetPtr(sfcnInfo,
      &Model210_DT_sv1_M->ModelData.zCCacheNeedsReset);
    rtssSetBlkStateChangePtr(sfcnInfo,
      &Model210_DT_sv1_M->ModelData.blkStateChange);
    rtssSetSampleHitsPtr(sfcnInfo, &Model210_DT_sv1_M->Timing.sampleHits);
    rtssSetPerTaskSampleHitsPtr(sfcnInfo,
      &Model210_DT_sv1_M->Timing.perTaskSampleHits);
    rtssSetSimModePtr(sfcnInfo, &Model210_DT_sv1_M->simMode);
    rtssSetSolverInfoPtr(sfcnInfo, &Model210_DT_sv1_M->solverInfoPtr);
  }

  Model210_DT_sv1_M->Sizes.numSFcns = (1);

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

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

      /* timing info */
      time_T *sfcnPeriod = Model210_DT_sv1_M->NonInlinedSFcns.Sfcn0.sfcnPeriod;
      time_T *sfcnOffset = Model210_DT_sv1_M->NonInlinedSFcns.Sfcn0.sfcnOffset;
      int_T *sfcnTsMap = Model210_DT_sv1_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_DT_sv1_M->NonInlinedSFcns.blkInfo2[0]);
      }

      ssSetRTWSfcnInfo(rts, Model210_DT_sv1_M->sfcnInfo);

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

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

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

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

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

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

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

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

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

      /* adjust sample time */
      ssSetSampleTime(rts, 0, 0.02);
      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_DT_sv1_terminate(void)
{
  /* Level2 S-Function Block: '<S3>/S-Function' (ECPDSPDriver) */
  {
    SimStruct *rts = Model210_DT_sv1_M->childSfunctions[0];
    sfcnTerminate(rts);
  }
}

/*========================================================================*
 * Start of GRT compatible call interface                                 *
 *========================================================================*/
void MdlOutputs(int_T tid)
{
  Model210_DT_sv1_output(tid);
}

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

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

void MdlInitializeSampleTimes(void)
{
}

void MdlInitialize(void)
{
  /* InitializeConditions for Derivative: '<S2>/Derivative' */
  Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampA = rtInf;
  Model210_DT_sv1_DWork.Derivative_RWORK.TimeStampB = rtInf;

  /* InitializeConditions for Derivative: '<S2>/Derivative2' */
  Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampA = rtInf;
  Model210_DT_sv1_DWork.Derivative2_RWORK.TimeStampB = rtInf;

  /* InitializeConditions for UnitDelay: '<S1>/Unit Delay' */
  Model210_DT_sv1_DWork.UnitDelay_DSTATE = Model210_DT_sv1_P.UnitDelay_X0;
}

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

  /* Start for UnitDelay: '<S1>/Unit Delay' */
  Model210_DT_sv1_B.UnitDelay = Model210_DT_sv1_P.UnitDelay_X0;
  MdlInitialize();
}

void MdlTerminate(void)
{
  Model210_DT_sv1_terminate();
}

RT_MODEL_Model210_DT_sv1 *Model210_DT_sv1(void)
{
  Model210_DT_sv1_initialize(1);
  return Model210_DT_sv1_M;
}

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