forked from srinathava/raspberry-pi-sleep-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotionStateMachine.py
133 lines (103 loc) · 4.26 KB
/
MotionStateMachine.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
127
128
129
130
131
132
133
from datetime import datetime
def timeElapsed(tnow, t):
dt = tnow - t
return dt.total_seconds()
class MotionStateMachine:
IDLE = 1
MOTION_DETECTED = 2
SUSTAINED_MOTION = 3
MOTION_DETECTED_MOTION = 1
MOTION_DETECTED_NOMOTION = 2
SUSTAINED_MOTION_MOTION = 1
SUSTAINED_MOTION_NOMOTION = 2
def __init__(self):
self.reset()
def reset(self):
self.state = 0
self.MOTION_DETECTED_state = 0
self.SUSTAINED_MOTION_state = 0
self.MOTION_DETECTED_tStart = 0
self.MOTION_DETECTED_NOMOTION_tStart = 0
self.SUSTAINED_MOTION_NOMOTION_tStart = 0
self.CALM_TIME = 30
self.SUSTAINED_TIME = 90
def inSustainedMotion(self):
return self.state == self.SUSTAINED_MOTION
def secondsInSustainedMotion(self):
if not self.inSustainedMotion():
return 0
return timeElapsed(self.SUSTAINED_MOTION_tStart)
def step(self, motion, tnow=None):
if not tnow:
tnow = datetime.now()
if self.state == 0:
self.state = self.IDLE
if self.state == self.IDLE:
if motion:
self.state = self.MOTION_DETECTED
self.MOTION_DETECTED_state = self.MOTION_DETECTED_MOTION
self.MOTION_DETECTED_tStart = tnow
elif self.state == self.MOTION_DETECTED:
if motion and timeElapsed(tnow, self.MOTION_DETECTED_tStart) >= self.SUSTAINED_TIME:
self.state = self.SUSTAINED_MOTION
self.MOTION_DETECTED_state = 0
self.SUSTAINED_MOTION_state = self.SUSTAINED_MOTION_MOTION
self.SUSTAINED_MOTION_tStart = tnow
elif self.MOTION_DETECTED_state == self.MOTION_DETECTED_MOTION:
if not motion:
self.MOTION_DETECTED_state = self.MOTION_DETECTED_NOMOTION
self.MOTION_DETECTED_NOMOTION_tStart = tnow
elif self.MOTION_DETECTED_state == self.MOTION_DETECTED_NOMOTION:
if motion:
self.MOTION_DETECTED_state = self.MOTION_DETECTED_MOTION
else:
dt = timeElapsed(tnow, self.MOTION_DETECTED_NOMOTION_tStart)
if dt >= self.CALM_TIME:
self.MOTION_DETECTED_state = 0
self.state = self.IDLE
elif self.state == self.SUSTAINED_MOTION:
if self.SUSTAINED_MOTION_state == self.SUSTAINED_MOTION_MOTION:
if not motion:
self.SUSTAINED_MOTION_state = self.SUSTAINED_MOTION_NOMOTION
self.SUSTAINED_MOTION_NOMOTION_tStart = tnow
elif self.SUSTAINED_MOTION_state == self.SUSTAINED_MOTION_NOMOTION:
if motion:
self.SUSTAINED_MOTION_state = self.SUSTAINED_MOTION_MOTION
elif timeElapsed(tnow, self.SUSTAINED_MOTION_NOMOTION_tStart) >= self.CALM_TIME:
self.SUSTAINED_MOTION_state = 0
self.state = self.IDLE
if __name__ == "__main__":
sm = MotionStateMachine()
from dateutil.parser import parse
import matplotlib.pyplot as plot
import re
PAT_INFO = re.compile(r'(?P<time>\S+): motionDetected (?P<motion>\d)')
tStart = parse('2015-09-03 03:00:00')
tEnd = parse('2016-09-03 05:10:00')
x = []
rawMotion = []
state = []
detectedState = []
for line in open('logs/sleep_2015_09_03.log'):
m = PAT_INFO.match(line)
if m:
t = parse(m.group('time'))
if t > tEnd:
break
if t >= tStart:
motion = int(m.group('motion'))
sm.step(motion, t)
x.append(t)
rawMotion.append(motion)
state.append(sm.state)
detectedState.append(sm.MOTION_DETECTED_state)
ax1 = plot.subplot(311)
plot.plot(x, rawMotion, '-+')
plot.xlim(min(x), max(x))
plot.subplot(312, sharex=ax1)
plot.plot(x, detectedState, '-+')
plot.xlim(min(x), max(x))
plot.subplot(313, sharex=ax1)
plot.plot(x, state, '-+')
plot.xlim(min(x), max(x))
plot.show()