I thought it would be fun to have an 'inverted flight' switch on my transmitter. With an APM, it's easy! I just added a check for a PWM value above 1700 on channel 7 in read_control_switch(), and when it is above 1700 it sets a inverted_flight boolean. That adds 180 degrees to the nav_roll, which is the target roll for the stabiliser, and it switches the sense of the elevator stabilisation in the main stabilize() code. The result is this patch to the current ArduPilotMega code.
I tried this out a few times with HIL simulation using FlightGear first, then took it flying today with my SkyWalker. There was a lot of wind, so not exactly ideal conditions to be trying experimental patches, but I didn't want to wait for next weekend so I took a chance. It works perfectly!
I setup a spring loaded switch on my transmitter to set channel 7 to 1900 while I held the switch. That allows me to takeoff normally, then whenever I hold the switch in the on position the plane immediately flips over and holds nicely stable flight upside down. You can direct it reasonably well while inverted, although it certainly isn't as responsive as with upright flight (a glider like the SkyWalker isn't really designed for this sort of thing!). The manual mixing of elevator is inverted while upside down (just like it is when you flight upside down manually), which makes this patch good for practising inverted flight, as you can get used to the controls.
The only tricky part of the patch was the change to stabilize() to handle wrap of nav_roll at plus and minus 180 degrees. I originally wrapped it just based on the value, but the HIL simulation showed there was a discontinuity in the stability control at exactly 180 degrees, so the plane would suddenly try to flip over again while upside down as you passed the 180 degree mark. This turned out to be an interaction between the wrapping of the dcm roll_sensor and the nav_roll wrap. The fix was to wrap the nav_roll variable based on the sign of the dcm.roll_sensor variable, and that leads to really nice inverted flight with no crazy behaviour near 180 degrees.
This patch works with any of the stabilised flight modes (ie. anything but manual), which means you can flick the switch for inverted flight while flying a mission, or while in any of the other autopilot modes. It should add a bit of interest to longer missions!
(thanks to Stan for taking the video and coping with the gale blowing at the time)