/*
 * Model205_Openloop.c
 *
 * Real-Time Workshop code generation for Simulink model "Model205_Openloop.mdl".
 *
 * Model Version              : 1.120
 * Real-Time Workshop version : 7.1  (R2008a)  23-Jan-2008
 * C source code generated on : Fri Sep 04 09:27:35 2009
 */

#include "Model205_Openloop.h"
#include "Model205_Openloop_private.h"
#include <stdio.h>
#include "Model205_Openloop_dt.h"

/* Block signals (auto storage) */
BlockIO_Model205_Openloop Model205_Openloop_B;

/* Block states (auto storage) */
D_Work_Model205_Openloop Model205_Openloop_DWork;

/* Real-time model */
RT_MODEL_Model205_Openloop Model205_Openloop_M_;
RT_MODEL_Model205_Openloop *Model205_Openloop_M = &Model205_Openloop_M_;

/* Model output function */
void Model205_Openloop_output(int_T tid)
{
  /* local block i/o variables */
  real_T rtb_scale1[2];
  real_T rtb_scale;

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

  /* Gain Block: '<S3>/get positions'
   * About '<S3>/get positions :'
   *   Gain value: [0 1 0 0 0; 1 0 0 0 0 ]


   */
  {
    static const int_T dims[3] = { 2, 5, 1 };

    rt_MatMultRR_Dbl((real_T *)rtb_scale1, (real_T *)
                     &Model205_Openloop_P.getpositions_Gain[0],
                     (real_T *)Model205_Openloop_B.SFunction, &dims[0]);
  }

  /* Gain: '<S3>/scale1' */
  rtb_scale1[0] *= Model205_Openloop_P.scale1_Gain;
  rtb_scale1[1] *= Model205_Openloop_P.scale1_Gain;

  /* Gain: '<Root>/to degrees1' */
  Model205_Openloop_B.todegrees1 = Model205_Openloop_P.todegrees1_Gain *
    rtb_scale1[0];

  /* Gain: '<Root>/to degrees' */
  Model205_Openloop_B.todegrees = Model205_Openloop_P.todegrees_Gain *
    rtb_scale1[1];

  /* Clock: '<Root>/Clock' */
  Model205_Openloop_B.Clock = Model205_Openloop_M->Timing.t[0];

  /* Gain: '<S1>/scale' incorporates:
   *  Sin: '<Root>/Sine Wave'
   */
  rtb_scale = (sin(Model205_Openloop_P.SineWave_Freq *
                   Model205_Openloop_M->Timing.t[0] +
                   Model205_Openloop_P.SineWave_Phase) *
               Model205_Openloop_P.SineWave_Amp +
               Model205_Openloop_P.SineWave_Bias) *
    Model205_Openloop_P.scale_Gain;

  /* Saturate: '<S1>/Saturation' */
  Model205_Openloop_B.Saturation = rt_SATURATE(rtb_scale,
    Model205_Openloop_P.Saturation_LowerSat,
    Model205_Openloop_P.Saturation_UpperSat);
  UNUSED_PARAMETER(tid);
}

