Introduction: Rubik's Cube Solver
So here's a project I've been working on for a while...
This robot that can solve a Rubik's cube using Arduino.
I learned how to solve a Rubiks cube last year, and I was also into Arduino, so eventually I ended up with an idea to make my own Rubik's cube solver.
Like many people searching for "Rubik's Cube Solver" on Google, one of the first robots I found was the Tilted Twister design by Hans Andersson:
I looked around at some other Rubik's cube solvers, but I liked that design because of it's (relative) simplicity, so I built a similar version of it with popsicle sticks, an Arduino and 2 servos.
In the end, my robot takes about 20 minutes to enter the cube state and solve.
Step 1: Materials
Electronics:
- Arduino UNO R3
- 2 servos (I used Hitec HS-311's)
- wires
- USB cable
You can get all of these parts from any robotics store. I got them from canadarobotix.com
Hardware:
- popsicle sticks
- wooden skewers
- thin plywood
- wooden wheel (skewer must fit inside the hole)
- hot glue
- paper towel roll
Software:
- Arduino IDE - (Algorithms in C++)
- Python 2.7+ and Tkinter - (GUI)
- Pyserial
Step 2: Electronics
Wiring up the servos to the Arduino is pretty straightforward
1. Connect the yellow (signal) wires from the push and rotation servos to pin 6 and 9, respectively.
2. Connect the positive and negative wires to the 5v power source and ground. Sometimes the servos had jitters, so I think a few capacitors could have smoothed out the current.
Step 3: Mechanical Design
I looked at a few designs but settled on the Tilted Twister because it only required 2 servos to operate, which wasn't as complex mechanically as other robots such as the Cubestormer.
So here's how the mechanism basically works:
1. A platform holds and rotates the cube.
2. The arm pushes and holds the cube.
When I first tried to build the solver, I tried to follow building instructions here:
But I was pretty lazy and didn't follow the instructions, so I made a wooden replica with a few tweaks.
I haven't documented every step of the build process, but I think you can reproduce the tilted twister design with a few tweaks:
1. Slanted sticks to prevent the cube from snagging.
2. To build the arm joints,
- I drilled a hole into ends of the popsicle sticks.
- I cut out 2 cm of a wooden skewer.
- I inserted the skewer into the sticks
- Glued wooden wheels that act as washers on each end.
Honestly building the physical solver was a lot harder and complex than I expected, so if you have any questions on how I built the solver, just ask.
Step 4: Arduino Sketch - Cube Solving Algorithm
[Code: https://github.com/matt2uy/Cube-Solver]
So basically the code I wrote uses combinations of pushes, holds and platform rotations to apply algorithms to the cube.
There are 3 main parts of my code: the algortihm, GUI (enter cube state) and the serial communications
Let's start with the Arduino sketch. It basically uses algorithms to manipulate the cube colours:
1. There are 6 char arrays represent each face of the cube.
2. Using a basic layer by layer method of solving the Rubik's cube, I made a function, cube_decide(), that goes through each stage or the cube solving process.
3. Within each stage, such as cube_decide_cross() (solving the cross), the program would check for specific locations of colours in the cube, and if an algorithm was matched with the condition, cube notation like 'U' (Up) or entire algorithms, like fix_cross_instance_1() would be run.
4. The cube notation and algorithms control the servo functions, such as push_cube() or rotate_one().
Here's a basic overview of the code structure (layers of abstraction):
Cube Decide functions < Cube Algorithms < Cube Move functions < Servo functions < Move Function
One major breakthrough I had was that I actually simulated the cube's movements in the program. The program reassigns the values in the arrays to simulate and cube rotation. This way, the program can simulate the cube move before it physically executes it.
Step 5: Python GUI
I needed a better way of entering the cube state than directly typing them into the Arduino sketch, so I got the idea of creating a GUI from this robot:
Because I'm new to making Tkinter GUI's (Graphical user interface) and didn't want to make one from scratch, I found a tic tac toe GUI here: http://www.dzone.com/snippets/gui-tic-tac-toe-less...
Then I turned the 3x3 array of buttons into something resembling a cube and added a few buttons. I also added buttons that would change colour each time they were clicked. Finally I made 5 more windows that would pop up one after the other, and transfer the colours entered into a local array in the script.
However, I really slapped the code together in a mess, since I simply copy and pasted 6 separate windows, with almost identical functions. Any suggestions on how I could clean up the code are welcome!
Step 6: Connecting It All - Serial Communications
Once the algorithm in the Arduino sketch and the python GUI were ready, I needed something something that would use the cube state collected from the GUI and transfer it to the Arduino sketch. After the sketch receives the data, it can put the cube state through the algorithms and physically solve the cube.
To do that, I used a library called Pyserial, which helps my python script communicate with the Arduino over a serial interface.
Basically, this is how I coded the the serial communication:
1. After the cube colours were entered in the GUI in Send_Cube_State.py, I converted the data into a string
2. Used a 'handshake', which in this case the Arduino tells the computer it's ready, then the computer transfers the data to the robot.
3. The Arduino converts the string received into chars, which are assigned to arrays in the sketch.
4. Now that the cube colours have been successfully transferred from the computer to the Arduino, the cube colours can be put through the algorithms and solve the Rubiks cube.
Step 7: How to Use
If you're trying to build the robot yourself, or just trying out the code, here's a checklist to help you out:
1. Download and install these packages and applications
- Arduino IDE (http://arduino.cc/en/Main/Software)
- Python 2.7 (Tkinter is included) (https://www.python.org/downloads/)
- Pyserial (https://pypi.python.org/pypi/pyserial)
2. Download the source files here: https://github.com/matt2uy/Cube-Solver
3. Copy and paste Cube_Solver.cpp on to the Arduino IDE.
4. Plug the Arduino in and upload the sketch.
5. Right after uploading, take note of the serial port number at the bottom right corner. (See picture above)
6. In Send_Cube_State.py, change the address in line 18 to match the number on the IDE. (See picture above)
7. Open the command prompt (Windows) or terminal (Mac/Linux)
8. Go to the directory where you put the source files in:
- using the command 'cd', for example:
- cd 'path/to/Cube-Solver'
9. Run the .py script with: python send_cube_state.py
10. If you see a series of 'y's being print on the screen:
- wait about 5 seconds
- Exit using ctrl-c or command-c
- Repeat step 9 again.
- There should be a GUI that shows up.
11. Enter the Cube colours in the correct orientation:
Colour in Front | Colour on Top
1. Yellow | Blue
2. White | Green
3. Blue | White
4. Red | White
5. Green | White
6. Orange | White
12. Put the cube inside the solver in the orientation seen here: http://goo.gl/tSqSpp
13. Click 'Solve!'
14. If that didn't work:
- Copy and paste Cube_Solver_No_GUI.cpp on to the Arduino IDE
- In line 32-54, enter the cube colors in the same convention as in step 11.
- Do step 12
- Upload the sketch on to the Arduino.
216 Comments
Question 1 year ago
Hi, can I use other servo motors. something like SG90 or SG92R?
Question 1 year ago
How can I connect gui to Arduino
2 years ago
PLEASE HELP : Could you send me the CODE of GUI I can not download it
My email: khaledmachatronics@gamil.com OR khaledmurrar77@gmail.com
2 years ago
PLEASE HELP : Could you send me the CODE of GUI I can not download it , please !
this is my email : kaisarhk007@gmail.com
3 years ago
can you please send us the link of all the hardware you used in the projects... and which kind of wires you are using?
Question 3 years ago on Step 1
Hiee...we are a team of 4 mechanical engineers and have been given this project on Rubik’s cube solving robot and none of us knows python or C++ . Please can you help us in building this robot from a scratch.
Thanking you in anticipation.
Regards
5 years ago
How can u enter the cube state without the python gui?
Reply 3 years ago
Preset values in the Arduino code
Reply 3 years ago
How can i preset that values in the arduino code !
show me how to do it please
8 years ago on Introduction
Hi Matt, I have a question. I am currently building my robot and I keep getting a blank GUI every time I run the send_cube_state.py code on my terminal. I'm using a mac. What should I do to make the colors appear on the GUI? Here is a screenshot so you can see if there is a problem. Any help is much appreciated. Thanks in advance!
Reply 3 years ago
HELP : Could you send me the CODE of GUI i can not download it , please !
this is my email : chikhinazim@gmail.com
Reply 8 years ago on Introduction
Sorry for the late reply, my email notifications didn't work.
Anyway, I'm not sure how to fix this problem because I don't use a mac. It might be a problem with tkinter, but i'm not totally sure.
Someone else had the same issue here: https://github.com/matt2uy/Cube-Solver/issues/4 so it could still work if you memorized the order of the colours, or you could tinker with the python script.
Reply 8 years ago on Introduction
I considered using different versions of mac OS and none of them worked. I switched to a windows PC and the GUI showed up! Complete with all set of colors! I will update you when I finish building my project. Thanks by the way!
7 years ago
Hi, I just finished building the Cube solver and now made my way to the coding part, it seems that the tic tac toe GUI isn't available to download anymore. Is there another GUI link available?
Reply 3 years ago
HELP : Could you send me the CODE of GUI i can not download it , please !
this is my email : chikhinazim@gmail.com
Question 5 years ago on Step 5
I have many problems with this step, the link you have already been deleted.
koala.mitzy@hotmail.com
Answer 3 years ago
HELP : Could you send me the CODE of GUI i can not download it , please !
this is my email : chikhinazim@gmail.com
Question 3 years ago on Step 3
Hello matt2uy! I have difficulty in building the mechanical design for the rubic's cube solver. Could you please help me with it by providing more images of the robot ( The image for attachment of servo motor wheel with popsicle stand, more images of popsicle stand and the model required for servo motor in robot etc.). If possible, also provide me more steps for building the mechanical design.
I hope you take my concern seriously and provide me feedback of this message as soon as possible.
Thank you
4 years ago
rotate one two and three are not working properly
Reply 3 years ago
hello did you find the solution?