Advertisement
Guest User

Arduino IMU

a guest
Apr 23rd, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. #include <Joystick.h>
  2.  
  3. ////////////////////////////////////////////////////////////////////////////
  4. //
  5. //  This file is part of RTIMULib-Arduino
  6. //
  7. //  Copyright (c) 2014-2015, richards-tech
  8. //
  9. //  Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. //  this software and associated documentation files (the "Software"), to deal in
  11. //  the Software without restriction, including without limitation the rights to use,
  12. //  copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  13. //  Software, and to permit persons to whom the Software is furnished to do so,
  14. //  subject to the following conditions:
  15. //
  16. //  The above copyright notice and this permission notice shall be included in all
  17. //  copies or substantial portions of the Software.
  18. //
  19. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  20. //  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  21. //  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. //  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. //  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. //  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  
  26. #include <Wire.h>
  27. #include "I2Cdev.h"
  28. #include "RTIMUSettings.h"
  29. #include "RTIMU.h"
  30. #include "RTFusionRTQF.h"
  31. #include "CalLib.h"
  32. #include <EEPROM.h>
  33.  
  34. RTIMU *imu;                                           // the IMU object
  35. RTFusionRTQF fusion;                                  // the fusion object
  36. RTIMUSettings settings;                               // the settings object
  37. RTMath mathobj;
  38.  
  39. //  DISPLAY_INTERVAL sets the rate at which results are displayed
  40.  
  41. #define DISPLAY_INTERVAL  20                         // interval between pose displays
  42.  
  43. //  SERIAL_PORT_SPEED defines the speed to use for the debug serial port
  44.  
  45. #define  SERIAL_PORT_SPEED  115200
  46.  
  47. #define JOYSTICK 1
  48.  
  49.  
  50. unsigned long lastDisplay;
  51. unsigned long lastRate;
  52. int sampleCount;
  53.  
  54. //JOYSTICK STUFF
  55.  
  56.  
  57. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  58.   JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
  59.   true, true, false, false, false, false,
  60.   true, true, false, false, false);
  61.  
  62. // Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
  63. //const bool testAutoSendMode = true;
  64. const bool testAutoSendMode = false;
  65.  
  66. const unsigned long gcCycleDelta = 1000;
  67. const unsigned long gcAnalogDelta = 25;
  68. const unsigned long gcButtonDelta = 500;
  69. unsigned long gNextTime = 0;
  70. unsigned int gCurrentStep = 0;
  71. int xAxis;
  72. int yAxis;
  73.  
  74. float pitch1;
  75. float yaw1;
  76.  
  77. void setup()
  78. {
  79.     int errcode;
  80.  
  81.     if (!JOYSTICK) {Serial.begin(SERIAL_PORT_SPEED);}
  82.     Wire.begin();
  83.     imu = RTIMU::createIMU(&settings);                        // create the imu object
  84.  
  85.     //Serial.print("ArduinoIMU starting using device "); Serial.println(imu->IMUName());
  86.     if ((errcode = imu->IMUInit()) < 0) {
  87.         //Serial.print("Failed to init IMU: "); Serial.println(errcode);
  88.     }
  89.  
  90.     //if (imu->getCalibrationValid())
  91.         //Serial.println("Using compass calibration");
  92.     //else
  93.         //Serial.println("No valid compass calibration data");
  94.  
  95.     lastDisplay = lastRate = millis();
  96.     sampleCount = 0;
  97.  
  98.     // Slerp power controls the fusion and can be between 0 and 1
  99.     // 0 means that only gyros are used, 1 means that only accels/compass are used
  100.     // In-between gives the fusion mix.  0.02 default
  101.    
  102.     fusion.setSlerpPower(0.005);
  103.    
  104.     // use of sensors in the fusion algorithm can be controlled here
  105.     // change any of these to false to disable that sensor
  106.    
  107.     fusion.setGyroEnable(true);
  108.     fusion.setAccelEnable(true);
  109.     fusion.setCompassEnable(true);
  110.  
  111.     //JOYSTICK STUFF
  112.   if (JOYSTICK) {
  113.         Joystick.setXAxisRange(-32766, 32766);
  114.         Joystick.setYAxisRange(-32766, 32766);
  115.         Joystick.setZAxisRange(-127, 127);
  116.         Joystick.setThrottleRange(0, 255);
  117.         Joystick.setRudderRange(0, 255);
  118.        
  119.         if (testAutoSendMode)
  120.         {
  121.           Joystick.begin();
  122.         }
  123.         else
  124.         {
  125.           Joystick.begin(false);
  126.         }
  127.  
  128.   }
  129. }
  130.  
  131. void loop()
  132. {  
  133.     unsigned long now = millis();
  134.     unsigned long delta;
  135.     int loopCount = 1;
  136.  
  137.     while (imu->IMURead()) {                                // get the latest data if ready yet
  138.         // this flushes remaining data in case we are falling behind
  139.         if (++loopCount >= 10)
  140.             continue;
  141.         fusion.newIMUData(imu->getGyro(), imu->getAccel(), imu->getCompass(), imu->getTimestamp());
  142.         sampleCount++;
  143.         /*if ((delta = now - lastRate) >= 1000) {
  144.             Serial.print("Sample rate: "); Serial.print(sampleCount);
  145.             if (imu->IMUGyroBiasValid())
  146.                 Serial.println(", gyro bias valid");
  147.             else
  148.                 Serial.println(", calculating gyro bias");
  149.        
  150.             sampleCount = 0;
  151.             lastRate = now;
  152.         }*/
  153.         if ((now - lastDisplay) >= DISPLAY_INTERVAL) {
  154.             lastDisplay = now;
  155. //          RTMath::display("Gyro:", (RTVector3&)imu->getGyro());                // gyro data
  156. //          RTMath::display("Accel:", (RTVector3&)imu->getAccel());              // accel data
  157. //          RTMath::display("Mag:", (RTVector3&)imu->getCompass());              // compass data
  158.             if (!JOYSTICK) {
  159.             RTMath::displayRollPitchYaw("Pose:", (RTVector3&)fusion.getFusionPose()); // fused output
  160.             Serial.println();
  161.             }
  162.         }
  163.     }
  164.  
  165.     //JOYSTICK STUFF
  166.     //add 90 to y axis, 180 to z axis
  167.     //Serial.print(","); Serial.print(vec.y() * RTMATH_RAD_TO_DEGREE);
  168.     //Serial.print(","); Serial.print(vec.z() * RTMATH_RAD_TO_DEGREE);
  169.  
  170.     if (JOYSTICK) {
  171.           pitch1 = ((RTVector3&)fusion.getFusionPose()).y();
  172.           pitch1 *= RTMATH_RAD_TO_DEGREE;
  173.           pitch1 += 90;
  174.           pitch1 = pitch1 / 180;
  175.           pitch1 *= 65532;
  176.           pitch1 -= 32766;
  177.           yAxis = pitch1;
  178.      
  179.           yaw1 = ((RTVector3&)fusion.getFusionPose()).z();
  180.           yaw1 *= RTMATH_RAD_TO_DEGREE;
  181.           yaw1 += 90;
  182.           yaw1 = yaw1 / 180;
  183.           yaw1 *= 65532;
  184.           yaw1 -= 32766;
  185.           xAxis = yaw1;
  186.      
  187.           Joystick.setXAxis(xAxis);
  188.           Joystick.setYAxis(yAxis);
  189.          
  190.           if (testAutoSendMode == false) {
  191.             Joystick.sendState();
  192.           }
  193.     }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement