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

Revision:
15:5de8394fdb3c
Parent:
13:2caedc56b863
Child:
16:b53721840a38
--- a/main.cpp	Tue Aug 23 08:31:15 2016 +0000
+++ b/main.cpp	Wed Aug 24 07:43:33 2016 +0000
@@ -1,19 +1,20 @@
 /**
 * @file main.cpp
 * @brief This file programs the processor for the inductive force sensor
-* using the library LDC1101.h and LDC1101.cpp.
-* - Led: processing communication with LDC1101
+* using the library LDC1614.h and LDC1614.cpp.
+* - Green Led: processing communication with LDC1614
+* - Red Led: error
 *
 * Log protocol:
-* -  0:30 minutes at 20 Hz
+* -  0:30 minutes at 13 Hz
 * - 14:30 minutes rest
 *
 * @author Bob Giesberts
 *
-* @date 2015-12-17
+* @date 2016-08-23
 */
 #include "mbed.h"
-#include "LDC1101.h"        // inductive force sensor
+#include "LDC1614.h"        // inductive force sensor
 #include "SDFileSystem.h"   // control the SD card
 #include "Bob.h"            // processorboard
 #include "DS1825.h"         // thermometer
@@ -26,25 +27,40 @@
 // SETTINGS
 bool DEBUG              = false;
 float C                 = 120E-12;      // Capacitor in F
+int sensors             = 2;            // number of attached sensors
 uint16_t INTERVAL_FIRST = 3600;         // First sampling time in seconds.  60:00 minutes = 3600 sec
 uint16_t INTERVAL_OFF   = 870;          // Waiting interval in seconds.     14:30 minutes = 870 sec (14*60+30)
 uint16_t INTERVAL_ON    = 30;           // Sampling interval in seconds.     0:30 minutes =  30 sec
 
-// Correct pinnames
-// PinName _LEDpin = PTB0;     // For the first sensor (a bright red led)
-// PinName _Tpin = PTB1;       // For the first sensor 
-PinName _LEDpin = PTB1;     // For all other sensors (a faint green led)
-PinName _Tpin = PTB0;       // For all other sensors
+// Leds
+PinName _LED_PROCESS = PTB1; // Green led (PTB0 for the first sensor) // old = new: PTB1
+PinName _LED_ERROR   = PTB0; // Red led // new: PTB0;
+
+// Thermometer
+PinName _Tpin        = PTC1; // OneWire system (PTB1 for the first sensor) // old = PTB0, new: PTC1
+
+// LDC1614
+PinName _LDC_SDA     = PTC5; // I2C: SDA
+PinName _LDC_SCL     = PTC4; // I2C: SCL
+PinName _LDC_SD      = PTC6; // Shutdown
+
+// SD File system
+PinName _SD_ENABLE   = PTC3;
+PinName _SD_PRESENT  = PTE0; // card detect // old = new: PTE0
+PinName _SD_MOSI     = PTD6; // mosi        // old = new: PTD6
+PinName _SD_MISO     = PTD7; // miso        // old = new: PTD7
+PinName _SD_SCLK     = PTD5; // sclk        // old = new: PTD5
+PinName _SD_CS       = PTD4; // cs          // old = new: PTD4
+
+// Other components
+PinName _OSCILLATOR  = PTC3;
+PinName _BATTERY     = PTC2;   // voltage   // old = new: PTC2
+PinName _BUTTON      = PTA4;
 
 
 // load libraries
-Bob bob( _LEDpin, // LED
-            PTC3, // sd_enable
-            PTE0, // sd_present
-            PTC2, // battery
-            PTE30 // 3V3_DET        TODO: can we use this to detect if we're charging the battery? If so, how should we respond?
-            );
-// Serial pc(USBTX, USBRX);
+Bob bob( _LED_PROCESS, _SD_ENABLE, _SD_PRESENT, _BATTERY, _BUTTON );            
+Serial pc(USBTX, USBRX);
 
 // timer variables
 uint32_t now = 0, next = 0, prev = 0;
@@ -71,6 +87,7 @@
     uint32_t    L[package_size];    // LHR_DATA
 } collected;
 uint8_t counter = 0;
+uint8_t status;
 
 // function to write all data to the SD card
 void storeit( float T )
@@ -127,11 +144,7 @@
     }   
     //fclose( fp ); // close file
     bob.ledoff();
-    */
-    
-    
-    
-    
+    */   
     
     // write data to SD card
     bob.ledon();
@@ -144,9 +157,6 @@
     //pc.printf( "%.2f;%d;%.2f;%.4f\r\n", (float) collected.t[counter-1]/100.0, collected.L[counter-1], bob.battery(), T ); // write to screen
     
     bob.ledoff();
-    
-
-    
                     
     // Reset data
     memset(collected.t, 0, counter);
