Working code for pc app 12/01/2018 commit

Dependencies:   mbed MS5607 mbed-dsp

Fork of Turrentine_Code by Alex Stokoe

Revision:
1:ab3dacbfcde6
Parent:
0:2c6d81be69d8
Child:
2:3d3e21c907e4
--- a/main.cpp	Fri Jan 12 09:22:44 2018 +0000
+++ b/main.cpp	Fri Jan 12 13:17:35 2018 +0000
@@ -2,6 +2,8 @@
 
 #define M_PI 3.14159265358979323846
 
+#define N_SAMPLES 500
+
 //mbed class def
 Serial pc(USBTX, USBRX); // tx, rx
 SPI spi(p5, p6, p7); // mosi, miso, sclk
@@ -15,16 +17,16 @@
 unsigned short calData[6];
 
 char buffer16[3];
-short int serbuffer[2]; 
+short int serbuffer[2];
 
 char tempBuffer[4];
 unsigned int temp = 0;
 char pressBuffer[4];
 unsigned int press = 0;
 
-unsigned int pData[1000];
-float spData[1000];
-unsigned int tData[1000];
+unsigned int pData[N_SAMPLES];
+float spData[N_SAMPLES];
+unsigned int tData[N_SAMPLES];
 
 const char cb1 = 0xA2;
 const char cb2 = 0xA4;
@@ -49,32 +51,37 @@
 }
 
 //calculate temperature
-int calcT(unsigned int Tval, unsigned short consts[6]) {
+int calcT(unsigned int Tval, unsigned short consts[6])
+{
     int dT = Tval - consts[4]*(2<<7);
-    
+
     int T = 2000 + dT*consts[5]/(2<<22);
-    printf("Temp %d C\n\r", T); 
+    printf("Temp: %d C\n", T);
     return T;
-    }
+}
 
 //calculate 1st order temperature compensated pressure
