-
Notifications
You must be signed in to change notification settings - Fork 2
/
st.m
121 lines (107 loc) · 3.14 KB
/
st.m
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
function st(d)
d = 10;
mylego = legoev3('bt','00165344463c');
%mylego = legoev3('USB');
m1 = motor(mylego, 'A');
m2 = motor(mylego, 'B');
SPEED1 = 25;
SPEED2 = 25;
wd = 5.5;
m1.Speed = SPEED1;
m2.Speed = SPEED2;
resetRotation(m1);
resetRotation(m2);
a1 = readRotation(m1);
a2 = readRotation(m2);
rotd = ((d-5)/(pi*wd))*360;
Kp = 0.8;
Ki = 0;
Kd = 0.01;
lastR1=0; lastR2=0;
lasterror = 0;
integral = 0;
%gyro = gyroSensor(mylego);
%resetRotationAngle(gyro);
while (a1<rotd && a2<rotd)
a1 = readRotation(m1)
a2 = readRotation(m2);
%oangle = readRotationAngle(gyro);
SPEED1 = 20;
SPEED2 = 25;
m1.Speed = SPEED1;
m2.Speed = SPEED2;
start(m1);
start(m2);
SPEED1 = 20;
SPEED2 = 20;
m1.Speed = SPEED1;
m2.Speed = SPEED2;
% if (oangle>0)
% SPEED2 = SPEED2 + 1;
% m2.Speed = SPEED2;
% elseif (oangle<0)
% SPEED1 = SPEED1 + 1;
% m1.Speed = SPEED1;
% else
% continue
%end
%PID Calculations
r1 = readRotation(m1);
r2 = readRotation(m2);
sp1 = (r1 - lastR1)/0.1;
sp2 = (r2 - lastR2)/0.1;
if (sp1>sp2)
error = sp1 - sp2;
else
error = sp2 - sp1;
end
integral = integral + (error*0.1);
derivative = (error - lasterror)/0.1;
pid = (Kp * error) + (Ki * integral) + (Kd * derivative);
if (error>0)
SPEED2 = SPEED2 + pid;
SPEED1 = SPEED1 - pid;
if (SPEED2<0)
SPEED2 = SPEED2 * (-1);
elseif (SPEED1<0)
SPEED1 = SPEED1 * (-1);
else
continue
end
%if (SPEED1>SPEED2)
% diff = SPEED1-SPEED2;
% SPEED2 = SPEED2 + diff;
% elseif (SPEED2>SPEED1)
% diff = SPEED2 - SPEED1;
% SPEED1 = SPEED1 + diff;
%end
else
SPEED1 = SPEED1 + pid;
SPEED2 = SPEED2 - pid;
if (SPEED2<0)
SPEED2 = SPEED2 * (-1);
elseif (SPEED1<0)
SPEED1 = SPEED1 * (-1);
else
continue
end
end
% if (SPEED1>SPEED2)
% diff = SPEED1-SPEED2;
% SPEED2 = SPEED2 + diff;
%elseif (SPEED2>SPEED1)
% diff = SPEED2 - SPEED1;
% SPEED1 = SPEED1 + diff;
%end
m1.Speed = SPEED1;
m2.Speed = SPEED2;
lastR1 = r1;
lastR2 = r2;
lasterror = error;
a1 = readRotation(m1);
a2 = readRotation(m2);
end
stop(m1);
stop(m2);
clear all;
end