Advertisement
    SHARE
    TWEET
    Guest User

    Untitled

    a guest
    Feb 23rd, 2012
    635
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 2.92 KB | None | 0 0
    1. #include "math.h"
    2. float gPitch = 0;
    3. float gRoll = 0;
    4. float gYaw = 0;
    5. /* Estimated values by using accelerometer and gyroscope data */
    6. float estimatedX = 0.0;
    7. float estimatedY = 0.0;
    8. float estimatedZ = 0.0;
    9. static bool gVeryFirstTime = true;
    10. /* Vector compoments calculated using gyroscope data */
    11. static float gyroscopeX = 0.0;
    12. static float gyroscopeY = 0.0;
    13. static float gyroscopeZ = 0.0;
    14. static const float gkWeigth = 5;
    15. static void normalize3(float& x, float& y, float& z);
    16. void estimateOrientation(float& ax, float& ay, float& az,
    17. const float& xz, const float& yz, const float& xy)
    18. {
    19. /* Calculate vector component from accelerometer data */
    20. normalize3(ax, ay, az);
    21. if (gVeryFirstTime) {
    22. estimatedX = ax;
    23. estimatedY = ay;
    24. estimatedZ = az;
    25. gVeryFirstTime = false;
    26. return;
    27. }
    28. /* Evaluate vector from gyroscope data */
    29. if ( -0.1 < estimatedZ && estimatedZ < 0.1) {
    30. /* The z component is too small, better use previous estimation,
    31. * otherwise it will simply amplify the error for pitch and roll */
    32. gyroscopeX = estimatedX;
    33. gyroscopeY = estimatedY;
    34. gyroscopeZ = estimatedZ;
    35. } else {
    36. /* Get angle displacement from gyroscope
    37. * Datasheet says degree per seconds, but it appears they are
    38. * in radiants per second
    39. * Output Data Rate = 100Hz, thus T = 0.01s */
    40. float deltaPitch = (0.01 * xz) * 180 / M_PI;
    41. float deltaRoll = (0.01 * yz) * 180 / M_PI;
    42. float deltaYaw = (0.01 * xy) * 180 / M_PI;
    43. /* Get angle change in degree */
    44. gPitch = atan2(estimatedX, estimatedZ) * 180 / M_PI;
    45. gRoll = atan2(estimatedY, estimatedZ) * 180 / M_PI;
    46. gYaw = atan2(estimatedX, estimatedY) * 180 / M_PI;
    47. /* Update angle based on gyroscope readings */
    48. gPitch += deltaPitch;
    49. gRoll += deltaRoll;
    50. gYaw += deltaYaw;
    51. /* Estimate z component sign by looking in what quadrant pitch (xz) is */
    52. float zSign = (cos(gPitch * M_PI / 180) >= 0) ? 1 : -1;
    53. /* And now the fun part... Reverse calculate R components for gyroscope */
    54. gyroscopeX = sin(gPitch * M_PI / 180);
    55. gyroscopeX /= sqrt( 1 + pow(cos(gPitch * M_PI / 180), 2) * pow(tan(gRoll * M_PI / 180), 2));
    56. gyroscopeY = sin(gRoll * M_PI / 180);
    57. gyroscopeY /= sqrt( 1 + pow(cos(gRoll * M_PI / 180), 2) * pow(tan(gPitch * M_PI / 180), 2));
    58. gyroscopeZ = zSign * sqrt( 1 - gyroscopeX * gyroscopeX - gyroscopeY * gyroscopeY);
    59. }
    60. /* Combine accelerometer data with gyroscope, hopefully getting better result */
    61. estimatedX = (ax + gkWeigth * gyroscopeX) / (1 + gkWeigth);
    62. estimatedY = (ay + gkWeigth * gyroscopeY) / (1 + gkWeigth);
    63. estimatedZ = (az + gkWeigth * gyroscopeZ) / (1 + gkWeigth);
    64. /* Normalize vector */
    65. normalize3(estimatedX, estimatedY, estimatedZ);
    66. return ;
    67. }
    68. void normalize3(float& x, float& y, float& z)
    69. {
    70. float r = sqrt(x*x + y*y + z*z);
    71. x /= r;
    72. y /= r;
    73. z /= r;
    74. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Advertisement
    Public Pastes
    Advertisement
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /