Version 3.0: Switching to newer LDC1614 which is placed on the same PCB.

Dependencies:   Bob DS1825 LDC1614 LDC1101 SDFileSystem mbed

Fork of Inductive_Sensor by Bob Giesberts

main.cpp

Committer:
bobgiesberts
Date:
2016-01-25
Revision:
7:6c4cac1ec122
Parent:
6:ff39d60061ca
Child:
8:8cc1960467ae

File content as of revision 7:6c4cac1ec122:

#include "mbed.h"
#include "LDC1101.h"
#include "SDFileSystem.h"
#include "Bob.h"
#include <iostream>
#include <vector>
#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
*/

// SETTINGS
bool DEBUG = false;
float C = 120E-12;      // pF
int INTERVAL_ON  = 5;  //   30 =       30 sec
int INTERVAL_OFF = 5;  // 1770 = 29*60+30 sec

// load libraries
Bob bob(PTB0, PTB1, PTC3, PTE0, PTC2, PTE30); // red led, green led, sd_enable, sd_present, batt, 3V3_DET
Serial pc(USBTX, USBRX);

// timer variables
uint32_t now = 0, next = 0, prev = 0;
int t_high = 0;
uint32_t lost = 0, lost_prev = 0;
int lost_high = 0;

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

// temporal storage for data samples
vector < uint32_t > Svector;
uint32_t S;
vector < uint32_t > Lvector;
uint32_t L;
float batt;

uint8_t bla;

int main(void){

    if( DEBUG ){
        LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
        
        while(1){
            while( !ldc->is_New_LHR_data() ){ } // wait until data is ready
            
            pc.printf( "%d\r\n", ldc->get_LHR_Data() );
        }
    }



    bob.flash(2);

    // Load SD File system
    bob.SDon();
    SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
    
    // Create a new data file (data00.txt)
    mkdir("/sd", 0777);   
    for(uint8_t i = 0; i < 100; i++) {
        filename[8] = i/10 + '0';
        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();
                fclose( fp );
                break;
            }else{
                bob.red();
            }
        }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, "C             :  %f\r\n",   C);
    // fprintf(fp, "\r\n\r\n");
    // fclose(fp);
    
    // Unload SD File system
    delete sd; sd = NULL;



    while(1)
    {        
        // SD on + sensor on
        bob.SDon();

        // Sensor on        
        LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
        
        // SD on
        SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd"); 
        mkdir("/sd", 0777); // select folder  
        
        // clock
        now  = (uint32_t) clock();
        next = (uint32_t) now + INTERVAL_ON*100;
        
        // Take samples for INTERVAL_ON seconds
        while( (uint32_t) clock() < next )
        {
            // Collect a package 16 data points
            while( Lvector.size() < 16 ) // Write is per 512 bits: 16 * 32 = 512
            {
                // wait for new data to be ready
                while( !ldc->is_New_LHR_data() ) { }
                
                if( !ldc->is_Oscillation_Error() ){ // sensor not overloaded
                    // time
                    prev = now;
                    now = (uint32_t) clock();
                    if( now < prev ) t_high++;
                    S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
                    Svector.push_back( S );
                    
                    // induction
                    L = ldc->get_LHR_Data();
                    Lvector.push_back( L );                    
                }
            }
            // battery level
            batt = bob.battery();
    
            // Store the package of 16 data points
            pc.printf( "Writing %d samples of data to SD card: Time = %.2f; Inductance = %d; Battery = %.4f\r\n", Lvector.size(), (float) Svector.at(15)/100.0, Lvector.at(15), batt );
            
            bob.green();
            fp = fopen( fn, "a" ); // open file
            // if( fp == NULL ) { pc.printf("error (append)...\r\n"); }
            for( int i = 0; i < Lvector.size(); i++ )
                fprintf( fp, "%.2f;%d;%.4f\r\n", (float) Svector.at(i)/100.0, Lvector.at(i), batt ); // write to file
            fclose( fp ); // close file
            bob.greenoff();
            
            // Release data
            Lvector.clear();
            Svector.clear();
            
        }               
        
        // SD off
        delete sd; sd = NULL;
        bob.SDoff();
        DigitalOut *sdP2 = new DigitalOut(PTD4); sdP2->write(0); delete sdP2; sdP2 = NULL;// cs
        DigitalOut *sdP3 = new DigitalOut(PTD6); sdP3->write(0); delete sdP3; sdP3 = NULL;// mosi
        DigitalOut *sdP5 = new DigitalOut(PTD5); sdP5->write(0); delete sdP5; sdP5 = NULL;// sck
        DigitalOut *sdP7 = new DigitalOut(PTD7); sdP7->write(0); delete sdP7; sdP7 = NULL;// miso 
        
        // Sensor off
        delete ldc; ldc = NULL;
        DigitalOut *senP2 = new DigitalOut(PTC7); senP2->write(0); delete senP2; senP2 = NULL; // miso
        DigitalOut *senP3 = new DigitalOut(PTC5); senP3->write(0); delete senP3; senP3 = NULL; // sck
        DigitalOut *senP4 = new DigitalOut(PTC6); senP4->write(0); delete senP4; senP4 = NULL; // mosi
        DigitalOut *senP5 = new DigitalOut(PTC4); senP5->write(0); delete senP5; senP5 = NULL; // cs
        
        pc.printf( "zzz...\r\n\r\n" );
        
        // Sleep for INTERVAL_OFF samples
        bob.sleep( (float) INTERVAL_OFF - (clock() - next)/100.0 );
        
        // Add lost time to the counter
        lost_prev = lost;
        lost += (INTERVAL_OFF)*100;
        if( lost < lost_prev ) lost_high++;
    }

}