Tracks GPS coordinates of an asset. Uses: GPS GSM Accelerometer mbed
Dependencies: mbed MODSERIAL ADXL345 MODGPS
main.cpp@0:4415987ca08f, 2012-05-04 (annotated)
- Committer:
- gsulc
- Date:
- Fri May 04 14:11:45 2012 +0000
- Revision:
- 0:4415987ca08f
v0.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsulc | 0:4415987ca08f | 1 | #include "mbed.h" |
gsulc | 0:4415987ca08f | 2 | #include "stdio.h" |
gsulc | 0:4415987ca08f | 3 | #include "sms.h" //custom sms library |
gsulc | 0:4415987ca08f | 4 | #include "SecurityMechanism.h" //custom security mechanism library |
gsulc | 0:4415987ca08f | 5 | #include "MODSERIAL.h" //buffer library for serial |
gsulc | 0:4415987ca08f | 6 | #include "GPS.h" //GPS library |
gsulc | 0:4415987ca08f | 7 | #include "ADXL345.h" //Sparkfun ADXL345 accelerometer library |
gsulc | 0:4415987ca08f | 8 | |
gsulc | 0:4415987ca08f | 9 | // System |
gsulc | 0:4415987ca08f | 10 | DigitalOut pulse(LED1); //heartbeat |
gsulc | 0:4415987ca08f | 11 | Serial pc(USBTX,USBRX); //for debugging |
gsulc | 0:4415987ca08f | 12 | |
gsulc | 0:4415987ca08f | 13 | // Peripherals |
gsulc | 0:4415987ca08f | 14 | MODSERIAL gsm(p28,p27); |
gsulc | 0:4415987ca08f | 15 | GPS gps(NC, p10); |
gsulc | 0:4415987ca08f | 16 | ADXL345 accelerometer(p5, p6, p7, p8); |
gsulc | 0:4415987ca08f | 17 | |
gsulc | 0:4415987ca08f | 18 | // Power Control |
gsulc | 0:4415987ca08f | 19 | DigitalOut gps_on(p22); |
gsulc | 0:4415987ca08f | 20 | DigitalOut gps_xstandby(p21); |
gsulc | 0:4415987ca08f | 21 | DigitalOut accelerometer_on(p20); |
gsulc | 0:4415987ca08f | 22 | //DigitalIn security_pwrd(p17); |
gsulc | 0:4415987ca08f | 23 | AnalogIn voltage_level(p19); |
gsulc | 0:4415987ca08f | 24 | |
gsulc | 0:4415987ca08f | 25 | // Global Vars |
gsulc | 0:4415987ca08f | 26 | int accel_reading[3] = {0, 0, 0}; //x, y, z coordinates of accelerometer |
gsulc | 0:4415987ca08f | 27 | int prev_accel_reading[3] = {0, 0, 0}; //x, y, z coordinates of accelerometer |
gsulc | 0:4415987ca08f | 28 | string phoneNumber = "4047849578"; |
gsulc | 0:4415987ca08f | 29 | bool security_enabled = true; |
gsulc | 0:4415987ca08f | 30 | bool movement_detected = false; |
gsulc | 0:4415987ca08f | 31 | bool command_sent = false; |
gsulc | 0:4415987ca08f | 32 | #define ACCEL_THRESH 50 |
gsulc | 0:4415987ca08f | 33 | |
gsulc | 0:4415987ca08f | 34 | int main() { |
gsulc | 0:4415987ca08f | 35 | pc.printf("Initializing Program.\r\n"); |
gsulc | 0:4415987ca08f | 36 | //****init GSM |
gsulc | 0:4415987ca08f | 37 | GSM_init(); |
gsulc | 0:4415987ca08f | 38 | |
gsulc | 0:4415987ca08f | 39 | //****init GPS |
gsulc | 0:4415987ca08f | 40 | pc.printf("Initializing GPS.\r\n"); |
gsulc | 0:4415987ca08f | 41 | GPS_Time t; |
gsulc | 0:4415987ca08f | 42 | gps.baud(4800); |
gsulc | 0:4415987ca08f | 43 | |
gsulc | 0:4415987ca08f | 44 | //****init accelerometer |
gsulc | 0:4415987ca08f | 45 | accelerometer_on = 1; |
gsulc | 0:4415987ca08f | 46 | pc.printf("Initializing Accelerometer.\r\n"); |
gsulc | 0:4415987ca08f | 47 | //Go into standby mode to configure the device. |
gsulc | 0:4415987ca08f | 48 | accelerometer.setPowerControl(0x00); |
gsulc | 0:4415987ca08f | 49 | |
gsulc | 0:4415987ca08f | 50 | //Full resolution, +/-16g, 4mg/LSB. |
gsulc | 0:4415987ca08f | 51 | accelerometer.setDataFormatControl(0x0B); |
gsulc | 0:4415987ca08f | 52 | |
gsulc | 0:4415987ca08f | 53 | //3.2kHz data rate. |
gsulc | 0:4415987ca08f | 54 | accelerometer.setDataRate(ADXL345_3200HZ); |
gsulc | 0:4415987ca08f | 55 | |
gsulc | 0:4415987ca08f | 56 | //Measurement mode. |
gsulc | 0:4415987ca08f | 57 | accelerometer.setPowerControl(0x08); |
gsulc | 0:4415987ca08f | 58 | |
gsulc | 0:4415987ca08f | 59 | pc.printf("Initializing Complete.\r\n"); |
gsulc | 0:4415987ca08f | 60 | |
gsulc | 0:4415987ca08f | 61 | wait(0.4); |
gsulc | 0:4415987ca08f | 62 | |
gsulc | 0:4415987ca08f | 63 | while(1) { |
gsulc | 0:4415987ca08f | 64 | accelerometer.getOutput(accel_reading); |
gsulc | 0:4415987ca08f | 65 | //check for movement |
gsulc | 0:4415987ca08f | 66 | for (int i = 0; i < 3; i++) { |
gsulc | 0:4415987ca08f | 67 | if (((int16_t)prev_accel_reading[i] != 0) && (int16_t)abs(accel_reading[i] - prev_accel_reading[i]) > ACCEL_THRESH) { |
gsulc | 0:4415987ca08f | 68 | pc.printf("Movement detected\r\n"); |
gsulc | 0:4415987ca08f | 69 | movement_detected = true; |
gsulc | 0:4415987ca08f | 70 | } |
gsulc | 0:4415987ca08f | 71 | prev_accel_reading[i] = accel_reading[i]; |
gsulc | 0:4415987ca08f | 72 | } |
gsulc | 0:4415987ca08f | 73 | |
gsulc | 0:4415987ca08f | 74 | //pc.printf("Seurity system = %d\r\n", security_pwrd); |
gsulc | 0:4415987ca08f | 75 | //security_enabled = security_pwrd; |
gsulc | 0:4415987ca08f | 76 | |
gsulc | 0:4415987ca08f | 77 | if (movement_detected) { |
gsulc | 0:4415987ca08f | 78 | if (security_enabled) { |
gsulc | 0:4415987ca08f | 79 | //turn off accelerometer |
gsulc | 0:4415987ca08f | 80 | accelerometer_on = 0; |
gsulc | 0:4415987ca08f | 81 | pc.printf("Alert\r\n"); |
gsulc | 0:4415987ca08f | 82 | send_location(); |
gsulc | 0:4415987ca08f | 83 | wait(30); |
gsulc | 0:4415987ca08f | 84 | } |
gsulc | 0:4415987ca08f | 85 | } |
gsulc | 0:4415987ca08f | 86 | |
gsulc | 0:4415987ca08f | 87 | if (command_sent) { |
gsulc | 0:4415987ca08f | 88 | pc.printf("Ping.\r\n"); |
gsulc | 0:4415987ca08f | 89 | send_location(); |
gsulc | 0:4415987ca08f | 90 | command_sent = false; |
gsulc | 0:4415987ca08f | 91 | gps_on = 0; |
gsulc | 0:4415987ca08f | 92 | gps_xstandby = 0; |
gsulc | 0:4415987ca08f | 93 | } |
gsulc | 0:4415987ca08f | 94 | |
gsulc | 0:4415987ca08f | 95 | //heartbeat |
gsulc | 0:4415987ca08f | 96 | pulse = 1; |
gsulc | 0:4415987ca08f | 97 | wait(0.2); |
gsulc | 0:4415987ca08f | 98 | pulse = 0; |
gsulc | 0:4415987ca08f | 99 | wait(0.2); |
gsulc | 0:4415987ca08f | 100 | } |
gsulc | 0:4415987ca08f | 101 | } |