Guest User

Untitled

a guest
Apr 13th, 2012
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1.  
  2. /************************************************************************************
  3.  *  
  4.  *  Name    : Comp6DOF_n0m1 Library Example: Yaw, Pitch, Roll                      
  5.  *  Author  : Noah Shibley, Michael Grant, NoMi Design Ltd. http://n0m1.com                      
  6.  *  Date    : Feb 27th 2012                                    
  7.  *  Version : 0.1                                              
  8.  *  Notes   : Arduino Library for compass tilt compensation and hard iron offset
  9.  *
  10.  ***********************************************************************************/
  11.  
  12. #include <I2C.h>
  13. #include <ADXL345.h>
  14. #include <Comp6DOF_n0m1.h>
  15. #include <HMC5883L.h> // Reference the HMC5883L Compass Library
  16. #include <Wire.h>
  17.  
  18. ADXL345 accel;
  19. Comp6DOF_n0m1 sixDOF;
  20. HMC5883L compass;
  21.  
  22. // Record any errors that may occur in the compass.
  23. int error = 0;
  24.  
  25.  
  26. void setup()
  27. {
  28.   Serial.begin(115200);
  29.   accel.powerOn();
  30.   accel.setRangeSetting(2);
  31.   accel.setFullResBit(true);
  32.  
  33.   //accel.dataMode(true, 2); //enable highRes 10bit, 2g range [2g,4g,8g]
  34.  
  35.   compass = HMC5883L(); // Construct a new HMC5883 compass.
  36.  
  37.   error = compass.SetScale(1.3); // Set the scale of the compass.
  38.   if(error != 0) // If there is an error, print it out.
  39.     Serial.println(compass.GetErrorText(error));
  40.  
  41.   error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  42.   if(error != 0) // If there is an error, print it out.
  43.     Serial.println(compass.GetErrorText(error));
  44.  
  45.   Serial.println("n0m1.com");
  46.  
  47.   /* hard iron offset finder
  48.    int doneoffset =0;
  49.    while (doneoffset ==0)
  50.    {
  51.    // Should indicate with a blink right here
  52.    int donecombo = 0;
  53.    while ( donecombo == 0 )   // load tuning array
  54.    {  
  55.    delay (50);
  56.    MagnetometerRaw  raw = compass.ReadRawAxis();
  57.    donecombo = sixDOF.deviantSpread (raw.XAxis, raw.YAxis, raw.ZAxis);
  58.    }
  59.    
  60.    doneoffset = sixDOF.calOffsets();
  61.    }
  62.    */
  63.  
  64. }
  65.  
  66. void loop()
  67. {
  68.  
  69.   // poll sensors for new data
  70.   //accel.update();
  71.   MagnetometerRaw raw = compass.ReadRawAxis();
  72.  
  73.   // offset compass by hard iron
  74.   // raw.XAxis -= 40;
  75.   // raw.YAxis -= 100;
  76.   // raw.ZAxis -= 350;
  77.  
  78.   int x,y,z;  
  79.   accel.readAccel(&x, &y, &z);
  80.  
  81.   //enter compass data and accel data for calculation
  82.   sixDOF.compCompass(raw.XAxis, raw.YAxis, raw.ZAxis, x, y, z, false);
  83.  
  84.   Serial.print (sixDOF.yaw()/100);
  85.   Serial.print (",");
  86.   Serial.print (sixDOF.pitch()/100);      
  87.   Serial.print (",");
  88.   Serial.print (sixDOF.roll()/100);  
  89.   Serial.println ("");
  90.  
  91.   delay(5);
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment