Skip to main content
13 events
when toggle format what by license comment
Apr 24, 2017 at 1:16 comment added Timmy @DMGregory Thank you for the Sensor Fusion information! The Complementary Filter seems to be exactly what I would need. Unfortunately I will not have time to implement it in my current project. It's seems complicated but perfect for what I'm attempting to do. I'll see if I can getting it up and running tonight! Thanks again Gregory!
Apr 24, 2017 at 0:42 comment added DMGregory If you have gyroscope data too, then you can usually get a higher-quality result by combining the gyro and accelerometer data together to arrive at your new orientation estimate, a process called sensor-fusion. I described one simple method I like to use for this here, which also handles the nudge back to zero yaw implicitly.
Apr 24, 2017 at 0:37 comment added Timmy @DMGregory Final question, I've been trying to set the yaw to zero, which is either 0, or 180 (depending on the orientation). Luckily for me I also get in gyroscope data, so I know then the device is at rest. But setting the angle properly seems to be an issue for me. I know when the pitch crosses 90 degrees the Y axis would flip to 180 degrees, so I tried to retain the current X and Z axis angles and just keep a solid Y so it wouldn't drift. Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, 0, transform.rotation.eulerAngles.z)), speed);
Apr 23, 2017 at 21:30 vote accept Timmy
Apr 23, 2017 at 21:30 comment added Timmy @DMGregory Not normalizing the yaw component seemed to do the trick! Keeping the order new Vector3(-LPFPrevX, LPFPrevZ, LPFPrevY) for the orientation of my device and not normalizing the Yaw make both rotations work. I did run into the drifting Yaw, I plan on taking your approach by nudging it back into place. I plan on implementing that soon. Thank you again. I learned quite a bit from your solution!
Apr 22, 2017 at 22:39 history edited DMGregory CC BY-SA 3.0
Clarifying, adding option to leave yaw uncontrolled
Apr 22, 2017 at 22:31 comment added DMGregory Since I lack your physical input device to test with, I've done synthetic tests here with a virtual accelerometer, and confirmed that the code above behaves correctly if fed acceleromater data in a coordinate convention that matches Unity's, so I strongly suspect your device coordinates are in some other convention. Try placing the object on each of its six faces and record what acceleration value you get in each orientation - this will tell us how the device labels its various axes. I'll also edit the answer to include an option that does not normalize yaw.
Apr 22, 2017 at 17:59 comment added Timmy @DMGregory Using var worldToLocal = Quaternion.LookRotation(new Vector3(-LPFPrevX, LPFPrevZ, LPFPrevY), Vector3.back) * Quaternion.Euler(90f, 0f, 0f); Keeps the correct orientation of the object in unity, the strange thing is now, the pitch and roll seemed to be swapped with each other, (which can be ok for what I'm doing). So the pitch(which is shown as roll on the screen) works fine. But when rolling the other direction it still does the 180 degree flip. I tried playing around with Quaternion.Euler(90f, 0f, 0f); but the 180 flip still happens on the one axis. I can add pics if needed.
Apr 22, 2017 at 17:45 comment added Timmy @Bálint Isn't that what lerp does?
Apr 21, 2017 at 5:59 comment added Bálint @Timmy you'll have to interpolate between the 2 angles
Apr 21, 2017 at 3:39 comment added DMGregory That sounds like what would happen if the acceleration vector was very close to the z axis. Does it happen when you're pitching the object almost nose-up? If you see this when the object is sitting close to level, then the device's coordinate system might be different from Unity's, and you might need to swap the y and z axes (and possibly negate the x)
Apr 21, 2017 at 2:03 comment added Timmy Thanks, this actually makes alot of sense. I seem to be running into a problem though. I set my code up as var worldToLocal = Quaternion.LookRotation(new Vector3(LPFPrevX, LPFPrevY, LPFPrevZ), Vector3.back) * Quaternion.Euler(90f, 0f, 0f); Where the LPF values are just the smoothed X,Y,Z values. It seems when ever either the X or the Y value cross from a positive to negative value or vice versa, the object like to swing a 180. I believe I set your code up correctly though. Please correct if I'm wrong on how this works.
Apr 21, 2017 at 1:21 history answered DMGregory CC BY-SA 3.0