Guest User

ArduinoIMU

a guest
Jul 30th, 2023
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.83 KB | None | 0 0
  1. #include <Joystick.h> //include USB input libraries
  2. #include <Mouse.h>
  3. #include <Keyboard.h>
  4. #include <HID.h>
  5.  
  6. #include <Wire.h> //include basic Arduino interfacing libraries
  7. #include "I2Cdev.h"
  8. #include <EEPROM.h>
  9.  
  10. #include "RTIMUSettings.h" //include IMU libraries
  11. #include "RTIMU.h"
  12. #include "RTFusionRTQF.h"
  13. #include "CalLib.h"
  14.  
  15.  
  16. RTIMU *imu;                                           // the IMU object
  17. RTFusionRTQF fusion;                                  // the fusion object
  18. RTIMUSettings settings;                               // the settings object
  19. RTMath mathobj;
  20.  
  21. //  DISPLAY_INTERVAL sets the rate at which results are displayed
  22.  
  23. #define DISPLAY_INTERVAL  20                         // interval between pose displays in milliseconds
  24.  
  25. //  SERIAL_PORT_SPEED defines the baud speed to use for the debug serial port
  26.  
  27. #define  SERIAL_PORT_SPEED  115200
  28.  
  29. #define JOYSTICK 1
  30.  
  31.  
  32. unsigned long lastDisplay;  //declare a bunch of things for stuff
  33. unsigned long zeroTime = 2000;
  34. bool zeroed = false;
  35. unsigned long lastRate;
  36. int sampleCount;
  37.  
  38. //JOYSTICK STUFF
  39.  
  40.  
  41. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,   //declare the USB joystick with settings that only enable two X,Y axes with 10 bit precision each
  42.   JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
  43.   true, true, false, false, false, false,
  44.   true, true, false, false, false);
  45.  
  46. // Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
  47. const bool testAutoSendMode = false;
  48.  
  49. const unsigned long gcCycleDelta = 1000; //declare more things that I forget why
  50. const unsigned long gcAnalogDelta = 25;
  51. const unsigned long gcButtonDelta = 500;
  52. unsigned long gNextTime = 0;
  53. unsigned int gCurrentStep = 0;
  54. int xAxis;
  55. int yAxis;
  56. int xoffset;
  57. int yoffset;
  58. float pitch1;
  59. float yaw1;
  60.  
  61. void setup() //Do this on device powerup
  62. {
  63.     int errcode;
  64.     if (!JOYSTICK) {Serial.begin(SERIAL_PORT_SPEED);}  //if joystick isn't present yet, start the serial port
  65.     Wire.begin(); //start the wire interfacing
  66.     imu = RTIMU::createIMU(&settings);                        // create the imu object
  67.     //all serial port debugging code has been commented out as it is no longer needed:
  68.     //Serial.print("ArduinoIMU starting using device "); Serial.println(imu->IMUName());
  69.     if ((errcode = imu->IMUInit()) < 0) {
  70.         //Serial.print("Failed to init IMU: "); Serial.println(errcode);
  71.     }
  72.  
  73.     //if (imu->getCalibrationValid())
  74.         //Serial.println("Using compass calibration");
  75.     //else
  76.         //Serial.println("No valid compass calibration data");
  77.  
  78.     lastDisplay = lastRate = millis();  //set the two timekeeping variables to equal the current time
  79.     sampleCount = 0;
  80.  
  81.     // Slerp power controls the fusion and can be between 0 and 1
  82.     // 0 means that only gyros are used, 1 means that only accels/compass are used
  83.     // In-between gives the fusion mix.  0.02 default
  84.    
  85.     fusion.setSlerpPower(0.005); //0.005 is perfect jan072022
  86.    
  87.     // use of sensors in the fusion algorithm can be controlled here
  88.     // change any of these to false to disable that sensor
  89.    
  90.     fusion.setGyroEnable(true);
  91.     fusion.setAccelEnable(true);
  92.     fusion.setCompassEnable(true);
  93.  
  94.     //JOYSTICK STUFF
  95.   if (JOYSTICK) {
  96.         Joystick.setXAxisRange(-32766, 32766); //set X and Y axes to maximum 10 bit resolution
  97.         Joystick.setYAxisRange(-32766, 32766);
  98.         Joystick.setZAxisRange(-127, 127); //we're not really ussing these 3 other axes but I'm not touching that code
  99.         Joystick.setThrottleRange(0, 245);
  100.         Joystick.setRudderRange(0, 255);
  101.        
  102.         if (testAutoSendMode)
  103.         {
  104.           Joystick.begin();
  105.         }
  106.         else
  107.         {
  108.           Joystick.begin(false);
  109.         }
  110.  
  111.   }
  112. }
  113.  
  114. void loop() //Do this stuff while the device is powered on, after booting up
  115. {  
  116.     unsigned long now = millis();
  117.     unsigned long delta;
  118.     int loopCount = 1;
  119.  
  120.     while (imu->IMURead()) {                                // get the latest data if ready yet
  121.         // this flushes remaining data in case we are falling behind
  122.         if (++loopCount >= 10)
  123.             continue;
  124.         fusion.newIMUData(imu->getGyro(), imu->getAccel(), imu->getCompass(), imu->getTimestamp()); //do the thing that gets the stuff
  125.         sampleCount++; //increase the variable that tracks the number of samples we have taken
  126.         //all serial port debugging code has been commented out:
  127.         /*if ((delta = now - lastRate) >= 1000) {
  128.             Serial.print("Sample rate: "); Serial.print(sampleCount);
  129.             if (imu->IMUGyroBiasValid())
  130.                 Serial.println(", gyro bias valid");
  131.             else
  132.                 Serial.println(", calculating gyro bias");
  133.        
  134.             sampleCount = 0;
  135.             lastRate = now;
  136.         }*/
  137.         if ((now - lastDisplay) >= DISPLAY_INTERVAL) {  //Do this every DISPLAY_INTERVAL (20ms)
  138.             lastDisplay = now; //reset timer
  139.             //this is debugging code, commented out:
  140. //          RTMath::display("Gyro:", (RTVector3&)imu->getGyro());                // gyro data
  141. //          RTMath::display("Accel:", (RTVector3&)imu->getAccel());              // accel data
  142. //          RTMath::display("Mag:", (RTVector3&)imu->getCompass());              // compass data
  143.             if (!JOYSTICK) {
  144.             RTMath::displayRollPitchYaw("Pose:", (RTVector3&)fusion.getFusionPose()); // fuse the output
  145.             Serial.println();
  146.             }
  147.         }
  148.     }
  149.  
  150.  
  151.     if (JOYSTICK) { //If Joystick is working properly, convert IMU radians output to Joystick-compatible integers
  152.           pitch1 = ((RTVector3&)fusion.getFusionPose()).y(); //pitch1 is IMU library output in radians
  153.           pitch1 *= RTMATH_RAD_TO_DEGREE; //convert that to degrees
  154.           pitch1 += 90; //do a bunch of math to convert 360 values into 65532 values
  155.           pitch1 = pitch1 / 180;
  156.           pitch1 *= 65532;
  157.           pitch1 -= 32766;
  158.           yAxis = pitch1 - yoffset; //subtract the zeroing offset
  159.      
  160.           yaw1 = ((RTVector3&)fusion.getFusionPose()).z(); //do all that again but for the other axis
  161.           yaw1 *= RTMATH_RAD_TO_DEGREE;
  162.           yaw1 += 90;
  163.           yaw1 = yaw1 / 180;
  164.           yaw1 *= 65532;
  165.           yaw1 -= 32766;
  166.           xAxis = yaw1 - xoffset;
  167.  
  168.           if (!zeroed && (millis() > 2000)) { //if device has been powered on for more than 2 seconds and we haven't zeroed yet
  169.           xoffset = xAxis; //set the offset values to the current x,y values
  170.           yoffset = yAxis;
  171.           zeroed = true;  //we've zeroed now, don't do this again
  172.           }
  173.      
  174.           Joystick.setXAxis(xAxis); //tell the USB library code to actually send that data we just calculated
  175.           Joystick.setYAxis(yAxis);
  176.          
  177.           if (testAutoSendMode == false) {
  178.             Joystick.sendState();
  179.           }
  180.     }
  181.  
  182. }
  183.  
  184. //that's all folks
  185.  
Advertisement
Add Comment
Please, Sign In to add comment