The software that runs on the Sentinel base station. This code is for the LPC1768.

Dependencies:   XBEE_900HP_SPI mbed-rtos mbed

main.cpp

Committer:
ottaviano3
Date:
2015-05-01
Revision:
0:1e026f2c8707
Child:
1:dd8228f2263c

File content as of revision 0:1e026f2c8707:

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

// Serial output to PC
Serial pc(USBTX,USBRX);

// GPS to pc
Serial gps(p28,p27);

// setup xbee
xbee900hp xbee(p11,p12,p13, p8, p9);

DigitalOut led1(LED1);
DigitalOut led2(LED2);

// Buffer for reading in from xbee
char buffer[256];

// Predefine scanner thread
void xbeeScanner(void const *args);
void hostScanner(void const *args);
void getline();

char gpsmsg[256];


int main()
{
    /*Configuration Area
    */

    // Set pc baud rate to pretty high for some speed
    pc.baud(57600);
    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);

    /*End Configuration Area
    */
    while (true) {
        // Define Keys
        char startkey[12] = "SentinelOn";
        char endkey[13] = "SentinelOff";
        char buffer[30];

        do {
            if (pc.readable() == true) {
                // Get bytes from computer
                pc.scanf("%s",buffer);
            }

            led1 = 1;
        } while (strcmp( startkey, buffer ) != 0);
        led1 = 0;

        // Code to respond saying its started.
        pc.printf("SLON\r\n");

        xbee.clearBuff();

        // Start xbeeScanner Thread
        Thread xbscan(xbeeScanner);
        Thread hostscan(hostScanner);

        // Main runloop
        do {
            if (pc.readable() == true) {
                // Get bytes from computer
                pc.scanf("%s",buffer);
            }

            led2 = 1;
        } while (strcmp( endkey, buffer ) != 0);
        led2 = 0;

        xbscan.terminate();
        hostscan.terminate();
    }
}

// Thread to update host position
void hostScanner(void const *args)
{
    char nodeid[4] = "0";
    char ns, ew;
    int lock;
    int satsused;
    float hdop;
    float latitude, longitude;
    float altitude;
    float time;
    
    while (1) {
        // update.
        getline();
        if(sscanf(gpsmsg, "GPGGA,%f,%f,%c,%f,%c,%i,%i,%f,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &satsused, &hdop, &altitude) >= 1) {
            if((lock != 1) && (lock != 2) && (lock != 6)) {
                pc.printf("DNLK,%s,NOLOCK\n",nodeid, latitude, longitude, altitude);
            } else {
                double degrees;
                double minutes = modf(latitude/100.0f, &degrees);
                minutes = (minutes*100.0f)/60.0f;
                latitude = degrees + minutes;

                minutes = modf(longitude/100.0f, &degrees);
                minutes = (minutes*100.0f)/60.0f;
                longitude = degrees + minutes;

                if(ns == 'S') {
                    latitude  *= -1.0;
                }
                if(ew == 'W') {
                    longitude *= -1.0;
                }
                pc.printf("DLIN,%s,%f,%f,%f\n",nodeid, latitude, longitude, altitude);
            }
        }
    }
}

// Thread to scan the xbee
void xbeeScanner(void const *args)
{
    // Main runloop
    while (true) {
        // check if xbee is flagging data to be read
        while(xbee.attn() == 0) {
            // Read in data and run all background checksum and validation stuff
            if (xbee.readPacket(buffer) == 0) {
                pc.printf("%s", buffer);
            } else {
                pc.printf("Packet Skipped\n\r");
                xbee.clearBuff();
            }
        }
        // Give up rest of timeslice to boost that performance
        Thread::yield();
    }
}

void getline()
{
    while(gps.getc() != '$');    // wait for the start of a line
    for(int i=0; i<256; i++) {
        gpsmsg[i] = gps.getc();
        if(gpsmsg[i] == '\r') {
            gpsmsg[i] = 0;
            return ;
        }
    }
    error("Overflowed message limit");
}