Final project for ECE 4180.

Dependencies:   GPS SDFileSystem mbed-rtos mbed MPL3115A2

Fork of GPS_HelloWorld by kris gjika

main.cpp

Committer:
cmiller86
Date:
2015-11-17
Revision:
4:ebf8c354c758
Parent:
3:b490294520d5
Child:
5:c3e1fc7fa00d

File content as of revision 4:ebf8c354c758:

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

#include "GPS.h"
#include "SDFileSystem.h"

#define PC_DEBUG

Serial pc(USBTX, USBRX);
SDFileSystem sd(p5, p6, p7, p8, "sd");
GPS gps(p9, p10);

AnalogIn temp1(p18);
AnalogIn temp2(p19);
AnalogIn temp3(p20);

DigitalIn dtmf(p11);

DigitalOut led1(LED1);
DigitalOut relay(p8);

bool attempted = false, cutdown = false;
float tempF1, tempF2, tempF3;

FILE *sdout;
Timer t;
Mutex log_mutex;

void init()
{
    t.start();
    
    led1  = 0;
    relay = 0;
    
    mkdir("/sd/weather_balloon", 0777);
    sdout = fopen("/sd/weather_balloon/log.txt", "w");
}

void update_gps(void const *args)
{
    while(true)
    {
        gps.sample();
        
        if(!gps.longitude)
            led1 = 1;
        
        Thread::wait(250);
    }
}

void update_temperature(void const *args)
{
    float tempC1, tempC2, tempC3;
    
    while(true)
    {
        tempC1 = ((temp1 * 3.3) - 0.600) * 100.0;
        tempC2 = ((temp2 * 3.3) - 0.600) * 100.0;
        tempC3 = ((temp3 * 3.3) - 0.600) * 100.0;
        tempF1 = (9.0 * tempC1) / 5.0 + 32;
        tempF2 = (9.0 * tempC2) / 5.0 + 32;
        tempF3 = (9.0 * tempC3) / 5.0 + 32;
        
        Thread::wait(250);
    }
}

void write_to_log(void const *args)
{
    while(true)
    {
        log_mutex.lock();
        
        fprintf(sdout, "----- %f -----\n\r", t.read());
        fprintf(sdout, "Long = %f\n\rLati = %f\n\r", gps.longitude, gps.latitude);
        fprintf(sdout, "Temp1 = %f\n\rTemp2 = %f\n\rTemp3 = %f\n\r", tempF1, tempF2, tempF3);
        fprintf(sdout, dtmf ? "DTMF = True\n\r" : "DTMF = False\n\r");
        
        #ifdef PC_DEBUG
        pc.printf("----- %f -----\n\r", t.read());
        pc.printf("Long = %f\n\rLati = %f\n\r", gps.longitude, gps.latitude);
        pc.printf("Temp1 = %f\n\rTemp2 = %f\n\rTemp3 = %f\n\r", tempF1, tempF2, tempF3);
        pc.printf(dtmf ? "DTMF = True\n\r" : "DTMF = False\n\r");
        #endif
        
        log_mutex.unlock();
        
        Thread::wait(1000);
    }
}

void check_cutdown(void const *args)
{
    while(true)
    {
        if(t.read() >= 20 || dtmf)
            cutdown = true;

        if(cutdown && !attempted)
        {
            log_mutex.lock();
            pc.printf("Cutdown Started = %f\n\r", t.read());
            fprintf(sdout, "Cutdown Started = %f\n\r", t.read());
            log_mutex.unlock();
            
            relay = 1;
            Thread::wait(200000);
            relay = 0;
            
            log_mutex.lock();
            pc.printf("Cutdown Ended = %f\n\r", t.read());
            fprintf(sdout, "Cutdown Ended = %f\n\r", t.read());
            log_mutex.unlock();
            
            attempted = true;
        }
        
        Thread::wait(100);
    }
}

int main()
{
    init();
    
    Thread gps_thread(update_gps);
    Thread temperature_thread(update_temperature);
    Thread log_thread(write_to_log);
    Thread cutdown_thread(check_cutdown);
}