/*
 * control_Velocidad.cpp
 *
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * Code generation for model "control_Velocidad".
 *
 * Model version              : 1.10
 * Simulink Coder version : 24.2 (R2024b) 21-Jun-2024
 * C++ source code generated on : Mon Jun  9 17:59:10 2025
 *
 * Target selection: speedgoat.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: Intel->x86-64 (Linux 64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "control_Velocidad.h"
#include "control_Velocidad_cal.h"
#include "rte_control_Velocidad_parameters.h"
#include <cmath>
#include "control_Velocidad_private.h"
#include "rtwtypes.h"
#include "zero_crossing_types.h"
#include <cstring>

extern "C"
{

#include "rt_nonfinite.h"

}

#include <cfloat>

/* Block signals (default storage) */
B_control_Velocidad_T control_Velocidad_B;

/* Block states (default storage) */
DW_control_Velocidad_T control_Velocidad_DW;

/* Previous zero-crossings (trigger) states */
PrevZCX_control_Velocidad_T control_Velocidad_PrevZCX;

/* Real-time model */
RT_MODEL_control_Velocidad_T control_Velocidad_M_ = RT_MODEL_control_Velocidad_T
  ();
RT_MODEL_control_Velocidad_T *const control_Velocidad_M = &control_Velocidad_M_;
real_T look1_pbinlxpw(real_T u0, const real_T bp0[], const real_T table[],
                      uint32_T prevIndex[], uint32_T maxIndex)
{
  real_T frac;
  real_T yL_0d0;
  uint32_T bpIdx;

  /* Column-major Lookup 1-D
     Search method: 'binary'
     Use previous index: 'on'
     Interpolation method: 'Linear point-slope'
     Extrapolation method: 'Linear'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
  /* Prelookup - Index and Fraction
     Index Search method: 'binary'
     Extrapolation method: 'Linear'
     Use previous index: 'on'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
  if (u0 <= bp0[0U]) {
    bpIdx = 0U;
    frac = (u0 - bp0[0U]) / (bp0[1U] - bp0[0U]);
  } else if (u0 < bp0[maxIndex]) {
    uint32_T found;
    uint32_T iLeft;
    uint32_T iRght;

    /* Binary Search using Previous Index */
    bpIdx = prevIndex[0U];
    iLeft = 0U;
    iRght = maxIndex;
    found = 0U;
    while (found == 0U) {
      if (u0 < bp0[bpIdx]) {
        iRght = bpIdx - 1U;
        bpIdx = ((bpIdx + iLeft) - 1U) >> 1U;
      } else if (u0 < bp0[bpIdx + 1U]) {
        found = 1U;
      } else {
        iLeft = bpIdx + 1U;
        bpIdx = ((bpIdx + iRght) + 1U) >> 1U;
      }
    }

    frac = (u0 - bp0[bpIdx]) / (bp0[bpIdx + 1U] - bp0[bpIdx]);
  } else {
    bpIdx = maxIndex - 1U;
    frac = (u0 - bp0[maxIndex - 1U]) / (bp0[maxIndex] - bp0[maxIndex - 1U]);
  }

  prevIndex[0U] = bpIdx;

  /* Column-major Interpolation 1-D
     Interpolation method: 'Linear point-slope'
     Use last breakpoint for index at or above upper limit: 'off'
     Overflow mode: 'portable wrapping'
   */
  yL_0d0 = table[bpIdx];
  return (table[bpIdx + 1U] - yL_0d0) * frac + yL_0d0;
}

real_T rt_remd_snf(real_T u0, real_T u1)
{
  real_T y;
  if (rtIsNaN(u0) || rtIsNaN(u1) || rtIsInf(u0)) {
    y = (rtNaN);
  } else if (rtIsInf(u1)) {
    y = u0;
  } else {
    if (u1 < 0.0) {
      y = std::ceil(u1);
    } else {
      y = std::floor(u1);
    }

    if ((u1 != 0.0) && (u1 != y)) {
      real_T q;
      q = std::abs(u0 / u1);
      if (!(std::abs(q - std::floor(q + 0.5)) > DBL_EPSILON * q)) {
        y = 0.0 * u0;
      } else {
        y = std::fmod(u0, u1);
      }
    } else {
      y = std::fmod(u0, u1);
    }
  }

  return y;
}

