/*
 * rozbeh_motoru.c
 *
 * Code generation for model "rozbeh_motoru".
 *
 * Model version              : 1.55
 * Simulink Coder version : 8.3 (R2012b) 20-Jul-2012
 * C source code generated on : Wed Feb 11 17:43:31 2015
 *
 * Target selection: rtwin.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: Generic->32-bit x86 compatible
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */
#include "rozbeh_motoru.h"
#include "rozbeh_motoru_private.h"
#include "rozbeh_motoru_dt.h"

/* options for Real-Time Windows Target board 0 */
static double RTWinBoardOptions0[] = {
  1.0,
};

/* list of Real-Time Windows Target timers */
const int RTWinTimerCount = 1;
const double RTWinTimers[2] = {
  0.0001, 0.0,
};

/* list of Real-Time Windows Target boards */
const int RTWinBoardCount = 1;
RTWINBOARD RTWinBoards[1] = {
  { "Humusoft/MF624", 4294967295U, 1, RTWinBoardOptions0 },
};

/* Block signals (auto storage) */
BlockIO_rozbeh_motoru rozbeh_motoru_B;

/* Block states (auto storage) */
D_Work_rozbeh_motoru rozbeh_motoru_DWork;

/* Real-time model */
RT_MODEL_rozbeh_motoru rozbeh_motoru_M_;
RT_MODEL_rozbeh_motoru *const rozbeh_motoru_M = &rozbeh_motoru_M_;
real_T rt_powd_snf(real_T u0, real_T u1)
{
  real_T y;
  real_T tmp;
  real_T tmp_0;
  if (rtIsNaN(u0) || rtIsNaN(u1)) {
    y = (rtNaN);
  } else {
    tmp = fabs(u0);
    tmp_0 = fabs(u1);
    if (rtIsInf(u1)) {
      if (tmp == 1.0) {
        y = (rtNaN);
      } else if (tmp > 1.0) {
        if (u1 > 0.0) {
          y = (rtInf);
        } else {
          y = 0.0;
        }
      } else if (u1 > 0.0) {
        y = 0.0;
      } else {
        y = (rtInf);
      }
    } else if (tmp_0 == 0.0) {
      y = 1.0;
    } else if (tmp_0 == 1.0) {
      if (u1 > 0.0) {
        y = u0;
      } else {
        y = 1.0 / u0;
      }
    } else if (u1 == 2.0) {
      y = u0 * u0;
    } else if ((u1 == 0.5) && (u0 >= 0.0)) {
      y = sqrt(u0);
    } else if ((u0 < 0.0) && (u1 > floor(u1))) {
      y = (rtNaN);
    } else {
      y = pow(u0, u1);
    }
  }

  return y;
}

