forked from utkarsh2211/grid-solving-line-follower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_follow.ino
68 lines (57 loc) · 1.23 KB
/
line_follow.ino
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
void sensor_read()
{
digitalWrite(13,HIGH);
s1= analogRead(A0);
s2= analogRead(A1);
s3= analogRead(A2);
s4= analogRead(A3);
s5= analogRead(A4);
s6= analogRead(A5);
s7= analogRead(A6);
if(s1 > 250) s1 = 1; else s1 = 0;
if(s2 > 250) s2 = 1; else s2 = 0;
if(s3 > 250) s3 = 1; else s3 = 0;
if(s4 > 250) s4 = 1; else s4 = 0;
if(s5 > 250) s5 = 1; else s5 = 0;
if(s6 > 250) s6 = 1; else s6 = 0;
if(s7 > 250) s7 = 1; else s7 = 0;
digitalWrite(13,LOW);
Serial.println("Start");
Serial.println(s1);
Serial.println(s2);
Serial.println(s3);
Serial.println(s4);
Serial.println(s5);
Serial.println(s6);
Serial.println(s7);
}
void calc_pwm()
{
preErr = err;
wsum = (-6)*s7 + (-4)*s6 + (-2)*s5 + 2*s3 + 4*s2 + 6*s1;
sum = s1+s2+s3+s4+s5+s6+s7;
err = wsum/sum;
if(sum == 0) err = 0;
derr = err - preErr;
sum_err += err;
}
void motor_drive()
{
corr = kp*err + kd*derr + ki*sum_err;
v1 = v + corr;
v2 = v - corr;
constrain(v1,0,255);
constrain(v2,0,255);
digitalWrite(m11,HIGH);
digitalWrite(m12,LOW);
digitalWrite(m21,HIGH);
digitalWrite(m22,LOW);
analogWrite(pwm1, v1);
analogWrite(pwm2, v2);
}
void line_follow()
{
sensor_read();
calc_pwm();
motor_drive();
}