Tasos Ch / Mbed OS Fall_Detect

Dependencies:   LSM303DLHC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LSM303DLHC.h"
00003 
00004 
00005 DigitalOut led1(LED1);
00006 DigitalOut led2 (LED2);
00007 
00008 
00009 float aax, aay, aaz, mmx, mmy, mmz, aaz_old;
00010 bool retval;
00011 EventQueue queue;
00012 InterruptIn free_fall(P5_1); // Free Fall Interupt asserted on pin 5_1
00013 
00014 LSM303DLHC sensor(P3_4, P3_5);  //Initialise LMS303
00015 
00016 /** Function to calculate Impact Acceleration after a Free Fall Interrupt has been Asserted **/
00017 void ff_proc()
00018 {
00019 double atotal;
00020 double TH=4;
00021 printf("Free Fall Interupt Asserted ..... \n");
00022 retval=sensor.read(&aax, &aay, &aaz, &mmx, &mmy, &mmz);
00023 atotal=sqrt(pow(aax,2)+pow(aay,2)+pow(aaz,2));
00024 printf("Total Acceleration  %f\n", atotal);
00025 if (atotal<TH) //Fall NOT verified
00026 {
00027     led2=0;
00028     led1=1;
00029     }
00030     
00031 }
00032 
00033 /** Free Fall Interrupt Service **/
00034 void ff()
00035 {
00036 // Potential Fall detected    
00037     led2=1;
00038     led1=0;
00039     
00040     queue.call(&ff_proc); //Calculate Impact Acceleration and any other thresholds to verify Free Fall Event
00041 }    
00042 
00043 
00044 
00045 //*******************************************************************************//
00046 
00047 // main() 
00048     int main() {
00049 
00050 int flag=1;
00051 Thread eventThread;
00052 
00053    led1=1;  // LED1 OFF
00054    led2=1;  // LED2 OFF
00055    
00056  eventThread.start(callback(&queue, &EventQueue::dispatch_forever));  
00057     
00058     led2=0;
00059         printf("Initialising LSM303DLHC ..... \n");
00060         
00061         retval=sensor.read(&aax, &aay, &aaz, &mmx, &mmy, &mmz);
00062         if (retval==1)
00063         {
00064             printf("Success: LSM303DLHC Connected, Accel Values:  %3.3f %3.3f %3.3f\n", aax, aay, aaz);
00065                         
00066          }
00067          else {
00068            printf("Failure : LSM303DLHC NOT RESPONDING................... \n");
00069            flag=0;
00070            }     
00071         
00072 if (flag) {
00073              
00074 free_fall.fall(&ff);              
00075 
00076 while(1)
00077 {    
00078 retval=sensor.read(&aax, &aay, &aaz, &mmx, &mmy, &mmz);
00079 printf("Accel Values:  %3.3f %3.3f %3.3f\n", aax, aay, aaz);   
00080 wait(5);
00081 }
00082 
00083 }
00084 }