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

Dependencies:   XBEE_900HP_SPI mbed

main.cpp

Committer:
ottaviano3
Date:
2015-05-01
Revision:
0:3540612dfbf8
Child:
1:b51fb321d026

File content as of revision 0:3540612dfbf8:

#include "mbed.h"
#include "xbee900hp.h"


// Define GPS pin connections.
Serial gps(dp16,dp15);

// Define xbee connections. Mosi,Miso,SCK,ATTN,Reset
xbee900hp xbee(dp2,dp1,dp6, dp9, dp4);

// Function prototyp for gps getline fuction
void getline();

// Container for read GPS message.
char gpsmsg[256];

int main()
{
    // Set gps baud rate to 4800, the default.
    gps.baud(4800);

    // Configure gps with default values.
    unsigned int gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'0'^','^'1'^','^'4'^'8'^'0'^'0'^','^'8'^','^'1'^','^'0';
    gps.printf("$PSRF100,1,4800,8,1,0*%i\r\n",gpsxor);

    // Enable GGA
    gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'0'^','^'0'^','^'1'^','^'1';
    gps.printf("$PSRF103,0,0,1,1*%i\r\n",gpsxor);

    // Disable GLL
    gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'1'^','^'0'^','^'0'^','^'1';
    gps.printf("$PSRF103,1,0,0,1*%i\r\n",gpsxor);

    // Disable GSA
    gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'2'^','^'0'^','^'0'^','^'1';
    gps.printf("$PSRF103,2,0,0,1*%i\r\n",gpsxor);

    // Disable GSV
    gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'3'^','^'0'^','^'0'^','^'1';
    gps.printf("$PSRF103,3,0,0,1*%i\r\n",gpsxor);

    // Disable RMC
    gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'4'^','^'0'^','^'0'^','^'1';
    gps.printf("$PSRF103,4,0,0,1*%i\r\n",gpsxor);

    // Disable VTG
    gpsxor = 'P'^'S'^'R'^'F'^'1'^'0'^'3'^','^'5'^','^'0'^','^'0'^','^'1';
    gps.printf("$PSRF103,5,0,0,1*%i\r\n",gpsxor);


    // Set the node id of this module. Generated from the serial of the xbee
    char nodeidarray[10];
    int nodeid = 0;
    
    // Repeat until gets a value.
    while (nodeid == 0) {
        nodeid = 0;
        // Initialize nodeidarray
        for (int i = 0; i<10; i++) {
            nodeidarray[i] = 0;
        }
    
        // Get xbee serial number
        int serialget = xbee.getSerial(nodeidarray);
        
        
        if (serialget == 0) {
            for (int i = 0; i<10; i++) {
                // Nonlinear function to expand unique id space
                nodeid += nodeidarray[i]*nodeidarray[i]+nodeidarray[i];
            }
        }
    }

    // Node id should now be a unique int

    // Variables for the various important gps command codes
    float latitude, longitude, altitude, hdop, time;
    char ns, ew;
    int lock, satsused;

    // Store message to send out.
    char buffer[256];
    
    // Announce to base platform
    // Format code line to send out on radio that node has no lock
    unsigned int filledbuff = snprintf (buffer, sizeof(buffer)-1, "DNLK,%i\n\n",nodeid);
    // Send packet out into the air.
    xbee.sendPacket(buffer, filledbuff);

    // Main program run loop.
    while(1) {
        // Get the next gps line.
        getline();

        // Extract gps information from the GPGGA line.
        if(sscanf(gpsmsg, "GPGGA,%f,%f,%c,%f,%c,%i,%i,%f,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &satsused, &hdop, &altitude) >= 1) {

            // Check if the GPS has a lock or not. (1,2, or 6 indicate lock)
            if((lock != 1) && (lock != 2)) {
                // Format code line to send out on radio that node has no lock
                unsigned int filledbuff = snprintf (buffer, sizeof(buffer)-1, "DNLK,%i\n\n",nodeid);
                // Send packet out into the air.
                xbee.sendPacket(buffer, filledbuff);
            } else {

                // Convert latitude from ddmm.mmmm to dd.dddddd
                double degrees;
                double minutes = modf(latitude/100.0f, &degrees);
                minutes = (minutes*100.0f)/60.0f;
                latitude = degrees + minutes;

                // Convert longitude from ddmm.mmmm to dd.dddddd
                minutes = modf(longitude/100.0f, &degrees);
                minutes = (minutes*100.0f)/60.0f;
                longitude = degrees + minutes;

                // Set signs correctly
                if(ns == 'S') {
                    latitude  *= -1.0;
                }
                if(ew == 'W') {
                    longitude *= -1.0;
                }

                // Format string for sending out over radio.
                unsigned int filledbuff = snprintf (buffer, sizeof(buffer)-1, "DLIN,%i,%f,%f,%f\n\n",nodeid, latitude, longitude, altitude);
                // Send packet out.
                xbee.sendPacket(buffer, filledbuff);
            }
        }
    }
}

// GPS serial get line function
void getline()
{
    // wait for the start of a line which $ denotes
    while(gps.getc() != '$');

    // read the gps line until end of line
    for(int i=0; i<256; i++) {
        gpsmsg[i] = gps.getc();
        if(gpsmsg[i] == '\r') {
            gpsmsg[i] = 0;
            return ;
        }
    }
}