Skip to content

Commit ad0d7cb

Browse files
committed
Perpendicular parking roughly done
1 parent 73ff7a4 commit ad0d7cb

15 files changed

+320
-52
lines changed

common/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .backward_for_some_time import BackwardForSomeTime
2+
from .forward_for_some_time import ForwardForSomeTime
3+
from .wait_some_time import WaitSomeTime

common/backward_for_some_time.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from control import Stage
2+
from time import time
3+
4+
5+
class BackwardForSomeTime(Stage):
6+
7+
def __init__(self, duration):
8+
super().__init__()
9+
self.duration = duration
10+
self.start_time = None
11+
12+
def started(self, tank, distances):
13+
self.start_time = time()
14+
15+
def control(self, tank, distances):
16+
tank.forward(-5)
17+
return time() - self.start_time > self.duration

common/forward_for_some_time.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from control import Stage
2+
from time import time
3+
4+
5+
class ForwardForSomeTime(Stage):
6+
7+
def __init__(self, duration):
8+
super().__init__()
9+
self.duration = duration
10+
self.start_time = None
11+
12+
def started(self, tank, distances):
13+
self.start_time = time()
14+
tank.restart_plot()
15+
16+
def control(self, tank, distances):
17+
tank.forward(20)
18+
return time() - self.start_time > self.duration

common/model.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
import skfuzzy as fuzz
4+
from skfuzzy import control as ctrl
5+
from skfuzzy.control import Antecedent, Consequent, Rule
6+
7+
8+
class FuzzyModel:
9+
10+
MAX_DIST = 6
11+
12+
def __init__(self, max_vel, stop_dist, break_dist, break_vel, sharpness):
13+
self._max_vel = max_vel
14+
self._stop_dist = stop_dist
15+
self._break_dist = break_dist
16+
self._break_vel = break_vel
17+
self._sharpness = sharpness
18+
19+
self._model = self._construct_model()
20+
21+
def get_velocity(self, distance):
22+
if distance < self._stop_dist:
23+
return 0
24+
25+
self._model.input['dist'] = distance
26+
self._model.compute()
27+
velocity = self._model.output['vel']
28+
29+
if velocity < 0.2:
30+
return 0
31+
32+
return velocity
33+
34+
def _construct_model(self):
35+
l_span = self._break_vel
36+
h_span = self._max_vel - self._break_vel
37+
38+
dist = Antecedent(np.linspace(0, self.MAX_DIST, 600), 'dist')
39+
vel = Consequent(np.linspace(-l_span, self._max_vel + h_span, 600), 'vel')
40+
41+
s = self._sharpness
42+
dist['h'] = fuzz.trapmf(dist.universe, [self._break_dist - s, self._break_dist + s, self.MAX_DIST, self.MAX_DIST])
43+
dist['m'] = fuzz.trapmf(dist.universe, [self._stop_dist - s, self._stop_dist + s, self._break_dist - s, self._break_dist + s])
44+
dist['l'] = fuzz.trapmf(dist.universe, [0, 0, self._stop_dist - s, self._stop_dist + s])
45+
46+
vel['l'] = fuzz.trimf(vel.universe, [-l_span, 0, l_span])
47+
vel['m'] = fuzz.trimf(vel.universe, [0, self._break_vel, self._max_vel])
48+
vel['h'] = fuzz.trimf(vel.universe, [self._max_vel - h_span, self._max_vel, self._max_vel + h_span])
49+
50+
dist.view()
51+
vel.view()
52+
plt.show()
53+
54+
rules = [
55+
Rule(dist['l'], vel['l']),
56+
Rule(dist['m'], vel['m']),
57+
Rule(dist['h'], vel['h']),
58+
]
59+
60+
ctrl_system = ctrl.ControlSystem(rules)
61+
model = ctrl.ControlSystemSimulation(ctrl_system)
62+
63+
return model

common/wait_some_time.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from time import time
2+
from control import Stage
3+
4+
5+
class WaitSomeTime(Stage):
6+
7+
def __init__(self, duration):
8+
super().__init__()
9+
self.duration = duration
10+
self.start_time = None
11+
12+
def started(self, tank, distances):
13+
self.start_time = time()
14+
15+
def control(self, tank, distances):
16+
tank.stop()
17+
return time() - self.start_time > self.duration

control.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def control(self):
1313
return True
1414

1515
distances = self._tank.read_distances()
16+
# print(f'sw: {distances.sw}; se: {distances.se}')
1617
current_stage = self._stages[0]
1718

1819
# start stage
@@ -54,4 +55,4 @@ def run_simulation_with_controller(controller):
5455
while not finished:
5556
finished = controller.control()
5657

57-
tank.plot_distances()
58+
tank.plot_distances2()

map.ttt

1 Byte
Binary file not shown.

parallel/__init__.py

Whitespace-only changes.

perp.py

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
1-
from time import time
2-
from control import Controller, Stage
3-
4-
5-
class ForwardStage(Stage):
6-
7-
def __init__(self):
8-
self.start = None
9-
super().__init__()
10-
11-
def started(self, tank, distances):
12-
self.start = time()
13-
14-
def control(self, tank, distances):
15-
tank.forward(5)
16-
return time() - self.start > 5
17-
18-
19-
class LeftStage(Stage):
20-
21-
def __init__(self):
22-
self.start = None
23-
super().__init__()
24-
25-
def started(self, tank, distances):
26-
self.start = time()
27-
28-
def control(self, tank, distances):
29-
tank.turn_left(5)
30-
return time() - self.start > 5
31-
32-
33-
class PerpParkController(Controller):
34-
35-
def __init__(self, tank):
36-
stages = [
37-
ForwardStage(),
38-
LeftStage(),
39-
ForwardStage(),
40-
]
41-
super().__init__(tank, stages)
42-
1+
# from common import WaitSomeTime
2+
# from control import Controller
3+
#
4+
#
5+
# class PerpParkController(Controller):
6+
#
7+
# def __init__(self, tank):
8+
# stages = [
9+
# WaitSomeTime(0.1),
10+
# ForwardToFindLeftSpace(),
11+
# BackwardBeforeTurn(),
12+
# TurnLeftToPark(),
13+
# # ForwardForSomeTime(3)
14+
# ]
15+
# super().__init__(tank, stages)

perp/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from control import Controller
2+
from common import WaitSomeTime, BackwardForSomeTime, ForwardForSomeTime
3+
from .forward_to_find_left_space import ForwardToFindLeftSpace
4+
from .turn_left_to_park import TurnLeftToPark
5+
from .backward_before_turn import BackwardBeforeTurn
6+
7+
8+
class PerpParkController(Controller):
9+
10+
def __init__(self, tank):
11+
stages = [
12+
WaitSomeTime(0.1),
13+
ForwardToFindLeftSpace(),
14+
BackwardBeforeTurn(),
15+
TurnLeftToPark(),
16+
# ForwardForSomeTime(3)
17+
]
18+
super().__init__(tank, stages)

0 commit comments

Comments
 (0)