Advertisement
alessia_rapalino

3 imu with RTIMULib

Jun 16th, 2017
3,397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  This file is part of RTIMULib-Arduino
  2. //
  3. //  Copyright (c) 2014-2015, richards-tech
  4. //
  5. //  Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. //  this software and associated documentation files (the "Software"), to deal in
  7. //  the Software without restriction, including without limitation the rights to use,
  8. //  copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  9. //  Software, and to permit persons to whom the Software is furnished to do so,
  10. //  subject to the following conditions:
  11. //
  12. //  The above copyright notice and this permission notice shall be included in all
  13. //  copies or substantial portions of the Software.
  14. //
  15. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. //  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. //  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. //  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. //  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  20. //  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. #include <Wire.h>
  23. #include "I2Cdev.h"
  24. #include "RTIMUSettings.h"
  25. #include "RTIMU.h"
  26. #include "RTFusionRTQF.h"
  27. #include "CalLib.h"
  28. #include <EEPROM.h>
  29.  
  30.  
  31. RTIMU *imu;                                           // the IMU object
  32. RTIMU *imu2;
  33. RTIMU *imu3;
  34.  
  35. RTFusionRTQF fusion;                                  // the fusion object
  36. RTFusionRTQF fusion2;
  37. RTFusionRTQF fusion3;
  38.  
  39. RTIMUSettings settings;                               // the settings object
  40.  
  41.  
  42.  
  43.  
  44. //  DISPLAY_INTERVAL sets the rate at which results are displayed
  45.  
  46. #define DISPLAY_INTERVAL  30                         // interval between pose displays
  47.  
  48. //  SERIAL_PORT_SPEED defines the speed to use for the debug serial port
  49.  
  50. #define  SERIAL_PORT_SPEED  9600
  51.  
  52. int EN = 53;
  53. int S0 = 47;
  54. int S1 = 49;
  55. int S2 = 50;
  56.  
  57. void out1()
  58. {
  59.   digitalWrite (EN,LOW);
  60.   digitalWrite (S0,LOW);
  61.   digitalWrite (S1,LOW);
  62.   digitalWrite (S2,LOW);
  63.   }
  64.  
  65. void out2()
  66. {
  67.   digitalWrite (EN,LOW);
  68.   digitalWrite (S0,LOW);
  69.   digitalWrite (S1,HIGH);
  70.   digitalWrite (S2,LOW);
  71.   }
  72.  
  73. void out3()
  74. {
  75.   digitalWrite (EN,LOW);
  76.   digitalWrite (S0,LOW);
  77.   digitalWrite (S1,LOW);
  78.   digitalWrite (S2,HIGH);
  79.   }
  80.  
  81. unsigned long lastDisplay;
  82. unsigned long lastDisplay2;
  83. unsigned long lastDisplay3;
  84.  
  85. unsigned long lastRate;
  86. unsigned long lastRate2;
  87. unsigned long lastRate3;
  88.  
  89. int sampleCount;
  90. int sampleCount2;
  91. int sampleCount3;
  92.  
  93.  
  94. void setup()
  95. {
  96.  out3();
  97.  out2();
  98.  out1();
  99.  
  100.  
  101.   pinMode (EN,OUTPUT);
  102.   pinMode (S0,OUTPUT);
  103.   pinMode (S1,OUTPUT);
  104.   pinMode (S2,OUTPUT);
  105.  
  106.     int errcode;
  107.  
  108.     Serial.begin(SERIAL_PORT_SPEED);
  109.     Wire.begin();
  110.  
  111.  ///////// inizializzazione imu1  /////////////////
  112.  
  113.    out1();
  114.    
  115.    imu = RTIMU::createIMU(&settings);                        // create the imu object
  116. //
  117. //  
  118.    Serial.print("ArduinoIMU starting using the first device "); Serial.println(imu->IMUName());
  119.   if ((errcode = imu->IMUInit()) < 0) {
  120.       Serial.print("Failed to init IMU: "); Serial.println(errcode);
  121.  }
  122. //
  123.    if (imu->getCalibrationValid())
  124.        Serial.println("1: Using compass calibration");
  125.     else
  126.        Serial.println("1: No valid compass calibration data");
  127. //
  128.     lastDisplay = lastRate = millis();
  129.     sampleCount = 0;
  130. //
  131. //    // Slerp power controls the fusion and can be between 0 and 1
  132. //    // 0 means that only gyros are used, 1 means that only accels/compass are used
  133. //    // In-between gives the fusion mix.
  134. //    
  135.     fusion.setSlerpPower(0.02);
  136. //    
  137. //    // use of sensors in the fusion algorithm can be controlled here
  138. //    // change any of these to false to disable that sensor
  139. //    
  140.     fusion.setGyroEnable(true);
  141.     fusion.setAccelEnable(true);
  142.     fusion.setCompassEnable(true);
  143.  
  144.   ///////// inizializzazione imu2  /////////////////
  145.  
  146.   out2();
  147.    
  148.     imu2 = RTIMU::createIMU(&settings);                        // create the imu object
  149.  
  150.     Serial.print("ArduinoIMU starting using the second device "); Serial.println(imu2->IMUName());
  151.     if ((errcode = imu2->IMUInit2()) < 0) {
  152.        Serial.print("Failed to init the second IMU: "); Serial.println(errcode);
  153.    }
  154.  
  155.    if (imu2->getCalibrationValid())
  156.         Serial.println("2: Using compass calibration");
  157.     else
  158.         Serial.println("2: No valid compass calibration data");
  159.  
  160.     lastDisplay2 = lastRate2 = millis();
  161.     sampleCount2 = 0;
  162.     // Slerp power controls the fusion and can be between 0 and 1
  163.     // 0 means that only gyros are used, 1 means that only accels/compass are used
  164.     // In-between gives the fusion mix.
  165.    
  166.     fusion2.setSlerpPower(0.02);
  167.    
  168.     // use of sensors in the fusion algorithm can be controlled here
  169.     // change any of these to false to disable that sensor
  170.    
  171.     fusion2.setGyroEnable(true);
  172.     fusion2.setAccelEnable(true);
  173.     fusion2.setCompassEnable(true);
  174.  
  175.    
  176.   ///////// inizializzazione imu3  /////////////////
  177.  
  178.  out3();
  179. //
  180.    imu3 = RTIMU::createIMU(&settings);                        // create the imu object
  181. //  
  182.     Serial.print("ArduinoIMU starting using the third device "); Serial.println(imu3->IMUName());
  183. //        
  184.   if ((errcode = imu3->IMUInit3()) < 0) {
  185.      Serial.print("Failed to init IMU: "); Serial.println(errcode);
  186.  }
  187. //
  188. if (imu3->getCalibrationValid())
  189.       Serial.println("3: Using compass calibration");
  190.     else
  191.        Serial.println("3: No valid compass calibration data");
  192.  
  193.     lastDisplay3 = lastRate3 = millis();
  194.    sampleCount3 = 0;
  195. //    // Slerp power controls the fusion and can be between 0 and 1
  196. //    // 0 means that only gyros are used, 1 means that only accels/compass are used
  197. //    // In-between gives the fusion mix.
  198. //    
  199.     fusion3.setSlerpPower(0.02);
  200. //    
  201. //    // use of sensors in the fusion algorithm can be controlled here
  202. //    // change any of these to false to disable that sensor
  203. //    
  204.    fusion3.setGyroEnable(true);
  205.     fusion3.setAccelEnable(true);
  206.     fusion3.setCompassEnable(true);
  207.      
  208. }
  209.  
  210. void loop()
  211. {  
  212.   out1();
  213.      
  214.     unsigned long now = millis();
  215.     unsigned long now2 = millis();
  216.     unsigned long now3 = millis();
  217.     unsigned long delta;
  218.     unsigned long delta2;
  219.     unsigned long delta3;
  220.     int loopCount = 1;
  221.     int loopCount2 = 1;
  222.     int loopCount3 = 1;
  223.  
  224.       out1();
  225.  
  226.      
  227.     while (imu->IMURead()) {   // Serial.println("ciao ciao");                        // get the latest data if ready yet
  228.         // this flushes remaining data in case we are falling behind
  229.         if (++loopCount >= 3)
  230.             continue;
  231.  
  232.          
  233.        fusion.newIMUData(imu->getGyro(), imu->getAccel(), imu->getCompass(), imu->getTimestamp());
  234.        
  235.         sampleCount++;
  236.         if ((delta = now - lastRate) >= 1000) {
  237.   /*          Serial.print("Sample rate: "); Serial.print(sampleCount);
  238.             if (imu->IMUGyroBiasValid())
  239.                 Serial.println(", gyro bias valid");
  240.             else
  241.                 Serial.println(", calculating gyro bias");*/
  242.        
  243.             sampleCount = 0;
  244.             lastRate = now;
  245.         }
  246.         if ((now - lastDisplay) >= DISPLAY_INTERVAL) {
  247.            
  248.             lastDisplay = now;
  249. //          RTMath::display("Gyro:", (RTVector3&)imu->getGyro());                // gyro data
  250.             RTMath::display("Accel1:", (RTVector3&)imu->getAccel());              // accel data
  251. //          RTMath::display("Mag:", (RTVector3&)imu->getCompass());              // compass data
  252.             RTMath::displayRollPitchYaw("Pose1:", (RTVector3&)fusion.getFusionPose()); // fused output
  253.            Serial.println();
  254.      
  255.         }
  256.     }
  257.  
  258.     out2();
  259.    
  260.    
  261.    while (imu2->IMURead()) {  //Serial.println("ciao ciao");                    // get the latest data if ready yet
  262.         // this flushes remaining data in case we are falling behind
  263.            
  264.        if (++loopCount2 >= 3)
  265.            continue;
  266.          
  267.         fusion2.newIMUData(imu2->getGyro(), imu2->getAccel(), imu2->getCompass(), imu2->getTimestamp());
  268.        
  269.         sampleCount2++;
  270.         if ((delta2 = now2 - lastRate2) >= 1000) {
  271.   /*          Serial.print("Sample rate: "); Serial.print(sampleCount);
  272.             if (imu->IMUGyroBiasValid())
  273.                 Serial.println(", gyro bias valid");
  274.             else
  275.                 Serial.println(", calculating gyro bias");*/
  276.        
  277.             sampleCount2 = 0;
  278.             lastRate2 = now2;
  279.         }
  280.         if ((now2 - lastDisplay2) >= DISPLAY_INTERVAL) {
  281.             lastDisplay2 = now2;
  282. //          RTMath::display("Gyro:", (RTVector3&)imu->getGyro());                // gyro data
  283.           RTMath::display("Accel2:", (RTVector3&)imu2->getAccel());              // accel data
  284. //          RTMath::display("Mag:", (RTVector3&)imu->getCompass());              // compass data
  285.           RTMath::displayRollPitchYaw("Pose2:", (RTVector3&)fusion2.getFusionPose()); // fused output
  286.            Serial.println();
  287.      
  288.  
  289.         }
  290.    }
  291.    
  292.      out3();
  293.      
  294.  
  295.    while (imu3->IMURead()) {   //  Serial.println("ciao ciao");                 // get the latest data if ready yet
  296.        // this flushes remaining data in case we are falling behind
  297.       if (++loopCount3 >= 10)
  298.            continue;
  299.       fusion3.newIMUData(imu3->getGyro(), imu3->getAccel(), imu3->getCompass(), imu3->getTimestamp());
  300.         sampleCount3++;
  301.        if ((delta3 = now3 - lastRate3) >= 1000) {
  302.  /*          Serial.print("Sample rate: "); Serial.print(sampleCount);
  303.           if (imu->IMUGyroBiasValid())
  304.                Serial.println(", gyro bias valid");
  305.           else
  306.                Serial.println(", calculating gyro bias");*/
  307.        
  308.           sampleCount3 = 0;
  309.            lastRate3 = now3;
  310.         }
  311.        if ((now3 - lastDisplay3) >= DISPLAY_INTERVAL) {
  312.             lastDisplay3 = now3;
  313.          RTMath::display("Gyro:", (RTVector3&)imu->getGyro());                // gyro data
  314.           RTMath::display("Accel3:", (RTVector3&)imu3->getAccel());              // accel data
  315.           RTMath::display("Mag:", (RTVector3&)imu->getCompass());              // compass data
  316.            RTMath::displayRollPitchYaw("Pose3:", (RTVector3&)fusion3.getFusionPose()); // fused output
  317.          Serial.println();
  318.         }
  319.    }
  320.  
  321.  
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement