![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
main.cpp@0:94b743e06998, 2018-10-27 (annotated)
- Committer:
- pathfindr
- Date:
- Sat Oct 27 23:08:33 2018 +0000
- Revision:
- 0:94b743e06998
- Child:
- 2:fd554f01abdf
init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pathfindr | 0:94b743e06998 | 1 | #include "main.h" |
pathfindr | 0:94b743e06998 | 2 | #include "board.h" |
pathfindr | 0:94b743e06998 | 3 | #include "modes.h" |
pathfindr | 0:94b743e06998 | 4 | |
pathfindr | 0:94b743e06998 | 5 | //LowPowerTicker ticker; //no impact on power consumption |
pathfindr | 0:94b743e06998 | 6 | |
pathfindr | 0:94b743e06998 | 7 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 8 | //Function declarations- Local |
pathfindr | 0:94b743e06998 | 9 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 10 | static void setup(void); |
pathfindr | 0:94b743e06998 | 11 | static void loop(void); |
pathfindr | 0:94b743e06998 | 12 | static void mode_mtu(void); |
pathfindr | 0:94b743e06998 | 13 | static int selftest(void); |
pathfindr | 0:94b743e06998 | 14 | static void LEDon(int milliseconds); |
pathfindr | 0:94b743e06998 | 15 | static void LEDoff(void); |
pathfindr | 0:94b743e06998 | 16 | |
pathfindr | 0:94b743e06998 | 17 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 18 | //Var declarations- Local |
pathfindr | 0:94b743e06998 | 19 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 20 | int mode = 0; |
pathfindr | 0:94b743e06998 | 21 | string exceptions = ""; |
pathfindr | 0:94b743e06998 | 22 | bool accel_healthy = false; |
pathfindr | 0:94b743e06998 | 23 | |
pathfindr | 0:94b743e06998 | 24 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 25 | //Var declarations- RETAINED |
pathfindr | 0:94b743e06998 | 26 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 27 | int RET_mode = 0; |
pathfindr | 0:94b743e06998 | 28 | char RET_pf_identifier[9]; //includes null byte at end |
pathfindr | 0:94b743e06998 | 29 | int RET_interval_setting = 720; |
pathfindr | 0:94b743e06998 | 30 | int RET_interval_hours_failsafe = 24; |
pathfindr | 0:94b743e06998 | 31 | int RET_motioncheck_interval_seconds = 60; |
pathfindr | 0:94b743e06998 | 32 | int RET_gps_timeout = 180; |
pathfindr | 0:94b743e06998 | 33 | int RET_gsm_timeout = 120; |
pathfindr | 0:94b743e06998 | 34 | bool RET_gsmtimedout = false; |
pathfindr | 0:94b743e06998 | 35 | int RET_beacon_scan = 0; |
pathfindr | 0:94b743e06998 | 36 | int RET_accel_awake_thr = 11; |
pathfindr | 0:94b743e06998 | 37 | int RET_accel_awake_triggercount = 0; |
pathfindr | 0:94b743e06998 | 38 | int RET_accel_alarm_thr = 127; |
pathfindr | 0:94b743e06998 | 39 | unsigned long RET_unixtime_configrun = 0; |
pathfindr | 0:94b743e06998 | 40 | unsigned long RET_unixtime_lastpost = 0; |
pathfindr | 0:94b743e06998 | 41 | unsigned long RET_unixtime_nextpost = 0; |
pathfindr | 0:94b743e06998 | 42 | unsigned long RET_unixtime_nextpost_failsafe = 0; |
pathfindr | 0:94b743e06998 | 43 | unsigned long RET_unixtime_framestart = 0; |
pathfindr | 0:94b743e06998 | 44 | unsigned long RET_motionstarttime = 0; |
pathfindr | 0:94b743e06998 | 45 | unsigned long RET_motionstoptime = 0; |
pathfindr | 0:94b743e06998 | 46 | int RET_gsm_failcount = 0; |
pathfindr | 0:94b743e06998 | 47 | int RET_serverresponse_failcount = 0; |
pathfindr | 0:94b743e06998 | 48 | int RET_motionconsecutive = 0; |
pathfindr | 0:94b743e06998 | 49 | int RET_no_motionconsecutive = 0; |
pathfindr | 0:94b743e06998 | 50 | bool RET_wethinkinmotion = false; |
pathfindr | 0:94b743e06998 | 51 | bool RET_wethinkhasmoved = false; |
pathfindr | 0:94b743e06998 | 52 | bool RET_haveservertime = false; |
pathfindr | 0:94b743e06998 | 53 | float RET_operationTimeHours = 0; |
pathfindr | 0:94b743e06998 | 54 | float RET_operationFrameTimeHours = 0; |
pathfindr | 0:94b743e06998 | 55 | int RET_operationCount = 0; |
pathfindr | 0:94b743e06998 | 56 | char RET_operationTimeString[100]; |
pathfindr | 0:94b743e06998 | 57 | |
pathfindr | 0:94b743e06998 | 58 | |
pathfindr | 0:94b743e06998 | 59 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 60 | //Pin states |
pathfindr | 0:94b743e06998 | 61 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 62 | DigitalOut led1(p11); |
pathfindr | 0:94b743e06998 | 63 | DigitalOut vreg_en(p29); |
pathfindr | 0:94b743e06998 | 64 | DigitalOut gsm_pwkey(p28); |
pathfindr | 0:94b743e06998 | 65 | |
pathfindr | 0:94b743e06998 | 66 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 67 | //Peripherals |
pathfindr | 0:94b743e06998 | 68 | //------------------------------------------------------------------------------ |
pathfindr | 0:94b743e06998 | 69 | Serial uart(USBTX, USBRX, 115200); |
pathfindr | 0:94b743e06998 | 70 | LIS3DH accelerometer(p23, p19, p24, p22, LIS3DH_DR_NR_LP_25HZ, LIS3DH_FS_8G); |
pathfindr | 0:94b743e06998 | 71 | BLE myble; |
pathfindr | 0:94b743e06998 | 72 | |
pathfindr | 0:94b743e06998 | 73 | |
pathfindr | 0:94b743e06998 | 74 | void gotosleep(long milliseconds) { |
pathfindr | 0:94b743e06998 | 75 | accelerometer.configureForSleep(); |
pathfindr | 0:94b743e06998 | 76 | Thread::wait(milliseconds); |
pathfindr | 0:94b743e06998 | 77 | //system_reset(); |
pathfindr | 0:94b743e06998 | 78 | } |
pathfindr | 0:94b743e06998 | 79 | |
pathfindr | 0:94b743e06998 | 80 | void LED1on(int milliseconds = 0) { |
pathfindr | 0:94b743e06998 | 81 | led1 = 0; |
pathfindr | 0:94b743e06998 | 82 | if (milliseconds > 0) { |
pathfindr | 0:94b743e06998 | 83 | Thread::wait(milliseconds); |
pathfindr | 0:94b743e06998 | 84 | led1 = 1; |
pathfindr | 0:94b743e06998 | 85 | } |
pathfindr | 0:94b743e06998 | 86 | } |
pathfindr | 0:94b743e06998 | 87 | void LED1off() { |
pathfindr | 0:94b743e06998 | 88 | led1 = 1; |
pathfindr | 0:94b743e06998 | 89 | } |
pathfindr | 0:94b743e06998 | 90 | |
pathfindr | 0:94b743e06998 | 91 | int main() { |
pathfindr | 0:94b743e06998 | 92 | setup(); |
pathfindr | 0:94b743e06998 | 93 | while(true) { |
pathfindr | 0:94b743e06998 | 94 | loop(); |
pathfindr | 0:94b743e06998 | 95 | } |
pathfindr | 0:94b743e06998 | 96 | } |
pathfindr | 0:94b743e06998 | 97 | |
pathfindr | 0:94b743e06998 | 98 | |
pathfindr | 0:94b743e06998 | 99 | void setup() { |
pathfindr | 0:94b743e06998 | 100 | led1 = 1; |
pathfindr | 0:94b743e06998 | 101 | set_time(1256729737); |
pathfindr | 0:94b743e06998 | 102 | |
pathfindr | 0:94b743e06998 | 103 | LIS3DH accelerometer(p23, p19, p24, p22, LIS3DH_DR_NR_LP_25HZ, LIS3DH_FS_8G); |
pathfindr | 0:94b743e06998 | 104 | accelerometer.configureForSleep(); |
pathfindr | 0:94b743e06998 | 105 | } |
pathfindr | 0:94b743e06998 | 106 | |
pathfindr | 0:94b743e06998 | 107 | void loop() { |
pathfindr | 0:94b743e06998 | 108 | |
pathfindr | 0:94b743e06998 | 109 | bool accel_awake; |
pathfindr | 0:94b743e06998 | 110 | //uart.printf("blah blah blah"); |
pathfindr | 0:94b743e06998 | 111 | //sleep_manager_lock_deep_sleep(); |
pathfindr | 0:94b743e06998 | 112 | //uart.printf("Deep sleep allowed: %i\r\n", sleep_manager_can_deep_sleep()); |
pathfindr | 0:94b743e06998 | 113 | /* |
pathfindr | 0:94b743e06998 | 114 | if (accelerometer.read_id() == 51){ |
pathfindr | 0:94b743e06998 | 115 | led1 = 0; |
pathfindr | 0:94b743e06998 | 116 | Thread::wait(20); |
pathfindr | 0:94b743e06998 | 117 | led1 = 1; |
pathfindr | 0:94b743e06998 | 118 | } else { |
pathfindr | 0:94b743e06998 | 119 | led1 = 1; |
pathfindr | 0:94b743e06998 | 120 | }; |
pathfindr | 0:94b743e06998 | 121 | */ |
pathfindr | 0:94b743e06998 | 122 | mode = MODE_SETUP; |
pathfindr | 0:94b743e06998 | 123 | |
pathfindr | 0:94b743e06998 | 124 | switch(mode) { |
pathfindr | 0:94b743e06998 | 125 | case MODE_SETUP : |
pathfindr | 0:94b743e06998 | 126 | |
pathfindr | 0:94b743e06998 | 127 | selftest(); |
pathfindr | 0:94b743e06998 | 128 | break; |
pathfindr | 0:94b743e06998 | 129 | |
pathfindr | 0:94b743e06998 | 130 | case MODE_INTERVAL : |
pathfindr | 0:94b743e06998 | 131 | |
pathfindr | 0:94b743e06998 | 132 | break; |
pathfindr | 0:94b743e06998 | 133 | |
pathfindr | 0:94b743e06998 | 134 | case MODE_INTELLIGENT : |
pathfindr | 0:94b743e06998 | 135 | |
pathfindr | 0:94b743e06998 | 136 | break; |
pathfindr | 0:94b743e06998 | 137 | |
pathfindr | 0:94b743e06998 | 138 | case MODE_MTU : |
pathfindr | 0:94b743e06998 | 139 | mode_mtu(); |
pathfindr | 0:94b743e06998 | 140 | break; |
pathfindr | 0:94b743e06998 | 141 | |
pathfindr | 0:94b743e06998 | 142 | case MODE_DORMANT : |
pathfindr | 0:94b743e06998 | 143 | |
pathfindr | 0:94b743e06998 | 144 | break; |
pathfindr | 0:94b743e06998 | 145 | |
pathfindr | 0:94b743e06998 | 146 | default : |
pathfindr | 0:94b743e06998 | 147 | mode = MODE_SETUP; |
pathfindr | 0:94b743e06998 | 148 | } |
pathfindr | 0:94b743e06998 | 149 | |
pathfindr | 0:94b743e06998 | 150 | |
pathfindr | 0:94b743e06998 | 151 | |
pathfindr | 0:94b743e06998 | 152 | |
pathfindr | 0:94b743e06998 | 153 | gotosleep(60000); |
pathfindr | 0:94b743e06998 | 154 | } |
pathfindr | 0:94b743e06998 | 155 | |
pathfindr | 0:94b743e06998 | 156 | |
pathfindr | 0:94b743e06998 | 157 | |
pathfindr | 0:94b743e06998 | 158 | |
pathfindr | 0:94b743e06998 | 159 | void initMotion() { |
pathfindr | 0:94b743e06998 | 160 | //CHECK FOR ANY MOTION |
pathfindr | 0:94b743e06998 | 161 | if (!accelerometer.selfTest()) { |
pathfindr | 0:94b743e06998 | 162 | if (exceptions.find("A.") < 0) { |
pathfindr | 0:94b743e06998 | 163 | exceptions += "A."; |
pathfindr | 0:94b743e06998 | 164 | } |
pathfindr | 0:94b743e06998 | 165 | DEBUG("Couldnt start accelerometer"); |
pathfindr | 0:94b743e06998 | 166 | } else { |
pathfindr | 0:94b743e06998 | 167 | accel_healthy = true; |
pathfindr | 0:94b743e06998 | 168 | } |
pathfindr | 0:94b743e06998 | 169 | } |
pathfindr | 0:94b743e06998 | 170 | |
pathfindr | 0:94b743e06998 | 171 | void mode_mtu() { |
pathfindr | 0:94b743e06998 | 172 | bool accel_awake = accelerometer.getINT2(); |
pathfindr | 0:94b743e06998 | 173 | |
pathfindr | 0:94b743e06998 | 174 | //CHECK FOR MOTION |
pathfindr | 0:94b743e06998 | 175 | if (accel_awake == true) { |
pathfindr | 0:94b743e06998 | 176 | LED1on(50); |
pathfindr | 0:94b743e06998 | 177 | RET_motionconsecutive ++; |
pathfindr | 0:94b743e06998 | 178 | RET_no_motionconsecutive = 0; |
pathfindr | 0:94b743e06998 | 179 | |
pathfindr | 0:94b743e06998 | 180 | //this is needed to ensure accel_awake_triggercount is 1 or higher for mode 2 |
pathfindr | 0:94b743e06998 | 181 | if (RET_accel_awake_triggercount == 0) { |
pathfindr | 0:94b743e06998 | 182 | RET_accel_awake_triggercount = 1; |
pathfindr | 0:94b743e06998 | 183 | } |
pathfindr | 0:94b743e06998 | 184 | |
pathfindr | 0:94b743e06998 | 185 | if (RET_motionconsecutive == RET_accel_awake_triggercount && RET_wethinkinmotion == false) { |
pathfindr | 0:94b743e06998 | 186 | RET_wethinkinmotion = true; |
pathfindr | 0:94b743e06998 | 187 | RET_motionstarttime = (time(NULL) - ((RET_accel_awake_triggercount+1) * RET_motioncheck_interval_seconds)); |
pathfindr | 0:94b743e06998 | 188 | long eventEpoch = RET_motionstarttime; |
pathfindr | 0:94b743e06998 | 189 | long epochoffsetminutes = ((eventEpoch - RET_unixtime_framestart) / 60); |
pathfindr | 0:94b743e06998 | 190 | |
pathfindr | 0:94b743e06998 | 191 | char buf[20]; |
pathfindr | 0:94b743e06998 | 192 | sprintf(buf,"1.%i!", epochoffsetminutes); |
pathfindr | 0:94b743e06998 | 193 | strcat(RET_operationTimeString, buf); |
pathfindr | 0:94b743e06998 | 194 | RET_operationCount ++; |
pathfindr | 0:94b743e06998 | 195 | |
pathfindr | 0:94b743e06998 | 196 | DEBUG("%s", RET_operationTimeString); |
pathfindr | 0:94b743e06998 | 197 | |
pathfindr | 0:94b743e06998 | 198 | LEDon(100); |
pathfindr | 0:94b743e06998 | 199 | LEDon(100); |
pathfindr | 0:94b743e06998 | 200 | LEDon(100); |
pathfindr | 0:94b743e06998 | 201 | LEDon(100); |
pathfindr | 0:94b743e06998 | 202 | } |
pathfindr | 0:94b743e06998 | 203 | } else if (accel_awake == false) { |
pathfindr | 0:94b743e06998 | 204 | RET_no_motionconsecutive ++; |
pathfindr | 0:94b743e06998 | 205 | RET_motionconsecutive = 0; |
pathfindr | 0:94b743e06998 | 206 | if (RET_no_motionconsecutive == RET_accel_awake_triggercount && RET_wethinkinmotion == true) { |
pathfindr | 0:94b743e06998 | 207 | RET_wethinkinmotion = false; |
pathfindr | 0:94b743e06998 | 208 | // log engine stop |
pathfindr | 0:94b743e06998 | 209 | RET_motionstoptime = (time(NULL) - ((RET_accel_awake_triggercount+1) * RET_motioncheck_interval_seconds)); |
pathfindr | 0:94b743e06998 | 210 | long eventEpoch = RET_motionstoptime; |
pathfindr | 0:94b743e06998 | 211 | long epochoffsetminutes = ((eventEpoch - RET_unixtime_framestart) / 60); |
pathfindr | 0:94b743e06998 | 212 | |
pathfindr | 0:94b743e06998 | 213 | char buf[20]; |
pathfindr | 0:94b743e06998 | 214 | sprintf(buf,"0.%i!", epochoffsetminutes); |
pathfindr | 0:94b743e06998 | 215 | strcat(RET_operationTimeString, buf); |
pathfindr | 0:94b743e06998 | 216 | |
pathfindr | 0:94b743e06998 | 217 | RET_operationFrameTimeHours = (float(RET_motionstoptime - RET_motionstarttime) / 3600.0); |
pathfindr | 0:94b743e06998 | 218 | RET_operationTimeHours += RET_operationFrameTimeHours; |
pathfindr | 0:94b743e06998 | 219 | |
pathfindr | 0:94b743e06998 | 220 | //test by posting on every stop event |
pathfindr | 0:94b743e06998 | 221 | //doOperationTimePost = true; |
pathfindr | 0:94b743e06998 | 222 | |
pathfindr | 0:94b743e06998 | 223 | DEBUG("%i", RET_operationTimeHours); |
pathfindr | 0:94b743e06998 | 224 | LEDon(500); |
pathfindr | 0:94b743e06998 | 225 | } |
pathfindr | 0:94b743e06998 | 226 | } |
pathfindr | 0:94b743e06998 | 227 | |
pathfindr | 0:94b743e06998 | 228 | |
pathfindr | 0:94b743e06998 | 229 | // Check time interval for location run |
pathfindr | 0:94b743e06998 | 230 | if (time(NULL) > RET_unixtime_nextpost_failsafe) { |
pathfindr | 0:94b743e06998 | 231 | //setfailsafetime(); //these must be before gpsPost, incase gpsPost fails, then stuck in broadcast loop |
pathfindr | 0:94b743e06998 | 232 | //gpsPost(); |
pathfindr | 0:94b743e06998 | 233 | } |
pathfindr | 0:94b743e06998 | 234 | |
pathfindr | 0:94b743e06998 | 235 | //Check for operatingTime post time or if data buffer is full for force post |
pathfindr | 0:94b743e06998 | 236 | if (RET_operationFrameTimeHours > 0.0) { |
pathfindr | 0:94b743e06998 | 237 | if (time(NULL) > RET_unixtime_nextpost || strlen(RET_operationTimeString) > 60) { |
pathfindr | 0:94b743e06998 | 238 | //only bother if we actual have data to post |
pathfindr | 0:94b743e06998 | 239 | //setwaketime(); //these must be before gpsPost, incase gpsPost fails, then stuck in broadcast loop |
pathfindr | 0:94b743e06998 | 240 | //uploadOperationTimeData(); |
pathfindr | 0:94b743e06998 | 241 | } |
pathfindr | 0:94b743e06998 | 242 | } |
pathfindr | 0:94b743e06998 | 243 | |
pathfindr | 0:94b743e06998 | 244 | } |
pathfindr | 0:94b743e06998 | 245 | |
pathfindr | 0:94b743e06998 | 246 | |
pathfindr | 0:94b743e06998 | 247 | |
pathfindr | 0:94b743e06998 | 248 | |
pathfindr | 0:94b743e06998 | 249 | |
pathfindr | 0:94b743e06998 | 250 | |
pathfindr | 0:94b743e06998 | 251 | int selftest() { |
pathfindr | 0:94b743e06998 | 252 | int result = 0; |
pathfindr | 0:94b743e06998 | 253 | int tests = 0; |
pathfindr | 0:94b743e06998 | 254 | int testscore = 0; |
pathfindr | 0:94b743e06998 | 255 | led1 = 0; |
pathfindr | 0:94b743e06998 | 256 | string failures; |
pathfindr | 0:94b743e06998 | 257 | |
pathfindr | 0:94b743e06998 | 258 | //Accelerometer |
pathfindr | 0:94b743e06998 | 259 | tests ++; |
pathfindr | 0:94b743e06998 | 260 | if (accelerometer.read_id() == 51){ |
pathfindr | 0:94b743e06998 | 261 | testscore ++; |
pathfindr | 0:94b743e06998 | 262 | } else { |
pathfindr | 0:94b743e06998 | 263 | //Accelerometer Fail |
pathfindr | 0:94b743e06998 | 264 | failures += 'Ac,'; |
pathfindr | 0:94b743e06998 | 265 | }; |
pathfindr | 0:94b743e06998 | 266 | |
pathfindr | 0:94b743e06998 | 267 | return result; |
pathfindr | 0:94b743e06998 | 268 | } |