Friday, May 6, 2011

Arduino helicopter infrared controller


Some time ago I got the S107 helicopter as a birthday present, and as I found myself with some free time, I got an Arduino board with the intent of flying it with my computer.

Helicopter being tracked with webcam and speed adjusted
My experience has always been on the software side, so this is my first try  utilizing hardware and micro controllers.

While waiting for the Uno, I started reading the basics and found this amazing tutorial on how Infrared works, and code samples on reading and emitting infrared signals.

The code in that tutorial was extremely helpful in getting me started, and understanding the basics of Infrared communications.

Arduino Uno and IR emitter and receiver circuit
Delivery day and I get my arduino, (for some reason I ordered two usb cables instead of two IR leds, guess I didn't double check the order)

Following the first steps, I installed the software, and then tested out the board with the begginer examples it provides.
Later on, I built the ladyala emitter and receiver circuit, and its ready for work!

The schematics are identical to the ones here, but on an Uno board instead of the duemilanove version, but it shouldn't be a problem.

Before getting into the helicopter remote, I tried it with  my audio remote control, which has only 3 buttons and seems simpler.
After decoding the pulse, I played around to make a program that graphics the pulses.

As the Arduino runs in the board, the communication is done through the serial port.
To communicate with it while it’s running, it’s done through the same serial port used to upload data to it.

Utilizing Processing language ( the program to graphic the pulse is quite simple.

Here’s a picture of the three button’s pulses (on, vol up, vol down), done with the Processing language.

On to the helicopter!
This is a little bit trickier, because once you’re flying the helicopter it’s a continuos stream of data from the remote to the helicopter, instead of a single burst of data.
We’ll need to figure out how it transmits the data to the helicopter, that is, which protocol it uses.

Reading a bit more into IR protocols, I found a thread that depicted usual protocol pulses here.

There is a Header followed by “bits”. Each bit is a pair of on/off IR pulse. The 0 is a short on, short off, and the 1 is a short on, longer off.

With the decoding program loaded into the board, the output for the helicopter is the following:

(off / on)
-10072 – 1930
1880 - 330
250 - 370
230 - 350
590 - 320
640 - 330
640 - 370
210 – 370

So, the header is a 2000 ms on, 2000ms off, the 0 a 300/300 pair and the 1 a 300/600 pair pulse.

The separation between headers is between 32 pairs, so I assume the helicopter uses a 32bit pulse.
Modifying the receiver program to adapt to this format, and make it easier to figure out the protocol, the output now shows H for header, 0 and 1, in a 32 bit string:


The next step is the most tedious:
Detecting the changes when only one movement is done, to figure out which bits do what.
I recorded hundreds of combinations, and did a little program to eliminate the repeated ones.

The outcome:

Byte 1: Yaw
Byte 2: Pitch
Byte 3: Throttle
Byte 4: Yaw correction from Remote.

They all use the last 7 bits of the byte.
Throttle goes from 0 to 126, while pitch and yaw start centered in 63.

With all this info, it’s time to create a processing program to test this, a program to transmit pulses with the decoded format.
Then some buttons to handle the increases and decreases, and off it goes!

The emitter program in the arduino to keep sending the same pulse, and the processing program to just notify the changes in the bits, instead of always telling the board what pulse to send, for timing purposes
(It uses a simple way of just sending one ascii character, and both programs know which bit it’s referring to)

It works! But the IR Led seems to be quite underpowered compared to the one in the remote control, so the range is about one meter, then the helicopter drops dead.

One final test is to have the computer do some flying on its own.
As a proof of concept, with the jMyron library for Processing to handle video input, I want to make the program have the helicopter hover at a steady position.

Utilizing the input from the webcam, it adjusts the speed until the helicopter is in the middle of the screen.
If it goes too high, it lowers the speed. If it gets too low or is stopped, it slowly increases the upwards speed.
It works!

Any ideas on what to move on to next?

Arduino Source Code: