Hacking a mini helicopter – with my newly “updated” BORA and VHDL

s107

So I’ve decided to use my BORA and hack a small helicopter –  a Syma S107G (3.5channels), which is one of the cheap and very popular helis. It’s cute, looks surprisingly sturdy for its price, it’s chargeable with USB (nice one!) and well-documented.

And I want to learn how to work with VHDL. 🙂

The hardware you need is:

a) a Syma S107G (3.5 channels).

If needed, add extra 6*1.5V (AA) batteries and a small (1? or so..I’ve got a  set..) Siemens screwdriver, if you don’t have them available, to change them. (Or use USB.)

b) a BORA, including some jumper wires. If you haven’t already, install the toolchain. (I’ve written instructions for Windows, including the 64-bit issues)

c) an Infrared LED + some resistor, 470 Ohm plusminus something should be ok, but don’t worry about the exact value.

d) mabye something that can measure frequencies .. (you could also use a fitting IR receiver and look at its output..)

At some point I’ll describe how to use an Infrared Receiver for decoding the signal (hint: 38kHz. Peak LED color  is ideally 940nm), but right now, I want to get this thing into the air with you. If you’re interested, just read this very good article.

How it works

The heli is remote controlled via IR.
If you’d look at that LED from your remote control with an oscilloscope, you’d see something like this:

pulsed

This is called carrier pulsing. Read an article if you’re interested in a good explanation.

Fact is: We need to generate two signals:

a) The carrier (the fast pulsing constant) frequency, which is 38kHz
b) The real signal (which tells the heli to go somewhere.)

We’re going to use the AVR on the BORA board to generate the carrier and then, we’re going to produce the second signal with the CPLD itself.

Step 1: Generating the pulse: 

Finding out that the protocol uses 38kHz infrared was a matter of web research, but if you are really starting from scratch, I suggest you to read the popular IR decoding tutorial by Ada. I usually use a NE555 for generating  such signals, but this time, I’m proudly presenting my newest creation, the updated BORA.

BORA  just provides a 25kHz clock, and that is too slow. However, it’s pretty easy to update the firmware, as I’ve demonstrated on Friday. Download the BoraHID105.hex file from github , and follow the instructions to get into the correct boot mode. (I recommend watching the video).

If it doesn’t automatically find the drivers, go to the device manager, and have them look up C:\Program Files\Atmel.

After you got your device successfully installed, remove the BOOT short, and open FLIP.

Select the correct device.

00_selectdevice

Tell FLIP that you’re going to use USB.

01_usb

Load your hex file

03_loadhex

You might have to change to the flash buffer (1, sometimes I do have to do this, sometimes I don’t – don’t worry, the next step simply will fail if you do the wrong thing ), then press “RUN” (2)

Just check (3) if it worked.

04_program

As a next step, close FLIP and remove the USB from your PC, then add it again. This reboots the board.

(Hint: After this experiment, please do yourself a favor and return to the original 25kHz 😉 You’ll forget that your 25kHz are incorrect and spend some time debugging..)

Measure the frequency at the lower right of the board, against GND:

measure

You should get something like this:

37khz

It’s not exactly 38 kHz, because we don’t have an exact 8MHz oscillator.

To improve this result, I used another factor (104 instead of 105) and tried again. (File is on github, too.)

38khz

——————————————————

Step 2: Working out the protocol

So, we got ourselves a carrier signal. Now we’re going to have to create our data signal.

And for this, we’re actually going to have to find out what it is. As stated, I won’t spend time teaching you how to decode this thing (ADA’s link, somewhere above does a so much better job at this than I do), we’re going to look it up.

Kerry Wrong did that work for us brilliantly 🙂 Thanks a lot!

Let’s take a look at the remote control:

Controls

In the middle, you can pick your channel. Put that one to channel A (and, if neccessary, change your helis’ channel, too.)

Switching channels means switch carrier frequencies. Channel A is 38kHz.

We’re going to focus on the “middle” lever. In fact, we won’t be looking at the lever at all, because it provides analog signals (and I’m not going to implement an ADC.. for now.). We just consider SWITCH 1 as 100% middle lever.

To test, I’m using a colored LED with the breadboard. It helps with debugging.

LED for debugging

(BTW, you can use BORA with Fritzing – great!)

