A robot to recover Rubik's cube, based on Arduino, using Python combined with Machine Learning and OpenCV.

Thanks to these projects and resources:

• https://github.com/krustnic/RubikKeras
• http://kociemba.org/cube.htm
• https://github.com/g20150120/cubot
• https://github.com/hkociemba/RubiksCube-TwophaseSolver

## 1. Software Part

The function of this robot is divided into two parts: Rubik's cube recognition and automatically recover it.

The first part focuses on recognizing and solving Rubik's cube, which is mainly written in Python. The second part focuses on controlling the Arduino to move the Rubik's cube, which is written in a .ino Arduino program.

### 1.1. Recognizing and solve the Rubik's cube 魔方识别与求解

Written in Python. There are 5 steps:

1. Using Keras to build up a Unet model, and carve up the 6 faces of the Rubik's cube.
2. Using OpenCV to parse the colors of the bricks.
3. A GUI to show the colors, allowing correcting the color.
4. Solving the Rubik's cube via Two-phase algorithm.
5. Sending the solution to the Arduino via serial port.

First, the computer's front camera captures images of every three sides of a Rubik’s cube. The the machine learning program then recognizes the six faces of the Rubik’s cube, with OpenCV to detect the color of each brick and read the HSV value in the image. In real-time, a visualization window outputs colors of 54 bricks on six faces. After obtaining the color data, Herbert Kociemba's two-phase algorithm solves the Rubik's cube. Then, the solution, in the form of a string, is transmitted to the Arduino through the serial port, and the Arduino controls the stepper motor to rotate the Rubik's cube and recover it.

1. 采用Keras库建立Unet模型，实现从摄像头图像中分割魔方的6个面。感谢RubikKeras项目提供的模型。
2. 采用OpenCV，解析魔方块的颜色
3. 以GUI界面显示魔方色块，提供手工校正魔方色块的功能
4. 用Two-phase算法求出魔方最优解
5. 将魔方解以字符串形式通过串口传给Arduino板

## 2. Hardware Part

• Arduino MEGA2560
• 42 Stepper * 6
• L298N to drive

The most common stepping motor-- 28BYJ-48 four-phase stepping motor was initially selected. But in the initial test, it was found that its torque was too small to turn the Rubik's Cube, so it was replaced with the 42 stepper motor with greater torque, driven by L298N. Due to the need to drive 6 four-phase stepper motors at the same time, the commonly used Arduino UNO has insufficient digital signal ports, so the Arduino MEGA2560 is used for control.