# Vol. 12: Homebrew

My Lego UAV.

Photos by Anne Anderson

### My Lego UAV: the Full Story

I didn't have my children to justify playing with Legos, but it certainly didn't hurt. Yet after a while, simply building Lego creations with my kids wasn't enough. We (okay, mostly I) wanted to do something with Lego that had never been done before. But what?

The answer came to me while out on a run, and it combined three geeky things that were on my mind. We'd recently got some radio-controlled (RC) airplanes in the office for review and the kids and I had taken them out, crashed most of them, but eventually learned how to fly. Fun, but hardly groundbreaking. Then, I happened to read that HiTechnic, a company that makes third-party sensors for Lego's Mindstorms robotics set, was going to release a gyro sensor. Suddenly it came to me. Gyros are what you use for autopilots. An RC plane with an autopilot? That's an Unmanned Aerial Vehicle (UAV), or drone. Bingo. We were going to build the world's first Lego UAV.

When I got back from the run, I built a prototype on the dining room table. I blogged it as one does, and got Slashdotted, so then I had to actually make it work. HiTechnic sent me an early version of their sensor, and I got my first dose of reality. The HiTechnic sensors use the tiny MEMS-based gyros that are so ubiquitous today, from the Nintendo WII controller to the iPhone. The sensors use tiny “rate gyros", which don't measure absolute position. They've got no idea where “down" really is. To get them to actually keep a plane flying straight and level, you'd have to combine them with acceleration sensors (the force of gravity — “down" — is measured as an acceleration) and do a ton of gnarly math to get around inertial forces, drift, and other complications. Unfortunately, our beloved Mindstorms doesn't even support floating-point math, to say nothing of real-time integration of gyro data, so a Lego inertial measurement unit wasn't going to happen.

Then came the second flash of insight. I realized that an autopilot actually has two jobs: 1) keep the plane flying level; and 2) navigate to geographic waypoints. Keeping a plane flying straight and true is a solved problem — companies such as FMA Direct sell “co-pilots" for around \$100 that look at the infrared gradient from earth and sky in four directions and move the plane's control surfaces to keep those IR signals balanced. They can keep a plane level in any conditions. By handing the flight stabilization job to the FMA co-pilot, I'd turned a 3D problem into a 2D problem. So that just left navigation for the Lego: while the FMA co-pilot controlled the ailerons and elevator, the Lego could control the rudder, steering right or left to get to the next waypoint.

I found a plane (a Hobbico ElectriStar) that was big enough to hold the Lego Mindstorms controller, gears, sensors and other elements necessary to have two control systems. The RC system used the usual servos, while the Mindstorms motor was geared to move the entire rudder servo back and forth. This meant that we had a total disconnect between the two systems, which is a good thing from a safety perspective; if the autopilot failed we'd still be able to move the servo arms manually. The right way to do navigation is with GPS, but at the moment I started, there was no good way to read GPS data with Mindstorms via its built-in Bluetooth. So I started with a proof of concept that used HiTechnic's compass sensor, and helped my then-9-year-old to write a program that would just tell the plane to fly a square pattern when it was turned on by a RC servo pushing a Mindstorms touch sensor. (While we were at it, we mounted a wireless video camera on the bottom of the plane with a Lego pan-tilt assembly, which is another story!) So far, so good. Everything worked as hoped and the plane flew autonomously, providing a Lego-driven eye in the sky, albeit one that just flew in a square.

Fortunately there here were several groups working on the Bluetooth GPS problem on Mindstorms. The team behind the RobotC programming language were the first to really crack it. So we ported all the code from Mindstorms NXT-G to RobotC, which turned out to be pretty easy. Today we have a fully-functional Lego UAV. You give it GPS waypoints, take it off manually, flick a switch on the RC transmitter, and it flies to whatever coordinates you've entered (you can watch the real-time aerial video from the ground). When it's done, you switch off the autopilot and land it manually. That's pretty awesome, but we want more. So the next job will be to integrate an onboard cellphone that communicates with the Mindstorms via Bluetooth. Then the plane will have a phone number, which means we will be able to send it text messages. “Goto Lat XX, Lon XX". Cool, huh?

Join the conversation -- every MAKE article has an online page that includes a place for discussion. We've made these RSS and Atom feeds to help you watch the discussions: subscribe.