/* Model update function */
void Model205_Openloop_update(int_T tid)
{
  /* Update absolute time for base rate */
  if (!(++Model205_Openloop_M->Timing.clockTick0))
    ++Model205_Openloop_M->Timing.clockTickH0;
  Model205_Openloop_M->Timing.t[0] = Model205_Openloop_M->Timing.clockTick0 *
    Model205_Openloop_M->Timing.stepSize0 +
    Model205_Openloop_M->Timing.clockTickH0 *
    Model205_Openloop_M->Timing.stepSize0 * 4294967296.0;

  {
    /* Update absolute timer for sample time: [0.004s, 0.0s] */
    if (!(++Model205_Openloop_M->Timing.clockTick1))
      ++Model205_Openloop_M->Timing.clockTickH1;
    Model205_Openloop_M->Timing.t[1] = Model205_Openloop_M->Timing.clockTick1 *
      Model205_Openloop_M->Timing.stepSize1 +
      Model205_Openloop_M->Timing.clockTickH1 *
      Model205_Openloop_M->Timing.stepSize1 * 4294967296.0;
  }

  UNUSED_PARAMETER(tid);
}

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

  /* Registration code */

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

  /* initialize real-time model */
  (void) memset((char_T *)Model205_Openloop_M,0,
                sizeof(RT_MODEL_Model205_Openloop));

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

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

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

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

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

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

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

  rtmSetTFinal(Model205_Openloop_M, 90.0);
  Model205_Openloop_M->Timing.stepSize0 = 0.004;
  Model205_Openloop_M->Timing.stepSize1 = 0.004;

  /* external mode info */
  Model205_Openloop_M->Sizes.checksums[0] = (2231903681U);
  Model205_Openloop_M->Sizes.checksums[1] = (1955763724U);
  Model205_Openloop_M->Sizes.checksums[2] = (2924759398U);
  Model205_Openloop_M->Sizes.checksums[3] = (793830382U);

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

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

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

  {
    int_T i;
    void *pVoidBlockIORegion;
    pVoidBlockIORegion = (void *)(&Model205_Openloop_B.SFunction[0]);
    for (i = 0; i < 9; i++) {
      ((real_T*)pVoidBlockIORegion)[i] = 0.0;
    }
  }

  /* parameters */
  Model205_Openloop_M->ModelData.defaultParam = ((real_T *) &Model205_Openloop_P);

  /* states (dwork) */
  Model205_Openloop_M->Work.dwork = ((void *) &Model205_Openloop_DWork);
  (void) memset((char_T *) &Model205_Openloop_DWork,0,
                sizeof(D_Work_Model205_Openloop));

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

  Model205_Openloop_M->Sizes.numSFcns = (1);

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

    /* Level2 S-Function Block: Model205_Openloop/<S4>/S-Function (ECPDSPDriver) */
    {
      SimStruct *rts = Model205_Openloop_M->childSfunctions[0];

      /* timing info */
      time_T *sfcnPeriod = Model205_Openloop_M->NonInlinedSFcns.Sfcn0.sfcnPeriod;
      time_T *sfcnOffset = Model205_Openloop_M->NonInlinedSFcns.Sfcn0.sfcnOffset;
      int_T *sfcnTsMap = Model205_Openloop_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, &Model205_Openloop_M->NonInlinedSFcns.blkInfo2[0]);
        ssSetRTWSfcnInfo(rts, Model205_Openloop_M->sfcnInfo);
      }

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

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

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

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

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

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

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &Model205_Openloop_M->NonInlinedSFcns.Sfcn0.params;
        ssSetSFcnParamsCount(rts, 4);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)&Model205_Openloop_P.SFunction_P1_Size
                       [0]);
        ssSetSFcnParam(rts, 1, (mxArray*)&Model205_Openloop_P.SFunction_P2_Size
                       [0]);
        ssSetSFcnParam(rts, 2, (mxArray*)&Model205_Openloop_P.SFunction_P3_Size
                       [0]);
        ssSetSFcnParam(rts, 3, (mxArray*)&Model205_Openloop_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 Model205_Openloop_terminate(void)
{
  /* Level2 S-Function Block: '<S4>/S-Function' (ECPDSPDriver) */
  {
    SimStruct *rts = Model205_Openloop_M->childSfunctions[0];
    sfcnTerminate(rts);
  }

  /* External mode */
  rtExtModeShutdown(2);
}

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

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

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

void MdlInitializeSampleTimes(void)
{
}

void MdlInitialize(void)
{
}

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

  MdlInitialize();
}

RT_MODEL_Model205_Openloop *Model205_Openloop(void)
{
  Model205_Openloop_initialize(1);
  return Model205_Openloop_M;
}

void MdlTerminate(void)
{
  Model205_Openloop_terminate();
}

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