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, 27 February 2016

The code so far.

The code written below performs all the basic turns of the cube (only in the software side.Yet to interface stepper motors.) such as L L' R R' F F' and B B'.

/*
 * array format
 *          E
 *     A  B  C  D
 *          F
 * */


//colour - number code
//yellow 1
//orange 2
//white  3
//red    4
//blue   5
//green  6

//matrix name - number code
// A 7
// B 8
// C 9
// D 10
// E 11
// F 12

int A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
int B[3][3]={{2,2,2},{2,2,2},{2,2,2}};
int C[3][3]={{3,3,3},{3,3,3},{3,3,3}};
int D[3][3]={{4,4,4},{4,4,4},{4,4,4}};
int E[3][3]={{5,5,5},{5,5,5},{5,5,5}};
int F[3][3]={{6,6,6},{6,6,6},{6,6,6}};

int matrix_name[3][3];
int temp[3][3];
int temp1;
int temp2;
int i;
int j;
int d=0;
int y=2;
char c=0;                                                 //used in print_matrix() function;
char f=0;                                                 //used to get Y or N in check_cubes_correctness();
int count=0;
int cross_value[4][3];
int edgepair[12];

void get_matrices(int *,char);
void check_cubes_correctness(void);
void print_matrix(int *);//prints out a single matrix
void print_matrices(void);//prints out all 6 matrices

void column_away(int);
void column_toward(int);

void row_right(int);
void row_left(int);

void front_clockwise(void);
void front_anticlockwise(void);

void back_clk(void);
void back_anticlk(void);

void circle_clockwise(char);
void circle_anticlockwise(char);

void change_matrix_name(int,char);      //0=>matrix to matrix_name
                                                                //1=>matrix_name to matrix

void search_cross_location(void);
void calculate_edgepairs(void);
void solve_for_cross(void);

int main(void)
{

  /*
  how i solve the cube
 
  1.form plus
  2.solve first layer
  3.solve for middle layer
  4.form top cross
  5.make sure corner pieces are in place
  6.put them in place
  7.end up with solved cube
  */
 
  printf("\n Color - number code\n");
  printf("\n yellow 1 \n orange 2 \n white 3 \n red 4 \n blue 5 \n green 6");
 
  get_matrices(A,'A');
  get_matrices(B,'B');
  get_matrices(C,'C');
  get_matrices(D,'D');
  get_matrices(E,'E');
  get_matrices(F,'F');
 
  check_cubes_correctness();
  while(count!=189)
  {
 check_cubes_correctness();
  }
 
  print_matrices();

  printf("\n\n\n\n");
 
  column_toward(0);
  printf("column toward 0 \n\n");
  print_matrices();
 
  column_away(0);
  printf("column away 0 \n\n");
  print_matrices();
 
  printf("\n\n\n");
 
  column_toward(2);
  printf("column toward 2\n\n");
  print_matrices();
 
  column_away(2);
  printf("column away 2\n\n");
  print_matrices();
 
  printf("\n\n\n");
   
  row_right(0);
  printf("row right 0 \n\n");
  print_matrices();
 
  row_left(0);
  printf("row left 0 \n\n");
  print_matrices();
 
  printf("\n\n\n");
   
  row_right(2);
  printf("row right 2 \n\n");
  print_matrices();
 
  row_left(2);
  printf("row left 2 \n\n");
  print_matrices();
 
  printf("\n\n\n");
   
  front_clockwise();
  printf("Front clockwise\n\n");
  print_matrices();
 
  front_anticlockwise();
  printf("Front anticlockwise\n\n");
  print_matrices();
 
  printf("\n\n\n");

while(1)
{
}

}


void get_matrices(int *z,char b)
{
printf("\n Enter matrix %c: \n",b);

//empty the temp matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=0;
}
}

//take input and store in temp matrix as characters
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%c",&temp[i][j]);
printf("%c",temp[i][j]);
}
}

//copy from temp to appropriate matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
*z=temp[i][j]-48;
z++;
}
}



}

void print_matrix(int *a)
 {
  for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(*a==1) c='Y';
if(*a==2) c='O';
if(*a==3) c='W';
if(*a==4) c='R';
if(*a==5) c='B';
if(*a==6) c='G';
printf("%c\t\t",c);
a++;
}
printf("\n");
}
  printf("\n\n");
  }
 
