This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gogatusai
439 lines (362 loc) · 13.6 KB
/
gogatusai
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//外回り東京>品川>池袋
//山手線制御用
//定義の順番は崩さないこと
//定数を設定すればコンパイルできることを確認済
#include<Servo.h>
const int SENSOR[6] = {0,1,2,3,4,5};//ここにセンサーピンを定義(配列)
const int SERVO[9] = {2,3,4,5,6,7,8,9,10};//ここにサーボピンを定義
//ここにサーボのインスタンスを定義
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;
Servo servo10;
const int stop_time1 = 6000;//shibu_shinaの停車時間
const int stop_time2 = 7000;//tokyo_shinjukuの停車時間
const int spare_time1 = 2400;//各停の発車に要する時間
const int spare_time2 = 1000;//快速の発車にかかる時間
const int short_time = 0;//列車がセンサーの上に来てから本当にセンサを読ませるまでの時間
const int sup1_mult = 0.85;//快速判定の倍率
const int sup2_mult = 0.7;//各停判定の倍率
unsigned long new_time;
struct Station{//駅の構造体
int Rstock;
int Lstock;
int value;//valueはloopの中で更新
int Rstatus;
int Lstatus;
unsigned long old_time1;
unsigned long old_time2;
unsigned long old_time3;
unsigned long old_time4;
unsigned long old_time5;
int sup1;
int sup2;
};
Station tokyo = {0};//構造体の初期化
Station shinjuku = {0};
Station ikebukuro = {0};
Station shibuya = {0};
Station shinagawa = {0};
Station tabata = {0};
void get(unsigned long &time){//時間を取得する関数
time = millis();
}
bool rapid(Station &station){//快速を判定する関数
if(station.value < station.sup1 &&station.value >= station.sup2){
return true;
}else{
return false;
}
}
bool local(Station &station){//各停を判定する関数
if(station.value < station.sup2){
return true;
}else{
return false;
}
}
bool NextisFree(Station &station){//前のstockが空いているか判定する関数
if(station.Rstock == 0 && station.Lstock == 0){
return true;
}else{
return false;
}
}
//池袋、渋谷、品川の関数1 ikebukuro
void shibu_shina1(Station &station, Station &backward_station, Servo servo){
Serial.print(station.Lstatus);//0:いない1:入線、2:停車、3:発車、4:発車完了
Serial.print(station.Rstatus);
Serial.print(station.Lstock);//0:stationの前の区間に車両がいる、1:stationの前の区間に車両がいない
Serial.print(station.Rstock);
Serial.print(station.value);
if(station.Lstatus == 0 && station.Rstatus == 0 && station.value < 650 /*station.sup1*/){
backward_station.Lstock = 0;
backward_station.Rstock = 0;
station.Lstatus = 1;
station.Rstatus = 1;
get(station.old_time1);
}
else if(station.Lstatus == 1 && station.Rstatus == 1 /*&& new_time - station.old_time1 > short_time*/){
if(station.value < 650 /*local(station)*/){//各停接近
servo.write(0);//停車
backward_station.Lstock = 0;//消してもいいかも
station.Lstatus = 2;
station.Rstatus = 0;
get(station.old_time2);
}
else if(station.value < 650 && station.value > 500 /*rapid(station)*/){//急行接近
backward_station.Rstock = 0;//消してもいいかも
if(NextisFree(station)){
servo.write(90);//通過
station.Rstock = 1;
station.Rstatus = 3;
station.Lstatus = 0;
get(station.old_time5);
}else{//停車
servo.write(0);
station.Rstatus = 2;
station.Lstatus = 0;
get(station.old_time3);
}
}
}
else if(station.Lstatus == 2 && NextisFree(station) && new_time - station.old_time2 > stop_time1){//各停発車
servo.write(90);
station.Lstock = 1;
station.Lstatus = 3;
get(station.old_time4);
}
else if(station.Lstatus == 3 && new_time - station.old_time4 > spare_time1){//各停発車完了
station.Lstatus = 0;
}
else if(station.Rstatus == 2 && NextisFree(station) && new_time - station.old_time3 > stop_time1){//急行発車
servo.write(90);
station.Rstock = 1;
station.Rstatus = 3;
get(station.old_time5);
}
else if(station.Rstatus == 3 && new_time - station.old_time5 > spare_time2){//快速発車完了
station.Rstatus = 0;
}
}
//池袋、渋谷、品川の関数2 sninagawa
void shibu_shina2(Station &station, Station &backward_station, Servo servo){
Serial.print(station.Lstatus);//0:いない1:入線、2:停車、3:発車、4:発車完了
Serial.print(station.Rstatus);
Serial.print(station.Lstock);//0:stationの前の区間に車両がいる、1:stationの前の区間に車両がいない
Serial.print(station.Rstock);
Serial.print(station.value);
if(station.Lstatus == 0 && station.Rstatus == 0 && station.value < 700 /*station.sup1*/){
backward_station.Lstock = 0;
backward_station.Rstock = 0;
station.Lstatus = 1;
station.Rstatus = 1;
get(station.old_time1);
}
else if(station.Lstatus == 1 && station.Rstatus == 1 /*&& new_time - station.old_time1 > short_time*/){
if(station.value < 700 /*local(station)*/){//各停接近
servo.write(0);//停車
backward_station.Lstock = 0;//消してもいいかも
station.Lstatus = 2;
station.Rstatus = 0;
get(station.old_time2);
}
else if(station.value < 700 && station.value > 500 /* rapid(station)*/){//急行接近
backward_station.Rstock = 0;//消してもいいかも
if(NextisFree(station)){
servo.write(90);//通過
station.Rstock = 1;
station.Rstatus = 3;
station.Lstatus = 0;
get(station.old_time5);
}else{//停車
servo.write(0);
station.Rstatus = 2;
station.Lstatus = 0;
get(station.old_time3);
}
}
}
else if(station.Lstatus == 2 && NextisFree(station) && new_time - station.old_time2 > stop_time1){//各停発車
servo.write(90);
station.Lstock = 1;
station.Lstatus = 3;
get(station.old_time4);
}
else if(station.Lstatus == 3 && new_time - station.old_time4 > spare_time1){//各停発車完了
station.Lstatus = 0;
}
else if(station.Rstatus == 2 && NextisFree(station) && new_time - station.old_time3 > stop_time1){//急行発車
servo.write(90);
station.Rstock = 1;
station.Rstatus = 3;
get(station.old_time5);
}
else if(station.Rstatus == 3 && new_time - station.old_time5 > spare_time2){//快速発車完了
station.Rstatus = 0;
}
}
//池袋、渋谷、品川の関数3 tokyo
void shibu_shina3(Station &station, Station &backward_station, Servo servo){
Serial.print(station.Lstatus);//0:いない1:入線、2:停車、3:発車、4:発車完了
Serial.print(station.Rstatus);
Serial.print(station.Lstock);//0:stationの前の区間に車両がいる、1:stationの前の区間に車両がいない
Serial.print(station.Rstock);
Serial.println(station.value);
if(station.Lstatus == 0 && station.Rstatus == 0 && station.value < 700 /*station.sup1*/){
backward_station.Lstock = 0;
backward_station.Rstock = 0;
station.Lstatus = 1;
station.Rstatus = 1;
get(station.old_time1);
}
else if(station.Lstatus == 1 && station.Rstatus == 1 /*&& new_time - station.old_time1 > short_time*/){
if(station.value < 700 /*local(station)*/){//各停接近
servo.write(0);//停車
backward_station.Lstock = 0;//消してもいいかも
station.Lstatus = 2;
station.Rstatus = 0;
get(station.old_time2);
}
}
else if(station.Lstatus == 2 && NextisFree(station) && new_time - station.old_time2 > stop_time1){//各停発車
servo.write(90);
station.Lstock = 1;
station.Lstatus = 3;
get(station.old_time4);
}
else if(station.Lstatus == 3 && new_time - station.old_time4 > spare_time1){//各停発車完了
station.Lstatus = 0;
}
else if(station.Rstatus == 2 && NextisFree(station) && new_time - station.old_time3 > stop_time1){//急行発車
servo.write(90);
station.Rstock = 1;
station.Rstatus = 3;
get(station.old_time5);
}
else if(station.Rstatus == 3 && new_time - station.old_time5 > spare_time2){//快速発車完了
station.Rstatus = 0;
}
}
//ここに東京と新宿の関数を定義
void tokyo_shinjuku(Station &station, Station &backward_station, Servo servo1, Servo servo2, Servo servo3){
Serial.print(station.Lstatus);
Serial.print(station.Rstatus);
Serial.print(station.Lstock);
Serial.print(station.Rstock);
Serial.println(station.value);
if(station.Lstatus == 0 && station.Rstatus == 0 && station.value < station.sup1){//各停も急行もいない
station.Lstatus = 1;
station.Rstatus = 1;
backward_station.Lstock = 0;
backward_station.Rstock = 0;
get(station.old_time1);
}
else if(station.Lstatus == 2 && station.Rstatus == 0 && station.value < station.sup1 && new_time - station.old_time2 >spare_time1){//各停停車中に快速が侵入
servo3.write(90);
servo2.write(0);//停車
station.Rstatus = 2;
backward_station.Rstock = 0;
get(station.old_time3);
}
/*else if(快速に各停追いつく)*/
else if(station.Lstatus == 1 && station.Rstatus == 1 && new_time - station.old_time1 > short_time){
if(/*station.value < 400*/local(station)){//各停接近
servo3.write(180);
servo1.write(0);
backward_station.Lstock = 0;//消してもいいかも
station.Lstatus = 2;
station.Rstatus = 0;
get(station.old_time2);
}
else if(/*station.value < 750 && station.value > 400 */rapid(station)){//急行接近
servo3.write(90);
servo2.write(0);
backward_station.Rstock = 0;//消してもいいかも
station.Rstatus = 2;
station.Lstatus = 0;
get(station.old_time3);
}
}
else if(station.Lstatus == 2 && NextisFree(station) && new_time - station.old_time2 > stop_time2 && station.Rstatus == 0 && backward_station.Rstock == 0){
servo1.write(90);
station.Lstock = 1;
station.Lstatus = 0;
}
else if(station.Rstatus == 2 && NextisFree(station) && new_time - station.old_time3 > stop_time2){
servo2.write(90);
station.Rstock = 1;
station.Rstatus = 0;
}
}
//田端信号所の関数
void tabata_sig(Station &station, Station &backward_station){
Serial.print(station.Lstatus);//0:いない1:入線、2:停車、3:発車、4:発車完了
Serial.print(station.Rstatus);
Serial.print(station.Lstock);//0:stationの前の区間に車両がいる、1:stationの前の区間に車両がいない
Serial.print(station.Rstock);
Serial.print(station.value);
if(station.Lstatus == 0 && station.Rstatus == 0 && station.value < station.sup1){
backward_station.Lstock = 0;
backward_station.Rstock = 0;
station.Lstatus = 1;
station.Rstatus = 1;
get(station.old_time1);
}
else if(station.Lstatus == 1 && station.Rstatus == 1 && new_time - station.old_time1 > short_time){
if(local(station)){
backward_station.Lstock = 0;
station.Lstock = 1;
station.Lstatus = 2;
station.Rstatus = 0;
get(station.old_time2);
}
else if(rapid(station)){
backward_station.Rstock = 0;
station.Rstock = 1;
station.Rstatus = 2;
station.Lstatus = 0;
get(station.old_time3);
}
}
else if(station.Lstatus == 2 && new_time - station.old_time2 > spare_time1){
station.Lstatus = 0;
}
else if(station.Rstatus == 2 && new_time - station.old_time3 > spare_time2){
station.Rstatus = 0;
}
}
void setup(){
Serial.begin(9600);
//ここにサーボの初期化を書く
servo2.attach(SERVO[0]);
servo3.attach(SERVO[1]);
servo4.attach(SERVO[2]);
servo5.attach(SERVO[3]);
servo6.attach(SERVO[4]);
servo7.attach(SERVO[5]);
servo8.attach(SERVO[6]);
servo9.attach(SERVO[7]);
servo10.attach(SERVO[8]);
servo2.write(90);
servo3.write(90);
servo4.write(90);
servo5.write(90);
servo6.write(90);
servo7.write(90);
servo8.write(90);
servo9.write(90);
servo10.write(90);
tokyo.sup1 = analogRead(SENSOR[0]) * 0.9;
tokyo.sup2 = analogRead(SENSOR[0]) * 0.7;
ikebukuro.sup1 = analogRead(SENSOR[1]) * sup1_mult;
ikebukuro.sup2 = analogRead(SENSOR[1]) * sup2_mult;
shinagawa.sup1 = analogRead(SENSOR[2]) * sup1_mult;
shinagawa.sup2 = analogRead(SENSOR[2]) * sup2_mult;
}
void loop(){
get(new_time);
tokyo.value = analogRead(SENSOR[2]);
ikebukuro.value = analogRead(SENSOR[1]);
shinagawa.value = analogRead(SENSOR[3]);
//ここにメインの処理を書く
shibu_shina3(tokyo, ikebukuro, servo2);
shibu_shina1(ikebukuro, shinagawa, servo5);
shibu_shina2(shinagawa, tokyo, servo6);
/*Serial.print(analogRead(SENSOR[0]));
Serial.print(analogRead(SENSOR[1]));
Serial.println(analogRead(SENSOR[2]));*/
/*servo6.write(90);
delay(1000);
servo6.write(0);
delay(1000);*/
//servo3.write(90);
//elay(1000);
//servo6.write(0);
//delay(1000);
}