Tuesday, October 20, 2015

MindCuber EV3 in Python

MindCuber

If you have an EV3 you have probably watched a video of the awesome MindCub3r robot solving a Rubiks cube.  I too built this robot and made a video :).  One twist is that mine is running ev3dev (Linux) and all of the code is written in Python. I am a big Python fan and would much rather program crazy lego robots in it than I would the graphical programming language provided by lego.


The Code

cavenel on github wrote the original MindCuber python implementation based on a release of ev3dev from early 2014.  It no longer worked on the latest ev3dev though so I started fixings bugs and got the basics working.  I also rewrote most of the code that does the color analysis.

The code is available here:

Color Analysis

The lego color sensor can scan something and return one of a few basic colors (blue, green, yellow, red, etc) but for scanning a Rubiks cube this doesn't work very well.  The reason being certain colors on the cube are too similar, Red vs. Orange are really close, even Blue vs. Green can confuse the sensor because they are both so dark.

There is hope though, you can tell the color sensor to return the raw RGB (Red, Green, Blue) value instead of trying to guess at the color.  I take the RGB values for all 54 squares and figure out which of the six possible colors each square is.  I learned a lot about Color Difference and Rubiks Cube Parity :)

Solution Code

Once you know what color each square is you can feed that data to one of many rubiks cube solving programs out there on the web and they will return a list of instructions that tell you how to solve the cube.  This is some pretty complex stuff, people have done their PhD thesis on algorithms for solving Rubiks cubes.

I did not dive into this in depth, this problem was solved in depth long ago by people a lot smarter than me.  I wanted to keep everything in python though so I grabbed a copy of the following and am using that:
https://github.com/muodov/kociemba