-
Notifications
You must be signed in to change notification settings - Fork 0
/
proportional.c
80 lines (77 loc) · 1.33 KB
/
proportional.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
/////////////////////////////////////////////////////////////////
//This code is logically not correct but runs correctly. why??????
/////////////////////////////////////////////////////////////////
int[]sens = new int[8];
int cte =0;
int lastcte =0;
int totcte=0;
int power =0;
double kp=14.2857, ki=0,kd=0;
void setup()
{
}
void loop()
{
readSens();
calcCte();
lcd("%d",cte);
power = (int)(kp*(double)cte + ki*(double)(totcte) + kd*(double)(cte - lastcte));
if(power>0)
{
motor(1,100+power);
motor(2,-power);
}
else if(power<0)
{
motor(1,power);
motor(2,100-power);
}
else
{
motor(1,100);
motor(2,100);
}
lastcte = cte;
totcte +=cte;
lcd(" ");
}
void calcCte()
{
int lcte =0, rcte=0;
int i=0;
while(sens[i] != 0)
{
lcte = lcte+sens[i];
i++;
if(i==8)
{
break;
}
}
i=7;
while(sens[i] != 0)
{
rcte = rcte+sens[i];
i--;
if(i==-1)
{
break;
}
}
cte =(int)((lcte-rcte));
}
void readSens()
{
for(int i=0; i<8;i++)
{
int res = analog(i);
if(res>=512 && res<=1024)
{
sens[i] = 1;
}
else
{
sens[i] = 0;
}
}
}