-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_code.c
1497 lines (1313 loc) · 30.4 KB
/
Final_code.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
/********************************************************************************
Platform: SPARK V
Experiment: Swarm Robotics implementation code
Written by: Shailesh Jain,Mitul Tailor,Paras Gosalia
Last Modification: 30th April 2012
AVR Studio Version 4.17, Build 666
size: 10.9 KB
Concepts covered: Grid exploration by multi-robot system communication using zigbee wireless module
Use baud rate as 9600bps.
Note:
1. Make sure that in the configuration options following settings are
done for proper operation of the code
Microcontroller: atmega16
Frequency: 7372800
Optimization: -Os (For more information read section: Selecting proper optimization
options below figure 4.22 in the hardware manual)
*********************************************************************************/
/********************************************************************************
Copyright (c) 2010, NEX Robotics Pvt. Ltd. -*- c -*-
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
* Source code can be used for academic purpose.
For commercial use permission form the author needs to be taken.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Software released under Creative Commence cc by-nc-sa licence.
For legal information refer to:
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
********************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd.c"
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define N 1 // Direction North
#define S 2 // Direction South
#define E 3 // Direction East
#define W 4 // Direction West
#define THRESHOLD 25 // i.e on LCD Sensor values are betwn 0 To 25 on white line
#define VELOCITY_MAX 100
#define VELOCITY_MIN 60
#define VELOCITY_LOW 0
#define F 0x11
#define B 0x12
#define R 0x13
#define L 0x14
#define M 0x15
#define Servo2port PORTD // servo 2 control pin connection
#define Servo2bit 7
#define S1 OCR2
#define S2 OCR0
unsigned int current_value =0;
volatile unsigned char ShaftCountLeft = 0; //to keep track of left position encoder
volatile unsigned char ShaftCountRight = 0; //to keep track of right position encoder
volatile unsigned int Degrees; //to accept angle in degrees for turning
unsigned char s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0; // varables to store sensors value
unsigned char bot_dir = N;
unsigned int get_start=0;
unsigned int dead[]={0,0,0,0};
unsigned int grid[8][8]=
{
};
unsigned int pathindex = 0;
unsigned int check_node[64];
unsigned int path[]={F,F,F,F,F,F,F,L,
L,F,F,F,F,F,F,R,
R,F,F,F,F,F,F,L,
L,F,F,F,F,F,F,R,
R,F,F,F,F,F,F,L,
L,F,F,F,F,F,F,R,
R,F,F,F,F,F,F,L,
L,F,F,F,F,F,F,M};
unsigned int path1[]={F,F,F,L,L,F,F,R,R,F,F,L,L,F,F,M};
unsigned cond=0;
unsigned int toggel = 0; //used as a variable for buzzer state toggel action
unsigned char value=0,x_cord=0,y_cord=0,length=0,end=0,x_future=0,y_future=0;
unsigned int check_pos =0,to_begin = 0;
unsigned int flag[4]= {0,0,0,0};
unsigned int count =0;
unsigned int k=0,j=0;
unsigned int size = 8;
unsigned int current = 0;
unsigned int counter = 0;
unsigned int bot_id_set =0;
unsigned int cal =0;
unsigned int visited = 0;
char store[4]={0,0,0,0};
char tempor[4]={0,0,0,0};
unsigned int next = 0;
unsigned int visit_node = 0;
unsigned int to_process = 0;
unsigned char rx1=0,rx2=0,rx3=0,rx4=0;
unsigned int total_turn = 0;
unsigned int neigh=0;
unsigned int final_done =0;
unsigned int block_found =0;
unsigned char ADC_Conversion(unsigned char);
unsigned char ADC_Value;
unsigned char Left_white_line = 0;
unsigned char Center_white_line = 0;
unsigned char Right_white_line = 0;
unsigned char Front_IR_Sensor;
unsigned char receive_data=0; // used to save Receiving data
unsigned char ssss=0;
unsigned char bot_id = '0';
unsigned int done[4]={0,0,0,0};
//--------------------****TIMER INTERRUPT**** ----------------------------------------------------
ISR(TIMER0_OVF_vect)
{
// Timer 0 over flow
Servo2port |= (1 << Servo2bit); // set servo 2 control pin
TCNT0 = 0x6F; // reload timer 0 initial value
}
ISR(TIMER0_COMP_vect)
{
// Timer 0 compare match interrupt
Servo2port &= ~(1 << Servo2bit); //clear servo 2 control pin
}
//ISR for right position encoder
ISR(INT0_vect)
{
ShaftCountRight++; //increment right shaft position count
}
//ISR for left position encoder
ISR(INT1_vect)
{
ShaftCountLeft++; //increment left shaft position count
}
//----------------------------------****PORT INITIALIZATION****---------------------------------------------------
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)
{
DDRA = 0x00; //set PORTF direction as input
PORTA = 0x00; //set PORTF pins floating
}
void motion_pin_config (void)
{
DDRB = DDRB | 0x0F; //set direction of the PORTB3 to PORTB0 pins as output
PORTB = PORTB & 0xF0; // set initial value of the PORTB3 to PORTB0 pins to logic 0
DDRD = DDRD | 0x30; //Setting PD4 and PD5 pins as output for PWM generation
PORTD = PORTD | 0x30; //PD4 and PD5 pins are for velocity control using PWM
}
void buzzer_pin_config (void)
{
DDRC = DDRC | 0x08; //Setting PORTC 3 as outpt
PORTC = PORTC & 0xF7; //Setting PORTC 3 logic low to turnoff buzzer
}
//Function to configure INT1 (PORTD 3) pin as input for the left position encoder
void left_encoder_pin_config (void)
{
DDRD = DDRD & 0xF7; //Set the direction of the PORTD 3 pin as input
PORTD = PORTD | 0x08; //Enable internal pull-up for PORTD 3 pin
}
//Function to configure INT0 (PORTD 2) pin as input for the right position encoder
void right_encoder_pin_config (void)
{
DDRD = DDRD & 0xFB; //Set the direction of the PORTD 2 pin as input
PORTD = PORTD | 0x04; //Enable internal pull-up for PORTD 2 pin
}
void port_init()
{
lcd_port_config();
adc_pin_config();
motion_pin_config();
buzzer_pin_config();
left_encoder_pin_config(); //left encoder pin config
right_encoder_pin_config(); //right encoder pin config
}
//-----------------------------------------------------------------------****MOTION CONTROL****------------------------------------------------------------
//Function used for setting motor's direction
void motion_set (unsigned char Direction)
{
unsigned char PortBRestore = 0;
Direction &= 0x0F; // removing upper nibbel as it is not needed
PortBRestore = PORTB; // reading the PORTB's original status
PortBRestore &= 0xF0; // setting lower direction nibbel to 0
PortBRestore |= Direction; // adding lower nibbel for direction command and restoring the PORTB status
PORTB = PortBRestore; // setting the command to the port
}
void forward (void) //both wheels forward
{
motion_set(0x06);
}
void stop (void) //hard stop
{
motion_set(0x00);
}
void back(void)
{
motion_set(0x09);
}
//to move bot differential right
void right(void)
{
motion_set(0x0A);
}
//to move bot differential left
void left(void)
{
motion_set(0x05);
}
void velocity (unsigned char left_motor, unsigned char right_motor)
{
OCR1AH = 0x00;
OCR1AL = left_motor;
OCR1BH = 0x00;
OCR1BL = right_motor;
}
//--------------------------------------****ENCODER FUNCTION****-----------------------------------------------------------
void linear_distance_mm(unsigned int DistanceInMM)
{
float ReqdShaftCount = 0;
unsigned char ReqdShaftCountInt = 0;
ReqdShaftCount = DistanceInMM / 12.92; // division by resolution to get shaft count
ReqdShaftCountInt = (unsigned char)ReqdShaftCount;
ShaftCountRight = 0;
while (1)
{
if (ShaftCountRight > ReqdShaftCountInt)
{
break;
}
}
// stop(); //Stop robot
}
void angle_rotate(unsigned int Degrees)
{
float ReqdShaftCount = 0;
unsigned char ReqdShaftCountInt = 0;
ReqdShaftCount = (float)Degrees / 12.85; // division by resolution to get shaft count
ReqdShaftCountInt = (unsigned char)ReqdShaftCount;
ShaftCountRight = 0;
ShaftCountLeft = 0;
while (1)
{
if ((ShaftCountRight >= ReqdShaftCountInt) | (ShaftCountLeft >= ReqdShaftCountInt))
break;
}
//stop(); //Stop robot
}
void forward_mm(unsigned int DistanceInMM)
{
forward();
linear_distance_mm(DistanceInMM);
}
//function below make the bot to move backward through fixed distance
void back_mm(unsigned int DistanceInMM)
{
back();
linear_distance_mm(DistanceInMM);
}
//function used below rotates the bot as per the angle send
void left_degrees(unsigned int Degrees)
{
// 28 pulses for 360 degrees rotation 12.92 degrees per count
left(); //Turn left
angle_rotate(Degrees);
}
//function used below rotates the bot as per the angle send
void right_degrees(unsigned int Degrees)
{
// 28 pulses for 360 degrees rotation 12.92 degrees per count
right(); //Turn right
angle_rotate(Degrees);
}
//------------------------------------------------------****UART INITIALIZATION****-----------------------------------------------------
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = 0x86;
UBRRL = 0x2F; //set baud rate lo //67 is for 16MHz 9600 baudrate
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
{
}
/* Put data into buffer, sends the data */
UDR = data;
}
//---------------------------------------------------****ADC CONVERSION AND LCD FUNCTION****----------------------------------
//This Function accepts the Channel Number and returns the corresponding Analog Value
unsigned char ADC_Conversion(unsigned char Ch)
{
unsigned char a;
Ch = Ch & 0x07;
ADMUX= 0x20| Ch;
ADCSRA = ADCSRA | 0x40; //Set start conversion bit
while((ADCSRA&0x10)==0); //Wait for ADC conversion to complete
a=ADCH;
ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to it
return a;
}
// This Function prints the Analog Value Of Corresponding Channel No. at required Row and Coloumn Location.
void print_sensor(char row, char coloumn,unsigned char channel)
{
ADC_Value = ADC_Conversion(channel);
lcd_print(row, coloumn, ADC_Value, 3);
}
//Function to Initialize ADC
void adc_init()
{
ADCSRA = 0x00;
ADMUX = 0x20; //Vref=5V external --- ADLAR=1 --- MUX4:0 = 0000
ACSR = 0x80;
ADCSRA = 0x86; //ADEN=1 --- ADIE=1 --- ADPS2:0 = 1 1 0
}
void left_position_encoder_interrupt_init (void) //Interrupt 1 enable
{
cli(); //Clears the global interrupt
MCUCR = MCUCR | 0x08; // INT1 is set to trigger with falling edge
GICR = GICR | 0x80; // Enable Interrupt INT1 for left position encoder
sei(); // Enables the global interrupt
}
void right_position_encoder_interrupt_init (void) //Interrupt 0 enable
{
cli(); //Clears the global interrupt
MCUCR = MCUCR | 0x02; // INT0 is set to trigger with falling edge
GICR = GICR | 0x40; // Enable Interrupt INT5 for right position encoder
sei(); // Enables the global interrupt
}
//-------------------------------------------------------****TIMER INIT****------------------------------------------------
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xAA; //setup
TCNT1L = 0x00;
TCCR1A = 0x00;
TCCR1B = 0x0D; //start
}
void timer2_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x01;
OCR1AH = 0x00;
OCR1AL = 0xFF;
OCR1BH = 0x00;
OCR1BL = 0xFF;
ICR1H = 0x00;
ICR1L = 0xFF;
TCCR1A = 0xA1;
TCCR1B = 0x0D; //start Timer
}
void timer0_init(void)
{
TCCR0 |= (1 << CS02) | (1 << CS00); // Timer 0 prescaler 1024 , normal mode
TCNT0 = 0x6F; // Timer 0 initial vlaue
// TIMSK |= (1 << OCIE0) | (1 << TOIE0); // timer 0 compare match and overflow interrupt enable
}
//-------------------------------------------------------****BUZZER 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;
}
//-------------------------------------------------------****INIT DEVICES ****------------------------------------------------
void init_devices (void)
{
DDRA=0x00;
DDRB&=~_BV(4);
DDRD&=~_BV(6);
PORTA=0xFF;
PORTB|=_BV(4);
PORTD|=_BV(6);
cli(); //Clears the global interrupts
DDRA |= (1 << PA7); // making servo contol pins as output
DDRD |= (1 << PD7);
uart0_init();
port_init();
left_position_encoder_interrupt_init();
right_position_encoder_interrupt_init();
timer1_init();
// timer2_init();
TIMSK = 0x04; //timer1 interrupt sources
adc_init();
sei(); //Enables the global interrupts
}
void init_devices2 (void)
{
DDRA=0x00;
DDRB&=~_BV(4);
DDRD&=~_BV(6);
PORTA=0xFF;
PORTB|=_BV(4);
PORTD|=_BV(6);
cli(); //Clears the global interrupts
DDRA |= (1 << PA7); // making servo contol pins as output
DDRD |= (1 << PD7);
uart0_init();
port_init();
left_position_encoder_interrupt_init();
right_position_encoder_interrupt_init();
timer2_init();
adc_init();
sei(); //Enables the global interrupts
}
//-------------------------------------------------------****SENSOR VALUE READ FUNCTION****------------------------------------------------
void in_sens_val()
{
if (bit_is_set(PINA, 0))
s5 = 1;
else
s5 = 0;
if (bit_is_set(PINA, 1))
s4 = 1;
else
s4 = 0;
if (bit_is_set(PINA, 2))
s3 = 1;
else
s3 = 0;
if (bit_is_set(PIND, 6))
s2 = 1;
else
s2 = 0;
if (bit_is_set(PINB, 4))
s1 = 1;
else
s1 = 0;
}
//-------------------------------------------------------****LINE FOLLOWER FUNCTION****------------------------------------------------
void follow()
{
if((bit_is_set(PINA, 1)) && (bit_is_set(PIND, 6))) // if middle 3 on line go straight
{
velocity(VELOCITY_MAX,VELOCITY_MAX);
lcd_print (2,1,VELOCITY_MAX,3);
lcd_print (2,5,VELOCITY_MAX,3);
}
else
if((bit_is_clear(PINA, 1)) && (bit_is_set(PIND, 6)))
{
velocity(VELOCITY_MIN,VELOCITY_MAX);
lcd_print (2,1,VELOCITY_MIN,3);
lcd_print (2,5,VELOCITY_MAX,3);
}
else
if((bit_is_set(PINA, 1)) && (bit_is_clear(PIND, 6)))
{
velocity(VELOCITY_MAX,VELOCITY_MIN);
lcd_print (2,1,VELOCITY_MAX,3);
lcd_print (2,5,VELOCITY_MIN,3);
}
}
//-------------------------------------------------------****NODE AND ITS PROPERTIES****------------------------------------------------
int isPlus()
{
if(bit_is_set(PINA,0) && bit_is_set(PINB,4))
return 1;
else
return 0;
}
int isEnd()
{
if(bit_is_clear(PINA,1) && bit_is_clear(PIND,6) && bit_is_clear(PINA,2) && bit_is_clear(PINA,0) && bit_is_clear(PINB,4))
return 1;
else
return 0;
}
//-------------------------------------------------------****OBSTACLE DETECTION FUNCTION****------------------------------------------------
int isObstacle()
{
if(bit_is_clear(PINA,7))
return 1;
else
return 0;
}
int isBlock1()
{
if(bit_is_clear(PINA,1) && bit_is_clear(PIND,6) && bit_is_clear(PINA,2) && bit_is_set(PINA,0) && bit_is_set(PINB,4))
return 1;
else
return 0;
}
int isBlock2()
{
if((bit_is_clear(PINA,1) && bit_is_set(PIND,6) && bit_is_set(PINA,0) && bit_is_set(PINB,4)) ||
(bit_is_set(PINA,1) && bit_is_clear(PIND,6) && bit_is_set(PINA,0) && bit_is_set(PINB,4)) )
return 1;
else
return 0;
}
//-------------------------------------------------------****TIMER OVERFLOW INTERRUPT****------------------------------------------------
//This ISR can be used to toggle the buzzer input
ISR(TIMER1_OVF_vect)
{
if (toggel == 0)
{
USART_Transmit('$');
USART_Transmit('s');
if(counter == 0)
{
bot_id = 'A';
bot_id_set = 1;
toggel = 1;
}
if(counter == 1 && bot_id_set == 0)
{
bot_id = 'C';
bot_id_set =1;
toggel =1;
}
if(counter == 2 && bot_id_set == 0)
{
bot_id = 'B';
bot_id_set =1;
toggel = 1;
}
if(counter == 3 && bot_id_set == 0)
{
bot_id='D';
bot_id_set = 1;
toggel = 1;
}
return;
}
if(check_pos == 1)
{
to_begin = 1 ;
return;
}
}
//-------------------------------------------------------****GRIPPING FUNCTION****------------------------------------------------
void pickup(void)
{
S2 = 0x78;
_delay_ms(500);
}
void drop_down(void)
{
S2 = 0x73;
_delay_ms(500);
}
//-------------------------------------------------------****SHARP 90 TURN FUNCTION****------------------------------------------------
void turnLeft_done(void)
{
forward_mm(60);
left();
_delay_ms(350);
in_sens_val();
while(((bit_is_clear(PINA, 1)) && (bit_is_clear(PIND, 6))))
{
in_sens_val();
left();
}
PORTB = 0x06;
}
void turnRight_done()
{
forward_mm(60);
right();
_delay_ms(350);
in_sens_val();
while(((bit_is_clear(PINA, 1)) && (bit_is_clear(PIND, 6))))
{
in_sens_val();
right();
}
PORTB= 0x06;
}
void turnRight()
{
forward_mm(30);
right();
_delay_ms(350);
in_sens_val();
while(((bit_is_clear(PINA, 1)) && (bit_is_clear(PIND, 6))))
{
in_sens_val();
right();
}
PORTB = 0x06;
}
void turnRight1()
{
forward_mm(70);
right();
_delay_ms(350);
in_sens_val();
while(((bit_is_clear(PINA, 1)) && (bit_is_clear(PIND, 6))))
{
in_sens_val();
right();
}
PORTB = 0x06;
}
void turnLeft(void)
{
forward_mm(30);
left();
_delay_ms(350);
in_sens_val();
while(((bit_is_clear(PINA, 1)) && (bit_is_clear(PIND, 6))))
{
in_sens_val();
left();
}
PORTB = 0x06;
}
void turnLeft1(void)
{
left();
_delay_ms(350);
in_sens_val();
while(((bit_is_clear(PINA, 1)) && (bit_is_clear(PIND, 6))))
{
in_sens_val();
left();
}
PORTB = 0x06;
}
void turnBack()
{
right();
_delay_ms(700);
in_sens_val();
while(((bit_is_clear(PINA, 1)) && (bit_is_clear(PIND, 6))) )
{
in_sens_val();
right();
}
PORTB = 0x06;
}
//-------------------------------------------------------****CHANGE DIRECTION FUNCTION****------------------------------------------------
void change_direction_to(char dir)
{
if (bot_dir == N)
{
switch (dir)
{
case N:
break;
case S:
{
turnBack();
bot_dir = S;
break;
}
case E:
{
turnRight();
bot_dir = E;
break;
}
case W:
{
turnLeft();
bot_dir = W;
break;
}
}
}
else if (bot_dir == S)
{
switch (dir)
{
case N:
{
turnBack();
bot_dir = N;
break;
}
case S:
{
break;
}
case E:
{
turnLeft();
bot_dir = E;
break;
}
case W:
{
turnRight();
bot_dir = W;
break;
}
}
}
else if (bot_dir == W)
{
switch (dir)
{
case W:
break;
case E:
{
turnBack();
bot_dir = E;
break;
}
case N:
{
turnRight();
bot_dir = N;
break;
}
case S:
{
turnLeft();
bot_dir = S;
break;
}
}
}
else if (bot_dir == E)
{
switch (dir)
{
case E:
break;
case W:
{
turnBack();
bot_dir = W;
break;
}
case S:
{
turnRight();
bot_dir = S;
break;
}
case N:
{
turnLeft();
bot_dir = N;
break;
}
}
}
}
void change_dirn(unsigned char dir)
{
switch(dir)
{
case N: bot_dir = W; break;
case E: bot_dir = N; break;
case W:bot_dir = S; break;
case S:bot_dir = E; break;
}
}
//-------------------------------------------------------****BOT AVAILABILITY FUNCTION****------------------------------------------------
void alive(unsigned char check_bit)
{
switch(check_bit)
{
case 'A' : flag[0] = 1;
break;
case 'B' : flag[1] = 1;
break;
case 'C' : flag[2] = 1;
break;
case 'D' : flag[3] = 1;
break;
}
}
void update_node()
{
if(to_process ==1)
{
lcd_cursor(2,13);
lcd_string(store);
visited =atoi(store);
check_node[visited]=1;
to_process=0;
buzzer_off();
for(k=0;k<4;k++)
store[k]=0;
forward();
return;
}
}
//-------------------------------------------------------****COMMUNICATION INTERRUPT****------------------------------------------------
SIGNAL(SIG_UART_RECV)
{
rx1= rx2;
rx2= UDR;
if(toggel == 0)
{
if(rx1 == '$' && bot_id_set == 0)
{
counter++;
return;
}
}
if(to_begin == 1)
{
if(rx1 == '*')
{
switch(rx2)
{
case 'A': flag[0]++;break;
case 'B': flag[1]++;break;
case 'C': flag[2]++;break;
case 'D': flag[3]++;break;
}
}
else if(rx1 == '@')
{
get_start=1;
switch(rx2)
{
case 'q': cond=2;
break;
case 'w': cond=1;
path[31]=M;
break;
case 'e': switch(bot_dir)
{
case N : cond = 1;break;
case S : cond =2;path[31]=M;break;
case E : break;
case W : cond = 1 ;break;
}
break;
case 'r': cond=1;
break;
}