-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzy.py
64 lines (56 loc) · 2.07 KB
/
fuzzy.py
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
import skfuzzy.control as ctrl
from typing import Callable
class FuzzyControl(ctrl.ControlSystemSimulation):
"""Simple Fuzzy controller for simulations with system model."""
def __init__(
self,
ref: float,
err: ctrl.Antecedent,
derivative_err: ctrl.Antecedent,
u: ctrl.Consequent,
sys_func: Callable[[float], float],
control_system: ctrl.ControlSystem,
clip_to_bounds: bool = True,
cache: bool = True,
flush_after_run: bool = 1000
) -> None:
"""Simple Fuzzy controller.
Args:
ref (float): Reference value.
err (ctrl.Antecedent): Error input.
derivative_err (ctrl.Antecedent): Derivative of error input.
u (ctrl.Consequent): Output.
sys_func (Callable[[float], float]): Mathematical representation of the system.
control_system (ctrl.ControlSystem): Control system object with defined rules.
clip_to_bounds (bool, optional): Defaults to True.
cache (bool, optional): Defaults to True.
flush_after_run (bool, optional): Defaults to 1000.
"""
super().__init__(control_system, clip_to_bounds, cache, flush_after_run)
self.ref = ref
self.err = err
self.derr = derivative_err
self.u = u
self.sys_func = sys_func
self.control_sys = control_system
self.clear()
def clear(self) -> None:
"""Clears *last error* memory.
Call this if you want to repeat your simulations sequentially.
"""
self._last_err = 0
def step(self, feedback: float) -> float:
"""Calculates system output for one loop.
Args:
feedback (float): Initial feedback.
Returns:
float: System output.
"""
err = self.ref - feedback
derr = err - self._last_err
self._last_err = err
self.input[self.err.label] = err
self.input[self.derr.label] = derr
self.compute()
u = self.output[self.u.label]
return self.sys_func(u)