Alexandre Salconi-Denis / Mbed 2 deprecated Nucleo_i2c_9dof

Dependencies:   L3GD20 LSM303DLH debug_addon_graphique_ASCII mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  //#include "debug.h"
00003  
00004 #include "LSM303DLH.h"
00005 #include "AnsiGraphicalConsole.h"
00006 #include "vector.h"
00007 #include "L3GD20.h"
00008  
00009 #define LSM303_REG_TEMP (0x00) // Temperature Register
00010 #define LSM303_REG_CONF (0x01) // Configuration Register
00011 #define LSM303_ADDR     (0x90) // LSM303 address
00012  
00013  
00014 #define USE_LM303
00015 #define USE_L3GD20
00016 DigitalIn mybutton(USER_BUTTON);
00017 I2C ptr_i2c(I2C_SDA, I2C_SCL);
00018 #if defined(USE_L3GD20)
00019 L3GD20    gyro(&ptr_i2c);//(I2C_SDA, I2C_SCL);   
00020 #endif
00021 #if defined(USE_LM303)
00022 LSM303DLH compass(&ptr_i2c);//(I2C_SDA, I2C_SCL);
00023 #endif
00024 
00025 DigitalOut myled(LED1);
00026  
00027 Serial pc(SERIAL_TX, SERIAL_RX);
00028  
00029 //volatile char TempCelsiusDisplay[] = "+abc.d C";
00030 
00031 int main()
00032 {
00033  
00034     pc.printf("\n Test Salco \n");
00035     clearscreen();
00036     
00037     /*char data_write[2];
00038     char data_read[2];*/
00039     
00040     /* Configure the Temperature sensor device STLM75:
00041     - Thermostat mode Interrupt
00042     - Fault tolerance: 0
00043     */
00044     //data_write[0] = LM75_REG_CONF;
00045    // data_write[1] = 0x02;
00046    // int status =
00047     /*if (status != 0) { // Error
00048         while (1) {
00049             myled = !myled;
00050             wait(0.2);
00051         }
00052     }*/
00053     
00054 #if defined(USE_LM303)
00055   //compass.setOffset(29.50, -0.50, 4.00); // example calibration
00056   compass.setScale(1.00,1.00,1.00); //1.03, 1.21);    // example calibration
00057   compass.frequency(100000);
00058   vector accelerometer,magnetometer;
00059   float hdg;
00060   #endif
00061     
00062     #if defined(USE_L3GD20)
00063     vector gyroscope;
00064     #endif
00065     char index=0;
00066     
00067     while (1) {
00068         
00069         
00070         //hdg = compass.heading();
00071         //debug.printf("Heading: %.2f\n", hdg);
00072  
00073         // Display result
00074         //pc.printf("Heading: %.2f\n", hdg);
00075         myled = !myled;
00076         if(index>=2)
00077         {
00078             #if defined(USE_LM303)          
00079             if(compass.read(accelerometer,magnetometer))
00080             {
00081                 setText(0,10,"accelerometer: X:%.2f Y:%.2f Z:%.2f\n", accelerometer.x,accelerometer.y,accelerometer.z);
00082                 setText(0,11,"magnetometer:  X:%.4f Y:%.4f Z:%.4f\n", magnetometer.x,magnetometer.y,magnetometer.z);
00083                 
00084                
00085                 hdg = compass.heading((vector){0,0,-1});
00086                 setText(0,12,"Heading: %.2f\n", hdg);
00087                 //magnetometre {+-20@ 0}== axe nord/sud
00088                 //si X pos coter NORD et si X neg SUD
00089             }
00090             else
00091             {
00092                 pc.printf("Error with I2C com\n");
00093             }
00094             #endif
00095             index=0;
00096             #if defined(USE_L3GD20)
00097             if(gyro.read(&gyroscope.x,&gyroscope.y,&gyroscope.z))
00098             {
00099                 gyro.Convert_to_RadPerSec(&gyroscope.x,&gyroscope.y,&gyroscope.z);
00100                 setText(0,13,"gyroscope:  X:%.2f Y:%.2f Z:%.2f\n", gyroscope.x,gyroscope.y,gyroscope.z);
00101             }
00102             else
00103             {
00104                 pc.printf("Error with I2C com\n");
00105                 
00106             }
00107             #endif
00108         }
00109                 
00110         wait(.5);
00111         index++;
00112         
00113          if (mybutton == 0) 
00114          { // Button is pressed
00115              do{
00116                 clearscreen();
00117                 setText(0,1,"PAUSE\n");
00118                 wait(2);
00119              }while(mybutton != 0);
00120              clearscreen();
00121          }
00122     }
00123  
00124 }
00125