-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathfuzzy.js
126 lines (120 loc) · 3.56 KB
/
fuzzy.js
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
var fuzzyRun = false;
function fuzzyStart(parameter) {
switch (parameter) {
case 'start':
fuzzyRun = true;
break;
case 'stop':
fuzzyRun = false;
break;
default:
fuzzyRun = !fuzzyRun;
}
if (fuzzyRun) {
document.getElementById('fuzzyStart').innerHTML = 'Stop (F)';
geneticStart('stop');
psoStart('stop');
startMotion('start');
} else {
document.getElementById('fuzzyStart').innerHTML = 'Start (F)';
startMotion('stop');
}
}
function fuzzyControl() {
var alpha = [];
// Center Small, Right Medium, Right Large, Left Medium, Left Large, Center Large & Right Small, Center Large & Left Small
var functionValue = [60, 40, 80, -40, -80, 30, -30];
alpha.push(fuzzyRulesCenter(distanceCenter, 'Small'));
alpha.push(fuzzyRulesRight(distanceRight, 'Medium'));
alpha.push(fuzzyRulesRight(distanceRight, 'Large'));
alpha.push(fuzzyRulesLeft(distanceLeft, 'Medium'));
alpha.push(fuzzyRulesLeft(distanceLeft, 'Large'));
alpha.push(fuzzyRulesCenter(distanceCenter, 'Large') * fuzzyRulesRight(distanceRight, 'Small'));
alpha.push(fuzzyRulesCenter(distanceCenter, 'Large') * fuzzyRulesLeft(distanceLeft, 'Small'));
angleWheel = discreteCenterOfMass(alpha, functionValue);
readAngleWheel();
}
function discreteCenterOfMass(alpha, functionValue) {
var molecular = 0,
denominator = 0,
output;
for (var i = 0; i < alpha.length; i++) {
molecular += alpha[i] * functionValue[i];
denominator += alpha[i];
}
if (denominator == 0)
return 40;
output = molecular / denominator;
return Math.max(Math.min(output, 40), -40);
}
function fuzzyRulesCenter(distance, type) {
if (distance == -1)
distance = 1000;
switch (type) {
case 'Small':
if (distance < 3)
return 1;
if (distance < 10)
return -distance / 7.0 + 10.0 / 7.0;
break;
case 'Large':
if (distance >= 30)
return 1;
break;
}
return 0;
}
function fuzzyRulesRight(distance, type) {
if (distance == -1)
distance = 1000;
switch (type) {
case 'Small':
if (distance < 4)
return 1;
if (distance < 5)
return -distance + 5;
break;
case 'Medium':
if (distance < 4)
return 0;
if (distance < 10)
return distance / 6 - 4 / 6.0;
if (distance < 16)
return -distance / 6 + 16 / 6.0;
break;
case 'Large':
if (distance < 8)
return 0;
if (distance < 16)
return distance / 8 - 1;
return 1;
}
return 0;
}
function fuzzyRulesLeft(distance, type) {
if (distance == -1)
distance = 1000;
switch (type) {
case 'Small':
if (distance < 4)
return 1;
if (distance < 5)
return -distance + 5;
break;
case 'Medium':
if (distance < 4)
return 0;
if (distance < 10)
return distance / 6 - 4 / 6.0;
if (distance < 16)
return -distance / 6 + 16 / 6.0;
break;
case 'Large':
if (distance < 10)
return 0;
if (distance < 16)
return distance / 6 - 10 / 6.0;
return 1;
}
return 0;
}