# Lego

### Tips and tricks for Lego Mindstorms Robotics

David Gilday's MindCuber Rubik's cube solver is truly amazing. I use it to attract young kids to programming. I tryed the NXT version with my old Rubik's cube from the 80's. Since my cube is a bit old, it is not smooth enough (even after using a silicon lubricant on it), so I decided to buy a new cube. The good news is that the new cube is more slick (it has colored plastic plates instead of stickers), but the bad news is that the colors are *slightly different* !! This has terrible consequences for MindCuber: the colors are different enough from the original ones to make the MindCuber program fail when scanning the cube.

I developed a new algorithm (that I called "ColorSense"), that can work for both "plain old 80's cube" and newer cubes. Both the compiled "rxe" program and source files (original David Gilday's MindCuber with my "ColorSense" algorithm grafted in it) are included in the following ZIP file:  MindCuberWithColorSense.zip

The algorithm first obtains the (r,g,b) triplets for each square of the cube (using David Gilday's routines), then transforms them into a 2D color space (I observed that intensity values obtained with the lego color sensor were not very reliable so I projected all colors into a 2D triangle). Then the "clusters" of points in this 2D space that correspond to the six colors are found, using an iterative algorithm (called 'k-means clustering'), that is very simple to implement. The good point is that the same algorithm can recognize both 80's cubes and recent ones, without needing any parameter tuning. I did not test it with cubes that have a black face. It failed once in my experiments, when I had too much direct lighting on the cube (but it seems to be robust in most cases).