void print_matrices(void)
{
printf("Matrix A\n");
print_matrix(A);

printf("Matrix B\n");
print_matrix(B);

printf("Matrix C\n");
print_matrix(C);

printf("Matrix D\n");
print_matrix(D);

printf("Matrix E\n");
print_matrix(E);

printf("Matrix F\n");
print_matrix(F);
}

void check_cubes_correctness(void)
{
    //The logic of this function is that when you consider a solved cube in my arrangement,
//side A is full of 1's,
//-||- B ----||-----2's
//-||- C ----||-----3's and so on.
//this function just sums up each od the numbers.
//if the value ends up to be 189,the cube is valid

count=0;
printf("\n\n count = %d \n\n", count);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
count=count+A[i][j]+B[i][j]+C[i][j]+D[i][j]+E[i][j]+F[i][j];
}
}
printf("\n count = %d \n", count);

if(count==189)
{
printf("\n valid cube\n\n");
}
else
{
printf("\n invalid cube.re-enter values.\n\n");
//add code to rescan all 6 sides again
printf("\n Enter Y or N in \"Caps\" \n");
printf("\n Matrix A \n");
print_matrix(A);
printf(" \n Enter  Y for correct  N for not correct \n");
        scanf("%c",&f);
        if(f=='Y')
        {
       
        }
        if(f=='N')
        {
        get_matrices(A,'A');
        }
       
        printf("\n Matrix B \n");
print_matrix(B);
printf(" \n Enter  Y for correct  N for not correct \n");
        scanf("%c",&f);
        if(f=='Y')
        {
       
        }
        if(f=='N')
        {
        get_matrices(B,'B');
        }
       
        printf("\n Matrix C \n");
print_matrix(C);
printf(" \n Enter  Y for correct  N for not correct \n");
        scanf("%c",&f);
        if(f=='Y')
        {
       
        }
        if(f=='N')
        {
        get_matrices(C,'C');
        }
       
        printf("\n Matrix D \n");
print_matrix(D);
printf(" \n Enter  Y for correct  N for not correct \n");
        scanf("%c",&f);
        if(f=='Y')
        {
       
        }
        if(f=='N')
        {
        get_matrices(D,'D');
        }
       
       
        printf("\n Matrix E \n");
print_matrix(E);
printf(" \n Enter  Y for correct  N for not correct \n");
        scanf("%c",&f);
        if(f=='Y')
        {
       
        }
        if(f=='N')
        {
        get_matrices(E,'E');
        }
       
        printf("\n Matrix F \n");
print_matrix(F);
printf(" \n Enter  Y for correct  N for not correct \n");
        scanf("%c",&f);
        if(f=='Y')
        {
       
        }
        if(f=='N')
        {
        get_matrices(F,'F');
        }

}


}

void column_away(int j)

{
//stepper turn mechanism
y=2;
for(i=0;i<=2;i++)
{
temp[i][j]=E[i][j];
E[i][j]=B[i][j];
B[i][j]=F[i][j];
}
for(i=0;i<=2;i++)
{
if(j==0)
{
F[y][j]=D[i][j+2];
D[i][j+2]=temp[y][j];
}

if(j==2)
{
F[i][j]=D[y][j-2];
D[y][j-2]=temp[i][j];
}

y=y-1;
}

if(j==2)
{
circle_clockwise('C');
}
if(j==0)
{
circle_anticlockwise('A');

}


}

void column_toward(int j)
{
//stepper turn mechanism
y=2;
for(i=0;i<=2;i++)
{
if(j==0)
{
temp[i][j]=D[y][j+2];
D[y][j+2]=F[i][j];
}
if(j==2)
{
temp[i][j]=D[y][j-2];
D[y][j-2]=F[i][j];
}
y=y-1;
}
for(i=0;i<=2;i++)
{
F[i][j]=B[i][j];
B[i][j]=E[i][j];
E[i][j]=temp[i][j];
}

if(j==2)
{
circle_anticlockwise('C');
}
if(j==0)
{
circle_clockwise('A');
}

}

