Communication with LDC1101. Working version, ready for extensive calibration tests for resolution, linearity, etc.

Dependencies:   Bob DS1825 LDC1101 SDFileSystem mbed

Fork of Inductive_Sensor by Bob Giesberts

Revision:
2:1a203732fc95
Parent:
1:22c272515015
Child:
4:ae441c5727b9
--- a/main.cpp	Sat Dec 12 11:26:35 2015 +0000
+++ b/main.cpp	Fri Dec 18 15:50:58 2015 +0000
@@ -1,71 +1,148 @@
 #include "mbed.h"
 #include "LDC1101.h"
 #include "SDFileSystem.h"
+#include "Bob.h"
 #include <iostream>
 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
+*
+* @author Bob Giesberts
+*
+* @date 2015-12-17
+*/
 
-// Feedback on console
+Bob bob(PTB0, PTB1, PTC3, PTE0); // red led, green led, sd_enable, sd_present
 Serial pc(USBTX, USBRX);
 
-
-// Feedback with leds
-DigitalOut redled(PTB0); // Red
-DigitalOut greenled(PTB1); // Green
-void flash(int n){
-    for(int i = 0; i < n*2; i++){
-        redled = 1-redled;
-        greenled = 1-greenled;
-        wait(0.2);
-    }
-}
-
-
-// Main action
 int main(void){
     
-    pc.printf("5x knipperen...\n");
-    flash(5);
-    
+    /** --- 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 LDC1101
-    pc.printf("Contact maken met LDC1101...");
-    LDC1101 ldc(PTC6, PTC7, PTC5, PTC4, 120E-12, 6000000);  // mosi, miso, sck, cs, capacitor, f_CLKIN
-    pc.printf("success!\n");
+    /** --- 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
-    // Enable PTC3
-    DigitalOut sd_pin(PTC3);
-    sd_pin = 1;
-    // Load SD card
+    
+    
+    
+    /* let's leave this part out while calibrating the sensor
+
+    // --- Connection to SD card --- 
+    pc.printf("Connect to SD card...");
+    if(bob.checkSD() == 1){
+        pc.printf("success!\r\n");
+    }else{
+        pc.printf("failed!\r\n");
+    }
+     
+    // Load SD File system
     SDFileSystem SD(PTD6, PTD7, PTD5, PTD4, "sd");  // mosi, miso, sclk, cs, sd_name
-    // something with card detect?
-    // if(PTE0 == 1)
+
+    // Open or create data file
+    pc.printf("Opening data file...");
     mkdir("/sd/mydir", 0777);
-    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
-    if(fp == NULL) {
-        error("ERROR: Could not open file for write\n");
+    FILE *fp = fopen("/sd/data.txt", "w");
+    if(fp != NULL) {
+        pc.printf("success!\r\n");
+    }else{
+        pc.printf("failed!\r\n");
     }
-    fprintf(fp, "Start writing the SD card!\r\n");
+    
+    // Write current settings to data file
+    fprintf(fp, "DIVIDER       :  %d\r\n", ldc.get_divider());
+    fprintf(fp, "RESPONSETIME  :  %d\r\n", ldc.get_responsetime());
+    fprintf(fp, "RP_MIN        :  %f\r\n", ldc.get_RPmin());
+    fprintf(fp, "LHR_RCOUNT    :  %d\r\n", ldc.get_Rcount());
+    fprintf(fp, "\r\n\r\n");
     fclose(fp); 
 
+
+
     while(1)
-    {
-        // feedback with LEDs
-        flash(1);
-        
-        // Feedback in putty
-        pc.printf("%4.0f",ldc.get_raw_l());
-        pc.printf(", %f \r\n", 1000000*ldc.getInductance());
+    {       
+        clock_t t = clock();
+             
+        // get value from sensor
+        int L = ldc.get_LHR_Data();
         
-        // Store in SD_card
-        fprintf(fp, "Doet het!\r\n");
+        // if the sensor is connected
+        if(L != 16777215) {
+            
+            // settings
+            pc.printf("ResponseTime: %d", ldc.get_responsetime());
+            pc.printf(", Divider: %d", ldc.get_divider());
+            pc.printf(", RP_MIN: %.0f", ldc.get_RPmin());
+            pc.printf(", R_COUNT: %d", ldc.get_Rcount());
+            pc.printf(", Q: %.2f", ldc.get_Q());
+            pc.printf("\r\n");
+            
+            // results
+            pc.printf("Time = %.2f", t/100.0);
+            pc.printf(", L_DATA = %d", L);
+            pc.printf(", f_sensor = %.2f MHz", ldc.get_fsensor()/1000000);
+            pc.printf(", L = %.2f uH", 1000000*ldc.get_Inductance());
+            pc.printf("\r\n");
+            pc.printf("\r\n");
+            
+            
+            // feedback: it's working
+            bob.flash_red(1);
+            
+            
+            // Store data on SD card
+            if( bob.checkSD() == 1 ) {
+                fp = fopen("/sd/data.txt", "a");
+                //for(int i = 0; i < 100; i++){     Store one continuous stream of data, 30 seconds
+                    fprintf(fp, "%.2f, ", t/100.0);
+                    fprintf(fp, "%d\r\n", L);
+                //}
+                fclose(fp);
+            }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();
+        }
+
         
-        wait_ms(250);
+        // Telkens een periode van 10 - 30 seconden continue meten, dan een half uur pauze.
+        // In die pauze alles op laagste vermogen (LDC1101 in shutdown modus, SD uit, leds uit)
+        // ldc.sleep(); sd_pin = 0;
+        // wait(1800); // FOR LATER: wait half an hour, 60 * 30 = 1800
+        // ldc.wakeup(); sd_pin = 1;
+        
     }
-    
-    // On exit
-    fclose(fp);
-    
+    */
 }