-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlineFollowerCondensed.c
663 lines (509 loc) · 14.5 KB
/
lineFollowerCondensed.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
/* ********************************************
Line Follower 2013 | Condensed Source Code File
******************************************** */
// Library Header Files
#include <avr/io.h>
#include <util/delay.h>
#include <math.h>
#include <stdlib.h>
/* ****************************
Header Files for Line Follower.
**************************** */
// Main Header File for Line Follower
#define TRUE 1
#define FALSE 0
#define HIGH 1
#define LOW 0
#define TOTALSENSORS 7
void initializeAll(int sensorArray[TOTALSENSORS]);
void inputSensorArray(int sensorArray[TOTALSENSORS]);
int isRunningFeasible(void);
void normalizeSensorArray(int sensorArray[TOTALSENSORS]);
void moveLineFollower(int sensorArray[TOTALSENSORS]);
void initializeADC(void);
void initializePWM(void);
// Header File for ADC Conversion of Microcontroller Input
#define THRESH 512
void initializeADC(void);
unsigned int readADC(unsigned char channel);
// Header File for Normalization of Sensor Array Algorithm
int isArrayEmpty(int sensorArray[TOTALSENSORS]);
int isArrayFull(int sensorArray[TOTALSENSORS]);
// Header For Line Follower Motion Algorithm
#define Kp 1.5
#define Ki 0.75
#define Kd 0.6
#define TOP 0x00FF
#define MAX 255
#define BOTTOM 0
#define VOLTAGESOURCE 11
#define MOTORSCALEDOWN 1.4
#define IDEALERROR 0
void getMotorVoltage(int sensorArray[TOTALSENSORS], double motorVoltage[2]);
void getOCRValue(int OCRValue[2], double motorVoltage[2]);
double getPIDControllerValue(int sensorArray[TOTALSENSORS]);
int getError(int sensorArray[TOTALSENSORS]);
double getProportional(int error);
double getIntegral(int error);
double getDerivate(int error);
double getControlValue(double P, double I, double D);
double normalizePIDControllerValue(double PIDControllerValue);
void driveMotors(double motorVoltage[2]);
void startPWM(int OCRValue[2]);
/* ***********************************
Source Code For Functions Begins Here.
*********************************** */
int main()
{
int sensorArray[TOTALSENSORS];
initializeAll(sensorArray);
while(TRUE)
{
// For Clean Simulator
//system("clear"); // Clears Screen | For Unix
inputSensorArray(sensorArray);
if(isRunningFeasible())
{
normalizeSensorArray(sensorArray);
moveLineFollower(sensorArray);
}
else
break;
_delay_ms(100); // Gives a Delay of 100 milliseconds between 2 successive Iterations to reduce stress on Microcontroller and let Bot run PWM sucessfully.
// For Clean Simulator
//usleep(500000); // Adds a Delay | Argument in Microseconds
}
return 0;
}
void initializeAll(int sensorArray[TOTALSENSORS])
{
int i;
// Initialize Sensor Array. Make Bot Think it doesn't detect any line.
for(i = 0 ; i < TOTALSENSORS ; i++)
sensorArray[i] = HIGH;
initializeADC();
initializePWM();
// Assigns PORTD Pins as Output Pins
DDRD = 0xFF;
// Assigns PORTA Pins as Input Pins
DDRA = 0x00;
// Initializes PORTD
PORTD = 0x00;
PORTD |= 0x08;
PORTD |= 0x02;
}
void inputSensorArray(int sensorArray[TOTALSENSORS])
{
// LINE is to the LEFT of Bot
/*
sensorArray[0] = LOW;
sensorArray[1] = LOW;
sensorArray[2] = HIGH;
sensorArray[3] = HIGH;
sensorArray[4] = HIGH;
sensorArray[5] = HIGH;
sensorArray[6] = HIGH;
sensorArray[7] = HIGH;
*/
// LINE is to the RIGHT of Bot
/*
sensorArray[0] = HIGH;
sensorArray[1] = HIGH;
sensorArray[2] = HIGH;
sensorArray[3] = HIGH;
sensorArray[4] = HIGH;
sensorArray[5] = HIGH;
sensorArray[6] = LOW;
sensorArray[7] = LOW;
*/
// Only LEFT for some time. Then, only RIGHT for some time and back
/*
static int phase = 0;
static int counter = 0;
if(phase == 0)
{
sensorArray[0] = LOW;
sensorArray[1] = LOW;
sensorArray[2] = HIGH;
sensorArray[3] = HIGH;
sensorArray[4] = HIGH;
sensorArray[5] = HIGH;
sensorArray[6] = HIGH;
sensorArray[7] = HIGH;
counter++;
}
else if(phase == 1)
{
sensorArray[0] = HIGH;
sensorArray[1] = HIGH;
sensorArray[2] = HIGH;
sensorArray[3] = HIGH;
sensorArray[4] = HIGH;
sensorArray[5] = HIGH;
sensorArray[6] = LOW;
sensorArray[7] = LOW;
counter++;
}
if(counter >= 1000)
{
phase = (phase + 1) % 2;
counter = 0;
}
*/
// Sinusoidal Track
/*
int i;
static int current = 0, phase = 1, counter = 0;
for(i = 0 ; i < TOTALSENSORS ; i++)
sensorArray[i] = HIGH;
if(phase == 1)
{
if(current < TOTALSENSORS)
{
sensorArray[current] = LOW;
if(counter >= 63)
{
current++;
counter = 0;
}
counter++;
}
else
{
phase = 2;
current--;
counter = 63;
}
}
if(phase == 2)
{
if(current == 0)
{
phase = 1;
}
if(current > 0)
{
if(counter >= 63)
{
current--;
counter = 0;
}
counter++;
sensorArray[current] = LOW;
}
}
*/
// To Test Reaction when No Line is Found
/*
int i;
for(i = 0 ; i < TOTALSENSORS ; i++)
sensorArray[i] = LOW;
*/
// Reading Input From ADC
/*
int i;
for(i = 0 ; i < TOTALSENSORS ; i++)
{
if(readADC(i) >= THRESH)
sensorArray[i] = HIGH;
else if(readADC(i) < THRESH)
sensorArray[i] = LOW;
}
*/
// Directly Get Input from Array. Use ONLY if Comparator is used.
int pinAStatus = PINA;
int i;
for(i = 0 ; i < TOTALSENSORS ; i++)
sensorArray[i] = (pinAStatus >> (i)) & ~(~0 << 1);
// Print Sensor Array
/*
printf("SENSOR ARRAY: ");
for(i = 0 ; i < TOTALSENSORS ; i++)
printf("%d ", sensorArray[i]);
printf("| ");
*/
}
// Function to Initialize ADC
void initializeADC(void)
{
/*
// AVcc with external capacitor at AREF
ADMUX = (1<<REFS0);
// Enable ADC and set Prescaler division factor as 16
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0);
*/
}
// Function to Read ADC Value for a Channel
unsigned int readADC(unsigned char channel)
{
/*
// channel must be b/w 0 to (TOTALSENSORS - 1)
channel = channel & 0b00000110;
// selecting channel
ADMUX |= channel;
// start conversion
ADCSRA |= (1<<ADSC);
// waiting for ADIF, conversion complete
while(!(ADCSRA & (1<<ADIF)));
// clearing of ADIF, it is done by writing 1 to it
ADCSRA |= (1<<ADIF);
return (ADC);
*/
}
// Check if Running Bot is Feasible. If Yes, Return TRUE. Else, return FALSE
int isRunningFeasible(void)
{
return TRUE;
}
void normalizeSensorArray(int sensorArray[TOTALSENSORS])
{
// Stores Previous Line Signal. If New Line Signal is Empty, then restore previous Line Signal as current one.
static int previousSensorArray[TOTALSENSORS] = {0, 0, 0, 1, 0, 0, 0};
int i;
if(isArrayEmpty(sensorArray)) // Check if Robot DOES NOT detect Line. If TRUE, restore previous known Line Position.
{
for(i = 0 ; i < TOTALSENSORS ; i++)
{
sensorArray[i] = previousSensorArray[i];
}
}
else if(isArrayFull(sensorArray)) // Check if Robot detects a line throughout. If TRUE, restore previous known Line Position.
{
for(i = 0 ; i < TOTALSENSORS ; i++)
{
sensorArray[i] = previousSensorArray[i];
}
}
else // If Line is distinguished properly, Remember it.
{
for(i = 0 ; i < TOTALSENSORS ; i++)
{
previousSensorArray[i] = sensorArray[i];
}
}
}
int isArrayEmpty(int sensorArray[TOTALSENSORS])
{
int i;
for(i = 0 ; i < TOTALSENSORS ; i++)
{
if(sensorArray[i] == LOW)
return FALSE;
}
return TRUE;
}
int isArrayFull(int sensorArray[TOTALSENSORS])
{
int i;
for(i = 0 ; i < TOTALSENSORS ; i++)
{
if(sensorArray[i] == HIGH)
return FALSE;
}
return TRUE;
}
void moveLineFollower(int sensorArray[TOTALSENSORS])
{
int OCRValue[2];
double motorVoltage[2];
getMotorVoltage(sensorArray, motorVoltage); // Get Voltages to be Fed to Motors
getOCRValue(OCRValue, motorVoltage); // Get OCR Values for PWM of Motors
// To Test OCR Value Output
//printf("OCR Value Left = %3d | OCR Value Right = %3d | ", OCRValue[0], OCRValue[1]);
//printf("Motor Voltage Left = %3lf | Motor Voltage Right = %3lf\n", motorVoltage[0], motorVoltage[1]);
driveMotors(motorVoltage); // Get Motors to Run in Correct Direction as Calculated in motorVoltage.
startPWM(OCRValue); // Get Motors to Run with Calculated Motor Voltages.
}
// Gets PID Controller Value. PID Controller Value has 3 Parts (Proportional, Integral and Derivate). Returns The Controller Value.
double getPIDControllerValue(int sensorArray[TOTALSENSORS])
{
int error;
double P, I, D, controlValue;
error = getError(sensorArray);
P = getProportional(error);
I = getIntegral(error);
D = getDerivate(error);
controlValue = getControlValue(P, I, D);
return controlValue;
}
// Gets Error/Deviation of Bot from Ideal Position. Uses Weigths on different sensors to calculate Error/Deviation.
// Works only for ODD Number of Sensors eg: 7.
int getError(int sensorArray[TOTALSENSORS])
{
int i, error;
for(i = 0, error = 0 ; i < TOTALSENSORS ; i++)
{
if(sensorArray[i] == LOW)
error += i - ((int)(TOTALSENSORS / 2)) - IDEALERROR;
else
continue;
}
return error;
}
double getProportional(int error)
{
return (Kp * error);
}
double getIntegral(int error)
{
static double integral = 0;
static int previousError = 0;
// Nullifies the Integral Part of PID when the accumulated Integral is no longer needed. Else, calculates Integral.
if (error == 0 || (integral / abs(integral)) != (previousError / abs(previousError)))
{
previousError = error;
integral = 0;
return integral;
}
else
{
integral = Ki * (integral + error);
previousError = error;
}
return integral;
}
double getDerivate(int error)
{
static int previousError = 0;
double derivate;
derivate = (error - previousError);
derivate *= Kd;
previousError = error;
return derivate;
}
double getControlValue(double P, double I, double D)
{
double controlValue;
controlValue = P + I + D;
return controlValue;
}
// Normalizes PID Controller Value to lie within a Specified Range.
// Removes Error due to Abnormally Large PID Controller Values.
double normalizePIDControllerValue(double PIDControllerValue)
{
// Formula to Normalize PID Controller Value to approximately lie between -(2 x VOLTAGESOURCE) and (2 x VOLTAGESOURCE)
PIDControllerValue = (PIDControllerValue / ((3 * Kp) + Kd)) * (2 * VOLTAGESOURCE);
return PIDControllerValue;
}
void getMotorVoltage(int sensorArray[TOTALSENSORS], double motorVoltage[2])
{
double PIDControllerValue;
PIDControllerValue = getPIDControllerValue(sensorArray);
PIDControllerValue = normalizePIDControllerValue(PIDControllerValue);
// Assigns Motor Voltages based on PID Controller Value.
if(PIDControllerValue > 0) // If PIDControllerValue is +ve, Line should be LEFT of Bot.
{
motorVoltage[0] = VOLTAGESOURCE;
motorVoltage[1] = VOLTAGESOURCE - PIDControllerValue;
}
else if(PIDControllerValue < 0) // If PIDControllerValue is -ve, Line should be RIGHT of Bot.
{
motorVoltage[0] = VOLTAGESOURCE + PIDControllerValue;
motorVoltage[1] = VOLTAGESOURCE;
}
else // If PIDControllerValue is 0, LINE is at IDEALPOSITION.
{
motorVoltage[0] = VOLTAGESOURCE;
motorVoltage[1] = VOLTAGESOURCE;
}
// If No Error. Run Bot Straight Ahead at FULL SPEED.
/*if(!getError(sensorArray))
{
motorVoltage[0] = VOLTAGESOURCE;
motorVoltage[1] = VOLTAGESOURCE;
}*/
// Special Case for 90 Degrees Turn.
if(sensorArray[0] == LOW && sensorArray[1] == LOW) // If 90 Degree Turn is to LEFT of Bot.
{
motorVoltage[0] = -VOLTAGESOURCE;
motorVoltage[1] = VOLTAGESOURCE;
}
else if(sensorArray[5] == LOW && sensorArray[6] == LOW) // If 90 Degree Turn is to RIGHT of Bot.
{
motorVoltage[0] = VOLTAGESOURCE;
motorVoltage[1] = -VOLTAGESOURCE;
}
// To Test ADC. Motor Stops on Corresponding Side on Sensor Array Finding Line.
/*
if(sensorArray[0] == LOW || sensorArray[1] == LOW || sensorArray[2] == LOW)
motorVoltage[0] = 0;
if(sensorArray[3] == LOW)
{
motorVoltage[0] = 0;
motorVoltage[1] = 0;
}
if(sensorArray[4] == LOW || sensorArray[5] == LOW || sensorArray[6] == LOW)
motorVoltage[1] = 0;
*/
// If Calculated Motor Voltage is Out of Range, Make it Maximum Possbile while retaining Direction of Motor Motion.
if(motorVoltage[0] > VOLTAGESOURCE || motorVoltage[0] < -VOLTAGESOURCE)
motorVoltage[0] = (motorVoltage[0] / fabs(motorVoltage[0])) * VOLTAGESOURCE;
if(motorVoltage[1] > VOLTAGESOURCE || motorVoltage[1] < -VOLTAGESOURCE)
motorVoltage[1] = (motorVoltage[1] / fabs(motorVoltage[1])) * VOLTAGESOURCE;
// Scale Down Motor Voltages by dividing with a factor of MOTORSCALEDOWN
motorVoltage[0] /= MOTORSCALEDOWN;
motorVoltage[1] /= MOTORSCALEDOWN;
}
void getOCRValue(int OCRValue[2], double motorVoltage[2])
{
double dutyCycle;
int i;
// Calculate OCRValues for PWM for both Motors based on their calculated Motor Voltages
for(i = 0 ; i < 2 ; i++)
{
dutyCycle = motorVoltage[i] / VOLTAGESOURCE; // dutyCycle is ON_TIME by (ON_TIME + OFF_TIME) of Motor.
OCRValue[i] = dutyCycle * TOP;
OCRValue[i] = abs(OCRValue[i]);
}
// If Calculated OCR Value is Out of Range, Make it Maximum Possbile.
if(OCRValue[0] > TOP)
OCRValue[0] = TOP;
if(OCRValue[1] > TOP)
OCRValue[1] = TOP;
}
// Drives Motors by Assigning Bits to PORTD
void driveMotors(double motorVoltage[2])
{
if(motorVoltage[0] > 0) // PORTD Configuration for Left Motor FORWARD
{
PORTD &= 0xFB;
PORTD |= 0x08;
}
else if(motorVoltage[0] < 0) // PORTD Configuration for Left Motor BACKWARD
{
PORTD &= 0xF7;
PORTD |= 0x04;
}
else // PORTD Configuration for Left Motor STOP
PORTD &= 0xF3;
if(motorVoltage[1] > 0) // PORTD Configuration for Right Motor FORWARD
{
PORTD &= 0xFE;
PORTD |= 0x02;
}
else if(motorVoltage[1] < 0) // PORTD Configuration for Right Motor BACKWAWRD
{
PORTD &= 0xFD;
PORTD |= 0x01;
}
else // PORTD Configuration for Right Motor STOP
PORTD &= 0xFC;
}
// Initializes PWM for use
void initializePWM(void)
{
ICR1 = TOP; // TOP Number to which Counter Counts.
DDRD |= 1<<PD5; // PD5 Pin of PORTD is now Enabler Pin. Does PWM.
DDRD |= 1<<PD4; // PD4 Pin of PORTD is now Enabler Pin. Does PWM.
TCCR1A |= 1<<COM1A1; // Sets Compare Output Mode for Phase Correct PWM.
TCCR1A |= 1<<WGM11; // Assigning Bits 1 to WGM11 and WGM13 makes PWM Mode as PWM Phase Correct.
TCCR1B |= 1<<WGM13;
TCCR1A |= 1<<COM1B1; // Sets Compare Output Mode for Phase Correct PWM.
TCCR1B |= 1<<CS10; // We use Internal Clock. Assigning 1 to CS10 Bit does not Prescale this Clock.
}
// Assigns OCR Values to PWM Output Pins to start PWM Generation
void startPWM(int OCRValue[2])
{
OCR1B = OCRValue[0]; // Assigns OCR Value to Output Compare Register of Left Motor.
OCR1A = OCRValue[1]; // Assigns OCR Value to Output Compare Register of Right Motor.
}