-
Notifications
You must be signed in to change notification settings - Fork 3
/
actuators.py
377 lines (316 loc) · 11.8 KB
/
actuators.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 12 2017
@author: Mark Dammer
Various actuator classes to provide an interface to external hard- and software.
The 'dummy' class can be used for testing and training.
"""
from threading import Thread, Event
from kivy.graphics import Line, Color
from kivy.graphics.instructions import InstructionGroup
import requests
import pyautogui
import liblo
import time
class Dummy:
__slots__ = ['cnf', 'name', 'x', 'y', 'z', 'oldx', 'oldy', 'oldz', 'jx', 'jy', 'joldx', 'joldy',
'actuator_thread_run', 'sendrequest', 'requestthread']
def __init__(self, cnf):
self.name = 'Dummy'
self.cnf = cnf
self.x = self.y = self.z = 0
self.oldx = self.oldy = self.oldz = 0
self.jx = self.jy = 0
self.joldx = self.joldy = 0
self.actuator_thread_run = True
self.sendrequest = Event()
if not self.cnf.single_thread:
self.requestthread = Thread(target=self.request_thread)
self.requestthread.start()
def transmit(self, x, y, z, jx, jy):
self.x = int(x)
self.y = int(y)
self.z = int(z)
self.jx = int(jx)
self.jy = int(jy)
if self.x != self.oldx or self.y != self.oldy or self.z != self.oldz or self.jx != self.joldx or self.jy != self.joldy:
if self.cnf.single_thread:
self.output()
else:
self.sendrequest.set()
self.oldx = self.x
self.oldy = self.y
self.oldz = self.z
self.joldx = self.jx
self.joldy = self.jy
def start(self):
return
def stop(self):
return
def shutdown(self):
self.actuator_thread_run = False
self.stop()
self.x = self.y = self.z = 0
self.sendrequest.set()
if not self.cnf.single_thread:
self.requestthread.join()
def video_play(self):
print('play videofeed')
def video_stop(self):
print('stop videofeed')
def output(self):
print("%s,%s,%s,%s,%s" % (
str(time.time()), time.strftime("%d.%m.%Y %H:%M:%S"), str(self.x), str(self.y), str(self.z)))
def request_thread(self):
while self.actuator_thread_run:
self.sendrequest.wait()
self.output()
self.sendrequest.clear()
class Paint(object):
__slots__ = ['name', 'cnf', 'x', 'y', 'z', 'oldx', 'oldy', 'oldz', 'video', 'draw_widget',
'draw_instructions', 'firstrun', 'actuator_thread_run', 'sendrequest', 'requestthread']
def __init__(self, cnf):
self.name = 'Paint'
self.cnf = cnf
self.x = self.y = self.z = 0
self.oldx = self.oldy = self.oldz = 0
self.video = self.cnf.rootwidget.oimage_wid
self.video.source = self.cnf.background_source
self.draw_widget = self.cnf.rootwidget.oimage_wid
self.draw_instructions = InstructionGroup()
self.draw_widget.canvas.add(self.draw_instructions)
self.firstrun = True
self.actuator_thread_run = True
self.sendrequest = Event()
if not self.cnf.single_thread:
self.requestthread = Thread(target=self.request_thread)
self.requestthread.start()
def transmit(self, x, y, z, jx, jy):
self.x = int(x * self.cnf.out_xscale + self.cnf.out_xcenter)
self.y = int(y * self.cnf.out_yscale + self.cnf.out_ycenter)
self.z = z
if self.cnf.single_thread:
self.output()
else:
self.sendrequest.set()
def start(self):
self.cnf.show_z = False
self.cnf.rootwidget.zindicator_wid.text = ''
self.cnf.rootwidget.marker_wid.source = 'assets/draw_marker.png'
self.draw_instructions = InstructionGroup()
self.draw_widget.canvas.add(self.draw_instructions)
def stop(self):
self.draw_widget.canvas.remove(self.draw_instructions)
self.cnf.rootwidget.marker_wid.source = 'assets/hud_marker.png'
self.cnf.show_z = True
def shutdown(self):
self.actuator_thread_run = False
self.stop()
self.sendrequest.set()
if not self.cnf.single_thread:
self.requestthread.join()
def video_play(self):
self.video.state = 'play'
def video_stop(self):
self.video.state = 'stop'
def output(self):
if self.firstrun:
self.oldx = self.x + 1
self.oldy = self.y + 1
self.oldz = self.z
self.firstrun = False
if self.x != self.oldx or self.y != self.oldy:
self.draw_instructions.add(Color(self.z / 255, 1, 1, mode='hsv'))
self.draw_instructions.add(
Line(points=[self.oldx, self.oldy, self.x, self.y], width=2))
self.oldx = self.x
self.oldy = self.y
self.oldz = self.z
def request_thread(self):
while self.actuator_thread_run:
self.sendrequest.wait()
self.output()
self.sendrequest.clear()
class Mouse(object):
__slots__ = ['cnf', 'name', 'url', 'screenx', 'screeny', 'x', 'y', 'oldx', 'oldy', 'centerx', 'centery', 'xfactor',
'yfactor', 'actuator_thread_run', 'requestthread', 'sendrequest']
def __init__(self, cnf):
self.name = 'Mouse'
self.cnf = cnf
self.screenx, self.screeny = pyautogui.size()
self.x = self.oldx = self.centerx = self.screenx / 2
self.y = self.oldy = self.centery = self.screeny / 2
self.xfactor = self.centerx / 1000
self.yfactor = self.centery / -1000
pyautogui.moveTo(self.x, self.y, duration=1)
self.actuator_thread_run = True
if not self.cnf.single_thread:
self.requestthread = Thread(target=self.request_thread)
self.requestthread.start()
def transmitabs(self, x, y):
self.x = int(x * self.xfactor + self.centerx)
self.y = int(y * self.yfactor + self.centery)
if self.x != self.oldx or self.y != self.oldy:
pyautogui.moveTo(self.x, self.y)
self.oldx = self.x
self.oldy = self.y
def transmit(self, x, y, z, jx, jy):
self.x = int(x / 10)
self.y = int(y / -10)
if self.x != 0 or self.y != 0:
if self.cnf.single_thread:
self.set_pointer()
else:
self.sendrequest.set()
def start(self):
print('start actuator')
def stop(self):
print('stop actuator')
def shutdown(self):
self.actuator_thread_run = False
self.stop()
self.sendrequest.set()
if not self.cnf.single_thread:
self.requestthread.join()
def video_play(self):
print('play')
def video_stop(self):
print('stop')
def set_pointer(self):
pyautogui.moveRel(self.x, self.y)
def request_thread(self):
while self.actuator_thread_run:
self.sendrequest.wait()
self.set_pointer()
self.sendrequest.clear()
class OSC(object):
__slots__ = ['cnf', 'name', 'url', 'x', 'y', 'z', 'oldx', 'oldy', 'oldz', 'jx', 'jy', 'joldx', 'joldy', 'oscclient',
'video', 'actuator_thread_run', 'sendrequest', 'requestthread']
def __init__(self, cnf):
self.name = 'OSC'
self.cnf = cnf
self.url = "osc.udp://" + str(self.cnf.actuator_parm)
self.x = self.y = self.z = 0
self.oldx = self.oldy = self.oldz = 0
self.jx = self.jy = 0
self.joldx = self.joldy = 0
self.oscclient = liblo.Address(self.url)
self.video = self.cnf.rootwidget.oimage_wid
self.video.source = self.cnf.background_source
if not self.cnf.single_thread:
self.actuator_thread_run = True
self.sendrequest = Event()
self.requestthread = Thread(target=self.request_thread)
self.requestthread.start()
def transmit(self, x, y, z, jx, jy):
self.x = int(x)
self.y = int(y)
self.z = int(z)
self.jx = int(jx)
self.jy = int(jy)
if self.x != self.oldx or self.y != self.oldy or self.z != self.oldz or self.jx != self.joldx or self.jy != self.joldy:
if self.cnf.single_thread:
self.osc_output()
else:
self.sendrequest.set()
def start(self):
print('start actuator')
def stop(self):
self.x = self.y = self.z = 0
self.sendrequest.set()
print('stop actuator')
def shutdown(self):
self.actuator_thread_run = False
self.stop()
if not self.cnf.single_thread:
self.requestthread.join()
def video_play(self):
self.video.state = 'play'
def video_stop(self):
self.video.state = 'stop'
def request_thread(self):
while self.actuator_thread_run:
self.sendrequest.wait()
self.osc_output()
self.sendrequest.clear()
def osc_output(self):
if self.x != self.oldx:
liblo.send(self.oscclient, "/xaxis", self.x)
self.oldx = self.x
if self.y != self.oldy:
liblo.send(self.oscclient, "/yaxis", self.y)
self.oldy = self.y
if self.z != self.oldz:
liblo.send(self.oscclient, "/zaxis", self.z)
self.oldz = self.z
if self.jx != self.joldx:
liblo.send(self.oscclient, "/joyx", self.jx)
self.joldx = self.jx
if self.jy != self.joldy:
liblo.send(self.oscclient, "/joyy", self.jy)
self.joldy = self.jy
class STSpilot(object):
__slots__ = ['cnf', 'name', 'session', 'x', 'y', 'oldx', 'oldy', 'video', 'reqtimeout', 'sendrequest', 'url',
'joystick', 'videofeed', 'actuator_thread_run', 'requestpayload', 'requestthread']
def __init__(self, cnf):
self.name = 'STSpilot'
self.cnf = cnf
self.session = requests.Session()
self.x = 0
self.y = 0
self.oldx = 0
self.oldy = 0
self.video = self.cnf.rootwidget.oimage_wid
self.reqtimeout = 1.0
self.sendrequest = Event()
self.url = 'http://' + self.cnf.actuator_parm
self.joystick = ':5000/joystick'
self.videofeed = ':8080/stream/video.mjpeg'
self.actuator_thread_run = True
self.requestpayload = {'x': '0', 'y': '0'}
if not self.cnf.single_thread:
self.requestthread = Thread(target=self.request_thread)
self.requestthread.start()
def video_play(self):
self.video.unload()
self.video.source = self.url + self.videofeed
self.video.state = 'play'
def video_stop(self):
self.video.state = 'stop'
self.video.unload()
self.video.source = ''
def transmit(self, x, y, z, jx, jy):
self.x = int(x)
self.y = int(y)
if self.x != self.oldx or self.y != self.oldy:
payload = {'x': str(self.x), 'y': str(self.y)}
self.send_request(payload)
self.oldx = self.x
self.oldy = self.y
def start(self):
print('start actuator')
def stop(self):
self.send_request({'x': '0', 'y': '0'})
def shutdown(self):
self.actuator_thread_run = False
self.stop()
if not self.cnf.single_thread:
self.requestthread.join()
def send_request(self, payload):
self.requestpayload = payload
if self.cnf.single_thread:
self.http_request()
else:
self.sendrequest.set()
def http_request(self):
try:
r = self.session.get(
self.url + self.joystick, params=self.requestpayload, timeout=self.reqtimeout)
except:
print('HTTP Request to ' + self.url + ' timed out')
def request_thread(self):
while self.actuator_thread_run:
self.sendrequest.wait()
self.http_request()
self.sendrequest.clear()