-
Notifications
You must be signed in to change notification settings - Fork 1
/
rubik_solver.ROBOTC
2030 lines (1833 loc) · 52.6 KB
/
rubik_solver.ROBOTC
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Wiring:
1 Color sensor
2 Ultrasonic sensor
A Center motor
B Tilter arm motor
C Color sensor motor
*/
#define LEFTFACE 0
#define FRONTFACE 1
#define RIGHTFACE 2
#define BACKFACE 3
#define UPPERFACE 4
#define DOWNFACE 5
#define UPPERLEFT 0
#define UPPERMID 1
#define UPPERRIGHT 2
#define MIDLEFT 3
#define CENTER 4
#define MIDRIGHT 5
#define DOWNLEFT 6
#define DOWNMID 7
#define DOWNRIGHT 8
#define LEFTFACE_UPPERLEFT 0
#define LEFTFACE_UPPERMID 1
#define LEFTFACE_UPPERRIGHT 2
#define LEFTFACE_MIDLEFT 3
#define LEFTFACE_CENTER 4
#define LEFTFACE_MIDRIGHT 5
#define LEFTFACE_DOWNLEFT 6
#define LEFTFACE_DOWNMID 7
#define LEFTFACE_DOWNRIGHT 8
#define FRONTFACE_UPPERLEFT 9
#define FRONTFACE_UPPERMID 10
#define FRONTFACE_UPPERRIGHT 11
#define FRONTFACE_MIDLEFT 12
#define FRONTFACE_CENTER 13
#define FRONTFACE_MIDRIGHT 14
#define FRONTFACE_DOWNLEFT 15
#define FRONTFACE_DOWNMID 16
#define FRONTFACE_DOWNRIGHT 17
#define RIGHTFACE_UPPERLEFT 18
#define RIGHTFACE_UPPERMID 19
#define RIGHTFACE_UPPERRIGHT 20
#define RIGHTFACE_MIDLEFT 21
#define RIGHTFACE_CENTER 22
#define RIGHTFACE_MIDRIGHT 23
#define RIGHTFACE_DOWNLEFT 24
#define RIGHTFACE_DOWNMID 25
#define RIGHTFACE_DOWNRIGHT 26
#define BACKFACE_UPPERLEFT 27
#define BACKFACE_UPPERMID 28
#define BACKFACE_UPPERRIGHT 29
#define BACKFACE_MIDLEFT 30
#define BACKFACE_CENTER 31
#define BACKFACE_MIDRIGHT 32
#define BACKFACE_DOWNLEFT 33
#define BACKFACE_DOWNMID 34
#define BACKFACE_DOWNRIGHT 35
#define UPPERFACE_UPPERLEFT 36
#define UPPERFACE_UPPERMID 37
#define UPPERFACE_UPPERRIGHT 38
#define UPPERFACE_MIDLEFT 39
#define UPPERFACE_CENTER 40
#define UPPERFACE_MIDRIGHT 41
#define UPPERFACE_DOWNLEFT 42
#define UPPERFACE_DOWNMID 43
#define UPPERFACE_DOWNRIGHT 44
#define DOWNFACE_UPPERLEFT 45
#define DOWNFACE_UPPERMID 46
#define DOWNFACE_UPPERRIGHT 47
#define DOWNFACE_MIDLEFT 48
#define DOWNFACE_CENTER 49
#define DOWNFACE_MIDRIGHT 50
#define DOWNFACE_DOWNLEFT 51
#define DOWNFACE_DOWNMID 52
#define DOWNFACE_DOWNRIGHT 53
#define SENSORPOS_HOME 50
#define SENSORPOS_CENTER 150
#define SENSORPOS_CORNERS 177
#define SENSORPOS_EDGES 172
#define SENSORPOS_LASTEDGE 130
#define OFFSET_EDGE 35
#define OFFSET_LASTEDGE 200
#define TILTREST 800 // Time (ms) to rest before return tilterarm
#define SCANSPEED 40
#define MAXMOVES 300
#define BT_CONN 2
#define INBOX 5
#define OUTBOX 1
#define REMOTE_MAXTIME 10000
#define REMOTESOLVE_OK 0
#define REMOTESOLVE_ERROR 1
#define REMOTESOLVE_ABSENT 2
int staticOrientations[]={
LEFTFACE,FRONTFACE,RIGHTFACE,BACKFACE,UPPERFACE,DOWNFACE,
FRONTFACE,RIGHTFACE,BACKFACE,LEFTFACE,UPPERFACE,DOWNFACE,
RIGHTFACE,BACKFACE,LEFTFACE,FRONTFACE,UPPERFACE,DOWNFACE,
BACKFACE,LEFTFACE,FRONTFACE,RIGHTFACE,UPPERFACE,DOWNFACE,
DOWNFACE,FRONTFACE,UPPERFACE,BACKFACE,LEFTFACE,RIGHTFACE,
FRONTFACE,UPPERFACE,BACKFACE,DOWNFACE,LEFTFACE,RIGHTFACE,
UPPERFACE,BACKFACE,DOWNFACE,FRONTFACE,LEFTFACE,RIGHTFACE,
BACKFACE,DOWNFACE,FRONTFACE,UPPERFACE,LEFTFACE,RIGHTFACE,
LEFTFACE,DOWNFACE,RIGHTFACE,UPPERFACE,FRONTFACE,BACKFACE,
DOWNFACE,RIGHTFACE,UPPERFACE,LEFTFACE,FRONTFACE,BACKFACE,
RIGHTFACE,UPPERFACE,LEFTFACE,DOWNFACE,FRONTFACE,BACKFACE,
UPPERFACE,LEFTFACE,DOWNFACE,RIGHTFACE,FRONTFACE,BACKFACE,
FRONTFACE,DOWNFACE,BACKFACE,UPPERFACE,RIGHTFACE,LEFTFACE,
DOWNFACE,BACKFACE,UPPERFACE,FRONTFACE,RIGHTFACE,LEFTFACE,
BACKFACE,UPPERFACE,FRONTFACE,DOWNFACE,RIGHTFACE,LEFTFACE,
UPPERFACE,FRONTFACE,DOWNFACE,BACKFACE,RIGHTFACE,LEFTFACE,
LEFTFACE,UPPERFACE,RIGHTFACE,DOWNFACE,BACKFACE,FRONTFACE,
UPPERFACE,RIGHTFACE,DOWNFACE,LEFTFACE,BACKFACE,FRONTFACE,
RIGHTFACE,DOWNFACE,LEFTFACE,UPPERFACE,BACKFACE,FRONTFACE,
DOWNFACE,LEFTFACE,UPPERFACE,RIGHTFACE,BACKFACE,FRONTFACE,
LEFTFACE,BACKFACE,RIGHTFACE,FRONTFACE,DOWNFACE,UPPERFACE,
BACKFACE,RIGHTFACE,FRONTFACE,LEFTFACE,DOWNFACE,UPPERFACE,
RIGHTFACE,FRONTFACE,LEFTFACE,BACKFACE,DOWNFACE,UPPERFACE,
FRONTFACE,LEFTFACE,BACKFACE,RIGHTFACE,DOWNFACE,UPPERFACE};
int staticCorners[]={
UPPERFACE_UPPERLEFT,LEFTFACE_UPPERLEFT,BACKFACE_UPPERRIGHT,
UPPERFACE_UPPERRIGHT,BACKFACE_UPPERLEFT,RIGHTFACE_UPPERRIGHT,
UPPERFACE_DOWNLEFT,FRONTFACE_UPPERLEFT,LEFTFACE_UPPERRIGHT,
UPPERFACE_DOWNRIGHT,RIGHTFACE_UPPERLEFT,FRONTFACE_UPPERRIGHT,
DOWNFACE_UPPERLEFT,LEFTFACE_DOWNRIGHT,FRONTFACE_DOWNLEFT,
DOWNFACE_UPPERRIGHT,FRONTFACE_DOWNRIGHT,RIGHTFACE_DOWNLEFT,
DOWNFACE_DOWNLEFT,BACKFACE_DOWNRIGHT,LEFTFACE_DOWNLEFT,
DOWNFACE_DOWNRIGHT,RIGHTFACE_DOWNRIGHT,BACKFACE_DOWNLEFT};
int staticEdges[]={
UPPERFACE_UPPERMID,BACKFACE_UPPERMID,
UPPERFACE_MIDLEFT,LEFTFACE_UPPERMID,
UPPERFACE_MIDRIGHT,RIGHTFACE_UPPERMID,
UPPERFACE_DOWNMID,FRONTFACE_UPPERMID,
LEFTFACE_MIDRIGHT,FRONTFACE_MIDLEFT,
FRONTFACE_MIDRIGHT,RIGHTFACE_MIDLEFT,
RIGHTFACE_MIDRIGHT,BACKFACE_MIDLEFT,
BACKFACE_MIDRIGHT,LEFTFACE_MIDLEFT,
DOWNFACE_UPPERMID,FRONTFACE_DOWNMID,
DOWNFACE_MIDLEFT,LEFTFACE_DOWNMID,
DOWNFACE_MIDRIGHT,RIGHTFACE_DOWNMID,
DOWNFACE_DOWNMID,BACKFACE_DOWNMID};
struct colorType
{
int colorval;
unsigned int rawRed;
unsigned int rawGreen;
unsigned int rawBlue;
unsigned int normRed;
unsigned int normGreen;
unsigned int normBlue;
};
colorType cubeColor[6*9];
char cube[6*9];
char tmpCube[6*9];
char moves[MAXMOVES];
int movesCount=0;
char solution[MAXMOVES];
int solutionCount;
int solutionTwists;
int twists;
char staticfaces[]={'L','F','R','B','U','D'};
char faces[]={'L','F','R','B','U','D'};
bool cubeGrabbed;
int currentAngle; //Current position of turntable
int tiltPower=85; //Initial tiltpower. Continuously adjusted depending on actual speed
int grabPower;
struct rgb
{
int red;
int green;
int blue;
};
rgb refColor[6];
rgb sensorColor[6*9];
unsigned long costMatrix[12*12];
int twistMatrix[12*12];
byte color[6*9];
inline void ColorSet(int face, int pos,int col)
{
color[face*9+pos]=col;
}
inline void CostMatrixSet(int x, int y, unsigned long cost)
{
costMatrix[x*12+y]=cost;
}
inline unsigned long CostMatrixGet(int x, int y)
{
return costMatrix[x*12+y];
}
inline void TwistMatrixSet(int x, int y, int twist)
{
twistMatrix[x*12+y]=twist;
}
inline int TwistMatrixGet(int x, int y)
{
return twistMatrix[x*12+y];
}
inline int Tile(int face, int pos)
{
return face*9+pos;
}
inline int OrientationGet(int orientation, int face)
{
return staticOrientations[orientation * 6 + face];
}
inline int CornerGet(int corner, int side)
{
return staticCorners[corner * 3 + side];
}
inline int EdgeGet(int edge, int side)
{
return staticEdges[edge * 2 + side];
}
void Tilt()
{
long tachoPrevious,tachoNow;
unsigned long tick,time;
Coast(OUT_A);
Wait(200);
tick=CurrentTick();
OnFwd(OUT_B,tiltPower);
while(MotorRotationCount(OUT_B)<65);
time=CurrentTick()-tick;
Off(OUT_B);
if(time > 215)
{
tiltPower += 1 + (time-215)/10;
if(tiltPower>100)
tiltPower=100;
}
else
tiltPower--;
grabPower=tiltPower/11;
Wait(TILTREST);
OnFwd(OUT_B,-100);
while(MotorRotationCount(OUT_B)>40);
OnFwd(OUT_B,-20);
while(MotorTachoCount(OUT_B)>20);
tachoNow=MotorTachoCount(OUT_B);
do{
tachoPrevious=tachoNow;
Wait(10);
tachoNow=MotorTachoCount(OUT_B);
}while(tachoNow!=tachoPrevious);
Coast(OUT_B);
char uf=faces[UPPERFACE];
faces[UPPERFACE]=faces[LEFTFACE];
faces[LEFTFACE]=faces[DOWNFACE];
faces[DOWNFACE]=faces[RIGHTFACE];
faces[RIGHTFACE]=uf;
}
void GrabCube()
{
Coast(OUT_A);
OnFwd(OUT_B,100);
while(MotorRotationCount(OUT_B)<20);
OnFwd(OUT_B,20);
while(MotorRotationCount(OUT_B)<30);
OnFwd(OUT_B,grabPower);
Wait(300);
cubeGrabbed=true;
}
void ReleaseCube()
{
int tachoNow,tachoPrevious;
Off(OUT_A);
Off(OUT_B);
OnFwd(OUT_B,-100);
while(MotorRotationCount(OUT_B)>20);
OnFwd(OUT_B,-20);
while(MotorTachoCount(OUT_B)>20);
tachoNow=MotorTachoCount(OUT_B);
do
{
tachoPrevious=tachoNow;
Wait(10);
tachoNow=MotorTachoCount(OUT_B);
}while(tachoNow!=tachoPrevious);
Coast(OUT_B);
cubeGrabbed=false;
}
void TurnTo(int newAngle)
{
int direction;
int angle1,angle2;
long pwr;
int speed;
int distance;
if(newAngle!=currentAngle)
{
if(newAngle==0) //home
{
OnFwd(OUT_A,100);
do
{
angle1=MotorRotationCount(OUT_A);
Wait(20);
angle2=MotorRotationCount(OUT_A);
speed=abs(angle2-angle1);
distance=abs(newAngle-angle2);
if(speed>11)
{
pwr=20+distance;
if(pwr<100)
OnFwd(OUT_A,pwr);
}
}while(distance>30 || angle2!=angle1);
Off(OUT_A);
Wait(300);
}
else
{
int angle=newAngle - MotorRotationCount(OUT_A);
ResetTachoCount(OUT_A);
Wait(50);
RotateMotor(OUT_A,60,angle);
Wait(300);
}
int angle=MotorRotationCount(OUT_A);
if(!cubeGrabbed)
{
int num=(4+(currentAngle-newAngle)/90)%4;
for(int i=0;i<num;i++)
{
char lf=faces[LEFTFACE];
faces[LEFTFACE]=faces[FRONTFACE];
faces[FRONTFACE]=faces[RIGHTFACE];
faces[RIGHTFACE]=faces[BACKFACE];
faces[BACKFACE]=lf;
}
}
currentAngle=newAngle;
}
}
inline void TurnQuarter(int num)
{
TurnTo((currentAngle-num*90)%360);
}
void Twist(int num)
{
int angle=num * -90;
if(angle==currentAngle)
Wait(TILTREST);
else
TurnTo(angle);
GrabCube();
TurnTo(0);
ReleaseCube();
}
//*****************************************************************************
// Initialisation
void InitTurntable()
{
int tachoNow,tachoPrevious;
OnFwd(OUT_A,15);
do
{
tachoPrevious=tachoNow;
Wait(100);
tachoNow=MotorTachoCount(OUT_A);
}while(tachoNow!=tachoPrevious);
Coast(OUT_A);
Wait(500);
ResetAllTachoCounts(OUT_A);
Wait(500);
currentAngle=0;
}
void InitTilter()
{
int tachoNow,tachoPrevious;
OnFwd(OUT_B,-20);
tachoNow=MotorTachoCount(OUT_B);
do
{
tachoPrevious=tachoNow;
Wait(100);
tachoNow=MotorTachoCount(OUT_B);
}while(tachoNow!=tachoPrevious);
Coast(OUT_B);
Wait(500);
ResetAllTachoCounts(OUT_B);
}
void InitScanner()
{
int tachoNow,tachoPrevious;
OnFwd(OUT_C,-15);
tachoNow=MotorTachoCount(OUT_C);
do
{
tachoPrevious=tachoNow;
Wait(100);
tachoNow=MotorTachoCount(OUT_C);
}while(tachoNow!=tachoPrevious);
Off(OUT_C);
Wait(500);
ResetAllTachoCounts(OUT_C);
Wait(500);
RotateMotor(OUT_C,10,SENSORPOS_HOME);
}
void Initialize()
{
InitTilter();
InitScanner();
InitTurntable();
SetSensorLowspeed(IN_2);
}
//*****************************************************************************
// Scan cube
inline void DrawFacelet(int pos)
{
RectOut(59-(pos/3)*17,42-(pos%3)*17,15,15);
}
void ReadColor(int face, int pos)
{
int colorval;
unsigned int raw[];
unsigned int norm[];
int scaled[];
while(!ReadSensorColorEx(IN_1,colorval,raw,norm,scaled));
int tile=Tile(face,pos);
cubeColor[tile].colorval=colorval;
cubeColor[tile].normRed=norm[0];
cubeColor[tile].normGreen=norm[1];
cubeColor[tile].normBlue=norm[2];
cubeColor[tile].rawRed=raw[0];
cubeColor[tile].rawGreen=raw[1];
cubeColor[tile].rawBlue=raw[2];
}
void ScanFace(int face,int c1,int c2,int c3,int c4,int c5,int c6,int c7,int c8,int endAngle)
{
TurnTo(0);
ResetTachoCount(OUT_A);
ClearScreen();
RotateMotorEx(OUT_C,20,SENSORPOS_CENTER-SENSORPOS_HOME,0,false,false);
ReadColor(face,CENTER);
DrawFacelet(CENTER);
RotateMotor(OUT_C,20,SENSORPOS_CORNERS-SENSORPOS_CENTER);
ReadColor(face,c1);
DrawFacelet(UPPERRIGHT);
RotateMotorEx(OUT_A,SCANSPEED,-90,0,false,false);
ReadColor(face,c2);
DrawFacelet(DOWNRIGHT);
RotateMotorEx(OUT_A,SCANSPEED,-90,0,false,false);
ReadColor(face,c3);
DrawFacelet(DOWNLEFT);
RotateMotor(OUT_A,SCANSPEED,-90);
ReadColor(face,c4);
DrawFacelet(UPPERLEFT);
RotateMotor(OUT_C,10,SENSORPOS_EDGES-SENSORPOS_CORNERS);
RotateMotorEx(OUT_A,SCANSPEED,OFFSET_EDGE,0,false,false);
ReadColor(face,c5);
DrawFacelet(MIDLEFT);
RotateMotorEx(OUT_A,SCANSPEED,90,0,false,false);
ReadColor(face,c6);
DrawFacelet(UPPERMID);
RotateMotorEx(OUT_A,SCANSPEED,90,0,false,false);
ReadColor(face,c7);
DrawFacelet(MIDRIGHT);
RotateMotor(OUT_A,SCANSPEED,OFFSET_LASTEDGE - 270 - OFFSET_EDGE);
RotateMotorEx(OUT_C,20,SENSORPOS_LASTEDGE-SENSORPOS_EDGES,0,false,false);
ReadColor(face,c8);
DrawFacelet(DOWNMID);
RotateMotor(OUT_C,20,SENSORPOS_HOME-SENSORPOS_LASTEDGE);
currentAngle=-1;
TurnTo(endAngle);
}
void ScanCube()
{
SetSensorColorFull(IN_1);
ScanFace(BACKFACE,UPPERRIGHT,UPPERLEFT,DOWNLEFT,DOWNRIGHT,DOWNMID,MIDLEFT,UPPERMID,MIDRIGHT,0);
Tilt();
ScanFace(LEFTFACE,UPPERRIGHT,UPPERLEFT,DOWNLEFT,DOWNRIGHT,DOWNMID,MIDLEFT,UPPERMID,MIDRIGHT,0);
Tilt();
ScanFace(FRONTFACE,UPPERRIGHT,UPPERLEFT,DOWNLEFT,DOWNRIGHT,DOWNMID,MIDLEFT,UPPERMID,MIDRIGHT,-270);
Tilt();
ScanFace(DOWNFACE,UPPERRIGHT,UPPERLEFT,DOWNLEFT,DOWNRIGHT,DOWNMID,MIDLEFT,UPPERMID,MIDRIGHT,0);
Tilt();
ScanFace(RIGHTFACE,UPPERLEFT,DOWNLEFT,DOWNRIGHT,UPPERRIGHT,MIDRIGHT,DOWNMID,MIDLEFT,UPPERMID,0);
Tilt();
ScanFace(UPPERFACE,DOWNLEFT,DOWNRIGHT,UPPERRIGHT,UPPERLEFT,UPPERMID,MIDRIGHT,DOWNMID,MIDLEFT,0);
SetSensorType(IN_1,SENSOR_TYPE_NONE);
}
//*****************************************************************************
// Color resolving
void Sorry()
{
TextOut(5,LCD_LINE2,"SORRY",true);
TextOut(5,LCD_LINE4,"Can't resolve" );
TextOut(5,LCD_LINE5,"colors" );
PlayFile("Sorry.rso");
}
int GetRefCornerColor(rgb refColor1,rgb refColor2)
{
int pos3;
unsigned long minCost=$FFFFFFFF;
for(int corner=1;corner<8;corner++)
{
int minCostTwist;
for(int twist=0;twist<3;twist++)
{
int pos1 = CornerGet(corner,twist);
int pos2 = CornerGet(corner,(1+twist)%3);
rgb color1 = sensorColor[pos1];
rgb color2 = sensorColor[pos2];
unsigned long cost = ((refColor1.red - color1.red) * (refColor1.red - color1.red) + (refColor1.green - color1.green) * (refColor1.green - color1.green) + (refColor1.blue - color1.blue) * (refColor1.blue - color1.blue))/3;
cost += ((refColor2.red - color2.red) * (refColor2.red - color2.red) + (refColor2.green - color2.green) * (refColor2.green - color2.green) + (refColor2.blue - color2.blue) * (refColor2.blue - color2.blue))/3;
if(cost<minCost)
{
minCost=cost;
minCostTwist=twist;
pos3=CornerGet(corner,(2+twist)%3);
}
}
}
return pos3;
}
void InitColors()
{
for(int pos=0;pos<6*9;pos++)
{
sensorColor[pos].red=cubeColor[pos].rawRed;
sensorColor[pos].green=cubeColor[pos].rawGreen;
sensorColor[pos].blue=cubeColor[pos].rawBlue;
}
refColor[UPPERFACE]=sensorColor[UPPERFACE_UPPERLEFT];
refColor[LEFTFACE]=sensorColor[LEFTFACE_UPPERLEFT];
refColor[BACKFACE]=sensorColor[BACKFACE_UPPERRIGHT];
refColor[FRONTFACE]=sensorColor[GetRefCornerColor(refColor[LEFTFACE],refColor[UPPERFACE])];
refColor[RIGHTFACE]=sensorColor[GetRefCornerColor(refColor[UPPERFACE],refColor[BACKFACE])];
refColor[DOWNFACE]=sensorColor[GetRefCornerColor(refColor[BACKFACE],refColor[LEFTFACE])];
}
int CenterFit(int orientation)
{
int fit=0;
for(int face=0;face<6;face++)
if(color[Tile(face,CENTER)] == OrientationGet(orientation, face))
fit++;
return fit;
}
void ResolveCenterColors()
{
TextOut(2,LCD_LINE4,"Center colors...");
for(int center=0;center<6;center++)
{
unsigned long minCost=$FFFFFFFF;
int bestCubie;
for(int cubie=0;cubie<6;cubie++)
{
rgb centerColor,cubieColor;
centerColor = refColor[center];
cubieColor=sensorColor[Tile(cubie,CENTER)];
unsigned long cost = (centerColor.red - cubieColor.red) * (centerColor.red - cubieColor.red) + (centerColor.green - cubieColor.green) * (centerColor.green - cubieColor.green) + (centerColor.blue - cubieColor.blue) * (centerColor.blue - cubieColor.blue);
if(cost<minCost)
{
minCost=cost;
bestCubie=cubie;
}
}
ColorSet(center,CENTER,bestCubie);
}
int bestFit=0;
int bestOrientation;
for(int orientation=0;orientation<24;orientation++)
{
int fit=CenterFit(orientation);
if(fit>bestFit)
{
bestFit=fit;
bestOrientation=orientation;
}
}
rgb tmpRefColor[6];
ArraySubset(tmpRefColor,refColor,0,6);
for(int face=0;face<6;face++)
{
rgb col=tmpRefColor[face];
refColor[OrientationGet(bestOrientation, face)]=col;
}
for(int face=0;face<6;face++)
ColorSet(face,CENTER,face);
}
int BestFitCubie(int &cubie, int &twist, int dim)
{
int bestX,bestY;
unsigned long bestDiff=0;
for(int x = 0; x < dim;x++)
{
unsigned long minCost=$FFFFFFFF;
unsigned long min2Cost=$FFFFFFFF;
int minY;
for(int y=0; y < dim; y++)
if(CostMatrixGet(x,y) < minCost)
{
minCost=CostMatrixGet(x,y);
minY=y;
}
for(int y=0; y < dim; y++)
if(y != minY && CostMatrixGet(x,y) < min2Cost)
min2Cost=CostMatrixGet(x,y);
if(min2Cost-minCost > bestDiff)
{
bestX = x;
bestY = minY;
bestDiff = min2Cost-minCost;
}
}
for(int x=0;x<dim;x++)
CostMatrixSet(x,bestY,$FFFFFFFF);
for(int y=0;y<dim;y++)
CostMatrixSet(bestX,y,$FFFFFFFF);
cubie = bestY;
twist = TwistMatrixGet(bestX,bestY);
return bestX;
}
void ResolveCornerColors()
{
TextOut(2,LCD_LINE5,"Corner colors...");
for(int corner=0;corner<8;corner++)
for(int cubie=0;cubie<8;cubie++)
{
unsigned long minCost=$FFFFFFFF;
int minCostTwist;
for(int twist=0;twist<3;twist++)
{
unsigned long cost=0;
for(int s=0;s<3;s++)
{
int face = CornerGet(corner,s) / 9;
int pos = CornerGet(cubie,(s+twist)%3);
rgb color1,color2;
color1 = refColor[face];
color2 = sensorColor[pos];
cost += ((color1.red - color2.red) * (color1.red - color2.red) + (color1.green - color2.green) * (color1.green - color2.green) + (color1.blue - color2.blue) * (color1.blue - color2.blue))/3;
}
if(cost<minCost)
{
minCost=cost;
minCostTwist=twist;
}
}
CostMatrixSet(corner,cubie,minCost);
TwistMatrixSet(corner,cubie,minCostTwist);
}
for(int face=0;face<6;face++) //calibrate refColor
{
refColor[face].red=0;
refColor[face].green=0;
refColor[face].blue=0;
}
for(int i=0;i<8;i++)
{
int cubie;
int twist;
int corner=BestFitCubie(cubie,twist,8);
for(int s=0;s<3;s++)
{
int face = (CornerGet(corner,s))/9;
int pos = CornerGet(cubie,(s+twist)%3);
color[pos]=face;
//calibrate refColor
rgb ref=refColor[face];
rgb col=sensorColor[pos];
ref.red+=col.red;
ref.green+=col.green;
ref.blue+=col.blue;
refColor[face]=ref;
}
}
//calibrate refColor
for(int face = 0 ; face < 6 ;face++)
{
rgb tmp=refColor[face];
tmp.red/=4;
tmp.green/=4;
tmp.blue/=4;
refColor[face]=tmp;
}
}
void ResolveEdgeColors()
{
TextOut(2,LCD_LINE6,"Edge colors...");
for(int edge=0;edge<12;edge++)
for(int cubie=0;cubie<12;cubie++)
{
unsigned long minCost=$FFFFFFFF;
int minCostTwist;
for(int twist=0;twist<2;twist++)
{
unsigned long cost=0;
for(int s=0;s<2;s++)
{
int face = EdgeGet(edge,s)/ 9;
int pos = EdgeGet(cubie,(s+twist)%2);
rgb color1,color2;
color1 = refColor[face];
color2 = sensorColor[pos];
cost += ((color1.red - color2.red) * (color1.red - color2.red) + (color1.green - color2.green) * (color1.green - color2.green) + (color1.blue - color2.blue) * (color1.blue - color2.blue))/2;
}
if(cost<minCost)
{
minCost=cost;
minCostTwist=twist;
}
}
CostMatrixSet(edge,cubie,minCost);
TwistMatrixSet(edge,cubie,minCostTwist);
}
for(int i = 0 ; i < 12 ;i++)
{
int cubie;
int twist;
int edge=BestFitCubie(cubie,twist,12);
for(int s=0;s<2;s++)
color[EdgeGet(cubie,(s+twist)%2)]=EdgeGet(edge,s)/9;
}
}
string ColorToStr(int face, int pos)
{
byte c=color[Tile(face,pos)];
return SubStr("LFRBUD",c,1);
}
void DumpCube()
{
int handle;
DeleteFile("rubikscube.txt");
CreateFile("rubikscube.txt", 4000, handle);
int count;
string msg,c;
for(int row=0;row<3;row++)
{
msg=" ";
for(int col=0;col<3;col++)
{
c=ColorToStr(UPPERFACE,row*3+col);
msg=StrCat(msg,c);
}
WriteLnString(handle,msg,count);
}
WriteLnString(handle,"",count);
for(int row=0;row<3;row++)
{
msg="";
for(int face=LEFTFACE;face<=BACKFACE;face++)
{
for(int col=0;col<3;col++)
{
c=ColorToStr(face,row*3+col);
msg=StrCat(msg,c);
}
msg=StrCat(msg," ");
}
WriteLnString(handle,msg,count);
}
WriteLnString(handle,"",count);
for(int row=0;row<3;row++)
{
msg=" ";
for(int col=0;col<3;col++)
{
c=ColorToStr(DOWNFACE,row*3+col);
msg=StrCat(msg,c);
}
WriteLnString(handle,msg,count);
}
WriteLnString(handle,"",count);
WriteLnString(handle,"colorval Raw-RGB [Norm-RGB] = Resolved face",count);
WriteLnString(handle,"",count);
for(int face=0;face<6;face++)
{
msg=SubStr("LFRBUD",face,1); WriteString(handle,msg,count); WriteString(handle,": ",count);
for(int pos=0;pos<9;pos++)
{
colorType col=cubeColor[Tile(face,pos)];
msg=NumToStr(col.colorval); WriteString(handle,msg,count); WriteString(handle," ",count);
msg=NumToStr(col.rawRed); WriteString(handle,msg,count); WriteString(handle,".",count);
msg=NumToStr(col.rawGreen); WriteString(handle,msg,count); WriteString(handle,".",count);
msg=NumToStr(col.rawBlue); WriteString(handle,msg,count); WriteString(handle," [",count);
msg=NumToStr(col.normRed); WriteString(handle,msg,count); WriteString(handle,".",count);
msg=NumToStr(col.normGreen); WriteString(handle,msg,count); WriteString(handle,".",count);
msg=NumToStr(col.normBlue); WriteString(handle,msg,count); WriteString(handle,"]=",count);
msg=ColorToStr(face,pos); WriteString(handle,msg,count); WriteString(handle,", ",count);
}
WriteLnString(handle,"",count);
}
WriteLnString(handle,"",count);
WriteLnString(handle,"Ref RGB",count);
WriteLnString(handle,"",count);
for(int face=0;face<6;face++)
{
msg=SubStr("LFRBUD",face,1); WriteString(handle,msg,count); WriteString(handle,": ",count);
rgb col=refColor[face];
msg=NumToStr(col.red); WriteString(handle,msg,count); WriteString(handle,".",count);
msg=NumToStr(col.green); WriteString(handle,msg,count); WriteString(handle,".",count);
msg=NumToStr(col.blue); WriteLnString(handle,msg,count);
}
CloseFile(handle);
}
void ResolveColors()
{
TextOut(2,LCD_LINE2,"RESOLVING COLORS",true);
InitColors();
ResolveCenterColors();
ResolveCornerColors();
ResolveEdgeColors();
DumpCube();
}
//*****************************************************************************
// Solve functions
inline void CubeSet(int face, int pos, char value)
{
cube[face * 9 + pos] = value;
}
inline char CubeGet(int face, int pos)
{
return cube[face * 9 + pos];
}
inline char TmpCubeGet(int face, int pos)
{
return tmpCube[face * 9 + pos];
}
inline void CopyCube()
{
ArraySubset(tmpCube,cube,0,54);
}
void CopyFace(int fromFace,int toFace)
{
int fromFaceOffset=fromFace*9;
int toFaceOffset=toFace*9;
for(int i=0;i<9;i++)
cube[toFaceOffset+i]=tmpCube[fromFaceOffset+i];
}
void CopyFaceClockwise(int fromFace, int toFace, int turns)
{
int fromFaceOffset = fromFace*9;
int toFaceOffset = toFace*9;
if(turns==1)
{
cube[toFaceOffset + UPPERLEFT] = tmpCube[fromFaceOffset + DOWNLEFT];
cube[toFaceOffset + UPPERMID] = tmpCube[fromFaceOffset + MIDLEFT];
cube[toFaceOffset + UPPERRIGHT] = tmpCube[fromFaceOffset + UPPERLEFT];
cube[toFaceOffset + MIDLEFT] = tmpCube[fromFaceOffset + DOWNMID];
cube[toFaceOffset + CENTER] = tmpCube[fromFaceOffset + CENTER];
cube[toFaceOffset + MIDRIGHT] = tmpCube[fromFaceOffset + UPPERMID];
cube[toFaceOffset + DOWNLEFT] = tmpCube[fromFaceOffset + DOWNRIGHT];
cube[toFaceOffset + DOWNMID] = tmpCube[fromFaceOffset + MIDRIGHT];
cube[toFaceOffset + DOWNRIGHT] =tmpCube[fromFaceOffset +UPPERRIGHT];
}
else if(turns==2)
{
cube[toFaceOffset + UPPERLEFT] = tmpCube[fromFaceOffset + DOWNRIGHT];
cube[toFaceOffset + UPPERMID] = tmpCube[fromFaceOffset + DOWNMID];
cube[toFaceOffset + UPPERRIGHT] = tmpCube[fromFaceOffset + DOWNLEFT];
cube[toFaceOffset + MIDLEFT] = tmpCube[fromFaceOffset + MIDRIGHT];
cube[toFaceOffset + CENTER] = tmpCube[fromFaceOffset + CENTER];
cube[toFaceOffset + MIDRIGHT] = tmpCube[fromFaceOffset + MIDLEFT];
cube[toFaceOffset + DOWNLEFT] = tmpCube[fromFaceOffset + UPPERRIGHT];
cube[toFaceOffset + DOWNMID] = tmpCube[fromFaceOffset + UPPERMID];
cube[toFaceOffset + DOWNRIGHT] =tmpCube[fromFaceOffset +UPPERLEFT];
}
else //turns==3
{
cube[toFaceOffset + UPPERLEFT] = tmpCube[fromFaceOffset + UPPERRIGHT];
cube[toFaceOffset + UPPERMID] = tmpCube[fromFaceOffset + MIDRIGHT];
cube[toFaceOffset + UPPERRIGHT] = tmpCube[fromFaceOffset + DOWNRIGHT];
cube[toFaceOffset + MIDLEFT] = tmpCube[fromFaceOffset + UPPERMID];
cube[toFaceOffset + CENTER] = tmpCube[fromFaceOffset + CENTER];
cube[toFaceOffset + MIDRIGHT] = tmpCube[fromFaceOffset + DOWNMID];
cube[toFaceOffset + DOWNLEFT] = tmpCube[fromFaceOffset + UPPERLEFT];
cube[toFaceOffset + DOWNMID] = tmpCube[fromFaceOffset + MIDLEFT];
cube[toFaceOffset + DOWNRIGHT] =tmpCube[fromFaceOffset +DOWNLEFT];
}
}
void TurnCube(int turns)
{
CopyCube();
if(turns==1)
{
CopyFaceClockwise(UPPERFACE,UPPERFACE,1);
CopyFace(LEFTFACE,BACKFACE);
CopyFace(BACKFACE,RIGHTFACE);
CopyFace(RIGHTFACE,FRONTFACE);
CopyFace(FRONTFACE,LEFTFACE);
CopyFaceClockwise(DOWNFACE,DOWNFACE,3);
}
else if(turns==2)
{
CopyFaceClockwise(UPPERFACE,UPPERFACE,2);
CopyFace(LEFTFACE,RIGHTFACE);
CopyFace(BACKFACE,FRONTFACE);