-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogic.c
593 lines (577 loc) · 17.3 KB
/
logic.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
static unsigned char pizza_shop[10] = {0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99}; //Elements define what pizza at individual; stop
static unsigned char order_timeline_1[7] = {0x71,0xD8,0xA2,0x5B,0x7C,0x95,0xE5}; //Order Timeline for pizza type and house
static unsigned char order_track[7] = {0,0,0,0,0,0,0};
static unsigned char order_type[7] = {'r','r','p','p','r','p','p'};
static unsigned int order_time[7] = {40,100,115,200,250,350,350};
static unsigned char timeline_count = 0;
unsigned char estimate_house[12] ={12,17,23,25,25,23,17,12,13,18};
unsigned char estimate_shop[10] = {21,18,16,14,12,12,14,16,18,21};
void logic (void)
{
short int k;
short int i;
short int l=0;
short int n=0;
short int o=0;
short int j=0;
unsigned char houseconvert0;
unsigned char houseconvert1;
unsigned char houseconvert2;
unsigned char houseconvert3;
unsigned char pizzaconvert0;
unsigned char pizzaconvert1;
unsigned char pizzaconvert2;
unsigned char pizza1size;
unsigned char pizza2size;
unsigned char pizza1colour;
unsigned char pizza2colour;
unsigned int order_process[3] = {0, 0, 0};
unsigned int order_accepted[2] = {0, 0};
restart: for(k=0;k<=7;k++) //job scheduling. k=1 means corresponding order delivered. 0 indicates not delivered.
// k=2 means delivery later time (TIP time elapsed for example)
{
if(order_track[k]==0)
{
order_process[l]=k; //passing of order numbers to different array, which holds orders
l++; //that need to be in consideration for delivery management
if(l==3) //at a time three orders considered
break; //THESE NUMBERS ARE USED TO DETERMINE HOUSE and PIZZA
}
}
if(order_process[0]==0 && order_process[1]==0 && order_process[0]==0) //0 all three places indicates order_track array as either 1 or 2.
{ //initially, order track all 0
for(k=0;k<=7;k++)
{ //as robot keeps delivering, order_track changes value to 1 or 2
if(order_track[k]==2) // 1 indicate delivered and 2 means delivery later time
{ //if order track non zero, no new orders pending, we can deliver earlier orders
order_process[l]=k; //(denoted by 2) without affecting tip
l++;
if(l==3)
break;
}
}
}
houseconvert0=order_timeline_1[order_process[0]] & 0x0F;
houseconvert1=order_timeline_1[order_process[1]] & 0x0F; //decode house from timeline value
houseconvert2=order_timeline_1[order_process[2]] & 0x0F; //please refer to start of code for how order
houseconvert3=order_timeline_1[order_process[2]+1] & 0x0F; //stored in timeline array. Order process used to access various
pizzaconvert0=order_timeline_1[order_process[0]] & 0xF0; //timeline orders
pizzaconvert1=order_timeline_1[order_process[1]] & 0xF0;
pizzaconvert2=order_timeline_1[order_process[2]] & 0xF0;
//ORDER DECIDING PHASE. WHICH ORDERS RoBoT should accept based on type of pizza
if(order_type[order_process[0]]=='r' && order_type[order_process[1]]=='r')
{
if((houseconvert0<0x04 && houseconvert1<0x04) || (((houseconvert0>0x04)&&(houseconvert0<=0x08)) && ((houseconvert1>0x04)&&(houseconvert1<=0x08))) || abs(houseconvert0-houseconvert1)== (1||7))
//If order house lies on same side of flex or adjacent accross the sides. Eg H4 and H5
{
if(order_timeline_1[order_process[1]]-order_timeline_1[order_process[0]]<=10) //difference between two orders is 10s
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==pizzaconvert0)
break;
}
for(o=0;o<=9;o++)
{
if(pizza_shop[o]==pizzaconvert1)
break;
}
if(n!=10 && o!=10) //if difference is less than 10 and pizza locations known
{
attempt_two_delivery=1;
order_accepted[0]=order_process[0];
order_accepted[1]=order_process[1];
order_track[order_process[0]]=1;
order_track[order_process[1]]=1;
}
}
}
else //attempt single delivery if above conditions not specified. For TWO REGULAR
{
attempt_two_delivery=0;
order_accepted[0]=99; //99 indicating only one order
order_accepted[1]=order_process[0];
order_track[order_process[0]]=1;
}
}
else if(order_type[order_process[0]]=='r' && order_type[order_process[1]]=='r' && order_type[order_process[2]]=='p')
{
if((houseconvert0<0x04 && houseconvert1<0x04) || (((houseconvert0>0x04)&&(houseconvert0<=0x08)) && ((houseconvert1>0x04)&&(houseconvert1<=0x08))) || abs(houseconvert0-houseconvert1)== (1||7))
{
if(order_timeline_1[order_process[1]]-order_timeline_1[order_process[0]]<=10)
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==pizzaconvert0)
break;
}
for(o=0;o<=9;o++)
{
if(pizza_shop[o]==pizzaconvert1)
break;
}
if(n!=10 && o!=10)
{
attempt_two_delivery=1;
order_accepted[0]=order_process[0];
order_accepted[1]=order_process[1];
order_track[order_process[0]]=1;
order_track[order_process[1]]=1;
}
}
}
//Special case, If the third order taken into consideration is preorder and time window lies somehwere near delivery time
//of regular pizza.
else if((order_time[order_process[2]]<=order_time[order_process[0]]+55) && (houseconvert3!=houseconvert2))
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==pizzaconvert0)
break;
}
for(o=0;o<=9;o++)
{
if(pizza_shop[o]==pizzaconvert2)
break;
}
if(n!=10 && o!=10)
{
attempt_two_delivery=1;
order_accepted[0]=order_process[0];
order_accepted[1]=order_process[2];
order_track[order_process[0]]=1;
order_track[order_process[2]]=1;
}
}
else
{
attempt_two_delivery=0;
order_accepted[0]=99;
order_accepted[1]=order_process[0];
order_track[order_process[0]]=1;
}
}
else if(order_type[order_process[0]]=='r' && order_type[order_process[1]]=='p')
{
if((order_time[order_process[1]]<=order_time[order_process[0]]+60) && (houseconvert1!=houseconvert2))
//the second condition in loop checks for if same house orders two pizzas. if it does, then preorder pizza is dropped
//from consideration. better deliver the two pizzas in one go only rather than separate it.
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==pizzaconvert0)
break;
}
for(o=0;o<=9;o++)
{
if(pizza_shop[o]==pizzaconvert1)
break;
}
if(n!=10 && o!=10)
{
attempt_two_delivery=1;
order_accepted[0]=order_process[0];
order_accepted[1]=order_process[1];
order_track[order_process[0]]=1;
order_track[order_process[1]]=1;
}
}
else
{
attempt_two_delivery=0;
order_accepted[0]=99;
order_accepted[1]=order_process[0];
order_track[order_process[0]]=1;
}
}
else if(order_type[order_process[0]]=='p' && order_type[order_process[1]]=='r')
//advantage of preorder lies on double time window. Halfway past the time window, it acts as regular order only.
//hence similar condition to TWO REGULAR pizza orders.
{
if((houseconvert0<0x04 && houseconvert1<0x04) || (((houseconvert0>0x04)&&(houseconvert0<=0x08)) && ((houseconvert1>0x04)&&(houseconvert1<=0x08))) || abs(houseconvert0-houseconvert1)== (1||7))
{
if(order_timeline_1[order_process[1]]-order_timeline_1[order_process[0]]<=10)
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==pizzaconvert0)
break;
}
for(o=0;o<=9;o++)
{
if(pizza_shop[o]==pizzaconvert1)
break;
}
if(n!=10 && o!=10)
{
attempt_two_delivery=1;
order_accepted[0]=order_process[0];
order_accepted[1]=order_process[1];
order_track[order_process[0]]=1;
order_track[order_process[1]]=1;
}
}
}
else
{
attempt_two_delivery=0;
order_accepted[0]=99;
order_accepted[1]=order_process[0];
order_track[order_process[0]]=1;
}
}
//if both preorders same house, we attempt delivery for both
else if(order_type[order_process[0]]=='p' && order_type[order_process[1]]=='p')
{
if(houseconvert0==houseconvert1)
{
attempt_two_delivery=1;
order_accepted[0]=order_process[0];
order_accepted[1]=order_process[1];
order_track[order_process[0]]=1;
order_track[order_process[1]]=1;
}
//if different house, condition identical to ONE REGULAR AND PREORDER. Explained earlier
else if((order_time[order_process[1]]<=order_time[order_process[0]]+55) && (houseconvert1!=houseconvert2))
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==pizzaconvert0)
break;
}
for(o=0;o<=9;o++)
{
if(pizza_shop[o]==pizzaconvert1)
break;
}
if(n!=10 && o!=10)
{
attempt_two_delivery=1;
order_accepted[0]=order_process[0];
order_accepted[1]=order_process[1];
order_track[order_process[0]]=1;
order_track[order_process[1]]=1;
}
}
else
{
attempt_two_delivery=0;
order_accepted[0]=99;
order_accepted[1]=order_process[0];
order_track[order_process[0]]=1;
}
}
//delivery not able to make it on time CONDITION
if(time_ssd==order_time[order_process[0]]+20)
{
order_track[order_accepted[0]]=2;
goto restart;
}
//PIZZA IDENTIFICATION PHASE
if(attempt_two_delivery==0) //only one delivery
{ //try to scan till particular time after order placed. If block not found, abort order and deliver at end.
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==(order_timeline_1[order_accepted[1]]&0xF0))
break;
}
if(n==10) //pizza location not known
{
loop1: // SCAN shop while time is still enough to deliver pizza. If IF condition not satisfied, it implies robot didnt find pizza on time.
if(order_time[order_accepted[0]]+40>=time_ssd+estimate_house[(int)(order_timeline_1[order_accepted[0]] & 0x0F)])
{
for(i=0;i<=9;i++)
{
if(pizza_shop[i]==0x99)
break;
}
j=i;
for(;j<=9;j++)
{
if(pizza_shop[j]==0x99)
break;
}
move_shop('s',i);
sense(i);
if(pizza_shop[i]!=(order_timeline_1[order_accepted[1]]&0xF0)) //pizza not found
{
move_shop(i,j);
sense(j);
if(pizza_shop[j]!=(order_timeline_1[order_accepted[1]]&0xF0))
{
goto loop1; //loop if pizza not found.
}
}
}
else //if condition failed, time limit exceeded, abort order and mark it for later delivery (indicated by 2)
{
order_track[order_accepted[1]]=2;
goto restart;
}
}
//pizza found if it has not gone to loop1
//scan while estimated delivery time is 35 seconds + order time. In previous IF after loop1:, we considered 40s as
//if pizza found at 40, it can still deliver on time. However, scan pizza shop till 35s over order time after
//locating pizza as robot needs time to come back to pizza counter. 5s time considered.
while(order_time[order_accepted[0]]+35>=time_ssd+estimate_house[(int)(order_timeline_1[order_accepted[0]] & 0x0F)]) //scan shop till it is time to deliver pizza
{
for(i=0;i<=9;i++)
{
if(pizza_shop[i]==0x99)
break;
}
j=i;
for(;j<=9;j++) //identify pizza shop where pizza still not identified
{
if(pizza_shop[j]==0x99)
break;
}
if(j<=9)
{
move_shop(i,j);
sense(j);
}
}
//exit while loop means time to deliver order (only one pizza carry condition specified by attempt_two_delivery variable
}
if(attempt_two_delivery==1) //order accepted[0] indicates it needs to be delivered first. order accepted [1] indicates second delivery.
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==(order_timeline_1[order_accepted[0]]&0xF0))
break;
}
if(n==10) //pizza location not known
{
loop2: // SCAN shop while time is still enough to deliver pizza. If IF condition not satisfied, it implies robot didnt find pizza on time.
if(order_time[order_accepted[0]]+40>=time_ssd+estimate_house[(int)(order_timeline_1[order_accepted[0]] & 0x0F)])
{
for(i=0;i<=9;i++)
{
if(pizza_shop[i]==0x99)
break;
}
j=i;
for(;j<=9;j++)
{
if(pizza_shop[j]==0x99)
break;
}
move_shop('s',i);
sense(i);
if(pizza_shop[i]!=(order_timeline_1[order_accepted[0]]&0xF0)) //pizza not found
{
move_shop(i,j);
sense(j);
if(pizza_shop[j]!=(order_timeline_1[order_accepted[0]]&0xF0))
{
goto loop2; //loop if pizza not found.
}
}
}
else //if condition failed, time limit exceeded, abort order and mark it for later delivery.
{
order_track[order_accepted[0]]=2;
goto restart;
}
} //for attempt_two_delivery==1, condition same as for 0. First priority pizza needs to be found. Indicated
//by ORDER_ACCEPTED[0]. since that delivery window gets over earlier.
//We found first pizza. now similar sequence for second pizza.
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==(order_timeline_1[order_accepted[1]]&0xF0))
break;
}
if(n==10) //pizza location not known
{
loop3: // SCAN shop while time is still enough to deliver pizza. If IF condition not satisfied, it implies robot didnt find pizza on time.
if(order_time[order_accepted[1]]+40>=time_ssd+estimate_house[(int)(order_timeline_1[order_accepted[1]] & 0x0F)])
{
for(i=0;i<=9;i++)
{
if(pizza_shop[i]==0x99)
break;
}
j=i;
for(;j<=9;j++)
{
if(pizza_shop[j]==0x99)
break;
}
move_shop('s',i);
sense(i);
if(pizza_shop[i]!=(order_timeline_1[order_accepted[1]]&0xF0)) //pizza not found
{
move_shop(i,j);
sense(j);
if(pizza_shop[j]!=(order_timeline_1[order_accepted[1]]&0xF0))
{
goto loop3; //loop if pizza not found.
}
}
}
else //if condition failed, time limit exceeded, abort order and mark it for later delivery.
{
order_track[order_accepted[1]]=2;
attempt_two_delivery=0; //second priority pizza not found, abort it, single delivery only
order_accepted[1]=order_accepted[0];
order_accepted[0]=99; // for single delivery only order_accepted[1] used
}
}
}
//PIZZA PICKING PHASE
if(attempt_two_delivery==0)
{
for(n=0;n<=9;n++)
{
if(pizza_shop[n]==(order_timeline_1[order_accepted[1]]&0xF0))
break;
}
return_shop(j+1); //robot is at Jth location during search. J+1 because return_shop uses 1 to 10 as reference and not 0 to 9
move_shop('s',n+1);
pizza1size=(order_timeline_1[order_accepted[1]] & 0x30);
pizza1colour=(order_timeline_1[order_accepted[1]] & 0xC0);
if(pizza1size==0x01) //descriptions given at top of code
{
pick_block('s',1);
}
if(pizza1size==0x02)
{
pick_block('m',1);
}
if(pizza1size==0x03)
{
pick_block('l',1);
}
led_color(pizza1colour);
return_shop(n+1);
}
else
{
for(n=0;n<=9;n++) //pick priority 2 pizza
{
if(pizza_shop[n]==(order_timeline_1[order_accepted[1]]&0xF0))
break;
}
return_shop(j+1); //robot is at Jth location during search. J+1 because return_shop uses 1 to 10 as reference and not 0 to 9
move_shop('s',n+1);
pizza1size=(order_timeline_1[order_accepted[1]] & 0x30);
pizza1colour=(order_timeline_1[order_accepted[1]] & 0xC0);
if(pizza1size==0x01) //descriptions given at top of code
{
pick_block('s',1);
}
if(pizza1size==0x02)
{
pick_block('m',1);
}
if(pizza1size==0x03)
{
pick_block('l',1);
}
led_color(pizza1colour);
//pick priority1 pizza
for(o=0;o<=9;n++)
{
if(pizza_shop[o]==(order_timeline_1[order_accepted[0]&0xF0]))
break;
}
move_shop(n+1,o+1); //move_shop() can directly go from one node to next without going to pizza counter every time
pizza2size=(order_timeline_1[order_accepted[0]] & 0x30);
pizza2colour=(order_timeline_1[order_accepted[0]] & 0xC0);
if(pizza2size==0x01) //descriptions given at top of code
{
pick_block('s',0);
}
if(pizza2size==0x02)
{
pick_block('m',0);
}
if(pizza2size==0x03)
{
pick_block('l',0);
}
led_color(pizza2colour);
return_shop(o+1);
}
//PIZZA DEPOSITION PHASE
if(attempt_two_delivery==0)
{
move_house('s',(order_timeline_1[order_accepted[1]]&0x0F)); //masking size and colour
if(pizza1size==0x01) //descriptions given at top of code
{
drop_block('s',1);
}
if(pizza1size==0x02)
{
drop_block('m',1);
}
if(pizza1size==0x03)
{
drop_block('l',1);
}
ssd_freeze=1; //sound buzzer and freeze display
led_color(0);
for (n=0;n<=7;n++)
{
if(order_track[n]==0||2)
break;
}
if (n!=8) //deliveries still remaining
return_house(order_timeline_1[order_accepted[1]]&0x0F);
else
{
buzzer_on();
_delay_ms(5000);
while(1); //end of RUN
}
}
if(attempt_two_delivery==1)
{
//deliver first block
move_house('s',(order_timeline_1[order_accepted[0]]&0x0F)); //masking size and colour
if(pizza2size==0x01) //descriptions given at top of code
{
drop_block('s',1);
}
if(pizza2size==0x02)
{
drop_block('m',1);
}
if(pizza2size==0x03)
{
drop_block('l',1);
}
ssd_freeze=1; //sound buzzer and freeze display
led_color(pizza2colour);
move_house((order_timeline_1[order_accepted[0]]&0x0F),(order_timeline_1[order_accepted[1]]&0x0F));
if(pizza1size==0x01) //descriptions given at top of code
{
drop_block('s',1);
}
if(pizza1size==0x02)
{
drop_block('m',1);
}
if(pizza1size==0x03)
{
drop_block('l',1);
}
ssd_freeze=1;
led_color(0);
for (n=0;n<=7;n++)
{
if(order_track[n]==0||2)
break;
}
if (n!=8) //deliveries still remaining
return_house(order_timeline_1[order_accepted[1]]&0x0F);
else
{
buzzer_on();
_delay_ms(5000);
while(1); //end of RUN
}
goto restart; //process repeats all over again!
}
}