πŸ₯‡1ST PLACE β€” EMR EMBEDX COMPETITION

Rubik's Cube
Solver Bot

A full-stack hardware + software system combining OpenCV computer vision, Kociemba's two-phase algorithm, and a custom mechanical translation engine to physically solve a Rubik's Cube using Arduino-driven servo motors.

~20

Moves to solve

54

Character state string

3

Servo mechanisms

SCROLL
πŸ₯‡

1st Place

EMR Embedx β€” Embedded Systems Competition

Category

Embedded Systems & Robotics

Team Size

4 Members

My Role

Team Lead & Computer Vision Engineer

Competed against teams with a fully functional autonomous Rubik's Cube solving robot β€” demonstrated live scanning, solving, and mechanical execution on stage.

Kavy PagdharPrabhav DaveJay ShouryaDevansh Arora
01 / PROBLEM

The Core Challenge

Bridging the gap between mathematical optimality and physical mechanical constraints.

∞

Theoretical Solver

Kociemba Two-Phase Algorithm

Kociemba's algorithm produces an optimal solution in ~20 moves from any scrambled state. However, it operates in an abstract mathematical space β€” any face can be rotated from any direction, regardless of physical accessibility.

// Example solver output
solution = "U2 R F' D B2 L U R2 D2"
// U=Up, R=Right, F=Front, D=Down β€” unrestricted
βš™

Physical Robot

3-servo constrained mechanism

The physical robot can only interact with the cube from one fixed orientation using three servo mechanisms. Every mathematical move must be translated into a valid sequence of flip, block, and rotate operations.

Flipper

Tilts cube 90Β°

Blocker

Holds position

Rotator

Spins bottom face

The Fundamental Mismatch

A solver output like R F' U tells the robot to rotate the Right face, then the Front face, then the Up face β€” but physically, the robot can only ever access the bottom face. To rotate the Right face, the cube must first be flipped to the correct orientation, the move executed, then potentially re-oriented for the next move.

Naively translating each move independently generates redundant flip-unflip sequences that cancel each other out β€” tripling execution time. The key innovation is a cost-optimized, stack-based reduction algorithm that eliminates these redundancies before they reach the hardware.

02 / PIPELINE

System Overview

Four-stage pipeline from physical cube to solved state β€” vision, cognition, translation, and execution.

01πŸ“·
01

Scan

OpenCV + Webcam

Webcam captures each of the 6 cube faces. OpenCV detects color regions using HSV masking. Each of 54 stickers is classified into one of 6 colors and encoded into a positional string.

HSV color maskingContour detectionColor classificationFace ordering logic
54-char state string
02🧠
02

Solve

Kociemba Algorithm

The 54-character cube state is passed to a Python implementation of Kociemba's Two-Phase Algorithm. Phase 1 reduces the cube to a subgroup; Phase 2 solves within that subgroup optimally in ≀20 moves.

Two-phase searchPruning tables≀20 move guaranteeGod's Number bound
Move sequence string
KEY INNOVATION
03βš™οΈ
03

Translate

Custom Stack Algorithm

The KEY INNOVATION. Maps abstract moves (U, R, F, D, L, B) to physically valid servo operations. Uses cost-based optimization and stack-based cancellation to minimize redundant flip operations.

Move-to-servo mappingStack cancellationCost optimizationConstraint solving
Serial command list
04πŸ€–
04

Execute

Arduino + Servos

Optimized commands are sent over USB serial to an Arduino Uno. The Arduino interprets each command and drives the three servo motors β€” flipper, blocker, and rotator β€” in precise timed sequences.

Serial protocolServo PWM controlTiming sequencesState feedback
Physical cube solved
03 / ARCHITECTURE

System Architecture

Three decoupled layers β€” each independently testable, each solving a distinct class of problem.

πŸ‘
Layer 1

Computer Vision

