-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
executable file
·582 lines (462 loc) · 13.8 KB
/
control.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/python3
###
#
# MIT License
#
# Copyright (c) 2016 Daniela Kilian, Lea Reisinger, Martin Drawitsch
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###
import ev3dev.ev3 as ev3
from time import sleep
from math import sqrt
from solve import Sudoku, solve
### GLOBAL VARIABLES
# Motor speeds
SPEED_X = 180
SPEED_Y = 55
SPEED_C = 40
# Factors that translate motor angles to fields
RX = 112
RY = 41
# Relative offsets that determine distance between color sensor and pen
OFF_X = 150
OFF_Y = -55
# Correction factors for physical biases
PEN_GX_CORR = 0 # Motor moving more in positive direction than in negative direction
# # (This effect magically disappeared overnight, therefore now set to 0).
PEN_DOWN_CORR = 1 # Compensate for slightly sloping table
# Distance between pen points in numbers
PX = RX / 5
PY = RY / 4
PEN_IDLE_POS = 0
PEN_WRITE_POS = 6
MWT = 0.1 # Motor wait time (check interval for check if a motor is running)
PEN_DOT_TIME = 0.1 # Time that is spent with pen being down (more time -> bigger dot for marker)
# Map color numbers to color names
COLORTABLE = {
0: 'white',
1: 'yellow',
2: 'dark green',
3: 'baby blue',
4: 'dark blue',
5: 'black',
6: 'red',
7: 'pink',
8: 'rose',
9: 'orange',
}
## Example of a sudoku with its solution
EXAMPLE_PUZZLE_STR = """
0 3 0 0 5 0 0 4 0
0 0 8 0 1 0 5 0 0
4 6 0 0 0 0 0 1 2
0 7 0 5 0 2 0 8 0
0 0 0 6 0 3 0 0 0
0 4 0 1 0 9 0 3 0
2 5 0 0 0 0 0 9 8
0 0 1 0 2 0 6 0 0
0 8 0 0 6 0 0 2 0""".strip()
EXAMPLE_SOLUTION_STR = """
1 3 7 2 5 6 8 4 9
9 2 8 3 1 4 5 6 7
4 6 5 8 9 7 3 1 2
6 7 3 5 4 2 9 8 1
8 1 9 6 7 3 2 5 4
5 4 2 1 8 9 7 3 6
2 5 6 7 3 1 4 9 8
3 9 1 4 2 8 6 7 5
7 8 4 9 6 5 1 2 3""".strip()
## Reference sudoku that we have printed out (for error checking)
REF_PUZZLE_STR = """
3 0 0 8 0 5 0 0 6
0 0 4 0 0 0 1 0 0
1 5 0 2 0 6 0 7 8
0 0 0 1 5 9 0 0 0
0 0 7 0 0 0 9 0 0
0 0 0 6 3 7 0 0 0
9 1 0 3 0 4 0 0 2
0 0 6 0 0 0 8 0 0
2 0 0 0 0 8 0 0 3""".strip()
REF_SOLUTION_STR = """
3 7 2 8 1 5 4 9 6
6 8 4 7 9 3 1 2 5
1 5 9 2 4 6 3 7 8
4 2 3 1 5 9 6 8 7
5 6 7 4 8 2 9 3 1
8 9 1 6 3 7 2 5 4
9 1 8 3 7 4 5 6 2
7 3 6 5 2 1 8 4 9
2 4 5 9 6 8 7 1 3""".strip()
### I/O INITIALIZATION
# Initialize motors
a = ev3.LargeMotor('outA') # Moves in y direction
b = ev3.LargeMotor('outB') # Moves in x direction
c = ev3.MediumMotor('outC') # Rotates pen holder ("c direction")
# Initialize sensor
csensor = ev3.ColorSensor()
csensor.mode = 'RGB-RAW'
### FUNCTIONS, SUBROUTINES
## MOTOR CONTROL
def reset():
"""
Reset every motor to its initial configuration. This does not move anything.
"""
a.reset()
b.reset()
c.reset()
a.position = 0
b.position = 0
c.position = 0
a.speed_sp = SPEED_Y
b.speed_sp = SPEED_X
c.speed_sp = SPEED_C
a.stop_action = 'brake'
b.stop_action = 'brake'
c.stop_action = 'brake'
def is_moving(check_stall=False):
"""
Check if any motor is currently running.
Necessary for blocking movement until the wanted position is reached.
"""
motor_states = a.state + b.state + c.state
if check_stall and 'stalled' in motor_states:
print('\nWarning: At least one motor is stalled. Check speed_sp attributes. States:')
print('A:', a.state)
print('B:', b.state)
print('C:', c.state)
return 'running' in motor_states
def pen_down():
"""
Move pen down to writing position.
"""
xmod = round(PEN_DOWN_CORR * b.position / (RX * 9))
c.run_to_abs_pos(position_sp=PEN_WRITE_POS+xmod)
sleep(MWT)
while is_moving():
sleep(MWT)
def pen_up():
"""
Move pen up to neutral position.
"""
c.run_to_abs_pos(position_sp=PEN_IDLE_POS)
sleep(MWT)
while is_moving():
sleep(MWT)
def pen_dot():
"""
Make a dot with the pen at the current position.
"""
pen_down()
sleep(PEN_DOT_TIME)
pen_up()
# HOW TO WRITE NUMBERS ON PAPER
def one():
pen_dot()
def two():
pen_dot()
mx(-PX)
pen_dot()
mx(PX)
def three():
pen_dot()
mx(-PX)
pen_dot()
mx(-PX)
pen_dot()
mx(2*PX)
def four():
two()
my(PY)
two()
def five():
three()
my(PY)
two()
def six():
three()
my(PY)
three()
def seven():
three()
my(PY)
three()
my(PY)
one()
def eight():
three()
my(PY)
three()
my(PY)
two()
def nine():
three()
my(PY)
three()
my(PY)
three()
def write_number(n):
"""
Make n dots at the current position.
The position should be adjusted before by pen_gfield().
"""
{
1: one,
2: two,
3: three,
4: four,
5: five,
6: six,
7: seven,
8: eight,
9: nine
}[n]()
def move(dx=0, dy=0):
"""
Turn motors b and a by (dx, dy) degrees.
"""
if dy != 0:
a.run_to_rel_pos(position_sp=dy)
if dx != 0:
b.run_to_rel_pos(position_sp=dx)
sleep(MWT)
while is_moving():
sleep(MWT)
def mx(dx=0):
move(dx=dx)
def my(dy=0):
move(dy=dy)
def mfield(nx=0, ny=0):
""" move (nx, ny) fields """
fy = ny*RY
fx = nx*RX
if fy != a.position:
a.run_to_rel_pos(position_sp=ny*RY)
if fx != b.position:
b.run_to_rel_pos(position_sp=nx*RX)
sleep(MWT)
while is_moving():
sleep(MWT)
def gfield(nx=0, ny=0):
""" Move color sensor to field (nx, ny) """
fy = ny*RY
fx = nx*RX
if fy != a.position:
a.run_to_abs_pos(position_sp=fy)
if fx != b.position:
b.run_to_abs_pos(position_sp=fx)
sleep(MWT)
while is_moving():
sleep(MWT)
def pen_gfield(nx=0, ny=0, off_x=OFF_X, off_y=OFF_Y):
"""
Move the pen above field (nx, ny) so we can begin writing a number in this field.
(off_x, off_y) is the relative offset between the color sensor and the pen (in motor angles).
"""
xmod = -round(PEN_GX_CORR * b.position / (RX * 9))
fy = ny*RY + off_y
fx = nx*RX + off_x + xmod
if fy != a.position:
a.run_to_abs_pos(position_sp=fy)
if fx != b.position:
b.run_to_abs_pos(position_sp=fx)
sleep(MWT)
while is_moving():
sleep(MWT)
def goto(x=0, y=0):
"""
Move motors to absolute position (x, y) (in angles, not fields).
"""
fy = y
fx = x
if fy != a.position:
a.run_to_abs_pos(position_sp=y)
if fx != b.position:
b.run_to_abs_pos(position_sp=x)
sleep(MWT)
while is_moving():
sleep(MWT)
def origin():
goto(0, 0)
## COLOR CONTROL
def calibrate_colors(filename='refcolors.txt'):
"""
Calibrate the color sensor, starting in the white space
in front of the yellow field on the calibration strip.
"""
refcolors = []
with open(filename, 'w') as f:
def save_color(index, print_info=True):
"""
Write current RGB value to the opened file f.
"""
# Write to file
print(csensor.value(0), csensor.value(1), csensor.value(2), file=f, flush=True)
if print_info: # Write to stdout
print(COLORTABLE[index], csensor.value(0), csensor.value(1), csensor.value(2), flush=True)
refcolors.append([csensor.value(0), csensor.value(1), csensor.value(2)])
# White
save_color(0)
my(44) # Move from white to first field in the color strip
# Yellow
save_color(1)
# Colors betwen 2 (dark green) and 9 (orange)
for i in range(8):
mfield(-1, 0) # Go to next field
save_color(i + 2)
my(73/42 * RY) # Move to first sudoku field
return refcolors
def getrefcolors(filename='refcolors.txt'):
"""
Read ordered reference color values from a file that was written in the calibration step (calibrate_colors()).
"""
with open(filename) as rf:
content = rf.read().splitlines()
refcolors = []
for line in content:
if not line.strip() == '':
refcolors.append([int(n) for n in line.split()])
return refcolors
def minus(x, y):
"""
Elementwise substraction of two lists of numbers (vectors).
"""
c = []
for a, b in zip(x, y):
c.append(a - b)
return c
def norm(x):
"""
Compute euclidian norm of a list of numbers (vector)
"""
return abs(sqrt(sum([a**2 for a in x])))
def dst(x, y):
"""
Compute euclidian distance of two lists of numbers (vectors).
"""
return norm(minus(x, y))
def nearest_neighbor(color, refcolors):
"""
Classify RGB value as a color number using a reference color list (refcolors)
and the k-nearest-neighbors algorithm (where k == 1).
"""
color_distances = []
for ref in refcolors:
# Store reference color with its euclidian distance to the currently examined color
color_distances.append([ref, dst(color, ref)])
color_distances.sort(key=lambda x: x[1]) # Sort by distance -> First element is the nearest neighbor.
return refcolors.index(color_distances[0][0]) # Return index (= number) of nearest color.
def getrgb():
"""
Output currently measured raw RGB value in the form [red, green, blue].
"""
return [csensor.value(0), csensor.value(1), csensor.value(2)]
def read_number():
"""
Read color value and classify it using one of the reference colors by finding the nearest neighbor.
"""
mcol = [csensor.value(0), csensor.value(1), csensor.value(2)]
return nearest_neighbor(mcol, getrefcolors())
def getcolorname():
return COLORTABLE[read_number()]
def scolw():
ev3.Sound.speak(getcolorname()).wait()
## HIGH-LEVEL SUDOKU SOLVING ROUTINES
def scan_sudoku(n=9, print_output=True):
"""
Scans the sudoku by moving to all fields, measuring their color values and classifying them as numbers.
Returns a string representation of the sudoku.
If n != 9, scanning will work in a smaller/larger square, but solving will be impossible of course.
"""
clist = []
clist2d = []
for y in range(n):
for x in range(n):
# Reverse scan direction in every odd line to reduce unnecessary movements
if y % 2 == 1:
gfield(n - x - 1, y)
else:
gfield(x, y)
current_number = read_number()
clist.append(current_number)
if print_output:
print(current_number)
print(getcolorname())
for y in range(n):
if y % 2 == 1:
clist2d.append(clist[y*n:(y+1)*n][::-1])
else:
clist2d.append(clist[y*n:(y+1)*n])
sudokustring = '\n'.join(' '.join([str(x) for x in l]) for l in clist2d)
return sudokustring
def check_ref_puzzle_str(puzzle, ref_puzzle=REF_PUZZLE_STR):
"""
Check if the read puzzle is the same as the saved reference puzzle.
"""
correct = True
puzzle_compact = str(puzzle).replace('-', '0').replace(' ', '')
ref_puzzle_compact = str(ref_puzzle).replace('-', '0').replace(' ', '')
for i in range(len(puzzle_compact)):
if puzzle_compact[i] != ref_puzzle_compact[i]:
print('Mismatch at row', i // 9, 'row', i % 9)
print('Read', puzzle_compact[i], COLORTABLE[int(puzzle_compact[i])])
print('Expected', ref_puzzle_compact[i], COLORTABLE[int(ref_puzzle_compact[i])])
correct = False
if correct:
print('Everything OK.')
return correct
def write_solution(puzzle=Sudoku(EXAMPLE_PUZZLE_STR), solution=Sudoku(EXAMPLE_SOLUTION_STR)):
"""
Writes a solution down on the sheet of paper by making dots representing numbers.
"puzzle" and "solution" are both either strings or solve.Sudoku objects (the latter ones are
automatically converted to strings).
The "puzzle" parameter has to be passed to show where the unknown numbers were.
"""
# Create compact rows of the sudoku representation. Their elements are always castable to int.
puzzle_rows = [row.replace('-', '0').replace(' ', '') for row in str(puzzle).splitlines()]
solution_rows = [row.replace(' ', '') for row in str(solution).splitlines()]
unknown_indices = [[i for i, j in enumerate(row) if int(j) == 0] for row in puzzle_rows]
for y, x_values in enumerate(unknown_indices):
for x in x_values:
pen_gfield(x, y)
write_number(int(solution_rows[y][x]))
# Shortcuts for interactive use
o = origin
x = mx
y = my
reset() # This needs to stay here to initialize motors with speeds
if __name__ == '__main__':
calibrate_colors()
reset()
puzzle_raw = scan_sudoku()
puzzle = Sudoku(puzzle_raw)
with open('sudoku.txt', 'w') as f:
print(puzzle, file=f)
solution = solve(puzzle)
puzzle = str(puzzle)
solution = str(solution)
print(puzzle)
print('\n Solution:\n')
print(solution)
if not check_ref_puzzle_str(puzzle):
print('Sudoku could not be solved :(')
else:
write_solution(puzzle, solution)
my(-500) # push sheet out