-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
303 lines (257 loc) · 10.7 KB
/
Controller.cpp
File metadata and controls
303 lines (257 loc) · 10.7 KB
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
#include <Arduino.h>
#include <EEPROM.h>
#include "Controller.h"
#define MIN_POWER 0
#define MAX_POWER 255
#define NUM_RULES 25
//#define DEBUG
Fuzzy *Controller::fuzzy = NULL;
FuzzyInput Controller::errorInput = FuzzyInput(1);
FuzzySet Controller::L_N = FuzzySet(-3, -3, -2.75, -0.5);
FuzzySet Controller::S_N = FuzzySet(-2.75, -0.5, -0.05, 0);
FuzzySet Controller::Z = FuzzySet(-0.05, 0, 0, 0.05);
FuzzySet Controller::S_P = FuzzySet(0, 0.05, 0.5, 2.75);
FuzzySet Controller::L_P = FuzzySet(0.5, 2.75, 3, 3);
FuzzyInput Controller::errorDeltaInput = FuzzyInput(2);
FuzzySet Controller::L_NA = FuzzySet(-0.05, -0.05, -0.0075, -0.0025);
FuzzySet Controller::S_NA = FuzzySet(-0.0075, -0.0025, -0.00125, 0);
FuzzySet Controller::Z_A = FuzzySet(-0.00125, 0, 0, 0.00125);
FuzzySet Controller::S_PA = FuzzySet(0, 0.00125, 0.0025, 0.0075);
FuzzySet Controller::L_PA = FuzzySet(0.0025, 0.0075, 0.05, 0.05);
FuzzyOutput Controller::adjust = FuzzyOutput(1);
FuzzySet Controller::VL_D = FuzzySet(-34, -32, -32, -30);
FuzzySet Controller::L_D = FuzzySet(-18, -16, -16, -14);
FuzzySet Controller::M_D = FuzzySet(-6, -4, -4, -2);
FuzzySet Controller::S_D = FuzzySet(-4, -2, -2, 0);
FuzzySet Controller::K = FuzzySet(-1,0,0,1);
FuzzySet Controller::S_I = FuzzySet(0, 2, 2, 4);
FuzzySet Controller::M_I = FuzzySet(2, 4, 4, 6);
FuzzySet Controller::L_I = FuzzySet(14, 16, 16, 18);
FuzzySet Controller::VL_I = FuzzySet(30, 32, 32, 34);
Controller::Controller (float *sensor, uint8_t pinNumber) {
int i = 1;
_sensor = sensor;
pin = pinNumber;
power = (MAX_POWER - MIN_POWER) / 2;
target = 0;
lastError = *_sensor - target;
lastTime = millis();
modified = false;
// Serial.begin(9600);
if (fuzzy == NULL) {
fuzzy = new Fuzzy();
// Serial.println((unsigned long int)errorInput.fuzzySets);
// Serial.println((unsigned long int)errorInput.fuzzySetsCursor);
errorInput.addFuzzySet(&L_N);
// Serial.println((unsigned long int)errorInput.fuzzySets);
// Serial.println((unsigned long int)errorInput.fuzzySetsCursor);
errorInput.addFuzzySet(&S_N);
// Serial.println((unsigned long int)errorInput.fuzzySets);
// Serial.println((unsigned long int)errorInput.fuzzySetsCursor);
errorInput.addFuzzySet(&Z);
// Serial.println((unsigned long int)errorInput.fuzzySets);
// Serial.println((unsigned long int)errorInput.fuzzySetsCursor);
errorInput.addFuzzySet(&S_P);
// Serial.println((unsigned long int)errorInput.fuzzySets);
// Serial.println((unsigned long int)errorInput.fuzzySetsCursor);
errorInput.addFuzzySet(&L_P);
// Serial.println((unsigned long int)errorInput.fuzzySets);
// Serial.println((unsigned long int)errorInput.fuzzySetsCursor);
fuzzy->addFuzzyInput(&errorInput);
errorDeltaInput.addFuzzySet(&L_NA);
errorDeltaInput.addFuzzySet(&S_NA);
errorDeltaInput.addFuzzySet(&Z_A);
errorDeltaInput.addFuzzySet(&S_PA);
errorDeltaInput.addFuzzySet(&L_PA);
fuzzy->addFuzzyInput(&errorDeltaInput);
adjust.addFuzzySet(&VL_D);
adjust.addFuzzySet(&L_D);
adjust.addFuzzySet(&M_D);
adjust.addFuzzySet(&S_D);
adjust.addFuzzySet(&K);
adjust.addFuzzySet(&S_I);
adjust.addFuzzySet(&M_I);
adjust.addFuzzySet(&L_I);
adjust.addFuzzySet(&VL_I);
fuzzy->addFuzzyOutput(&adjust);
FuzzyRuleAntecedent* ifLargeNegative = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallNegativeAndQuicklyDecreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallNegativeAndSlowlyDecreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallNegativeAndNotMoving = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallNegativeAndSlowlyIncreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallNegativeAndQuicklyIncreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifZeroAndQuicklyDecreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifZeroAndSlowlyDecreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifZeroAndNotMoving = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifZeroAndSlowlyIncreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifZeroAndQuicklyIncreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallPositiveAndSlowlyIncreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallPositiveAndQuicklyIncreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallPositiveAndNotMoving = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallPositiveAndSlowlyDecreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifSmallPositiveAndQuicklyDecreasing = new FuzzyRuleAntecedent();
FuzzyRuleAntecedent* ifLargePositive = new FuzzyRuleAntecedent();
ifLargeNegative->joinSingle(&L_N);
ifSmallNegativeAndQuicklyDecreasing->joinWithAND(&S_N,&L_NA);
ifSmallNegativeAndSlowlyDecreasing->joinWithAND(&S_N,&S_NA);
ifSmallNegativeAndNotMoving->joinWithAND(&S_N,&Z_A);
ifSmallNegativeAndSlowlyIncreasing->joinWithAND(&S_N, &S_PA);
ifSmallNegativeAndQuicklyIncreasing->joinWithAND(&S_N, &L_PA);
ifZeroAndQuicklyDecreasing->joinWithAND(&Z, &L_NA);
ifZeroAndSlowlyDecreasing->joinWithAND(&Z, &S_NA);
ifZeroAndNotMoving->joinWithAND(&Z, &Z_A);
ifZeroAndSlowlyIncreasing->joinWithAND(&Z, &S_PA);
ifZeroAndQuicklyIncreasing->joinWithAND(&Z, &L_PA);
ifSmallPositiveAndSlowlyIncreasing->joinWithAND(&S_P,&S_PA);
ifSmallPositiveAndQuicklyIncreasing->joinWithAND(&S_P,&L_PA);
ifSmallPositiveAndNotMoving->joinWithAND(&S_P,&Z_A);
ifSmallPositiveAndSlowlyDecreasing->joinWithAND(&S_P, &S_NA);
ifSmallPositiveAndQuicklyDecreasing->joinWithAND(&S_P, &L_NA);
ifLargePositive->joinSingle(&L_P);
FuzzyRuleConsequent* VeryLargeDecrease = new FuzzyRuleConsequent();
FuzzyRuleConsequent* LargeDecrease = new FuzzyRuleConsequent();
FuzzyRuleConsequent* MediumDecrease = new FuzzyRuleConsequent();
FuzzyRuleConsequent* SmallDecrease = new FuzzyRuleConsequent();
FuzzyRuleConsequent* Keep = new FuzzyRuleConsequent();
FuzzyRuleConsequent* SmallIncrease = new FuzzyRuleConsequent();
FuzzyRuleConsequent* MediumIncrease = new FuzzyRuleConsequent();
FuzzyRuleConsequent* LargeIncrease = new FuzzyRuleConsequent();
FuzzyRuleConsequent* VeryLargeIncrease = new FuzzyRuleConsequent();
VeryLargeDecrease->addOutput(&VL_D);
LargeDecrease->addOutput(&L_D);
MediumDecrease->addOutput(&M_D);
SmallDecrease->addOutput(&S_D);
Keep->addOutput(&K);
SmallIncrease->addOutput(&S_I);
MediumIncrease->addOutput(&M_I);
LargeIncrease->addOutput(&L_I);
VeryLargeIncrease->addOutput(&VL_I);
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifLargeNegative, VeryLargeIncrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallNegativeAndQuicklyDecreasing, LargeIncrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallNegativeAndSlowlyDecreasing, MediumIncrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallNegativeAndNotMoving, SmallIncrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallNegativeAndSlowlyIncreasing, Keep));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallNegativeAndQuicklyIncreasing, SmallDecrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifZeroAndQuicklyDecreasing, MediumIncrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifZeroAndSlowlyDecreasing, SmallIncrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifZeroAndNotMoving, Keep));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifZeroAndSlowlyIncreasing, SmallDecrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifZeroAndQuicklyIncreasing, MediumDecrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallPositiveAndQuicklyDecreasing, SmallIncrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallPositiveAndSlowlyDecreasing, Keep));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallPositiveAndNotMoving, SmallDecrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallPositiveAndSlowlyIncreasing, MediumDecrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifSmallPositiveAndQuicklyIncreasing, LargeDecrease));
fuzzy->addFuzzyRule(new FuzzyRule(i++, ifLargePositive, VeryLargeDecrease));
}
}
void Controller::setTarget (float targetValue) {
target = targetValue;
modified = true;
}
void Controller::setMaxError (float max) {
maxError = max;
}
float Controller::getTarget (void) {
return target;
}
uint8_t Controller::getPower (void) {
return power;
}
float* Controller::getSensor(void) {
return _sensor;
}
void Controller::control (void) {
float error;
float delta;
float throttle;
float elapsedSeconds;
error = *_sensor - target;
elapsedSeconds = (float)(millis() - lastTime) /1000;
delta = (error - lastError)/elapsedSeconds; // TODO: check roll over
lastTime = millis();
lastError = error;
fuzzy->setInput(1, error);
fuzzy->setInput(2, delta);
fuzzy->fuzzify();
#ifdef DEBUG
Serial.println(error,3);
Serial.println(delta,3);
Serial.print(L_N.getPertinence());
Serial.print(' ');
Serial.print(S_N.getPertinence());
Serial.print(' ');
Serial.print(Z.getPertinence());
Serial.print(' ');
Serial.print(S_P.getPertinence());
Serial.print(' ');
Serial.println(L_P.getPertinence());
Serial.print(L_NA.getPertinence());
Serial.print(' ');
Serial.print(S_NA.getPertinence());
Serial.print(' ');
Serial.print(Z_A.getPertinence());
Serial.print(' ');
Serial.print(S_PA.getPertinence());
Serial.print(' ');
Serial.println(L_PA.getPertinence());
for (i=1; i<=NUM_RULES; i++) {
Serial.print(fuzzy->isFiredRule(i));
if (i%5) {
Serial.print(' ');
} else {
Serial.println();
}
}
Serial.println();
Serial.print(VL_D.getPertinence());
Serial.print(' ');
Serial.print(L_D.getPertinence());
Serial.print(' ');
Serial.print(M_D.getPertinence());
Serial.print(' ');
Serial.print(S_D.getPertinence());
Serial.print(' ');
Serial.print(K.getPertinence());
Serial.print(' ');
Serial.print(S_I.getPertinence());
Serial.print(' ');
Serial.print(M_I.getPertinence());
Serial.print(' ');
Serial.print(L_I.getPertinence());
Serial.print(' ');
Serial.println(VL_I.getPertinence());
#endif
throttle = fuzzy->defuzzify(1);
#ifdef DEBUG
Serial.println(throttle);
#endif
if (throttle > 0)
throttle += 0.5; //to round it
else if (throttle < 0)
throttle -= 0.5;
power = (uint8_t)constrain(power +
(int)throttle, MIN_POWER, MAX_POWER);
analogWrite (pin, power);
}
void Controller::save(int address) {
unsigned int i;
byte *b = (byte *)⌖
if (modified) {
for (i=0; i<sizeof(float); i++) {
EEPROM.write(address+i, *b++);
}
}
modified = false;
}
void Controller::restore(int address) {
unsigned int i;
byte *b = (byte *)⌖
for (i=0; i<sizeof(float); i++) {
*b++ = EEPROM.read(address+i);
}
modified = false;
}
bool Controller::getStatus() {
return fabs(*_sensor - target) <= maxError;
}