-
Notifications
You must be signed in to change notification settings - Fork 3
/
jbrik.py
98 lines (80 loc) · 3.29 KB
/
jbrik.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
import time # import the time library for the sleep function
import sys
from utils import log_utils
from tracker_core import tracker
from tracker_core import resolver
from solver_core import jbrik_solver as solver
from motor_core import jbrik_motor as motor
_PicRotCount = 0 # 0 for no rotation, 1 for 2 pics - 180 rotation, 3 for one pic from all 4 directions
_DebugSteps = False
_TruingGrab = True
# take pictures
def _photo_face_rotations(facenum, cuber):
if _PicRotCount == 0:
if _TruingGrab:
cuber.grab_cube()
cuber.release_cube()
tracker.jbrick_tracker.photo_face(facenum, 0)
else:
for rotnum in range(0, _PicRotCount + 1):
if _TruingGrab:
cuber.grab_cube()
cuber.release_cube()
tracker.jbrick_tracker.photo_face(facenum, rotnum)
if rotnum > -1:
if _PicRotCount == 1:
log_utils.log("Rotate 180 CW")
cuber.rotate_cube(2)
else:
log_utils.log("Rotate 90 CW")
cuber.rotate_cube(1)
log_utils.log("Rotation pics for face: " + facenum.__str__() + " complete.")
# load picture to map
def _resolve_cubestate():
cubestatestr = ""
for facenum in range(1, 7):
# Convert the photo of the face into a map of rgb values
facemap = tracker.jbrick_tracker.convert_face_pics_to_rgb_facemap(facenum, _PicRotCount)
# convert the map of face rgb values to cubeStateStr
cubestatestr += resolver.jbrik_resolver.resolve_colors(facemap, _PicRotCount)
return cubestatestr
def _photo_all_faces(cuber):
# Photo inline cube faces
for facenum in range(1, 7):
log_utils.log("Flip to facenum: " + facenum.__str__())
cuber.flip_to_facenumup(facenum, True)
_photo_face_rotations(facenum, cuber)
def _run_solve_movements(solvemap, cuber):
for phase in range(1, 8):
log_utils.log("Performing movement ops for phase: " + phase.__str__())
solveoplist = solvemap[phase]
motoroplist = _convert_solve_movements_to_motor_movements(solveoplist)
log_utils.log("Converted solve op list to motor op list:\nsolveoplist: " + solveoplist.__str__()
+ "\nmotoroplist: " + motoroplist.__str__())
cuber.perform_motor_ops_for_phase(motoroplist)
if _DebugSteps:
raw_input("\nPhase: " + phase.__str__() + " complete, Press Enter to continue...\n")
def _convert_solve_movements_to_motor_movements(solveroplist):
motoroplist = []
for solveop in solveroplist:
motorop = motor.convert_solver_op_to_motor_op(solveop)
motoroplist.append(motorop)
return motoroplist
Cuber = None
try:
# initialize the solver machine
Cuber = motor.JbrikMotorLib(_TruingGrab)
# take photos of all faces
_photo_all_faces(Cuber)
# Load photos into color map and covert to cubeStateString
CubeStateStr = _resolve_cubestate()
log_utils.log("\n\nInitial cube state: " + CubeStateStr)
# Solve cube
SolveMap = solver.solve_cube(CubeStateStr)
if _DebugSteps:
raw_input("\nSolution determined, Press Enter to continue...\n")
# Run movement commands on cube
_run_solve_movements(SolveMap, Cuber)
finally:
Cuber.release_cube()
Cuber.shutdown()