IGOR / Mbed 2 deprecated PONY_Ph0-uAXIS

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Files at this revision

API Documentation at this revision

Comitter:
sgmcb
Date:
Tue Dec 29 08:48:44 2015 +0000
Parent:
46:ab4cccab10c9
Child:
48:3512b3c7a5ae
Commit message:
Pushing systime to M2X

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Dec 29 08:09:21 2015 +0000
+++ b/main.cpp	Tue Dec 29 08:48:44 2015 +0000
@@ -17,7 +17,7 @@
 //----
 // DEBUG DEFINITIONS
 #define THROWAWAY
-#define MDMDEBUG
+//#define MDMDEBUG
 #define LOCDEBUG
 
 //------------------------------------------------------------------------------------
@@ -70,6 +70,10 @@
 char statusBuf[145] = "";
 
 // Location reading
+unsigned int kLocLoopDelayMin = 30 * 1000;         // The minimum loop waiting time for location reads
+unsigned int kLocLoopDelay = kLocLoopDelayMin;     // The default loop waiting time for location reads
+
+
 unsigned int kReadingDelay = 3 * 60 * 1000;         // How many seconds to wait between reads
 unsigned int kReadingDelayClimb = 15000;    // How many seconds to add to the wait period when sitting idle
 unsigned int kReadingDelayMax = 5 * 60000;  // What's the maximum time between readings?
@@ -77,10 +81,12 @@
 
 // System time
 time_t kSysSeconds = time(NULL);
+time_t thisTime = time(NULL);
 bool kSysClockSet = false;
 tm kFormatTime;
 
 
+
 // Global function definitions
 
 void flip() {
@@ -159,9 +165,9 @@
     int locAccUpper = 5000; // Don't accept locations with uncertainty greater than this
     
     // Filter counters
-    int fail1count = -1;
-    int fail2count = -1;
-    int fail3count = -1;
+    int fail1count = 0;
+    int fail2count = 0;
+    int fail3count = 0;
 
 
     // Cell location call variables
@@ -183,20 +189,19 @@
         
         // Request to fill cell-location buffer
         cellLocDelay.start();
+        #ifdef LOCDEBUG
+            printf("Initiate cellLocRequest call...\r\n");
+        #endif
         mdm.cellLocRequest(sensorMask, cellLocReqTimeout, targetAccuracy);
 
-    // Hold next functions until we give cellLocRequest time to execute.
+        
+        // Hold next functions until we give cellLocRequest time to execute.
         while((cellLocDelay.read() < cellLocReqTimeout)  &&  (!mdm.cellLocGet(&thisLoc)) ) {
             // Nothin much
         };
-        
-        // wait 5 to make sure that our URCs are ready?
-        delay(5000);
-        
-        //printf("cellLocDelay=%f\r\n",cellLocDelay.read());
-        
+      
         // Stop and reset cellLocDelay timer
-        cellLocDelay.start();cellLocDelay.reset();
+        cellLocDelay.stop();cellLocDelay.reset();
         
         // We've either got SOME location data or we've timed out. There are three filters:
         // 1. Does it exist?
@@ -209,13 +214,28 @@
         */
         
         // 1. Does the location data exist?
-        if( (mdm.cellLocGet(&thisLoc)) ) {
+        if( thisLoc.validData ) {//if( (mdm.cellLocGet(&thisLoc)) ) {
             fail1count = 0;
             #ifdef LOCDEBUG
                 printf("LOC uncertainty= %d, sensor= %d\r\n", thisLoc.uncertainty, thisLoc.sensorUsed );
                 printf("LOC = latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude);
             #endif
             
+            
+            (thisLoc.time).tm_year -= 1900;
+            kSysSeconds = mktime( &(thisLoc.time) );
+            
+            // Set the system clock, if it's not already
+            if(!kSysClockSet) {
+                set_time(kSysSeconds);
+                printf("\033[31mSet system clock time.\033[39m\r\n");
+                kSysClockSet=true;
+            }            
+            
+            printf("thisLoc read at: %s\r", ctime(&kSysSeconds) );                 
+            
+
+            
             // 2. Is this new position data, or extrapolated from an old reading?
             if(thisLoc.sensorUsed == 2) {
                 fail2count = 0;
@@ -231,10 +251,10 @@
                     #endif                    
                     
                     
-                    // REPORT TO M2X
+                    // PASSSES ALL 3 TESTS => REPORT TO M2X
                     
                     // Report position
-                    printf("\r\nTRANSMIT LOCATION DATA:\r\nla=%0.5f, lo=%0.5f, alt=%d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude);
+                    printf("\r\n\033[34mTRANSMIT LOCATION DATA:\r\nla=%0.5f, lo=%0.5f, alt=%d\033[39m\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude);
                     M2X_response = m2xClient.updateLocation(DEVID, "pony-spot", (double) thisLoc.latitude, (double) thisLoc.longitude, (double) thisLoc.altitude);
                     if(M2X_response == 202)
                         printf("Location POST successful\r\n");
@@ -242,7 +262,15 @@
                     // Report uncertainty
                     M2X_response = m2xClient.updateStreamValue(DEVID, "locacc", thisLoc.uncertainty);
                     if(M2X_response == 202)
-                        printf("Location Confidence (%d) POST successful\r\n", thisLoc.uncertainty);                    
+                        printf("Location Confidence (%d) POST successful\r\n", thisLoc.uncertainty);
+                        
+                    // Report system time
+                    thisTime = time(NULL);
+                    M2X_response = m2xClient.updateStreamValue(DEVID, "systime", ctime(&thisTime) );
+                    if(M2X_response == 202)
+                        printf("Systime (%s) POST successful\r\n", ctime(&thisTime) );
+                        
+                            
                 }
                 // Fails 3:
                 else {
@@ -256,22 +284,50 @@
             else {
                 ++fail2count;
                 #ifdef LOCDEBUG
-                    printf("LOC has is extrapolated information; disregard.\r\n");
+                    printf("LOC is extrapolated information; disregard.\r\n");
                 #endif
                 
             }            
         }
         // Fails 1 but inside cellLocReq timeout length; repeat without delay:
+        /* NOT USING THIS STRATEGY
         else if ( cellLocDelay < cellLocReqTimeout ){
             printf("Waiting on cellLoc timeout...\r\n");
         }
+        */
         // Fails 1 and timer is up:
         else {
             ++fail1count;
             printf("CellLocRequest timeout; no location data available.\r\n");    
-            delay(30*1000);
+        }
+        
+        // DETERMINE LOOP DELAY
+        
+        
+        
+        // TODO: Add conditionals to handle successful location and unsuccessful communication with M2X
         
+        // Successful read
+        if( (!fail3count) ) {
+            kLocLoopDelay = (60 * 1000);    // Set ### minute wait before next read
+            printf("Successful LOC read; ");
         }
+        // Some other behavior
+        else if (false) {
+            
+        }
+        else {
+            kLocLoopDelay = kLocLoopDelayMin;
+            printf("Failed LOC loop; ");
+        }
+        
+        
+        // Set loop delay (i.e. period)
+        printf("loop waiting for %i seconds...\r\n---\n\r\n", (kLocLoopDelay / 1000) );
+        delay(kLocLoopDelay);
+        
+        
+        
         
         
         continue;