Aleksandr Koptevtsov / Mbed 2 deprecated adxl345

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "adxl345.h"
00003 #include <string>
00004 
00005 #define DEBUG       // it doesnt use modem in debug mode. I dont have it and code will stack on my prototype cuz will never receive answers from modem.
00006                     // comment this line to turn off debug mode
00007 
00008 // skywire -------------------
00009 /* --CHANGE THIS FOR YOUR SETUP-- */
00010 #define DeviceID "yourDeviceIDhere"  //Freeboard DweetIO unique ID
00011 /* --CHANGE THIS FOR YORU SETUP (IF APPLICABLE)-- */
00012 string APN = "yourAPNhere";
00013 // ===========================
00014 
00015 PwmOut mypwm(D3);
00016 Ticker timer1;
00017 
00018 ADXL345 adxl;
00019 
00020 int x, y, z;
00021 
00022 volatile char input_buffer[90];             // store everything received 
00023 volatile char message_buffer[90];           // store message
00024 volatile char input_buffer_counter = 0;   
00025 volatile char message_counter = 0;  
00026 volatile char input_flag = false;
00027 
00028 volatile char latitude[15] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
00029 volatile char longtitude[15] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
00030 volatile char coorFlag = 0;
00031 
00032 volatile int period = 1000;         // buzzer variables
00033 volatile int rising = 1;
00034 
00035 
00036 // skywire ------------------------
00037 DigitalOut skywire_en(PA_6);                        // Skywire Enable
00038 DigitalOut skywire_rts(PA_7);                       // Skywire Send
00039 Serial skywire(PA_9,PA_10);                         // Serial comms to Skywire
00040 // Variables for UART comms
00041 volatile int rx_in=0;
00042 volatile int rx_out=0;
00043 const int buffer_size = 600;
00044 char rx_buffer[buffer_size+1];
00045 char rx_line[buffer_size];
00046 // char array for reading from Skywire
00047 char str[255];
00048 // ===============================
00049 
00050 Serial pcSerial(SERIAL_TX, SERIAL_RX, 9600);  // tx, rx     // pc 
00051 Serial gpsSerial(A0, A1, 9600);  // tx, rx                  // gps
00052 
00053 I2C i2cAcc(I2C_SDA, I2C_SCL);
00054 
00055 DigitalOut led(LED1);
00056 
00057 void ADXL_ISR();
00058 void rxHandler();
00059 void data_send();
00060 void alarm_on();
00061 void alarm_off();
00062 
00063 
00064 
00065 //===================================================================
00066 //FUNCTIONS
00067 //===================================================================
00068 /********************* ISR *********************/
00069 /* Look for Interrupts and Triggered Action    */
00070 void ADXL_ISR() {
00071   
00072   // getInterruptSource clears all triggered actions after returning value
00073   // Do not call again until you need to recheck for triggered actions
00074   int interrupts = adxl.getInterruptSource();
00075   
00076   // Free Fall Detection
00077   if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
00078     pcSerial.printf("*** FREE FALL ***\n");
00079     
00080     alarm_on();     // turn of alarm
00081     led = 1;        // turn led on
00082     
00083     #ifndef DEBUG
00084     data_send();        // send data if not in debug mode
00085     #else
00086     wait(10);      // just wait if in debug
00087     #endif
00088     
00089     alarm_off();    // turn alarm off
00090     led = 0;        // turn led off
00091   } 
00092 }
00093 
00094 //===================================================================
00095 // RX interrupt handler
00096 // stores everything into input_buffer
00097 void rxHandler()
00098 {
00099   char tmp;
00100   
00101   do
00102   {
00103     tmp = gpsSerial.getc();       // read serial data
00104   
00105     if(tmp == '$')                    // if message start character( every nmea message starts with $)
00106     {
00107       input_buffer_counter = 0;       // reset inut buffer counter
00108       return;
00109     }
00110   
00111     if(tmp == '*')                    // if end of message( every nmea message ends with *+CRC)
00112     { 
00113       input_buffer[input_buffer_counter] = tmp;
00114       input_buffer_counter++; 
00115       if(input_buffer[3] == 'L')      // if nmea string type is GPGLL
00116       {
00117           int t = 0; 
00118           int lat = 0;
00119           int lon = 0;
00120           
00121           for(int i=0; i<15; i++)           // clear latitude and longtitude
00122           {
00123               latitude[lat++] = ' '; 
00124               longtitude[lon++] = ' ';
00125           }
00126           
00127           while(input_buffer[t] != ',')      // find coma after GPGLL
00128             t++;                            // t points coma after GPGLL
00129             
00130           t++;              // set to to first latitude character
00131           
00132           
00133           lat = 0;
00134           
00135           while(input_buffer[t] != ',')      // copy latitude value
00136           {
00137             latitude[lat] = input_buffer[t];
00138             lat++;
00139             t++;                            // t points coma after latitude 
00140           }    
00141           latitude[lat] = input_buffer[t];          // copy coma
00142           lat++;
00143           t++; 
00144           latitude[lat] = input_buffer[t];          // copy N or S direction
00145           lat++;
00146           t++; 
00147           latitude[lat] = '\0';         // write null character
00148 
00149           t++;              // set t to first character of longtitude
00150           
00151           
00152           lon = 0;
00153           
00154           while(input_buffer[t] != ',')      // copy longtitude value
00155           {
00156             longtitude[lon] = input_buffer[t];
00157             lon++;
00158             t++;                            // t points coma after longtitude 
00159           }    
00160           longtitude[lon] = input_buffer[t];          // copy coma
00161           lon++;
00162           t++; 
00163           longtitude[lon] = input_buffer[t];          // copy W or E direction
00164           lon++;
00165           longtitude[lon] = '\0';         // write null character
00166           
00167           coorFlag = 1;      
00168       }
00169       return;
00170     }
00171     input_buffer[input_buffer_counter] = tmp;
00172     input_buffer_counter++;
00173   }
00174   while(gpsSerial.readable());
00175 }
00176 
00177 //===================================================================
00178 // Read line from the UART
00179 void read_line() 
00180 {
00181     int i;
00182     i = 0;
00183 // Start Critical Section - don't interrupt while changing global buffer variables
00184     __disable_irq();
00185 // Loop reading rx buffer characters until end of line character
00186     while ((i==0) || (rx_line[i-1] != '\n')) {
00187 // Wait if buffer empty
00188         if (rx_in == rx_out) {
00189 // End Critical Section - need to allow rx interrupt to get new characters for buffer
00190             __enable_irq();
00191             while (rx_in == rx_out) {
00192             }
00193 // Start Critical Section - don't interrupt while changing global buffer variables
00194             __disable_irq();
00195         }
00196         rx_line[i] = rx_buffer[rx_out];
00197         i++;
00198         rx_out = (rx_out + 1) % buffer_size;
00199     }
00200 // End Critical Section
00201     __enable_irq();
00202     rx_line[i-1] = 0;
00203     return;
00204 }
00205 
00206 //=======================================================================
00207 // Wait for specific response
00208 int WaitForResponse(char* response, int num) 
00209 {
00210     do {
00211         read_line();
00212         pcSerial.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line);
00213     } while (strncmp(rx_line, response, num));
00214     return 0;
00215 }
00216 
00217 //==========================================================================
00218 // Interrupt for the Skywire
00219 void Skywire_Rx_interrupt()
00220 {
00221 // Loop just in case more than one character is in UART's receive FIFO buffer
00222 // Stop if buffer full
00223     while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
00224         rx_buffer[rx_in] = skywire.getc();
00225         rx_in = (rx_in + 1) % buffer_size;
00226     }
00227     return;
00228 }
00229 
00230 void data_send()
00231 {
00232     skywire.printf("AT+SQNSD=3,0,80,\"dweet.io\"\r\n");
00233     WaitForResponse("CONNECT", 7);
00234     
00235     pcSerial.printf("Sending information...\r\n");
00236     // Report the sensor data to dweet.io
00237     skywire.printf("POST /dweet/for/%s?latitude=%s&longtitude=%s HTTP/1.0\r\n\r\n", DeviceID, latitude, longtitude);
00238 
00239     // Wait for response from dweet.io
00240     WaitForResponse("OK", 2);
00241     wait(1);
00242 }
00243 
00244 void modem_init()
00245 {
00246     //Enable Skywire
00247     skywire_en=0;
00248     wait(2);
00249     skywire_en=1;
00250     wait(2);
00251     skywire_en=0;
00252 
00253     // Wait for modem to initialize
00254     wait(60);
00255     
00256     //Turn off echo
00257     // Helps with checking responses from Skywire
00258     pcSerial.printf("Turning off echo...\r\n");
00259     skywire.printf("ATE0\r\n");
00260     WaitForResponse("OK", 2);
00261 
00262     // Turn on DNS Response Caching
00263     // Used on the Telit-based Skywires
00264     pcSerial.printf("Turning on DNS Cacheing to improve speed...");
00265     skywire.printf("AT#CACHEDNS=1\r\n");
00266     WaitForResponse("OK", 2);
00267     
00268     pcSerial.printf("Connecting to Network...\r\n");
00269     // get IP address
00270     // The last parameter in AT+SQNSCFG sets the timeout if transmit buffer is not full
00271     // Time is in hundreds of ms: so, a value of 5 = 500ms
00272     pcSerial.printf("Configuring context part 1...\r\n");
00273     skywire.printf("AT+SQNSCFG=3,3,300,90,600,5\r\n");
00274     WaitForResponse("OK", 2);
00275     wait(1);
00276     pcSerial.printf("Configuring context part 2...\r\n");
00277     skywire.printf("AT+CGDCONT=3,\"IP\",\"vzwinternet\"\r\n");
00278     WaitForResponse("OK", 2);
00279     wait(1);
00280     pcSerial.printf("Activating context...\r\n");
00281     skywire.printf("AT+CGACT=1,3\r\n");
00282     WaitForResponse("OK", 2);
00283 }
00284 
00285 // interrupt using for encreasing/decreasing tone
00286 void attime()
00287 {
00288     
00289     if(rising == 1)
00290     {
00291         if(period < 1500)
00292             period += 19;
00293         else
00294             rising = 0;
00295     }
00296     else
00297     {
00298         if(period > 1000)
00299             period -= 19;
00300         else
00301             rising = 1;
00302     }
00303     
00304     mypwm.period_us(period);
00305     mypwm.pulsewidth_us(period/2); 
00306 }
00307 
00308 // turn alarm sound on
00309 void alarm_on()     
00310 {
00311     timer1.attach(&attime, 0.01);
00312 }
00313 
00314 // turn alarm sound off
00315 void alarm_off()
00316 {
00317     mypwm.pulsewidth(0);
00318     timer1.detach();
00319 }
00320 
00321 
00322 
00323 // MAIN -=-=-=-=-=-=-=-=-
00324 //++++++++++++++++++++++++++++++++++++++++++++++++++++
00325 int main() {
00326     
00327     skywire.baud(115200);
00328     skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
00329     
00330     i2cAcc.frequency(100000);
00331     
00332     adxl.powerOn();                     // Power on the ADXL345
00333 
00334     adxl.setRangeSetting(8);           // Give the range settings
00335                                       // Accepted values are 2g, 4g, 8g or 16g
00336                                       // Higher Values = Wider Measurement Range
00337                                       // Lower Values = Greater Sensitivity
00338  
00339     // Set values for what is considered FREE FALL (0-255)
00340     adxl.setFreeFallThreshold(9);       // (5 - 9) recommended - 62.5mg per increment
00341     adxl.setFreeFallDuration(15);       // (20 - 70) recommended - 5ms per increment
00342  
00343     // Setting all interupts to take place on INT1 pin
00344     adxl.setImportantInterruptMapping(0, 0, 1, 0, 0);     // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);" 
00345                                                         // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
00346                                                         // This library may have a problem using INT2 pin. Default to INT1 pin.
00347   
00348     // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
00349     adxl.FreeFallINT(1);
00350 
00351     
00352     
00353     pcSerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n");
00354     
00355     gpsSerial.attach(rxHandler);
00356     
00357     #ifndef DEBUG       // if not debug mode
00358     pcSerial.printf("Starting Demo...\r\n");
00359     pcSerial.printf("Waiting for Skywire to Boot...\r\n");
00360 
00361     modem_init();       // init modem
00362     #endif
00363 
00364     
00365     while(1) {
00366         
00367         // Accelerometer Readings
00368         //int x,y,z;   
00369         //adxl.readAccel(&x, &y, &z);         // Read the accelerometer values and store them in variables declared above x,y,z
00370     
00371         // Output Results to Serial
00372         /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */  
00373         //mySerial.printf("X: %i\nY: %i\nZ: %i\n================\n", x, y, z);
00374         //wait(2);
00375       
00376         ADXL_ISR();
00377         // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR(); 
00378         //  and place it within the loop instead.  
00379         // This may come in handy when it doesn't matter when the action occurs. 
00380         
00381         
00382         if(coorFlag)                // show coordinates
00383         {                      
00384             pcSerial.printf("message: %s\n", input_buffer);
00385             pcSerial.printf("latitude: %s\n", latitude);
00386             pcSerial.printf("longtitude: %s\n", longtitude);
00387             
00388             #ifndef DEBUG
00389             data_send();
00390             #endif
00391                 
00392             coorFlag = 0;
00393         }
00394         
00395         
00396         
00397     }
00398 }
00399 
00400 
00401 
00402