Challenge

Project 2 Adaptive Cruise Control

AIM:  To develop an adaptive CruiseControl feauture as per the  model according to the given requirements in the Document Using Matlab/Simulink

MATLAB Version: MATLAB2018a

Steps

  • Linking of the Simulink Data Dictionary
  • Developement of the Desired model
  • Configuration Parameter Setting
  • Requirement Tagging
  • Signal resolving and Signal Propogation
  • Model Advisor Report Generation
  • C-code generation

Create the simulink Data Dictionary                                          

To Create a sldd file to update all the given inputs, outputs & parameters on this file later calling these variables on the Simulink blocks are shown in the fig. select the storage classes for inputs as imported extern,outputs storage class is Export to file,local variables storage class as Localizable

Developement of the Desired model

As per the requirement develop the  model here there is 3 requirements with the inputs, outputs

Requirement_1

Requirement_2

Requirement_3

Configuration Settings

Solver Type is changed from variable step to Fixed step and Solver type is selected as Discrete ( No continuous state) and also sample time is declared as 0.01 sec as per the project requirement.

Also under the Code generation option selecting embedded real time as system target file (ert.tlc) under target selection option

Requirement Tagging:-

 A MS Word file is created which contain all the requirements and necessary information to develop the model appropiately. This Word file is tagged for both the subsystem Requirement1 , Requirement2, Requirement3 respectively.

Model Advisor Report Generation

Click on the Model advisor  and click on ok after that report is generated than selected modelling standards for MAAB after that click on run selected checks  and save the report format in html format

Code generation Report

after execution of the model advisor report click on the build model than code is generated

Code generation

File: PROJECT2Adaptive.c

1

/*

2

* File: PROJECT2Adaptive.c

3

*

4

* Code generated for Simulink model 'PROJECT2Adaptive'.

5

*

6

* Model version : 1.0

7

* Simulink Coder version : 8.14 (R2018a) 06-Feb-2018

8

* C/C++ source code generated on : Thu Jul 27 23:21: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 "PROJECT2Adaptive.h"

17

#include "PROJECT2Adaptive_private.h"

18

 

19

/* Named constants for Chart: '/Chart' */

20

#define IN_LeadVehicle_Detected_Resume ((uint8_T)2U)

21

#define IN_LeadVehicle_Not_Detected_Res ((uint8_T)4U)

22

#define IN_LeadVehicle_Speed_equal_Set_ ((uint8_T)5U)

23

#define IN_LeadVehicle_Speed_lessthan_S ((uint8_T)6U)

24

#define PROJECT2Ada_IN_ACC_STANDBY_mode ((uint8_T)3U)

25

#define PROJECT2Adap_IN_NO_ACTIVE_CHILD ((uint8_T)0U)

26

#define PROJECT2Adaptiv_IN_ACC_OFF_mode ((uint8_T)1U)

27

#define PROJECT2Adaptive_IN_ACC_ON_MODE ((uint8_T)2U)

28

#define PROJECT_IN_LeadVehicle_Detected ((uint8_T)1U)

29

#define PRO_IN_LeadVehicle_NOT_Detected ((uint8_T)3U)

30

 

31

/* Exported data definition */

32

 

33

/* Definition for custom storage class: ExportToFile */

34

uint8_T Acceleration_Mode;

35

 

36

/* Definition for custom storage class: Localizable */

37

static uint8_T DriveVehicle_Speed;

38

static uint8_T LeadVehicle_Detected;

39

static uint8_T LeadVehicle_Speed;

40

 

41

/* Block states (default storage) */

42

DW_PROJECT2Adaptive_T PROJECT2Adaptive_DW;

43

 

44

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

45

ExtU_PROJECT2Adaptive_T PROJECT2Adaptive_U;

46

 

47

/* Real-time model */

48

RT_MODEL_PROJECT2Adaptive_T PROJECT2Adaptive_M_;

49

RT_MODEL_PROJECT2Adaptive_T *const PROJECT2Adaptive_M = &PROJECT2Adaptive_M_;

50

 

51

/* Forward declaration for local functions */

52

