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
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.
为实现魔方识别与自动复原,我们将程序主要分为两部分。
第一部分实现魔方的识别及求解功能,主要用Python编写;第二部分实现控制Arduino驱动步进电机实现转动魔方,主要实现为.ino格式的单个Arduino程序。
Written in Python. There are 5 steps:
- Using Keras to build up a Unet model, and carve up the 6 faces of the Rubik's cube.
- Using OpenCV to parse the colors of the bricks.
- A GUI to show the colors, allowing correcting the color.
- Solving the Rubik's cube via Two-phase algorithm.
- 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.
本部分程序采用Python实现。将魔方识别与求解的步骤更具体地分为五个部分:
- 采用Keras库建立Unet模型,实现从摄像头图像中分割魔方的6个面。感谢RubikKeras项目提供的模型。
- 采用OpenCV,解析魔方块的颜色
- 以GUI界面显示魔方色块,提供手工校正魔方色块的功能
- 用Two-phase算法求出魔方最优解
- 将魔方解以字符串形式通过串口传给Arduino板
首先通过电脑前置摄像头对魔方的三个面进行图像捕捉,使用机器学习分两次完成对魔方六个面的识别,配合OpenCV对图像中魔方块的颜色进行实时检测并读取HSV值,并输出六个面54块颜色,通过Tkinter做出可视化页面,显示解析结果。获得魔方数据后使用Herbert Kociemba's two-phase algorithm对魔方进行求解,得解后将解以字符串的形式通过串口传给Arduino,Arduino控制步进电机转动魔方将其还原。
- 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.
步进电机最开始选择了最为常见的28BYJ-48四相步进电机,在最初的测试中发现其扭矩过小,无法转动魔方,于是更换为扭矩更大的42步进电机,通过L298N驱动。由于需要同时驱动6个四相步进电机,常见的Arduino UNO存在数字信号端口输出不够的情况,因此选择Arduino MEGA2560来进行控制。