@@ -156,6 +166,34 @@
 
 int main(void)
 {
+    if( DEBUG )
+    {
+        Serial pc( USBTX, USBRX );
+
+        LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, _OSCILLATOR, 16E6, sensors, C );
+        
+        uint8_t status;
+        
+        while( 1 ) {
+            // check the sensor
+            status = ldc.get_status();
+
+            // if there is an error turn on the red led
+            if( ldc.is_error( status ) )
+            {
+                // error
+                // bob.error();
+            }else{
+                if( ldc.is_ready( status ) )
+                {
+                    //bob.no_error();
+                    pc.printf( "sensor 1: %d | sensor 2: %d\r\n", ldc.get_Data( 0 ), ldc.get_Data( 1 ) );
+                }
+            }
+        }   
+    }    
+    
+    
 
     // TODO:
     // - implement this data conversion in writing to SD!!
@@ -166,9 +204,6 @@
     // float temp_a = 127.9375;
     // pc.printf( "A: %.4f (%08x); \r\n", temp_a, * (uint32_t *) &temp_a);
 
-   
-
-
     // Load SD File system
     // - the Raspberry Pie SD cards give a warning here that might just be ignored:
     //      "Not in idle state after sending CMD8 (not an SD card?)
@@ -176,12 +211,7 @@
     //       Set 512-byte block timed out"
     // TODO: buy 'better' SD cards from IAPC / the Stores (Kingston ...)
     bob.wakeup();
-    SDFileSystem *sd = new SDFileSystem(PTD6, // mosi
-                                        PTD7, // miso
-                                        PTD5, // sclk
-                                        PTD4, // cs
-                                        "sd"
-                                        );
+    SDFileSystem *sd = new SDFileSystem( _SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd" );
 
     // Create a new data file (data00.txt)
     mkdir("/sd", 0777);
@@ -227,24 +257,12 @@
             t_high++;                                                   // 0 -> 255         --> (255*4 294,96 s)    (12 days)
         t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high;  // 0 -> 219 901 952 --> (2 199 019,52 s)    (25 days)
         
-        // load libraries to take control over the communication        
-        LDC1101 *ldc = new LDC1101( PTC6,   // mosi
-                                    PTC7,   // miso
-                                    PTC5,   // sck
-                                    PTC4,   // cs
-                                    C,      // capacitor
-                                    16E6    // f_CLKIN
-                                    );      // load sensor (LDC1101) library (this takes 0.81 s)
-        DS1825 *thermometer = new DS1825( _Tpin );                          // load thermometer (DS1825) library
-        SDFileSystem *sd = new SDFileSystem(PTD6, // mosi
-                                            PTD7, // miso
-                                            PTD5, // sclk
-                                            PTD4, // cs
-                                            "sd"
-                                            );  // load SD system on
+        // load libraries to take control over the communication
+        LDC1614 *ldc = new LDC1614(_LDC_SDA, _LDC_SCL, _LDC_SD, _OSCILLATOR, 16E6, sensors, C);        
+        DS1825 *thermometer = new DS1825( _Tpin );                                        // load thermometer (DS1825) library
+        SDFileSystem *sd = new SDFileSystem(_SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd");  // load SD system
             mkdir("/sd", 0777); // select folder
 
-
         // How long should we takes samples?
         if( next == 0 )
         {
@@ -256,9 +274,6 @@
         // Take samples for INTERVAL_ON seconds
         while( t < next )
         {
-            // wait for new data to be ready
-            while( !ldc->is_New_LHR_data() ) { }
-
             // what time is it now?
             prev = now;
             now = (uint32_t) clock();
@@ -266,17 +281,31 @@
                 t_high++;
             t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high;
 
-            if( !ldc->is_Oscillation_Error() ) // sensor overloaded, this happends when the target is too close to the coil
+            status = ldc->get_status();
+
+            // Is there any error?
+            if( ldc->is_error(status) )
             {
-                // Store data in temporal memory
-                collected.t[counter] = t;
-                collected.L[counter] = ldc->get_LHR_Data();
-                counter++;
+                // red led on
+                // bob.error();
+            }else{  
+                // red led off
+                // bob.no_error();
+            
+                if( ldc->is_ready(status) ) // data from all sensors is ready
+                {
+                    // Store data in temporal memory
+                    collected.t[counter] = t;
+                    collected.L[counter] = ldc->get_Data( 0 );
+                    counter++;
+                    
+                    // Write a full package of data points to the SD card
+                    if( counter >= package_size-1  )
+                        storeit( thermometer->getTemperature() );
+                }
             }
-   
-            // Write a full package of data points to the SD card
-            if( counter >= package_size-1  )
-                storeit( thermometer->getTemperature() );
+            
+
         }
         
         // Write remaining data to the SD card