Featured post

A reason to exercise.

It's been two years since I started going to the gym and this post is just a summary of my experience. Before going to the gym. Iv...

Saturday, 6 February 2016

Phase 2 of Rubik's cube solver-Basics of "The code!!"

This is the interesting part wherein I make my device to "think" and solve the cube.
The code is no where near to being complete and that's what makes it perfect for the reader,because you grow as the code grow.
Another thing to keep in mind is that the already written code is not static.it is subject to change anytime,if it proves useful than it already is.

To enable the solver to "see" the different colors of the cube,I plan to integrate an CMOS camera module.As this is for a later stage,currently the different colors are manually entered before each run.

The Rubik's cube is converted from 3D to 2D , making it easier to solve the cube and represent it on paper.

something like this
The matrices are split into 6 different matrices A,B,C,D,E and F.
A = left face
B = front face
C = right face
D = back face
E = top face
F = bottom face

I'm someone who is very comfortable with numbers.
So,in solving the cube,instead of using char to represent the different colors,I assign to each color a code and then fill the matrix with that number.


Face              Matrix name          Color           Number

Left                   A                       Yellow             1
Front                 B                       Orange             2 
Right                 C                       White               3
Back                  D                       Red                  4
Top                    E                       Blue                 5
Bottom              F                        Green              6

So,to the human,the cube may look like an scrambled array of colors but to my code,its an scrambled arrangement of numbers which have to be "solved" in place.
Is there any benefit of using numbers as opposed to characters,I don't know,
The code written however is all concerned with the number coded cube.

Let's begin.

The basic movements of the cube are 
R , R', L , L' , U , U' , D and D'.

All of these movements are taken care of by 4 functions I have defined; namely

column_toward(int);

column_away(int);

row_right(int);  and 

row_left(int);

Here is how the different matrices are connected to form the cube.



Lets analyse each of the functions now.

column_toward(int);
This function performs a stepper motor turn towards us by 90°.
column_toward(0) = L.
column_toward(2) = R'.

column_away(int);
This function performs a stepper motor turn away from us by 90°.
column_away(0) = L'.
column_away(2) = R.

As we observe from the function - cube notation correspondence, column_toward and column_away helps us perform the left and right rotations of the cube.
Next comes row_right(int) and row_left(int) which needs a bit of explaining to do.
Since only 3 steppers are being used (two on either side and one at the bottom),
the bottom stepper will very comfortably perform the D and D' movements.
For the U and U',the cube has to be rotated by 180°(either towards or away from us).
This is achieved by the following steps:

1.Release the holding mechanism

2.Turn both steppers to 180°.
   (Keep in mind that as the steppers on either side are facing each other,clockwise rotation of one is       anticlockwise to the other and vice versa.
    So,to rotate(not turn) the cube,one stepper turn 180° clockwise and the other 180°                                 anticlockwise.) 

3.Hold the holding mechanism and perform the needed rotation.

row_right(int);
This function performs a motion that turns the side to the right by 90°.
row_right(0) = U'.
row_right(2) = D.

row_left(int);
This function performs a motion that turns the side to the left by 90°.
row_left(0) = U.
row_left(2) = D'.

Never is a value of 1 passed to the functions as they would correspond to moving the center column and row, a rotation which can never be performed by my hardware arrangement.



No comments:

Post a Comment