Search code examples
unity-game-enginerotationpid-controller

Quadcopter PID in Unity Malfunctioning When Rotated


I'm making a Quadcopter simulator game with Unity (5.6.0f3). My quadcopter has four evenly spaced "rotors" attached to a central body (and legs) with Fixed Joints. It flies (currently only upwards) by applying force to the rotors (rigidbody.AddForce method). I'm using PID controllers to stabilize the copter, and I've done enough tuning that it stabilizes pretty well when subject to outside forces...

However, when I rotate the copter around the Y-Axis (yaw) and the forward axis of the copter comes near -90 degrees, the stabilization freaks out and pitches the copter up or down dramatically (and for some reason, it alternates between up and down if I keep applying Yaw in the same direction after the copter stabilizes again, and it happens at the same -90 degrees every time).

I can't figure out what's wrong-- the PID controllers I'm using only stabilize Pitch (rotation around the X axis) and Roll (rotation around the Z axis), so changing the Y rotation shouldn't affect anything.

I'm only using the transform.Rotate method to rotate the copter (so there's nothing to do with forces or another PID controller for Yaw).

Has anyone else come across a similar problem when working with a PID controller?


Solution

  • When you rotate an object around one of the axis sometimes the other axes get aligned, causing weird behavior. This is called Gimbal Lock, google it. That's the reason we have Quaternions - they are hard to read and work in mysterious ways, but they never get 'locked'

    [Edit] I'm sorry, I'm trying to be as specific as the question allows me to be