PNI / Mbed 2 deprecated VSG_DataLogger1_VariableSpeed

Dependencies:   SDFileSystemVSG mbed

Fork of VSG_DataLogger1 by PNI

Revision:
7:f655a9ef12e5
Parent:
6:bd675690d831
Child:
8:9304b2c411e4
--- a/main.cpp	Wed Aug 15 00:22:05 2018 +0000
+++ b/main.cpp	Thu Aug 16 17:30:43 2018 +0000
@@ -7,10 +7,12 @@
 #define SAMPLE_RATE_HIGH (300)    //Hz
 #define TMRC_VLOW       (0x99)    // TMRC hex value corresponding to reading/rate
 #define TMRC_VHIGH      (0x93)    // TMRC hex value corresponding to reading/rate
-#define SAMPLE_PERIOD   (60)      // seconds
+#define SAMPLE_PERIOD   (30)      // seconds
 #define SAMPLE_DELAY    (5)      // seconds -to allow time to seal unit and place in the field
 #define CYCLE_COUNT     (50)
 #define PRINT_ALL       (0)     //define whether to print all data at the end or line by line during
+#define N_REPEAT_LOW    (1)     //number of times to repeat sample the low speed
+#define N_REPEAT_HIGH   (3)     //number of times to repeat sample the high speed
 //------------------------------------------------
 
 
@@ -59,9 +61,6 @@
 Timer       t;
 
 char takeAsample_F;
-int islow = 1;
-int nhigh = 0;
-float tprev = 0;
 
 void sampletimer() {
     takeAsample_F = 1;
@@ -94,11 +93,18 @@
     int response;
     unsigned int count = 0;
     char SDOK;
-    char disp_buffer[136];
+    char disp_buffer[34*6];
     char big_disp_buffer[10000];
     char disp_char[34];
     int dbuf_size = 0;
+    int newslow = 0;
     int X,Y,Z;
+    int islow = 0;
+    int nhigh = 0;
+    int slow_data_ok = 0;
+    int nextdatahs = 0;
+    float tprev = 0;
+    float tnow = 0;
     mag.lock();
 //    magTimer.attach(&sampletimer,SAMPLE_INTERVAL);
     
@@ -196,83 +202,141 @@
         LED = 1; // LED to show logging is in wait phase
         wait(SAMPLE_DELAY); 
     }
+    
+    sel1a = 0;
+    response = mag.write(TMRC_REG);     // Mag continuous measurement Register
+    response = mag.write(TMRC_VLOW);     // start continuous measurement - all Axes
+    sel1a = 1;
 
     sel1a = 0;
-    response = mag.write(CMM_REG);     // Mag single measurement Register
-    response = mag.write(ALL_AXES+START_BIT);     // start single measurement - all Axes
+    response = mag.write(CMM_REG);     // Mag continuous measurement Register
+    response = mag.write(ALL_AXES+START_BIT);     // start continuous measurement - all Axes
     sel1a = 1;
     //Now go read the STM internal channel selected above
