Appendix: Robot Command Encoding

The signal in the right channel of the MP3-player contains the commands that tell the robot how to move and blink. The way the data is encoded is very similar to Morse code. We use short and long pulses to encode zeroes and ones, the building block of binary numbers (just like our decimal numbers that go from 0 to 9). The raw signal that the MP3-player sends is shown below:


The signal starts with a long pulse, marked with R and green above. This long pulse tells the micro-controller that a new command message starts. This message is composed of short- and medium-duration pulses. The short pulses are zeroes, and the long pulses are ones. We call these ones and zeros bits, the smallest unit of information in computers (the MP3-file itself is made up of many many bits). The bits in the signal contain the information about how the robot should move and blink. Specifically, the first, red part containing 01001101, tells the robot at what speed and in which direction the left motor should turn. Then the same for the right motor in blue, and at the end the command for the eight LEDs in orange. So what does 01001101 mean for the left motor? The last 1 is easy: It tells the motor to turn forward. If the last bit is a zero, the motor should turn backwards. The first bits encode how fast the motor should turn in the given direction. These bits are the digits of a binary number. Let us figure out how to read the number. Before we read the binary number, we briefly review the decimal system that we are used to. Let’s look at the number 235, two-hundred and thirty-five, for example. Dissecting the number by digits, each digit has a different meaning based on its location in the number. The 5 means „add 5 to the number“. The 3 means „add thirty to the number“, and the two means „add two-hundred to the number“. So the 5 is a multiplier for the 1s added to the number, the 3 is the multiplier for the 10s, and the 2 is the multiplier for the 100s. The 1, 10, 100, etc. are powers of ten. Specifically, 1 = 100, 10 = 101, 100 = 102. One way to think about the powers is as follows: 100 = 102 = 1·10·10, i.e. the superscript two indicates how often we need to multiply 10 to 1. The use of 10 is why we call it the decimal-, or base-10 system. The binary numbers work the same way, but instead of 10 as the base, they use 2. So therefore, we can convert binary number 101 to the decimal system by calculating 1·20 + 0·21 + 1·22 = 1·1 + 0·2 + 1·4 = 1 + 4 = 5. We are ready to read the number in the motor command above, then. First, we need to reverse the sequence 0100110, since the signal sends the lowest digit, or bit, first. The actual number we need to read is therefore 0110010. Converting to decimal, we find 0·20 + 1·21 + 0·22 + 0·23 + 1·24 + 1·25 + 1·26 = 0·1 + 1·2 + 0·4 + 0·8 + 1·16 + 1·32 + 1·64 = 2 + 16 + 32 = 50. So the red part of the signal above for the left motor tells the motor to spin forwards at a speed of 50. Looking at the blue part for the right motor, we see that the command is identical to the left motor. The right motor will also spin forwards at speed 50. Therefore, the robot will drive straight at a medium speed and forwards. Finally, the orange part of the signal is the command for the LEDs. This is easy: A one tells the LED to turn on, and a zero to turn off. The robot has 8 LEDs, so the orange part contains 8 bits. The command in the signal sets the motors and LEDs to a constant setting. How can the robot then blink and move in more interesting ways? Part of the answer is given in the image above: Immediately after the command that is color-coded, a new reset pulse and a new command is being transmitted. This command may change the wheel speeds and the LEDs. The commands are also very short, and take only a small fraction of a second. Therefore, by sending lots of commands where the wheel speeds and LEDs change, we can make the robot dance and blink in interesting motions and patterns.