/*
 * 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.
 *
 * File: GearPosition.c
 *
 * Code generated for Simulink model 'GearPosition'.
 *
 * Model version                  : 13.12
 * Simulink Coder version         : 24.1 (R2024a) 19-Nov-2023
 * C/C++ source code generated on : Wed May  7 16:00:58 2025
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: NXP->Cortex-M4
 * Code generation objectives:
 *    1. Execution efficiency
 *    2. RAM efficiency
 * Validation result: Not run
 */

#include "GearPosition.h"

/* Block signals and states (default storage) */
GearPosition_DW GearPosition_DW_l;

/* External inputs (root inport signals with default storage) */
GearPosition_ExtU GearPosition_U;

/* External outputs (root outports fed by signals with default storage) */
GearPosition_ExtY GearPosition_Y;

/* Real-time model */
static GearPosition_RT_MODEL GearPosition_M_;
GearPosition_RT_MODEL *const GearPosition_M = &GearPosition_M_;

/* Model step function */
void GearPosition_step(void)
{
  double GearPosition_Memory;

  /* Delay: '<S1>/Memory' */
  GearPosition_Memory = GearPosition_DW_l.GearPosition_Memory_DSTATE;

  /* MATLAB Function: '<S1>/Gear_FUNCTION1' incorporates:
   *  Constant: '<S2>/Constant'
   *  Inport: '<Root>/BS_CMD'
   *  Inport: '<Root>/GEAR_POSITION_CMD'
   *  Inport: '<Root>/Vehicle_Speed'
   *  RelationalOperator: '<S2>/Compare'
   */
  if (GearPosition_Memory == 0.0) {
    if (GearPosition_U.GearPosition_Vehicle_Speed <= 2.0) {
      if (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 2.0) {
        GearPosition_Memory = 2.0;
      } else if (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 1.0) {
        GearPosition_Memory = 2.0;
      } else if (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 3.0) {
        GearPosition_Memory = 2.0;
      }
    }
  } else if (GearPosition_Memory == 2.0) {
    if ((GearPosition_U.GearPosition_Vehicle_Speed <= 2.0) &&
        (GearPosition_U.GearPosition_BS_CMD >= 20.0)) {
      if (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 1.0) {
        GearPosition_Memory = 1.0;
      } else if (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 3.0) {
        GearPosition_Memory = 3.0;
      } else if (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 0.0) {
        GearPosition_Memory = 0.0;
      }
    }
  } else if (GearPosition_Memory == 1.0) {
    if (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 2.0) {
      GearPosition_Memory = 2.0;
    }
  } else if ((GearPosition_Memory == 3.0) &&
             (GearPosition_U.GearPosition_GEAR_POSITION_CMD == 2.0)) {
    GearPosition_Memory = 2.0;
  }

  /* End of MATLAB Function: '<S1>/Gear_FUNCTION1' */

  /* Outport: '<Root>/G_Position_output' */
  GearPosition_Y.GearPosition_G_Position_output = GearPosition_Memory;

  /* Update for Delay: '<S1>/Memory' */
  GearPosition_DW_l.GearPosition_Memory_DSTATE = GearPosition_Memory;
}

/* Model initialize function */
void GearPosition_initialize(void)
{
  /* (no initialization code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
