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-16
Revision:
5:736a81a59f3c
Parent:
4:ae441c5727b9
Child:
6:ff39d60061ca

File content as of revision 5:736a81a59f3c:

#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_MODE = false;
float C = 120E-12;          // pF
int INTERVAL_ON  = 2;       //   30 =       30 sec
int INTERVAL_OFF = 2;       // 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
clock_t t;
uint32_t t_new = 0, t_old = 0; // real time in PEE modus (48 MHz)
int t_high = 0;
uint32_t t_lost_new = 0, t_lost_old = 0; // lost time in BLPI modus (16 kHz)
int t_lost_high = 0;
float seconds = (float) 2.0 * INTERVAL_ON;
float t_period = 0;

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

// temporal storage for data samples
vector < float > secvector;
vector < uint32_t > Lvector;
float batt;

int main(void){



    /** --- Connection to LDC1101 --- */
    pc.printf("Connect to LDC1101...");
    LDC1101* ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
    pc.printf("success!\r\n");
    bob.flash(1);

    /** --- Connection to sensor --- */
    pc.printf("Connect to sensor...");
    while( !ldc->is_New_LHR_data() ){
       pc.printf(".");
    }
    // while( ldc.get_LHR_Data() == 0 ) { pc.printf(":"); bob.wakeup(); }        // sometimes this gives an error! TODO: fix the error!
    if( ldc->get_LHR_Data() != 16777215 ) {
        pc.printf(" success!\r\n");
        bob.flash_red(1);
    }else{
        pc.printf(" failed!\r\n");
        bob.red();
    }
    

    /**********************************/
    /*********** DEBUG MODUS **********/
    /**********************************/
    if( DEBUG_MODE )
    {
        while(1)
        {
            if( ldc->is_Oscillation_Error() ){
                pc.printf("Oscillation Error! \r\n");
                
            }else{
                while( !ldc->is_New_LHR_data() ) {
                    // pc.printf(".");
                }
                pc.printf("LHR_Data: %d, battery: %.4f\r\n", ldc->get_LHR_Data(), bob.battery());
            }
            wait( 0.1 );
        }
    }
    /**********************************/
    /**********************************/

    
    /** --- Connection to SD card --- */
    pc.printf("Connect to SD card...");
    while( bob.checkSD() != 1 ) { pc.printf("."); bob.green(); bob.wakeup(); }
    pc.printf("success!\r\n");
    bob.flash_green(1);
         
    // Load SD File system
    // SDFileSystem SD(PTD6, PTD7, PTD5, PTD4, "sd");  // mosi, miso, sclk, cs, sd_name
    SDFileSystem* sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
    
    // 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, "C             :  %f\r\n",   C);
    fprintf(fp, "\r\n\r\n");
    fclose(fp);
    
    delete ldc;
    delete sd;

   
    // initialise timer
    t = clock();
    t_new = (uint32_t) t;
     

    
    // Take samples for a period of INTERVAL_ON sec, then shut down everything and
    // sleep for a period of INTERVAL_OFF seconds.    
    while(1)
    {
        
        // wake up.
        pc.printf("waking up!\r\n\r\n");
        
        bob.wakeup();       // SD + Sensor on
        
        pc.printf("Connect to SD card...");
        while( bob.checkSD() != 1 ) { pc.printf("."); bob.green(); bob.wakeup(); }
        pc.printf("success!\r\n");
        bob.greenoff();
        
        // connection to LDC1101
        pc.printf("Connect to LDC1101...");
        LDC1101* ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
        pc.printf("succes!\r\n");
        
        // SD-card
        // BIG FAT ERROR RIGHT HERE!!!
        //                               //
        //                               //
        //       SDFileSystem gives an   //
        //       error: after power off  //
        //       everything seems wrong  //
        //                               //
        //                               //
        // BIG FAT ERROR RIGHT HERE!!!
        // SDFileSystem* sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
        // mkdir("/sd", 0777);
        // fp = fopen(fn, "a");
        pc.printf("Let's begin\r\n\r\n");
        // fprintf(fp, "Let's begin\r\n\r\n");
        // fclose(fp);
        
        
                
        // Add lost time to the counter
        t_lost_old = t_lost_new;
        t_lost_new += (INTERVAL_OFF);
        t_period = seconds + INTERVAL_OFF;
        
        while( seconds < t_period + INTERVAL_ON )
        {
            
            // Set the timer
            t_old = t_new;
            t = clock();
            t_new = (uint32_t) t;
            if( t_new < t_old ) t_high++; // uint32_t <clock_t> overflows at 2^32 = 4294.967296 sec = 71 min
            if( t_lost_new < t_lost_old ) t_lost_high++; // uint32_t <t_lost_new> overflows at 2^32 = 4294.967296 sec = 71 min
            
            seconds = ((float) t_new/100.0) + 4294.967296*(float)t_high + t_lost_new + 4294.967296*(float)t_lost_high;
            
            // check if sensor is not overloaded
            if(ldc->is_Oscillation_Error()){
                // Error: sensor overloaded
                
            }else{
                // wait until the next value is ready, this takes about 0.2 s?
                while(!ldc->is_New_LHR_data()) {
                    // pc.printf(".");
                    bob.red();
                }
                bob.redoff();
                        
                // get the sensor value
                int L = ldc->get_LHR_Data();
                
                // if the sensor is connected
                if( L != 0 && L != 16777215 ) {
                    
                    bob.green();
                    
                    // store sample data in RAM / flash memory
                    secvector.push_back( seconds );               
                    Lvector.push_back( L ); 
                    
                    // RAM reaches memory limit when vector size = 64
                    // 64 * 32bit * 2 = 512 bytes | KL25Z: 128 kb flash + 16 kb RAM
                    if( Lvector.size() > 31 )
                    {
                        // If SD-carddetect fails, try to wake it up setting enable to 1 and wait
                        while( bob.checkSD() != 1 ) { pc.printf("."); bob.green(); bob.wakeup(); }
                        
                        if( bob.checkSD() == 1 )
                        {
                            // no need to have accurate timestamp for battery level, just one sample for this period
                            batt = bob.battery();
                            
                            bob.red();
                            
                            // BIG FAT ERROR RIGHT HERE!!!
                            //                               //
                            //                               //
                            //       fopen gives a stupid    //
                            //              error            //
                            //                               //
                            //                               //
                            //                               //
                            // BIG FAT ERROR RIGHT HERE!!!
                            
                            /* debugging...
                            fp = fopen( fn, "r" );
                            if(fp == NULL)
                            {
                                pc.printf("r werkt niet \r\n;");
                            }else{
                                pc.printf("r werkt\r\n;");
                            }
                            fclose(fp);
                            wait (0.5);
                            */
                            
                            pc.printf("Write RAM memory to SD card...");
                            // fp = fopen( fn, "a" );
                            // for(uint8_t i = 0; i < Lvector.size(); i++)
                            // {
                            //     fprintf( fp, "%.2f;%d;%.6f\r\n", secvector.at(i), Lvector.at(i), batt );
                            // }
                            // fclose(fp);
                            pc.printf("success!\r\n");
                            bob.redoff();
                            
                            secvector.clear();
                            Lvector.clear();
                        }
                    }
                    
                    pc.printf( "[%d]: %.2f;%d;%.6f\r\n", secvector.size(), seconds, L, bob.battery() );
                    
                    bob.greenoff();
                        
                  
                }
                    
            }

        }

        // store remaining data before going to sleep
        if( bob.checkSD() == 1 && Lvector.size() != 0)
        {
            // no need to have accurate timestamp for battery level, just one sample for this period
            batt = bob.battery();
            
            bob.red();
            pc.printf("Write RAM memory to SD card...");
            // fp = fopen( fn, "a" );
            // for(uint8_t i = 0; i < Lvector.size(); i++)
            //     fprintf( fp, "%.2f;%d;%.6f\r\n", secvector[i], Lvector[i], batt );
            // fclose(fp);
            pc.printf("succes!\r\n");
            bob.redoff();    

        }
        secvector.clear();
        Lvector.clear();

        
        
        // After measuring for a period of INTERVAL_ON seconds, wait for INTERVAL_OFF minutes
        // While waiting, put everything in lowest power modus        
        pc.printf("zzz...\r\n\r\n");


        
        // SD-card: Kill SPI to SD card
        delete sd;
        DigitalOut sdP2(PTD4); sdP2 = 0; // cs
        DigitalOut sdP3(PTD6); sdP3 = 0; // mosi
        DigitalOut sdP5(PTD5); sdP5 = 0; // sck
        DigitalOut sdP7(PTD7); sdP7 = 0; // miso

        // LDC in SHUTDOWN mode
        ldc->sleep(); 

        // Sensor: Kill SPI to sensor        
        DigitalOut senP2(PTC7); senP2 = 0; // miso
        DigitalOut senP3(PTC5); senP3 = 0; // sck
        DigitalOut senP4(PTC6); senP4 = 0; // mosi
        DigitalOut senP5(PTC4); senP5 = 0; // cs

        // Power off SD + Sensor, KL25Z in deepsleep mode
        bob.sleep( INTERVAL_OFF );
    
            // RESULTS
            // 0.101 mA (zonder sensor, zonder SD)
            // 1.300 mA (zonder sensor, met SD)
            // 2.574 mA (met sensor, zonder SD)
            // 3.185 mA (met sensor, met SD)
    }
    
}