Challenge

Project 2 Adaptive Cruise Control

Aim:

To develop an Adaptive Cruise Control feature as per the Requirement Document using MATLAB Simulink.

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.

Now to Develop the Simulink model we go step-by-step as follows.

Step-1

Creating a SLDD File as per given input and output in the question.

step-2:

Change the configuration parameters solver settings as below.

STEP-3

Change the code generation parameter to ert.tlc

step-4

Requirement:-1

step-5:

Requirement-2

step-6

Requirement-3

here we develop a flowchart as per the question.

step-7

Now we convert this overall 3 subsystem to single subsystem. and treat this as atomic subsystem.

step-9

Do the requirement tagging for all 3 subsystems. only one requirement shown here. for all 3 subsystem, requirement tagging done.

step-10

Model advisory report generated and checked for MAAB Guidelines.

step-11

code generation done.

ert.main.c

* Event Support License -- for demonstration use and event support.  Not
 * for government, research, commercial, or other organizational use.
 *
 * File: ert_main.c
 *
 * Code generated for Simulink model 'ADAS'.
 *
 * Model version                  : 1.5
 * Simulink Coder version         : 9.7 (R2022a) 13-Nov-2021
 * C/C++ source code generated on : Wed Mar 29 17:25:47 2023
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include <stddef.h>
#include <stdio.h>            /* This example main program uses printf/fflush */
#include "ADAS.h"                      /* Model header file */

/*
 * Associating rt_OneStep with a real-time clock or interrupt service routine
 * is what makes the generated code "real-time".  The function rt_OneStep is
 * always associated with the base rate of the model.  Subrates are managed
 * by the base rate from inside the generated code.  Enabling/disabling
 * interrupts and floating point context switches are target specific.  This
 * example code indicates where these should take place relative to executing
 * the generated code step function.  Overrun behavior should be tailored to
 * your application needs.  This example simply sets an error status in the
 * real-time model and returns from rt_OneStep.
 */
void rt_OneStep(void);
void rt_OneStep(void)
{
  static boolean_T OverrunFlag = false;

  /* Disable interrupts here */

  /* Check for overrun */
  if (OverrunFlag) {
    rtmSetErrorStatus(ADAS_M, "Overrun");
    return;
  }

  OverrunFlag = true;

  /* Save FPU context here (if necessary) */
  /* Re-enable timer or interrupt here */
  /* Set model inputs here */

  /* Step the model */
  ADAS_step();

  /* Get model outputs here */

  /* Indicate task complete */
  OverrunFlag = false;

  /* Disable interrupts here */
  /* Restore FPU context here (if necessary) */
  /* Enable interrupts here */
}

/*
 * The example main function illustrates what is required by your
 * application code to initialize, execute, and terminate the generated code.
 * Attaching rt_OneStep to a real-time clock is target specific. This example
 * illustrates how you do this relative to initializing the model.
 */
int_T main(int_T argc, const char *argv[])
{
  /* Unused arguments */
  (void)(argc);
  (void)(argv);

  /* Initialize model */
  ADAS_initialize();

  /* Attach rt_OneStep to a timer or interrupt service routine with
   * period 0.01 seconds (base rate of the model) here.
   * The call syntax for rt_OneStep is
   *
   *  rt_OneStep();
   */
  printf("Warning: The simulation will run forever. "
         "Generated ERT main won't simulate model step behavior. "
         "To change this behavior select the 'MAT-file logging' option.\n");
  fflush((NULL));
  while (rtmGetErrorStatus(ADAS_M) == (NULL)) {
    /*  Perform application tasks here */
  }

  /* Terminate model */
  ADAS_terminate();
  return 0;
}

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

ADAS.C

* Event Support License -- for demonstration use and event support.  Not
 * for government, research, commercial, or other organizational use.
 *
 * File: ADAS.c
 *
 * Code generated for Simulink model 'ADAS'.
 *
 * Model version                  : 1.5
 * Simulink Coder version         : 9.7 (R2022a) 13-Nov-2021
 * C/C++ source code generated on : Wed Mar 29 17:25:47 2023
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "ADAS.h"
#include "rtwtypes.h"
#include "ADAS_private.h"