/* Model output function */
void rozbeh_motoru_output(void)
{
  /* local block i/o variables */
  real_T rtb_ManualSwitch;

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

  /* Math: '<Root>/Math Function' incorporates:
   *  Constant: '<Root>/Constant'
   */
  if ((rozbeh_motoru_B.Clock < 0.0) && (rozbeh_motoru_P.Constant_Value > floor
       (rozbeh_motoru_P.Constant_Value))) {
    rtb_ManualSwitch = -rt_powd_snf(-rozbeh_motoru_B.Clock,
      rozbeh_motoru_P.Constant_Value);
  } else {
    rtb_ManualSwitch = rt_powd_snf(rozbeh_motoru_B.Clock,
      rozbeh_motoru_P.Constant_Value);
  }

  /* End of Math: '<Root>/Math Function' */

  /* Saturate: '<Root>/Saturation' */
  if (rtb_ManualSwitch >= rozbeh_motoru_P.Saturation_UpperSat) {
    rozbeh_motoru_B.Saturation = rozbeh_motoru_P.Saturation_UpperSat;
  } else if (rtb_ManualSwitch <= rozbeh_motoru_P.Saturation_LowerSat) {
    rozbeh_motoru_B.Saturation = rozbeh_motoru_P.Saturation_LowerSat;
  } else {
    rozbeh_motoru_B.Saturation = rtb_ManualSwitch;
  }

  /* End of Saturate: '<Root>/Saturation' */

  /* ManualSwitch: '<Root>/Manual Switch' incorporates:
   *  Constant: '<Root>/Constant1'
   *  Constant: '<Root>/Constant2'
   */
  if (rozbeh_motoru_P.ManualSwitch_CurrentSetting == 1) {
    rtb_ManualSwitch = rozbeh_motoru_P.Constant1_Value;
  } else {
    rtb_ManualSwitch = rozbeh_motoru_P.Constant2_Value;
  }

  /* End of ManualSwitch: '<Root>/Manual Switch' */
  /* S-Function Block: <Root>/Frequency Output */
  {
    {
      RTBIO_DriverIO(0, FREQUENCYOUTPUT, IOWRITE, 1,
                     &rozbeh_motoru_P.FrequencyOutput_Channels,
                     &rozbeh_motoru_B.Saturation, &rozbeh_motoru_P.Strida_Value);
    }
  }

  /* S-Function Block: <Root>/DIR */
  {
    RTBIO_DriverIO(0, DIGITALOUTPUT, IOWRITE, 1, &rozbeh_motoru_P.DIR_Channels,
                   &rtb_ManualSwitch, &rozbeh_motoru_P.DIR_BitMode);
  }

  /* S-Function Block: <Root>/Enable */
  {
    RTBIO_DriverIO(0, DIGITALOUTPUT, IOWRITE, 1,
                   &rozbeh_motoru_P.Enable_Channels,
                   &rozbeh_motoru_P.Constant4_Value,
                   &rozbeh_motoru_P.Enable_BitMode);
  }

  /* S-Function Block: <Root>/Encoder Input */
  {
    ENCODERINPARM parm;
    parm.quad = (QUADMODE) 0;
    parm.index = (INDEXPULSE) 2;
    parm.infilter = rozbeh_motoru_P.EncoderInput_InputFilter;
    RTBIO_DriverIO(0, ENCODERINPUT, IOREAD, 1,
                   &rozbeh_motoru_P.EncoderInput_Channels,
                   &rozbeh_motoru_B.EncoderInput, &parm);
  }

  /* Gain: '<Root>/Gain' */
  rozbeh_motoru_B.Gain = rozbeh_motoru_P.Gain_Gain *
    rozbeh_motoru_B.EncoderInput;
}

