mbed code Inductive_Sensor_Jasper for Bsc paper

Dependencies:   Bob DS1825 LDC1101 SDFileSystem mbed

Fork of Inductive_Sensor by Bob Giesberts

main.cpp

Committer:
bobgiesberts
Date:
2016-01-05
Revision:
4:ae441c5727b9
Parent:
2:1a203732fc95
Child:
5:736a81a59f3c

File content as of revision 4:ae441c5727b9:

#include "mbed.h"
#include "LDC1101.h"
#include "SDFileSystem.h"
#include "Bob.h"
#include <iostream>
#include <string>
using namespace std;

/**
* @file main.cpp
* @brief This file programs the processor for the inductive force sensor
* using the library LDC1101.h and LDC1101.cpp.
* - Red Led: processing communication with LDC1101
* - Green Led: processing SD card
* 
* Log protocol:
* -  1 minute  at 20 Hz
* - 29 minutes rest
*
* @author Bob Giesberts
*
* @date 2015-12-17
*/

Bob bob(PTB0, PTB1, PTC3, PTE0); // red led, green led, sd_enable, sd_present
Serial pc(USBTX, USBRX);

// battery
AnalogIn batt(PTC2);    // battery voltage: batt.read(); should be x.xV (min) < 3.7V (typ) < 4.22V (max)
float battery;

// timer variables
clock_t t;
uint32_t t_new = 0, t_old = 0;
int t_high = 0;
float seconds = 0;

// file variables
FILE *fp;
string filename = "/sd/data00.txt";
const char *fn;

int main(void){
    
    /** --- Connection to LDC1101 --- */
    pc.printf("Connect to LDC1101...");
    LDC1101 ldc(PTC6, PTC7, PTC5, PTC4, 120E-12, 16E6);  // mosi, miso, sck, cs, capacitor (F), f_CLKIN (Hz)
    pc.printf("success!\r\n");
    bob.flash(2);
    wait(0.2);

    /** --- Connection to sensor --- */
    pc.printf("Connect to sensor...");
    if(ldc.get_LHR_Data() != 16777215)
    {
        pc.printf("success!\r\n");
        bob.flash_red(2);
    }else{
        pc.printf("failed!\r\n");
        bob.red();
    }

    /** --- Start measuring! --- */
    /*
    while(1)
    {
        clock_t t = clock();
        int L = ldc.get_LHR_Data();
        pc.printf("%.2f;%d\n", t/100.0, L);
        wait(0.05); // 20 Hz
    }
    */


    // --- Connection to SD card --- 
    pc.printf("Connect to SD card...");
    if(bob.checkSD() == 1){
        pc.printf("success!\r\n");
        bob.flash_green(2);
    }else{
        pc.printf("failed!\r\n");
        bob.green();
    }
     
    // Load SD File system
    SDFileSystem SD(PTD6, PTD7, PTD5, PTD4, "sd");  // mosi, miso, sclk, cs, sd_name

    // Create a new data file
    pc.printf("Creating data file...");
    mkdir("/sd", 0777);   
    for(uint8_t i = 0; i < 100; i++) {
        filename[8] = i/10 + '0';    // arrays count from 0 so the number is 16 and 17
        filename[9] = i%10 + '0';
        fp = fopen( filename.c_str() , "r" );
        if( fp == NULL ) { // read failed so file does not exist
            fp = fopen( filename.c_str(), "w" ); // create it
            if( fp != NULL ){
                fn = filename.c_str();
                pc.printf("success! Filename: %s\r\n", fn);
                break;
            }else{
                pc.printf("failed!\r\n");
            }
        }else{ // file already exists
            fclose( fp );
        }
    }
    
    // Write current settings to data file
    fp = fopen( fn, "w" );
    fprintf(fp, "DIVIDER       :  %d\r\n",   ldc.get_divider());
    fprintf(fp, "RESPONSETIME  :  %d\r\n",   ldc.get_responsetime());
    fprintf(fp, "RP_MIN        :  %.2f\r\n", ldc.get_RPmin());
    fprintf(fp, "LHR_RCOUNT    :  %d\r\n",   ldc.get_Rcount());
    fprintf(fp, "\r\n\r\n");
    fclose(fp);
    
    // initialise timer
    t = clock();
    t_new = (uint32_t) t;
     
    while(1)
    {
        // Store one continuous stream of data
        for( int i = 0; i < 1200; i++  ) { // FOR LATER: record 60 seconds --> 60 * 20 Hz = 1200
        
            // Set the timer
            t_old = t_new;
            t = clock();
            t_new = (uint32_t) t;
            if( t_new < t_old ) t_high++; // clock_t overflows at 2^32 = 4294.967296 sec = 71 min
            seconds = ((float) t_new/100.0) + 4294.967296*(float)t_high;
            
            // get the sensor value
            int L = ldc.get_LHR_Data();
            
            // get the current battery level
            battery = (float)batt.read()*3.0*2.0;
        
            // if the sensor is connected
            if( L != 16777215 ) {
                           
                // Store data on SD card
                if( bob.checkSD() == 1 ) {
                    fp = fopen( fn, "a" );
                    fprintf( fp, "%.2f;%d;%.6f\r\n", seconds, L, battery );
                    fclose(fp);
                    
                    // pc.printf( "Time: %.2f sec; LHR_DATA: %d; Battery: %f V\r\n", seconds, L, battery );
                    
                    wait( 0.03 ); // Write takes about 0.02 --> 20 Hz --> 0.05 - 0.02 = 0.03s
                }else{
                    pc.printf("Error: SD card disconnected, please reinsert.\r\n");
                    bob.green();    
                }
              
            }else{
                pc.printf("Error: Sensor disconnected, please reconnect.\r\n");
                bob.red();
            }
        }
        
        // After measuring for a period of 60 seconds, wait for 29 minutes
        // While waiting, put every thing in lowest power modus
        bob.sleep();
        ldc.sleep(); 
        
        wait(1740); // FOR LATER: wait half an hour, 60 * 29 = 1740
        
        // Now wake up.
        ldc.wakeup();
        bob.wakeup();
    }
}