init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Thu Nov 08 15:08:47 2018 +0000
Revision:
2:fd554f01abdf
Parent:
0:94b743e06998
Child:
4:8d8e9bfa82e4
tetw

Who changed what in which revision?

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