This project answers one question: when a robot plays a song on a piano, where should its hand be at each moment, and which finger presses each key?
A hand covers about one octave, it moves at a limited speed, and every note
must be reachable when it is played. A position that is convenient now can
also leave the hand badly placed for the next few notes. This document
explains how the program handles that, without code. The code is in
findOptimalHandPos.py, and the dashboard/ shows it running step by step.
At each note there are several places the thumb could sit and still reach that note or chord. Each move between notes has a cost. Longer moves cost more, moves faster than the hardware allows cost much more, and awkward fingerings add a small cost. The goal is the lowest total cost for the whole song, not the lowest cost at each note separately.
This is similar to planning a road trip: the cheapest next stop is not always part of the cheapest overall route.
There are far too many possible sequences to compare one by one. The program avoids this with one observation: for any position at any note, only the cheapest way of arriving there matters. Any more expensive way of reaching the same position can never lead to a better result later, so it is dropped immediately.
The program moves through the song one note at a time. For each candidate position, it keeps the cheapest way of reaching it and records which earlier position that came from. At the end, it takes the cheapest final position and follows those records backward to recover the full plan.
This method is the Viterbi algorithm. It is also used in speech recognition and route planning. It solves each song in under a second.
The split point between the hands affects how hard each hand’s part is, so it is not fixed at middle C. The program tries a range of split points, runs the full search for both hands at each one, and keeps the split with the lowest combined cost. It first checks the whole keyboard at wide intervals, then checks closely around the best result.
Some chords are wider than one hand can reach in any position, even with stretching. In that case the program reports that the chord is unplayable instead of producing an invalid plan. The dashboard’s right-hand-only mode shows this: with one hand covering every note, some songs’ widest chords become unplayable.