-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lisp
249 lines (216 loc) · 6.19 KB
/
test.lisp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
(asdf:load-system :gooptest)
(defpackage :cat-door-tests
(:use :cl :gooptest :gooptest-avr))
(in-package :cat-door-tests)
(defun flashing-p ()
(< 0.45 (pin-duty-cycle :b2 :stop '(1 :s) :skip '(1 :ms) :pull :up) 0.55))
;; a4 = open, a5 = closed, active high
(defun set-switch-open ()
(setf (pin :a4) :high)
(setf (pin :a5) :low))
(defun set-switch-one-way ()
(setf (pin :a4) :low)
(setf (pin :a5) :low))
(defun set-switch-closed ()
(setf (pin :a4) :low)
(setf (pin :a5) :high))
;; a6 = open, a7 = closed, active low
(defun set-sensor-open ()
(setf (pin :a6) :low)
(setf (pin :a7) :high))
(defun set-sensor-floating ()
(setf (pin :a6) :high)
(setf (pin :a7) :high))
(defun set-sensor-closed ()
(setf (pin :a6) :high)
(setf (pin :a7) :low))
(defun motor (pin1 pin2)
(ecase (pin pin1)
(:high (ecase (pin pin2)
(:low :opening)))
(:low (ecase (pin pin2)
(:high :closing)
(:low :idling)))))
;; a0 = inner up, a1 = inner down
(defun inner-motor ()
(motor :a0 :a1))
;; a2 = outer up, a3 = outer down
(defun outer-motor ()
(motor :a2 :a3))
(defun assert-outer-motor (state)
(assert (eq state (outer-motor))))
(defun assert-inner-motor (state)
(assert (eq state (inner-motor))))
(defsuite cat-door
:core (make-instance 'avr-core
:frequency 128000
:mcu :attiny84
;; TODO: What path are relative pathnames relative to?
:firmware-path #P"/home/markasoftware/Development/cat-door/main.elf"))
(in-suite cat-door)
(runtest "Open outer motor (sw open)"
(set-switch-open)
(set-sensor-closed)
(cycles 3 :s)
;(assert (flashing-p))
(assert-outer-motor :opening))
(runtest "Open outer motor (sw one-way)"
(set-switch-one-way)
(set-sensor-closed)
(cycles 3 :s)
;(assert (flashing-p))
(assert-outer-motor :opening))
(runtest "Close outer motor (sw closed)"
(set-switch-closed)
(set-sensor-open)
(cycles 3 :s)
;(assert (flashing-p))
(assert-outer-motor :closing))
(runtest "Toggle outer motor as things change"
(set-switch-closed)
(set-sensor-open)
(cycles 3 :s)
(assert-outer-motor :closing)
(set-sensor-closed)
(cycles 50 :ms)
(assert-outer-motor :idling)
;(assert (not (flashing-p)))
(set-sensor-open)
(cycles 50 :ms)
(assert-outer-motor :closing)
;(assert (not (flashing-p)))
(set-sensor-closed)
(cycles 50 :ms)
(assert-outer-motor :idling)
(set-switch-one-way)
(cycles 50 :ms)
(assert-outer-motor :opening)
(set-sensor-floating)
(cycles 50 :ms)
(assert-outer-motor :opening)
;; one-way -> open triggers inner door
(set-switch-open)
(cycles 50 :ms)
(assert-outer-motor :opening)
(set-sensor-open)
(cycles 50 :ms)
(assert-outer-motor :idling))
(runtest "Open inner motor on startup (sw open)"
(set-switch-open)
(set-sensor-open)
(cycles 1000)
(assert-inner-motor :opening)
(cycles 3 :s)
(assert-inner-motor :idling))
(runtest "Close inner motor on startup (sw open)"
(set-switch-one-way)
(set-sensor-open)
(cycles 1000)
(assert-inner-motor :closing)
(cycles 3 :s)
(assert-inner-motor :idling))
(runtest "Close inner motor on startup (sw closed)"
(set-switch-closed)
(set-sensor-closed)
(cycles 1000)
(assert-inner-motor :closing)
(cycles 3 :s)
(assert-inner-motor :idling))
(runtest "Toggles inner motor when things change"
(set-switch-open)
(set-sensor-open)
(cycles 1000)
(assert-inner-motor :opening)
(set-switch-closed)
(cycles 1000)
;; Shouldn't get interrupted
(assert-inner-motor :opening)
(cycles 3 :s)
;; but, once done, it should reverse
(assert-inner-motor :closing)
(cycles 3 :s)
;; and, eventually, it should stop
(assert-inner-motor :idling)
;; Make sure it resets the timer to zero every time the direction changes
(dotimes (i 10)
(set-switch-open)
(cycles 500 :ms)
(set-switch-closed)
(cycles 500 :ms))
(set-switch-open)
(cycles 3 :s)
(assert-inner-motor :opening)
(cycles 3 :s)
(assert-inner-motor :idling))
(runtest "Does both inner and outer door operations on startup"
(set-switch-open)
(set-sensor-closed)
(cycles 50 :ms)
(assert-inner-motor :opening)
(assert-outer-motor :idling)
(cycles 3 :s)
(assert-inner-motor :idling)
(assert-outer-motor :opening)
(set-sensor-open)
(cycles 50 :ms)
(assert-inner-motor :idling)
(assert-outer-motor :idling))
(runtest "Re-opens the inner door every so often"
(cycles 1000)
(set-switch-open)
(set-sensor-open)
(cycles 3 :s)
(assert-inner-motor :idling)
(assert-outer-motor :idling)
(assert
(cycles-between (:start '(1 :m) :stop '(5 :m) :skip '(100 :ms))
(eq (inner-motor) :opening)))
(cycles 3 :s)
(assert-inner-motor :idling))
(runtest "Close-Rewind based on timeout"
(set-switch-closed)
(set-sensor-open)
(cycles 3 :s)
(assert-outer-motor :closing)
(set-sensor-floating)
(cycles 10 :s)
(assert-outer-motor :opening)
(set-sensor-closed)
(cycles 50 :ms)
(assert-outer-motor :idling))
;; TODO: do we actually /want/ to rewind multiple times?
(runtest "Rewinds a couple times"
(set-switch-closed)
(set-sensor-open)
(cycles 3 :s)
(assert-outer-motor :closing)
(set-sensor-floating)
(cycles 1000)
(set-sensor-open)
(assert-outer-motor :opening)
(cycles 1000)
(set-sensor-floating)
(assert-outer-motor :opening)
(cycles 1000)
(set-sensor-open)
(assert-outer-motor :closing)
(cycles 1000)
(set-sensor-closed)
(cycles 1000)
(assert-outer-motor :idling))
(runtest "Waits a minute to retry after a botched rewind"
(set-switch-closed)
(set-sensor-open)
(cycles 3 :s)
(assert-outer-motor :closing))
;; More:
;; + Switch changes interrupting an inner door motion
;; + Periodic timer interrupting outer door motion: Interrupt
;; + Periodic timer interrupting rewind: Wait
;; + Switch changes interrupting rewind: Wait for rewind to finish
;; + Switch changes interrupting pause between rewinds: Interrupt
;; + major TODO: Rewind when opening outer door takes too long
;;
;; Have cli- and sei-like functions that only operate on some interrupts so, eg,
;; we can still get sensor interrupts while rewinding while having, eg, switch
;; interrupts disabled? Or we could just use an atomic busy loop for rewind.