-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final Code
4988 lines (4514 loc) · 111 KB
/
Final Code
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
/*
? * Team Id :eYRC-HD#3761
? * Team Leader:Sachin Aggarwal
? * Author List :Sachin Aggarwal,Hitesh Rishi,Sachin Gupta
? * Filename:final.c
? * Theme: Hazardous Waste Disposal
? * Functions: buzzer_pin_config (void), servo1_pin_config (void), servo2_pin_config (void), servo3_pin_config (void), color_sensor_pin_config(void),
left_encoder_pin_config (void), right_encoder_pin_config (void), lcd_port_config (void), adc_pin_config (void), motion_pin_config (void), port_init(),
left_position_encoder_interrupt_init (void), right_position_encoder_interrupt_init (void), timer5_init(),timer1_init(void), adc_init(),
char ADC_Conversion(unsigned char Ch), print_sensor(char row, char coloumn,unsigned char channel), int Sharp_GP2D12_estimation(unsigned char adc_reading),
color_sensor_pin_interrupt_init(void), velocity (unsigned char left_motor, unsigned char right_motor), motion_set (unsigned char Direction),
angle_rotate(unsigned int Degrees), left_degrees(unsigned int Degrees), right_degrees(unsigned int Degrees), soft_left_degrees(unsigned int Degrees),
soft_right_degrees(unsigned int Degrees), soft_left_2_degrees(unsigned int Degrees), soft_right_2_degrees(unsigned int Degrees), forward (void), back (void),
stop (void), left (void), right (void), soft_left (void), soft_right (void), soft_left_2 (void), soft_right_2 (void), init_devices, filter_red(void),
filter_green(void), filter_blue(void), filter_clear(void), color_sensor_scaling(),red_read(void), green_read(void), blue_read(void), check_color(void),
servo_1(unsigned char degrees), servo_2(unsigned char degrees), servo_3(unsigned char degrees), servo_1_free (void), servo_2_free (void), servo_3_free (void),
blackline(),turn_left(),center_left(),turn_right(),center_right(),buzzer_on (void), buzzer_off(void), linear_distance_mm(unsigned int DistanceInMM),
forward_mm(unsigned int DistanceInMM), back_mm(unsigned int DistanceInMM), rotation_using_blackline_right(void), rotation_using_blackline_left(void),
sense_top2_HZ_waste_block_color(void), pick_the_top_HZ_waste(),drop_the_block(void), pick_top_weight_block(),pick_bottom_weight_block(),main(void)
? * Global Variables:ShaftCountLeft,ShaftCountRight,Degrees,flag1_color,flag2_color,flag3_color,
top_HZ_waste_block_color,middle_HZ_waste_color,bottom_HZ_waste_block_color,bottom_position
pulse,red,green,blue,value.
? *
? */
#define F_CPU 14745600
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd.h"
volatile unsigned long int ShaftCountLeft = 0; //to keep track of left position encoder
volatile unsigned long int ShaftCountRight = 0; //to keep track of right position encoder
volatile unsigned int Degrees;
//to accept angle in degrees for turning
char flag1_color,flag2_color,flag3_color,top_HZ_waste_block_color,middle_HZ_waste_color,bottom_HZ_waste_block_color,bottom_position; // to store the colors of hazardous waste blocks and color of flags
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
void buzzer_pin_config (void)
{
DDRC = DDRC | 0x08; //Setting PORTC 3 as output
PORTC = PORTC & 0xF7; //Setting PORTC 3 logic low to turnoff buzzer
}
//Configure PORTB 5 pin for servo motor 1 operation
void servo1_pin_config (void)
{
DDRB = DDRB | 0x20; //making PORTB 5 pin output
PORTB = PORTB | 0x20; //setting PORTB 5 pin to logic 1
}
//Configure PORTB 6 pin for servo motor 2 operation
void servo2_pin_config (void)
{
DDRB = DDRB | 0x40; //making PORTB 6 pin output
PORTB = PORTB | 0x40; //setting PORTB 6 pin to logic 1
}
//Configure PORTB 7 pin for servo motor 3 operation
void servo3_pin_config (void)
{
DDRB = DDRB | 0x80; //making PORTB 7 pin output
PORTB = PORTB | 0x80; //setting PORTB 7 pin to logic 1
}
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 port_init();
void timer5_init();
void velocity(unsigned char, unsigned char);
void motors_delay();
unsigned char ADC_Conversion(unsigned char);
unsigned char ADC_Value;
unsigned char sharp, distance, adc_reading;
unsigned int value;
unsigned char flag = 0;
unsigned char Left_white_line = 0;
unsigned char Center_white_line = 0;
unsigned char Right_white_line = 0;
void left_encoder_pin_config (void)
{
DDRE = DDRE & 0xEF; //Set the direction of the PORTE 4 pin as input
PORTE = PORTE | 0x10; //Enable internal pull-up for PORTE 4 pin
}
//Function to configure INT5 (PORTE 5) pin as input for the right position encoder
void right_encoder_pin_config (void)
{
DDRE = DDRE & 0xDF; //Set the direction of the PORTE 4 pin as input
PORTE = PORTE | 0x20; //Enable internal pull-up for PORTE 4 pin
}
//Function to configure LCD port
void lcd_port_config (void)
{
DDRC = DDRC | 0xF7; //all the LCD pin's direction set as output
PORTC = PORTC & 0x80; // all the LCD pins are set to logic 0 except PORTC 7
}
//ADC pin configuration
void adc_pin_config (void)
{
DDRF = 0x00;
PORTF = 0x00;
DDRK = 0x00;
PORTK = 0x00;
}
//Function to configure ports to enable robot's motion
void motion_pin_config (void)
{
DDRA = DDRA | 0x0F;
PORTA = PORTA & 0xF0;
DDRL = DDRL | 0x18; //Setting PL3 and PL4 pins as output for PWM generation
PORTL = PORTL | 0x18; //PL3 and PL4 pins are for velocity control using PWM.
}
//ISR for color sensor
ISR(INT0_vect)
{
pulse++; //increment on receiving pulse from the color sensor
}
//Function to Initialize PORTS
void port_init()
{
buzzer_pin_config();
servo1_pin_config(); //Configure PORTB 5 pin for servo motor 1 operation
servo2_pin_config(); //Configure PORTB 6 pin for servo motor 2 operation
servo3_pin_config(); //Configure PORTB 7 pin for servo motor 3 operation
color_sensor_pin_config(); //color sensor pin configuration
lcd_port_config();
adc_pin_config();
motion_pin_config();
left_encoder_pin_config(); //left encoder pin config
right_encoder_pin_config(); //right encoder pin config
}
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(); //Clears the global interrupt
EICRB = EICRB | 0x08; // INT5 is set to trigger with falling edge
EIMSK = EIMSK | 0x20; // Enable Interrupt INT5 for right position encoder
sei(); // Enables the global interrupt
}
ISR(INT5_vect)
{
ShaftCountRight++; //increment right shaft position count
}
//ISR for left position encoder
ISR(INT4_vect)
{
ShaftCountLeft++; //increment left shaft position count
}
// Timer 5 initialized in PWM mode for velocity control
// Pre-scale:256
// PWM 8bit fast, TOP=0x00FF
// Timer Frequency:225.000Hz
void timer5_init()
{
TCCR5B = 0x00; //Stop
TCNT5H = 0xFF; //Counter higher 8-bit value to which OCR5xH value is compared with
TCNT5L = 0x01; //Counter lower 8-bit value to which OCR5xH value is compared with
OCR5AH = 0x00; //Output compare register high value for Left Motor
OCR5AL = 0xFF; //Output compare register low value for Left Motor
OCR5BH = 0x00; //Output compare register high value for Right Motor
OCR5BL = 0xFF; //Output compare register low value for Right Motor
OCR5CH = 0x00; //Output compare register high value for Motor C1
OCR5CL = 0xFF; //Output compare register low value for Motor C1
TCCR5A = 0xA9; /*{COM5A1=1, COM5A0=0; COM5B1=1, COM5B0=0; COM5C1=1 COM5C0=0}
For Overriding normal port functionality to OCRnA outputs.
{WGM51=0, WGM80=1} Along With WGM52 in TCCR5B for Selecting FAST PWM 8-bit Mode*/
TCCR5B = 0x0B; //WGM12=1; CS12=0, CS11=1, CS10=1 (Prescaler=64)
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFC; //Counter high value to which OCR1xH value is to be compared with
TCNT1L = 0x01; //Counter low value to which OCR1xH value is to be compared with
OCR1AH = 0x03; //Output compare Register high value for servo 1
OCR1AL = 0xFF; //Output Compare Register low Value For servo 1
OCR1BH = 0x03; //Output compare Register high value for servo 2
OCR1BL = 0xFF; //Output Compare Register low Value For servo 2
OCR1CH = 0x03; //Output compare Register high value for servo 3
OCR1CL = 0xFF; //Output Compare Register low Value For servo 3
ICR1H = 0x03;
ICR1L = 0xFF;
TCCR1A = 0xAB; /*{COM1A1=1, COM1A0=0; COM1B1=1, COM1B0=0; COM1C1=1 COM1C0=0}
For Overriding normal port functionality to OCRnA outputs.
{WGM11=1, WGM10=1} Along With WGM12 in TCCR1B for Selecting FAST PWM Mode*/
TCCR1C = 0x00;
TCCR1B = 0x0C; //WGM12=1; CS12=1, CS11=0, CS10=0 (Prescaler=256)
}
void adc_init()
{
ADCSRA = 0x00;
ADCSRB = 0x00; //MUX5 = 0
ADMUX = 0x20; //Vref=5V external --- ADLAR=1 --- MUX4:0 = 0000
ACSR = 0x80;
ADCSRA = 0x86; //ADEN=1 --- ADIE=1 --- ADPS2:0 = 1 1 0
}
//Function For ADC Conversion
unsigned char ADC_Conversion(unsigned char Ch)
{
unsigned char a;
if(Ch>7)
{
ADCSRB = 0x08;
}
Ch = Ch & 0x07;
ADMUX= 0x20| Ch;
ADCSRA = ADCSRA | 0x40; //Set start conversion bit
while((ADCSRA&0x10)==0); //Wait for conversion to complete
a=ADCH;
ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to it
ADCSRB = 0x00;
return a;
}
//Function To Print Sensor Values At Desired Row And Column Location on LCD
void print_sensor(char row, char coloumn,unsigned char channel)
{
ADC_Value = ADC_Conversion(channel);
lcd_print(row, coloumn, ADC_Value, 3);
}
// This Function calculates the actual distance in millimeters(mm) from the input
// analog value of Sharp Sensor.
unsigned int Sharp_GP2D12_estimation(unsigned char adc_reading)
{
float distance;
unsigned int distanceInt;
distance = (int)(10.00*(2799.6*(1.00/(pow(adc_reading,1.1546)))));
distanceInt = (int)distance;
if(distanceInt>800)
{
distanceInt=800;
}
return distanceInt;
}
void color_sensor_pin_interrupt_init(void) //Interrupt 0 enable
{
cli(); //Clears the global interrupt
EICRA = EICRA | 0x02; // INT0 is set to trigger with falling edge
EIMSK = EIMSK | 0x01; // Enable Interrupt INT0 for color sensor
sei(); // Enables the global interrupt
}
//Function for velocity control
void velocity (unsigned char left_motor, unsigned char right_motor)
{
OCR5AL = (unsigned char)left_motor;
OCR5BL = (unsigned char)right_motor;
}
//Function used for setting motor's direction
void motion_set (unsigned char Direction)
{
unsigned char PortARestore = 0;
Direction &= 0x0F; // removing upper nibbel for the protection
PortARestore = PORTA; // reading the PORTA original status
PortARestore &= 0xF0; // making lower direction nibbel to 0
PortARestore |= Direction; // adding lower nibbel for forward command and restoring the PORTA status
PORTA = PortARestore; // executing the command
}
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 left_degrees(unsigned int Degrees)
{
// 88 pulses for 360 degrees rotation 4.090 degrees per count
left(); //Turn left
angle_rotate(Degrees);
}
void right_degrees(unsigned int Degrees)
{
// 88 pulses for 360 degrees rotation 4.090 degrees per count
right(); //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 forward (void)
{
motion_set (0x06);
}
void back (void) //both wheels backward
{
motion_set(0x09);
}
void stop (void)
{
motion_set (0x00);
}
void left (void) //Left wheel backward, Right wheel forward
{
motion_set(0x05);
}
void right (void) //Left wheel forward, Right wheel backward
{
motion_set(0x0A);
}
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 init_devices (void)
{
cli(); //Clears the global interrupts
port_init();
adc_init();
timer5_init();
left_position_encoder_interrupt_init();
right_position_encoder_interrupt_init();
color_sensor_pin_interrupt_init();
timer1_init();
sei(); //Enables the global interrupts
}
//Filter Selection
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 filter_clear(void) //select no filter
{
//Filter Select - no filter
PORTD = PORTD | 0x40; //set S2 High
PORTD = PORTD & 0x7F; //set S3 Low
}
//Color Sensing Scaling
/*
* Function Name: color_sensor_scaling
* Logic: This function is used to select the scaled down version of the original frequency of the output generated by the color sensor.
* Example Call: color_sensor_scaling();
*/
void color_sensor_scaling()
{
//Output Scaling 20% from datasheet though it can be changed by refering to datasheet
//PORTD = PORTD & 0xEF;
PORTD = PORTD | 0x10; //set S0 high
//PORTD = PORTD & 0xDF; //set S1 low
PORTD = PORTD | 0x20; //set S1 high
}
/*
* Function Name: red_read
* Input: capture the pulses for 100 ms or 0.1 second, store the count in variable called red.
* Output: None
* Logic: 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 or any other color.
* Example Call: red_read();
*
*/
void red_read(void)
{
//Red
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
}
/*
* Function Name: green_read
* Input: capture the pulses for 100 ms or 0.1 second, store the count in variable called green.
* Output: None
* Logic: 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.
* Example Call: green_read();
*
*/
void green_read(void)
{
//Green
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
//Clear the LCD
}
/*
* Function Name: green_read
* Input: capture the pulses for 100 ms or 0.1 second, store the count in variable called green.
* Output: None
* Logic: 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.
* Example Call: blue_read();
*
*/
void blue_read(void)
{
//Blue
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
//Clear the LCD
}
/*
* Function Name: check_color
* Input: None
* Output: Characters 'R','B','G' on the basis of defined conditions in logic.
* Logic: Three different conditions for checking the colors.
eg: if red color pulse is maximum(compared from green and blue pulses) then it detects a red color and returns 'r'
similarly for other 2 colors.
* Example Call: check_color();
*/
char check_color(void)
{
red_read();
_delay_ms(1000); //display the pulse count when red filter is selected
green_read(); //display the pulse count when green filter is selected
_delay_ms(1000);
blue_read(); //display the pulse count when blue filter is selected
_delay_ms(1000);
if((red>green) && (red>blue))
{
return 'R'; //red color
}
if((blue>green) && (blue>red))
{
return 'B'; //blue color
}
if((green>red) && (green>blue))
{
return 'G'; //green color
}
}
void servo_1(unsigned char degrees)
{
float PositionPanServo = 0;
PositionPanServo = ((float)degrees / 1.86) + 35.0;
OCR1AH = 0x00;
OCR1AL = (unsigned char) PositionPanServo;
}
//Function to rotate Servo 2 by a specified angle in the multiples of 1.86 degrees
void servo_2(unsigned char degrees)
{
float PositionTiltServo = 0;
PositionTiltServo = ((float)degrees / 1.86) + 35.0;
OCR1BH = 0x00;
OCR1BL = (unsigned char) PositionTiltServo;
}
//Function to rotate Servo 3 by a specified angle in the multiples of 1.86 degrees
void servo_3(unsigned char degrees)
{
float PositionServo = 0;
PositionServo = ((float)degrees / 1.86) + 35.0;
OCR1CH = 0x00;
OCR1CL = (unsigned char) PositionServo;
}
void servo_1_free (void) //makes servo 1 free rotating
{
OCR1AH = 0x03;
OCR1AL = 0xFF; //Servo 1 off
}
void servo_2_free (void) //makes servo 2 free rotating
{
OCR1BH = 0x03;
OCR1BL = 0xFF; //Servo 2 off
}
void servo_3_free (void) //makes servo 3 free rotating
{
OCR1CH = 0x03;
OCR1CL = 0xFF; //Servo 3 off
}
/*
* Function Name: blackline
* Variables: None
* Input: Left_white_line, Center_white_line, Right_white_line variable values, which are actually the three white line sensor values
respectively.
* Output: Corresponding motion according to the combination defined in logic.
* Logic: Sensors can have seven different conditions inside on basis of 7 different combinations of sensor values.
The combination is commented just above each condition as 010, 101, 111, 000 etc.
0 corresponds for a white region and 1 corresponds for a black region.
eg: 010 represents: Left_white_line sensor color : white
Center_white_line sensor color : black
Right_white_line sensor color : white
A node is defined when all three white line sensors detect black color.
0x0c is the threshold sensor value upto which the sensor reads a white color.
* Example Call: backline();
*/
int blackline()
{
while(1){
Left_white_line = ADC_Conversion(3); //Getting data of Left WL Sensor
Center_white_line = ADC_Conversion(2); //Getting data of Center WL Sensor
Right_white_line = ADC_Conversion(1); //Getting data of Right WL Sensor
if(Center_white_line>0x20 && Left_white_line<0x20 && Right_white_line<0x20) // Center on black line-010
{ forward();
velocity(130,122); //velocity value calibrated as per requirement
}
else if((Left_white_line>0x20 && Center_white_line<0x20) ) //left sensor on black line, take left turn to get back on blackline
{
forward();
velocity(40,120); //velocity of left and right motors calibrated as per requirement
_delay_ms(15);
}
else if((Right_white_line>0x20 && Center_white_line<0x20)) //right sensor on black line, take right turn to get back on blackline
{ forward();
velocity(120,40);
_delay_ms(15);
}
else if(Center_white_line>0x20 && Left_white_line>0x20 && Right_white_line<0x20){//110
stop();
return;
}
else if(Center_white_line<0x20 && Left_white_line>0x20 && Right_white_line>0x20){//101
stop();
return;
}
else if(Center_white_line>0x20 && Left_white_line>0x20 && Right_white_line>0x20){//111
stop();
return;
}
else if(Center_white_line>0x20 && Left_white_line<0x20 && Right_white_line>0x20){//011
stop();
return;
}
}
}
/*
* Function Name: turn_left
* Input: Left_white_line, which is actually the value of leftmost white line sensor.
* Output: Helps in taking a restricted left turn on the basis of certain condition that is defined in logic.
* Logic: Takes a left until its left sensor gets a black value.
0x0c is the threshold sensor value upto which the sensor reads a white color.
* Example Call: turn_left();
*/
void turn_left()
{
left();
while(1)
{
Left_white_line = ADC_Conversion(3); //Getting data of Left WL Sensor
Center_white_line = ADC_Conversion(2); //Getting data of Center WL Sensor
Right_white_line = ADC_Conversion(1); //Getting data of Right WL Sensor
flag=0;
print_sensor(1,1,3); //Prints value of White Line Sensor1
print_sensor(1,5,2); //Prints Value of White Line Sensor2
print_sensor(1,9,1); //Prints Value of White Line Sensor3
velocity(140,140);
_delay_ms(6);
if(Left_white_line>=0x40)
{ stop();
velocity(0,0);
return;
}
}
}
/*
* Function Name: center_left
* Input: Center_white_line, which is actually the value of center white line sensor.
* Output: Rotates the robot to take a restricted right turn.
* Logic: Takes a right until its center sensor gets a black value.
0x0c is the threshold sensor value upto which the sensor reads a white color.
* Example Call: center_left();
*/
void center_left()
{
soft_right_2();
while(1)
{
Left_white_line = ADC_Conversion(3); //Getting data of Left WL Sensor
Center_white_line = ADC_Conversion(2); //Getting data of Center WL Sensor
Right_white_line = ADC_Conversion(1); //Getting data of Right WL Sensor
flag=0;
print_sensor(1,1,3); //Prints value of White Line Sensor1
print_sensor(1,5,2); //Prints Value of White Line Sensor2
print_sensor(1,9,1); //Prints Value of White Line Sensor3
velocity(0,130);
_delay_ms(6);
if(Center_white_line>=0x0b)
{
velocity(0,0);
return;
}
}
}
/*
* Function Name: turn_right
* Input: Right_white_line, which is actually the value of rightmost white line sensor.
* Output: Helps in taking a restricted right turn on the basis of certain condition that is defined in logic.
* Logic: Takes a right until its right sensor gets a black value.
0x0c is the threshold sensor value upto which the sensor reads a white color.
* Example Call: turn_right();
*/
void turn_right()
{
right();
while(1)
{
Left_white_line = ADC_Conversion(3); //Getting data of Left WL Sensor
Center_white_line = ADC_Conversion(2); //Getting data of Center WL Sensor
Right_white_line = ADC_Conversion(1); //Getting data of Right WL Sensor
flag=0;
print_sensor(1,1,3); //Prints value of White Line Sensor1
print_sensor(1,5,2); //Prints Value of White Line Sensor2
print_sensor(1,9,1); //Prints Value of White Line Sensor3
velocity(140,140);
_delay_ms(6);
if(Right_white_line>=0x40)
{
velocity(0,0);
return;
}
}
}
/*
* Function Name: center_right
* Input: Center_white_line, which is actually the value of center white line sensor.
* Output: Rotates the robot to take a restricted left turn.
* Logic: Takes a left until its center sensor gets a black value.
0x0c is the threshold sensor value upto which the sensor reads a white color.
* Example Call: center_right();
*/
void center_right()
{
soft_left_2();
while(1)
{
Left_white_line = ADC_Conversion(3); //Getting data of Left WL Sensor
Center_white_line = ADC_Conversion(2); //Getting data of Center WL Sensor
Right_white_line = ADC_Conversion(1); //Getting data of Right WL Sensor
flag=0;
print_sensor(1,1,3); //Prints value of White Line Sensor1
print_sensor(1,5,2); //Prints Value of White Line Sensor2
print_sensor(1,9,1); //Prints Value of White Line Sensor3
velocity(130,0);
_delay_ms(6);
if(Center_white_line>=0x0b)
{
velocity(0,0);
return;
}
}
}
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 used for moving robot forward by specified distance
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)
{
if(ShaftCountRight > ReqdShaftCountInt)
{
break;
}
}
stop(); //Stop robot
}
void forward_mm(unsigned int DistanceInMM)
{
forward();
linear_distance_mm(DistanceInMM);
}
void back_mm(unsigned int DistanceInMM)
{
back();
linear_distance_mm(DistanceInMM);
}
/*
? * Function Name:rotation_using_blackline_right
? * Logic: This function is used to rotate robot right until it finds a blakline.It is used only when robot is at a node.
Since robot will be at node it must be moved from it or otherwise turn_right function will keep robot at same position.
Thus robot is forwarded and then turn_right function is called.
? * Example Call:rotation_using_blackline_right
? */
void rotation_using_blackline_right(void)
{
forward_mm(89); //robot is forwarded by fixed distance using encoder
stop();
_delay_ms(500);
turn_right(); //robot will rotate right until it gets blackline
velocity(150,150);
_delay_ms(50);
stop();
}
/*
? * Function Name:rotation_using_blackline_left
? * Logic: This function is used to rotate robot left until it finds a blakline.It is used only when robot is at a node.
Since robot will be at node it must be moved from it or otherwise turn_left function will keep robot at same position.
Thus robot is forwarded and then turn_left function is called.
? * Example Call: rotation_using_blackline_left();
? */
void rotation_using_blackline_left(void)
{
forward_mm(89); //robot is forwarded by fixed distance using encoder
stop();
_delay_ms(500);
turn_left(); //robot will rotate right until it gets blackline
velocity(150,150);
_delay_ms(50);
stop();
}
/*
? * Function Name: sense_top2_HZ_waste_block_color
? * Logic:This function will be used to sense the color of top 2 High CCL hazardous waste blocks.
Color sensor is mounted on the gripper and thus gripper as closed and arm is moved to bring.
Once color of top 2 blocks are known remaining color out of R,G,B will be color of bottommost Low CCL block
the color sensor in front of blocks and sense the color of top 2 blocks one by one.
? * Example Call: sense_top2_HZ_waste_block_color();
? */
void sense_top2_HZ_waste_block_color(void)
{
_delay_ms(500);
velocity(130,135);
back_mm(30); //robot must move some back distance before closing the gripper otherwise gripper will hit the blocks.
velocity(115,110);
servo_1(0);
_delay_ms(1000);
servo_3(140); //gripper closes
_delay_ms(1000);
servo_2(135); //gripper i.e. color sensor brought in front of middle block
_delay_ms(1000);
for(int i=0;i<=40;i+=10) //to bring gripper exactly in front of gripper
{
servo_1(i);
_delay_ms(1000);
}
middle_HZ_waste_color=check_color(); //color of middle block is stored in middle_HZ_waste_color
_delay_ms(1000);
servo_1(26); //bring the gripper i.e. color sensor in front of top HZ block
_delay_ms(1000);
top_HZ_waste_block_color=check_color(); //color of top block is stored in top_HZ_waste_block
_delay_ms(5000);
// color sensing done
if(top_HZ_waste_block_color!='R' && middle_HZ_waste_color!='R')
bottom_HZ_waste_block_color='R';
else if(top_HZ_waste_block_color!='G' && middle_HZ_waste_color!='G')
bottom_HZ_waste_block_color='G';
else if(top_HZ_waste_block_color!='B' && middle_HZ_waste_color!='B')
bottom_HZ_waste_block_color='B';
}
/*
? * Function Name: pick_the_top_HZ_waste
? * Logic: This function is used to pick the topmost HZ waste using robotic arm.
This function is used just after sense_top2_HZ_waste_block_color function.
? * Example Call: pick_the_top_HZ_waste();
? */
void pick_the_top_HZ_waste()
{
servo_3(0); //open the gripper
forward_mm(24); //robot moved forward to be able to contain the block
velocity(160,120);
_delay_ms(1000);
stop();
for(int i=0;i<=25;i+=5) //bring the arm to contain the block
{
servo_1(i);
_delay_ms(1000);
}
servo_3(114); //close the gripper
_delay_ms(2000);
servo_1(0); //lift the HZ block
}
/*
? * Function Name: drop_the_block
? * Logic:This function is used to drop the weight block into the container or colored hazardous block at deposition zones using robotic arm
? * Example Call: drop_the_block
? */
void drop_the_block(void)
{
servo_1(0);
_delay_ms(1000);
servo_2(85); //bring arm near container