Sunday, July 26, 2009

Autopilot

I had too so much fun testing my RC plane with an autopilot that it's worth a blog post. There are a few pictures and no video. I'll try to get some video taken next time we test now that things are starting to work, I rather wish I had video of the latest flight...

Original Concept
  • I have a MiniMag RC plane that I have been flying around for the past year.
  • I have a microstrain 6 DOF IMU that Piwai gave me for my birthday to attach to my airplane.
  • I have an XMos XC-2 test board that I got to evaluate as a candidate for making a microphone array for the robot at work. It looked pretty cool in theory: a quad-core processor, with plenty of RAM (64k per core). Eight hardware threads per core, making it easy to implement UARTS, ethernet MACs and other IO devices in software. Dev tools that looked easy to use and complete. What better way to test it than to use it in a cool project.

After a suprisingly short 4 evenings of development, the plane was ready to fly upside-down in auto-pilot mode mode: flick a switch, and the plane will servo roll and pitch to be level and
upside-down. By this time, I was also sold on the X-core. Good dev tools and lots of ressources unlike a PIC. Near-instant compiling, and ability to do complex IO like an FPGA. The .xc language tries a bit too hard to prevent you from running into locking and pointer problems, but you can work in C/C++ too. (And it looks like they are coming out with a single-core version in a LQFP/TQFP package, for those who like to do their own soldering.)

Saturday
Went out to the field with the autopilot programmed for inverted flight, and all the electronics in a large hunk of foam in place of the cockpit.

We had quite a crowd (9 including Jonathan) along for the test so everything went about as badly as it could. There was a tremendous wind. Not usually a problem, but in this case it was apparently accompanied by a down-draft just above the flying field, which made it pretty much impossible to gain any altitude for testing the autopilot, and which made flying really difficult because of turbulent air. The plane was flying so slow I couldn't even get it inverted to try out the autopilot. After a crash landing, we stopped to replace the propeller and to glue up cracks in the foam around the motor. Piwai and Francois-Marie cut off excess foam from around the electronics to try to reduce drag (and greatly improve appearance). I also did some in-field reprogramming (literally) with the laptop, so that the plane would servo to an upside-right position instead of upside down.


Subsequent flights went a bit better. The plane flew better with a LiPo battery (more power, less mass), but the battery only being partially charged, I had to land before any testing could be done. Under NiMh power, I found that flying was much better a bit up-wind from where were standing, where there seemed to be an up-draft rather than a down-draft. The plane finally reached a safe height for the test. I flicked the switch, and the plane promptly entered a fast downward spin. I flicked the switch back to manual, and recovered the plane, but by now the battery was starting to die, and I had to land. The spin was probably due to the low battery voltage causing the IMU to shut down (in which case the autopilot currently continues to trust the last reading it got which is bound to cause a spin).

Conclusion
  • Remember to plug in the charger when you want to charge your LiPos.
  • Don't try to fly in a down-draft.
  • Wind isn't always irrelevant.
  • Fly upside-right before you fly upside-down. (I have been bit by this before, right Jim?)
  • Power the IMU straight from the battery, not from the 5V servo supply (good advice from Piwai).
  • 110g of extra electronics and foam makes this the heaviest I have ever flown the plane (by about 50g). Always weigh what you put on the plane to know what to expect.

Sunday Morning
Saturday evening, and Sunday morning I made various changes to the plane:
  • I changed the IMU's power supply to be directly from the battery.
  • I moved the IMU to the back of the plane where it is easy to slide in and snugly attach, to reduce bulk at the front of the plane.
  • I decided to do away with the protective foam around the electronics. That isn't the part of the plane that is going to touch the ground first unless Jim O'keeffe is flying it, and I wanted to have as little stuff behind the propeller as possible.
  • I changed the code so that in auto mode the aileron/elevator stick can be used to set the target attitude for the plane: the position of the stick directly maps to a target attitude).
  • I programmed the buttons on the X-Core board so that one button selects upside-right mode, and the other selects upside-down mode.
We headed back to the field with all of Valerie's family, without the
computer as it was already late for lunch. Things went better this time,
all with NiMH batteries, except with a few problems with a faulty
connection on the throttle control line (not a major flight-safety issue as
you can still glide to the ground).

