Repository navigation
Expand file tree
/
Copy pathTraffic.c
More file actions
412 lines (358 loc) · 11.3 KB
/
Copy pathTraffic.c
File metadata and controls
412 lines (358 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**********************************************************
*Copyright(c) Automation Excellence Class
*FileName: Traffic.c
*Author: YJH
*Date: 2017/08/31
*Description: 交通相关各种初始化以及基本操作
*Others: 无
*Function List:
*History:
1.Date:
Author:
Modification:
2.
**********************************************************/
#include "Traffic.h"
#include "Head.h"
#define Node_Num 15 //只有15个结点,16为了数组定义
#define Road_NumMax 4 //定义最大路口数目
#define RoadType_Num 3 //定义车道种类数目
#define Road_Width 24 //定义车道宽 一个车道宽+一条白线宽
#define AtoR 0.01745329 // 角度转弧度乘数因子
// 地图的邻接矩阵
const int Map[Node_Num][Node_Num] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E
{0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0}, //0
{0,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0}, //1
{0,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0}, //2
{1,0,0, 0,0,1,0, 0,0,0,0, 0,0,0,0}, //3
{0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0}, //4
{0,0,0, 1,1,0,1, 1,0,0,0, 0,0,0,0}, //5
{0,0,0, 0,0,1,0, 0,0,0,0, 1,0,0,0}, //6
{0,0,0, 0,0,1,0, 0,1,0,0, 0,0,0,0}, //7
{0,0,1, 0,0,0,0, 1,0,1,0, 0,1,0,0}, //8
{0,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0}, //9
{0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0}, //10
{0,0,0, 0,0,0,1, 0,0,0,1, 0,1,0,1}, //11
{0,0,0, 0,0,0,0, 0,1,0,0, 1,0,1,0}, //12
{0,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0}, //13
{0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0}, //14
};
// 路口种类
const int TypeRoad[Node_Num] =
// 0 1 2 3 4 5 6 7 8 9 A B C D E
{3,3,3, 2,2,0,2, 2,0,3,3, 0,1,3,3};
/*************************************************
*Function: void TrafficInit(void);
*Description: 整个交通系统的初始化
*Calls: // 被本函数调用的函数清单
*Input: mode : 无;
*Output: 错误信息
*Return:
*Others: 初始化道路角度,红路灯,四个十字路口
*************************************************/
void TrafficInit(_Crossroad Intersection[])
{
int i;
for (i = 0; i <= Node_Num; i++)
{
//初始化种类和数目
Intersection[i].type = TypeRoad[i];
Intersection[i].num = Road_NumMax - Intersection[i].type;
//初始化角度
switch(TypeRoad[i])
{
/*十字路口初始化*/
case 0:
if (i == 5)
{
Intersection[i].arc = 45;
} else
{
Intersection[i].arc = 0;
}
break;
case 1:
case 3:
Intersection[i].arc = 0;
break;
/*45°弯道初始化*/
case 2:
if (i == 4) Intersection[i].arc = 45;
if (i == 7) Intersection[i].arc = 135;
if (i == 6) Intersection[i].arc = 225;
if (i == 3) Intersection[i].arc = 315;
break;
}
//初始化红绿灯
//初始化道路
RoadInit(& Intersection[i],i);
}
}
/*************************************************
*Function: void RoadInit(void);
*Description: 整个道路系统的初始化
*Calls: // 被本函数调用的函数清单
*Input: mode : 无;
*Output: 错误信息
*Return:
*Others: 初始化的为四个十字路口信息
*************************************************/
void RoadInit(_Crossroad * Inter,const int InterNum)
{
int i; // 车道循环变量
int j; // 左转右转直行车道循环变量
_CAR * Tail;
//每个路口节点的第二车道坐标信息
const int Road_Location[15][4][2] = {
{{191,0}, {0,0}, {0,0}, {0,0} },
{{458,0}, {0,0}, {0,0}, {0,0} },
{{789,0}, {0,0}, {0,0}, {0,0} },
{{115,150}, {180,196}, {0,0}, {0,0} },
{{382,150}, {425,229}, {0,0}, {0,0} },
{{313,238}, {363,340}, {258,389}, {207,288} },
{{191,474}, {148,396}, {0,0}, {0,0} },
{{430,390}, {351,431}, {0,0}, {0,0} },
{{713,354}, {824,389}, {789,500}, {677,465} },
{{1024,465},{0,0}, {0,0}, {0,0} },
{{0,605}, {0,0}, {0,0}, {0,0} },
{{115,570}, {225,605}, {190,716}, {80,681} },
{{712,570}, {824,605}, {677,681}, {0,0} },
{{1024,681},{0,0}, {0,0}, {0,0} },
{{115,768}, {0,0}, {0,0}, {0,0} }
};
/*int Link[Node_Num][Node_Num] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E
{0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0}, //0
{0,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0}, //1
{0,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0}, //2
{1,0,0, 0,0,1,0, 0,0,0,0, 0,0,0,0}, //3
{0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0}, //4
{0,0,0, 1,1,0,1, 1,0,0,0, 0,0,0,0}, //5
{0,0,0, 0,0,1,0, 0,0,0,0, 1,0,0,0}, //6
{0,0,0, 0,0,1,0, 0,1,0,0, 0,0,0,0}, //7
{0,0,1, 0,0,0,0, 1,0,1,0, 0,1,0,0}, //8
{0,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0}, //9
{0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0}, //10
{0,0,0, 0,0,0,1, 0,0,0,1, 0,1,0,1}, //11
{0,0,0, 0,0,0,0, 0,1,0,0, 1,0,1,0}, //12
{0,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0}, //13
{0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0}, //14
};*/
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[i][j].end = InterNum;
}
switch(Inter->type) {
// 十字路口
case 0:
// 初始化道路角度和中点坐标
if (!Inter->arc) //非特殊角度十字路口
{
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
// 初始化道路角度
Inter->Road[i][j].arc = (i * 90 + 180) % 360;
// 初始化停车线中点坐标
if (i & 1) // 道路标号为奇数,即道路为东西方向
{
Inter->Road[i][j].x = Road_Location[InterNum][i][0];
Inter->Road[i][j].y = Road_Location[InterNum][i][1] + ((j - 1) * Road_Width) * (sgn(sin(AtoR * Inter->Road[i][j].arc)));
} else // 道路标号为偶数,即道路为南北方向
{
Inter->Road[i][j].x = Road_Location[InterNum][i][0] + ((j - 1) * Road_Width) * (sgn(cos(AtoR * Inter->Road[i][j].arc)));
Inter->Road[i][j].y = Road_Location[InterNum][i][1];
}
// 初始化停车线可见状态
Inter->Road[i][j].sight = 1;
// 构建并链接首尾节点 并初始化标识符
if (Tail = (_CAR *) malloc(sizeof(_CAR)) == NULL)
{
delay(5000);
//return 0;
}
Inter->Road[i][j].drive_flag = Tail->drive_flag = -2;
Inter->Road[i][j].prev = NULL;
Inter->Road[i][j].next = Tail;
Tail->prev = &Inter->Road[i][j];
Tail->next = NULL;
}
} else // 特殊角度十字路口
{
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
// 初始化道路角度
Inter->Road[i][j].arc = (i * 90 + 225) % 360;
// 初始化停车线中点坐标
Inter->Road[i][j].x = Road_Location[InterNum][i][0] + ((j - 1) * Road_Width) * (sgn(cos(AtoR * Inter->Road[i][j].arc)));
Inter->Road[i][j].y = Road_Location[InterNum][i][1] + ((j - 1) * Road_Width) * (sgn(sin(AtoR * Inter->Road[i][j].arc)));
// 初始化停车线可见状态
Inter->Road[i][j].sight = 1;
// 构建并链接首尾节点 并初始化标识符
if (Tail = (_CAR *) malloc(sizeof(_CAR)) == NULL)
{
delay(5000);
//return 0;
}
Inter->Road[i][j].drive_flag = Tail->drive_flag = -2;
Inter->Road[i][j].prev = NULL;
Inter->Road[i][j].next = Tail;
Tail->prev = &Inter->Road[i][j];
Tail->next = NULL;
}
}
break;
// 丁字路口
case 1:
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
// 初始化道路角度
Inter->Road[i][j].arc = ((i == 0) ? 180 : (i == 1) ? 270 : 90);
// 初始化停车线中点坐标
if (i != 0) // 1,2号道路为东西方向
{
Inter->Road[i][j].x = Road_Location[InterNum][i][0];
Inter->Road[i][j].y = Road_Location[InterNum][i][1] + ((j - 1) * Road_Width) * (sgn(sin(AtoR * Inter->Road[i][j].arc)));
} else // 0号道路为南北方向
{
Inter->Road[i][j].x = Road_Location[InterNum][i][0] + ((j - 1) * Road_Width) * (sgn(cos(AtoR * Inter->Road[i][j].arc)));
Inter->Road[i][j].y = Road_Location[InterNum][i][1];
}
// 初始化停车线可见状态
Inter->Road[i][j].sight = 1;
// 构建并链接首尾节点 并初始化标识符
if (Tail = (_CAR *) malloc(sizeof(_CAR)) == NULL)
{
delay(5000);
//return 0;
}
Inter->Road[i][j].drive_flag = Tail->drive_flag = -2;
Inter->Road[i][j].prev = NULL;
Inter->Road[i][j].next = Tail;
Tail->prev = &Inter->Road[i][j];
Tail->next = NULL;
}
break;
// 45°弯道
case 2:
// 初始化道路角度
switch(InterNum)
{
case 3:
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[0][j].arc = 180;
Inter->Road[1][j].arc = 315;
}
break;
case 4:
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[0][j].arc = 180;
Inter->Road[1][j].arc = 45;
}
break;
case 6:
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[0][j].arc = 0;
Inter->Road[1][j].arc = 225;
}
break;
case 7:
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[0][j].arc = 270;
Inter->Road[1][j].arc = 135;
}
break;
}
// 初始化停车线中点坐标以及停车线可见状态
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[i][j].x = Road_Location[InterNum][i][0] + ((j - 1) * Road_Width) * (sgn(cos(AtoR * Inter->Road[i][j].arc)));
Inter->Road[i][j].y = Road_Location[InterNum][i][1] + ((j - 1) * Road_Width) * (sgn(sin(AtoR * Inter->Road[i][j].arc)));
// 初始化停车线可见状态
Inter->Road[i][j].sight = 0;
// 构建并链接首尾节点 并初始化标识符
if (Tail = (_CAR *) malloc(sizeof(_CAR)) == NULL)
{
delay(5000);
//return 0;
}
Inter->Road[i][j].drive_flag = Tail->drive_flag = -2;
Inter->Road[i][j].prev = NULL;
Inter->Road[i][j].next = Tail;
Tail->prev = &Inter->Road[i][j];
Tail->next = NULL;
}
break;
// 出地图路口
case 3:
// 初始化道路角度
switch(InterNum)
{
// 北方出地图车道
case 0:
case 1:
case 2:
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[i][j].arc = 0;
}
break;
// 东方出地图车道
case 9:
case 13:
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[i][j].arc = 90;
}
break;
// 南方出地图车道
case 14:
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[i][j].arc = 180;
}
break;
// 西方出地图车道
case 10:
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[i][j].arc = 270;
}
break;
}
// 初始化停车线中点坐标以及停车线可见状态
for (i = 0; i < Inter->num; i++)
for (j = 0; j < RoadType_Num; j++)
{
Inter->Road[i][j].x = Road_Location[InterNum][i][0] + ((j - 1) * Road_Width) * (sgn(cos(AtoR * Inter->Road[i][j].arc)));
Inter->Road[i][j].y = Road_Location[InterNum][i][1] + ((j - 1) * Road_Width) * (sgn(sin(AtoR * Inter->Road[i][j].arc)));
// 初始化停车线可见状态
Inter->Road[i][j].sight = 0;
// 构建并链接首尾节点 并初始化标识符
if (Tail = (_CAR *) malloc(sizeof(_CAR)) == NULL)
{
delay(5000);
//return 0;
}
Inter->Road[i][j].drive_flag = Tail->drive_flag = -2;
Inter->Road[i][j].prev = NULL;
Inter->Road[i][j].next = Tail;
Tail->prev = &Inter->Road[i][j];
Tail->next = NULL;
}
break;
}
}