/* Named constants for Chart: '<S1>/Requirement_3' */
#define ADAS_IN_NO_ACTIVE_CHILD        ((uint8_T)0U)
#define ADAS_IN_OFF_Mode               ((uint8_T)1U)
#define ADAS_IN_ON_MODE                ((uint8_T)2U)
#define ADAS_IN_STANDBY_Mode           ((uint8_T)3U)
#define ADA_IN_LeadVehicle_Not_Detected ((uint8_T)3U)
#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)

/* Exported data definition */

/* Definition for custom storage class: ExportToFile */
uint8_T Acceleration_Mode;             /* '<S1>/Unit Delay' */
uint8_T DriveVehicle_Speed;            /* '<S3>/Add' */
uint8_T LeadVehicle_Detected;          /* '<S3>/Signal Conversion' */
uint8_T LeadVehicle_Speed;             /* '<S2>/Sum' */

/* Block states (default storage) */
DW_ADAS_T ADAS_DW;

/* External outputs (root outports fed by signals with default storage) */
ExtY_ADAS_T ADAS_Y;

/* Real-time model */
static RT_MODEL_ADAS_T ADAS_M_;
RT_MODEL_ADAS_T *const ADAS_M = &ADAS_M_;

/* Model step function */
void ADAS_step(void)
{
  /* Outputs for Atomic SubSystem: '<Root>/Subsystem' */
  /* Sum: '<S2>/Sum' incorporates:
   *  Inport: '<Root>/CameraInput_LeadVehicle '
   *  Inport: '<Root>/RadarInput_LeadVehicle'
   */
  LeadVehicle_Speed = (uint8_T)(CameraInput_LeadVehicle + RadarInput_LeadVehicle);

  /* UnitDelay: '<S1>/Unit Delay' */
  Acceleration_Mode = ADAS_Y.Acceleration_Mode_m;

  /* Sum: '<S3>/Add' incorporates:
   *  Inport: '<Root>/CameraInput_DriveVehicle'
   *  Inport: '<Root>/RadarInput_DriveVehicle'
   */
  DriveVehicle_Speed = (uint8_T)((uint8_T)(CameraInput_DriveVehicle +
    RadarInput_DriveVehicle) + Acceleration_Mode);

  /* SignalConversion: '<S3>/Signal Conversion' incorporates:
   *  Inport: '<Root>/RadarInput_DriveVehicle'
   */
  LeadVehicle_Detected = RadarInput_DriveVehicle;

  /* Chart: '<S1>/Requirement_3' incorporates:
   *  Inport: '<Root>/CruiseSwitch'
   *  Inport: '<Root>/SetSwitch'
   *  Inport: '<Root>/Set_Gap'
   *  Inport: '<Root>/Set_Speed'
   *  Inport: '<Root>/Time_Gap'
   *  UnitDelay: '<S1>/Unit Delay'
   */
  if (ADAS_DW.is_active_c3_ADAS == 0U) {
    ADAS_DW.is_active_c3_ADAS = 1U;
    ADAS_DW.is_c3_ADAS = ADAS_IN_OFF_Mode;
    ADAS_Y.Acceleration_Mode_m = 0U;
  } else {
    switch (ADAS_DW.is_c3_ADAS) {
     case ADAS_IN_OFF_Mode:
      ADAS_Y.Acceleration_Mode_m = 0U;
      if (CruiseSwitch) {
        ADAS_DW.is_c3_ADAS = ADAS_IN_STANDBY_Mode;
        ADAS_Y.Acceleration_Mode_m = 1U;
      }
      break;

     case ADAS_IN_ON_MODE:
      if (!SetSwitch) {
        ADAS_DW.is_ON_MODE = ADAS_IN_NO_ACTIVE_CHILD;
        ADAS_DW.is_c3_ADAS = ADAS_IN_STANDBY_Mode;
        ADAS_Y.Acceleration_Mode_m = 1U;
      } else if (!CruiseSwitch) {
        ADAS_DW.is_ON_MODE = ADAS_IN_NO_ACTIVE_CHILD;
        ADAS_DW.is_c3_ADAS = ADAS_IN_OFF_Mode;
        ADAS_Y.Acceleration_Mode_m = 0U;
      } else {
        switch (ADAS_DW.is_ON_MODE) {
         case IN_LeadVehicle_Detected_Follow:
          ADAS_Y.Acceleration_Mode_m = 2U;
          if (LeadVehicle_Detected == 0) {
            ADAS_DW.is_ON_MODE = ADA_IN_LeadVehicle_Not_Detected;
            ADAS_Y.Acceleration_Mode_m = 1U;
          } else if (((LeadVehicle_Detected == 1) && (LeadVehicle_Speed <
                       Set_Speed)) || (Time_Gap < Set_Gap)) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Speed_lessthan_S;
            ADAS_Y.Acceleration_Mode_m = 4U;
          }
          break;

         case IN_LeadVehicle_Detected_Resume:
          ADAS_Y.Acceleration_Mode_m = 3U;
          if ((DriveVehicle_Speed == Set_Speed) && (LeadVehicle_Speed >=
               Set_Speed) && (Time_Gap >= Set_Gap)) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Detected_Follow;
            ADAS_Y.Acceleration_Mode_m = 2U;
          } else if (LeadVehicle_Detected == 0) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Not_Detected_Res;
            ADAS_Y.Acceleration_Mode_m = 1U;
          } else if ((DriveVehicle_Speed < Set_Speed) && (LeadVehicle_Speed >
                      DriveVehicle_Speed) && (Time_Gap >= Set_Gap)) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Speed_equal_Set_;
            ADAS_Y.Acceleration_Mode_m = 5U;
          }
          break;

         case ADA_IN_LeadVehicle_Not_Detected:
          ADAS_Y.Acceleration_Mode_m = 1U;
          if ((LeadVehicle_Detected == 1) && (DriveVehicle_Speed == Set_Speed) &&
              (LeadVehicle_Speed >= Set_Speed) && (Time_Gap >= Set_Gap)) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Detected_Follow;
            ADAS_Y.Acceleration_Mode_m = 2U;
          } else if (((LeadVehicle_Detected == 1) && (LeadVehicle_Speed <
                       Set_Speed)) || (Time_Gap < Set_Gap)) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Speed_lessthan_S;
            ADAS_Y.Acceleration_Mode_m = 4U;
          }
          break;

         case IN_LeadVehicle_Not_Detected_Res:
          ADAS_Y.Acceleration_Mode_m = 1U;
          break;

         case IN_LeadVehicle_Speed_equal_Set_:
          ADAS_Y.Acceleration_Mode_m = 5U;
          if ((LeadVehicle_Detected == 0) || (DriveVehicle_Speed <= Set_Speed))
          {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Not_Detected_Res;
            ADAS_Y.Acceleration_Mode_m = 1U;
          } else if (((DriveVehicle_Speed < Set_Speed) && (LeadVehicle_Speed >
                       DriveVehicle_Speed)) || (Time_Gap >= Set_Gap)) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Detected_Resume;
            ADAS_Y.Acceleration_Mode_m = 3U;
          } else if (((LeadVehicle_Speed < Set_Speed) && (LeadVehicle_Speed <
                       DriveVehicle_Speed)) || (0.75 * (real_T)Set_Gap ==
                      Time_Gap)) {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Speed_lessthan_S;
            ADAS_Y.Acceleration_Mode_m = 4U;
          }
          break;

         default:
          /* case IN_LeadVehicle_Speed_lessthan_Set_Speed: */
          ADAS_Y.Acceleration_Mode_m = 4U;
          if ((LeadVehicle_Detected == 0) && (DriveVehicle_Speed == Set_Speed))
          {
            ADAS_DW.is_ON_MODE = ADA_IN_LeadVehicle_Not_Detected;
            ADAS_Y.Acceleration_Mode_m = 1U;
          } else if (((real_T)LeadVehicle_Speed * 1.25 >= DriveVehicle_Speed) &&
                     ((real_T)LeadVehicle_Speed * 0.75 <= DriveVehicle_Speed) &&
                     (DriveVehicle_Speed < Set_Speed) && ((Time_Gap <= 1.25 *
                       (real_T)Set_Gap) && (Time_Gap >= 0.75 * (real_T)Set_Gap)))
          {
            ADAS_DW.is_ON_MODE = IN_LeadVehicle_Speed_equal_Set_;
            ADAS_Y.Acceleration_Mode_m = 5U;
          }
          break;
        }
      }
      break;

     default:
      /* case IN_STANDBY_Mode: */
      ADAS_Y.Acceleration_Mode_m = 1U;
      if (!CruiseSwitch) {
        ADAS_DW.is_c3_ADAS = ADAS_IN_OFF_Mode;
        ADAS_Y.Acceleration_Mode_m = 0U;
      } else if (SetSwitch) {
        ADAS_DW.is_c3_ADAS = ADAS_IN_ON_MODE;
        ADAS_DW.is_ON_MODE = IN_LeadVehicle_Detected_Follow;
        ADAS_Y.Acceleration_Mode_m = 2U;
      }
      break;
    }
  }

  /* End of Chart: '<S1>/Requirement_3' */
  /* End of Outputs for SubSystem: '<Root>/Subsystem' */
}

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