/* Model update function */
void rozbeh_motoru_update(void)
{
  /* 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 (!(++rozbeh_motoru_M->Timing.clockTick0)) {
    ++rozbeh_motoru_M->Timing.clockTickH0;
  }

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

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

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

/* Model initialize function */
void rozbeh_motoru_initialize(void)
{
  /* S-Function Block: <Root>/Frequency Output */
  {
    {
      RTBIO_DriverIO(0, FREQUENCYOUTPUT, IOWRITE, 1,
                     &rozbeh_motoru_P.FrequencyOutput_Channels,
                     &rozbeh_motoru_P.FrequencyOutput_Frequency,
                     &rozbeh_motoru_P.FrequencyOutput_Duty);
    }
  }

  /* S-Function Block: <Root>/DIR */

  /* no initial value should be set */

  /* S-Function Block: <Root>/Enable */

  /* no initial value should be set */

  /* S-Function Block: <Root>/Encoder Input */
  {
    ENCODERINPARM parm;
    parm.quad = (QUADMODE) 0;
    parm.index = (INDEXPULSE) 2;
    parm.infilter = rozbeh_motoru_P.EncoderInput_InputFilter;
    RTBIO_DriverIO(0, ENCODERINPUT, IORESET, 1,
                   &rozbeh_motoru_P.EncoderInput_Channels, NULL, &parm);
  }
}

/* Model terminate function */
void rozbeh_motoru_terminate(void)
{
  /* S-Function Block: <Root>/Frequency Output */
  {
    {
      RTBIO_DriverIO(0, FREQUENCYOUTPUT, IOWRITE, 1,
                     &rozbeh_motoru_P.FrequencyOutput_Channels,
                     &rozbeh_motoru_P.FrequencyOutput_FrequencyFinalValue,
                     &rozbeh_motoru_P.FrequencyOutput_DutyFinalValue);
    }
  }

  /* S-Function Block: <Root>/DIR */

  /* no final value should be set */

  /* S-Function Block: <Root>/Enable */
  {
    RTBIO_DriverIO(0, DIGITALOUTPUT, IOWRITE, 1,
                   &rozbeh_motoru_P.Enable_Channels,
                   &rozbeh_motoru_P.Enable_FinalValue,
                   &rozbeh_motoru_P.Enable_BitMode);
  }
}

/*========================================================================*
 * Start of Classic call interface                                        *
 *========================================================================*/
void MdlOutputs(int_T tid)
{
  rozbeh_motoru_output();

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

void MdlUpdate(int_T tid)
{
  rozbeh_motoru_update();

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

void MdlInitializeSizes(void)
{
}

void MdlInitializeSampleTimes(void)
{
}

void MdlInitialize(void)
{
}

void MdlStart(void)
{
  rozbeh_motoru_initialize();
}

void MdlTerminate(void)
{
  rozbeh_motoru_terminate();
}

RT_MODEL_rozbeh_motoru *rozbeh_motoru(void)
{
  /* Registration code */

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

  /* non-finite (run-time) assignments */
  rozbeh_motoru_P.EncoderInput_InputFilter = rtInf;

  /* initialize real-time model */
  (void) memset((void *)rozbeh_motoru_M, 0,
                sizeof(RT_MODEL_rozbeh_motoru));

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

  rtsiSetSimTimeStep(&rozbeh_motoru_M->solverInfo, MAJOR_TIME_STEP);
  rtsiSetSolverName(&rozbeh_motoru_M->solverInfo,"FixedStepDiscrete");

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

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

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

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

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

  rtmSetTFinal(rozbeh_motoru_M, -1);
  rozbeh_motoru_M->Timing.stepSize0 = 0.0001;
  rozbeh_motoru_M->Timing.stepSize1 = 0.0001;

  /* External mode info */
  rozbeh_motoru_M->Sizes.checksums[0] = (1153928805U);
  rozbeh_motoru_M->Sizes.checksums[1] = (611249717U);
  rozbeh_motoru_M->Sizes.checksums[2] = (2491101099U);
  rozbeh_motoru_M->Sizes.checksums[3] = (3120915768U);

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

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

  /* block I/O */
  rozbeh_motoru_M->ModelData.blockIO = ((void *) &rozbeh_motoru_B);
  (void) memset(((void *) &rozbeh_motoru_B), 0,
                sizeof(BlockIO_rozbeh_motoru));

  /* parameters */
  rozbeh_motoru_M->ModelData.defaultParam = ((real_T *)&rozbeh_motoru_P);

  /* states (dwork) */
  rozbeh_motoru_M->Work.dwork = ((void *) &rozbeh_motoru_DWork);
  (void) memset((void *)&rozbeh_motoru_DWork, 0,
                sizeof(D_Work_rozbeh_motoru));

  /* data type transition information */
  {
    static DataTypeTransInfo dtInfo;
    (void) memset((char_T *) &dtInfo, 0,
                  sizeof(dtInfo));
    rozbeh_motoru_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;
  }

  /* Initialize Sizes */
  rozbeh_motoru_M->Sizes.numContStates = (0);/* Number of continuous states */
  rozbeh_motoru_M->Sizes.numY = (0);   /* Number of model outputs */
  rozbeh_motoru_M->Sizes.numU = (0);   /* Number of model inputs */
  rozbeh_motoru_M->Sizes.sysDirFeedThru = (0);/* The model is not direct feedthrough */
  rozbeh_motoru_M->Sizes.numSampTimes = (2);/* Number of sample times */
  rozbeh_motoru_M->Sizes.numBlocks = (20);/* Number of blocks */
  rozbeh_motoru_M->Sizes.numBlockIO = (4);/* Number of block outputs */
  rozbeh_motoru_M->Sizes.numBlockPrms = (29);/* Sum of parameter "widths" */
  return rozbeh_motoru_M;
}

/*========================================================================*
 * End of Classic call interface                                          *
 *========================================================================*/
