Alex Allen / Mbed 2 deprecated Balloon

Dependencies:   UM12 mbed

main.cpp

Committer:
AlexAllen
Date:
2012-03-07
Revision:
0:feaa05d35ccf

File content as of revision 0:feaa05d35ccf:

#include <fstream>
#include "mbed.h"
#include "SDFileSystem.h"
#include "GPS.h"
#include "BMP085.h"
#include "flash.h"
#include "HIH4030.h"
#include "UM12.h"

const float expmin = 8000.0;
const float expmax = 11000.0;

Serial pc(USBTX, USBRX);

Timer tmr;
HIH4030 humSens(p15);
BMP085 presSens(p28, p27);
SDFileSystem sd(p5, p6, p7, p8, "sd");
GPS gps(p13, p14);
UM12 radio(p9, p10);
DigitalInOut cam(p21);
DigitalOut jimShut(p19);
DigitalOut jimOpen(p20);

int main(void) 
{
    bool lock=false, open = false;
    float lat = 9001.0, lng = 9001.0, alt=-999.0, maxalt=-999.0, temp, pres, hum;
    int start, sat=0, loop=0;
    unsigned int time;
    char str[] = "Loading";

    // Set timer going
    tmr.start();
    start = tmr.read_us();
    
    // Ensure camera pin is set high
    cam.mode(OpenDrain);
    cam.output();
    cam=1;

    // Give obvious sign that program has started
    lightup();
    flash(3, 0.25);
    
    // Ensure Jim's thing is closed
    jimOpen = 0;
    jimShut = 1;
    wait(1.0);
    jimShut = 0;
    
    // Set up text file for recording data on sd card
    ofstream fout("/sd/data.txt");
    ofstream ferr("/sd/errors.txt");
    if(!fout)
    {
        errormsg1(0.25);
    }
    fout << "#Time Loop Lock Lat Long Altitude Pressure Temperature Humidity Sat" << endl;
    fout.close();
    ferr.close();
    
    // Wait for GPS lock 
    while(gps.lock!=1)
    {
        gps.sample();
        wait(0.1);
        flash1(0.25);
        radio.send('@');
        radio.send((int)strlen(str));
        for(int z=0; z<strlen(str); z++) radio.send(str[z]);
    }
    
    // Give all clear signal
    signal(0.25);
    flash(5, 0.25);
    
    while(1) 
    {      
        // Get time
        time = tmr.read_us() - start;
        
        // reopen file in append mode
        ofstream out("/sd/data.txt", fstream::app );
        
        gps.sample();
        
        // Probe GPS
        if(gps.lock==1)
        {
            lat = gps.latitude;
            lng = gps.longitude;
            alt = gps.altitude;
            sat = gps.satillites;
            lock = true;
            if(alt>maxalt) maxalt = alt;
        }
        else 
        {
            // Set up text for for recording errors on sd card
            ofstream err("/sd/errors.txt", fstream::app);
            err << "No GPS lock at time " << time << endl;
            err.close();
            
            lock = false;
        }
        
        // Probe temperature/pressure sensor
        presSens.update();
        pres = presSens.get_pressure();
        temp = presSens.get_temperature();
        
        // Probe humidity sensor 
        hum = humSens.getH(temp);
        
        // Record data on SD card (endl to ensure flushing)
        out << time << " " << loop << " " << lock << " " << lat << " " << lng << " ";
        out << alt << " " << pres << " " << temp << " " << hum << " " << sat << endl;
        out.close();
        
        // Decide whether or not to take a picture
        if(!(loop%5))
        {
            cam = 0;
        }
        if(!((loop-2)%5))
        {
            cam = 1;
        }
        
        // Decide if Jim's thing should be open or closed and make it so
        if(alt > expmin && alt < expmax && ((alt+3000)>maxalt))
        {
            jimShut = 0;
            jimOpen = 1;
            if(open) wait(0.1);
            else wait(1.0);
            jimOpen = 0;
            open = true;
        }
        else
        {
            jimOpen = 0;
            jimShut = 1;
            if(!open) wait(0.1);
            else wait(1.0);
            jimShut = 0;
            open = false;
        }
        
        // Send location over radio in the form "$<latitude>,<longitude>,<altitude>?"
        radio.send('$');
        radio.send(lat);
        radio.send(',');
        radio.send(lng);
        radio.send(',');
        radio.send(alt);
        radio.send(',');
        radio.send(lock);
        radio.send('?');
        
        loop++;
    }
    
    return 0;
}