Browse Source

add outline mode

Thomas Buck 1 year ago
parent
commit
88a7c2b5c2
1 changed files with 58 additions and 21 deletions
  1. 58
    21
      CatToy.py

+ 58
- 21
CatToy.py View File

4
 import random
4
 import random
5
 from machine import Timer
5
 from machine import Timer
6
 
6
 
7
-max_laser_power = 0.15
7
+max_laser_power = 0.1
8
 
8
 
9
 limits = [
9
 limits = [
10
     # pan_min, pan_max, tilt_min, tilt_max, name
10
     # pan_min, pan_max, tilt_min, tilt_max, name
11
     (84, 120, 53, 76, 'office desk, front right')
11
     (84, 120, 53, 76, 'office desk, front right')
12
 ]
12
 ]
13
 
13
 
14
+timerRunning = False
15
+timerData = None
16
+outlineIndex = 0
17
+
14
 def buildPage(header, footer):
18
 def buildPage(header, footer):
15
     html = """<!DOCTYPE html>
19
     html = """<!DOCTYPE html>
16
     <html>
20
     <html>
47
                 </select><br>
51
                 </select><br>
48
                 Steps: <input type="text" name="steps" value="100"><br>
52
                 Steps: <input type="text" name="steps" value="100"><br>
49
                 Duration: <input type="text" name="duration" value="1000">ms<br>
53
                 Duration: <input type="text" name="duration" value="1000">ms<br>
50
-                <input type="submit" name="s" value="Start Program">
54
+                <input type="submit" name="s" value="Random">
55
+                <input type="submit" name="s" value="Outline"><br>
56
+                Status: %s
51
             </form>
57
             </form>
52
             %s
58
             %s
53
         </body>
59
         </body>
60
         sl += '<option value="' + val + '">' + name + '</option>'
66
         sl += '<option value="' + val + '">' + name + '</option>'
61
     sl += '<option value="">None</option>'
67
     sl += '<option value="">None</option>'
62
 
68
 
63
-    page = html % (header, int(max_laser_power * 100.0), sl, footer)
69
+    status = "No program running"
70
+    if timerRunning:
71
+        status = "Program in progress"
72
+
73
+    page = html % (header, int(max_laser_power * 100.0), sl, status, footer)
64
     return page
74
     return page
65
 
75
 
66
 random.seed()
76
 random.seed()
145
     t.angle(t.tilt, tilt)
155
     t.angle(t.tilt, tilt)
146
     t.angle(t.pan, pan)
156
     t.angle(t.pan, pan)
147
 
157
 
148
-timerRunning = False
149
-timerData = None
158
+def doOutline(pan_min, pan_max, tilt_min, tilt_max, dur):
159
+    global outlineIndex
160
+    points = [
161
+        (pan_min, tilt_min),
162
+        (pan_min, tilt_max),
163
+        (pan_max, tilt_max),
164
+        (pan_max, tilt_min)
165
+    ]
166
+    outlineIndex = (outlineIndex + 1) % 4
167
+    pan, tilt = points[outlineIndex]
168
+    print("outline move: tilt={} pan={} duration={}".format(tilt, pan, dur))
169
+    t.angle(t.tilt, tilt)
170
+    t.angle(t.pan, pan)
150
 
171
 
151
 def timerCallback(unused):
172
 def timerCallback(unused):
152
     global timerRunning, timerData
173
     global timerRunning, timerData
154
     if not timerRunning:
175
     if not timerRunning:
155
         return
176
         return
156
 
177
 
157
-    pan_min, pan_max, tilt_min, tilt_max, steps, duration = timerData
178
+    pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline = timerData
158
 
179
 
159
     dur = duration
180
     dur = duration
160
-    if dur < 200:
161
-        dur = random.randint(200, 2000)
181
+    if not outline:
182
+        if dur < 200:
183
+            dur = random.randint(200, 2000)
184
+        else:
185
+            dur = random.randint(200, duration)
162
     else:
186
     else:
163
-        dur = random.randint(200, duration)
187
+        if dur < 200:
188
+            dur = 500
164
 
189
 
165
     if steps > 0:
190
     if steps > 0:
166
         steps -= 1
191
         steps -= 1
167
-        doMove(pan_min, pan_max, tilt_min, tilt_max, dur)
192
+        if not outline:
193
+            doMove(pan_min, pan_max, tilt_min, tilt_max, dur)
194
+        else:
195
+            doOutline(pan_min, pan_max, tilt_min, tilt_max, dur)
168
         tim = Timer(period = dur, mode=Timer.ONE_SHOT, callback = timerCallback)