static void PROJECT2Adaptive_ACC_ON_MODE(uint8_T *Acceleration_Mode_l);

53

real_T rt_roundd_snf(real_T u)

54

{

55

real_T y;

56

if (fabs(u) < 4>{

57

if (u >= 0.5) {

58

y = floor(u + 0.5);

59

} else if (u > -0.5) {

60

y = u * 0.0;

61

} else {

62

y = ceil(u - 0.5);

63

}

64

} else {

65

y = u;

66

}

67

 

68

return y;

69

}

70

 

71

/* Function for Chart: '/Chart' */

72

static void PROJECT2Adaptive_ACC_ON_MODE(uint8_T *Acceleration_Mode_l)

73

{

74

int32_T tmp;

75

int32_T tmp_0;

76

uint8_T tmp_1;

77

uint8_T tmp_2;

78

 

79

/* Inport: '/SetSwitch' incorporates:

80

* Inport: '/CruiseSwitch'

81

* Inport: '/Set_Gap'

82

* Inport: '/Set_Speed'

83

* Inport: '/Time_Gap'

84

*/

85

if (!SetSwitch) {

86

PROJECT2Adaptive_DW.is_ACC_ON_MODE = PROJECT2Adap_IN_NO_ACTIVE_CHILD;

87

PROJECT2Adaptive_DW.is_c3_PROJECT2Adaptive = PROJECT2Ada_IN_ACC_STANDBY_mode;

88

*Acceleration_Mode_l = 1U;

89

} else if (!CruiseSwitch) {

90

PROJECT2Adaptive_DW.is_ACC_ON_MODE = PROJECT2Adap_IN_NO_ACTIVE_CHILD;

91

PROJECT2Adaptive_DW.is_c3_PROJECT2Adaptive = PROJECT2Adaptiv_IN_ACC_OFF_mode;

92

*Acceleration_Mode_l = 0U;

93

} else {

94

switch (PROJECT2Adaptive_DW.is_ACC_ON_MODE) {

95

case PROJECT_IN_LeadVehicle_Detected:

96

*Acceleration_Mode_l = 2U;

97

if (LeadVehicle_Detected == 0) {

98

PROJECT2Adaptive_DW.is_ACC_ON_MODE = PRO_IN_LeadVehicle_NOT_Detected;

99

*Acceleration_Mode_l = 1U;

100

} else {

101

if (((LeadVehicle_Detected == 1) && (LeadVehicle_Speed < Set>

102

(Time_Gap < Set>{

103

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Speed_lessthan_S;

104

*Acceleration_Mode_l = 4U;

105

}

106

}

107

break;

108

 

109

case IN_LeadVehicle_Detected_Resume:

110

*Acceleration_Mode_l = 3U;

111

 

112

/* Inport: '/Set_Speed' incorporates:

113

* Inport: '/Set_Gap'

114

* Inport: '/Time_Gap'

115

*/

116

if ((DriveVehicle_Speed == Set_Speed) && (LeadVehicle_Speed >= Set_Speed) &&

117

(Time_Gap >= Set_Gap)) {

118

PROJECT2Adaptive_DW.is_ACC_ON_MODE = PROJECT_IN_LeadVehicle_Detected;

119

*Acceleration_Mode_l = 2U;

120

} else if (LeadVehicle_Detected == 0) {

121

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Not_Detected_Res;

122

*Acceleration_Mode_l = 1U;

123

} else {

124

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Speed_equal_Set_;

125

*Acceleration_Mode_l = 5U;

126

}

127

break;

128

 

129

case PRO_IN_LeadVehicle_NOT_Detected:

130

*Acceleration_Mode_l = 1U;

131

 

132

/* Inport: '/Set_Speed' incorporates:

133

* Inport: '/Set_Gap'

134

* Inport: '/Time_Gap'

135

*/

136

if ((LeadVehicle_Detected == 1) && (DriveVehicle_Speed == Set_Speed) &&

137

(LeadVehicle_Speed >= Set_Speed) && (Time_Gap >= Set_Gap)) {

138

PROJECT2Adaptive_DW.is_ACC_ON_MODE = PROJECT_IN_LeadVehicle_Detected;

139

*Acceleration_Mode_l = 2U;

140

} else {

141

if (((LeadVehicle_Detected == 1) && (LeadVehicle_Speed < Set>

142

(Time_Gap < Set>{

143

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Speed_lessthan_S;

144

*Acceleration_Mode_l = 4U;

145

}

146

}

147

break;

148

 

149

case IN_LeadVehicle_Not_Detected_Res:

150

*Acceleration_Mode_l = 1U;

151

break;

152

 

153

case IN_LeadVehicle_Speed_equal_Set_:

154

*Acceleration_Mode_l = 5U;

155

 

156

/* Inport: '/Set_Speed' incorporates:

157

* Inport: '/Set_Gap'

158

* Inport: '/Time_Gap'

159

*/

160

if (((DriveVehicle_Speed < Set>

161

DriveVehicle_Speed)) || (Time_Gap >= Set_Gap)) {

162

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Detected_Resume;

163

*Acceleration_Mode_l = 3U;

164

} else if ((LeadVehicle_Detected == 0) || (DriveVehicle_Speed <= Set_Speed))

165

{

166

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Not_Detected_Res;

167

*Acceleration_Mode_l = 1U;

168

} else {

169

if (((LeadVehicle_Speed < Set>

170

DriveVehicle_Speed)) || ((uint8_T)(int32_T)rt_roundd_snf(0.75 *

171

(real_T)Set_Gap) == Time_Gap)) {

172

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Speed_lessthan_S;

173

*Acceleration_Mode_l = 4U;

174

}

175

}

176

break;

177

 

178

default:

179

*Acceleration_Mode_l = 4U;

180

 

181

/* Inport: '/Set_Speed' */

182

if ((LeadVehicle_Detected == 0) && (DriveVehicle_Speed == Set_Speed)) {

183

PROJECT2Adaptive_DW.is_ACC_ON_MODE = PRO_IN_LeadVehicle_NOT_Detected;

184

*Acceleration_Mode_l = 1U;

185

} else {

186

tmp = (int32_T)rt_roundd_snf((real_T)LeadVehicle_Speed * 1.25);

187

 

188

/* Inport: '/Set_Gap' */

189

tmp_0 = (int32_T)rt_roundd_snf(1.25 * (real_T)Set_Gap);

190

if (tmp < 256>{

191

tmp_1 = (uint8_T)tmp;

192

} else {

193

tmp_1 = MAX_uint8_T;

194

}

195

 

196

if (tmp_0 < 256>{

197

tmp_2 = (uint8_T)tmp_0;

198

} else {

199

tmp_2 = MAX_uint8_T;

200

}

201

 

202

/* Inport: '/Time_Gap' incorporates:

203

* Inport: '/Set_Gap'

204

*/

205

if ((tmp_1 >= DriveVehicle_Speed) && ((uint8_T)(int32_T)rt_roundd_snf

206

((real_T)LeadVehicle_Speed * 0.75) <= DriveVehicle_Speed) &&

207

(DriveVehicle_Speed < Set xss=removed>

208

>= (uint8_T)(int32_T)rt_roundd_snf(0.75 * (real_T)Set_Gap))) {

209

PROJECT2Adaptive_DW.is_ACC_ON_MODE = IN_LeadVehicle_Speed_equal_Set_;

210

*Acceleration_Mode_l = 5U;

211

}

212

}

213

break;

214

}

215

}

216

 

217

/* End of Inport: '/SetSwitch' */

218

}

219

 

220

/* Model step function */

221

void PROJECT2Adaptive_step(void)

222

{

223

/* Sum: '/Add' incorporates:

224

* Inport: '/CameraInput_LeadVehicle'

225

* Inport: '/RadarInput_LeadVehicle'

226

*/

227

LeadVehicle_Speed = (uint8_T)((uint32_T)

228

PROJECT2Adaptive_U.CameraInput_LeadVehicle +

229

PROJECT2Adaptive_U.RadarInput_LeadVehicle);

230

 

231

/* SignalConversion: '/Signal Conversion' incorporates:

232

* Inport: '/RadarInput_DriveVehicle'

233

*/

234

LeadVehicle_Detected = RadarInput_DriveVehicle;

235

 

236

/* UnitDelay: '/Unit Delay' */

237

Acceleration_Mode = PROJECT2Adaptive_DW.UnitDelay_DSTATE;

238

 

239

/* Sum: '/Sum' incorporates:

240

* Inport: '/CameraInput_DriveVehicle'

241

* Inport: '/RadarInput_DriveVehicle'

242

*/

243

DriveVehicle_Speed = (uint8_T)((uint32_T)(uint8_T)((uint32_T)

244

CameraInput_DriveVehicle + Acceleration_Mode) + RadarInput_DriveVehicle);

245

 

246

/* Chart: '/Chart' incorporates:

247

* Inport: '/CruiseSwitch'

248

* Inport: '/SetSwitch'

249

*/

250

if (PROJECT2Adaptive_DW.is_active_c3_PROJECT2Adaptive == 0U) {

251

PROJECT2Adaptive_DW.is_active_c3_PROJECT2Adaptive = 1U;

252

PROJECT2Adaptive_DW.is_c3_PROJECT2Adaptive = PROJECT2Adaptiv_IN_ACC_OFF_mode;

253

 

254

/* Update for UnitDelay: '/Unit Delay' */

255

PROJECT2Adaptive_DW.UnitDelay_DSTATE = 0U;

256

} else {

257

switch (PROJECT2Adaptive_DW.is_c3_PROJECT2Adaptive) {

258

case PROJECT2Adaptiv_IN_ACC_OFF_mode:

259

/* Update for UnitDelay: '/Unit Delay' */

260

PROJECT2Adaptive_DW.UnitDelay_DSTATE = 0U;

261

if (CruiseSwitch) {

262

PROJECT2Adaptive_DW.is_c3_PROJECT2Adaptive =

263

PROJECT2Ada_IN_ACC_STANDBY_mode;

264

 

265

/* Update for UnitDelay: '/Unit Delay' */

266

PROJECT2Adaptive_DW.UnitDelay_DSTATE = 1U;

267

}

268

break;

269

 

270

case PROJECT2Adaptive_IN_ACC_ON_MODE:

271

/* Update for UnitDelay: '/Unit Delay' */

272

PROJECT2Adaptive_ACC_ON_MODE(&PROJECT2Adaptive_DW.UnitDelay_DSTATE);

273

break;

274

 

275

default:

276

/* Update for UnitDelay: '/Unit Delay' */

277

PROJECT2Adaptive_DW.UnitDelay_DSTATE = 1U;

278

if (!CruiseSwitch) {

279

PROJECT2Adaptive_DW.is_c3_PROJECT2Adaptive =

280

PROJECT2Adaptiv_IN_ACC_OFF_mode;

281

 

282

/* Update for UnitDelay: '/Unit Delay' */

283

PROJECT2Adaptive_DW.UnitDelay_DSTATE = 0U;

284

} else {

285

if (SetSwitch) {

286

PROJECT2Adaptive_DW.is_c3_PROJECT2Adaptive =

287

PROJECT2Adaptive_IN_ACC_ON_MODE;

288

PROJECT2Adaptive_DW.is_ACC_ON_MODE = PROJECT_IN_LeadVehicle_Detected;

289

 

290

/* Update for UnitDelay: '/Unit Delay' */

291

PROJECT2Adaptive_DW.UnitDelay_DSTATE = 2U;

292

}

293

}

294

break;

295

}

296

}

297

 

298

/* End of Chart: '/Chart' */

299

}

300

 

301

/* Model initialize function */

302

void PROJECT2Adaptive_initialize(void)

303

{

304

/* (no initialization code required) */

305

}

306

 

307

/* Model terminate function */

308

void PROJECT2Adaptive_terminate(void)

309

{

310

/* (no terminate code required) */

311

}

312

 

313

/*

314

* File trailer for generated code.

315

*

316

* [EOF]

317

*/

318

 

Result

After implementing the ACC model run the simulation. If the model is running successfully without any errors then the Adaptive Cruise Control is works as per the requirement.

  • The model is running successfully without any error
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.