DCS_TEAM / Mbed 2 deprecated DCS_FINAL_CODE

Dependencies:   Chemical_Sensor_DMA GPRS DPG_FINAL MBed_Adafruit-GPS-Library SDFileSystem Socket mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sensor.cpp Source File

Sensor.cpp

00001 #pragma once
00002 #include "mbed.h"
00003 #include "MBed_Adafruit_GPS.h"
00004 #include "SDFileSystem.h"
00005 #include "GSMLibrary.h"
00006 #include "stdlib.h"
00007 #include "GPRSInterface.h"
00008 #include "Chemical_Sensor_DMA/pause.cpp"
00009 #include "Chemical_Sensor_DMA/Sample/adc.h"
00010 #include "Chemical_Sensor_DMA/Sample/pdb.h"
00011 #include "Chemical_Sensor_DMA/SignalProcessing.h"
00012 
00013 /**************************************************
00014  **          GPS                                 **
00015  **************************************************/
00016 Serial * gps_Serial;
00017 Serial pc(USBTX, USBRX);
00018 
00019 /**************************************************
00020  **          SD FILE SYSTEM                       **
00021  **************************************************/
00022 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
00023 FILE *fpLog;
00024 FILE *fpData;
00025 
00026 /**************************************************
00027  **          SENSOR INPUTS                       **
00028  **************************************************/
00029 /*Global Variables*/
00030 extern GPRSInterface eth;
00031 uint16_t *p1;
00032 float filteredLong,filteredLongRef;
00033 /*End Global Variables*/
00034 
00035 /*int main ()
00036 {   
00037     Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
00038     const int refresh_Time = 1000; //refresh time in ms
00039     
00040     //set to 1 for daylight savings time
00041     int daylightsavings = 1;
00042     
00043     // GPS INITIALIZATION //////////////////////////////
00044     pc.printf("Initialize GPS\r\n");
00045     gps_Serial = new Serial(PTC4,PTC3);
00046     Adafruit_GPS myGPS(gps_Serial);
00047     char c;
00048     myGPS.begin(9600);
00049     myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
00050     myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
00051     myGPS.sendCommand(PGCMD_ANTENNA);
00052     ////////////////////////////////////////////////////
00053     
00054     wait(2.3);
00055     
00056     
00057     pc.printf("Compute Tables\r\n");
00058     pre_compute_tables();
00059     
00060     pc.printf("Initialize\r\n");
00061     initialize();
00062     
00063     
00064     int startAddress = (int)&sample_array0[0];
00065     int destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;
00066     int currentIndex = 0;
00067     
00068     pc.printf("hello :)\r\n");
00069     float filteredLong = 0.0;
00070     float filteredLongRef = 0.0;
00071 
00072     refresh_Timer.start();  //starts the clock on the timer
00073     
00074     //main while loop for data processing
00075     pc.printf("Begin Processing Data...\r\n");
00076     while(1) {
00077         //get GPS coordinates
00078         c = myGPS.read();
00079         if ( myGPS.newNMEAreceived() ) {
00080             if ( !myGPS.parse(myGPS.lastNMEA()) ) {
00081                 //continue;
00082             }
00083         }
00084         //eth.setCellTime();  
00085         //filter the data stored in the DAC
00086         do
00087         {
00088             destinationIndex = getDestinationIndex();
00089         }while (currentIndex == destinationIndex);
00090         
00091         while (currentIndex!=destinationIndex)
00092         {
00093             filter100K(sample_array0[currentIndex], sample_array1[currentIndex]);
00094             currentIndex++;
00095             if (currentIndex>=2000){
00096                 currentIndex = 0;
00097             }
00098             
00099         }
00100         
00101         //send data every second
00102         if (refresh_Timer.read_ms() >= refresh_Time) {
00103             refresh_Timer.reset();
00104             
00105             //pc.printf("\r\nCell Time: %s\r\n", eth.getCellTime());
00106             //gather all the data to be written
00107             filteredLong = getFiltered();
00108             filteredLongRef = getFilteredRef();
00109             //time
00110             int hours = myGPS.hour - (6 + daylightsavings);
00111             int minutes = myGPS.minute;
00112 
00113             //gps
00114             float latitude = myGPS.latitude;
00115             char lat = myGPS.lat;
00116             float longitude = myGPS.longitude;
00117             char lon  = myGPS.lon;
00118             
00119             
00120             
00121             //log data locally to sd card
00122             fpData = fopen("/sd/data.txt", "a");
00123             if (fpData != NULL) {
00124                 fprintf(fpData, "%f,", filteredLong);
00125                 fprintf(fpData, "%f,", filteredLongRef);
00126                 fprintf(fpData, "%f,", filteredLongRef ? (filteredLong/filteredLongRef) : 0);
00127                 fprintf(fpData, "%02d:%02d:%02d,", hours, minutes, myGPS.seconds);
00128                 if (myGPS.fix){ 
00129                     fprintf(fpData, "%5.2f%c,%5.2f%c\r\n", latitude, lat, longitude, lon);
00130                 }
00131                 else{
00132                     fprintf(fpData, "%5.2f%c,%5.2f%c\r\n", 0.00, 'N', 0.00, 'E');   
00133                 }
00134                 fclose(fpData);
00135             }
00136             //send data to google spreadsheet
00137             if(myGPS.fix){
00138                 gsm_send_data(filteredLong, filteredLongRef, hours, minutes, myGPS.seconds, latitude,lat, longitude,lon);
00139             }else{
00140                 gsm_send_data(filteredLong, filteredLongRef,hours, minutes, myGPS.seconds, 0, 0, 0, 0);
00141             }
00142             //tick the state machine
00143             gsm_send_data(filteredLong, filteredLongRef,0, 0, 0, 0, 0, 0, 0);
00144             gsm_tick();
00145         }
00146         
00147         
00148     }
00149 }*/
00150 // for debug purposes
00151 DigitalOut led_red(LED_RED);
00152 DigitalOut led_green(LED_GREEN);
00153 DigitalOut led_blue(LED_BLUE);
00154  
00155 Timer t1;
00156 using namespace std;
00157  
00158  
00159 #define PDB_DACINTC0_TOE 0x01 // 0x01 -> PDB DAC interal trigger enabled
00160 #define DAC0_DAT0 (uint16_t *)0x400CC000 // DAC word buffer base address
00161 
00162 void setUpDac()
00163 {
00164     SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK;  // turn on clock to the DAC
00165     SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK;  // turn on clock to the DAC
00166     DAC0_C0 = 0xC0;
00167     //DAC0_C0 |= DAC_C0_DACEN_MASK ;   // enable the DAC; must do before any of the following
00168     DAC0_C2 =9;//cycle through the first 10 values in the buffer
00169     DAC0_C1 |= 0x80;//enable the dac buffer
00170     p1 = DAC0_DAT0;
00171     for (int i = 0; i < 16; i++)//fill the buffer 
00172     {//460, 2870
00173         uint16_t value = (uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 460.0 + 2870.0);
00174         *p1++ = value; // 3351.0
00175         printf("Pointer: %d\tValue: %d\n\r", (uint32_t)p1,value);
00176     }
00177     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT0L,DAC0_DAT0H,DAC0_DAT0L|(DAC0_DAT0H<<8));
00178     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT1L,DAC0_DAT1H,DAC0_DAT1L|(DAC0_DAT1H<<8));
00179     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT2L,DAC0_DAT2H,DAC0_DAT2L|(DAC0_DAT2H<<8));
00180     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT3L,DAC0_DAT3H,DAC0_DAT3L|(DAC0_DAT3H<<8));
00181     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT4L,DAC0_DAT4H,DAC0_DAT4L|(DAC0_DAT4H<<8));
00182     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT5L,DAC0_DAT5H,DAC0_DAT5L|(DAC0_DAT5H<<8));
00183     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT6L,DAC0_DAT6H,DAC0_DAT6L|(DAC0_DAT6H<<8));
00184     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT7L,DAC0_DAT7H,DAC0_DAT7L|(DAC0_DAT7H<<8));
00185     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT8L,DAC0_DAT8H,DAC0_DAT8L|(DAC0_DAT8H<<8));
00186     printf("data low: %d\tdata high: %d\tTotal: %d\n\r",DAC0_DAT9L,DAC0_DAT9H,DAC0_DAT9L|(DAC0_DAT9H<<8));
00187  
00188 }
00189 int startAddress = (int)&sample_array0[0];
00190 int getDestinationIndex()
00191 {
00192     if ((int)(DMA_TCD0_DADDR-startAddress)/2-1<0)
00193         return 1999;
00194     else
00195         return (int)(DMA_TCD0_DADDR-startAddress)/2-1;
00196 }
00197 int main()
00198 {
00199      Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
00200     const int refresh_Time = 1000; //refresh time in ms
00201     
00202     //set to 1 for daylight savings time
00203     int daylightsavings = 1;
00204     
00205     // GPS INITIALIZATION //////////////////////////////
00206     pc.printf("Initialize GPS\r\n");
00207     gps_Serial = new Serial(PTC4,PTC3);
00208     Adafruit_GPS myGPS(gps_Serial);
00209     char c;
00210     myGPS.begin(9600);
00211     myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
00212     myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
00213     myGPS.sendCommand(PGCMD_ANTENNA);
00214     ////////////////////////////////////////////////////
00215     
00216     led_blue = 1;
00217     led_green = 1;
00218     led_red = 1;
00219     pre_compute_tables();
00220     
00221     pc.printf("Starting...\r\n");    
00222     for(int i = 0; i < 86; i++) 
00223     {
00224         if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
00225     }
00226     
00227     // Give hardware associated with 
00228     // sampling the highest priority
00229     NVIC_SetPriority(ADC1_IRQn,0);
00230     NVIC_SetPriority(ADC0_IRQn,0);
00231     NVIC_SetPriority(PDB0_IRQn,0);
00232     NVIC_SetPriority(DMA0_IRQn,0);
00233     NVIC_SetPriority(DMA1_IRQn,0);
00234     NVIC_SetPriority(DMA2_IRQn,0);
00235     
00236     NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
00237     NVIC_SetPriority(ENET_Transmit_IRQn,1);
00238     NVIC_SetPriority(ENET_Receive_IRQn,1);
00239     NVIC_SetPriority(ENET_Error_IRQn,1);
00240     
00241     adc_init(); // initialize ADCs (always initialize adc before dma)
00242     setUpDac();
00243     dma_init(); // initializes DMAs
00244     pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2 
00245     
00246     // flash green led indicating startup complete
00247     led_red = 1;
00248     led_blue = 1;
00249     led_green = 0;
00250     pause_ms(500);
00251     led_green = 1;
00252     pause_ms(200);
00253     led_green = 0;
00254     pause_ms(500); 
00255     led_green = 1;
00256     pdb_start();
00257     
00258     int destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;
00259     int currentIndex = 0;
00260     
00261     printf("DAC C2: %X\n\r",DAC0_C2);
00262     printf("DAC C0: %X\n\r",DAC0_C0);
00263     printf("DAC C1: %X\n\r",DAC0_C1);
00264     printf("DAC C2: %X\n\r",DAC0_C2);
00265     
00266     pc.printf("hello :)\r\n");
00267 
00268 
00269     refresh_Timer.start();  //starts the clock on the timer
00270     
00271     //main while loop for data processing
00272     pc.printf("Begin Processing Data...\r\n");
00273     while (1)
00274     {
00275         //filter the data stored in the DAC
00276         do
00277         {
00278             destinationIndex = getDestinationIndex();
00279             if((int)(DMA_TCD0_DADDR-startAddress)/2 ==currentIndex-1)
00280                 pc.printf("Buffer overrun\n\r");
00281         }while (currentIndex == destinationIndex);
00282         
00283         while (currentIndex!=destinationIndex)
00284         {
00285             filter100K(sample_array0[currentIndex], sample_array1[currentIndex]);
00286             
00287             //send data every second
00288             if (refresh_Timer.read_ms() >= refresh_Time) {
00289                 refresh_Timer.reset();
00290                 
00291                 //get GPS coordinates
00292                 c = myGPS.read();
00293                 if ( myGPS.newNMEAreceived() ) {
00294                     if ( !myGPS.parse(myGPS.lastNMEA()) ) {
00295                         //continue;
00296                     }
00297                 }
00298                 //pc.printf("\r\nCell Time: %s\r\n", eth.getCellTime());
00299                 
00300                 
00301                 //time
00302                 int hours = myGPS.hour - (6 + daylightsavings);
00303                 int minutes = myGPS.minute;
00304     
00305                 //gps
00306                 float latitude = myGPS.latitude;
00307                 char lat = myGPS.lat;
00308                 float longitude = myGPS.longitude;
00309                 char lon  = myGPS.lon;
00310                 
00311                 //log data locally to sd card
00312                 fpData = fopen("/sd/data.txt", "a");
00313                 if (fpData != NULL) {
00314                     fprintf(fpData, "%f,", filteredLong);
00315                     fprintf(fpData, "%f,", filteredLongRef);
00316                     fprintf(fpData, "%f,", filteredLongRef ? (filteredLong/filteredLongRef) : 0);
00317                     fprintf(fpData, "%02d:%02d:%02d,", hours, minutes, myGPS.seconds);
00318                     if (myGPS.fix){ 
00319                         fprintf(fpData, "%5.2f%c,%5.2f%c\r\n", latitude, lat, longitude, lon);
00320                     }
00321                     else{
00322                         fprintf(fpData, "%5.2f%c,%5.2f%c\r\n", 0.00, 'N', 0.00, 'E');   
00323                     }
00324                     fclose(fpData);
00325                 }
00326                 //send data to google spreadsheet
00327                 if(myGPS.fix){
00328                     gsm_send_data(filteredLong, filteredLongRef, hours, minutes, myGPS.seconds, latitude,lat, longitude,lon);
00329                 }else{
00330                     gsm_send_data(filteredLong, filteredLongRef,hours, minutes, myGPS.seconds, 0, 0, 0, 0);
00331                 }
00332                 //send data once a second
00333                 gsm_tick();
00334                 printf("V1: %f\tV2: %f\tRatio: %f(Mine)\n\r",filteredLong,filteredLongRef,filteredLong/filteredLongRef);
00335             
00336             }
00337             currentIndex++;
00338             if (currentIndex>=2000){
00339                 currentIndex = 0;
00340             }
00341             
00342         }
00343         
00344     }
00345           
00346 }
00347 void setFiltered(float ref){
00348     filteredLong = ref;
00349 }
00350 void setFilteredRef(float ref){
00351    filteredLongRef = ref;
00352 }