Challenge

Project 2 Adaptive Cruise Control

1.Adoptive Cruise control Model

2.Derive vehicle

3.Lead Vehicle

4.Operation of ACC

5.Stateflow

6.Tagging and Traccing

7. Model Advisor Report

8.Simulink Data Dictionary

9 . Code Generation

.C File

File: PROJECT_MBD.c
1	/*
2	 * File: PROJECT_MBD.c
3	 *
4	 * Code generated for Simulink model 'PROJECT_MBD'.
5	 *
6	 * Model version                  : 1.47
7	 * Simulink Coder version         : 9.5 (R2021a) 14-Nov-2020
8	 * C/C++ source code generated on : Mon Jan 23 22:58:07 2023
9	 *
10	 * Target selection: ert.tlc
11	 * Embedded hardware selection: Intel->x86-64 (Windows64)
12	 * Code generation objectives: Unspecified
13	 * Validation result: Not run
14	 */
15	
16	#include "PROJECT_MBD.h"
17	#include "PROJECT_MBD_private.h"
18	
19	/* Named constants for Chart: '<S2>/Chart' */
20	#define IN_LeadVehicle_Detected_Follow ((uint8_T)1U)
21	#define IN_LeadVehicle_Detected_Resume ((uint8_T)2U)
22	#define IN_LeadVehicle_Not_Detected_Res ((uint8_T)4U)
23	#define IN_LeadVehicle_Speed_equal_Set_ ((uint8_T)5U)
24	#define IN_LeadVehicle_Speed_lessthan_S ((uint8_T)6U)
25	#define PROJECT_MBD_IN_ACC_OFF         ((uint8_T)1U)
26	#define PROJECT_MBD_IN_ACC_ON          ((uint8_T)2U)
27	#define PROJECT_MBD_IN_ACC_STANDBY     ((uint8_T)3U)
28	#define PROJECT_MBD_IN_NO_ACTIVE_CHILD ((uint8_T)0U)
29	#define PRO_IN_LeadVehicle_Not_Detected ((uint8_T)3U)
30	
31	/* Block states (default storage) */
32	DW_PROJECT_MBD_T PROJECT_MBD_DW;
33	
34	/* External outputs (root outports fed by signals with default storage) */
35	ExtY_PROJECT_MBD_T PROJECT_MBD_Y;
36	real_T rt_roundd(real_T u)
37	{
38	  real_T y;
39	  if (fabs(u) < 4.503599627370496E+15) {
40	    if (u >= 0.5) {
41	      y = floor(u + 0.5);
42	    } else if (u > -0.5) {
43	      y = 0.0;
44	    } else {
45	      y = ceil(u - 0.5);
46	    }
47	  } else {
48	    y = u;
49	  }
50	
51	  return y;
52	}
53	
54	/* Model step function */
55	void PROJECT_MBD_step(void)
56	{
57	  /* local block i/o variables */
58	  uint8_T LeadVehicle_Speed;
59	  uint8_T DriveVehicle_Speed;
60	  uint8_T LeadVehicle_Detected;
61	  int32_T tmp;
62	  int32_T tmp_0;
63	  uint8_T tmp_1;
64	  uint8_T tmp_2;
65	
66	  /* Outputs for Atomic SubSystem: '<Root>/Subsystem' */
67	  /* Outputs for Atomic SubSystem: '<S1>/Drive Vehicle'
68	   *
69	   * Block description for '<S1>/Drive Vehicle':
70	   *  Drive Vehicle is the vehicle driven by the user & this is the vehicle
71	   *  which has ACC algorithm in it.
72	   */
73	  /* Sum: '<S3>/Add' incorporates:
74	   *  Inport: '<Root>/ CameraInput_DriveVehicle'
75	   *  Inport: '<Root>/RadarInput_DriveVehicle'
76	   *  UnitDelay: '<S1>/ Unit Delay'
77	   */
78	  DriveVehicle_Speed = (uint8_T)(((uint32_T)((uint8_T)(((uint32_T)
79	    CameraInput_DriveVehicle) + RadarInput_DriveVehicle))) +
80	    PROJECT_MBD_Y.Acceleration_Mode);
81	
82	  /* SignalConversion: '<S3>/Signal Conversion' incorporates:
83	   *  Inport: '<Root>/RadarInput_DriveVehicle'
84	   */
85	  LeadVehicle_Detected = RadarInput_DriveVehicle;
86	
87	  /* End of Outputs for SubSystem: '<S1>/Drive Vehicle' */
88	
89	  /* Outputs for Atomic SubSystem: '<S1>/Lead Vehicle'
90	   *
91	   * Block description for '<S1>/Lead Vehicle':
92	   *  Lead Vehicle is a vehicle which is driving in the road ahead of our
93	   *  drive vehicle. Two input signals (Signal Name: CameraInput_LeadVehicle
94	   *  & RadarInput_LeadVehicle).
95	   */
96	  /* Sum: '<S4>/Add' incorporates:
97	   *  Inport: '<Root>/ CameraInput_LeadVehicle'
98	   *  Inport: '<Root>/RadarInput_LeadVehicle'
99	   *
100	   * Block description for '<S4>/Add':
101	   *  hSDZnhjfdsb
102	   */
103	  LeadVehicle_Speed = (uint8_T)(((uint32_T)CameraInput_LeadVehicle) +
104	    RadarInput_LeadVehicle);
105	
106	  /* End of Outputs for SubSystem: '<S1>/Lead Vehicle' */
107	
108	  /* Outputs for Atomic SubSystem: '<S1>/ACC_Subsystem'
109	   *
110	   * Block description for '<S1>/ACC_Subsystem':
111	   *  Adaptive Cruise Control feature has 3 major modes of operation: OFF
112	   *  Mode, STANDBY Mode & ON Mode. This particular requirement has to be
113	   *  implemented as state machine logic in Simulink.
114	   */
115	  /* Chart: '<S2>/Chart' incorporates:
116	   *  Inport: '<Root>/CruiseSwitch'
117	   *  Inport: '<Root>/SetSwitch'
118	   *  Inport: '<Root>/Set_Gap'
119	   *  Inport: '<Root>/Set_Speed'
120	   *  Inport: '<Root>/Time_Gap'
121	   *  UnitDelay: '<S1>/ Unit Delay'
122	   */
123	  if (PROJECT_MBD_DW.is_active_c3_PROJECT_MBD == 0U) {
124	    PROJECT_MBD_DW.is_active_c3_PROJECT_MBD = 1U;
125	    PROJECT_MBD_DW.is_c3_PROJECT_MBD = PROJECT_MBD_IN_ACC_OFF;
126	    PROJECT_MBD_Y.Acceleration_Mode = 0U;
127	  } else {
128	    switch (PROJECT_MBD_DW.is_c3_PROJECT_MBD) {
129	     case PROJECT_MBD_IN_ACC_OFF:
130	      if (CruiseSwitch) {
131	        PROJECT_MBD_DW.is_c3_PROJECT_MBD = PROJECT_MBD_IN_ACC_STANDBY;
132	        PROJECT_MBD_Y.Acceleration_Mode = 1U;
133	      } else {
134	        PROJECT_MBD_Y.Acceleration_Mode = 0U;
135	      }
136	      break;
137	
138	     case PROJECT_MBD_IN_ACC_ON:
139	      if (!SetSwitch) {
140	        PROJECT_MBD_DW.is_ACC_ON = PROJECT_MBD_IN_NO_ACTIVE_CHILD;
141	        PROJECT_MBD_DW.is_c3_PROJECT_MBD = PROJECT_MBD_IN_ACC_STANDBY;
142	        PROJECT_MBD_Y.Acceleration_Mode = 1U;
143	      } else {
144	        switch (PROJECT_MBD_DW.is_ACC_ON) {
145	         case IN_LeadVehicle_Detected_Follow:
146	          if (LeadVehicle_Detected == 0) {
147	            PROJECT_MBD_DW.is_ACC_ON = PRO_IN_LeadVehicle_Not_Detected;
148	            PROJECT_MBD_Y.Acceleration_Mode = 1U;
149	          } else if (((LeadVehicle_Detected == 1) && (LeadVehicle_Speed <
150	                       Set_Speed)) || (Time_Gap < Set_Gap)) {
151	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Speed_lessthan_S;
152	            PROJECT_MBD_Y.Acceleration_Mode = 4U;
153	          } else {
154	            PROJECT_MBD_Y.Acceleration_Mode = 2U;
155	          }
156	          break;
157	
158	         case IN_LeadVehicle_Detected_Resume:
159	          if (((DriveVehicle_Speed == Set_Speed) && (LeadVehicle_Speed >=
160	                Set_Speed)) && (Time_Gap >= Set_Gap)) {
161	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Detected_Follow;
162	            PROJECT_MBD_Y.Acceleration_Mode = 2U;
163	          } else if (LeadVehicle_Detected == 0) {
164	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Not_Detected_Res;
165	            PROJECT_MBD_Y.Acceleration_Mode = 1U;
166	          } else if (((DriveVehicle_Speed < Set_Speed) && (LeadVehicle_Speed >
167	                       DriveVehicle_Speed)) && (Time_Gap >= Set_Gap)) {
168	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Speed_equal_Set_;
169	            PROJECT_MBD_Y.Acceleration_Mode = 5U;
170	          } else {
171	            PROJECT_MBD_Y.Acceleration_Mode = 3U;
172	          }
173	          break;
174	
175	         case PRO_IN_LeadVehicle_Not_Detected:
176	          if ((((LeadVehicle_Detected == 1) && (DriveVehicle_Speed == Set_Speed))
177	               && (LeadVehicle_Speed >= Set_Speed)) && (Time_Gap >= Set_Gap)) {
178	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Detected_Follow;
179	            PROJECT_MBD_Y.Acceleration_Mode = 2U;
180	          } else if (((LeadVehicle_Detected == 1) && (LeadVehicle_Speed <
181	                       Set_Speed)) || (Time_Gap < Set_Gap)) {
182	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Speed_lessthan_S;
183	            PROJECT_MBD_Y.Acceleration_Mode = 4U;
184	          } else {
185	            PROJECT_MBD_Y.Acceleration_Mode = 1U;
186	          }
187	          break;
188	
189	         case IN_LeadVehicle_Not_Detected_Res:
190	          PROJECT_MBD_Y.Acceleration_Mode = 1U;
191	          break;
192	
193	         case IN_LeadVehicle_Speed_equal_Set_:
194	          if ((LeadVehicle_Detected == 0) || (DriveVehicle_Speed <= Set_Speed))
195	          {
196	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Not_Detected_Res;
197	            PROJECT_MBD_Y.Acceleration_Mode = 1U;
198	          } else if (((DriveVehicle_Speed < Set_Speed) && (LeadVehicle_Speed >
199	                       DriveVehicle_Speed)) || (Time_Gap >= Set_Gap)) {
200	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Detected_Resume;
201	            PROJECT_MBD_Y.Acceleration_Mode = 3U;
202	          } else if (((LeadVehicle_Speed < Set_Speed) && (LeadVehicle_Speed <
203	                       DriveVehicle_Speed)) || (Time_Gap == ((int32_T)rt_roundd
204	                       (0.75 * ((real_T)Set_Gap))))) {
205	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Speed_lessthan_S;
206	            PROJECT_MBD_Y.Acceleration_Mode = 4U;
207	          } else {
208	            PROJECT_MBD_Y.Acceleration_Mode = 5U;
209	          }
210	          break;
211	
212	         default:
213	          /* case IN_LeadVehicle_Speed_lessthan_Set_Speed: */
214	          tmp = (int32_T)rt_roundd(((real_T)LeadVehicle_Speed) * 1.25);
215	          tmp_0 = (int32_T)rt_roundd(1.25 * ((real_T)Set_Gap));
216	          if (tmp < 256) {
217	            tmp_1 = (uint8_T)tmp;
218	          } else {
219	            tmp_1 = MAX_uint8_T;
220	          }
221	
222	          if (tmp_0 < 256) {
223	            tmp_2 = (uint8_T)tmp_0;
224	          } else {
225	            tmp_2 = MAX_uint8_T;
226	          }
227	
228	          if (((((tmp_1 >= DriveVehicle_Speed) && (((int32_T)rt_roundd(((real_T)
229	                     LeadVehicle_Speed) * 0.75)) <= DriveVehicle_Speed)) &&
230	                (DriveVehicle_Speed < Set_Speed)) && (Time_Gap <= tmp_2)) &&
231	              (Time_Gap >= ((int32_T)rt_roundd(0.75 * ((real_T)Set_Gap))))) {
232	            PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Speed_equal_Set_;
233	            PROJECT_MBD_Y.Acceleration_Mode = 5U;
234	          } else if ((LeadVehicle_Detected == 0) && (DriveVehicle_Speed ==
235	                      Set_Speed)) {
236	            PROJECT_MBD_DW.is_ACC_ON = PRO_IN_LeadVehicle_Not_Detected;
237	            PROJECT_MBD_Y.Acceleration_Mode = 1U;
238	          } else {
239	            PROJECT_MBD_Y.Acceleration_Mode = 4U;
240	          }
241	          break;
242	        }
243	      }
244	      break;
245	
246	     default:
247	      /* case IN_ACC_STANDBY: */
248	      if (!CruiseSwitch) {
249	        PROJECT_MBD_DW.is_c3_PROJECT_MBD = PROJECT_MBD_IN_ACC_OFF;
250	        PROJECT_MBD_Y.Acceleration_Mode = 0U;
251	      } else if (SetSwitch) {
252	        PROJECT_MBD_DW.is_c3_PROJECT_MBD = PROJECT_MBD_IN_ACC_ON;
253	        PROJECT_MBD_DW.is_ACC_ON = IN_LeadVehicle_Detected_Follow;
254	        PROJECT_MBD_Y.Acceleration_Mode = 2U;
255	      } else {
256	        PROJECT_MBD_Y.Acceleration_Mode = 1U;
257	      }
258	      break;
259	    }
260	  }
261	
262	  /* End of Chart: '<S2>/Chart' */
263	  /* End of Outputs for SubSystem: '<S1>/ACC_Subsystem' */
264	  /* End of Outputs for SubSystem: '<Root>/Subsystem' */
265	}
266	
267	/* Model initialize function */
268	void PROJECT_MBD_initialize(void)
269	{
270	  /* Registration code */
271	
272	  /* states (dwork) */
273	  (void) memset((void *)&PROJECT_MBD_DW, 0,
274	                sizeof(DW_PROJECT_MBD_T));
275	
276	  /* external outputs */
277	  PROJECT_MBD_Y.Acceleration_Mode = 0U;
278	
279	  /* SystemInitialize for Atomic SubSystem: '<Root>/Subsystem' */
280	  /* SystemInitialize for Atomic SubSystem: '<S1>/ACC_Subsystem'
281	   *
282	   * Block description for '<S1>/ACC_Subsystem':
283	   *  Adaptive Cruise Control feature has 3 major modes of operation: OFF
284	   *  Mode, STANDBY Mode & ON Mode. This particular requirement has to be
285	   *  implemented as state machine logic in Simulink.
286	   */
287	  /* SystemInitialize for Chart: '<S2>/Chart' */
288	  PROJECT_MBD_DW.is_ACC_ON = PROJECT_MBD_IN_NO_ACTIVE_CHILD;
289	  PROJECT_MBD_DW.is_active_c3_PROJECT_MBD = 0U;
290	  PROJECT_MBD_DW.is_c3_PROJECT_MBD = PROJECT_MBD_IN_NO_ACTIVE_CHILD;
291	
292	  /* End of SystemInitialize for SubSystem: '<S1>/ACC_Subsystem' */
293	  /* End of SystemInitialize for SubSystem: '<Root>/Subsystem' */
294	}
295	
296	/*
297	 * File trailer for generated code.
298	 *
299	 * [EOF]
300	 */
301	

