1
2
3 """Another fuzyy controller for the inverted pendulum.
4
5 @author: Marc Vollmer (modified by Rene Liebscher)
6 """
7
8 from simulation import Controller
9
10 import fuzzy.System
11 import math
12
14 '''
15 Definition des Fuzzy-Systems:
16
17 1. Eingangsvariable Phi
18 2. Eingangsvariable dPhi_dT
19 3. Ausgangsvariable a
20 4. Definition der Regeln
21 '''
22 from fuzzy.InputVariable import InputVariable
23 from fuzzy.OutputVariable import OutputVariable
24 from fuzzy.fuzzify.Plain import Plain
25 from fuzzy.defuzzify.COG import COG
26 from fuzzy.Adjective import Adjective
27 from fuzzy.set.Polygon import Polygon
28
29 system = fuzzy.System.System()
30
31
32
33
34 input_temp = InputVariable(fuzzify=Plain())
35
36 system.variables["Phi"] = input_temp
37
38 in1_set = Polygon()
39 in1_set.add(x =-22.5, y= 0.0)
40 in1_set.add(x = 0.0, y= 1.0)
41 in1_set.add(x = 22.5, y= 0.0)
42 in1 = Adjective(in1_set)
43 input_temp.adjectives["eN"] = in1
44
45 in2_set = Polygon()
46 in2_set.add(x = 0.0, y= 0.0)
47 in2_set.add(x = 22.5, y= 1.0)
48 in2_set.add(x = 45.0, y= 0.0)
49 in2 = Adjective(in2_set)
50 input_temp.adjectives["pk"] = in2
51
52 in3_set = Polygon()
53 in3_set.add(x = 22.5, y= 0.0)
54 in3_set.add(x = 45.0, y= 1.0)
55 in3_set.add(x = 67.5, y= 0.0)
56 in3 = Adjective(in3_set)
57 input_temp.adjectives["pm"] = in3
58
59 in4_set = Polygon()
60 in4_set.add(x = 45.0, y= 0.0)
61 in4_set.add(x = 67.5, y= 1.0)
62 in4_set.add(x = 90.0, y= 1.0)
63 in4_set.add(x =180.0, y= 0.0)
64 in4 = Adjective(in4_set)
65 input_temp.adjectives["pg"] = in4
66
67 in5_set = Polygon()
68 in5_set.add(x = -45.0, y= 0.0)
69 in5_set.add(x = -67.5, y= 1.0)
70 in5_set.add(x = -90.0, y= 1.0)
71 in5_set.add(x =-180.0, y= 0.0)
72 in5 = Adjective(in5_set)
73 input_temp.adjectives["ng"] = in5
74
75 in6_set = Polygon()
76 in6_set.add(x = -22.5, y= 0.0)
77 in6_set.add(x = -45.0, y= 1.0)
78 in6_set.add(x = -67.5, y= 0.0)
79 in6 = Adjective(in6_set)
80 input_temp.adjectives["nm"] = in6
81
82 in7_set = Polygon()
83 in7_set.add(x = -0.0, y= 0.0)
84 in7_set.add(x = -22.5, y= 1.0)
85 in7_set.add(x = -45.0, y= 0.0)
86 in7 = Adjective(in7_set)
87 input_temp.adjectives["nk"] = in7
88
89
90
91
92 input_tempa = InputVariable(fuzzify=Plain())
93
94 system.variables["dPhi_dT"] = input_tempa
95
96 in1a_set = Polygon()
97 in1a_set.add(x =-11.25, y= 0.0)
98 in1a_set.add(x = 0.0, y= 1.0)
99 in1a_set.add(x = 11.25, y= 0.0)
100 in1a = Adjective(in1a_set)
101 input_tempa.adjectives["eN"] = in1a
102
103 in2a_set = Polygon()
104 in2a_set.add(x = 0.0, y= 0.0)
105 in2a_set.add(x = 11.25, y= 1.0)
106 in2a_set.add(x = 22.5, y= 0.0)
107 in2a = Adjective(in2a_set)
108 input_tempa.adjectives["pk"] = in2a
109
110 in3a_set = Polygon()
111 in3a_set.add(x = 11.25, y= 0.0)
112 in3a_set.add(x = 22.50, y= 1.0)
113 in3a_set.add(x = 33.75, y= 0.0)
114 in3a = Adjective(in3a_set)
115 input_tempa.adjectives["pm"] = in3a
116
117 in4a_set = Polygon()
118 in4a_set.add(x = 22.5, y= 0.0)
119 in4a_set.add(x = 33.75, y= 1.0)
120 in4a_set.add(x = 45.0 , y= 1.0)
121 in4a_set.add(x = 90.0 , y= 0.0)
122 in4a = Adjective(in4a_set)
123 input_tempa.adjectives["pg"] = in4a
124
125 in5a_set = Polygon()
126 in5a_set.add(x = -0.0, y= 0.0)
127 in5a_set.add(x = -11.25, y= 1.0)
128 in5a_set.add(x = -22.5, y= 0.0)
129 in5a = Adjective(in5a_set)
130 input_tempa.adjectives["nk"] = in5a
131
132 in6a_set = Polygon()
133 in6a_set.add(x = -11.25, y= 0.0)
134 in6a_set.add(x = -22.50, y= 1.0)
135 in6a_set.add(x = -33.75, y= 0.0)
136 in6a = Adjective(in6a_set)
137 input_tempa.adjectives["nm"] = in6a
138
139 in7a_set = Polygon()
140 in7a_set.add(x = -22.5, y= 0.0)
141 in7a_set.add(x = -33.75, y= 1.0)
142 in7a_set.add(x = -45.0 , y= 1.0)
143 in7a_set.add(x = -90.0 , y= 0.0)
144 in7a = Adjective(in7a_set)
145 input_tempa.adjectives["ng"] = in7a
146
147
148
149
150 output_temp = OutputVariable(defuzzify=COG())
151
152 system.variables["a"] = output_temp
153 output_temp.failsafe = 0.0
154
155 out1_set = Polygon()
156 out1_set.add(x =-0.25, y= 0.0)
157 out1_set.add(x = 0.0, y= 1.0)
158 out1_set.add(x = 0.25, y= 0.0)
159 out1 = Adjective(out1_set)
160 output_temp.adjectives["eN"] = out1
161
162 out2_set = Polygon()
163 out2_set.add(x = 0.00, y= 0.0)
164 out2_set.add(x = 0.25, y= 1.0)
165 out2_set.add(x = 0.50, y= 0.0)
166 out2 = Adjective(out2_set)
167 output_temp.adjectives["pk"] = out2
168
169 out3_set = Polygon()
170 out3_set.add(x = 0.25, y= 0.0)
171 out3_set.add(x = 0.50, y= 1.0)
172 out3_set.add(x = 0.75, y= 0.0)
173 out3 = Adjective(out3_set)
174 output_temp.adjectives["pm"] = out3
175
176 out4_set = Polygon()
177 out4_set.add(x = 0.50, y= 0.0)
178 out4_set.add(x = 0.75, y= 1.0)
179 out4_set.add(x = 1.0, y= 1.0)
180 out4_set.add(x = 2.0, y= 0.0)
181 out4 = Adjective(out4_set)
182 output_temp.adjectives["pg"] = out4
183
184 out5_set = Polygon()
185 out5_set.add(x = 0.00, y= 0.0)
186 out5_set.add(x =-0.25, y= 1.0)
187 out5_set.add(x =-0.50, y= 0.0)
188 out5 = Adjective(out5_set)
189 output_temp.adjectives["nk"] = out5
190
191 out6_set = Polygon()
192 out6_set.add(x =-0.25, y= 0.0)
193 out6_set.add(x =-0.50, y= 1.0)
194 out6_set.add(x =-0.75, y= 0.0)
195 out6 = Adjective(out6_set)
196 output_temp.adjectives["nm"] = out6
197
198 out7_set = Polygon()
199 out7_set.add(x =-0.50, y= 0.0)
200 out7_set.add(x =-0.75, y= 1.0)
201 out7_set.add(x =-1.0, y= 1.0)
202 out7_set.add(x =-2.0, y= 0.0)
203 out7 = Adjective(out7_set)
204 output_temp.adjectives["ng"] = out7
205
206
207
208
209 from fuzzy.Rule import Rule
210 from fuzzy.norm.Min import Min
211 from fuzzy.operator.Input import Input
212 from fuzzy.operator.Compound import Compound
213
214 rule1 = Rule(adjective=system.variables["a"].adjectives["pk"],
215 operator=Compound(
216 Min(),
217 Input(system.variables["Phi"].adjectives["nk"]),
218 Input(system.variables["dPhi_dT"].adjectives["eN"])
219 )
220 )
221 system.rules["rule1"]=rule1
222
223
224 rule2 = Rule(adjective=system.variables["a"].adjectives["nk"],
225 operator=Input(system.variables["Phi"].adjectives["pk"],),
226 )
227 system.rules["rule2"]=rule2
228
229 rule3 = Rule(adjective=system.variables["a"].adjectives["pm"],
230 operator=Input(system.variables["Phi"].adjectives["nm"],),
231 )
232 system.rules["rule3"]=rule3
233
234 rule4 = Rule(adjective=system.variables["a"].adjectives["nm"],
235 operator=Input(system.variables["Phi"].adjectives["pm"],),
236 )
237 system.rules["rule4"]=rule4
238
239 rule5 = Rule(adjective=system.variables["a"].adjectives["pg"],
240 operator=Input(system.variables["Phi"].adjectives["ng"],),
241 )
242 system.rules["rule5"]=rule5
243
244 rule6 = Rule(adjective=system.variables["a"].adjectives["ng"],
245 operator=Input(system.variables["Phi"].adjectives["pg"],),
246 )
247 system.rules["rule6"]=rule6
248
249 return system
250
251
253 """
254 Fuzzy controller.
255
256 """
257
260
261
262 - def calculate(self,input={},output={'a':0.0}):
263 '''
264 Calculates the output value.
265 '''
266
267 input["Phi"] = input["Phi"] - 90.
268 if input["Phi"]>180.:
269 input["Phi"] = input["Phi"] - 360.
270
271 input["dPhi_dT"] = math.radians(input["dPhi_dT"])
272 self.system.calculate(input,output)
273
274 return output
275
277 """Create docs of all variables."""
278 from fuzzy.doc.plot.gnuplot import doc
279 d = doc.Doc(directory)
280 d.createDoc(self.system)
281 d.overscan=0
282 d.create3DPlot(self.system,"Phi","dPhi_dT","a",{"X":0.,"dX_dT":0.})
283
285 """Create docs of rules."""
286 import fuzzy.doc.structure.dot.dot
287 import subprocess
288 for name,rule in self.system.rules.items():
289 cmd = "dot -T png -o '%s/Rule %s.png'" % (directory,name)
290 f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin
291 fuzzy.doc.structure.dot.dot.print_header(f,"XXX")
292 fuzzy.doc.structure.dot.dot.print_dot(rule,f,self.system,"")
293 fuzzy.doc.structure.dot.dot.print_footer(f)
294 cmd = "dot -T png -o '%s/System.png'" % directory
295 f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin
296 fuzzy.doc.structure.dot.dot.printDot(self.system,f)
297