This arduino sketch is a flight controller for a 4ch radio-controlled helicopter. It uses an MPU6050 IMU to stabilize the aircraft.
The pitch, roll and yaw axes are stabilized with a respective PID controller using the angular velocity as a proportional term. The yaw axis includes a counter-torque term that increases with throttle.
In general, the program only improves stability in the short-term and a pilot (or other control algorithm) is needed to stabilize the helicopter in the long-term. Despite this, the aircraft can maintain a steady hover without pilot input if the PIDs are tuned correctly (in the order of 5 seconds).
The program is designed to receive 4 PWM inputs from an RC receiver and generates 3 PWM outputs. Two signals go to the cyclic servos and the third goes to the tail-servo or speed controller. Since it assumes that only two servos are used for the cyclic, only a 90-degree swashplate is supported. The code assumes the transmitter uses MODE 2, but MODE 1 can be used by swapping the arduino connections to the receiver.
The sketch was written for an Arduino Nano but it should be compatible with other boards.
Note: replace the tail motor and speed controller with a servo if you're using a variable pitch rotor.
These libraries are needed to compile the code:
This is the helicopter the code was initially writen for:
See: Flight video