Challenge

Project 2 Adaptive Cruise Control

Aim: To develop a Logic of Adaptive Cruise Control feature using MATLAB Simulink as per the requirement document.

Objective:

  1. Developing Adaptive Cruise Control feature as per the Requirement Document using MATLAB Simulink.
  2. Follow all the MBD related processes Requirement Tagging & Traceability, SLDD creation, Configuration Parameter changes, Model Advisor check & Code Generation.
  3. In Configuration Parameters: enable “Support Floating Numbers” under Code Generation settings.
  4. Use Embedded Coder to generate the code.
  5. If choosing code generation, Storage class for Input signals: ImportedExtern; Storage class for Output signal: Export to File; Storage class for local signals: localizable; Storage class for calibration signals: Const.
  6. Choose sample time for all signals as 0.01s

General Overview:

Adaptive Cruise Control Feature for passenger cars allows the host vehicle to adapt to the speed in line with the flow of traffic. Driving in heavy traffic or keeping a safe distance to the preceding vehicle calls for a high level of concentration. The Adaptive Cruise Control feature can reduce the stress on the driver by automatically controlling the vehicle speed & maintaining a predefined minimum distance to the preceding vehicle. As a consequence, the driver enjoys more comfort & can concentrate on the road little better.

A radar sensor is usually at the core of the Adaptive Cruise Control. Installed at the front of the vehicle, the system permanently monitors the road ahead. As long as the road ahead is clear, cruise control feature maintains the speed set by the driver. If the system spots a slower vehicle within its detection range, it gently reduces speed by releasing the accelerator or actively engaging the brake control system. If the vehicle ahead speeds up or changes lanes, the cruise control automatically accelerates to the driver’s desired speed.

Standard Adaptive Cruise Control can be activated from speeds of around 30 km/h (20 mph) upwards and supports the driver, primarily on cross-country journeys or on freeways. The cruise control stop & go variant is also active at speeds below 30 km/h (20 mph). It can maintain the set distance to the preceding vehicle even at very low speeds and can decelerate to a complete standstill. When the vehicle remains stopped longer, the driver needs only to reactivate the system, for example by briefly stepping on the gas pedal to return to cruise control mode. In this way, cruise control stop & go supports the driver even in heavy traffic and traffic jams.

Since Adaptive Cruise Control is a comfort and convenience system, brake interventions and vehicle acceleration only take place within defined limits. Even with Adaptive Cruise Control switched on, it remains the driver’s responsibility to monitor the speed and distance from the vehicle in front.

Model Development Procedure:

1. Create a new folder named ACC, add it to the MATLAB path and create a new Simulink file.

Under the model settings change solver type as fixed step, solver to discrete(no continuous state), fundamental sample time as 0.01s, system target file as ert.tlc (embedded coder)

2. Creation of a SLDD file using the data given and link it to the model.

The model is linked with data dictionary which have the values of signals with their data types, dimensions, storage class etc. which is given in the question

3. Make a subsystem named Adaptive_Cruise_Control.

Under the Adaptive_Cruise_Control Subsystem Create the Three more subsystems named Lead_Vehicle , Drive_Vehicle and ACC_Algorithm.

The requirement conditions for these Subsystems are given

4. Creation of Lead_vehicle subsystem logic.

Requirement conditions for Lead_vehicle:

  • Lead Vehicle is a vehicle which is driving in the road ahead of our drive vehicle. Two input signals are CameraInput_LeadVehicle & RadarInput_LeadVehicle.
  • Ideally sensor fusion techniques will be deployed to process & analyze data from camera & radar. For complexity reasons, let’s not adapt to any such algorithms.
  • We can simply add both the radar & camera inputs & the corresponding output is read as Speed profile output (Signal Name: LeadVehicle_Speed).
  • Speed data of the lead vehicle is critical in implementing the Adaptive Cruise Control algorithm.

Lead_Vehicle connection Logic:

5. Creation of Drive_vehicle subsystem logic.

Requirement conditions for Drive_Vehicle:

  • Drive Vehicle is the vehicle driven by the user & this is the vehicle which has ACC algorithm in it.
  • Like the Lead Vehicle, Drive Vehicle algorithm also has 2 input signals are CameraInput_DriveVehicle, RadarInput_DriveVehicle & one signal coming as an Input to this subsystem is Acceleration_Mode – three inputs into this requirement in total.
  • Like the above requirement, sensor fusion techniques will also be deployed here, for complexity reasons we are ignoring them.
  • Two output signals come from this subsystem are DriveVehicle_Speed & LeadVehicle_Detected.
  • Signal DriveVehicle_Speed is summation of three input signals mentioned above & LeadVehicle_Detected is renamed from Input Signal RadarInput_DriveVehicle by mere use of Signal Conversion block.

