Santhosh Kumar / Mbed 2 deprecated Gyro_Data_Acquisition1

Dependencies:   mbed X_NUCLEO_IKS01A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "XNucleoIKS01A2.h"
00003 
00004 /* Instantiate the expansion board */
00005 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00006 
00007 /* Retrieve the composing elements of the expansion board */
00008 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00009 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
00010 
00011 InterruptIn mybutton(USER_BUTTON);
00012 DigitalOut myled(LED1);
00013 
00014 volatile int mems_event = 0;
00015 volatile int mems_event1 = 0;
00016 volatile int step_count_reset_request = 0;
00017 uint32_t previous_tick = 0;
00018 uint32_t current_tick = 0;
00019 uint16_t step_count = 0;
00020 
00021 /* User button callback. */
00022 void pressed_cb() {
00023   step_count_reset_request = 1;
00024 }
00025 
00026 /* Interrupt 1 callback. */
00027 void int1_cb() {
00028   mems_event = 1;
00029 }
00030 
00031 /* Interrupt 1 callback. */
00032 void int2_cb() {
00033   mems_event1 = 1;
00034 }
00035 
00036 /* Print the orientation. */
00037 void send_orientation() {
00038   uint8_t xl = 0;
00039   uint8_t xh = 0;
00040   uint8_t yl = 0;
00041   uint8_t yh = 0;
00042   uint8_t zl = 0;
00043   uint8_t zh = 0;
00044   
00045   acc_gyro->get_6d_orientation_xl(&xl);
00046   acc_gyro->get_6d_orientation_xh(&xh);
00047   acc_gyro->get_6d_orientation_yl(&yl);
00048   acc_gyro->get_6d_orientation_yh(&yh);
00049   acc_gyro->get_6d_orientation_zl(&zl);
00050   acc_gyro->get_6d_orientation_zh(&zh);
00051   
00052   if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 ) {
00053     printf( "\r\n  ________________  " \
00054             "\r\n |                | " \
00055             "\r\n |  *             | " \
00056             "\r\n |                | " \
00057             "\r\n |                | " \
00058             "\r\n |                | " \
00059             "\r\n |                | " \
00060             "\r\n |________________| \r\n" );
00061   }
00062 
00063   else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) {
00064     printf( "\r\n  ________________  " \
00065             "\r\n |                | " \
00066             "\r\n |             *  | " \
00067             "\r\n |                | " \
00068             "\r\n |                | " \
00069             "\r\n |                | " \
00070             "\r\n |                | " \
00071             "\r\n |________________| \r\n" );
00072   }
00073   
00074   else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 ) {
00075     printf( "\r\n  ________________  " \
00076             "\r\n |                | " \
00077             "\r\n |                | " \
00078             "\r\n |                | " \
00079             "\r\n |                | " \
00080             "\r\n |                | " \
00081             "\r\n |  *             | " \
00082             "\r\n |________________| \r\n" );
00083             
00084             
00085             
00086   }
00087   
00088   else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) {
00089     printf( "\r\n  ________________  " \
00090             "\r\n |                | " \
00091             "\r\n |                | " \
00092             "\r\n |                | " \
00093             "\r\n |                | " \
00094             "\r\n |                | " \
00095             "\r\n |             *  | " \
00096             "\r\n |________________| \r\n" );
00097   }
00098   
00099   else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) {
00100     printf( "\r\n  __*_____________  " \
00101             "\r\n |________________| \r\n" );
00102   }
00103   
00104   else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 ) {
00105     printf( "\r\n  ________________  " \
00106             "\r\n |________________| " \
00107             "\r\n    *               \r\n" );
00108   }
00109   
00110   else {
00111     printf( "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" );
00112   }
00113 }
00114 
00115 int main() {
00116       uint8_t id;
00117         int32_t axes[3];
00118     /* Attach callback to User button press */
00119   mybutton.fall(&pressed_cb);
00120   
00121   /* Attach callback to LSM6DSL INT1 */
00122   acc_gyro->attach_int1_irq(&int1_cb);
00123   
00124   acc_gyro->enable_int1_irq();
00125   
00126     /* Attach callback to LSM6DSL INT1 */
00127   acc_gyro->attach_int2_irq(&int2_cb);
00128   
00129   
00130   acc_gyro->enable_int2_irq();
00131 
00132   /* Enable LSM6DSL accelerometer */
00133   acc_gyro->enable_x();
00134   /* Enable 6D Orientation. */
00135   acc_gyro->enable_6d_orientation();
00136   
00137   /* Enable Free Fall Detection. */
00138   acc_gyro->enable_free_fall_detection(LSM6DSL_INT2_PIN);  
00139   
00140     /* Enable Pedometer. */
00141   acc_gyro->enable_pedometer();
00142   
00143 /* Enable Tilt Detection. */
00144   acc_gyro->enable_tilt_detection();
00145   
00146   magnetometer->enable();
00147   
00148   previous_tick = clock();
00149   
00150   printf("\r\n--- Starting new run ---\r\n");
00151   
00152   magnetometer->read_id(&id);
00153   printf("LSM303AGR magnetometer            = 0x%X\r\n", id);
00154  
00155   while(1) {
00156     
00157     if (mems_event) 
00158     {
00159       mems_event = 0;
00160       LSM6DSL_Event_Status_t status;
00161       acc_gyro->get_event_status(&status);
00162       if (status.D6DOrientationStatus) {
00163         /* Send 6D Orientation */
00164         send_orientation();
00165         
00166         /* Led blinking. */
00167        // myled = 1;
00168         //wait(0.2);
00169        // myled = 0;
00170   
00171         
00172       }
00173 
00174       
00175       
00176       acc_gyro->get_event_status(&status);
00177         if (status.StepStatus) {
00178         /* New step detected, so print the step counter */
00179         acc_gyro->get_step_counter(&step_count);
00180         printf("Step counter: %d\r\n", step_count);
00181       
00182         /* Led blinking. */
00183         //myled = 1;
00184         //wait(0.1);
00185       //  myled = 0;
00186       }
00187       acc_gyro->get_event_status(&status);
00188         if (status.TiltStatus) {
00189         /* Led blinking. */
00190         //myled = 1;
00191         //wait(0.2);
00192        // myled = 0;
00193 
00194         /* Output data. */
00195         printf("Tilt Detected!\r\n");
00196       }    
00197     }
00198     
00199     if(step_count_reset_request) {
00200       step_count_reset_request = 0;
00201       acc_gyro->reset_step_counter();
00202     }
00203     
00204     
00205      if (mems_event1) 
00206     {   
00207        mems_event1 = 0;
00208       LSM6DSL_Event_Status_t status;   
00209     
00210         acc_gyro->get_event_status(&status);
00211         if (status.FreeFallStatus) {
00212         /* Led blinking. */
00213         myled = 1;
00214         wait(0.2);
00215         myled = 0;
00216 
00217         /* Output data. */
00218         printf("Free Fall Detected!\r\n");
00219       }
00220       
00221       
00222     magnetometer->get_m_axes(axes);
00223     printf("LSM303AGR [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00224   /*  //Algo 1 - Please fill the values. 
00225     if((axes[0] > fill_the_value) &&  (axes[0] < fill_the_value) &&
00226        (axes[1] > fill_the_value) &&  (axes[1] < fill_the_value) &&
00227        (axes[2] > fill_the_value) &&  (axes[2] < fill_the_value)) 
00228         {
00229             printf("Board is tilted on the Left side!\r\n");
00230         }  
00231         else 
00232         {
00233             printf("Board is tilted on the Right side\n\r");
00234             
00235         } */
00236         
00237     //Condition for right tilt check            
00238     if (axes[1] <= -350)
00239         printf("Message: Board is tilted on the Left side\n\r");
00240     
00241     //Condition for right tilt check
00242     if (axes[1] >= -180)
00243         printf("Message: Board is tilted on the Right side\n\r");
00244            
00245     wait(0.5); 
00246         
00247     }
00248 
00249     
00250   }
00251 }