-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSCApp.py
executable file
·67 lines (59 loc) · 1.76 KB
/
OSCApp.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
import sys
import osc
CLOCKWISE = True
COUNTER_CLOCKWISE = False
BOARD_ADDRESS = '192.168.1.8'
BOARD_PORT = 10000
laser_address = 0
servos = ({
'axis': 'y',
'address': 0,
'start': -200,
'end': 800,
'speed': 200,
'direction': COUNTER_CLOCKWISE,
},{
'axis': 'x',
'address': 1,
'start': -500,
'end': 1200,
'speed': 200,
'direction': CLOCKWISE,
})
class OSCApp():
def __init__(self):
osc.init()
bundle = osc.createBundle()
self.set_laser(False)
for servo in servos:
servo['range'] = self._range(servo)
osc.appendToBundle(
bundle,
"/digitalout/%i/speed" % servo['address'],
[servo['speed']]
)
osc.sendBundle(bundle, BOARD_ADDRESS, BOARD_PORT)
@staticmethod
def _range(servo):
return lambda percent: int(servo['start'] - (servo['start'] - servo['end']) * percent)
def update_servos(self, x, y):
bundle = osc.createBundle()
for servo in servos:
p = x if servo['axis'] == 'x' else y
osc.appendToBundle(
bundle,
"/servo/%i/position" % servo['address'],
[servo['range'](1 - p if servo['direction'] else p)]
)
osc.sendBundle(bundle, BOARD_ADDRESS, BOARD_PORT)
return {
'x': x,
'y': y,
}
def toggle_laser(self):
return self.set_laser(not self.laser_state)
def set_laser(self, state):
self.laser_state = state
int_state = 1 if self.laser_state else 0
osc.sendMsg("/digitalout/%i/value" % laser_address, [int_state], BOARD_ADDRESS, BOARD_PORT)
return self.laser_state