void row_right(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=D[i][j];
D[i][j]=C[i][j];
C[i][j]=B[i][j];
B[i][j]=temp[i][j];
}
if(i==0)
{
circle_anticlockwise('E');
}
if(i==2)
{
circle_clockwise('F');
}
}

void row_left(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=B[i][j];
B[i][j]=C[i][j];
C[i][j]=D[i][j];
D[i][j]=temp[i][j];
}
if(i==0)
{
circle_clockwise('E');
}
if(i==2)
{
circle_anticlockwise('F');
}
}

void circle_clockwise(char a)
{
change_matrix_name(0,a);

temp1=matrix_name[0][0];
matrix_name[0][0]=matrix_name[2][0];
matrix_name[2][0]=matrix_name[2][2];
matrix_name[2][2]=matrix_name[0][2];
matrix_name[0][2]=temp1;

temp2=matrix_name[0][1];
matrix_name[0][1]=matrix_name[1][0];
matrix_name[1][0]=matrix_name[2][1];
matrix_name[2][1]=matrix_name[1][2];
matrix_name[1][2]=temp2;

change_matrix_name(1,a);

}

void circle_anticlockwise(char a)
{
change_matrix_name(0,a);

temp1=matrix_name[0][0];
matrix_name[0][0]=matrix_name[0][2];
matrix_name[0][2]=matrix_name[2][2];
matrix_name[2][2]=matrix_name[2][0];
matrix_name[2][0]=temp1;

temp2=matrix_name[0][1];
matrix_name[0][1]=matrix_name[1][2];
matrix_name[1][2]=matrix_name[2][1];
matrix_name[2][1]=matrix_name[1][0];
matrix_name[1][0]=temp2;

change_matrix_name(1,a);

}

void change_matrix_name(int b,char a)
{
int *c;
if(a=='A')
{
c=A;
}
if(a=='B')
    {
c=B;
    }
if(a=='C')
{
c=C;
}
if(a=='D')
{
c=D;
}
if(a=='E')
{
c=E;
}
if(a=='F')
{
c=F;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(b==0)
{
matrix_name[i][j]=*c;
c++;
}
if(b==1)
{
     *c=matrix_name[i][j];
     c++;
}
}
}

}

void front_clockwise(void)
{
//stepper turn mechanism


temp[0][0]=A[0][2];
temp[0][1]=A[1][2];
temp[0][2]=A[2][2];

A[0][2]=F[0][0];
A[1][2]=F[0][1];
A[2][2]=F[0][2];

F[0][0]=C[2][0];
F[0][1]=C[1][0];
F[0][2]=C[0][0];

C[2][0]=E[2][2];
C[1][0]=E[2][1];
C[0][0]=E[2][0];

E[2][2]=temp[0][0];
E[2][1]=temp[0][1];
E[2][0]=temp[0][2];

circle_clockwise('B');

}

void front_anticlockwise(void)
{
//stepper turn mechanism


temp[0][0]=A[0][2];
temp[0][1]=A[1][2];
temp[0][2]=A[2][2];

A[0][2]=E[2][2];
A[1][2]=E[2][1];
A[2][2]=E[2][0];

E[2][2]=C[2][0];
E[2][1]=C[1][0];  
    E[2][0]=C[0][0];  


    C[2][0]=F[0][0];
    C[1][0]=F[0][1];
    C[0][0]=F[0][2];
   
   
    F[0][0]=temp[0][0];
F[0][1]=temp[0][1];
F[0][2]=temp[0][2];

circle_anticlockwise('B');


}


void search_cross_location(void)
{
// positions to check for are :01 10 12 21
d=0;
for(i=0,j=1;i<=2;i+=2)
{
if(A[i][j]==2)
{
cross_value[d][0]=7;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(B[i][j]==2)
{
cross_value[d][0]=8;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(C[i][j]==2)
{
cross_value[d][0]=9;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(D[i][j]==2)
{
cross_value[d][0]=10;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(E[i][j]==2)
{
cross_value[d][0]=11;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(F[i][j]==2)
{
cross_value[d][0]=12;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}
           
if(d==4)
{
break;
}
}

for(i=1,j=0;j<=2;j+=2)
{
if(A[i][j]==2)
{
cross_value[d][0]=7;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(B[i][j]==2)
{
cross_value[d][0]=8;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(C[i][j]==2)
{
cross_value[d][0]=9;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(D[i][j]==2)
{
cross_value[d][0]=10;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(E[i][j]==2)
{
cross_value[d][0]=11;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}

if(F[i][j]==2)
{
cross_value[d][0]=12;
cross_value[d][1]=i;
cross_value[d][2]=j;
            d+=1;
}
if(d==4)
{
break;
}
}
}

void calculate_edgepairs(void)
{
edgepair[0]=A[1][0]*10+D[1][2];
edgepair[1]=A[0][1]*10+E[1][0];
edgepair[2]=A[1][2]*10+B[1][0];
edgepair[3]=A[2][1]*10+F[1][0];
edgepair[4]=B[0][1]*10+E[2][1];
edgepair[5]=B[1][2]*10+C[1][0];
edgepair[6]=B[2][1]*10+F[0][1];
edgepair[7]=C[0][1]*10+E[1][2];
edgepair[8]=C[1][2]*10+D[1][0];
edgepair[9]=C[2][1]*10+F[1][2];
edgepair[10]=D[0][1]*10+E[0][1];
edgepair[11]=D[2][1]*10+F[2][1];

for(d=0;d<=11;d++)
{
printf("\nEdge pair %d : %d\n",d,edgepair[d]);
}
}


Friday, 26 February 2016

I,The Curious Engineer,cordially invite you to witness the holy matrimony of ARM and 8051.

I,The Curious Engineer,cordially invite you to witness the holy matrimony of ARM and 8051.

All this while,I was majorly involved with writing the code, that the hardware of my project was left untouched.
Now that I have reached the first milestone in my project (the program is now successfully able to solve the cross!!) ,my attention moved on to the hardware.

The initial design was of three arms, two of which are fixed (one on the left and right) and one on the bottom that is retractable.Two of my teammates took on the task of designing the arms, but a couple of weeks ago,at the college workshop, one of them got a very deep and unfortunate cut on his finger while trying to bend the sheet metal, tightly gripped to the vice.
This incident made us reconsider the use of three arms and consider if use of six arms would be better.
In the latter, we plan to connect the shaft of the stepper motor to the center of each side of the cube.
This eliminates the need of building the retracting fingers mechanism and hence, avoid further injuries.

The reason for preferring stepper motors over servo motors are
1. Steppers have high torque( mine are 4-kg/cm each.)
2. Stepper motors are very precise. Servo motors have a ± 1° offset angle.
3. Stepper cost < Servo cost. (Not true every time, but this time, since I bought the parts in bulk.)
4. Servo motors with 360° are unavailable.180° is the maximum.

A fresh piece of a stepper motor is very expensive. So, it is advisable to buy the steppers that have been scrapped from old broken down machinery, to keep the costs low.
(Works for me every time.)

The six steppers that I now have are all 5 wire, Unipolar motors with a step angle of 1.8°.
To drive these motors, ULN2003A IC's are being made use of.
The ULN's are a package of 7 Darlington pairs array with each capable of delivering a maximum current of 500ma.

Herein lies the problem!!
The ULN2003's are all 5V logic based IC's and the FRDM-KL25Z is 3.3V logic based.

One solution would be to use a 3v to 5v converter.The converters however are expensive (150 rupees).
The solution I want to use is to use a AT89S52 chip coupled with the FRDM board.
The output of the FRDM board is read in by the AT89S52 and correspondingly outputs the same to the ULN2003A.

                         FRDM(3.3V) ---> AT89S52(5V) ---> ULN2003A ---> Stepper motor


  

Wednesday, 24 February 2016

Codewarrior - Let's print "The Curious Engineer!!"

In this post,lets understand how to initialize the Serial Communication terminals of the FRDM board and print "The Curious Engineer!!".

After opening the Codewarrior IDE,(and waiting for the project/s to be opened),let's initialize some peripherals,in this case,the "Console".

The pictures say it all!!













Sunday, 7 February 2016

CodeWarrior-Processor configuration

In this post,let's go through the different changes that have to be performed in order to "configure the processor".

1. Create a project( I call mine "trial")

2. As I have multiple projects open,its good to double click on trial to make sure that the processor does these changes to trial and not my previous projects.





Once System oscillator 0 is enabled,you will be bombarded by yellow and red colored warnings.
It's nothing to worry about.Once the changes shown below are performed,the warnings will go.




Congratulations.You are done configuring your processor.

One more thing:The following steps are not mandatory but are advisable to perform as they will help rid us of any unseen/unknown bugs in the future.

Phase 2 of Cube solver-Let's get Coding!!

There are a few things to consider before starting off with the code.
Let's perform some rotations with the cube and note down the changes undergone by each side.








Decoding from my notes above we see that when column_toward(0) is performed, A rotates clockwise and column 0 of B,D, E and F rotate one step downwards.Same with column_toward(2),wherein C rotates anticlockwise and column 2 of B,D,E and F rotate one step downwards.

D has a strange characteristic and that is it is not like other matrices.
column[0] of other matrices = D[2];
column[1] of other matrices = D[1];
column[2] of other matrices = D[0];

This is a very important point to notice without which huge problems will appear in our code.Whenever there is anything weird near D,it is to correct this "change of columns".

row_right(0) is equivalent to U'.row_right(2) is equivalent to D and vice versa with row_left.

array format
        E
     A  B  C  D
        F


color - number code
yellow 1
orange 2
white  3
red    4
blue   5
green  6

matrix name - number code
 A 7
 B 8
 C 9
 D 10
 E 11
 F 12

This is where the 6 faces of the cube is initialized.As the camera module is not interfaced, every location is manually entered before solving.
This initialization represents a solved cube

int A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
int B[3][3]={{2,2,2},{2,2,2},{2,2,2}};
int C[3][3]={{3,3,3},{3,3,3},{3,3,3}};
int D[3][3]={{4,4,4},{4,4,4},{4,4,4}};
int E[3][3]={{5,5,5},{5,5,5},{5,5,5}};
int F[3][3]={{6,6,6},{6,6,6},{6,6,6}};

These were not initialized in one shot but as the code progressed.Use of each variable will be seen in the appropriate function definitions.

int matrix_name[3][3];
int temp[3][3];
int temp1;
int temp2;
int i;
int j;
int d=0;
int count=0;
int cross_value[4][3];

void print_matrix(int *);     //prints out a single matrix
void print_matrices(void);  //prints out all 6 matrices

//this function performs the R,R' and L,L'

void column_away(int);
void column_toward(int);

//this function performs U,U' and D,D'

void row_right(int);
void row_left(int);

//this function performs F and F'

void front_clockwise(void);
void front_anticlockwise(void);

void circle_clockwise(char);
void circle_anticlockwise(char);

void change_matrix_name(int,char);

void check_cubes_correctness(void);


The entire section of code above are the global declarations.

//prints a single matrix

  void print_matrix(int *a)
 {
  for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{

printf("%d\t\t",*a);
a++;
}
printf("\n");
}
  printf("\n\n");
  }
 


//prints all the 6 matrices

void print_matrices(void)
{
printf("Matrix A\n");
print_matrix(A);

printf("Matrix B\n");
print_matrix(B);

printf("Matrix C\n");
print_matrix(C);

printf("Matrix D\n");
print_matrix(D);

printf("Matrix E\n");
print_matrix(E);

printf("Matrix F\n");
print_matrix(F);
}


void column_away(int j)

{
//stepper turn mechanism
for(i=0;i<=2;i++)
{
temp[i][j]=E[i][j];
E[i][j]=B[i][j];
B[i][j]=F[i][j];
if(j==0)
{
F[i][j]=D[i][j+2];
D[i][j+2]=temp[i][j];
}

if(j==2)
{
F[i][j]=D[i][j-2];
D[i][j-2]=temp[i][j];
}

}

if(j==2)
{
circle_clockwise('C');
}
if(j==0)
{
circle_anticlockwise('A');

}


}

void column_toward(int j)
{
//stepper turn mechanism

for(i=0;i<=2;i++)
{
if(j==0)
{
temp[i][j]=D[i][j+2];
D[i][j+2]=F[i][j];
}
if(j==2)
{
temp[i][j]=D[i][j-2];
D[i][j-2]=F[i][j];
}


F[i][j]=B[i][j];
B[i][j]=E[i][j];
E[i][j]=temp[i][j];
}

if(j==2)
{
circle_anticlockwise('C');
}
if(j==0)
{
circle_clockwise('A');
}

}

void row_right(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=D[i][j];
D[i][j]=C[i][j];
C[i][j]=B[i][j];
B[i][j]=temp[i][j];
}
if(i==0)
{
circle_anticlockwise('E');
}
if(i==2)
{
circle_clockwise('F');
}
}

void row_left(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=B[i][j];
B[i][j]=C[i][j];
C[i][j]=D[i][j];
D[i][j]=temp[i][j];
}
if(i==0)
{
circle_clockwise('E');
}
if(i==2)
{
circle_anticlockwise('F');
}
}


void circle_clockwise(char a)
{
change_matrix_name(0,a);
temp1=matrix_name[0][0];
matrix_name[0][0]=matrix_name[2][0];
matrix_name[2][0]=matrix_name[2][2];
matrix_name[2][2]=matrix_name[0][2];
matrix_name[0][2]=temp1;
temp2=matrix_name[0][1];
matrix_name[0][1]=matrix_name[1][0];
matrix_name[1][0]=matrix_name[2][1];
matrix_name[2][1]=matrix_name[1][2];
matrix_name[1][2]=temp2;
change_matrix_name(1,a);

}

void circle_anticlockwise(char a)
{
change_matrix_name(0,a);
temp1=matrix_name[0][0];
matrix_name[0][0]=matrix_name[0][2];
matrix_name[0][2]=matrix_name[2][2];
matrix_name[2][2]=matrix_name[2][0];
matrix_name[2][0]=temp1;
temp2=matrix_name[0][1];
matrix_name[0][1]=matrix_name[1][2];
matrix_name[1][2]=matrix_name[2][1];
matrix_name[2][1]=matrix_name[1][0];
matrix_name[1][0]=temp2;
change_matrix_name(1,a);
}


void check_cubes_correctness(void)
{
    //The logic of this function is that when you consider a solved cube in my arrangement,
//side A is full of 1's,
//-||- B ----||-----2's
//-||- C ----||-----3's and so on.
//this function just sums up each od the numbers.
//if the value ends up to be 189,the cube is valid
printf("count = %d \n\n", count);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
count=count+A[i][j]+B[i][j]+C[i][j]+D[i][j]+E[i][j]+F[i][j];
}
}
printf("count = %d \n", count);
if(count==189)
{
printf("\n valid cube\n\n");
}
else
{
printf("\n invalid cube\n\n");
//add code to rescan all 6 sides again
}

}

void change_matrix_name(int b,char a)
{
int *c;
if(a=='A')
{
c=A;
}
if(a=='B')
    {
c=B;
    }
if(a=='C')
{
c=C;
}
if(a=='D')
{
c=D;
}
if(a=='E')
{
c=E;
}
if(a=='F')
{
c=F;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(b==0)
{
matrix_name[i][j]=*c;
c++;
}
if(b==1)
{
     *c=matrix_name[i][j];
     c++;
}
}
}
}

void front_clockwise(void)
{
//stepper turn mechanism
circle_clockwise('B');
temp[0][0]=A[0][2];
temp[0][1]=A[1][2];
temp[0][2]=A[2][2];
A[0][2]=F[0][0];
A[1][2]=F[0][1];
A[2][2]=F[0][2];
F[0][0]=C[2][0];
F[0][1]=C[1][0];
F[0][2]=C[0][0];
C[2][0]=E[2][2];
C[1][0]=E[2][1];
C[0][0]=E[2][0];
E[2][2]=temp[0][0];
E[2][1]=temp[0][1];
E[2][0]=temp[0][2];
}

void front_anticlockwise(void)
{
//stepper turn mechanism
circle_anticlockwise('B');
temp[0][0]=A[0][2];
temp[0][1]=A[1][2];
temp[0][2]=A[2][2];
A[0][2]=E[2][2];
A[1][2]=E[2][1];
A[2][2]=E[2][0];
E[2][2]=C[2][0];  
E[2][1]=C[1][0];   
    E[2][0]=C[0][0];   
    C[2][0]=F[0][0];
    C[1][0]=F[0][1];
    C[0][0]=F[0][2];
   
    
    F[0][0]=temp[0][0];
F[0][1]=temp[0][1];
F[0][2]=temp[0][2];
}






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.