The drivers to develop my project were:
- Rubik´s cube is a "best seller" category toy, more than 350 millions of units
- Time to solve by humans has been decreasing from 22 seconds in 1982 to 3.47 second in 2018
- Brilliant mathematicians as Kociemba, Rokiki and Fridrich has been fascinated with the toy - at adult age!!!
- There are people solving cubes blindfolded - 48 cubes in less than one hour
- Opencv and Python are subject I wanted to know, so I choose this challenge
- I need a pandemic project to maintain soul balance and my mind working in new knowledge as a mechanism to use the spare time
Conceptualization:
I took the Matt2uy design and optimize the image recognition and algorithm to solve by implementing the Herbert kociemba algorithm.
Process of development
It was an instinctive and observation process of the mechanics -assay and error - 4 models I built until found the more accurate and with more repeatibility featuring.
The idea was to built knowledge based on existent knowledge, so Instructables, youtube, github were very important knowledge's sources.
Results
Implementing some changes I got to save about 20-24 minutes solving the cube - Average time is 6 to 8 minutes to solve.
If you are looking for speed solving, please see my other project, this is more about strategies to solve a problem, learning and patient.
This video summarize, as integrated view, what I did:
Process:
Cube images are captured by a python program module, then color extraction for each sticker is proceesed by other program module and finally the cube is solved by Kociemba algorithm, the solution is sent to Arduino trough a serial port. Once the solution is received the arduino code start to processing the movements, after 6-8 minutes the process finish and the robot show all the faces of the cube solved.
Algorithmic Kinematics: The Python and Arduino Pipeline
A human solving a Rubik's Cube relies on physical pattern recognition. To make a machine do exactly this involves astronomical mathematical complexity uniting high-level optical engineering, the Kociemba algorithmic solving matrix, and terrifyingly synchronized mechanics! The Rubik's Cube Solver Robot acts as a unified master-slave architecture! A PC or Raspberry Pi captures exactly 54 color-square states via an intensive WebCam array, mathematically generates an optimized 20-move solution string (e.g., U2 R' F B'), and aggressively fires that entire Serial payload into an Arduino Uno slave! The Uno violently decodes the string and whips an intricate matrix of 4 to 6 high-torque Stepper/Servo motors completely solving the cube physically in literal seconds!
The Python OpenCV Visual Analysis Pipeline
Before any motor spins, the robot must perfectly memorize the scrambled state.
- The robot arms meticulously rotate the cube, presenting all 6 faces to a fixed USB Camera.
- OpenCV (Python) aggressively scans the image, generating 9 distinct Regions of Interest (ROI) per face.
- The incredibly volatile RGB values are translated natively into HSV (Hue, Saturation, Value) to precisely evaluate "Orange" even under terrible bedroom lighting conditions!
- It compiles a massive string of colors:
"UUUURRRRRFFFDDD..."and shoots it completely into a heavy-weight external Kociemba Python library to physically calculate the mathematical solve path instantaneously!
Orchestrating The Stepper Motor Array (Arduino Code)
Parsing the solution string "R2 L' F" requires the Arduino to map exact mechanical maneuvers utilizing the <AccelStepper.h> framework!
R2means the Right face must violently spin180 Degreesexactly.L'means the Left face must aggressively spin90 Degrees Counter-Clockwise!
void executeMove(String currentMove) {
if (currentMove == "R") {
// 90 Degree turn clockwise on Stepper Right! (Assume 200 steps/rev)
stepperR.moveTo(stepperR.currentPosition() + 50);
while (stepperR.distanceToGo() != 0) {
stepperR.run(); // Fast non-blocking acceleration sweep!
}
}
else if (currentMove == "R'") {
// 90 Degree turn counter-clockwise!
stepperR.moveTo(stepperR.currentPosition() - 50);
while (stepperR.distanceToGo() != 0) { stepperR.run(); }
}
}
Physical Solving Infrastructure Architecture
- Arduino Mega 2560 (An Uno simply does not pack the massive RAM and Digital IO pins required to concurrently handle 4 to 6 massive A4988 Stepper Motor Driver chips synchronously!).
- 4x NEMA 17 Stepper Motors or high-torque Metal Gear Servos (MG996R). (Steppers are highly preferable for instantaneous 90° snaps without horrific jittering errors that cause the Rubik's cube mechanism to jam entirely and shatter!).
- Custom 3D-Printed Kinematic Frame (The motors must literally grip the center pieces of the cube with millimeter perfection).
- A Desktop PC or Raspberry Pi 4 (Executing the terrifying weight of the Python OpenCV and Thistlethwaite/Kociemba Algorithm processing).
- A 12V High-Amperage Power Supply (To aggressively prevent Stepper driver brownouts when 4 axes lock their holding-torque instantaneously).
Next challenge
Based on this project I moved forward and got from 6 minutes to 30 seconds, 18 seconds time to solve. In other video I explain how can we reach 18 seconds. Please watch, below, my comparison video:
What for?
This robot is designed to teach:
1. Patient, resilience, frustration management
2. Repeatability and Accuracy, applied to robots
3. Rubik's cube notation, solving algorithms
4. Maths: matrixes theory, statistical, group theory,
5. Programming: Arduino-Python
6. Image processing with openCV