Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SlimeEnthusiast · Nov 03, 2016 at 01:05 PM · quaternionrollpitchflight simyaw

Problems getting PID system and rotations to work together.

I am working on a Unity project involving aircraft. As the users controls the aircraft, it is stabilized by a PID system.

The way the system was set up before I started working on it is as follows. First, the pitch, roll, and yaw values are stored individually as floats using Euler angles. (For the sake of brevity, I will only be showing the code used for pitch, but roll and yaw function identically)

 float pitchAngle = -AngleTransform(this.transform.rotation.eulerAngles.x);

(AngleTransform is a function that keeps the angle between -180 and 180. The pitch is set to negative because of the way user controls are stored [More below])

The target pitch is determined by the user input on the y axis of the right stick of a gamepad as per the below.

 float pitchTar += Map(rightStickY, -1.0f, 1.0f, 3.5f, -3.5f);

The error between the actual pitch and the target pitch is then found.

 float pitchErr = pitchTar - pitch;

The error is then fed into the PID system which has stored information about previous pitch errors and spits out a stability output.

 float pitch_stab_output = (pids[PID_PITCH_STAB].Compute(pitchErr));

This output is an estimate of the rate the pitch should be changing in order to reach the target pitch.

Next, the rate the pitch is actually changing is calculated using the pitch from the last frame (prevPitchAngle)

 float pitchRate = Mathf.DeltaAngle(prevPitchAngle, pitchAngle) / Time.fixedDeltaTime;

(Pitch is recalculated in FixedUpdate which is why this is divided by Time.fixedDeltaTime.)

Then, the pitch rate error is calculated.

 float pitchRateErr = pitch_stab_output - pitchRate;

This is fed into a seperate PID which gives us the final pitch output.

 float pitch_output = pids[PID_PITCH_RATE].Compute(pitchRateErr);

This pitch output is then used in various flight calculations.

The issue this setup has is that when the rotation about the x-axis >90 and <270, the y and z-axises flip by 180. This causes the PID system to believe the roll and the yaw have gone instantaneously from 0 to 180 causing outputs which attempt to stabilize such a scenario but invariably just make the aircraft end up crashing.

Enter me starting work on the system. I am aware that this flip of 180 is a result of the limitations of displaying the underlying quaternion that Unity uses in Euler angles.

I have tried many things from simple (subtracting 180 from the yaw and roll if the rotation around the x-axis if >90 and <270) to complex (using the underlying x, y, z, and w of the transform.rotation quaternion to calculate the Euler representation of the rotation) but nothing seems to work.

Below is my latest attempt at finding the error between the target rotation and actual rotation by keeping everything in quaternions.

 Quaternion pitchRollYawErr = (Quaternion.AngleAxis(yawTar, Vector3.up) * 
     Quaternion.AngleAxis(pitchTar, Vector3.right) * 
     Quaternion.AngleAxis(rollTar, Vector3.forward)) * 
     Quaternion.Inverse(transform.rotation);
 
 float pitchErr = pitchRollYawErr.eulerAngles.x;

I also find the pitch rate as follows:

 Quaternion prevRot = rot;
 rot = transform.rotation;
 Quaternion difference = rot * Quaternion.Inverse(prevRot);
 float pitchRate = difference.eulerAngles.x / Time.deltaTime;

This still does not fix the problem and actually makes the aircraft unpilotable (Which points to the possibility that I'm way off mark in what I'm attempting.) In particular, the calculations for pitch rate give me some huge results, and I can't seem to understand why.

To sum everything up in one question: Is it possible to get the Euler representation of each rotation (pitch, roll, and yaw) without the flip to 180 described above?

In the interest of keeping the problem easily digestible, I have opted not to go into too much depth about how the PID system works. I am fairly sure the problem stems from the conversion of the quaternion to Euler angles and what is happening in the PID is unrelated. If you think otherwise and that there's a chance the error could be occurring with something related to the PID, let me know and I'll give more details.

Please note that the way the PID system takes floats is something I cannot currently change. Also, I am not directly modifying the rotation of the aircraft, so just using the quaternions as is is also out of the question. If what I want is not possible in the configuration I have described, please tell me that rather than suggesting one of these things.

Thanks for reading! This is all pretty complex (at least for me!) so if I didn't describe something well enough, or if you would like me to expound on something please let me know.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting the roll of a rotation 1 Answer

Convert directions in Vector3 to yaw/pitch/(roll),Converting directions in Vector3d to yaw/pitch/(roll) 0 Answers

How to change angle-roll object with mobile input? 0 Answers

Adjust Yaw For A Gliding (Like Superman 64) Script 1 Answer

Get pitch and roll 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges