The official code that runs on the FRDM board for the chlorine sensor.

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed GSM_Library

Fork of DCS by Brandon Crofts

Sensor.cpp

Committer:
bjcrofts
Date:
2015-03-06
Revision:
1:8614e190908b
Parent:
0:d7b2716c5a4f
Child:
2:5c0513ab856e
Child:
8:6b4a6bcd7694

File content as of revision 1:8614e190908b:

#pragma once
#include "mbed.h"
#include "math.h"
#include "MBed_Adafruit_GPS.h"
#include "SDFileSystem.h"
#include "QAM.h"
#include "param.h"

DigitalOut led_red(LED_RED);
Serial pc(USBTX, USBRX);
Timer t;

/**************************************************
 **          SD FILE SYSTEM                       **
 **************************************************/
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
FILE *fp;

/**************************************************
 **          GPS                                 **
 **************************************************/
Serial * gps_Serial;

/**************************************************
 **          SENSOR INPUTS                       **
 **************************************************/
AnalogIn AnLong(A0);
AnalogIn AnShort(A1);
AnalogIn AnRefLong(A2);
AnalogIn AnRefShort(A3);
Ticker sample_tick;
bool takeSample = false;
void tick(){takeSample = true;}

/**************************************************
 **          SIN OUTPUT                          **
 **************************************************/
AnalogOut dac0(DAC0_OUT);
int sinRes = (int)1/(CARRIER_FREQ*TIME_CONST);
float sinWave[SIN_LENGTH] = {};
int sinIndex = 0;


/**************************************************
 **          QAM                                 **
 **************************************************/
float sLQ[SAMPLE_LENGTH] = {};
float sLI[SAMPLE_LENGTH] = {};
float sSQ[SAMPLE_LENGTH] = {};
float sSI[SAMPLE_LENGTH] = {};
float sRefLQ[SAMPLE_LENGTH] = {};
float sRefLI[SAMPLE_LENGTH] = {};
float sRefSQ[SAMPLE_LENGTH] = {};
float sRefSI[SAMPLE_LENGTH] = {};

float Iarray[SAMPLE_LENGTH] = {};
float Qarray[SAMPLE_LENGTH] = {};
int sampleIndex = 0;
float I = 0;
float Q = 0;
float lon = 0;
float lonRef = 0;
float shor = 0;
float shorRef = 0;

void buildIQ(){
    for(int i = 0; i < SAMPLE_LENGTH; i++){
        Iarray[i] = cos(2*PI*CARRIER_FREQ*i*TIME_CONST);
        Qarray[i] = -sin(2*PI*CARRIER_FREQ*i*TIME_CONST);
    }
}

void create_sinWave(){
    int i = 0;
    for(i = 0; i < sinRes; i++){
        sinWave[i] = 0.25 * sin(2.0*PI*i/sinRes) + 0.75;
    }
}

int main () {
    
    pc.baud(115200);
    pc.printf("hello\r\n");
    
    // GPS INITIALIZATION //////////////////////////////
    gps_Serial = new Serial(D1,D0);
    Adafruit_GPS myGPS(gps_Serial);
    char c;
    myGPS.begin(9600);
    myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
    myGPS.sendCommand(PGCMD_ANTENNA);
    ////////////////////////////////////////////////////
    
    buildIQ();
    create_sinWave();
    
    float filteredLong = 0;
    float filteredShort = 0;
    float filteredLongRef = 0;
    float filteredShortRef = 0;
    
    sample_tick.attach(&tick, 0.0001);
    
    t.start();
    
    while(1){
        
        if(takeSample){ //3.46 us per loop
            
            takeSample = false;
            dac0 = sinWave[sinIndex];
            sinIndex++;
            if((sinIndex+1) > sinRes){
                sinIndex = 0;
            }
        
            
            lon = AnLong.read();
            lonRef = AnRefLong.read();
            shor = AnShort.read();
            shorRef = AnRefShort.read();
            
            I = Iarray[sampleIndex];
            Q = Qarray[sampleIndex];
            sLI[sampleIndex] = lon*I;
            sLQ[sampleIndex] = lon*Q;
            sSI[sampleIndex] = shor*I;
            sSQ[sampleIndex] = shor*Q;
            sRefLI[sampleIndex] = lonRef*I;
            sRefLQ[sampleIndex] = lonRef*Q;
            sRefSI[sampleIndex] = shorRef*I;
            sRefSQ[sampleIndex] = shorRef*Q;
            
            sampleIndex++;
            if(sampleIndex+1 > SAMPLE_LENGTH){
                sampleIndex--;
            }
        }
        
        
        
        if(sampleIndex+2 > SAMPLE_LENGTH){ //0.50 seconds
            
            sampleIndex = 0;
            filteredLong = QAM(sLI, sLQ, &pc);
            filteredLongRef = QAM(sRefLI, sRefLQ, &pc);
            filteredShort = QAM(sSI, sSQ, &pc);
            filteredShortRef = QAM(sRefSI, sRefSQ, &pc);
        }  
        
        c = myGPS.read();
        if ( myGPS.newNMEAreceived() ) {
            if ( !myGPS.parse(myGPS.lastNMEA()) ) {
                continue;
            }
        }


        if(t.read_ms()>1000){
            led_red = !led_red;
            
            pc.printf("Long = %f\r\n", filteredLong);
            pc.printf("LongRef = %f\r\n", filteredLongRef);
            pc.printf("Short = %f\r\n", filteredShort);
            pc.printf("ShortRef = %f\r\n", filteredShortRef);
            pc.printf("Time: %d:%d:%d \r\n", myGPS.hour, myGPS.minute, myGPS.seconds);
            
            
            if (myGPS.fix) pc.printf("Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
            else           pc.printf("No GPS fix\r\n");
            pc.printf("--------------------------------\r\n");
            
            fp = fopen("/sd/data.txt", "w");
            if (fp != NULL){
                
                fprintf(fp, "Long = %f\r\n", filteredLong);
                fprintf(fp, "LongRef = %f\r\n", filteredLongRef);
                fprintf(fp, "Short = %f\r\n", filteredShort);
                fprintf(fp, "ShortRef = %f\r\n", filteredShortRef);
                fprintf(fp, "Time: %d:%d:%d \r\n", myGPS.hour, myGPS.minute, myGPS.seconds);
                if (myGPS.fix) fprintf(fp, "Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
                else           fprintf(fp, "No GPS fix\r\n");
                
                fclose(fp);    
            }
            
            t.reset();
        }
        
        
        
        
        
        /*
        if (sampleIndex+2 > SAMPLE_LENGTH) { // 0.25 seconds
            
            sampleIndex = 0;
            pc.printf("Time: %d:%d:%d \r\n", myGPS.hour, myGPS.minute, myGPS.seconds);
            fp = fopen("/sd/data.txt", "w");
            if (fp != NULL){
                fprintf(fp,"Time: %d:%d:%d.%u \r\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
                fclose(fp);
            }

            if (myGPS.fix) {
                pc.printf("Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
                led_red = !led_red;
                fp = fopen("/sd/data.txt", "w");
                if (fp != NULL){
                    fprintf(fp,"Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
                    fclose(fp);
                }
            }else{
                fprintf(fp,"no gps fix\r\n");
                fclose(fp);
            }    
        }
        */
    }
}