/* Model step function for TID0 */
void control_Velocidad_step0(void)     /* Sample time: [0.0s, 0.0s] */
{
  real_T u0;
  ZCSigState SampleandHold1_Trig_ZCE_tmp;
  boolean_T zcEvent;

  /* Reset subsysRan breadcrumbs */
  srClearBC(control_Velocidad_DW.TriggeredSubsystem_SubsysRanBC);

  /* Reset subsysRan breadcrumbs */
  srClearBC(control_Velocidad_DW.NEGATIVEEdge_SubsysRanBC);

  /* Reset subsysRan breadcrumbs */
  srClearBC(control_Velocidad_DW.POSITIVEEdge_SubsysRanBC);

  /* Reset subsysRan breadcrumbs */
  srClearBC(control_Velocidad_DW.SampleandHold1_SubsysRanBC);

  /* Gain: '<Root>/CTE_amplitud' incorporates:
   *  Constant: '<Root>/Constant1'
   */
  control_Velocidad_B.CTE_amplitud = *get_cte_amplitud() *
    control_Velocidad_cal->Constant1_Value;

  /* DiscreteIntegrator: '<S1>/Discrete-Time Integrator' */
  control_Velocidad_B.DiscreteTimeIntegrator =
    control_Velocidad_DW.DiscreteTimeIntegrator_DSTATE;

  /* Trigonometry: '<S1>/Trigonometric Function' */
  control_Velocidad_B.TrigonometricFunction = std::sin
    (control_Velocidad_B.DiscreteTimeIntegrator);

  /* Sum: '<S1>/Sum' incorporates:
   *  Constant: '<S1>/Constant1'
   */
  control_Velocidad_B.Sum = control_Velocidad_B.DiscreteTimeIntegrator +
    control_Velocidad_cal->Constant1_Value_l;

  /* Trigonometry: '<S1>/Trigonometric Function1' */
  control_Velocidad_B.TrigonometricFunction1 = std::sin(control_Velocidad_B.Sum);

  /* Sum: '<S1>/Sum1' incorporates:
   *  Constant: '<S1>/Constant2'
   */
  control_Velocidad_B.Sum1 = control_Velocidad_B.DiscreteTimeIntegrator +
    control_Velocidad_cal->Constant2_Value;

  /* Trigonometry: '<S1>/Trigonometric Function2' */
  control_Velocidad_B.TrigonometricFunction2 = std::sin(control_Velocidad_B.Sum1);

  /* Product: '<Root>/Product' */
  control_Velocidad_B.Product[0] = control_Velocidad_B.CTE_amplitud *
    control_Velocidad_B.TrigonometricFunction;
  control_Velocidad_B.Product[1] = control_Velocidad_B.CTE_amplitud *
    control_Velocidad_B.TrigonometricFunction1;
  control_Velocidad_B.Product[2] = control_Velocidad_B.CTE_amplitud *
    control_Velocidad_B.TrigonometricFunction2;

  /* Saturate: '<Root>/Saturation' */
  u0 = control_Velocidad_B.Product[0];
  if (u0 > control_Velocidad_cal->Saturation_UpperSat) {
    u0 = control_Velocidad_cal->Saturation_UpperSat;
  } else if (u0 < control_Velocidad_cal->Saturation_LowerSat) {
    u0 = control_Velocidad_cal->Saturation_LowerSat;
  }

  /* Saturate: '<Root>/Saturation' */
  control_Velocidad_B.Saturation[0] = u0;

  /* Saturate: '<Root>/Saturation' */
  u0 = control_Velocidad_B.Product[1];
  if (u0 > control_Velocidad_cal->Saturation_UpperSat) {
    u0 = control_Velocidad_cal->Saturation_UpperSat;
  } else if (u0 < control_Velocidad_cal->Saturation_LowerSat) {
    u0 = control_Velocidad_cal->Saturation_LowerSat;
  }

  /* Saturate: '<Root>/Saturation' */
  control_Velocidad_B.Saturation[1] = u0;

  /* Saturate: '<Root>/Saturation' */
  u0 = control_Velocidad_B.Product[2];
  if (u0 > control_Velocidad_cal->Saturation_UpperSat) {
    u0 = control_Velocidad_cal->Saturation_UpperSat;
  } else if (u0 < control_Velocidad_cal->Saturation_LowerSat) {
    u0 = control_Velocidad_cal->Saturation_LowerSat;
  }

  /* Saturate: '<Root>/Saturation' */
  control_Velocidad_B.Saturation[2] = u0;

  /* DigitalClock: '<S13>/Digital Clock' */
  control_Velocidad_B.DigitalClock = control_Velocidad_M->Timing.t[1];

  /* Sum: '<S13>/Add1' incorporates:
   *  Constant: '<S13>/Constant3'
   */
  control_Velocidad_B.Add1 = control_Velocidad_B.DigitalClock +
    control_Velocidad_cal->Constant3_Value;

  /* Math: '<S13>/Math Function' incorporates:
   *  Constant: '<S13>/Constant1'
   */
  control_Velocidad_B.MathFunction = rt_remd_snf(control_Velocidad_B.Add1,
    control_Velocidad_cal->Constant1_Value_l0);

  /* Gain: '<S13>/1\ib1' */
  control_Velocidad_B.uib1 = control_Velocidad_cal->uib1_Gain *
    control_Velocidad_B.MathFunction;

  /* Lookup_n-D: '<S13>/1-D Lookup Table' incorporates:
   *  Gain: '<S13>/1\ib1'
   */
  control_Velocidad_B.uDLookupTable = look1_pbinlxpw(control_Velocidad_B.uib1,
    control_Velocidad_cal->uDLookupTable_bp01Data,
    control_Velocidad_cal->uDLookupTable_tableData,
    &control_Velocidad_DW.m_bpIndex, 2U);

  /* Sum: '<S13>/Add3' incorporates:
   *  Constant: '<S13>/Constant2'
   */
  control_Velocidad_B.Add3 = control_Velocidad_B.uDLookupTable -
    control_Velocidad_cal->Constant2_Value_i;

  /* Sum: '<S4>/Add3' incorporates:
   *  Constant: '<S2>/Constant10'
   */
  control_Velocidad_B.Add3_b = control_Velocidad_cal->PWMGenerator2Level_MinMax
    [1] - control_Velocidad_cal->PWMGenerator2Level_MinMax[0];

  /* Gain: '<S4>/Gain1' */
  control_Velocidad_B.Gain1 = control_Velocidad_cal->Gain1_Gain *
    control_Velocidad_B.Add3_b;

  /* Product: '<S4>/MUL1' */
  control_Velocidad_B.MUL1 = control_Velocidad_B.Add3 *
    control_Velocidad_B.Gain1;

  /* Sum: '<S4>/Add4' incorporates:
   *  Constant: '<S2>/Constant10'
   */
  control_Velocidad_B.Add4 = (control_Velocidad_cal->PWMGenerator2Level_MinMax[0]
    + control_Velocidad_B.MUL1) + control_Velocidad_B.Gain1;

  /* RelationalOperator: '<S8>/Relational Operator2' */
  zcEvent = (control_Velocidad_B.Saturation[0] >= control_Velocidad_B.Add4);
  control_Velocidad_B.RelationalOperator2[0] = zcEvent;

  /* Logic: '<S2>/Logical Operator4' */
  control_Velocidad_B.LogicalOperator4[0] = !zcEvent;

  /* RelationalOperator: '<S8>/Relational Operator2' */
  zcEvent = (control_Velocidad_B.Saturation[1] >= control_Velocidad_B.Add4);
  control_Velocidad_B.RelationalOperator2[1] = zcEvent;

  /* Logic: '<S2>/Logical Operator4' */
  control_Velocidad_B.LogicalOperator4[1] = !zcEvent;

  /* RelationalOperator: '<S8>/Relational Operator2' */
  zcEvent = (control_Velocidad_B.Saturation[2] >= control_Velocidad_B.Add4);
  control_Velocidad_B.RelationalOperator2[2] = zcEvent;

  /* Logic: '<S2>/Logical Operator4' */
  control_Velocidad_B.LogicalOperator4[2] = !zcEvent;

  /* DataTypeConversion: '<S2>/Data Type Conversion' */
  control_Velocidad_B.DataTypeConversion[0] =
    control_Velocidad_B.RelationalOperator2[0];
  control_Velocidad_B.DataTypeConversion[1] =
    control_Velocidad_B.LogicalOperator4[0];
  control_Velocidad_B.DataTypeConversion[2] =
    control_Velocidad_B.RelationalOperator2[1];
  control_Velocidad_B.DataTypeConversion[3] =
    control_Velocidad_B.LogicalOperator4[1];
  control_Velocidad_B.DataTypeConversion[4] =
    control_Velocidad_B.RelationalOperator2[2];
  control_Velocidad_B.DataTypeConversion[5] =
    control_Velocidad_B.LogicalOperator4[2];

  /* S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output' */

  /* Level2 S-Function Block: '<Root>/Digital output' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[0];
    sfcnOutputs(rts,0);
  }

  /* Gain: '<S1>/Gain' incorporates:
   *  Constant: '<Root>/Constant1'
   */
  control_Velocidad_B.Gain = control_Velocidad_cal->Gain_Gain *
    control_Velocidad_cal->Constant1_Value;

  /* Delay: '<S3>/Delay1' */
  control_Velocidad_B.Delay1 = control_Velocidad_DW.Delay1_DSTATE;

  /* S-Function (sg_fpga_di_sf_a2): '<Root>/Digital input' */

  /* Level2 S-Function Block: '<Root>/Digital input' (sg_fpga_di_sf_a2) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[1];
    sfcnOutputs(rts,0);
  }

  /* DataTypeConversion: '<S3>/Cast To Boolean' */
  control_Velocidad_B.CastToBoolean = (control_Velocidad_B.Digitalinput_o1 !=
    0.0);

  /* DataTypeConversion: '<S17>/Data Type Conversion2' */
  control_Velocidad_B.DataTypeConversion2 = control_Velocidad_B.CastToBoolean;

  /* Memory: '<S17>/Memory' */
  control_Velocidad_B.Memory = control_Velocidad_DW.Memory_PreviousInput;

  /* MultiPortSwitch: '<S17>/Multiport Switch' incorporates:
   *  Constant: '<S17>/Constant1'
   */
  switch (static_cast<int32_T>(control_Velocidad_cal->EdgeDetector_model)) {
   case 1:
    /* MultiPortSwitch: '<S17>/Multiport Switch' incorporates:
     *  Constant: '<S17>/pos. edge'
     */
    control_Velocidad_B.MultiportSwitch[0] =
      control_Velocidad_cal->posedge_Value[0];
    control_Velocidad_B.MultiportSwitch[1] =
      control_Velocidad_cal->posedge_Value[1];
    break;

   case 2:
    /* MultiPortSwitch: '<S17>/Multiport Switch' incorporates:
     *  Constant: '<S17>/neg. edge'
     */
    control_Velocidad_B.MultiportSwitch[0] =
      control_Velocidad_cal->negedge_Value[0];
    control_Velocidad_B.MultiportSwitch[1] =
      control_Velocidad_cal->negedge_Value[1];
    break;

   default:
    /* MultiPortSwitch: '<S17>/Multiport Switch' incorporates:
     *  Constant: '<S17>/either edge'
     */
    control_Velocidad_B.MultiportSwitch[0] =
      control_Velocidad_cal->eitheredge_Value[0];
    control_Velocidad_B.MultiportSwitch[1] =
      control_Velocidad_cal->eitheredge_Value[1];
    break;
  }

  /* End of MultiPortSwitch: '<S17>/Multiport Switch' */

  /* Outputs for Enabled SubSystem: '<S17>/POSITIVE Edge' incorporates:
   *  EnablePort: '<S20>/Enable'
   */
  if (control_Velocidad_B.MultiportSwitch[0] > 0.0) {
    if (!control_Velocidad_DW.POSITIVEEdge_MODE) {
      control_Velocidad_DW.POSITIVEEdge_MODE = true;
    }

    /* RelationalOperator: '<S20>/Relational Operator1' */
    control_Velocidad_B.RelationalOperator1 = (static_cast<int32_T>
      (control_Velocidad_B.Memory) < static_cast<int32_T>
      (control_Velocidad_B.DataTypeConversion2));
    srUpdateBC(control_Velocidad_DW.POSITIVEEdge_SubsysRanBC);
  } else if (control_Velocidad_DW.POSITIVEEdge_MODE) {
    control_Velocidad_DW.POSITIVEEdge_MODE = false;
  }

  /* End of Outputs for SubSystem: '<S17>/POSITIVE Edge' */

  /* Outputs for Enabled SubSystem: '<S17>/NEGATIVE Edge' incorporates:
   *  EnablePort: '<S19>/Enable'
   */
  if (control_Velocidad_B.MultiportSwitch[1] > 0.0) {
    if (!control_Velocidad_DW.NEGATIVEEdge_MODE) {
      control_Velocidad_DW.NEGATIVEEdge_MODE = true;
    }

    /* RelationalOperator: '<S19>/Relational Operator1' */
    control_Velocidad_B.RelationalOperator1_e = (static_cast<int32_T>
      (control_Velocidad_B.Memory) > static_cast<int32_T>
      (control_Velocidad_B.DataTypeConversion2));
    srUpdateBC(control_Velocidad_DW.NEGATIVEEdge_SubsysRanBC);
  } else if (control_Velocidad_DW.NEGATIVEEdge_MODE) {
    control_Velocidad_DW.NEGATIVEEdge_MODE = false;
  }

  /* End of Outputs for SubSystem: '<S17>/NEGATIVE Edge' */

  /* Logic: '<S17>/Logical Operator1' */
  control_Velocidad_B.LogicalOperator1 =
    (control_Velocidad_B.RelationalOperator1 ||
     control_Velocidad_B.RelationalOperator1_e);

  /* Gain: '<S3>/CTE Encoder' */
  control_Velocidad_B.CTEEncoder = control_Velocidad_B.LogicalOperator1 ?
    *get_cte_encoder() : 0UL;

  /* DataTypeConversion: '<S3>/Cast To Boolean1' */
  control_Velocidad_B.CastToBoolean1 = (control_Velocidad_B.Digitalinput_o2 !=
    0.0);

  /* Clock: '<S3>/Clock' incorporates:
   *  Clock: '<S18>/Clock'
   */
  u0 = control_Velocidad_M->Timing.t[0];

  /* Clock: '<S3>/Clock' */
  control_Velocidad_B.Clock = u0;

  /* Outputs for Triggered SubSystem: '<S3>/Sample and Hold1' incorporates:
   *  TriggerPort: '<S16>/Trigger'
   */
  zcEvent = (control_Velocidad_B.LogicalOperator1 &&
             (control_Velocidad_PrevZCX.SampleandHold1_Trig_ZCE != POS_ZCSIG));
  if (zcEvent) {
    /* SignalConversion generated from: '<S16>/In' */
    control_Velocidad_B.In = control_Velocidad_B.Clock;
    control_Velocidad_DW.SampleandHold1_SubsysRanBC = 4;
  }

  SampleandHold1_Trig_ZCE_tmp = control_Velocidad_B.LogicalOperator1;
  control_Velocidad_PrevZCX.SampleandHold1_Trig_ZCE =
    SampleandHold1_Trig_ZCE_tmp;

  /* End of Outputs for SubSystem: '<S3>/Sample and Hold1' */

  /* UnitDelay: '<S14>/UD' */
  control_Velocidad_B.Uk1 = control_Velocidad_DW.UD_DSTATE;

  /* Sum: '<S14>/Diff' */
  control_Velocidad_B.Diff = control_Velocidad_B.In - control_Velocidad_B.Uk1;

  /* Delay: '<S3>/Delay' */
  control_Velocidad_B.Delay = control_Velocidad_DW.Delay_DSTATE;

  /* Switch: '<S3>/Switch' */
  if (control_Velocidad_B.Diff > *get_limite_direccion()) {
    /* Logic: '<S3>/Logical Operator' */
    control_Velocidad_B.LogicalOperator = (control_Velocidad_B.LogicalOperator1 &&
      control_Velocidad_B.CastToBoolean1);

    /* Switch: '<S3>/Switch1' */
    if (control_Velocidad_B.LogicalOperator) {
      /* Switch: '<S3>/Switch1' incorporates:
       *  Constant: '<S3>/Constant1'
       */
      control_Velocidad_B.Switch1 = control_Velocidad_cal->Constant1_Value_n;
    } else {
      /* Switch: '<S3>/Switch1' incorporates:
       *  Constant: '<S3>/Constant2'
       */
      control_Velocidad_B.Switch1 = control_Velocidad_cal->Constant2_Value_b;
    }

    /* End of Switch: '<S3>/Switch1' */

    /* Switch: '<S3>/Switch' */
    control_Velocidad_B.Switch = control_Velocidad_B.Switch1;
  } else {
    /* Switch: '<S3>/Switch' */
    control_Velocidad_B.Switch = control_Velocidad_B.Delay;
  }

  /* End of Switch: '<S3>/Switch' */

  /* Product: '<S3>/Product' incorporates:
   *  Gain: '<S3>/CTE Encoder'
   */
  control_Velocidad_B.Product_o = static_cast<real_T>
    (control_Velocidad_B.CTEEncoder) * 8.4703294725430034E-22 *
    control_Velocidad_B.Switch;

  /* Sum: '<S3>/Add' */
  control_Velocidad_B.Add = control_Velocidad_B.Delay1 +
    control_Velocidad_B.Product_o;

  /* Clock: '<S18>/Clock' */
  control_Velocidad_B.Clock_e = u0;

  /* Outputs for Triggered SubSystem: '<S18>/Triggered Subsystem' incorporates:
   *  TriggerPort: '<S21>/Trigger'
   */
  zcEvent = (control_Velocidad_B.LogicalOperator1 &&
             (control_Velocidad_PrevZCX.TriggeredSubsystem_Trig_ZCE != POS_ZCSIG));
  if (zcEvent) {
    /* SignalConversion generated from: '<S21>/In1' */
    control_Velocidad_B.In1 = control_Velocidad_B.Clock_e;
    control_Velocidad_DW.TriggeredSubsystem_SubsysRanBC = 4;
  }

  control_Velocidad_PrevZCX.TriggeredSubsystem_Trig_ZCE =
    SampleandHold1_Trig_ZCE_tmp;

  /* End of Outputs for SubSystem: '<S18>/Triggered Subsystem' */

  /* Sum: '<S18>/Sum' incorporates:
   *  Constant: '<S18>/Constant'
   */
  control_Velocidad_B.Sum_g = control_Velocidad_B.In1 +
    control_Velocidad_cal->Constant_Value;

  /* RelationalOperator: '<S18>/Relational Operator' */
  control_Velocidad_B.RelationalOperator = (control_Velocidad_B.Sum_g >
    control_Velocidad_B.Clock_e);

  /* user code (Output function Trailer for TID0) */
  {
  }

  /* Update for DiscreteIntegrator: '<S1>/Discrete-Time Integrator' */
  control_Velocidad_DW.DiscreteTimeIntegrator_DSTATE +=
    control_Velocidad_cal->DiscreteTimeIntegrator_gainval *
    control_Velocidad_B.Gain;

  /* Update for Delay: '<S3>/Delay1' */
  control_Velocidad_DW.Delay1_DSTATE = control_Velocidad_B.Add;

  /* Update for Memory: '<S17>/Memory' */
  control_Velocidad_DW.Memory_PreviousInput =
    control_Velocidad_B.DataTypeConversion2;

  /* Update for UnitDelay: '<S14>/UD' */
  control_Velocidad_DW.UD_DSTATE = control_Velocidad_B.In;

  /* Update for Delay: '<S3>/Delay' */
  control_Velocidad_DW.Delay_DSTATE = control_Velocidad_B.Switch;

  /* Update absolute time */
  /* 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 (!(++control_Velocidad_M->Timing.clockTick0)) {
    ++control_Velocidad_M->Timing.clockTickH0;
  }

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

  /* Update absolute time */
  /* 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 (!(++control_Velocidad_M->Timing.clockTick1)) {
    ++control_Velocidad_M->Timing.clockTickH1;
  }

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

/* Model step function for TID2 */
void control_Velocidad_step2(void)     /* Sample time: [0.001s, 0.0s] */
{
  /* S-Function (sg_fpga_IO397_ad): '<Root>/IO397 Analog Input' */

  /* Level2 S-Function Block: '<Root>/IO397 Analog Input' (sg_fpga_IO397_ad) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[2];
    sfcnOutputs(rts,2);
  }

  /* Gain: '<Root>/Gain' */
  control_Velocidad_B.Gain_n = control_Velocidad_cal->Gain_Gain_j *
    control_Velocidad_B.IO397AnalogInput_o1;

  /* Gain: '<Root>/Gain1' */
  control_Velocidad_B.Gain1_j = control_Velocidad_cal->Gain1_Gain_j *
    control_Velocidad_B.IO397AnalogInput_o2;

  /* Gain: '<Root>/Gain2' */
  control_Velocidad_B.Gain2 = control_Velocidad_cal->Gain2_Gain *
    control_Velocidad_B.IO397AnalogInput_o3;

  /* Update absolute time */
  /* The "clockTick2" counts the number of times the code of this task has
   * been executed. The absolute time is the multiplication of "clockTick2"
   * and "Timing.stepSize2". Size of "clockTick2" 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.clockTick2 and the high bits
   * Timing.clockTickH2. When the low bit overflows to 0, the high bits increment.
   */
  if (!(++control_Velocidad_M->Timing.clockTick2)) {
    ++control_Velocidad_M->Timing.clockTickH2;
  }

  control_Velocidad_M->Timing.t[2] = control_Velocidad_M->Timing.clockTick2 *
    control_Velocidad_M->Timing.stepSize2 +
    control_Velocidad_M->Timing.clockTickH2 *
    control_Velocidad_M->Timing.stepSize2 * 4294967296.0;
}

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

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

  /* Set task counter limit used by the static main program */
  (control_Velocidad_M)->Timing.TaskCounters.cLimit[0] = 1;
  (control_Velocidad_M)->Timing.TaskCounters.cLimit[1] = 1;
  (control_Velocidad_M)->Timing.TaskCounters.cLimit[2] = 26;

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

  rtsiSetSimTimeStep(&control_Velocidad_M->solverInfo, MAJOR_TIME_STEP);
  rtsiSetIsMinorTimeStepWithModeChange(&control_Velocidad_M->solverInfo, false);
  rtsiSetIsContModeFrozen(&control_Velocidad_M->solverInfo, false);
  rtsiSetSolverName(&control_Velocidad_M->solverInfo,"FixedStepDiscrete");
  control_Velocidad_M->solverInfoPtr = (&control_Velocidad_M->solverInfo);

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

    /* task periods */
    control_Velocidad_M->Timing.sampleTimes[0] = (0.0);
    control_Velocidad_M->Timing.sampleTimes[1] = (3.8461538461538463E-5);
    control_Velocidad_M->Timing.sampleTimes[2] = (0.001);

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

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

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

  rtmSetTFinal(control_Velocidad_M, -1);
  control_Velocidad_M->Timing.stepSize0 = 3.8461538461538463E-5;
  control_Velocidad_M->Timing.stepSize1 = 3.8461538461538463E-5;
  control_Velocidad_M->Timing.stepSize2 = 0.001;
  control_Velocidad_M->solverInfoPtr = (&control_Velocidad_M->solverInfo);
  control_Velocidad_M->Timing.stepSize = (3.8461538461538463E-5);
  rtsiSetFixedStepSize(&control_Velocidad_M->solverInfo, 3.8461538461538463E-5);
  rtsiSetSolverMode(&control_Velocidad_M->solverInfo, SOLVER_MODE_MULTITASKING);

  /* block I/O */
  (void) std::memset((static_cast<void *>(&control_Velocidad_B)), 0,
                     sizeof(B_control_Velocidad_T));

  /* states (dwork) */
  (void) std::memset(static_cast<void *>(&control_Velocidad_DW), 0,
                     sizeof(DW_control_Velocidad_T));

  /* child S-Function registration */
  {
    RTWSfcnInfo *sfcnInfo = &control_Velocidad_M->NonInlinedSFcns.sfcnInfo;
    control_Velocidad_M->sfcnInfo = (sfcnInfo);
    rtssSetErrorStatusPtr(sfcnInfo, (&rtmGetErrorStatus(control_Velocidad_M)));
    control_Velocidad_M->Sizes.numSampTimes = (3);
    rtssSetNumRootSampTimesPtr(sfcnInfo,
      &control_Velocidad_M->Sizes.numSampTimes);
    control_Velocidad_M->NonInlinedSFcns.taskTimePtrs[0] = (&rtmGetTPtr
      (control_Velocidad_M)[0]);
    control_Velocidad_M->NonInlinedSFcns.taskTimePtrs[1] = (&rtmGetTPtr
      (control_Velocidad_M)[1]);
    control_Velocidad_M->NonInlinedSFcns.taskTimePtrs[2] = (&rtmGetTPtr
      (control_Velocidad_M)[2]);
    rtssSetTPtrPtr(sfcnInfo,control_Velocidad_M->NonInlinedSFcns.taskTimePtrs);
    rtssSetTStartPtr(sfcnInfo, &rtmGetTStart(control_Velocidad_M));
    rtssSetTFinalPtr(sfcnInfo, &rtmGetTFinal(control_Velocidad_M));
    rtssSetTimeOfLastOutputPtr(sfcnInfo, &rtmGetTimeOfLastOutput
      (control_Velocidad_M));
    rtssSetStepSizePtr(sfcnInfo, &control_Velocidad_M->Timing.stepSize);
    rtssSetStopRequestedPtr(sfcnInfo, &rtmGetStopRequested(control_Velocidad_M));
    rtssSetDerivCacheNeedsResetPtr(sfcnInfo,
      &control_Velocidad_M->derivCacheNeedsReset);
    rtssSetZCCacheNeedsResetPtr(sfcnInfo,
      &control_Velocidad_M->zCCacheNeedsReset);
    rtssSetContTimeOutputInconsistentWithStateAtMajorStepPtr(sfcnInfo,
      &control_Velocidad_M->CTOutputIncnstWithState);
    rtssSetSampleHitsPtr(sfcnInfo, &control_Velocidad_M->Timing.sampleHits);
    rtssSetPerTaskSampleHitsPtr(sfcnInfo,
      &control_Velocidad_M->Timing.perTaskSampleHits);
    rtssSetSimModePtr(sfcnInfo, &control_Velocidad_M->simMode);
    rtssSetSolverInfoPtr(sfcnInfo, &control_Velocidad_M->solverInfoPtr);
  }

  control_Velocidad_M->Sizes.numSFcns = (3);

  /* register each child */
  {
    (void) std::memset(static_cast<void *>
                       (&control_Velocidad_M->NonInlinedSFcns.childSFunctions[0]),
                       0,
                       3*sizeof(SimStruct));
    control_Velocidad_M->childSfunctions =
      (&control_Velocidad_M->NonInlinedSFcns.childSFunctionPtrs[0]);
    control_Velocidad_M->childSfunctions[0] =
      (&control_Velocidad_M->NonInlinedSFcns.childSFunctions[0]);
    control_Velocidad_M->childSfunctions[1] =
      (&control_Velocidad_M->NonInlinedSFcns.childSFunctions[1]);
    control_Velocidad_M->childSfunctions[2] =
      (&control_Velocidad_M->NonInlinedSFcns.childSFunctions[2]);

    /* Level2 S-Function Block: control_Velocidad/<Root>/Digital output (sg_fpga_do_sf_a2) */
    {
      SimStruct *rts = control_Velocidad_M->childSfunctions[0];

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

      {
        ssSetBlkInfo2Ptr(rts, &control_Velocidad_M->NonInlinedSFcns.blkInfo2[0]);
      }

      _ssSetBlkInfo2PortInfo2Ptr(rts,
        &control_Velocidad_M->NonInlinedSFcns.inputOutputPortInfo2[0]);

      /* Set up the mdlInfo pointer */
      ssSetRTWSfcnInfo(rts, control_Velocidad_M->sfcnInfo);

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

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

      /* Allocate memory of model methods 4 */
      {
        ssSetModelMethods4(rts, &control_Velocidad_M->NonInlinedSFcns.methods4[0]);
      }

      /* Allocate memory for states auxilliary information */
      {
        ssSetStatesInfo2(rts, &control_Velocidad_M->NonInlinedSFcns.statesInfo2
                         [0]);
        ssSetPeriodicStatesInfo(rts,
          &control_Velocidad_M->NonInlinedSFcns.periodicStatesInfo[0]);
      }

      /* inputs */
      {
        _ssSetNumInputPorts(rts, 6);
        ssSetPortInfoForInputs(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn0.inputPortInfo[0]);
        ssSetPortInfoForInputs(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn0.inputPortInfo[0]);
        _ssSetPortInfo2ForInputUnits(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn0.inputPortUnits[0]);
        ssSetInputPortUnit(rts, 0, 0);
        ssSetInputPortUnit(rts, 1, 0);
        ssSetInputPortUnit(rts, 2, 0);
        ssSetInputPortUnit(rts, 3, 0);
        ssSetInputPortUnit(rts, 4, 0);
        ssSetInputPortUnit(rts, 5, 0);
        _ssSetPortInfo2ForInputCoSimAttribute(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn0.inputPortCoSimAttribute[0]);
        ssSetInputPortIsContinuousQuantity(rts, 0, 0);
        ssSetInputPortIsContinuousQuantity(rts, 1, 0);
        ssSetInputPortIsContinuousQuantity(rts, 2, 0);
        ssSetInputPortIsContinuousQuantity(rts, 3, 0);
        ssSetInputPortIsContinuousQuantity(rts, 4, 0);
        ssSetInputPortIsContinuousQuantity(rts, 5, 0);

        /* port 0 */
        {
          ssSetInputPortRequiredContiguous(rts, 0, 1);
          ssSetInputPortSignal(rts, 0, &control_Velocidad_B.DataTypeConversion[0]);
          _ssSetInputPortNumDimensions(rts, 0, 1);
          ssSetInputPortWidthAsInt(rts, 0, 1);
        }

        /* port 1 */
        {
          ssSetInputPortRequiredContiguous(rts, 1, 1);
          ssSetInputPortSignal(rts, 1, &control_Velocidad_B.DataTypeConversion[1]);
          _ssSetInputPortNumDimensions(rts, 1, 1);
          ssSetInputPortWidthAsInt(rts, 1, 1);
        }

        /* port 2 */
        {
          ssSetInputPortRequiredContiguous(rts, 2, 1);
          ssSetInputPortSignal(rts, 2, &control_Velocidad_B.DataTypeConversion[2]);
          _ssSetInputPortNumDimensions(rts, 2, 1);
          ssSetInputPortWidthAsInt(rts, 2, 1);
        }

        /* port 3 */
        {
          ssSetInputPortRequiredContiguous(rts, 3, 1);
          ssSetInputPortSignal(rts, 3, &control_Velocidad_B.DataTypeConversion[3]);
          _ssSetInputPortNumDimensions(rts, 3, 1);
          ssSetInputPortWidthAsInt(rts, 3, 1);
        }

        /* port 4 */
        {
          ssSetInputPortRequiredContiguous(rts, 4, 1);
          ssSetInputPortSignal(rts, 4, &control_Velocidad_B.DataTypeConversion[4]);
          _ssSetInputPortNumDimensions(rts, 4, 1);
          ssSetInputPortWidthAsInt(rts, 4, 1);
        }

        /* port 5 */
        {
          ssSetInputPortRequiredContiguous(rts, 5, 1);
          ssSetInputPortSignal(rts, 5, &control_Velocidad_B.DataTypeConversion[5]);
          _ssSetInputPortNumDimensions(rts, 5, 1);
          ssSetInputPortWidthAsInt(rts, 5, 1);
        }
      }

      /* path info */
      ssSetModelName(rts, "Digital output");
      ssSetPath(rts, "control_Velocidad/Digital output");
      ssSetRTModel(rts,control_Velocidad_M);
      ssSetParentSS(rts, (NULL));
      ssSetRootSS(rts, rts);
      ssSetVersion(rts, SIMSTRUCT_VERSION_LEVEL2);

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn0.params;
        ssSetSFcnParamsCount(rts, 6);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)
                       control_Velocidad_cal->Digitaloutput_P1_Size);
        ssSetSFcnParam(rts, 1, (mxArray*)
                       control_Velocidad_cal->Digitaloutput_P2_Size);
        ssSetSFcnParam(rts, 2, (mxArray*)
                       control_Velocidad_cal->Digitaloutput_P3_Size);
        ssSetSFcnParam(rts, 3, (mxArray*)
                       control_Velocidad_cal->Digitaloutput_P4_Size);
        ssSetSFcnParam(rts, 4, (mxArray*)
                       control_Velocidad_cal->Digitaloutput_P5_Size);
        ssSetSFcnParam(rts, 5, (mxArray*)
                       control_Velocidad_cal->Digitaloutput_P6_Size);
      }

      /* work vectors */
      ssSetIWork(rts, (int_T *) &control_Velocidad_DW.Digitaloutput_IWORK);
      ssSetPWork(rts, (void **) &control_Velocidad_DW.Digitaloutput_PWORK[0]);

      {
        struct _ssDWorkRecord *dWorkRecord = (struct _ssDWorkRecord *)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn0.dWork;
        struct _ssDWorkAuxRecord *dWorkAuxRecord = (struct _ssDWorkAuxRecord *)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn0.dWorkAux;
        ssSetSFcnDWork(rts, dWorkRecord);
        ssSetSFcnDWorkAux(rts, dWorkAuxRecord);
        ssSetNumDWorkAsInt(rts, 2);

        /* IWORK */
        ssSetDWorkWidthAsInt(rts, 0, 1);
        ssSetDWorkDataType(rts, 0,SS_INTEGER);
        ssSetDWorkComplexSignal(rts, 0, 0);
        ssSetDWork(rts, 0, &control_Velocidad_DW.Digitaloutput_IWORK);

        /* PWORK */
        ssSetDWorkWidthAsInt(rts, 1, 2);
        ssSetDWorkDataType(rts, 1,SS_POINTER);
        ssSetDWorkComplexSignal(rts, 1, 0);
        ssSetDWork(rts, 1, &control_Velocidad_DW.Digitaloutput_PWORK[0]);
      }

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

      /* adjust sample time */
      ssSetSampleTime(rts, 0, 3.8461538461538463E-5);
      ssSetOffsetTime(rts, 0, 0.0);
      sfcnTsMap[0] = 1;

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

      /* Update connectivity flags for each port */
      _ssSetInputPortConnected(rts, 0, 1);
      _ssSetInputPortConnected(rts, 1, 1);
      _ssSetInputPortConnected(rts, 2, 1);
      _ssSetInputPortConnected(rts, 3, 1);
      _ssSetInputPortConnected(rts, 4, 1);
      _ssSetInputPortConnected(rts, 5, 1);

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

    /* Level2 S-Function Block: control_Velocidad/<Root>/Digital input (sg_fpga_di_sf_a2) */
    {
      SimStruct *rts = control_Velocidad_M->childSfunctions[1];

      /* timing info */
      time_T *sfcnPeriod = control_Velocidad_M->NonInlinedSFcns.Sfcn1.sfcnPeriod;
      time_T *sfcnOffset = control_Velocidad_M->NonInlinedSFcns.Sfcn1.sfcnOffset;
      int_T *sfcnTsMap = control_Velocidad_M->NonInlinedSFcns.Sfcn1.sfcnTsMap;
      (void) std::memset(static_cast<void*>(sfcnPeriod), 0,
                         sizeof(time_T)*1);
      (void) std::memset(static_cast<void*>(sfcnOffset), 0,
                         sizeof(time_T)*1);
      ssSetSampleTimePtr(rts, &sfcnPeriod[0]);
      ssSetOffsetTimePtr(rts, &sfcnOffset[0]);
      ssSetSampleTimeTaskIDPtr(rts, sfcnTsMap);

      {
        ssSetBlkInfo2Ptr(rts, &control_Velocidad_M->NonInlinedSFcns.blkInfo2[1]);
      }

      _ssSetBlkInfo2PortInfo2Ptr(rts,
        &control_Velocidad_M->NonInlinedSFcns.inputOutputPortInfo2[1]);

      /* Set up the mdlInfo pointer */
      ssSetRTWSfcnInfo(rts, control_Velocidad_M->sfcnInfo);

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

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

      /* Allocate memory of model methods 4 */
      {
        ssSetModelMethods4(rts, &control_Velocidad_M->NonInlinedSFcns.methods4[1]);
      }

      /* Allocate memory for states auxilliary information */
      {
        ssSetStatesInfo2(rts, &control_Velocidad_M->NonInlinedSFcns.statesInfo2
                         [1]);
        ssSetPeriodicStatesInfo(rts,
          &control_Velocidad_M->NonInlinedSFcns.periodicStatesInfo[1]);
      }

      /* outputs */
      {
        ssSetPortInfoForOutputs(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn1.outputPortInfo[0]);
        ssSetPortInfoForOutputs(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn1.outputPortInfo[0]);
        _ssSetNumOutputPorts(rts, 2);
        _ssSetPortInfo2ForOutputUnits(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn1.outputPortUnits[0]);
        ssSetOutputPortUnit(rts, 0, 0);
        ssSetOutputPortUnit(rts, 1, 0);
        _ssSetPortInfo2ForOutputCoSimAttribute(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn1.outputPortCoSimAttribute[0]);
        ssSetOutputPortIsContinuousQuantity(rts, 0, 0);
        ssSetOutputPortIsContinuousQuantity(rts, 1, 0);

        /* port 0 */
        {
          _ssSetOutputPortNumDimensions(rts, 0, 1);
          ssSetOutputPortWidthAsInt(rts, 0, 1);
          ssSetOutputPortSignal(rts, 0, ((real_T *)
            &control_Velocidad_B.Digitalinput_o1));
        }

        /* port 1 */
        {
          _ssSetOutputPortNumDimensions(rts, 1, 1);
          ssSetOutputPortWidthAsInt(rts, 1, 1);
          ssSetOutputPortSignal(rts, 1, ((real_T *)
            &control_Velocidad_B.Digitalinput_o2));
        }
      }

      /* path info */
      ssSetModelName(rts, "Digital input");
      ssSetPath(rts, "control_Velocidad/Digital input");
      ssSetRTModel(rts,control_Velocidad_M);
      ssSetParentSS(rts, (NULL));
      ssSetRootSS(rts, rts);
      ssSetVersion(rts, SIMSTRUCT_VERSION_LEVEL2);

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn1.params;
        ssSetSFcnParamsCount(rts, 4);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)
                       control_Velocidad_cal->Digitalinput_P1_Size);
        ssSetSFcnParam(rts, 1, (mxArray*)
                       control_Velocidad_cal->Digitalinput_P2_Size);
        ssSetSFcnParam(rts, 2, (mxArray*)
                       control_Velocidad_cal->Digitalinput_P3_Size);
        ssSetSFcnParam(rts, 3, (mxArray*)
                       control_Velocidad_cal->Digitalinput_P4_Size);
      }

      /* work vectors */
      ssSetPWork(rts, (void **) &control_Velocidad_DW.Digitalinput_PWORK[0]);

      {
        struct _ssDWorkRecord *dWorkRecord = (struct _ssDWorkRecord *)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn1.dWork;
        struct _ssDWorkAuxRecord *dWorkAuxRecord = (struct _ssDWorkAuxRecord *)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn1.dWorkAux;
        ssSetSFcnDWork(rts, dWorkRecord);
        ssSetSFcnDWorkAux(rts, dWorkAuxRecord);
        ssSetNumDWorkAsInt(rts, 1);

        /* PWORK */
        ssSetDWorkWidthAsInt(rts, 0, 2);
        ssSetDWorkDataType(rts, 0,SS_POINTER);
        ssSetDWorkComplexSignal(rts, 0, 0);
        ssSetDWork(rts, 0, &control_Velocidad_DW.Digitalinput_PWORK[0]);
      }

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

      /* adjust sample time */
      ssSetSampleTime(rts, 0, 3.8461538461538463E-5);
      ssSetOffsetTime(rts, 0, 0.0);
      sfcnTsMap[0] = 1;

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

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

      /* Update the BufferDstPort flags for each input port */
    }

    /* Level2 S-Function Block: control_Velocidad/<Root>/IO397 Analog Input (sg_fpga_IO397_ad) */
    {
      SimStruct *rts = control_Velocidad_M->childSfunctions[2];

      /* timing info */
      time_T *sfcnPeriod = control_Velocidad_M->NonInlinedSFcns.Sfcn2.sfcnPeriod;
      time_T *sfcnOffset = control_Velocidad_M->NonInlinedSFcns.Sfcn2.sfcnOffset;
      int_T *sfcnTsMap = control_Velocidad_M->NonInlinedSFcns.Sfcn2.sfcnTsMap;
      (void) std::memset(static_cast<void*>(sfcnPeriod), 0,
                         sizeof(time_T)*1);
      (void) std::memset(static_cast<void*>(sfcnOffset), 0,
                         sizeof(time_T)*1);
      ssSetSampleTimePtr(rts, &sfcnPeriod[0]);
      ssSetOffsetTimePtr(rts, &sfcnOffset[0]);
      ssSetSampleTimeTaskIDPtr(rts, sfcnTsMap);

      {
        ssSetBlkInfo2Ptr(rts, &control_Velocidad_M->NonInlinedSFcns.blkInfo2[2]);
      }

      _ssSetBlkInfo2PortInfo2Ptr(rts,
        &control_Velocidad_M->NonInlinedSFcns.inputOutputPortInfo2[2]);

      /* Set up the mdlInfo pointer */
      ssSetRTWSfcnInfo(rts, control_Velocidad_M->sfcnInfo);

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

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

      /* Allocate memory of model methods 4 */
      {
        ssSetModelMethods4(rts, &control_Velocidad_M->NonInlinedSFcns.methods4[2]);
      }

      /* Allocate memory for states auxilliary information */
      {
        ssSetStatesInfo2(rts, &control_Velocidad_M->NonInlinedSFcns.statesInfo2
                         [2]);
        ssSetPeriodicStatesInfo(rts,
          &control_Velocidad_M->NonInlinedSFcns.periodicStatesInfo[2]);
      }

      /* outputs */
      {
        ssSetPortInfoForOutputs(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn2.outputPortInfo[0]);
        ssSetPortInfoForOutputs(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn2.outputPortInfo[0]);
        _ssSetNumOutputPorts(rts, 3);
        _ssSetPortInfo2ForOutputUnits(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn2.outputPortUnits[0]);
        ssSetOutputPortUnit(rts, 0, 0);
        ssSetOutputPortUnit(rts, 1, 0);
        ssSetOutputPortUnit(rts, 2, 0);
        _ssSetPortInfo2ForOutputCoSimAttribute(rts,
          &control_Velocidad_M->NonInlinedSFcns.Sfcn2.outputPortCoSimAttribute[0]);
        ssSetOutputPortIsContinuousQuantity(rts, 0, 0);
        ssSetOutputPortIsContinuousQuantity(rts, 1, 0);
        ssSetOutputPortIsContinuousQuantity(rts, 2, 0);

        /* port 0 */
        {
          _ssSetOutputPortNumDimensions(rts, 0, 1);
          ssSetOutputPortWidthAsInt(rts, 0, 1);
          ssSetOutputPortSignal(rts, 0, ((real_T *)
            &control_Velocidad_B.IO397AnalogInput_o1));
        }

        /* port 1 */
        {
          _ssSetOutputPortNumDimensions(rts, 1, 1);
          ssSetOutputPortWidthAsInt(rts, 1, 1);
          ssSetOutputPortSignal(rts, 1, ((real_T *)
            &control_Velocidad_B.IO397AnalogInput_o2));
        }

        /* port 2 */
        {
          _ssSetOutputPortNumDimensions(rts, 2, 1);
          ssSetOutputPortWidthAsInt(rts, 2, 1);
          ssSetOutputPortSignal(rts, 2, ((real_T *)
            &control_Velocidad_B.IO397AnalogInput_o3));
        }
      }

      /* path info */
      ssSetModelName(rts, "IO397 Analog Input");
      ssSetPath(rts, "control_Velocidad/IO397 Analog Input");
      ssSetRTModel(rts,control_Velocidad_M);
      ssSetParentSS(rts, (NULL));
      ssSetRootSS(rts, rts);
      ssSetVersion(rts, SIMSTRUCT_VERSION_LEVEL2);

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn2.params;
        ssSetSFcnParamsCount(rts, 9);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P1_Size);
        ssSetSFcnParam(rts, 1, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P2_Size);
        ssSetSFcnParam(rts, 2, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P3_Size);
        ssSetSFcnParam(rts, 3, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P4_Size);
        ssSetSFcnParam(rts, 4, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P5_Size);
        ssSetSFcnParam(rts, 5, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P6_Size);
        ssSetSFcnParam(rts, 6, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P7_Size);
        ssSetSFcnParam(rts, 7, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P8_Size);
        ssSetSFcnParam(rts, 8, (mxArray*)
                       control_Velocidad_cal->IO397AnalogInput_P9_Size);
      }

      /* work vectors */
      ssSetPWork(rts, (void **) &control_Velocidad_DW.IO397AnalogInput_PWORK[0]);

      {
        struct _ssDWorkRecord *dWorkRecord = (struct _ssDWorkRecord *)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn2.dWork;
        struct _ssDWorkAuxRecord *dWorkAuxRecord = (struct _ssDWorkAuxRecord *)
          &control_Velocidad_M->NonInlinedSFcns.Sfcn2.dWorkAux;
        ssSetSFcnDWork(rts, dWorkRecord);
        ssSetSFcnDWorkAux(rts, dWorkAuxRecord);
        ssSetNumDWorkAsInt(rts, 1);

        /* PWORK */
        ssSetDWorkWidthAsInt(rts, 0, 3);
        ssSetDWorkDataType(rts, 0,SS_POINTER);
        ssSetDWorkComplexSignal(rts, 0, 0);
        ssSetDWork(rts, 0, &control_Velocidad_DW.IO397AnalogInput_PWORK[0]);
      }

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

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

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

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

      /* Update the BufferDstPort flags for each input port */
    }
  }

  /* Start for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output' */
  /* Level2 S-Function Block: '<Root>/Digital output' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[0];
    sfcnStart(rts);
    if (ssGetErrorStatus(rts) != (NULL))
      return;
  }

  /* Start for S-Function (sg_fpga_di_sf_a2): '<Root>/Digital input' */
  /* Level2 S-Function Block: '<Root>/Digital input' (sg_fpga_di_sf_a2) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[1];
    sfcnStart(rts);
    if (ssGetErrorStatus(rts) != (NULL))
      return;
  }

  /* Start for S-Function (sg_fpga_IO397_ad): '<Root>/IO397 Analog Input' */
  /* Level2 S-Function Block: '<Root>/IO397 Analog Input' (sg_fpga_IO397_ad) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[2];
    sfcnStart(rts);
    if (ssGetErrorStatus(rts) != (NULL))
      return;
  }

  control_Velocidad_PrevZCX.TriggeredSubsystem_Trig_ZCE = POS_ZCSIG;
  control_Velocidad_PrevZCX.SampleandHold1_Trig_ZCE = POS_ZCSIG;

  /* InitializeConditions for DiscreteIntegrator: '<S1>/Discrete-Time Integrator' */
  control_Velocidad_DW.DiscreteTimeIntegrator_DSTATE =
    control_Velocidad_cal->DiscreteTimeIntegrator_IC;

  /* InitializeConditions for Delay: '<S3>/Delay1' */
  control_Velocidad_DW.Delay1_DSTATE =
    control_Velocidad_cal->Delay1_InitialCondition;

  /* InitializeConditions for Memory: '<S17>/Memory' */
  control_Velocidad_DW.Memory_PreviousInput =
    control_Velocidad_cal->EdgeDetector_ic;

  /* InitializeConditions for UnitDelay: '<S14>/UD' */
  control_Velocidad_DW.UD_DSTATE =
    control_Velocidad_cal->Difference1_ICPrevInput;

  /* InitializeConditions for Delay: '<S3>/Delay' */
  control_Velocidad_DW.Delay_DSTATE =
    control_Velocidad_cal->Delay_InitialCondition;

  /* SystemInitialize for Triggered SubSystem: '<S18>/Triggered Subsystem' */
  /* SystemInitialize for SignalConversion generated from: '<S21>/In1' incorporates:
   *  Outport: '<S21>/Out1'
   */
  control_Velocidad_B.In1 = control_Velocidad_cal->Out1_Y0;

  /* End of SystemInitialize for SubSystem: '<S18>/Triggered Subsystem' */

  /* SystemInitialize for Enabled SubSystem: '<S17>/NEGATIVE Edge' */
  /* SystemInitialize for RelationalOperator: '<S19>/Relational Operator1' incorporates:
   *  Outport: '<S19>/OUT'
   */
  control_Velocidad_B.RelationalOperator1_e = control_Velocidad_cal->OUT_Y0;

  /* End of SystemInitialize for SubSystem: '<S17>/NEGATIVE Edge' */

  /* SystemInitialize for Enabled SubSystem: '<S17>/POSITIVE Edge' */
  /* SystemInitialize for RelationalOperator: '<S20>/Relational Operator1' incorporates:
   *  Outport: '<S20>/OUT'
   */
  control_Velocidad_B.RelationalOperator1 = control_Velocidad_cal->OUT_Y0_p;

  /* End of SystemInitialize for SubSystem: '<S17>/POSITIVE Edge' */

  /* SystemInitialize for Triggered SubSystem: '<S3>/Sample and Hold1' */
  /* SystemInitialize for SignalConversion generated from: '<S16>/In' incorporates:
   *  Outport: '<S16>/ '
   */
  control_Velocidad_B.In = control_Velocidad_cal->_Y0;

  /* End of SystemInitialize for SubSystem: '<S3>/Sample and Hold1' */
}

/* Model terminate function */
void control_Velocidad_terminate(void)
{
  /* Terminate for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output' */
  /* Level2 S-Function Block: '<Root>/Digital output' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[0];
    sfcnTerminate(rts);
  }

  /* Terminate for S-Function (sg_fpga_di_sf_a2): '<Root>/Digital input' */
  /* Level2 S-Function Block: '<Root>/Digital input' (sg_fpga_di_sf_a2) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[1];
    sfcnTerminate(rts);
  }

  /* Terminate for S-Function (sg_fpga_IO397_ad): '<Root>/IO397 Analog Input' */
  /* Level2 S-Function Block: '<Root>/IO397 Analog Input' (sg_fpga_IO397_ad) */
  {
    SimStruct *rts = control_Velocidad_M->childSfunctions[2];
    sfcnTerminate(rts);
  }

  /* user code (Terminate function Trailer) */
  {
    freeFPGAModuleSgLib((uint32_t)1);
  }
}
