-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathspindle_camera.py
275 lines (233 loc) · 8.5 KB
/
spindle_camera.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
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.uix.behaviors import ToggleButtonBehavior, ButtonBehavior
from kivy.properties import ListProperty, BooleanProperty, NumericProperty
from kivy.logger import Logger
import time
import subprocess
import threading
import select
Builder.load_string('''
<IButton@IconButton>:
canvas.before:
Color:
rgb: [0.4, 0.4, 0.4] if self.state == 'normal' else [0.5, 0, 0]
Ellipse:
pos: [self.center[0] - self.height / 2, self.center[1] - self.height / 2]
size: self.height, self.height
size_hint_y: None
height: 40
<ITogButton@IconToggleButton>:
canvas.before:
Color:
rgb: [0.4, 0.4, 0.4] if self.state == 'normal' else [0.5, 0, 0]
Ellipse:
pos: [self.center[0] - self.height / 2, self.center[1] - self.height / 2]
size: self.height, self.height
size_hint_y: None
height: 40
<SpindleCamera>:
on_enter: camera.play = True
on_leave: camera.play = False
FloatLayout:
BoxLayout:
orientation: 'horizontal'
Camera:
# size_hint: None, None
# size: 640, 480
canvas.after:
Color:
rgb: 1, 0, 0
Line:
points: [self.center_x, 0, self.center_x, self.height]
width: 1
cap: 'none'
joint: 'none'
Line:
points: [0, self.center_y, self.width, self.center_y]
width: 1
cap: 'none'
joint: 'none'
Line:
circle:
(self.center_x, self.center_y, root.circle_size)
id: camera
resolution: (640, 480)
play: False
BoxLayout:
size_hint: None, 1.0
width: 44
orientation: 'vertical'
spacing: 8
padding: 4
Label:
text: "Circle"
size_hint_y: None
size: self.texture_size[0], self.texture_size[1]
Slider:
orientation: 'vertical'
min: 1
max: 240
value: root.circle_size
on_value: root.circle_size = self.value
ITogButton:
source: "img/cross-mouse.png"
on_state: root.jog = self.state == 'down'
ITogButton:
source: "img/invert_jog.png"
on_state: root.invert_jog = self.state == 'down'
IButton:
source: "img/set_zero.png"
on_press: root.setzero()
IButton:
source: "img/screenshot.png"
on_press: root.capture()
IButton:
source: "img/back.png"
on_press: root.manager.current = 'main'
Label:
text: '' if app.wpos == None else "{:1.3f},{:1.3f}".format(app.wpos[0], app.wpos[1])
size_hint: None, None
size: self.texture_size
pos_hint: {'top': 1.0, 'right': 0.5}
''')
class IconToggleButton(ToggleButtonBehavior, Image):
pass
class IconButton(ButtonBehavior, Image):
pass
class SpindleCamera(Screen):
invert_jog = BooleanProperty(False)
jog = BooleanProperty(False)
circle_size = NumericProperty(10)
def __init__(self, **kwargs):
super(SpindleCamera, self).__init__(**kwargs)
self.app = App.get_running_app()
self.nfingers = 0
self.z_jog = False
def setzero(self):
self.app.comms.write('G10 L20 P0 X0 Y0\n')
def capture(self):
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
def on_touch_down(self, touch):
if self.jog and self.ids.camera.collide_point(touch.x, touch.y):
# if within the camera window
touch.grab(self)
self.nfingers += 1
touch.ud["n"] = self.nfingers
if self.nfingers == 4:
self.z_jog = True
return True
return super(SpindleCamera, self).on_touch_down(touch)
def on_touch_move(self, touch):
if touch.grab_current is not self:
return super(SpindleCamera, self).on_touch_move(touch)
if self.nfingers >= 1 and not self.z_jog and touch.ud["n"] == 1:
# we only track the first finger that touched
n = self.nfingers
m = 0.001 * 10**(n - 1) # 1 finger moves 0.001, 2 moves 0.01, 3 moves .1
dx = 0
dy = 0
if abs(touch.dx) > 0:
dx = m * touch.dx
if abs(touch.dy) > 0:
dy = m * touch.dy
if dx != 0 or dy != 0:
if self.invert_jog:
dx = -dx
dy = -dy
self.app.comms.write("$J X{} Y{}\n".format(dx, dy))
elif self.z_jog and touch.ud["n"] == 1:
# we move Z to focus
dy = touch.dy
if dy != 0:
self.app.comms.write("$J Z{}\n".format(0.01 * dy))
return True
def on_touch_up(self, touch):
if touch.grab_current is self:
touch.ungrab(self)
self.nfingers -= 1
if self.nfingers == 0:
self.z_jog = False
return True
return super(SpindleCamera, self).on_touch_up(touch)
@staticmethod
def run_standalone(app):
t = threading.Thread(target=SpindleCamera._standalone_thread, daemon=True, args=(app, ))
t.start()
@staticmethod
def _standalone_thread(app):
# we run it as a separate program so it is in its own window
# and we capture stdout to pass onto smoothie
# print("SpindleCamera: standalone thread started")
with subprocess.Popen(['python3', '-u', 'spindle_camera.py'], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True) as p:
while True:
s = p.stdout.readline()
if not s:
break
if len(s.rstrip()) > 0:
# print("SpindleCamera: {}".format(s))
app.comms.write('{}'.format(s))
# print("SpindleCamera: standalone thread exited: {}".format(p.returncode))
if __name__ == '__main__':
import sys
Builder.load_string('''
<StartScreen>:
BoxLayout:
Button:
id: play_but
text: 'Play'
on_press: app.play()
size_hint_y: None
height: 40
Button:
text: 'Exit'
on_press: app.stop()
size_hint_y: None
height: 40
<ExitScreen>:
on_enter: app.stop()
''')
class StartScreen(Screen):
pass
class ExitScreen(Screen):
pass
class CommsDummy:
def write(self, msg):
try:
print(msg)
sys.stdout.flush()
except Exception as err:
sys.stderr.write("CommsDummy: write exception: {}\n".format(err))
class StandaloneSpindleCamera(App):
comms = CommsDummy()
wpos = None
def __init__(self, **kwargs):
super(StandaloneSpindleCamera, self).__init__(**kwargs)
self.test = False
self.running = False
if len(sys.argv) > 1:
if sys.argv[1] == 'd':
self.test = True
def build(self):
# Window.size = (800, 480)
self.title = 'Spindle Camera'
self.sm = ScreenManager()
self.sc = SpindleCamera(name='spindle camera')
self.sm.add_widget(self.sc)
if not self.test:
self.ecr = ExitScreen(name='main')
self.sm.add_widget(self.ecr)
self.sm.current = 'spindle camera'
else:
self.scr = StartScreen(name='main')
self.sm.add_widget(self.scr)
self.sm.current = 'main'
return self.sm
def play(self):
self.sm.current = 'spindle camera'
StandaloneSpindleCamera().run()