/* Model terminate function */
void ADAS_terminate(void)
{
  /* (no terminate code required) */
}

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

ADAS.H

/*
 * Event Support License -- for demonstration use and event support.  Not
 * for government, research, commercial, or other organizational use.
 *
 * File: ADAS.h
 *
 * Code generated for Simulink model 'ADAS'.
 *
 * Model version                  : 1.5
 * Simulink Coder version         : 9.7 (R2022a) 13-Nov-2021
 * C/C++ source code generated on : Wed Mar 29 17:25:47 2023
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#ifndef RTW_HEADER_ADAS_h_
#define RTW_HEADER_ADAS_h_
#ifndef ADAS_COMMON_INCLUDES_
#define ADAS_COMMON_INCLUDES_
#include "rtwtypes.h"
#endif                                 /* ADAS_COMMON_INCLUDES_ */

#include "ADAS_types.h"

/* Macros for accessing real-time model data structure */
#ifndef rtmGetErrorStatus
#define rtmGetErrorStatus(rtm)         ((rtm)->errorStatus)
#endif

#ifndef rtmSetErrorStatus
#define rtmSetErrorStatus(rtm, val)    ((rtm)->errorStatus = (val))
#endif

/* Block states (default storage) for system '<Root>' */
typedef struct {
  uint8_T is_active_c3_ADAS;           /* '<S1>/Requirement_3' */
  uint8_T is_c3_ADAS;                  /* '<S1>/Requirement_3' */
  uint8_T is_ON_MODE;                  /* '<S1>/Requirement_3' */
} DW_ADAS_T;