Drive_Vehicle Connection Logic:

6. Creation of ACC_Algorithm Subsystem logic:

  • Adaptive Cruise Control feature has 3 major modes of operation: OFF Mode, STANDBY Mode & ON Mode. This particular requirement has to be implemented as state machine logic in Simulink.
  • The input signals to this state machine system are Time_Gap, Set_Speed, Set_Gap, CruiseSwitch, SetSwitch.
  • Also, the output signals DriveVehicle_Speed & LeadVehicle_Detected from requirement-2 is fed back as an input signal into this state machine block.
  • Additionally, output signal LeadVehicle_Speed from requirement-1 is given as an as well.
  • Output from this subsystem is a signal Acceleration_Mode which governs the vehicular speed of the drive vehicle which automatically adjusts its speed & velocity to match the lead vehicle.

Requirement 3a – ACC OFF MODE state logic:

  • This is the default state inside state machine logic. Output signal Acceleration_Mode is at value 0 in this state.
  • This state is governed by input signal CruiseSwitch.
  • If CruiseSwitch is equal to 1, state ACC STANDBY mode will get activated. If CruiseSwitch is equal to 0, state ACC OFF mode will get activated, from either ACC ON mode or ACC STANDBY mode

Requirement 3b – ACC STANDBY MODE state logic:

  • This is the second activated state inside state machine logic. Output signal Acceleration_Mode is at value 1 in this state.
  • This state is governed by both input signals CruiseSwitch & SetSwitch.
  • If CruiseSwitch is equal to 1, state ACC STANDBY mode will get activated. If CruiseSwitch is equal to 0, state ACC OFF mode will get activated, from either ACC ON mode or ACC STANDBY mode
  • If SetSwitch is equal to 1, state ACC ON mode will get activated. If SetSwitch is equal to 0, state ACC STANDBY mode will get activated.

Requirement 3c – ACC ON MODE state logic:

This state will be activated when input signal SetSwitch is equal to 1. There are 6 sub states to this state logic: They are:

  • LeadVehicle_Detected_Follow (Default)
  • LeadVehicle_Not_Detected
  • LeadVehicle_Detected_Resume
  • LeadVehicle_Not_Detected_Resume
  • LeadVehicle_Speed_lessthan_Set_Speed
  • LeadVehicle_Speed_equal_Set_Speed

Requirement 3c (i) – LeadVehicle_Detected_Follow (ACC ON MODE):

  • This is the default sub state inside ACC ON MODE state. Output signal Acceleration_Mode is equal to 2.
  • Condition to transit from LeadVehicle_Detected_Follow to LeadVehicle_Not_Detected; Input signal condition LeadVehicle_Detected == 0.
  • Condition to transit from LeadVehicle_Detected_Follow to LeadVehicle_Speed_lessthan_Set_Speed; Input Signals condition (LeadVehicle_Detected == 1) && (LeadVehicle_Speed < Set_Speed) || (Time_Gap < Set_Gap).

Requirement 3c (ii) – LeadVehicle_Not_Detected (ACC ON MODE):

  • Output signal Acceleration_Mode is equal to 1.
  • Condition to transit from LeadVehicle_Not_Detected to LeadVehicle_Detected_Follow; Input signals condition [(LeadVehicle_Detected==1) && (DriveVehicle_Speed == Set_Speed) && (LeadVehicle_Speed >= Set_Speed) && (Time_Gap >= Set_Gap)]
  • Condition to transit from LeadVehicle_Not_Detected to LeadVehicle_Speed_lessthan_Set_Speed; Input signals condition [(LeadVehicle_Detected == 1) && (LeadVehicle_Speed < Set_Speed) || (Time_Gap < Set_Gap)]

Requirement 3c (iii) – LeadVehicle_Detected_Resume (ACC ON MODE):

  • Output signal Acceleration_Mode is equal to 3.
  • Condition to transit from LeadVehicle_Detected_Resume to LeadVehicle_Detected_Follow; Input signals condition [(DriveVehicle_Speed == Set_Speed) && (LeadVehicle_Speed >= Set_Speed) && (Time_Gap >= Set_Gap)]
  • Condition to transit from LeadVehicle_Detected_Resume to LeadVehicle_Not_Detected_Resume; Input signal condition LeadVehicle_Detected==0.
  • Condition to transit from LeadVehicle_Detected_Resume to LeadVehicle_Speed_equal_Set_Speed; Input Signal condition [(DriveVehicle_Speed < Set_Speed) && (LeadVehicle_Speed > DriveVehicle_Speed) && (Time_Gap >= Set_Gap)]