Theoretically, I could just use one of the included LEDs, but this makes it easier in the end: Simply exchange the individual LED for a IR one.

Intermezzo:

My  heli got into a fight with the cat.

Let’s just say that all three of us got hurt (the heli is definitely NOT a toy for small children), and the story ends with this picture (I did consider leaving the blood stains.. ;-))

Kitty, kitty,kitty

I got it into the air again, with some super-glue, mad soldering skills (basically a short with the charger and a broken wire for the infrared led) and luck.

intheairagaain

Back to our topic.

VHDL

We won’t be doing Schematics. We’ll be doing VHDL, simply because it’s way more high-level. First task is to push the 38kHz clock through the output, just to see if a) we can use the clock, and b) we can use the output.

Before I start, one word of caution: I’ve never really done any VHDL programming before, so this is not   good code. In fact, it’s horrible code.

Get started with this: http://www.seas.upenn.edu/~ese171/vhdl/vhdl_primer.html

Or one of the other bazillion websites that teach VHDL.

I also liked this: http://www.eda.org/rassp/vhdl/guidelines/vhdlqrc.pdfa lot.

I opened the standard project again (the one you get from the website or the DVD).

Pretty soon, I found that I need to add the source

01-src

02_messagetimer

Next window defines the interfaces (in and out) and constants. I later learned that this is the part of the program called „entity“. There are two further parts, „architecture“ (this is where the logic happens) and „configuration“ (which matches those two).

03_entity

I know that kind of architecture from some PLCs I’ve been playing with.

This creates some code.

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity messagetimer is
Port ( clk : in STD_LOGIC;
led : out STD_LOGIC);
end messagetimer;
architecture Behavioral of messagetimer is
begin

led <=clk;

end Behavioral;

I’ve noted the line I changed. It’s obvious, I’m just pushign thorugh the CLK to the LED.

We need to add this VHDL coude into our main schematic. You need to create a symbol from your VHDL source.

04_schematic

After that, you can add it to your main schematic.

05_addsymbol 06_addedsymbol

Compile this, and use program.bat.

Realise that the new firmware doesn’t allow you to install a new program.

Upload the old firmware with FLIP again.

Use program.bat.

The LED blinks.

And then, get creative with VHDL.

autogen


Yeah. That’s how I wrote it. I’m sorry. It’s the fucking ugliest piece of code I’ve written in forever. At least I didn’t write that per hand (it’s 32bits!*2), I autogenerated it. I like code that writes code.

It does take quite a bit of my resources, though, doesn’t it?

resources

Btw, you need to multiply the CLK with it, to get that carrier pulse onto the signal.

schematic

Oh, and do switch the LED (and the port, if you haven’t so) for a IR one.

Done.

(Unfortunately, it didn’t work out that well. It took me about 4 hours with a 2-channel oscilloscope to get the timings right. And when I change firmware version, it didn’t work out any more. It’s waay to shaky. But if flies!)

—————————————————–

Step 3: Conclusions

This is very shaky and flies only when it’s close to the board, which is a matter of power and would be solvable with a transistor and, maybe, some external power source.

The real problem is that we’re not really using multiples of 1/38kHz, we actually need a significantly faster timer. 2*38kHz still fits very well into the 4MHz, but we’ll see.

Next step is to optimize that ugly, ugly piece of code. I’d use a vector with the 32bit number that represents my data signal (001111000…), and have a pointer running through it. The length of each on-off-process is determined by whether its’ a 0 or 1.

I’d probably keep the AND with the CLK (I rather like the idea), but for this, you’d have to use a frequency divisor.

But before I do that, I’ve got quite a task ahead of me.

thingstodo

Theoretically, we could implement all the other signals, too. Optimise the code. Use potis and ADCs,
But there’s no point in it. This is a proof-of-concept, not something I’ll use, ever.  I wanted to play with BORAs’ firmware, I wanted to get started with VHDL, and I wanted to see if I could do this.

It flies.

Beautifully.

That’s the point.

takingoff

Advertisements

3 comments

  1. asdfsdfa · · Reply

    I like your cat.
    (Sounds like a mad, mad weekend.)

  2. Twowi · · Reply

    Nice one.

  3. Was at your awesome workshop. That idea evolved quite a bit, didn’t it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: