Wednesday 6 July 2011

Tilted Twister Like Robot - Lego Mindstorms NXT 2.0 Rubik's Cube Solver Using Matlab

Introduction
The purpose of this work is get a lego robot solve a Rubik's cube. All you need to do this is a Lego Mindstorm NXT 2.0 kit, matlab and obviously a Rubik's cube. Matlab is used to control the robot via USB port, since the robot doens't move around bluetooth connection it's only a plus and it's not implemented in this work. To get matlab working with the NXT you need to install the RWTH - Mindstorms NXT Toolbox which you can download from here. Instruction for the installation are in the web page of the version you choosed, it's possible install the toolbox in MacOS, Linux and Windows. Basically this project works in MacOs and Windows but it can work, maybe with some modify, also in Linux. Once you got your environment ready to work you can start to create your Rubik's cube robot solver.


The Robot
Fig. 1: Side view of the robot.
The robot which you can see in fig. 1 has a structure composite by three motors and two sensors. Basically the robot works by means of a color sensor, a rotatable box and an arm, all shown with an arrow in fig. 1. The three motors are used to move around the color sensor, turn the box and for the arm which has to roll the cube moving forward. The second sensor, the ultrasonic one, is not necessary to this work but i found it useful to have a landmark with respect to the cube, it is not shown in fig. 1 but you can see it just under the arm.
 
Buildings Instructions
Being a tilted twister like robot you can find the build instructions online here and in the folder with my matlab code where you can find the file pdf with all steps needed for the construction. Done this I made some modify to my robot because I think, after some tests, improves the performance of the robot decreasing the probability of fails dues to the mechanicals parts. Mainly i done three changes:
1) the rod fixed at the back wheel is addressed downwards as shown in fig. 2, this to reduce the slope of the robot.
Fig. 2: Back wheel view of the robot.
2) I added two wings to the arm where each one has a rubber band to keep more expanded the arm as shown in fig. 3. This because the width of the arm was too much precise with respect the width of the cube.
Fig. 3: Arm view of the robot.
 3) the rod that connect the arm with the rods attached to the back motor is connected to the last but one hole as shown in fig. 4. This because in this way the arm grabs the cube better.
Fig. 4: Arm connection view of the robot.

The Code
I wrote the code in matlab, and not to be fast to solve the cube. I tried to write code to make the robot reliable as much as possible. In example: the arm always do a double check of the cube position to be sure after a roll of the cube or a turn of the box that the cube is in right position, parallel to the rod where the peg of the box hits; a face of the cube is always rotated in a way that after the rotation of 90 degrees the peg hits the stop to have a more precise rotation and this is done 2 times. To find a solution is used an external program which apply the Herbert Kociemba'a algorithm. The program need the initial state of the cube in singmaster notation and return a string of letters, each letters represent a face of the cube to turn. If a " ' " follow the letter then the face has to be rotated in counter-clockwise direction otherwise clockwise. The letter are always with respect to the front face you got when the cube was in the initial state. Remember to start this program alone the first time, this because the first time it needs to create a "pruntable", a file of 500 Megabytes which it needs in future times you will use it, it will take about 5 minutes to create the file. My code has a function which keep updated the state of the cube with respect to the ultrasonic sensor to remember which original face is in front of it. Each function is well commented so shouldn't be problems to understand the code. I used some code from the "Cube Solver Project" from the Clemson University where i found some useful functions like the one which converts the initial state structure of the cube in singmaster form. Also the functions to manually insert the cube in the folder "insertCube" is a lot useful. To start the whole project just run, under matlab, the script "solveRubikCube.m".


Conclusions
This system clearly is not 100% reliable, I tried to do my best to improve the robot from this point of view and maybe you can improve it more and more applying changes to the code or to the robot structure. For the rest the code in general (without mechanicals fails) can solve all possible cube situation with more or less moves. I want to precise again that I didn't write the code to be fast also because usually fast mean less precision and in this case less precision to roll the cube and turn its faces, hence more probability of fails. It's also clear that probably you will need to set up by your own the right values for the movements inside the code because each case can be different, in example not all cubes have the same resistance when rotated. For any problems or questions contact me by email or comment this post, thanks for reading and have fun.

External Resource
Video of the robot while working here or click below

Download link for the code here.

4 comments:

  1. awesome work bro,,,,,,,really nice,,,,,

    ReplyDelete
  2. Hello, I build the robot, and followed the instructions. But everytime I turn on the program the turn table keeps rotating. I don't know what is the problem. I stop the turn table and load the cube, it says thank you, checks it once and then starts rotating again, and keeps on rotating.
    I really don't know what to do.

    Can you Help?

    Thank you
    Shobhit

    ReplyDelete
  3. I have the same problem! does anyone have a solution?

    ReplyDelete
  4. Did you put the stopping part on it?

    ReplyDelete