-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzy_car_controller.py
126 lines (99 loc) · 5.47 KB
/
fuzzy_car_controller.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
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
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import skfuzzy
import skfuzzy.control
from map import RayCastResult
from car import Car, CarController
from visualization import MyFuzzyVariableVisualizer
class FuzzyCarController(CarController):
def __init__(self, car: Car):
super().__init__(car)
self.fig = None
# TODO: more dynamic, based on sensor names/angles
self.setup_inputs()
self.setup_outputs()
self.setup_control_system()
self.simulation = skfuzzy.control.ControlSystemSimulation(self.control_system)
def setup_inputs(self):
velocity = skfuzzy.control.Antecedent(np.arange(0, 200 + 1, 1), 'velocity')
velocity['SLOW'] = skfuzzy.trapmf(velocity.universe, [0, 0, 0, 75])
velocity['MEDIUM'] = skfuzzy.trapmf(velocity.universe, [50, 100, 100, 150])
velocity['FAST'] = skfuzzy.trapmf(velocity.universe, [75, 200, 200, 200])
balance = skfuzzy.control.Antecedent(np.arange(-200, 200 + 1, 1), 'balance')
balance['LEFT'] = skfuzzy.trapmf(balance.universe, [-200, -200, -200, 0])
balance['CENTER'] = skfuzzy.trapmf(balance.universe, [-50, 0, 0, 50])
balance['RIGHT'] = skfuzzy.trapmf(balance.universe, [0, 200, 200, 200])
side = skfuzzy.control.Antecedent(np.arange(-100, 100 + 1, 1), 'side')
side['LEFT'] = skfuzzy.trapmf(side.universe, [-100, -100, -100, 0])
side['CENTER'] = skfuzzy.trapmf(side.universe, [-25, 0, 0, 25])
side['RIGHT'] = skfuzzy.trapmf(side.universe, [0, 100, 100, 100])
head = skfuzzy.control.Antecedent(np.arange(0, 200 + 1, 1), 'head')
head['CLOSE'] = skfuzzy.trapmf(head.universe, [ 0, 0, 25, 125])
head['AWAY'] = skfuzzy.trapmf(head.universe, [75, 200, 200, 200])
self.inputs = [velocity, balance, side, head]
def setup_outputs(self):
gas = skfuzzy.control.Consequent(np.arange(0 - 0.25, 1 + 0.02 + 0.25, 0.02), 'gas')
gas['NONE'] = skfuzzy.sigmf(gas.universe, 0.05, -40)
gas['SOFT'] = skfuzzy.sigmf(gas.universe, 0.33, -10)
gas['HARD'] = skfuzzy.sigmf(gas.universe, 0.75, 20)
brake = skfuzzy.control.Consequent(np.arange(0 - 0.25, 1 + 0.02 + 0.25, 0.02), 'brake')
brake['NONE'] = skfuzzy.sigmf(brake.universe, 0.05, -40)
brake['SOFT'] = skfuzzy.sigmf(brake.universe, 0.33, -10)
brake['HARD'] = skfuzzy.sigmf(brake.universe, 0.75, 20)
steer = skfuzzy.control.Consequent(np.arange(-1 - 0.5, 1 + 0.05 + 0.5, 0.05), 'steer')
steer['RIGHT'] = skfuzzy.sigmf(steer.universe, -0.5, -10)
steer['NONE'] = skfuzzy.gaussmf(steer.universe, 0, 0.10)
steer['LEFT'] = skfuzzy.sigmf(steer.universe, 0.5, 10)
self.outputs = [gas, brake, steer]
def setup_control_system(self):
c = skfuzzy.control
velocity, balance, side, head = self.inputs
gas, brake, steer = self.outputs
self.control_system = c.ControlSystem([
c.Rule(balance['LEFT'], steer['RIGHT']),
c.Rule(balance['RIGHT'], steer['LEFT']),
c.Rule(balance['CENTER'] & side['CENTER'], steer['NONE'] % 0.1),
c.Rule(side['LEFT'], steer['RIGHT']),
c.Rule(side['RIGHT'], steer['LEFT']),
c.Rule(head['CLOSE'], (brake['HARD'], gas['NONE'])),
c.Rule(head['AWAY'] & balance['CENTER'], (brake['NONE'], gas['HARD'])),
c.Rule(head['AWAY'] & ~balance['CENTER'], (brake['NONE'], gas['SOFT'])),
c.Rule(velocity['SLOW'], gas['SOFT']),
])
def update_simulation(self, sensors: dict[str, RayCastResult]):
self.simulation.input['velocity'] = self.car.velocity
self.simulation.input['balance'] = sensors['left'].distance - sensors['right'].distance
self.simulation.input['side'] = sensors['hard_left'].distance - sensors['hard_right'].distance
self.simulation.input['head'] = sensors['head'].distance
self.simulation.compute()
self.gas = max(0, self.simulation.output['gas']) # other are properly clamped in the base car controller
self.brake = self.simulation.output['brake']
self.steer = self.simulation.output['steer']
def update(self, dt: float, *args, **kwargs):
# self.update_simulation(sensors) # need to be called separately
super().update(dt, *args, **kwargs)
def setup_visualization(self, width: float, height: float):
velocity, balance, side, head = self.inputs
gas, brake, steer = self.outputs
dpi = 67
fig = plt.figure(figsize=(width / dpi, height / dpi), dpi=dpi)
gs = gridspec.GridSpec(3, 3, figure=fig)
self.visualizers = [
MyFuzzyVariableVisualizer(velocity, plt.subplot(gs[0, 2])),
MyFuzzyVariableVisualizer(head, plt.subplot(gs[1, 0])),
MyFuzzyVariableVisualizer(balance, plt.subplot(gs[1, 1])),
MyFuzzyVariableVisualizer(side, plt.subplot(gs[1, 2])),
MyFuzzyVariableVisualizer(gas, plt.subplot(gs[2, 0])),
MyFuzzyVariableVisualizer(brake, plt.subplot(gs[2, 1])),
MyFuzzyVariableVisualizer(steer, plt.subplot(gs[2, 2])),
]
self.fig = fig
print('Done visualization setup')
def visualize(self, width: float, height: float):
if self.fig is None:
self.setup_visualization(width, height)
for v in self.visualizers:
v.view(sim=self.simulation)
return self.fig