/* External outputs (root outports fed by signals with default storage) */
typedef struct {
  uint8_T Acceleration_Mode_m;         /* '<Root>/Acceleration_Mode' */
} ExtY_ADAS_T;

/* Real-time Model Data Structure */
struct tag_RTM_ADAS_T {
  const char_T * volatile errorStatus;
};

/* Block states (default storage) */
extern DW_ADAS_T ADAS_DW;

/* External outputs (root outports fed by signals with default storage) */
extern ExtY_ADAS_T ADAS_Y;

/* Model entry point functions */
extern void ADAS_initialize(void);
extern void ADAS_step(void);
extern void ADAS_terminate(void);

/* Exported data declaration */

/* Declaration for custom storage class: ExportToFile */
extern uint8_T Acceleration_Mode;      /* '<S1>/Unit Delay' */
extern uint8_T DriveVehicle_Speed;     /* '<S3>/Add' */
extern uint8_T LeadVehicle_Detected;   /* '<S3>/Signal Conversion' */
extern uint8_T LeadVehicle_Speed;      /* '<S2>/Sum' */

/* Real-time Model object */
extern RT_MODEL_ADAS_T *const ADAS_M;

/*-
 * The generated code includes comments that allow you to trace directly
 * back to the appropriate location in the model.  The basic format
 * is <system>/block_name, where system is the system number (uniquely
 * assigned by Simulink) and block_name is the name of the block.
 *
 * Use the MATLAB hilite_system command to trace the generated code back
 * to the model.  For example,
 *
 * hilite_system('<S3>')    - opens system 3
 * hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
 *
 * Here is the system hierarchy for this model
 *
 * '<Root>' : 'ADAS'
 * '<S1>'   : 'ADAS/Subsystem'
 * '<S2>'   : 'ADAS/Subsystem/Requirement_1'
 * '<S3>'   : 'ADAS/Subsystem/Requirement_2'
 * '<S4>'   : 'ADAS/Subsystem/Requirement_3'
 */
#endif                                 /* RTW_HEADER_ADAS_h_ */

/*
 * File trailer for generated code.
 *
 * [EOF]
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.