init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Wed Dec 19 09:02:20 2018 +0000
Parent:
19:22261767c87a
Child:
21:e0b866630c27
Commit message:
10

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
modem.cpp Show annotated file Show diff for this revision Revisions of this file
modem.h Show annotated file Show diff for this revision Revisions of this file
--- a/README.md	Tue Dec 18 21:41:39 2018 +0000
+++ b/README.md	Wed Dec 19 09:02:20 2018 +0000
@@ -2,9 +2,10 @@
 
 1) Add watchdog kicks to modem functions
 2) USSDmessage index needs to work
-
-
-
+3) clean up timeouts, do we hard code them or send as vars particlulary connectivity ones
+Should we shut down network registration while getting gps fix?
+4) check LNA power on GPS
+5) improve gps fix quality logic
 
 
 
--- a/main.cpp	Tue Dec 18 21:41:39 2018 +0000
+++ b/main.cpp	Wed Dec 19 09:02:20 2018 +0000
@@ -8,6 +8,7 @@
 static void selftest(void);
 static void buttonPress(void);
 static void buttonRelease(void);
+static void saveEventTimes(void);
 
 //------------------------------------------------------------------------------
 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
@@ -15,7 +16,7 @@
 char* GLOBAL_defaultApi = "b:gps2";
 bool GLOBAL_accel_healthy = false;
 bool GLOBAL_requireSoftReset = false;
-bool GLOBAL_motionFlagTriggered = false;
+bool GLOBAL_motionStopFlagTriggered = false;
 bool GLOBAL_debugLED = false;
 bool GLOBAL_needToConfigureLis3dh = false;
 bool GLOBAL_registeredOnNetwork = false;
@@ -61,6 +62,7 @@
 static time_t           RET_RTCunixtime                                 __attribute__((section("noinit"),zero_init));
 //MOTION STATE
 static bool             RET_motionTriggered                             __attribute__((section("noinit"),zero_init));
+static bool             RET_motionTriggeredInTXInterval                 __attribute__((section("noinit"),zero_init));
 static time_t           RET_motionStartTime                             __attribute__((section("noinit"),zero_init));
 static time_t           RET_motionStopTime                              __attribute__((section("noinit"),zero_init));
 static bool             RET_motionPendingOnState                        __attribute__((section("noinit"),zero_init));
@@ -95,7 +97,8 @@
 //PERIPHERALS
 //------------------------------------------------------------------------------ 
 //BLE myble;
-extern WatchdogTimer watchdog(240.0); //Do not set to less than 4500ms or can cause issues with softdevice
+WatchdogTimer watchdog(300.0); //Do not set to less than 4500ms or can cause issues with softdevice
+void watchdogKick() {watchdog.kick();}
 //SERIAL DEBUG?
 #if CONSOLE_DEBUG
     Serial uart(PN_UART_TX, PN_UART_RX, 115200);
@@ -125,7 +128,7 @@
 }
 void gotoSleep(long sleep_milliseconds) {
     turnOffEverything();
-    watchdog.kick();
+    watchdogKick();
     if (GLOBAL_requireSoftReset) { //dont need to clear this var as reset changes it back to false
         system_reset();
     }
@@ -233,6 +236,7 @@
     RET_buttonHoldTime = 0;
     //MOTION STATE
     RET_motionTriggered = 0;
+    RET_motionTriggeredInTXInterval = 0;
     RET_motionStartTime = 0;
     RET_motionStopTime = 0;
     RET_motionPendingOnState = 0;
@@ -297,6 +301,7 @@
         time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime);
         if (inMotionForSeconds >= RET_setting_motion_start_seconds) {
             RET_motionState = true;
+            RET_motionTriggeredInTXInterval = true;
             if (GLOBAL_debugLED) LED1blink(10,100);
         }
     }
@@ -304,6 +309,7 @@
         time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime);
         if (noMotionForSeconds >= RET_setting_motion_stop_seconds) {
             RET_motionState = false;
+            GLOBAL_motionStopFlagTriggered = true;
             RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); 
             if (GLOBAL_debugLED) LED1blink(5,500);
         }
@@ -349,25 +355,7 @@
         
         if (critical_fail_count == 0) { 
             DEBUG("GOT SETTINGS OK\n"); 
-            
-            //SET EVENT TIMES
-            if(RET_setting_location_tx_interval_mins > 0) { 
-                RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
-                DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);
-            }
-            if(RET_setting_location_tx_failsafe_hrs > 0) { 
-                RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
-                DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);
-            }
-            if(RET_setting_activity_tx_interval_mins > 0) { 
-                RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_mins * 60));
-                DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);
-            }
-            if(RET_eventTime_environmental_tx > 0) { 
-                RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
-                DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);
-            }
-            
+            saveEventTimes();
             RET_haveSettings = true;     
             return true;
         } else {
@@ -380,7 +368,25 @@
         return false;
     }
 }