196
         tim = Timer(period = dur, mode=Timer.ONE_SHOT, callback = timerCallback)
169
     else:
197
     else:
170
         timerRunning = False
198
         timerRunning = False
171
         t.laser(0.0)
199
         t.laser(0.0)
172
 
200
 
173
-    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration)
201
+    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline)
174
 
202
 
175
-def startRepeat(pan_min, pan_max, tilt_min, tilt_max, steps, duration):
203
+def startRepeat(pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline):
176
     global timerRunning, timerData
204
     global timerRunning, timerData
177
-    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration)
205
+    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline)
178
 
206
 
179
     if not timerRunning:
207
     if not timerRunning:
180
         timerRunning = True
208
         timerRunning = True
188
 
216
 
189
 def repeatCallback(request):
217
 def repeatCallback(request):
190
     q = request.find("/repeat?")
218
     q = request.find("/repeat?")
191
-    pl = request.find("limit=")
192
-    ps = request.find("steps=")
193
-    pd = request.find("duration=")
194
-    if (q < 0) or (pl < 0) or (ps < 0) or (pd < 0):
195
-        print("repeat query error: q={} pl={} ps={} pd={}".format(q, pl, ps, pd))
219
+    pl = request.find("limit=", q)
220
+    ps = request.find("steps=", pl)
221
+    pd = request.find("duration=", ps)
222
+    pp = request.find("s=", pd)
223
+    if (q < 0) or (pl < 0) or (ps < 0) or (pd < 0) or (pp < 0):
224
+        print("repeat query error: q={} pl={} ps={} pd={} pp={}".format(q, pl, ps, pd, pp))
196
         return buildPage(
225
         return buildPage(
197
             '<p>Error: no repeat arguments found in URL query string.</p>',
226
             '<p>Error: no repeat arguments found in URL query string.</p>',
198
             '<p><a href="/">Back to main page</a></p>'
227
             '<p><a href="/">Back to main page</a></p>'
199
         )
228
         )
200
 
229
 
201
-    data = [("limit=", pl), ("steps=", ps), ("duration=", pd)]
230
+    data = [("limit=", pl), ("steps=", ps), ("duration=", pd), ("s=", pp)]
202
     result = []
231
     result = []
203
     for s, p in data:
232
     for s, p in data:
233
+        #print(p)
204
         pe = request.find("&", p)
234
         pe = request.find("&", p)
235
+        #print(pe)
205
         if (pe < 0) or (p + len(s) > pe) or (pe - (p + 3) > 40):
236
         if (pe < 0) or (p + len(s) > pe) or (pe - (p + 3) > 40):
206
             pe = request.find(" HTTP", p)
237
             pe = request.find(" HTTP", p)
238
+            #print(pe)
207
         if (pe < 0) or (p + len(s) > pe) or (pe - (p + 3) > 40):
239
         if (pe < 0) or (p + len(s) > pe) or (pe - (p + 3) > 40):
208
             print("repeat query error: p={} pe={}".format(p, pe))
240
             print("repeat query error: p={} pe={}".format(p, pe))
209
             return buildPage(
241
             return buildPage(
212
             )
244
             )
213
         r = request[p + len(s) : pe]
245
         r = request[p + len(s) : pe]
214
         result.append(r)
246
         result.append(r)
247
+        #print()
215
 
248
 
216
-    print("repeat: limit={} steps={} duration={}".format(result[0], result[1], result[2]))
249
+    print("repeat: limit={} steps={} duration={} s={}".format(result[0], result[1], result[2], result[3]))
217
 
250
 
218
     if len(result[0]) == 0:
251
     if len(result[0]) == 0:
219
         stopRepeat()
252
         stopRepeat()
222
             '<p><a href="/">Back to main page</a></p>'
255
             '<p><a href="/">Back to main page</a></p>'
223
         )
256
         )
224
 
257
 
258
+    outline = False
259
+    if result[3].lower() == "outline":
260
+        outline = True
261
+
225
     for pan_min, pan_max, tilt_min, tilt_max, name in limits:
262
     for pan_min, pan_max, tilt_min, tilt_max, name in limits:
226
         val = name.replace(' ', '_').replace(',', '').lower()
263
         val = name.replace(' ', '_').replace(',', '').lower()
227
         if result[0] == val:
264
         if result[0] == val:
228
-            startRepeat(pan_min, pan_max, tilt_min, tilt_max, int(result[1]), int(result[2]))
265
+            startRepeat(pan_min, pan_max, tilt_min, tilt_max, int(result[1]), int(result[2]), outline)
229
             break
266
             break
230
 
267
 
231
     return buildPage(
268
     return buildPage(

Loading…
Cancel
Save