. H File

File: PROJECT_MBD.h
1	/*
2	 * File: PROJECT_MBD.h
3	 *
4	 * Code generated for Simulink model 'PROJECT_MBD'.
5	 *
6	 * Model version                  : 1.47
7	 * Simulink Coder version         : 9.5 (R2021a) 14-Nov-2020
8	 * C/C++ source code generated on : Mon Jan 23 22:58:07 2023
9	 *
10	 * Target selection: ert.tlc
11	 * Embedded hardware selection: Intel->x86-64 (Windows64)
12	 * Code generation objectives: Unspecified
13	 * Validation result: Not run
14	 */
15	
16	#ifndef RTW_HEADER_PROJECT_MBD_h_
17	#define RTW_HEADER_PROJECT_MBD_h_
18	#include <math.h>
19	#include <string.h>
20	#ifndef PROJECT_MBD_COMMON_INCLUDES_
21	#define PROJECT_MBD_COMMON_INCLUDES_
22	#include "rtwtypes.h"
23	#endif                                 /* PROJECT_MBD_COMMON_INCLUDES_ */
24	
25	#include "PROJECT_MBD_types.h"
26	
27	/* Macros for accessing real-time model data structure */
28	
29	/* Block states (default storage) for system '<Root>' */
30	typedef struct {
31	  uint8_T is_active_c3_PROJECT_MBD;    /* '<S2>/Chart' */
32	  uint8_T is_c3_PROJECT_MBD;           /* '<S2>/Chart' */
33	  uint8_T is_ACC_ON;                   /* '<S2>/Chart' */
34	} DW_PROJECT_MBD_T;
35	
36	/* External outputs (root outports fed by signals with default storage) */
37	typedef struct {
38	  uint8_T Acceleration_Mode;           /* '<Root>/Acceleration_Mode' */
39	} ExtY_PROJECT_MBD_T;
40	
41	/* Block states (default storage) */
42	extern DW_PROJECT_MBD_T PROJECT_MBD_DW;
43	
44	/* External outputs (root outports fed by signals with default storage) */
45	extern ExtY_PROJECT_MBD_T PROJECT_MBD_Y;
46	
47	/* Model entry point functions */
48	extern void PROJECT_MBD_initialize(void);
49	extern void PROJECT_MBD_step(void);
50	
51	/*-
52	 * The generated code includes comments that allow you to trace directly
53	 * back to the appropriate location in the model.  The basic format
54	 * is <system>/block_name, where system is the system number (uniquely
55	 * assigned by Simulink) and block_name is the name of the block.
56	 *
57	 * Use the MATLAB hilite_system command to trace the generated code back
58	 * to the model.  For example,
59	 *
60	 * hilite_system('<S3>')    - opens system 3
61	 * hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
62	 *
63	 * Here is the system hierarchy for this model
64	 *
65	 * '<Root>' : 'PROJECT_MBD'
66	 * '<S1>'   : 'PROJECT_MBD/Subsystem'
67	 * '<S2>'   : 'PROJECT_MBD/Subsystem/ACC_Subsystem'
68	 * '<S3>'   : 'PROJECT_MBD/Subsystem/Drive Vehicle'
69	 * '<S4>'   : 'PROJECT_MBD/Subsystem/Lead Vehicle'
70	 * '<S5>'   : 'PROJECT_MBD/Subsystem/ACC_Subsystem/Chart'
71	 */
72	
73	/*-
74	 * Requirements for '<Root>': PROJECT_MBD
75	 */
76	#endif                                 /* RTW_HEADER_PROJECT_MBD_h_ */
77	
78	/*
79	 * File trailer for generated code.
80	 *
81	 * [EOF]
82	 */
83	
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.