-
+void saveEventTimes() {
+    //SET EVENT TIMES
+    if(RET_setting_location_tx_interval_mins > 0) { 
+        RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
+        DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);
+    }
+    if(RET_setting_location_tx_failsafe_hrs > 0) { 
+        RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
+        DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);
+    }
+    if(RET_setting_activity_tx_interval_mins > 0) { 
+        RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_mins * 60));
+        DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);
+    }
+    if(RET_eventTime_environmental_tx > 0) { 
+        RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
+        DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);
+    }   
+}
 
 //------------------------------------------------------------------------------
 // SETUP
@@ -392,7 +398,7 @@
         RET_imei = modem.getIMEI();
         GLOBAL_imei = RET_imei;
         DEBUG("imei: %lld \n",RET_imei);
-        if (modem.registerOnNetwork(3,60000)) {
+        if (modem.registerOnNetwork(2,180000)) {
             char bytestosend[160];
             snprintf(bytestosend,sizeof(bytestosend),"(%s,im:%lld,v:%.2f,fr:1,z:SETUP,c:1)\0",GLOBAL_defaultApi,GLOBAL_imei,GLOBAL_voltage);
             char result[180];
@@ -421,8 +427,21 @@
 // EVENTS
 //------------------------------------------------------------------------------ 
 void event_location_tx() {
-    getBatteryV();
+    DEBUG("PERFORM LOCATION TX\n");
+    getBatteryV();  
+    
+    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+    if (modem.on()) {
         
+        //RET_setting_location_accuracy
+        modem.getLocation(true, 3, true, RET_setting_location_timeout);
+        
+    }
+    
+    
+    //RESETS
+    RET_motionTriggeredInTXInterval = 0;
+    saveEventTimes();
 }
 //------------------------------------------------------------------------------
 // STATE ENGINE
@@ -453,28 +472,35 @@
             bool run_location_tx = false;
             switch (RET_setting_location_mode) {
                 case 1: //INTERVAL POST
-                    if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
-                    if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
+                    if(RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { 
+                        DEBUG("INTERVAL LOC TX...\n");
+                        run_location_tx = true; 
+                    }
                     break;
-                
                 case 2: //INTERVAL POST WITH MOTION CHECK
-                    
+                    if(RET_motionTriggeredInTXInterval && RET_RTCunixtime > RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
+                        DEBUG("INTERVAL LOC TX WMC...\n"); 
+                        run_location_tx = true; 
+                    }
                     break;
-                    
                 case 3: //POST ON STOP MOTION
-                    
+                    if (GLOBAL_motionStopFlagTriggered) {
+                        DEBUG("MOTION STOP LOC TX...\n");
+                        run_location_tx = true;
+                        GLOBAL_motionStopFlagTriggered = false;
+                    }
                     break;   
             }
+            //Failsafe timer catchall
+            if(RET_RTCunixtime > RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { 
+                run_location_tx = true; 
+            }
             if (run_location_tx) {
                 event_location_tx();
             }
             
             
             
-            
-            
-            //Location TX
-            
             //Environmental Log
             if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
                 /*
@@ -487,14 +513,13 @@
             }
             //Environmental TX
             if(RET_RTCunixtime > RET_eventTime_environmental_tx && RET_eventTime_environmental_tx > 0) {
-                getBatteryV();
+                //getBatteryV();
                 //event_environmental_bc();
                 //Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
             }
             //Activity TX
             if(RET_RTCunixtime > RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
-                getBatteryV();
-                //event_activity_bc();
+                //event_activity_tx();
             }
             break;
         case STATE_DORMANT :
@@ -543,12 +568,8 @@
 // MAIN
 //------------------------------------------------------------------------------ 
 int main() {
-    RTCticker.attach(&RTCtick, 1.0);
     turnOffEverything();
     
-    button.fall(&buttonPress); //does this affect power?
-    button.rise(&buttonRelease);
-
     //CHECK IF THIS IS RESET
     //0x00000004 == soft reset  //0x00000002 == watchdog  //0x00000001 == button/hardreset 
     if (NRF_POWER->RESETREAS != 0xffffffff) {
@@ -567,15 +588,18 @@
         NRF_POWER->RESETREAS = 0xffffffff;
     }
     //CHECK FOR FIRST BOOT
-    if (RET_coldBoot == 1) factoryReset();
+    if (RET_coldBoot == 1) { factoryReset(); }
     
-    //COPY ESSENTIAL VALUES FROM RET TO GLOBAL AFTER RESET
+    //INIT
+    RTCticker.attach(&RTCtick, 1.0);
+    button.fall(&buttonPress); //does this affect power?
+    button.rise(&buttonRelease);
     copyRETtoGLOBAL();
     
     while(true) {
         //SLEEP
         if (RET_coldBoot != 1) gotoSleep(10000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP
-        watchdog.kick();
+        watchdogKick();
         
         //LOG START TIME
         GLOBAL_wakeTime = RET_RTCunixtime;
@@ -584,6 +608,11 @@
         //DEBUG("STATE: %d,  RTC_MILLIS: %lld,  RTC_SECONDS:%lld,  BUTT_T:%d,  BUTT_C,%d \n", RET_state, RET_RTCmillis, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
         DEBUG("STATE: %d,  RTC_SECONDS:%u\n", RET_state, RET_RTCunixtime);
 
+        
+        event_location_tx();
+        while(1) {   
+        }
+
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS
--- a/main.h	Tue Dec 18 21:41:39 2018 +0000
+++ b/main.h	Wed Dec 19 09:02:20 2018 +0000
@@ -90,4 +90,8 @@
 extern float GLOBAL_voltage;
 extern time_t GLOBAL_RTCunixtime;
 extern time_t GLOBAL_wakeTime;
+
+//FUNCS
+extern void watchdogKick();
+
 #endif
\ No newline at end of file
--- a/modem.cpp	Tue Dec 18 21:41:39 2018 +0000
+++ b/modem.cpp	Wed Dec 19 09:02:20 2018 +0000
@@ -103,6 +103,7 @@
     t.start();
     //TRY X NUMBER OF TIMES
     while (attempt <= maxAttempts) {
+        watchdogKick();
         attempt ++;
         t.reset();
         uint32_t startmillis = t.read_ms();
@@ -217,4 +218,80 @@
     }
 }
 
+
+void Modem::getLocation(bool gps, bool cellLocate, uint8_t accuracy, uint16_t timeout_seconds) 
+{ 
+    flushSerial();
+    bool haveGPSFix = false;
+    bool haveCellLocateFix = false;
+    
+    //
+    //ATsendCMD("AT+CREG=2");  
+    //ATwaitForWord("OK",30000);
+
+    
+
+    
+    while(1) {
+        Thread::wait(5000);
+        
+        
+        //ATsendCMD("AT+QENG=?");  
+        //ATwaitForWord("OK",15000);
+        
+        ATsendCMD("AT+QENG=1");  
+        ATwaitForWord("OK",15000);
+        
+        //ATsendCMD("AT+CREG?");
+        ATsendCMD("AT+QENG?");
+        
+        ATwaitForWord("OK",30000);
+    }
+    
+    if (gps) {
+        //TURN ON GPS
+        ATsendCMD("AT+QGPS=1");
+        ATwaitForWord("OK",5000);
+        
+        //Enable External LNA power - IS DISABLED BY DEFAULT
+        ATsendCMD("AT+QGPSCFG=\"lnacontrol\",1");
+        ATwaitForWord("OK",5000);
+        
+        Timer t;
+        t.start();
+        //TRY UNTIL TIMEOUT
+        uint8_t GPS_fixstage = 0;
+        uint8_t GPS_fixcount = 0;
+        uint32_t startmillis = t.read_ms();
+        uint32_t runtime = 0;
+        while(!haveGPSFix && runtime < (timeout_seconds*1000)) {
+            runtime = (t.read_ms() - startmillis);
+            ATsendCMD("AT+QGPSLOC=2");
+            if (ATwaitForWord("+QGPSLOC:",5000)) {
+                GPS_fixstage = 1;
+                GPS_fixcount ++;
+                if (GPS_fixcount >= 2) { //wait 10 seconds to get a better fix // need to improve this logic
+                    haveGPSFix = true;
+                    cellLocate = false; //disable the need for trying cell locate
+                }
+                Thread::wait(5000);
+            }
+            watchdogKick();
+        }
+        //TURN OFF GPS
+        ATsendCMD("AT+QGPSEND");
+        ATwaitForWord("OK",5000);
+    }
+    if (cellLocate) {
+           
+    }
+    
+    
+    
+    DEBUG("END\n");
+    while(1) {
+        
+    }
+}
+
 Modem::~Modem(){};
\ No newline at end of file
--- a/modem.h	Tue Dec 18 21:41:39 2018 +0000
+++ b/modem.h	Wed Dec 19 09:02:20 2018 +0000
@@ -24,6 +24,7 @@
             bool USSDsend(char* message, int maxAttempts);
             char* USSDreceive(int messageIndex);
             char* USSDmessage(char* message, bool needResponse, int maxAttempts, char* api);
+            void getLocation(bool gps, bool cellLocate, uint8_t accuracy, uint16_t timeout_seconds); //accuracy is 1,2,3   low, med, high
             
             //AT
             void flushSerial(void);