There wasn't a breath of wind. The plane flew just fine (despite the extra weight). I flicked the switch, and the plane started dancing side to side and up and down in a rather crazy pattern. Clearly the gains were set too high. I tried adjusting the set-point with the control stick, but the plane was dancing too wildly to make out any differenc. Around this point, the motor started causing large resonances. I landed, re-tightened the propellor, selected upside-down mode,
and took off again. The resonance was still pretty bad, but I decided to test anyways. I rolled the plane onto its back and flicked the switch. This time the plane spiraled down right away. After landing, I got the impression from the plane's behavior that it had somehow switched back to upside-right mode. Perhaps a power supply glitch reset the XCore.

Back at the parking lot, a guy who had been flying his helicopter on the same field asked what the crazy dance the plane had been doing was. I guess it was pretty obvious that it wasn't a human flying the plane when it was in auto mode.

Sunday Afternoon
Back home, I divided the gains in the controller by 5, and changed the IMU settings so that if it loses power, it automatically re-starts broadcasting attitude when it regains power.

After some canoeing at Shoreline, we made a final stop at the park to test out the new gains. There was a lot more wind than in the morning, but less than Saturday. As soon as I started the pre-flight check, I realized we had a problem. I had forgotten to turn off debug mode before programming the firmware, and the computer was at home.

In debug mode, the auto/manual switch is disabled, and always set to auto. This is so that I can test the plane while the controller is charging. If I hadn't added the ability to set the target attitude, this would have been a show-stopper, as the plane would have been un-controllable in auto-mode. With the ability to set the target attitude, the plane should be flyable in auto mode. In fact, it should be easier to fly than usual: set the attude you want, and watch the plane servo to that attitude. However, if the IMU stops sending data, things could get bad. If the last IMU
reading was way off-target the ailerons and elevators could be in an extreme position that could not be compensated for by setting the attitude set-point. Also, with the gain settings I had in the morning, the plane would have been un-controlable in auto-mode.

I decided to do a quick low-altitude test flight despite the risk, to see how things would go. If things didn't look good, I would cut power and hope for a soft crash...

I threw the plane and throttled up. It started diving a bit, so I pulled back on the stick. It obeyed and started gaining altitude. There was still a bit too much gain on the roll axis, and the plane tended to oscillate a bit in roll. But overall, things were looking good! By this time of course, it was no longer a small low-altitude test flight. The plane was flying upwind fast, and I wanted to bring it around to land while the going was good. I tried to adjust the roll set-point. It had some effect, but not much. I though I had given myself a lot of range on the roll set point when I was testing in the living room, but in the air, the amount of roll I could request seemed pretty pathetic. I could steer the plane, but a lot less that I would want for comfort, even with help from the rudder (which is always in manual mode). I managed to steer the plane to a cross-wind orientation, and it drifted back towards us, then I steered it back to face the wind ready to land. Throughout the maneuver, the plane would sometimes oscillate a bit in roll, and at other times it would be nice and steady, possibly because of varying aileron authority with airspeed. I throttled down to land, and the plane started diving a lot more than seemed comfortable. I throttled up again, and the plane, very low now, appeared to ricochet off the ground. I cut the throttle, and it landed softly a few meters later.

We ran to the plane fearing damage from the ricochet, but it turned out that the plane must have regained enough airspeed to recover just before it hit the ground (before that it may have been in control inversion, where, at slow airspeeds, the elevators have the opposite of their normal effect). It was intact, and potentially ready to go, with 2 and a half batteries remaining.

At this point I decided not to try my luck. I was pretty thrilled. The roll gain was still set too high, but we had managed to do a full flight with the autopilot in the loop. The overall setup was proven, and it was now time to keep improving the software, and think about adding the GPS module...

Thanks for everybody who came to watch this week-end, and especially to Piwai who forced me into this by giving me the most expensive pice of the setup (even if he does say it has been taken apart and is probably poorly calibrated). And thanks to Chiu-Ki for the pictures. I hope you don't mind that I stole a couple of them for this post.