Requirement 3c (iv) - LeadVehicle_Not_Detected_Resume (ACC ON MODE):

  • Output signal Acceleration_Mode is equal to 1.

Requirement 3c (v) - LeadVehicle_Speed_lessthan_Set_Speed (ACC ON MODE):

  • Output signal Acceleration_Mode is equal to 4.
  • Condition to transit from LeadVehicle_Speed_lessthan_Set_Speed to LeadVehicle_Not_Detected; Input signal condition [(LeadVehicle_Detected == 0) && (DriveVehicle_Speed == Set_Speed)]
  • Condition to transit from LeadVehicle_Speed_lessthan_Set_Speed to LeadVehicle_Speed_equal_Set_Speed; Input signals condition [((LeadVehicle_Speed*1.25>=DriveVehicle_Speed) && (LeadVehicle_Speed * 0.75<=DriveVehicle_Speed)) && (DriveVehicle_Speed < Set_Speed) && ((Time_Gap<=1.25*Set_Gap) && (Time_Gap >=0.75*Set_Gap))]

Requirement 3c (vi) - LeadVehicle_Speed_equal_Set_Speed (ACC ON MODE):

  • Output signal Acceleration_Mode is equal to 5.
  • Condition to transit from LeadVehicle_Speed_equal_Set_Speed to LeadVehicle_Not_Detected_Resume; Input signal conditions is [(LeadVehicle_Detected == 0) || (DriveVehicle_Speed <= Set_Speed)]
  • Condition to transit from LeadVehicle_Speed_equal_Set_Speed to LeadVehicle_Detected_Resume; Input signal conditions [(DriveVehicle_Speed < Set_Speed) && (LeadVehicle_Speed > DriveVehicle_Speed) || (Time_Gap >= Set_Gap)]
  • Condition to transit from LeadVehicle_Speed_equal_Set_Speed to LeadVehicle_Speed_lessthan_Set_Speed; Input signals conditions [(LeadVehicle_Speed

ACC_Algotithm Logic State flow:

ACC_Logic signal indication:

7. Resolving and propagating the signals:

the signals which appears for the first time are resolved and afterwards that gets propagated.

Final View of our Adaptive cruise control model:

8. Requirement Tagging:

Make the word file having all the supporting documents with the requirements and tag the requirements accordingly

9. Model Advisory Check for the MAB guidelines:

The model advisory check for MAB guidelines ran and we get 123 pass conditions and 18 warnings which report has been attached.

10. Embedded C code generation.

C code generated for our model whose folder is attached.

#include "Adaptive_Cruise_Control_sf.h"
#include "rtwtypes.h"
#include "Adaptive_Cruise_Control_sf_private.h"
#include "simstruc.h"
#include "fixedpoint.h"

/* Named constants for Chart: '<S2>/ACC_Logic' */
#define Ada_IN_LeadVehicle_Not_Detected ((uint8_T)3U)
#define Adaptive_Cr_IN_ACC_STANDBY_MODE ((uint8_T)3U)
#define Adaptive_Cru_IN_NO_ACTIVE_CHILD ((uint8_T)0U)
#define Adaptive_Cruise_IN_ACC_OFF_MODE ((uint8_T)1U)
#define Adaptive_Cruise__IN_ACC_ON_MODE ((uint8_T)2U)
#define IN_LeadVehicle_Detected_Follow ((uint8_T)1U)
#define IN_LeadVehicle_Detected_Resume ((uint8_T)2U)
#define IN_LeadVehicle_Not_Detected_Res ((uint8_T)4U)
#define IN_LeadVehicle_Speed_equal_Set_ ((uint8_T)5U)
#define IN_LeadVehicle_Speed_lessthan_S ((uint8_T)6U)
#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)

extern void *Adaptive_Cruise_Control_malloc(SimStruct *S);

#endif

#ifndef __RTW_UTFREE__
#if defined (MATLAB_MEX_FILE)

extern void * utMalloc(size_t);
extern void utFree(void *);

#endif
#endif                                 /* #ifndef __RTW_UTFREE__ */

#if defined(MATLAB_MEX_FILE)
#include "rt_nonfinite.c"
#endif

static const char_T *RT_MEMORY_ALLOCATION_ERROR =
  "memory allocation error in generated S-Function";

/* System initialize for root system: '<Root>' */
#define MDL_INITIALIZE_CONDITIONS

static void mdlInitializeConditions(SimStruct *S)
{
  if (ssIsFirstInitCond(S)) {
    B_Adaptive_Cruise_Control_T *_rtB;
    _rtB = ((B_Adaptive_Cruise_Control_T *) ssGetLocalBlockIO(S));

    /* SystemInitialize for Atomic SubSystem: '<Root>/Adaptive_Cruise_Control' */
    /* InitializeConditions for UnitDelay: '<S1>/Unit Delay' */
    ((uint8_T *)ssGetDWork(S, 0))[0] = 0U;

    /* SystemInitialize for Chart: '<S2>/ACC_Logic' */
    ((uint8_T *)ssGetDWork(S, 3))[0] = Adaptive_Cru_IN_NO_ACTIVE_CHILD;
    ((uint8_T *)ssGetDWork(S, 1))[0] = 0U;
    ((uint8_T *)ssGetDWork(S, 2))[0] = Adaptive_Cru_IN_NO_ACTIVE_CHILD;
    _rtB->Acceleration_Mode_h = 0U;

    /* End of SystemInitialize for SubSystem: '<Root>/Adaptive_Cruise_Control' */
  } else {
    B_Adaptive_Cruise_Control_T *_rtB;
    _rtB = ((B_Adaptive_Cruise_Control_T *) ssGetLocalBlockIO(S));

    /* SystemReset for Atomic SubSystem: '<Root>/Adaptive_Cruise_Control' */
    /* InitializeConditions for UnitDelay: '<S1>/Unit Delay' */
    ((uint8_T *)ssGetDWork(S, 0))[0] = 0U;

    /* SystemReset for Chart: '<S2>/ACC_Logic' */
    ((uint8_T *)ssGetDWork(S, 3))[0] = Adaptive_Cru_IN_NO_ACTIVE_CHILD;
    ((uint8_T *)ssGetDWork(S, 1))[0] = 0U;
    ((uint8_T *)ssGetDWork(S, 2))[0] = Adaptive_Cru_IN_NO_ACTIVE_CHILD;
    _rtB->Acceleration_Mode_h = 0U;

    /* End of SystemReset for SubSystem: '<Root>/Adaptive_Cruise_Control' */
  }
}

/* Start for root system: '<Root>' */
#define MDL_START

static void mdlStart(SimStruct *S)
{
  /* instance underlying S-Function data */
#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)
#if defined(MATLAB_MEX_FILE)

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

#endif

  Adaptive_Cruise_Control_malloc(S);
  if (ssGetErrorStatus(S) != (NULL) ) {
    return;
  }

#endif

  {
  }
}

/* Outputs for root system: '<Root>' */
static void mdlOutputs(SimStruct *S, int_T tid)
{
  B_Adaptive_Cruise_Control_T *_rtB;
  _rtB = ((B_Adaptive_Cruise_Control_T *) ssGetLocalBlockIO(S));

  /* Outputs for Atomic SubSystem: '<Root>/Adaptive_Cruise_Control' */
  /* Sum: '<S4>/Add' */
  _rtB->LeadVehicle_Speed = (uint8_T)((uint32_T)*((const uint8_T **)
    ssGetInputPortSignalPtrs(S, 0))[0] + *((const uint8_T **)
    ssGetInputPortSignalPtrs(S, 1))[0]);

  /* UnitDelay: '<S1>/Unit Delay' */
  _rtB->Acceleration_Mode = ((uint8_T *)ssGetDWork(S, 0))[0];

  /* Sum: '<S3>/Add' */
  _rtB->DriveVehicle_Speed = (uint8_T)((uint32_T)(uint8_T)((uint32_T)*((const
    uint8_T **)ssGetInputPortSignalPtrs(S, 2))[0] + _rtB->Acceleration_Mode) +
    *((const uint8_T **)ssGetInputPortSignalPtrs(S, 3))[0]);

  /* SignalConversion: '<S3>/Signal Conversion' */
  _rtB->LeadVehicle_Detected = *((const uint8_T **)ssGetInputPortSignalPtrs(S, 3))
    [0];

  /* Chart: '<S2>/ACC_Logic' */
  if (((uint8_T *)ssGetDWork(S, 1))[0] == 0U) {
    ((uint8_T *)ssGetDWork(S, 1))[0] = 1U;
    if (!*((const boolean_T **)ssGetInputPortSignalPtrs(S, 8))[0]) {
      ((uint8_T *)ssGetDWork(S, 2))[0] = Adaptive_Cr_IN_ACC_STANDBY_MODE;
      _rtB->Acceleration_Mode_h = 1U;
    } else {
      ((uint8_T *)ssGetDWork(S, 2))[0] = Adaptive_Cruise_IN_ACC_OFF_MODE;
      _rtB->Acceleration_Mode_h = 0U;
    }
  } else {
    switch (((uint8_T *)ssGetDWork(S, 2))[0]) {
     case Adaptive_Cruise_IN_ACC_OFF_MODE:
      _rtB->Acceleration_Mode_h = 0U;
      if (*((const boolean_T **)ssGetInputPortSignalPtrs(S, 4))[0]) {
        ((uint8_T *)ssGetDWork(S, 2))[0] = Adaptive_Cr_IN_ACC_STANDBY_MODE;
        _rtB->Acceleration_Mode_h = 1U;
      }
      break;

     case Adaptive_Cruise__IN_ACC_ON_MODE:
      switch (((uint8_T *)ssGetDWork(S, 3))[0]) {
       case IN_LeadVehicle_Detected_Follow:
        _rtB->Acceleration_Mode_h = 2U;
        if (_rtB->LeadVehicle_Detected == 0) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = Ada_IN_LeadVehicle_Not_Detected;
          _rtB->Acceleration_Mode_h = 1U;
        } else if (((_rtB->LeadVehicle_Detected == 1) &&
                    (_rtB->LeadVehicle_Speed < *((const uint8_T **)
                      ssGetInputPortSignalPtrs(S, 6))[0])) || (*((const uint8_T **)
                     ssGetInputPortSignalPtrs(S, 5))[0] < *((const uint8_T **)
                     ssGetInputPortSignalPtrs(S, 7))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Speed_lessthan_S;
          _rtB->Acceleration_Mode_h = 4U;
        }
        break;

       case IN_LeadVehicle_Detected_Resume:
        _rtB->Acceleration_Mode_h = 3U;
        if ((_rtB->DriveVehicle_Speed == *((const uint8_T **)
              ssGetInputPortSignalPtrs(S, 6))[0]) && (_rtB->LeadVehicle_Speed >=
             *((const uint8_T **)ssGetInputPortSignalPtrs(S, 6))[0]) && (*((
               const uint8_T **)ssGetInputPortSignalPtrs(S, 5))[0] >= *((const
               uint8_T **)ssGetInputPortSignalPtrs(S, 7))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Detected_Follow;
          _rtB->Acceleration_Mode_h = 2U;
        } else if (_rtB->LeadVehicle_Detected == 0) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Not_Detected_Res;
          _rtB->Acceleration_Mode_h = 1U;
        } else if ((_rtB->DriveVehicle_Speed < *((const uint8_T **)
                     ssGetInputPortSignalPtrs(S, 6))[0]) &&
                   (_rtB->LeadVehicle_Speed > _rtB->DriveVehicle_Speed) && (*((
                      const uint8_T **)ssGetInputPortSignalPtrs(S, 5))[0] >= *((
                      const uint8_T **)ssGetInputPortSignalPtrs(S, 7))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Speed_equal_Set_;
          _rtB->Acceleration_Mode_h = 0U;
        }
        break;

       case Ada_IN_LeadVehicle_Not_Detected:
        _rtB->Acceleration_Mode_h = 1U;
        if ((_rtB->LeadVehicle_Detected == 1) && (_rtB->DriveVehicle_Speed ==
             *((const uint8_T **)ssGetInputPortSignalPtrs(S, 6))[0]) &&
            (_rtB->LeadVehicle_Speed >= *((const uint8_T **)
              ssGetInputPortSignalPtrs(S, 6))[0]) && (*((const uint8_T **)
              ssGetInputPortSignalPtrs(S, 5))[0] >= *((const uint8_T **)
              ssGetInputPortSignalPtrs(S, 7))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Detected_Follow;
          _rtB->Acceleration_Mode_h = 2U;
        } else if (((_rtB->LeadVehicle_Detected == 1) &&
                    (_rtB->LeadVehicle_Speed < *((const uint8_T **)
                      ssGetInputPortSignalPtrs(S, 6))[0])) || (*((const uint8_T **)
                     ssGetInputPortSignalPtrs(S, 5))[0] < *((const uint8_T **)
                     ssGetInputPortSignalPtrs(S, 7))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Speed_lessthan_S;
          _rtB->Acceleration_Mode_h = 4U;
        }
        break;

       case IN_LeadVehicle_Not_Detected_Res:
        _rtB->Acceleration_Mode_h = 1U;
        break;

       case IN_LeadVehicle_Speed_equal_Set_:
        _rtB->Acceleration_Mode_h = 0U;
        if (((_rtB->DriveVehicle_Speed < *((const uint8_T **)
               ssGetInputPortSignalPtrs(S, 6))[0]) && (_rtB->LeadVehicle_Speed >
              _rtB->DriveVehicle_Speed)) || (*((const uint8_T **)
              ssGetInputPortSignalPtrs(S, 5))[0] >= *((const uint8_T **)
              ssGetInputPortSignalPtrs(S, 7))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Detected_Resume;
          _rtB->Acceleration_Mode_h = 3U;
        } else if ((_rtB->LeadVehicle_Detected == 0) ||
                   (_rtB->DriveVehicle_Speed <= *((const uint8_T **)
                     ssGetInputPortSignalPtrs(S, 6))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Not_Detected_Res;
          _rtB->Acceleration_Mode_h = 1U;
        } else if (((_rtB->LeadVehicle_Speed < *((const uint8_T **)
                      ssGetInputPortSignalPtrs(S, 6))[0]) &&
                    (_rtB->LeadVehicle_Speed < _rtB->DriveVehicle_Speed)) ||
                   (0.75 * (real_T)*((const uint8_T **)ssGetInputPortSignalPtrs
                     (S, 7))[0] == *((const uint8_T **)ssGetInputPortSignalPtrs
                     (S, 5))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Speed_lessthan_S;
          _rtB->Acceleration_Mode_h = 4U;
        }
        break;

       default:
        /* case IN_LeadVehicle_Speed_lessthan_Set_Speed: */
        _rtB->Acceleration_Mode_h = 4U;
        if ((_rtB->LeadVehicle_Detected == 0) && (_rtB->DriveVehicle_Speed ==
             *((const uint8_T **)ssGetInputPortSignalPtrs(S, 6))[0])) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = Ada_IN_LeadVehicle_Not_Detected;
          _rtB->Acceleration_Mode_h = 1U;
        } else if (((real_T)_rtB->LeadVehicle_Speed * 1.25 >=
                    _rtB->DriveVehicle_Speed) && ((real_T)
                    _rtB->LeadVehicle_Speed * 0.75 <= _rtB->DriveVehicle_Speed) &&
                   (_rtB->DriveVehicle_Speed < *((const uint8_T **)
                     ssGetInputPortSignalPtrs(S, 6))[0]) && ((*((const uint8_T **)
                      ssGetInputPortSignalPtrs(S, 5))[0] <= 1.25 * (real_T)*((
            const uint8_T **)ssGetInputPortSignalPtrs(S, 7))[0]) && (*((const
            uint8_T **)ssGetInputPortSignalPtrs(S, 5))[0] >= 0.75 * (real_T)*((
            const uint8_T **)ssGetInputPortSignalPtrs(S, 7))[0]))) {
          ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Speed_equal_Set_;
          _rtB->Acceleration_Mode_h = 0U;
        }
        break;
      }
      break;

     default:
      /* case IN_ACC_STANDBY_MODE: */
      _rtB->Acceleration_Mode_h = 1U;
      if (!*((const boolean_T **)ssGetInputPortSignalPtrs(S, 4))[0]) {
        ((uint8_T *)ssGetDWork(S, 2))[0] = Adaptive_Cruise_IN_ACC_OFF_MODE;
        _rtB->Acceleration_Mode_h = 0U;
      } else if (*((const boolean_T **)ssGetInputPortSignalPtrs(S, 8))[0]) {
        ((uint8_T *)ssGetDWork(S, 2))[0] = Adaptive_Cruise__IN_ACC_ON_MODE;
        ((uint8_T *)ssGetDWork(S, 3))[0] = IN_LeadVehicle_Detected_Follow;
        _rtB->Acceleration_Mode_h = 2U;
      }
      break;
    }
  }

  /* End of Chart: '<S2>/ACC_Logic' */
  /* End of Outputs for SubSystem: '<Root>/Adaptive_Cruise_Control' */

  /* Outport: '<Root>/Acceleration_Mode' */
  ((uint8_T *)ssGetOutputPortSignal(S, 0))[0] = _rtB->Acceleration_Mode_h;
  UNUSED_PARAMETER(tid);
}

/* Update for root system: '<Root>' */
#define MDL_UPDATE

static void mdlUpdate(SimStruct *S, int_T tid)
{
  B_Adaptive_Cruise_Control_T *_rtB;
  _rtB = ((B_Adaptive_Cruise_Control_T *) ssGetLocalBlockIO(S));

  /* Update for Atomic SubSystem: '<Root>/Adaptive_Cruise_Control' */
  /* Update for UnitDelay: '<S1>/Unit Delay' */
  ((uint8_T *)ssGetDWork(S, 0))[0] = _rtB->Acceleration_Mode_h;

  /* End of Update for SubSystem: '<Root>/Adaptive_Cruise_Control' */
  UNUSED_PARAMETER(tid);
}

/* Termination for root system: '<Root>' */
static void mdlTerminate(SimStruct *S)
{

#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)

  if (ssGetUserData(S) != (NULL) ) {
    rt_FREE(ssGetLocalBlockIO(S));
  }

  rt_FREE(ssGetUserData(S));

#endif

}

#if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE)
#include "Adaptive_Cruise_Control_mid.h"
#endif

/* Function to initialize sizes. */
static void mdlInitializeSizes(SimStruct *S)
{
  ssSetNumSampleTimes(S, 1);           /* Number of sample times */
  ssSetNumContStates(S, 0);            /* Number of continuous states */
  ssSetNumNonsampledZCs(S, 0);         /* Number of nonsampled ZCs */

  /* Number of output ports */
  if (!ssSetNumOutputPorts(S, 1))
    return;

  /* outport number: 0 */
  if (!ssSetOutputPortVectorDimension(S, 0, 1))
    return;
  if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
    ssSetOutputPortDataType(S, 0, SS_UINT8);
  }

  ssSetOutputPortSampleTime(S, 0, 0.01);
  ssSetOutputPortOffsetTime(S, 0, 0.0);
  ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);

  /* Number of input ports */
  if (!ssSetNumInputPorts(S, 9))
    return;

  /* inport number: 0 */
  {
    if (!ssSetInputPortVectorDimension(S, 0, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 0, SS_UINT8);
    }

    ssSetInputPortDirectFeedThrough(S, 0, 1);
    ssSetInputPortSampleTime(S, 0, 0.01);
    ssSetInputPortOffsetTime(S, 0, 0.0);
    ssSetInputPortOverWritable(S, 0, 0);
    ssSetInputPortOptimOpts(S, 0, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 1 */
  {
    if (!ssSetInputPortVectorDimension(S, 1, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 1, SS_UINT8);
    }

    ssSetInputPortDirectFeedThrough(S, 1, 1);
    ssSetInputPortSampleTime(S, 1, 0.01);
    ssSetInputPortOffsetTime(S, 1, 0.0);
    ssSetInputPortOverWritable(S, 1, 0);
    ssSetInputPortOptimOpts(S, 1, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 2 */
  {
    if (!ssSetInputPortVectorDimension(S, 2, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 2, SS_UINT8);
    }

    ssSetInputPortDirectFeedThrough(S, 2, 1);
    ssSetInputPortSampleTime(S, 2, 0.01);
    ssSetInputPortOffsetTime(S, 2, 0.0);
    ssSetInputPortOverWritable(S, 2, 0);
    ssSetInputPortOptimOpts(S, 2, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 3 */
  {
    if (!ssSetInputPortVectorDimension(S, 3, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 3, SS_UINT8);
    }

    ssSetInputPortDirectFeedThrough(S, 3, 1);
    ssSetInputPortSampleTime(S, 3, 0.01);
    ssSetInputPortOffsetTime(S, 3, 0.0);
    ssSetInputPortOverWritable(S, 3, 0);
    ssSetInputPortOptimOpts(S, 3, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 4 */
  {
    if (!ssSetInputPortVectorDimension(S, 4, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 4, SS_BOOLEAN);
    }

    ssSetInputPortDirectFeedThrough(S, 4, 1);
    ssSetInputPortSampleTime(S, 4, 0.01);
    ssSetInputPortOffsetTime(S, 4, 0.0);
    ssSetInputPortOverWritable(S, 4, 0);
    ssSetInputPortOptimOpts(S, 4, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 5 */
  {
    if (!ssSetInputPortVectorDimension(S, 5, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 5, SS_UINT8);
    }

    ssSetInputPortDirectFeedThrough(S, 5, 1);
    ssSetInputPortSampleTime(S, 5, 0.01);
    ssSetInputPortOffsetTime(S, 5, 0.0);
    ssSetInputPortOverWritable(S, 5, 0);
    ssSetInputPortOptimOpts(S, 5, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 6 */
  {
    if (!ssSetInputPortVectorDimension(S, 6, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 6, SS_UINT8);
    }

    ssSetInputPortDirectFeedThrough(S, 6, 1);
    ssSetInputPortSampleTime(S, 6, 0.01);
    ssSetInputPortOffsetTime(S, 6, 0.0);
    ssSetInputPortOverWritable(S, 6, 0);
    ssSetInputPortOptimOpts(S, 6, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 7 */
  {
    if (!ssSetInputPortVectorDimension(S, 7, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 7, SS_UINT8);
    }

    ssSetInputPortDirectFeedThrough(S, 7, 1);
    ssSetInputPortSampleTime(S, 7, 0.01);
    ssSetInputPortOffsetTime(S, 7, 0.0);
    ssSetInputPortOverWritable(S, 7, 0);
    ssSetInputPortOptimOpts(S, 7, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  /* inport number: 8 */
  {
    if (!ssSetInputPortVectorDimension(S, 8, 1))
      return;
    if (ssGetSimMode(S) != SS_SIMMODE_SIZES_CALL_ONLY) {
      ssSetInputPortDataType(S, 8, SS_BOOLEAN);
    }

    ssSetInputPortDirectFeedThrough(S, 8, 1);
    ssSetInputPortSampleTime(S, 8, 0.01);
    ssSetInputPortOffsetTime(S, 8, 0.0);
    ssSetInputPortOverWritable(S, 8, 0);
    ssSetInputPortOptimOpts(S, 8, SS_NOT_REUSABLE_AND_GLOBAL);
  }

  ssSetRTWGeneratedSFcn(S, 1);         /* Generated S-function */

  /* DWork */
  if (!ssSetNumDWork(S, 4)) {
    return;
  }

  /* '<S1>/Unit Delay': DSTATE */
  ssSetDWorkName(S, 0, "DWORK0");
  ssSetDWorkWidth(S, 0, 1);
  ssSetDWorkDataType(S, 0, SS_UINT8);
  ssSetDWorkUsedAsDState(S, 0, 1);

  /* '<S2>/ACC_Logic': DWORK1 */
  ssSetDWorkName(S, 1, "DWORK1");
  ssSetDWorkWidth(S, 1, 1);
  ssSetDWorkDataType(S, 1, SS_UINT8);

  /* '<S2>/ACC_Logic': DWORK2 */
  ssSetDWorkName(S, 2, "DWORK2");
  ssSetDWorkWidth(S, 2, 1);
  ssSetDWorkDataType(S, 2, SS_UINT8);

  /* '<S2>/ACC_Logic': DWORK3 */
  ssSetDWorkName(S, 3, "DWORK3");
  ssSetDWorkWidth(S, 3, 1);
  ssSetDWorkDataType(S, 3, SS_UINT8);

  /* Tunable Parameters */
  ssSetNumSFcnParams(S, 0);

  /* Number of expected parameters */
#if defined(MATLAB_MEX_FILE)

  if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {

#if defined(MDL_CHECK_PARAMETERS)

    mdlCheckParameters(S);

#endif                                 /* MDL_CHECK_PARAMETERS */

    if (ssGetErrorStatus(S) != (NULL) ) {
      return;
    }
  } else {
    return;                /* Parameter mismatch will be reported by Simulink */
  }

#endif                                 /* MATLAB_MEX_FILE */

  /* Options */
  ssSetOptions(S, (SS_OPTION_RUNTIME_EXCEPTION_FREE_CODE |
                   SS_OPTION_PORT_SAMPLE_TIMES_ASSIGNED ));

#if SS_SFCN_FOR_SIM

  {
    ssSupportsMultipleExecInstances(S, true);
    ssHasStateInsideForEachSS(S, false);
  }

#endif

}

/* Function to initialize sample times. */
static void mdlInitializeSampleTimes(SimStruct *S)
{
  /* task periods */
  ssSetSampleTime(S, 0, 0.01);

  /* task offsets */
  ssSetOffsetTime(S, 0, 0.0);
}

#if defined(MATLAB_MEX_FILE)
#include "fixedpoint.c"
#include "simulink.c"
#else
#undef S_FUNCTION_NAME
#define S_FUNCTION_NAME                Adaptive_Cruise_Control_sf
#include "cg_sfun.h"
#endif                                 /* defined(MATLAB_MEX_FILE) */

Conclusion:

The given final project for the Adaptive Cruise Control Logic is made whose all supporting documents are attached below.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.