Yaw discontinuity at 180 degrees for PID

Hello,

I have the same problem as: http://stackoverflow.com/questions/10697844/how-to-deal-with-the-discontinuity-of-yaw-angle-at-180-degree

My P(i)D, which is working for roll & pitch, is something like:

action = pid_P * (yaw - targetYaw) + pid_D (yaw - targetYaw - lastDeltaYaw);
lastDeltaYaw = yaw - targetYaw;

My yaw angle is between -180 and +180: at +180, it goes to -180. How can I convert my PID formula so that it works for yaw. In other words, when should I subtract 360?

Thanks in advance for any advice.

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Answering my own question: the difference of 2 angles can simply be put back to -180/180 with:

    #define MOD(a) ((a > 180.0) ? (a - 360.0) : ((a < -180.0) ? (a + 360.0) : a))

    • Could you please explain the code a bit?

      I'm confused because there are so many arguments in that mod function.

      Aren't there just two?

      Are those "(a > 180.0) ?" and "(a < -180.0) ?" if requensts?

      If so i would think that there will never be a sensor value that is over 180, because it would switch to -180 at this point.

      Is it right that a ist the value from the yaw sensor?

      Greetings

      Jonathan

      • Ok, I got it now.

        Works great! Thanks for the distribution of your solution.

        Greetings

        Jonathan

This reply was deleted.

Activity