publish

Dependencies:   mbed

Dependents:   RoboCup_2015

Fork of LSM9DS0 by Taylor Andrews

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Most of the Credit goes to jimblom and Judah Okeleye
00002 #include "LSM9DS0.h"
00003 #include "mbed.h"
00004 
00005 // SDO_XM and SDO_G are both grounded, so our addresses are:
00006 #define LSM9DS0_XM  0x1D // Would be 0x1E if SDO_XM is LOW
00007 #define LSM9DS0_G   0x6B // Would be 0x6A if SDO_G is LOW
00008 
00009 
00010 LSM9DS0 dof(p9, p10,LSM9DS0_G, LSM9DS0_XM);
00011 Serial pc(USBTX, USBRX); // tx, rx
00012 
00013 //Uncomment If you want to use interrupts
00014 /*DigitalIn pinDRDYG(p14);
00015 DigitalIn pinINTG(p13);
00016 DigitalIn pinINT1_XM(p14);
00017 DigitalIn pinINT2_XM(p15);*/
00018 
00019 #define PRINT_CALCULATED
00020 #define PRINT_SPEED 500 // 500 ms between prints
00021 
00022 //Init Serial port and LSM9DS0 chip
00023 void setup()
00024 {
00025     pc.baud(115200); // Start serial at 115200 bps
00026     // Use the begin() function to initialize the LSM9DS0 library.
00027     // You can either call it with no parameters (the easy way):
00028     uint16_t status = dof.begin();
00029     
00030    
00031    //Make sure communication is working
00032     pc.printf("LSM9DS0 WHO_AM_I's returned: 0x");
00033     pc.printf("%x\n",status);
00034     pc.printf("Should be 0x49D4\n");
00035     pc.printf("\n");
00036 }
00037 
00038 
00039 void printGyro()
00040 {
00041     // To read from the gyroscope, you must first call the
00042     // readGyro() function. When this exits, it'll update the
00043     // gx, gy, and gz variables with the most current data.
00044     
00045     //Uncomment code if you plan on using interrupts
00046     /*while(pinDRDYG == 0)
00047     {
00048         ;
00049     }*/
00050     
00051     
00052     dof.readGyro();
00053 
00054     // Now we can use the gx, gy, and gz variables as we please.
00055     // Either print them as raw ADC values, or calculated in DPS.
00056     pc.printf("G: ");
00057 #ifdef PRINT_CALCULATED
00058     // If you want to print calculated values, you can use the
00059     // calcGyro helper function to convert a raw ADC value to
00060     // DPS. Give the function the value that you want to convert.
00061     //pc.printf("The bias: %2f, %2f, %2f \n", dof.gbias[0], dof.gbias[1], dof.gbias[2]);
00062     pc.printf("%2f",dof.calcGyro(dof.gx) - dof.gbias[0]);
00063     pc.printf(", ");
00064     pc.printf("%2f",dof.calcGyro(dof.gy) - dof.gbias[1]);
00065     pc.printf(", ");
00066     pc.printf("%2f\n",dof.calcGyro(dof.gz) - dof.gbias[2]);
00067 #elif defined PRINT_RAW
00068     pc.printf("%d", dof.gx);
00069     pc.printf(", ");
00070     pc.printf("%d",dof.gy);
00071     pc.printf(", ");
00072     pc.printf("%d\n",dof.gz);
00073 #endif
00074 }
00075 
00076 void printAccel()
00077 {
00078     //Uncomment code if you plan on using interrupts
00079     /*while(pinINT1_XM == 0)
00080     {
00081         ;
00082     }*/
00083     
00084 
00085     dof.readAccel();
00086     
00087     // Now we can use the ax, ay, and az variables as we please.
00088     // Either print them as raw ADC values, or calculated in g's.
00089     pc.printf("A: ");
00090 #ifdef PRINT_CALCULATED
00091     // If you want to print calculated values, you can use the
00092     // calcAccel helper function to convert a raw ADC value to
00093     // g's. Give the function the value that you want to convert.
00094     //pc.printf("The bias: %2f, %2f, %2f \n", dof.abias[0], dof.abias[1], dof.abias[2]);
00095     pc.printf("%2f",dof.calcAccel(dof.ax) - dof.abias[0]);
00096     pc.printf(", ");
00097     pc.printf("%2f",dof.calcAccel(dof.ay) - dof.abias[1]);
00098     pc.printf(", ");
00099     pc.printf("%2f\n",dof.calcAccel(dof.az) - dof.abias[2]);
00100 #elif defined PRINT_RAW
00101     pc.printf("%d",dof.ax);
00102     pc.printf(", ");
00103     pc.printf("%d",dof.ay);
00104     pc.printf(", ");
00105     pc.printf("%d\n",dof.az);
00106 #endif
00107     
00108 }
00109 
00110 void printMag()
00111 {
00112      //Uncomment code if you plan on using interrupts
00113     /*while(pinINT2_XM == 0)
00114     {
00115         ;
00116     }*/
00117     
00118     // To read from the magnetometer, you must first call the
00119     // readMag() function. When this exits, it'll update the
00120     // mx, my, and mz variables with the most current data.
00121     dof.readMag();
00122     
00123     // Now we can use the mx, my, and mz variables as we please.
00124     // Either print them as raw ADC values, or calculated in Gauss.
00125     pc.printf("M: ");
00126 #ifdef PRINT_CALCULATED
00127     // If you want to print calculated values, you can use the
00128     // calcMag helper function to convert a raw ADC value to
00129     // Gauss. Give the function the value that you want to convert.
00130     pc.printf("%2f",dof.calcMag(dof.mx));
00131     pc.printf(", ");
00132     pc.printf("%2f",dof.calcMag(dof.my));
00133     pc.printf(", ");
00134     pc.printf("%2f\n",dof.calcMag(dof.mz));
00135 #elif defined PRINT_RAW
00136     pc.printf("%d", dof.mx);
00137     pc.printf(", ");
00138     pc.printf("%d", dof.my);
00139     pc.printf(", ");
00140     pc.printf("%d\n", dof.mz);
00141 #endif
00142 }
00143 
00144 void loop()
00145 {
00146     printGyro();  // Print "G: gx, gy, gz" 
00147     printAccel(); // Print "A: ax, ay, az"
00148     printMag();   // Print "M: mx, my, mz"    
00149     wait_ms(PRINT_SPEED);
00150 }
00151 
00152 
00153 int main()
00154 {
00155     setup();  //Setup sensor and Serial
00156     pc.printf("Hello World\n");
00157 
00158     while (true)
00159     {
00160          //Calculate bias
00161          dof.calLSM9DS0(dof.gbias, dof.abias);
00162          //pc.printf("Setup Done\n");
00163          loop(); //Get sensor values
00164    }
00165 }
00166