-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
2864 lines (2796 loc) · 60.9 KB
/
main.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Date: 10th January, 2016
//Author: Megh Shukla
//Interrupts : 0 (Color Sensor), 1 (node), 4 (left motor),5 (right motor) used
//Timers : 1 (Servo), 3 (Buzzer and ssd freeze), 5 (PWM), 4 (SSD)
//Robot straight line: 255,253
// timeline 1: 0 x a b c d e f g h
// ab: color cd: size efgh : house
// p = preorder, r = regular order
// pizza_shop: 0 x color size red = 1, green = 2, blue = 3
/*
TEAM ID : PD#176
AUTHOR: MEGH SHUKLA. No one else. Certain parts of code from EYRC experiments folder
FILENAME : MAIN.C
THEME: Pizza Delivery
Functions: Only one .C contains program. contains lot of folders
GLOBAL VARIABLES: Declarations before first function are only global variables used
*/
//PLEASE NOTE: IN CASE OF PUSH_RELOAD ERROR, PLEASE USE DIFFERENT VERSION OF ATMEL STUDIO 6.
//PLEASE MINIMIZE ALL FUNCTIONS FIRST
//VERY VERY IMPORTANT. The soul of code resides in LOGIC(); function, where robot takes time related decisions. Request to read that carefully
#define __OPTIMIZE__ -O0
#define F_CPU 14745600
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <math.h> //included to support power function
#include "lcd.h"
#include <stdlib.h>
volatile unsigned long int pulse = 0; //to keep the track of the number of pulses generated by the color sensor
volatile unsigned long int red; // variable to store the pulse count when read_red function is called
volatile unsigned long int blue; // variable to store the pulse count when read_blue function is called
volatile unsigned long int green; // variable to store the pulse count when read_green function is called
unsigned char ADC_Conversion(unsigned char);
unsigned char ADC_Value;
unsigned char sharp;
unsigned int value;
volatile long int ShaftCountLeft = 0; //to keep track of left position encoder
volatile long int ShaftCountRight = 0; //to keep track of right position encoder
unsigned int Degrees; //to accept angle in degrees for turning
volatile long int time_ssd = 0;
volatile long int temp_time_ssd=0;
unsigned char lookup[10] = {0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09};
volatile unsigned int cwl = 0, rwl = 0, lwl = 0;
volatile unsigned char a=230,b=230; //line following
unsigned char shop_node;
unsigned char pizza_shop[10] = {0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99}; //Elements define what pizza at individual; stop
unsigned char order_timeline_1[7] = {0x71,0xD8,0xA2,0x5B,0x7C,0x95,0xE5}; //Order Timeline for pizza type and house
unsigned char order_track[7] = {0,0,0,0,0,0,0};
unsigned char order_type[7] = {'r','r','p','p','r','p','p'};
unsigned int order_time[7] = {40,100,115,200,250,350,350};
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};
unsigned char buzzer_control_bit=0;
unsigned char ssd_freeze=0;
volatile long int fast_time=0;
unsigned char attempt_two_delivery=0;
//initalize port to use encoder interrupts
void left_encoder_pin_config (void) //Position Encoder
{
DDRE = DDRE & 0xEF; //Set the direction of the PORTE 4 pin as input
PORTE = PORTE | 0x10; //Enable internal pull-up for PORTE 4 pin
}
void right_encoder_pin_config (void)
{
DDRE = DDRE & 0xDF; //Set the direction of the PORTE 5 pin as input
PORTE = PORTE | 0x20; //Enable internal pull-up for PORTE 5 pin
}
//initalize servo motor pins
void servo1_pin_config (void)
{
DDRB = DDRB | 0x20; //making PORTB 5 pin output
PORTB = PORTB | 0x20; //setting PORTB 5 pin to logic 1
}
void servo2_pin_config (void)
{
DDRB = DDRB | 0x40; //making PORTB 6 pin output
PORTB = PORTB | 0x40; //setting PORTB 6 pin to logic 1
}
void servo3_pin_config (void)
{
DDRB = DDRB | 0x80; //making PORTB 7 pin output
PORTB = PORTB | 0x80; //setting PORTB 7 pin to logic 1
}
//initalize ADC and ADC CONVERSION to convert analog to digital values. Used from experiments
void lcd_port_config (void) //ADC
{
DDRC = DDRC | 0xF7;
PORTC = PORTC & 0x80;
}
void adc_pin_config (void)
{
DDRF = 0x00;
PORTF = 0x00;
DDRK = 0x00;
PORTK = 0x00;
}
void adc_init()
{
ADCSRA = 0x00;
ADCSRB = 0x00;
ADMUX = 0x20;
ACSR = 0x80;
ADCSRA = 0x86;
}
unsigned char ADC_Conversion(unsigned char Ch)
{
unsigned char a;
if(Ch>7)
{
ADCSRB = 0x08;
}
Ch = Ch & 0x07;
ADMUX= 0x20| Ch;
ADCSRA = ADCSRA | 0x40;
while((ADCSRA&0x10)==0);
a=ADCH;
ADCSRA = ADCSRA|0x10;
ADCSRB = 0x00;
return a;
}
void print_sensor(char row, char coloumn, unsigned char channel)
{
ADC_Value = ADC_Conversion(channel);
lcd_print(row, coloumn, ADC_Value, 3);
}
/*
FUNCTION NAME: SSD DISPLAY
INPUT: TIME VARIABLE
OUPTPUT : SSD
LOGIC: EACH DIGIT IS obtained by dividing by 100,10 and remainder. at one instant, one SSD selected. Display on that
EXAMPLE : ssd_display(time_ssd);
*/
void ssd_display(volatile long int time)
{
unsigned char PortLrestore;
PortLrestore = PINL;
PortLrestore = PortLrestore | 0x04;//enabling one pin ( CA0) AT A TIME
PortLrestore = PortLrestore & 0xBC;
PORTL = PortLrestore;
PORTJ = lookup[(time/100)];
_delay_us(3000);
PortLrestore = PINL;
PortLrestore = PortLrestore | 0x02;
PortLrestore = PortLrestore & 0xBA;
PORTL = PortLrestore;
PORTJ = lookup[((time%100)/10)];//DIVIDING THE internal timer count working
_delay_us(3000);
PortLrestore = PINL;
PortLrestore = PortLrestore | 0x01;
PortLrestore = PortLrestore & 0xB9;
PORTL = PortLrestore;
PORTJ = lookup[((time%100)%10)];
}
//switch buzzer on and off .experiment function
void buzzer_on (void)
{
unsigned char port_restore = 0;
port_restore = PINC;
port_restore = port_restore | 0x08;
PORTC = port_restore;
}
void buzzer_off (void)
{
unsigned char port_restore = 0;
port_restore = PINC;
port_restore = port_restore & 0xF7;
PORTC = port_restore;
}
/*
FUNCTION: DELIVERED CONTROL
INPUT: NONE
OUTPUT: Buzzer 1 second, timer freeze 5s
*/
void delivered_control(void)
{
if(fast_time<=32) //fast time = count of prescalar 8 timer fast time=32 indicates 1s
buzzer_on();
else
buzzer_off();
if(fast_time<=160)
ssd_display(temp_time_ssd);
else
{
ssd_display(time_ssd);
ssd_freeze=0;
fast_time=0;
}
}
void motion_pin_config (void) //PWM_Motion
{
DDRA = DDRA | 0x0F;
PORTA = PORTA & 0xF0;
DDRL = DDRL | 0x18; //configuring the motors though port A
}
void timer5_init()
{
TCCR5B = 0x00;
TCNT5H = 0xFF;
TCNT5L = 0x01;
OCR5AH = 0x00;
OCR5AL = 0xFF;
OCR5BH = 0x00;
OCR5BL = 0xFF;
OCR5CH = 0x00;
OCR5CL = 0xFF;
TCCR5A = 0xA9;
TCCR5B = 0x0B;
}
//Ready made function for PWM
void velocity (unsigned char left_motor, unsigned char right_motor)
{
OCR5AL = (unsigned char)left_motor;
OCR5BL = (unsigned char)right_motor;
}//velocity function used for pwm-Pulse width modulation
//specify PORT A to change direction of motor rotation
void motion_set (unsigned char Direction)
{
unsigned char PortARestore = 0;
Direction &= 0x0F;
PortARestore = PORTA;
PortARestore &= 0xF0;
PortARestore |= Direction;
PORTA = PortARestore;
}
void stop (void)
{
motion_set(0x00);
}
void forward (void) //both wheels forward
{
motion_set(0x06);
}
void back (void) //both wheels backward
{
motion_set(0x09);
}
//rotate left till black line
void left (void) //Left wheel backward, Right wheel forward
{
EIMSK = EIMSK & 0xCF; //masking position encoder interrupts
velocity(200,200); //works with 150 150
motion_set(0x05);
_delay_ms(700);
while(1)
{
cwl=ADC_Conversion(2);
lcd_print(1,6,cwl,3);
if(cwl>=10)
{
stop();
_delay_ms(5);
EIMSK = EIMSK | 0x30;
velocity(255,253);
break;
}
}
}
//rotate right till black line
void right (void) //Left wheel forward, Right wheel backward
{
EIMSK = EIMSK & 0xCF; //masking position encoder interrupts
velocity(200,200);
motion_set(0x0A);
_delay_ms(700);
while(1)
{
cwl=ADC_Conversion(2);
lcd_print(1,6,cwl,3);
if(cwl>=10)
{
stop();
_delay_ms(5);
EIMSK = EIMSK | 0x30;
velocity(255,253);
break;
}
}
}
void soft_left (void) //Left wheel stationary, Right wheel forward
{
motion_set(0x04);
}
void soft_right (void) //Left wheel forward, Right wheel is stationary
{
motion_set(0x02);
}
void soft_left_2 (void) //Left wheel backward, right wheel stationary
{
motion_set(0x01);
}
void soft_right_2 (void) //Left wheel stationary, Right wheel backward
{
motion_set(0x08);
}
void linear_distance_mm(unsigned int DistanceInMM)
{
float ReqdShaftCount = 0;
unsigned long int ReqdShaftCountInt = 0;
ReqdShaftCount = DistanceInMM / 5.338; // division by resolution to get shaft count
ReqdShaftCountInt = (unsigned long int) ReqdShaftCount;
ShaftCountRight = 0;
while(1)
{
lwl = ADC_Conversion(3);
cwl = ADC_Conversion(2);
rwl = ADC_Conversion(1);
lcd_print(1,1,lwl,3); //Prints value of White Line Sensor1 LEFT
lcd_print(1,6,cwl,3); //Prints Value of White Line Sensor2 CENTRE
lcd_print(1,10,rwl,3); //Prints Value of White Line Sensor3 RIGHT
if(ShaftCountRight > ReqdShaftCountInt)
{
break;
}
}
stop(); //Stop robot
}
//move a fixed distane, all from experiments folder
void forward_mm(unsigned int DistanceInMM)
{
forward();
linear_distance_mm(DistanceInMM);
}
void back_mm(unsigned int DistanceInMM)
{
back();
linear_distance_mm(DistanceInMM);
}
void left_position_encoder_interrupt_init (void) //Interrupt 4 enable
{
cli(); //Clears the global interrupt
EICRB = EICRB | 0x02; // INT4 is set to trigger with falling edge
EIMSK = EIMSK | 0x10; // Enable Interrupt INT4 for left position encoder
sei(); // Enables the global interrupt
}
void right_position_encoder_interrupt_init (void) //Interrupt 5 enable
{
cli();
EICRB = EICRB | 0x08; // INT5 is set to trigger with falling edge
EIMSK = EIMSK | 0x20; // Enable Interrupt INT5 for right position encoder
sei();
}
void angle_rotate(unsigned int Degrees)
{
float ReqdShaftCount = 0;
unsigned long int ReqdShaftCountInt = 0;
ReqdShaftCount = (float) Degrees/ 4.090; // division by resolution to get shaft count
ReqdShaftCountInt = (unsigned int) ReqdShaftCount;
ShaftCountRight = 0;
ShaftCountLeft = 0;
while (1)
{
if((ShaftCountRight >= ReqdShaftCountInt) | (ShaftCountLeft >= ReqdShaftCountInt))
break;
}
stop(); //Stop robot
}
void left1 (void) //Left wheel backward, Right wheel forward
{
motion_set(0x05);
}
void right1 (void) //Left wheel forward, Right wheel backward
{
motion_set(0x0A);
}
void left_degrees(unsigned int Degrees)
{
// 88 pulses for 360 degrees rotation 4.090 degrees per count
left1(); //Turn left
angle_rotate(Degrees);
}
void right_degrees(unsigned int Degrees)
{
// 88 pulses for 360 degrees rotation 4.090 degrees per count
right1(); //Turn right
angle_rotate(Degrees);
}
void soft_left_degrees(unsigned int Degrees)
{
// 176 pulses for 360 degrees rotation 2.045 degrees per count
soft_left(); //Turn soft left
Degrees=Degrees*2;
angle_rotate(Degrees);
}
void soft_right_degrees(unsigned int Degrees)
{
// 176 pulses for 360 degrees rotation 2.045 degrees per count
soft_right(); //Turn soft right
Degrees=Degrees*2;
angle_rotate(Degrees);
}
void soft_left_2_degrees(unsigned int Degrees)
{
// 176 pulses for 360 degrees rotation 2.045 degrees per count
soft_left_2(); //Turn reverse soft left
Degrees=Degrees*2;
angle_rotate(Degrees);
}
void soft_right_2_degrees(unsigned int Degrees)
{
// 176 pulses for 360 degrees rotation 2.045 degrees per count
soft_right_2(); //Turn reverse soft right
Degrees=Degrees*2;
angle_rotate(Degrees);
}
void seven_segment_pin_config (void)
{
DDRJ = DDRJ | 0xFF; //all Seven Segments pins set as output
DDRL = DDRL | 0x47;
PORTL = PORTL | 0x47;
PORTJ = 0x01;
}
/*
FUNCTIONNAME: INterrupt trigger
INPUT: NONE
OUPut: Trigger interrupt level trigg
LOGIC: Set to 0 to trigger interrupt
*/
void interruptTrigger( void )
{
if(cwl>40 && (rwl>20 || lwl>20)) //cwl>40 && (rwl>20 || lwl>20)
{
PORTD = PORTD & 0xFD; //Interrupt
PORTD = PORTD | 0x02;
stop();
_delay_ms(5);
forward_mm(35); //65mm distance
_delay_ms(5);
forward();
}
}
/*
FUNCTION NAME: Line following
Input : none
Outpur: line follow
logic: if right white line showing greater value, turn right
elsle if left wlgreater, turn left. if no white line showing greater value, use previous values to search for black line
*/
void lineFollowing( void )
{
lwl = ADC_Conversion(3);
cwl = ADC_Conversion(2);
rwl = ADC_Conversion(1);
if(lwl>15 && cwl<9)
{
velocity(30,240);
a=30;
b=240;
} //90,240
else if (rwl>13 && cwl<9) //Normal Line Following
{
velocity(250,30);
a=250;
b=30;
} //250,70
else if (cwl>10 && (rwl<9 || lwl<10))
velocity(255,253);
else if(lwl<10 && (cwl<9 && rwl<9))
velocity(a,b);
}
void buzzer_pin_config (void)
{
DDRC = DDRC | 0x08; //Setting PORTC 3 as outpt
PORTC = PORTC & 0xF7; //Setting PORTC 3 logic low to turnoff buzzer
}
void color_sensor_pin_config(void)
{
DDRD = DDRD | 0xFE; //set PD0 as input for color sensor output
PORTD = PORTD | 0x01;//Enable internal pull-up for PORTD 0 pin
}
void led_pin_config(void)
{
DDRH = DDRH | 0x70; //PH 4,5,6 output
PORTH = PORTH | 0x70; //initially led off all high
}
//funtions used from colour sensor tutorial
//select a filter and count the pulses
void color_sensor_scaling() //This function is used to select the scaled down version of the original frequency of the output generated by the color sensor, generally 20% scaling is preferable, though you can change the values as per your application by referring datasheet
{
//Output Scaling 20% from datasheet
//PORTD = PORTD & 0xEF;
PORTD = PORTD | 0x10; //set S0 high
//PORTD = PORTD & 0xDF; //set S1 low
PORTD = PORTD | 0x20; //set S1 high
}
void filter_red(void) //Used to select red filter
{
//Filter Select - red filter
PORTD = PORTD & 0xBF; //set S2 low
PORTD = PORTD & 0x7F; //set S3 low
}
void filter_green(void) //Used to select green filter
{
//Filter Select - green filter
PORTD = PORTD | 0x40; //set S2 High
PORTD = PORTD | 0x80; //set S3 High
}
void filter_blue(void) //Used to select blue filter
{
//Filter Select - blue filter
PORTD = PORTD & 0xBF; //set S2 low
PORTD = PORTD | 0x80; //set S3 High
}
void red_read(void) // function to select red filter and display the count generated by the sensor on LCD. The count will be more if the color is red. The count will be very less if its blue or green.
{
//Red
EIMSK = EIMSK | 0x01;
filter_red(); //select red filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
red = pulse; //store the count in variable called red
EIMSK = EIMSK & 0xFE;
}
void green_read(void) // function to select green filter and display the count generated by the sensor on LCD. The count will be more if the color is green. The count will be very less if its blue or red.
{
//Green
EIMSK = EIMSK | 0x01;
filter_green(); //select green filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
green = pulse; //store the count in variable called green
EIMSK = EIMSK & 0xFE;
}
void blue_read(void) // function to select blue filter and display the count generated by the sensor on LCD. The count will be more if the color is blue. The count will be very less if its red or green.
{
//Blue
EIMSK = EIMSK | 0x01;
filter_blue(); //select blue filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
blue = pulse; //store the count in variable called blue
EIMSK = EIMSK & 0xFE;
}
/*FUNCTION NAME:
MOVE SHOP
Input: current locaiotn at shop and next shop location to go to.
LOGIC:
If at pizza counter, specify by 's'. If going to 6,7,8,9,10, robot takes right and counts black boxes on flex it encounters before turning
similar for 1,2,3,4,5
If location is non start, then depending on if first shop is on one side and second shop on other side, interrupt is counted
black squares increment shop node
*/
void move_shop (unsigned char current, unsigned char next)
{
if(current=='s')
{
if(next>5) //from start to right pizza
{
shop_node=5;
right();
_delay_ms(10);
forward();
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==next)
{
stop();
_delay_ms(20);
forward_mm(40);
forward_mm(60);
_delay_ms(10);
soft_right_2_degrees(95);
break;
}
}
}
if(next<=5) //from start to left pizza
{
shop_node=next;
left();
right_degrees(10);
_delay_ms(10);
forward();
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==6)
{
stop();
_delay_ms(20);
forward_mm(40);
forward_mm(60);
_delay_ms(10);
soft_left_2_degrees(92);
break;
}
}
}
}
else //inter node assumed robot facing houses
{
if((next-current)>0) //from one pizza to another
{
shop_node=0;
soft_left_degrees(90);
back_mm(10);
_delay_ms(10);
forward();
if((current>5&&next>5)||(current<=5&&next<=5))
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==(next-current))
{
stop();
_delay_ms(20);
forward_mm(40);
forward_mm(60);
_delay_ms(10);
soft_right_2_degrees(97);
break;
}
}
}
if((current<=5&&next>5))
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==((next-current)+1))
{
stop();
_delay_ms(20);
forward_mm(40);
forward_mm(60);
_delay_ms(10);
soft_right_2_degrees(97);
break;
}
}
}
}
if((next-current)<0)
{
shop_node=0;
soft_right_degrees(90);
back_mm(10);
_delay_ms(10);
forward();
if((current>5&&next>5)||(current<=5&&next<=5))
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==(current-next))
{
stop();
_delay_ms(20);
forward_mm(40);
forward_mm(60);
_delay_ms(10);
soft_left_2_degrees(92);
break;
}
}
}
if((current>5&&next<=5))
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==((current-next)+1))
{
stop();
_delay_ms(20);
forward_mm(40);
forward_mm(60);
_delay_ms(10);
soft_left_2_degrees(92);
break;
}
}
}
}
}
}
/*FINCTION : Return shop
Depending on location of shop, it takes left or right turn and travels towards pizza counter
black squares used to counter interrupt driven shop node variable. Logical equations determine if counter reached or no
*/
void return_shop(unsigned char shop)
{
if(shop<=5)
{
shop_node=0;
soft_left_degrees(90);
back_mm(10); //Before return robot should face the direction of the houses
_delay_ms(5);
forward();
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==(6-shop))
{
stop();
forward_mm(30);
_delay_ms(5);
left();
_delay_ms(5);
break;
}
}
}
if(shop>5)
{
shop_node=0;
soft_right_degrees(90);
back_mm(10);
_delay_ms(5);
forward();
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==(shop-5))
{
stop();
forward_mm(30);
_delay_ms(5);
right();
_delay_ms(5);
break;
}
}
}
}
//Travel from one node to next node. IF CONdition specifies values of wl sensor reading at black sqaures
void node_node( void )
{
forward();
while(1)
{
ssd_display(time_ssd);
lineFollowing();
if(cwl>40 && (rwl>20 || lwl>20))
{
stop();
forward_mm(75);
_delay_ms(5);
break;
}
}
}
/*FUNCTION: OUTER RING
LOGIC: Houses divided into outer road and inner road. outer=1,2,3,4,5,6,7,8
rest are inside.
depending on house value, robot takes left or right. Interrupts used to count number of black squares crossed to reach
particular destinatatioin which satisfies condition in IF
*/
//Input is one house to which to got to
void outer_ring(unsigned char h1)
{
shop_node=0; //assume outer ring node
if(h1<=4) //bifurcation point between h1-h4 and h5-h8
left();
else
right();
forward();
if((h1>2) && (h1<=4))
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==(h1+1))
{
stop();
forward_mm(20);
_delay_ms(5);
left();
node_node();
break;
}
}
}
else if(h1<3)
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==h1)
{
stop();
forward_mm(20);
_delay_ms(5);
left();
node_node();
break;
}
}
}
else if(h1>6)
{
shop_node=h1;
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==9)
{
stop();
forward_mm(20);
right();
node_node();
break;
}
}
}
else
{
shop_node=h1;
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==10)
{
stop();
forward_mm(20);
_delay_ms(5);
right();
node_node();
break;
}
}
}
}
//Similar function to identify houses in the inner ring
void inner_ring(unsigned char h1)
{
shop_node=0;
if((h1==9) || (h1==10))
{
left();
forward();
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==1)
{
if(h1==9)
{
stop();
forward_mm(20);
_delay_ms(5);
break;
}
else
{
stop();
forward_mm(20);
_delay_ms(5);
right();
forward();
break;
}
}
}
if(h1==10)
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==3)
{
stop();
forward_mm(20);
_delay_ms(5);
break;
}
}
}
}
else
{
right();
forward();
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==1)
{
if(h1==12)
{
stop();
forward_mm(20);
_delay_ms(5);
break;
}
else
{
stop();
forward_mm(20);
_delay_ms(5);
left();
forward();
break;
}
}
}
if(h1==11)
{
while(1)
{
lineFollowing();
interruptTrigger();
if(shop_node==3)
{
stop();
forward_mm(20);
_delay_ms(5);
break;
}
}
}
}
}
//Outer outer
//travel between two houses in the outer loop
//two inputs to identify house to reach from first house
//expressions determine if house reached or not
void outer_outer(unsigned char h1, unsigned char h2)
{
shop_node=0;
if(((h2-h1)<=4 && (h2-h1)>0)||((h2-h1)<=-4 && (h2-h1)>=-7)) //conditions robot takes left
{
left();
forward();
while(1)
{
lineFollowing();
interruptTrigger();
if((h2-h1)<=4 && (h2-h1)>0)
{
if(h1%2==0) //even house
{
if(h2<=h1+2)
{
if(shop_node==(h2-h1+1))
{
stop();
forward_mm(20);
_delay_ms(5);
break;
}
}
else
{
if(shop_node==(h2-h1+2))