OpenCV + Python

  • Webcam captures each of the 6 faces sequentially
  • HSV color space used for illumination-invariant detection
  • Detection of each of the 9 stickers per face
  • K-means clustering maps detected colors to 6 canonical cube colors
  • Face ordering follows a fixed scanning protocol (Uβ†’Rβ†’Fβ†’Dβ†’Lβ†’B)
  • Output: 54-character string
πŸ”’
Layer 2

Kociemba Solver

Two-Phase Algorithm

  • Phase 1: Reduces cube to subgroup G1 (edges oriented, corners partially solved)
  • Phase 2: Solves within G1 subgroup using only half-turn moves
  • Precomputed pruning tables enable fast look-ahead in search tree
  • Guarantees solution in ≀20 moves (God's Number proven 2010)
  • Python `kociemba` library wraps the optimized C implementation
  • Typical solve time: <100ms on modern hardware
PRIMARY INNOVATION
βš™
Layer 3 β€” KEY

Mechanical Translation

Custom Stack Algorithm

  • Maps 18 possible cube moves (U/D/F/B/L/R Γ— CW/CCW/180Β°) to servo sequences
  • Maintains virtual orientation state β€” tracks current cube face at bottom
  • Stack-based cancellation removes inverse operation pairs before emission
  • Cost table assigns operation weight per face; selects minimum-cost path
  • Lookahead optimization leaves cube in best position for next move
  • Outputs compact binary command list sent over USB serial at 9600 baud
05 / ALGORITHM

Algorithm Deep Dive

Why Kociemba alone is insufficient β€” and the custom translation layer that bridges the gap.

I

Why Kociemba Alone Is Insufficient

Kociemba's Two-Phase Algorithm solves any Rubik's Cube in at most 20 moves β€” this is mathematically proven (God's Number). It works in an abstract group space where any of the 6 faces can be rotated independently at any time.

The algorithm has no concept of physical orientation β€” it outputs a flat sequence like U R2 F B R B2 R U2... with no knowledge of how those moves will be physically executed.

/* Kociemba output β€” abstract notation */
Phase1: U R F D2 L B
Phase2: R2 U2 D2 F2 B2 L2
// Problem: 'U' requires accessing the TOP face
// Robot can only access the BOTTOM face
// β†’ Must flip cube to bring 'U' to bottom
// β†’ Next move 'R' requires DIFFERENT orientation
// β†’ Naively: flip-back, flip-new = redundant ops
II

Stack-Based Move Reduction

The translation algorithm maintains a virtual orientation state β€” tracking which face of the cube is currently at the bottom (accessible to the rotator servo). Instead of resetting to a canonical position between every move, it carries forward the current orientation and finds the cheapest path from the current state to the required state for the next move.

Without Reduction

Input: U R
// NaΓ―ve translation:
FLIP_FORWARD // U β†’ flip to top
FLIP_FORWARD
ROTATE_CW // execute U
FLIP_BACKWARD // reset
FLIP_BACKWARD
FLIP_FORWARD // R β†’ flip right
ROTATE_CW // execute R
7 operations β€” 2 redundant pairs

With Stack Reduction

Input: U R
// Optimized translation:
FLIP_FORWARD // U β†’ bring top to bottom
FLIP_FORWARD
ROTATE_CW // execute U
// State: top face is now at bottom
FLIP_BACKWARD // direct path to R
ROTATE_CW // execute R
5 operations β€” 28% reduction

The stack tracks pending orientation changes and cancels inverse operations before they are emitted (e.g., a FLIP_FORWARD followed by a FLIP_BACKWARD that accomplishes nothing gets eliminated). This is analogous to peephole optimization in compiler design.

III

Cost-Based Move Optimization

Each face has a different physical cost to access from the current orientation. The algorithm uses a precomputed cost table to choose the minimum-cost transition path. When multiple reorientation paths have equal cost, it selects the one that leaves the cube in the best position for the next move (lookahead optimization).

FaceOp CostPhysical Sequence
D (Bottom)
Direct β€” no flip needed
U (Top)
Flip Γ— 2 + rotate
F (Front)
Single flip + rotate
B (Back)
Flip Γ— 2 opposite
L (Left)
Single flip + 90Β° rotation
R (Right)
Single flip + 270Β° rotation
04 / HARDWARE

Hardware Stack

Three servo mechanisms, one Arduino, and a clean serial protocol β€” minimal hardware, maximum precision.

πŸ”²

Arduino Uno

Main Controller

Receives serial commands from Python host, drives PWM signals to each servo, and manages timing between operations.

ATmega328P @ 16 MHz
14 digital I/O pins
6 PWM channels
USB-B serial (9600 baud)
πŸ”§

Flipper Servo

Cube Orientation

Grips the cube from one side and physically tilts it 90Β° to change which face is accessible to the rotator.

SG90 / MG996R
0°–180Β° range
PWM @ 50 Hz
Tilt cube 90Β° per actuation
πŸ”’

Blocker Servo

Position Lock

Engages to hold the top portion of the cube stationary while the rotator spins only the bottom layer.

SG90 micro
Binary position
Prevents slip
Active during rotation
πŸ”„

Rotator Servo

Face Rotation

Grips and rotates the bottom face of the cube β€” executes the actual cube moves after the flipper has positioned the correct face at the bottom.

MG996R (high torque)
90Β° / 180Β° / 270Β° turns
360Β° continuous mod
Primary execution servo
⚑

Power Supply

5V Regulated

Servos draw significant current under load. A dedicated 5V rail prevents Arduino resets caused by voltage drops during simultaneous servo actuation.

5V @ 3A regulated
Separate servo rail
Arduino USB power
Decoupling capacitors
πŸ’»

Host Computer

Vision + Solver

Runs the computer vision pipeline and Kociemba solver. Connects to Arduino over USB and streams translated commands after solving.

Python 3.10+
OpenCV 4.x
kociemba library
pyserial for comms
πŸ”Œ

Wiring Schematic

circuit diagram

06 / DEMO

See It in Action

Full team walkthrough and live solve demonstration from the competition floor.

Video 01 / Team Presentation

Team Explanation β€” System Walkthrough

Video 02 / Working Demo

Live Demo β€” Autonomous Cube Solve

~20

Avg Moves

Kociemba optimal

9600

Serial Baud

USB-B protocol

30

Vision FPS

OpenCV capture

3

Servos

Flip / Block / Rotate

08 / CONTRIBUTIONS

My Contribution

Individual ownership within a collaborative team project.

PRIMARY OWNERSHIP
πŸ‘

Computer Vision Pipeline

  • Designed and implemented cube state detection pipeline using OpenCV
  • Mapped webcam input to structured 3Γ—3 grid using coordinate-based ROI extraction
  • Converted BGR frames to HSV color space for lighting-invariant color detection
  • Applied morphological operations (opening/closing) to reduce noise and improve segmentation
  • Developed color classification logic to map detected stickers into URFDLB representation
  • Ensured consistent detection across varying lighting conditions through threshold tuning and filtering
  • Generated accurate 54-character cube state string for solver input
πŸ€–

Hardware & Firmware

  • Designed and implemented servo control system using Arduino Uno for cube manipulation
  • Controlled multiple servo motors (flipper, blocker, base rotation) using PWM signals
  • Developed firmware to parse and execute command sequences received over serial communication
  • Implemented synchronized motion logic to coordinate clamping, rotation, and flipping actions
  • Integrated Python backend with Arduino via PySerial for real-time instruction streaming
  • Optimized execution timing to ensure stable operation under mechanical constraints
  • Debugged hardware issues including servo jitter, timing delays, and power stability

Technologies I worked with directly

PythonOpenCVNumPykociembapyserialArduino CServo PWMHSV Color SpaceSerial Protocol

View Source Code

Full source code including OpenCV pipeline, Kociemba integration, custom translation algorithm, Arduino firmware, and wiring schematics.

github.com/code-with-devansh/rubiks-cube-solver
🐍 PythonπŸ‘ OpenCV⚑ Arduinoβš™ C++πŸ”’ NumPyπŸ”£ kociembaπŸ”Œ pyserial🎨 Tailwind CSS