-int calcP(unsigned int Tval, unsigned int Pval, unsigned short consts[6]) {
+int calcP(unsigned int Tval, unsigned int Pval, unsigned short consts[6])
+{
     int dT = Tval - consts[4]*(2<<7);
-    
+
     long long int off = (long long)consts[1]*(2<<16) + ((long long)consts[3] *(long long)dT)/(2<<5);
-    
+
     long long int sens = (long long)consts[0]*(2<<15) + ((long long)consts[2] *(long long)dT)/(2<<6);
-    
+
     int P = (Pval *(sens/(2<<20)) - off)/(2<<14);
-    //printf("Pressure %d Pa\n\r", P); 
+    //printf("Pressure %d Pa\n\r", P);
     return P;
-    }
- 
-int main() {
+}
+
+int main()
+{
+    printf("Turrentine\n");
+        
     pc.baud(115200);
     motorOn = 0;
     motorOn.period_ms(10);
-    
+
     // Chip must be deselected
     cs = 1;
 
@@ -83,18 +90,20 @@
     spi.format(8,3);
     spi.frequency(500000);
     spi.set_default_write_value(0x00);
- 
+
     // Select the device by seting chip select low
     cs = 0;
- 
+
     // Send 0x1E Command to reset the chip
     spi.write(0x1E);
     cs = 1;
     wait_ms(100);
-    cs =1;
+    cs = 1;
     
+    printf("Read cal data\n");
+
     //read cal data values from device into program
-    for (char i=0; i<6; i++){
+    for (char i=0; i<6; i++) {
         //spi read sequence
         cs= 0;
         spi.write(commarr[i], 1, buffer16, 3);
@@ -102,87 +111,103 @@
         wait_ms(10);
         cs=1;
         //Put data into 16bit unsigned int in calData array
-        calData[i] = buffer16[1]<<8 | buffer16[2];        
-        }
-        
+        calData[i] = buffer16[1]<<8 | buffer16[2];
+    }
+    
+    printf("Start loop\n");
+
     //display calibration values to check against datasheet
-    printf("C1: %hu\n\rC2: %hu\n\rC3: %hu\n\rC4: %hu\n\rC5: %hu\n\rC6: %hu\n\r", calData[0], calData[1], calData[2], calData[3], calData[4], calData[5]);
-    
+
     //program loop
-    while(1){
+    while(1) {
         //read temerature value
         cs = 0;
         spi.write(&D2conv4096, 1, buffer16, 1);
         wait_ms(10);
-        cs = 1; cs = 0;
-        
+        cs = 1;
+        cs = 0;
+
         spi.write(&readADC, 1, tempBuffer, 4);
         cs = 1;
-        
+
         //read pressure value
-        cs = 0;
+        /*cs = 0;
         spi.write(&D1conv512, 1, buffer16, 1);
         wait_ms(2);
         cs = 1; cs = 0;
-        
+
         spi.write(&readADC, 1, pressBuffer, 4);
-        cs = 1;
-        
+        cs = 1;*/
+
         //write values from buffers to program variables
         temp = tempBuffer[1]<<16 | tempBuffer[2]<<8 | tempBuffer[3];
-        press = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3];
-        
+        //press = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3];
+
+        printf("Pump On\n");
+
         //turn pump on
         motorOn.write(duty);
-        
+
         //dummy samples to wait for sensor response to stabilise
         int a =0;
-        while(a<10000) {
+        while(a < 10000) {
             cs = 0;
             spi.write(&D1conv256, 1, buffer16, 1);
             wait_us(500);
-            cs = 1; cs = 0;
+            cs = 1;
+            cs = 0;
             spi.write(&readADC, 1, pressBuffer, 4);
             cs = 1;
             a++;
-            }
-            
+        }
         
-        int nsample = 1000;
-        double cumsum = 0;
-        
+        printf("Start Measurement\n");
+
+        //double cumsum = 0;
+
         t.reset();
         t.start();
         //loop values
-        for (int x=0; x <nsample; x++){
-           
+        for (int x = 0; x < N_SAMPLES; x++) {
             cs = 0;
             spi.write(&D1conv256, 1, buffer16, 1);
-            
             wait_us(500);
-            cs = 1; cs = 0;
-            if (x >0){
+            cs = 1;
+
+            cs = 0;
+            if (x > 0) {
                 pData[x-1] = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3];
-                cumsum = cumsum + pData[x-1];
-                }
+                //cumsum = cumsum + pData[x-1];
+            }
             spi.write(&readADC, 1, pressBuffer, 4);
             tData[x] = t.read_us();
             cs = 1;
-        
-            }
-            
+        }
+
+        pData[N_SAMPLES - 1] = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3];
+
         //stop motor and timer
         t.stop();
         motorOn.write(0);
         
+        printf("Stop Measurement\n");
+
         //calc temperature
         calcT(temp, calData);
         long long sum = 0;
-        
-        float avg = calcP(temp, cumsum/(nsample-1), calData);
-        
-        printf("avg %f\n\r", avg); 
-        
+
+        //float avg = calcP(temp, cumsum/(nsample-1), calData);
+
+        //printf("avg %f\n\r", avg);
+
+        //output date to PC
+        printf("C1: %hu\nC2: %hu\nC3: %hu\nC4: %hu\nC5: %hu\nC6: %hu\n", calData[0], calData[1], calData[2], calData[3], calData[4], calData[5]);
+
+        for(int i = 0; i < N_SAMPLES; i++) {
+            printf("%04d: %d\n", i, pData[i]);
+        }
+
+        /*
         //for crossing detection
         bool cflag1 = (avg > pData[0]);
         bool cflag2 = (avg > pData[1]);
@@ -193,62 +218,62 @@
         float freq = 0;
         
         //smooth data array 20 sample size
-        for (int y=0; y< nsample-1; y++){
-            if (9< y && y <nsample-10){
-                sum = 0;
-                for (int z=0; z<20; z++){
-                    sum = sum + pData[y-10+z];
-                    }
-                    
-                spData[y] = (float) calcP(temp, sum/20, calData);
-                
-                //detect avg crossings for frequency calc
-                if (y>10){
-                    cflag2 = (avg > spData[y]);
-                    cflag1 = (avg > spData[y-1]);
-                    
-                    if (cflag1 != cflag2){
-                        if(lastcrosspoint<=(y-10)){
-                            numcross++;
-                            lastcrosspoint = y;
-                            
-                            if (numcross == 1){
-                                crosstart = tData[y];
-                                }
-                                
-                            crossend = tData[y];
-                            }
-                        }   
-                    }      
-                //calc pressure & display list
-                //printf("%d\t%d\t%d\n\r", tData[y], calcP(temp, spData[y], calData), calcP(temp, pData[y], calData));
-                printf("%d\t%f\n\r", tData[y],spData[y]);
-                } else if (10> y || y >nsample-10) {
-                    spData[y] = (float) calcP(temp, pData[y], calData);
-                    }
-            
-            
-            }
-        
+        for (int y=0; y< nsample-1; y++) {
+        if (9< y && y <nsample-10) {
+        sum = 0;
+        for (int z=0; z<20; z++) {
+        sum = sum + pData[y-10+z];
+        }
+
+        spData[y] = (float) calcP(temp, sum/20, calData);
+
+        //detect avg crossings for frequency calc
+        if (y>10) {
+        cflag2 = (avg > spData[y]);
+        cflag1 = (avg > spData[y-1]);
+
+        if (cflag1 != cflag2) {
+        if(lastcrosspoint<=(y-10)) {
+        numcross++;
+        lastcrosspoint = y;
+
+        if (numcross == 1) {
+        crosstart = tData[y];
+        }
+
+        crossend = tData[y];
+        }
+        }
+        }
+        //calc pressure & display list
+        //printf("%d\t%d\t%d\n\r", tData[y], calcP(temp, spData[y], calData), calcP(temp, pData[y], calData));
+        printf("%d\t%f\n\r", tData[y],spData[y]);
+        } else if (10> y || y >nsample-10) {
+        spData[y] = (float) calcP(temp, pData[y], calData);
+        }
+
+
+        }
+
         printf("numcross = %d\n\r", numcross);
-        printf("crosstart = %d\n\r", crosstart);
-        printf("crossend = %d\n\r", crossend);
-            
-        freq = (1e6 *numcross)/(2*(crossend - crosstart));
-        printf("frequency = %f\n\r", freq);
-        
-        for (int z=0; z< nsample-1; z++){
-            
-            spData[z] = spData[z] -avg;
-            }
-        
-        printf("Repeat Blockage Test?\n\rPress Enter to continue\n\r");
-        
-        while(1){             
-            if (pc.getc() != 0) {
-                break;
-                }
-            }
-            
-        }
+                           printf("crosstart = %d\n\r", crosstart);
+                                   printf("crossend = %d\n\r", crossend);
+
+                                           freq = (1e6 *numcross)/(2*(crossend - crosstart));
+                                           printf("frequency = %f\n\r", freq);
+
+                                                   for (int z=0; z< nsample-1; z++) {
+
+                                                   spData[z] = spData[z] -avg;
+                                               }
+
+                                                   printf("Repeat Blockage Test?\n\rPress Enter to continue\n\r");
+
+                                                   while(1) {
+                                                   if (pc.getc() != 0) {
+                                                   break;
+                                               }
+                                               }
+                                                   */
+    }
 }
\ No newline at end of file