-    HAL_ADC_ConfigChannel(&hadc1, &sConfig);
-    HAL_ADC_Start(&hadc1); // Start conversion
+    //HAL_ADC_ConfigChannel(&hadc1, &sConfig);
+    //HAL_ADC_Start(&hadc1); // Start conversion
     
 
-    while(count < MAX_COUNT) {
-        if (islow > 0) {
-            sel1a = 0;
-            response = mag.write(TMRC_REG);     // Mag continuous measurement Register
-            response = mag.write(TMRC_VLOW);     // start continuous measurement - all Axes
-            sel1a = 1;
-            islow = 0;
-            nhigh = 0;
-        }
-        else {
-            if (nhigh <  1) {
+    while (count < MAX_COUNT) {
+        
+        if (islow < N_REPEAT_LOW) {
+            /*if (islow < 1) {
+                sel1a = 0;
+                response = mag.write(CMM_REG);     // Mag continuous measurement Register
+                response = mag.write(0);     // start continuous measurement - all Axes
+                sel1a = 1;
+                sel1a = 0;
+                response = mag.write(TMRC_REG);     // Mag continuous measurement Register
+                response = mag.write(TMRC_VLOW);     // start continuous measurement - all Axes
+                sel1a = 1;
+                sel1a = 0;
+                response = mag.write(CMM_REG);     // Mag continuous measurement Register
+                response = mag.write(ALL_AXES+START_BIT);     // start continuous measurement - all Axes
+                sel1a = 1;
+            }*/
+            islow++;
+            if (islow == N_REPEAT_LOW) nhigh = 0;
+            nextdatahs = 0;
+            //printf("low");
+        } else {
+            if (nhigh < 1) {
+                wait_us(1100);
+                sel1a = 0;
+                response = mag.write(CMM_REG);     // Mag continuous measurement Register
+                response = mag.write(0);     // start continuous measurement - all Axes
+                sel1a = 1;
                 sel1a = 0;
                 response = mag.write(TMRC_REG);     // Mag continuous measurement Register
                 response = mag.write(TMRC_VHIGH);     // start continuous measurement - all Axes
                 sel1a = 1;
+                sel1a = 0;
+                response = mag.write(CMM_REG);     // Mag continuous measurement Register
+                response = mag.write(ALL_AXES+START_BIT);     // start continuous measurement - all Axes
+                sel1a = 1;
             }
             nhigh++;
-            if (nhigh > 2) islow = 1;
+            if (nhigh == N_REPEAT_HIGH) islow = 0;
+            nextdatahs = 1;
+            //printf("high");            
         }
-            
-        while (!(rdy1a)){
-            wait_us(10); // required
-            // spin here until all Data Ready signals go high
+        
+        tprev = t.read(); //time at start of loop
+        while (!(slow_data_ok)) {
+            while (!(rdy1a)) {
+                wait_us(10); // required
+                // spin here until all Data Ready signals go high
+            }
+            tnow = t.read();            
+            if ((nextdatahs) || ((tnow - tprev) > (0.5*SAMPLE_INTERVAL))) {
+                slow_data_ok = 1; // check if in hs mode or enough time has passed
+            } else { //remove unused data
+                sel1a = 0;
+                response = mag.write(MX_REG); 
+                response = mag.write(0);
+                sel1a = 1;
+            }
         }
-        /*while(!takeAsample_F){  
+        slow_data_ok = 0; // reset for next data point 
+        /*while(!takeAsample_F){
              wait_us(10); // required
         }*/
         takeAsample_F = 0;
-        
+
         // Wait end of conversion and get value
         //if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
-        uint16_t value = HAL_ADC_GetValue(&hadc1); //}
+        //uint16_t value = HAL_ADC_GetValue(&hadc1); //}
+        uint16_t value = 0;
         //Now go read the STM internal channel selected above
-        HAL_ADC_Start(&hadc1); // Start conversion
+        //HAL_ADC_Start(&hadc1); // Start conversion
         
         sel1a = 0;
-            response = mag.write(MX_REG);     // Read back mag data
-            response = mag.write(0); 
-            X = response << 16;
-            response = mag.write(0); 
-            X += (response << 8);
-            response = mag.write(0); 
-            X += response;
-            response = mag.write(0); 
-            Y = response << 16;
-            response = mag.write(0); 
-            Y += (response << 8);
-            response = mag.write(0); 
-            Y += response;
-            response = mag.write(0); 
-            Z = response << 16;
-            response = mag.write(0); 
-            Z += (response << 8);
-            response = mag.write(0); 
-            Z += response;
+        response = mag.write(MX_REG);     // Read back mag data
+        response = mag.write(0);
+        X = response << 16;
+        response = mag.write(0);
+        X += (response << 8);
+        response = mag.write(0);
+        X += response;
+        response = mag.write(0);
+        Y = response << 16;
+        response = mag.write(0);
+        Y += (response << 8);
+        response = mag.write(0);
+        Y += response;
+        response = mag.write(0);
+        Z = response << 16;
+        response = mag.write(0);
+        Z += (response << 8);
+        response = mag.write(0);
+        Z += response;
         sel1a = 1;
+        
+        newslow = 0;
+        if (islow < 1) {
+            sel1a = 0;
+            response = mag.write(CMM_REG);     // Mag continuous measurement Register
+            response = mag.write(0);     // start continuous measurement - all Axes
+            sel1a = 1;
+            sel1a = 0;
+            response = mag.write(TMRC_REG);     // Mag continuous measurement Register
+            response = mag.write(TMRC_VLOW);     // start continuous measurement - all Axes
+            sel1a = 1;
+            sel1a = 0;
+            response = mag.write(CMM_REG);     // Mag continuous measurement Register
+            response = mag.write(ALL_AXES+START_BIT);     // start continuous measurement - all Axes
+            sel1a = 1;
+            newslow = 1;
+        }
 
         // send/save data after receiving it
         if (X > (0x07FFFFF)) X -= 0x1000000;
         if (Y > (0x07FFFFF)) Y -= 0x1000000;
         if (Z > (0x07FFFFF)) Z -= 0x1000000;
-        
-        
+
+
         if (SDOK) {
             sprintf(disp_char,"%d,%0.4f,%i, %d,%d,%d\r\n",count,t.read(),value,X,Y,Z);
-        } 
-        else {
+        } else {
             sprintf(disp_char,"%d,%0.4f,%i, %d,%d,%d\r\n",count,t.read(),value,X,Y,Z);
         }
         
@@ -284,12 +348,12 @@
                 //printf("%i ",count);
             }
         } else {
-            if (nhigh == 0) {// reset at each slow interval
+            if ((islow == 1) && (nextdatahs == 0)) {// reset at each slow interval
                 strcpy(disp_buffer,disp_char);
             } else {
                 strcat(disp_buffer,disp_char);
             }
-            if (nhigh > 2) { //print at end of fast intervals
+            if (islow == 0) { //print at end of fast intervals
                 if (debugPin) { //SDOK
                     printf("%s",disp_buffer);
                 } else {
@@ -298,12 +362,12 @@
                 }
             }
         }
-        
+
         //printf("%s",disp_char);
         count++;
         LED = !LED;
-        
-    } // end of while(1)   
+
+    } // end of while(1)
     
     if (PRINT_ALL) {
         if (debugPin) { //SDOK