The software that runs on the Sentinel nodes. This code is for the LPC1114FN28.

Dependencies:   XBEE_900HP_SPI mbed

Committer:
ottaviano3
Date:
Fri May 01 15:53:34 2015 +0000
Revision:
0:3540612dfbf8
Child:
1:b51fb321d026
Sentinel Node Application

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ottaviano3 0:3540612dfbf8 1 #include "mbed.h"
ottaviano3 0:3540612dfbf8 2 #include "xbee900hp.h"
ottaviano3 0:3540612dfbf8 3
ottaviano3 0:3540612dfbf8 4
ottaviano3 0:3540612dfbf8 5 // Define GPS pin connections.
ottaviano3 0:3540612dfbf8 6 Serial gps(dp16,dp15);
ottaviano3 0:3540612dfbf8 7
ottaviano3 0:3540612dfbf8 8 // Define xbee connections. Mosi,Miso,SCK,ATTN,Reset
ottaviano3 0:3540612dfbf8 9 xbee900hp xbee(dp2,dp1,dp6, dp9, dp4);
ottaviano3 0:3540612dfbf8 10
ottaviano3 0:3540612dfbf8 11 // Function prototyp for gps getline fuction
ottaviano3 0:3540612dfbf8 12 void getline();
ottaviano3 0:3540612dfbf8 13
ottaviano3 0:3540612dfbf8 14 // Container for read GPS message.
ottaviano3 0:3540612dfbf8 15 char gpsmsg[256];
ottaviano3 0:3540612dfbf8 16
ottaviano3 0:3540612dfbf8 17 int main()
ottaviano3 0:3540612dfbf8 18 {
ottaviano3 0:3540612dfbf8 19 // Set gps baud rate to 4800, the default.
ottaviano3 0:3540612dfbf8 20 gps.baud(4800);
ottaviano3 0:3540612dfbf8 21
ottaviano3 0:3540612dfbf8 22 // Configure gps with default values.
ottaviano3 0:3540612dfbf8 23 unsigned int gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'0'^','^'1'^','^'4'^'8'^'0'^'0'^','^'8'^','^'1'^','^'0';
ottaviano3 0:3540612dfbf8 24 gps.printf("$PSRF100,1,4800,8,1,0*%i\r\n",gpsxor);
ottaviano3 0:3540612dfbf8 25
ottaviano3 0:3540612dfbf8 26 // Enable GGA
ottaviano3 0:3540612dfbf8 27 gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'0'^','^'0'^','^'1'^','^'1';
ottaviano3 0:3540612dfbf8 28 gps.printf("$PSRF103,0,0,1,1*%i\r\n",gpsxor);
ottaviano3 0:3540612dfbf8 29
ottaviano3 0:3540612dfbf8 30 // Disable GLL
ottaviano3 0:3540612dfbf8 31 gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'1'^','^'0'^','^'0'^','^'1';
ottaviano3 0:3540612dfbf8 32 gps.printf("$PSRF103,1,0,0,1*%i\r\n",gpsxor);
ottaviano3 0:3540612dfbf8 33
ottaviano3 0:3540612dfbf8 34 // Disable GSA
ottaviano3 0:3540612dfbf8 35 gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'2'^','^'0'^','^'0'^','^'1';
ottaviano3 0:3540612dfbf8 36 gps.printf("$PSRF103,2,0,0,1*%i\r\n",gpsxor);
ottaviano3 0:3540612dfbf8 37
ottaviano3 0:3540612dfbf8 38 // Disable GSV
ottaviano3 0:3540612dfbf8 39 gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'3'^','^'0'^','^'0'^','^'1';
ottaviano3 0:3540612dfbf8 40 gps.printf("$PSRF103,3,0,0,1*%i\r\n",gpsxor);
ottaviano3 0:3540612dfbf8 41
ottaviano3 0:3540612dfbf8 42 // Disable RMC
ottaviano3 0:3540612dfbf8 43 gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'4'^','^'0'^','^'0'^','^'1';
ottaviano3 0:3540612dfbf8 44 gps.printf("$PSRF103,4,0,0,1*%i\r\n",gpsxor);
ottaviano3 0:3540612dfbf8 45
ottaviano3 0:3540612dfbf8 46 // Disable VTG
ottaviano3 0:3540612dfbf8 47 gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'5'^','^'0'^','^'0'^','^'1';
ottaviano3 0:3540612dfbf8 48 gps.printf("$PSRF103,5,0,0,1*%i\r\n",gpsxor);
ottaviano3 0:3540612dfbf8 49
ottaviano3 0:3540612dfbf8 50
ottaviano3 0:3540612dfbf8 51 // Set the node id of this module. Generated from the serial of the xbee
ottaviano3 0:3540612dfbf8 52 char nodeidarray[10];
ottaviano3 0:3540612dfbf8 53 int nodeid = 0;
ottaviano3 0:3540612dfbf8 54
ottaviano3 0:3540612dfbf8 55 // Repeat until gets a value.
ottaviano3 0:3540612dfbf8 56 while (nodeid == 0) {
ottaviano3 0:3540612dfbf8 57 nodeid = 0;
ottaviano3 0:3540612dfbf8 58 // Initialize nodeidarray
ottaviano3 0:3540612dfbf8 59 for (int i = 0; i<10; i++) {
ottaviano3 0:3540612dfbf8 60 nodeidarray[i] = 0;
ottaviano3 0:3540612dfbf8 61 }
ottaviano3 0:3540612dfbf8 62
ottaviano3 0:3540612dfbf8 63 // Get xbee serial number
ottaviano3 0:3540612dfbf8 64 int serialget = xbee.getSerial(nodeidarray);
ottaviano3 0:3540612dfbf8 65
ottaviano3 0:3540612dfbf8 66
ottaviano3 0:3540612dfbf8 67 if (serialget == 0) {
ottaviano3 0:3540612dfbf8 68 for (int i = 0; i<10; i++) {
ottaviano3 0:3540612dfbf8 69 // Nonlinear function to expand unique id space
ottaviano3 0:3540612dfbf8 70 nodeid += nodeidarray[i]*nodeidarray[i]+nodeidarray[i];
ottaviano3 0:3540612dfbf8 71 }
ottaviano3 0:3540612dfbf8 72 }
ottaviano3 0:3540612dfbf8 73 }
ottaviano3 0:3540612dfbf8 74
ottaviano3 0:3540612dfbf8 75 // Node id should now be a unique int
ottaviano3 0:3540612dfbf8 76
ottaviano3 0:3540612dfbf8 77 // Variables for the various important gps command codes
ottaviano3 0:3540612dfbf8 78 float latitude, longitude, altitude, hdop, time;
ottaviano3 0:3540612dfbf8 79 char ns, ew;
ottaviano3 0:3540612dfbf8 80 int lock, satsused;
ottaviano3 0:3540612dfbf8 81
ottaviano3 0:3540612dfbf8 82 // Store message to send out.
ottaviano3 0:3540612dfbf8 83 char buffer[256];
ottaviano3 0:3540612dfbf8 84
ottaviano3 0:3540612dfbf8 85 // Announce to base platform
ottaviano3 0:3540612dfbf8 86 // Format code line to send out on radio that node has no lock
ottaviano3 0:3540612dfbf8 87 unsigned int filledbuff = snprintf (buffer, sizeof(buffer)-1, "DNLK,%i\n\n",nodeid);
ottaviano3 0:3540612dfbf8 88 // Send packet out into the air.
ottaviano3 0:3540612dfbf8 89 xbee.sendPacket(buffer, filledbuff);
ottaviano3 0:3540612dfbf8 90
ottaviano3 0:3540612dfbf8 91 // Main program run loop.
ottaviano3 0:3540612dfbf8 92 while(1) {
ottaviano3 0:3540612dfbf8 93 // Get the next gps line.
ottaviano3 0:3540612dfbf8 94 getline();
ottaviano3 0:3540612dfbf8 95
ottaviano3 0:3540612dfbf8 96 // Extract gps information from the GPGGA line.
ottaviano3 0:3540612dfbf8 97 if(sscanf(gpsmsg, "GPGGA,%f,%f,%c,%f,%c,%i,%i,%f,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &satsused, &hdop, &altitude) >= 1) {
ottaviano3 0:3540612dfbf8 98
ottaviano3 0:3540612dfbf8 99 // Check if the GPS has a lock or not. (1,2, or 6 indicate lock)
ottaviano3 0:3540612dfbf8 100 if((lock != 1) && (lock != 2)) {
ottaviano3 0:3540612dfbf8 101 // Format code line to send out on radio that node has no lock
ottaviano3 0:3540612dfbf8 102 unsigned int filledbuff = snprintf (buffer, sizeof(buffer)-1, "DNLK,%i\n\n",nodeid);
ottaviano3 0:3540612dfbf8 103 // Send packet out into the air.
ottaviano3 0:3540612dfbf8 104 xbee.sendPacket(buffer, filledbuff);
ottaviano3 0:3540612dfbf8 105 } else {
ottaviano3 0:3540612dfbf8 106
ottaviano3 0:3540612dfbf8 107 // Convert latitude from ddmm.mmmm to dd.dddddd
ottaviano3 0:3540612dfbf8 108 double degrees;
ottaviano3 0:3540612dfbf8 109 double minutes = modf(latitude/100.0f, &degrees);
ottaviano3 0:3540612dfbf8 110 minutes = (minutes*100.0f)/60.0f;
ottaviano3 0:3540612dfbf8 111 latitude = degrees + minutes;
ottaviano3 0:3540612dfbf8 112
ottaviano3 0:3540612dfbf8 113 // Convert longitude from ddmm.mmmm to dd.dddddd
ottaviano3 0:3540612dfbf8 114 minutes = modf(longitude/100.0f, &degrees);
ottaviano3 0:3540612dfbf8 115 minutes = (minutes*100.0f)/60.0f;
ottaviano3 0:3540612dfbf8 116 longitude = degrees + minutes;
ottaviano3 0:3540612dfbf8 117
ottaviano3 0:3540612dfbf8 118 // Set signs correctly
ottaviano3 0:3540612dfbf8 119 if(ns == 'S') {
ottaviano3 0:3540612dfbf8 120 latitude *= -1.0;
ottaviano3 0:3540612dfbf8 121 }
ottaviano3 0:3540612dfbf8 122 if(ew == 'W') {
ottaviano3 0:3540612dfbf8 123 longitude *= -1.0;
ottaviano3 0:3540612dfbf8 124 }
ottaviano3 0:3540612dfbf8 125
ottaviano3 0:3540612dfbf8 126 // Format string for sending out over radio.
ottaviano3 0:3540612dfbf8 127 unsigned int filledbuff = snprintf (buffer, sizeof(buffer)-1, "DLIN,%i,%f,%f,%f\n\n",nodeid, latitude, longitude, altitude);
ottaviano3 0:3540612dfbf8 128 // Send packet out.
ottaviano3 0:3540612dfbf8 129 xbee.sendPacket(buffer, filledbuff);
ottaviano3 0:3540612dfbf8 130 }
ottaviano3 0:3540612dfbf8 131 }
ottaviano3 0:3540612dfbf8 132 }
ottaviano3 0:3540612dfbf8 133 }
ottaviano3 0:3540612dfbf8 134
ottaviano3 0:3540612dfbf8 135 // GPS serial get line function
ottaviano3 0:3540612dfbf8 136 void getline()
ottaviano3 0:3540612dfbf8 137 {
ottaviano3 0:3540612dfbf8 138 // wait for the start of a line which $ denotes
ottaviano3 0:3540612dfbf8 139 while(gps.getc() != '$');
ottaviano3 0:3540612dfbf8 140
ottaviano3 0:3540612dfbf8 141 // read the gps line until end of line
ottaviano3 0:3540612dfbf8 142 for(int i=0; i<256; i++) {
ottaviano3 0:3540612dfbf8 143 gpsmsg[i] = gps.getc();
ottaviano3 0:3540612dfbf8 144 if(gpsmsg[i] == '\r') {
ottaviano3 0:3540612dfbf8 145 gpsmsg[i] = 0;
ottaviano3 0:3540612dfbf8 146 return ;
ottaviano3 0:3540612dfbf8 147 }
ottaviano3 0:3540612dfbf8 148 }
ottaviano3 0:3540612dfbf8 149 }