-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsm.py
More file actions
55 lines (42 loc) · 1.59 KB
/
fsm.py
File metadata and controls
55 lines (42 loc) · 1.59 KB
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
from time import time
class FSM(object):
def __init__(self, t_low, t_high):
self.state = 'IDLE'
self.low_thresh = t_low
self.high_thresh = t_high
self.states = { 'IDLE': self.idle,
'S1HI': self.s1hi,
'S2HI': self.s2hi,
'HIGH': self.high}
def idle(self, sensor1, sensor2):
if sensor1 > self.high_thresh:
#print('Sensor 1 saw something!')
self.state = 'S1HI'
elif sensor2 > self.high_thresh:
#print('Sensor 2 saw something!')
self.state = 'S2HI'
def s1hi(self, sensor1, sensor2):
if sensor1 < self.low_thresh:
#print("It's nothing..")
self.state = 'IDLE'
elif sensor2 > self.high_thresh:
#print('Yeah, me too!')
self.state = 'HIGH'
def s2hi(self, sensor1, sensor2):
if sensor2 < self.low_thresh:
#print("It's nothing..")
self.state = 'IDLE'
elif sensor1 > self.high_thresh:
#print('Yeah, me too!')
self.state = 'HIGH'
def high(self, sensor1, sensor2):
if sensor1 < self.low_thresh and sensor2 < self.low_thresh:
#print("It's gone now..")
self.state = 'IDLE'
def update(self, sensor1, sensor2):
state = self.state
self.states[state](sensor1,sensor2)
# if we just changed to the HIGH state, then both sensors have found a new object
if self.state == 'HIGH' and state != 'HIGH':
return True
return False