-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotter.py
448 lines (408 loc) · 11.5 KB
/
plotter.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python
'''
This module contains printer/plotter functions for the EV3 components.
I/O ports:
- A: Large motor, plotter rail
- B: Medium motor, plotter head
- D: Large motor, roller
- 1: Touch sensor, plotter rail reset sensor
- 2: Color sensor, paper feed detector
- /dev/video0: USB webcam
'''
import ev3dev.ev3 as ev3
import time
# hardware configuration
PLOTTER_RAIL_MOTOR = ev3.LargeMotor('A')
PLOTTER_HEAD_MOTOR = ev3.MediumMotor('B')
ROLLER_MOTOR = ev3.LargeMotor('D')
PLOTTER_RAIL_SENSOR = ev3.TouchSensor('1')
PAPER_FEED_SENSOR = ev3.ColorSensor('2')
SCREEN = ev3.Screen()
BUTTON = ev3.Button()
SPEAKER = ev3.Sound
WEBCAM_NUMBER = 0 # USB webcam
PAPER_FEED_EMPTY_COLOR = 1 # black
MAX_X = 350
MAX_Y = 16000
PLOTTER_HEAD_DOWN_DELAY = 0.450
# color sensor values:
# - 0: No color
# - 1: Black
# - 2: Blue
# - 3: Green
# - 4: Yellow
# - 5: Red
# - 6: White
# - 7: Brown
def waitMotor(motor, breakOnStall=False, stallSpeed=0):
'''
Wait until the specified motor stops running.
If breakOnStall is True, this procedure will also end if the motor is stalled.
Note: motor.state should have contained 'stalled' if the motor stalled, but this had not been implemented yet,
so we use a speed-based stall detection system.
in the December 2015 release of ev3dev.
'''
while 'running' in motor.state:
if breakOnStall and (abs(motor.speed) <= abs(stallSpeed)):
break
pass
def waitSensor(sensor, value, negate=False):
'''
Wait until the specified sensor's value equals to the specified value.
If negate is True, wait until the values are not equal.
'''
if negate:
while sensor.value() == value:
pass
else:
while sensor.value() != value:
pass
def waitButton(buttonType='any', mode='pressed'):
'''
Wait until the specified button is up/down/pressed (down then up).
Possible modes: up, down, pressed
Possible buttonTypes: any, backspace, enter, up, down, left, right
'''
if buttonType == 'any':
if (mode == 'down') or (mode == 'pressed'):
while not BUTTON.any():
pass
if (mode == 'up') or (mode == 'pressed'):
while BUTTON.any():
pass
elif buttonType == 'backspace':
if (mode == 'down') or (mode == 'pressed'):
while not BUTTON.backspace:
pass
if (mode == 'up') or (mode == 'pressed'):
while BUTTON.backspace:
pass
elif buttonType == 'enter':
if (mode == 'down') or (mode == 'pressed'):
while not BUTTON.enter:
pass
if (mode == 'up') or (mode == 'pressed'):
while BUTTON.enter:
pass
elif buttonType == 'up':
if (mode == 'down') or (mode == 'pressed'):
while not BUTTON.up:
pass
if (mode == 'up') or (mode == 'pressed'):
while BUTTON.up:
pass
elif buttonType == 'down':
if (mode == 'down') or (mode == 'pressed'):
while not BUTTON.down:
pass
if (mode == 'up') or (mode == 'pressed'):
while BUTTON.down:
pass
elif buttonType == 'left':
if (mode == 'down') or (mode == 'pressed'):
while not BUTTON.left:
pass
if (mode == 'up') or (mode == 'pressed'):
while BUTTON.left:
pass
elif buttonType == 'right':
if (mode == 'down') or (mode == 'pressed'):
while not BUTTON.right:
pass
if (mode == 'up') or (mode == 'pressed'):
while BUTTON.right:
pass
def plotterHeadUp(halfRaise=True):
'''Raises the plotter head. If halfRaise is True, the plotter head will only be lifted a bit to reduce time needed.'''
if halfRaise:
PLOTTER_HEAD_MOTOR.run_timed(time_sp=400, duty_cycle_sp=-50)
else:
PLOTTER_HEAD_MOTOR.run_timed(time_sp=800, duty_cycle_sp=-50)
waitMotor(PLOTTER_HEAD_MOTOR, breakOnStall=True, stallSpeed=30)
time.sleep(PLOTTER_HEAD_DOWN_DELAY)
def plotterHeadDown():
'''Presses the plotter head down.'''
PLOTTER_HEAD_MOTOR.run_timed(time_sp=800, duty_cycle_sp=50)
waitMotor(PLOTTER_HEAD_MOTOR, breakOnStall=True, stallSpeed=30)
time.sleep(PLOTTER_HEAD_DOWN_DELAY)
def reset():
'''Resets plotter rail, head and roller positions.'''
SCREEN.clear()
SCREEN.draw.text((60, 60), 'Preparing...')
SCREEN.update()
plotterHeadUp(halfRaise=False)
PLOTTER_RAIL_MOTOR.run_forever(duty_cycle_sp=-30)
waitSensor(PLOTTER_RAIL_SENSOR, 1)
PLOTTER_RAIL_MOTOR.stop(stop_command='coast')
PLOTTER_RAIL_MOTOR.reset()
PLOTTER_RAIL_MOTOR.stop_command = 'brake'
ROLLER_MOTOR.stop_command = 'brake'
PAPER_FEED_SENSOR.mode = 'COL-COLOR'
if PAPER_FEED_SENSOR.value() != PAPER_FEED_EMPTY_COLOR:
ROLLER_MOTOR.run_forever(duty_cycle_sp=100)
waitSensor(PAPER_FEED_SENSOR, PAPER_FEED_EMPTY_COLOR)
ROLLER_MOTOR.stop()
SCREEN.clear()
SCREEN.draw.text((45, 60), 'Reset complete')
SCREEN.update()
def feedPaper():
'''Positions paper, resets roller motor.'''
SCREEN.clear()
SCREEN.draw.text((45, 60), 'Feeding paper...')
SCREEN.update()
ROLLER_MOTOR.stop_command = 'brake'
ROLLER_MOTOR.run_forever(duty_cycle_sp=-100)
waitSensor(PAPER_FEED_SENSOR, PAPER_FEED_EMPTY_COLOR, negate=True)
ROLLER_MOTOR.run_forever(duty_cycle_sp=100)
waitSensor(PAPER_FEED_SENSOR, PAPER_FEED_EMPTY_COLOR)
ROLLER_MOTOR.run_to_rel_pos(position_sp=1500, duty_cycle_sp=100)
waitMotor(ROLLER_MOTOR)
ROLLER_MOTOR.reset()
ROLLER_MOTOR.stop_command = 'brake'
SCREEN.clear()
SCREEN.draw.text((35, 60), 'Paper in position')
SCREEN.update()
def unfeedPaper():
'''Cancel operation, return paper to starting position.'''
ROLLER_MOTOR.run_forever(duty_cycle_sp=100)
waitSensor(PAPER_FEED_SENSOR, PAPER_FEED_EMPTY_COLOR)
ROLLER_MOTOR.run_to_rel_pos(position_sp=3500, duty_cycle_sp=100)
waitMotor(ROLLER_MOTOR)
def beep(beepType='ok'):
'''Emit sounds according to beepType: ok, starting, ready, warning, error, done.'''
if beepType == 'ok':
SPEAKER.tone([(3000, 200, 200)]).wait()
elif beepType == 'starting':
SPEAKER.tone([(2000, 200, 200)]).wait()
elif beepType == 'ready':
SPEAKER.tone([(2000, 70, 30), (3000, 200, 0)]).wait()
elif beepType == 'warning':
SPEAKER.tone([(2000, 70, 30), (2000, 70, 30)]).wait()
elif beepType == 'error':
SPEAKER.tone([(2000, 70, 30), (2000, 70, 30), (2000, 70, 30)]).wait()
elif beepType == 'done':
SPEAKER.tone([(2000, 70, 30), (3000, 70, 30), (4000, 200, 0)]).wait()
def gotoXY(x, y, bcm=True):
'''
Positions the plotter head at the specified coordinate.
(0,0) is at the top-left corner of the paper.
Paper is fed bottom-first.
'''
x = MAX_X - x
y = MAX_Y - y
if x < 0:
x = 0
if x > MAX_X:
x = MAX_X
if y < 0 :
y = 0
if y > MAX_Y:
y = MAX_Y
dx = abs(x - PLOTTER_RAIL_MOTOR.position)
dy = abs(y + ROLLER_MOTOR.position)
if bcm:
if(y > -ROLLER_MOTOR.position):
bcmy = 200
else:
bcmy = -200
ROLLER_MOTOR.position += bcmy
if dy > 0:
ROLLER_MOTOR.run_to_abs_pos(position_sp=-y, duty_cycle_sp=100)
if dx > 0:
PLOTTER_RAIL_MOTOR.run_to_abs_pos(position_sp=x, duty_cycle_sp=30)
if dy > 0:
waitMotor(ROLLER_MOTOR)
if dx > 0:
waitMotor(PLOTTER_RAIL_MOTOR)
time.sleep(0.5)
def convertCameraCoordinates(cameraX, cameraY):
'''Converts camera coordinates (in pixels) to plotter coordinates (in degrees).'''
pcx = round((cameraX - 86) * 0.875)
pcy = round(((cameraY - 30) * 16.560) + 1400)
return (pcx, pcy)
def drawDigit(digit, x, y, width, height):
'''Draws a digit in the specified position, with the specified size.'''
plotterHeadUp()
if digit == 0:
gotoXY(x, y)
plotterHeadDown()
gotoXY(x, y+height)
gotoXY(x+width, y+height)
gotoXY(x+width, y)
gotoXY(x, y)
elif digit == 1:
gotoXY(x+width/2, y)
plotterHeadDown()
gotoXY(x+width/2, y+height)
elif digit == 2:
gotoXY(x, y)
plotterHeadDown()
gotoXY(x+width, y)
gotoXY(x+width, y+height/2)
gotoXY(x, y+height/2)
gotoXY(x, y+height)
gotoXY(x+width, y+height)
elif digit == 3:
gotoXY(x, y)
plotterHeadDown()
gotoXY(x+width, y)
gotoXY(x+width, y+height/2)
gotoXY(x, y+height/2)
plotterHeadUp()
gotoXY(x+width, y+height/2)
plotterHeadDown()
gotoXY(x+width, y+height)
gotoXY(x, y+height)
elif digit == 4:
gotoXY(x, y)
plotterHeadDown()
gotoXY(x, y+height/2)
gotoXY(x+width, y+height/2)
plotterHeadUp()
gotoXY(x+width, y)
plotterHeadDown()
gotoXY(x+width, y+height)
elif digit == 5:
gotoXY(x+width, y)
plotterHeadDown()
gotoXY(x, y)
gotoXY(x, y+height/2)
gotoXY(x+width, y+height/2)
gotoXY(x+width, y+height)
gotoXY(x, y+height)
elif digit == 6:
gotoXY(x+width, y)
plotterHeadDown()
gotoXY(x, y)
gotoXY(x, y+height)
gotoXY(x+width, y+height)
gotoXY(x+width, y+height/2)
gotoXY(x, y+height/2)
elif digit == 7:
gotoXY(x, y)
plotterHeadDown()
gotoXY(x+width, y)
gotoXY(x+width, y+height)
elif digit == 8:
gotoXY(x, y)
plotterHeadDown()
gotoXY(x, y+height)
gotoXY(x+width, y+height)
gotoXY(x+width, y)
gotoXY(x, y)
plotterHeadUp()
gotoXY(x, y+height/2)
plotterHeadDown()
gotoXY(x+width, y+height/2)
elif digit == 9:
gotoXY(x+width, y+height/2)
plotterHeadDown()
gotoXY(x, y+height/2)
gotoXY(x, y)
gotoXY(x+width, y)
gotoXY(x+width, y+height)
gotoXY(x, y+height)
plotterHeadUp()
def printGrid(grid, x, y, width, height):
'''Prints an image as contained on the 0-1 grid.'''
row = len(grid)
if row > 0:
col = len(grid[0])
dx = int(width/col)
dy = int(height/row)
plotterHeadUp()
gotoXY(x-20, y-100) # backlash compensation
gotoXY(x, y)
for i in range(row):
gotoXY(x-20, y+i*dy, bcm=False)
segments = []
cstart = 0
prev = 0
for j in range(col):
if (prev == 0) and (grid[i][j] == 1): # start of a segment
cstart = j
elif (prev == 1) and (grid[i][j] == 0): # end of a segment
segments.append([cstart, j])
prev = grid[i][j]
if (prev == 1):
segments.append([cstart, col])
for j in range(len(segments)):
segStartX = x+segments[j][0]*dx
segEndX = x+segments[j][1]*dx
gotoXY(segStartX, y+i*dy, bcm=False)
plotterHeadDown()
gotoXY(segEndX, y+i*dy, bcm=False)
plotterHeadUp()
gotoXY(x+width+10, y+i*dy, bcm=False)
gotoXY(x+width+10, y+(i+1)*dy, bcm=False)
def sudokuToGrid(sudoku, mask):
'''Converts a sudoku puzzle to grid format. Only convert digits which corresponding mask is 0.'''
grid = [[0 for j in range(44)] for i in range(61)]
for i in range(9):
for j in range(9):
if mask[i][j] == 0:
cr = i*7
cc = j*5
if sudoku[i][j] == 1:
for k in range(5):
grid[cr+k][cc+1] = 1
elif sudoku[i][j] == 2:
for k in range(3):
grid[cr][cc+k] = 1
grid[cr+2][cc+k] = 1
grid[cr+4][cc+k] = 1
grid[cr+3][cc] = 1
grid[cr+1][cc+3] = 1
elif sudoku[i][j] == 3:
for k in range(3):
grid[cr][cc+k] = 1
grid[cr+2][cc+k] = 1
grid[cr+4][cc+k] = 1
grid[cr+3][cc+3] = 1
grid[cr+1][cc+3] = 1
elif sudoku[i][j] == 4:
for k in range(3):
grid[cr+k][cc] = 1
for k in range(5):
grid[cr+k][cc+2] = 1
grid[cr+2][cc+1] = 1
elif sudoku[i][j] == 5:
for k in range(3):
grid[cr][cc+k] = 1
grid[cr+2][cc+k] = 1
grid[cr+4][cc+k] = 1
grid[cr+3][cc+3] = 1
grid[cr+1][cc] = 1
elif sudoku[i][j] == 6:
for k in range(3):
grid[cr][cc+k] = 1
grid[cr+2][cc+k] = 1
grid[cr+4][cc+k] = 1
grid[cr+3][cc] = 1
grid[cr+3][cc+3] = 1
grid[cr+1][cc] = 1
elif sudoku[i][j] == 7:
for k in range(3):
grid[cr+k][cc+2] = 1
grid[cr+3][cc+1] = 1
grid[cr+4][cc+1] = 1
grid[cr][cc] = 1
grid[cr][cc+1] = 1
elif sudoku[i][j] == 8:
for k in range(3):
grid[cr][cc+k] = 1
grid[cr+2][cc+k] = 1
grid[cr+4][cc+k] = 1
grid[cr+3][cc] = 1
grid[cr+1][cc] = 1
grid[cr+3][cc+3] = 1
grid[cr+1][cc+3] = 1
elif sudoku[i][j] == 9:
for k in range(3):
grid[cr][cc+k] = 1
grid[cr+2][cc+k] = 1
grid[cr+4][cc+k] = 1
grid[cr+1][cc] = 1
grid[cr+3][cc+3] = 1
grid[cr+1][cc+3] = 1
return grid