Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Bob DS1825 LDC1101 SDFileSystem mbed
Fork of Inductive_Sensor by
Diff: main.cpp
- Revision:
- 11:599ca9982e45
- Parent:
- 10:3cab80866536
- Child:
- 12:cceece4f3afb
--- a/main.cpp Wed Feb 24 16:26:37 2016 +0000
+++ b/main.cpp Wed Mar 30 12:01:01 2016 +0000
@@ -14,25 +14,22 @@
* @date 2015-12-17
*/
#include "mbed.h"
-#include "LDC1101.h"
-#include "SDFileSystem.h"
-#include "Bob.h"
-
-#include "OneWire.h"
+#include "LDC1101.h" // inductive force sensor
+#include "SDFileSystem.h" // control the SD card
+#include "Bob.h" // processorboard
+#include "DS1825.h" // thermometer
#include <iostream>
#include <vector>
#include <string>
using namespace std;
-
-
-
// SETTINGS
bool DEBUG = false;
float C = 120E-12; // pF
-int INTERVAL_ON = 30; // 30 = 30 sec
-int INTERVAL_OFF = 870; // 1770 = 29*60+30 sec
+uint16_t INTERVAL_FIRST = 600; // 5 sec
+uint16_t INTERVAL_OFF = 870; // 1770 = 29*60+30 sec
+uint16_t INTERVAL_ON = 30; // 30 = 30 sec
// load libraries
Bob bob(PTB0, PTB1, PTC3, PTE0, PTC2, PTE30); // red led, green led, sd_enable, sd_present, batt, 3V3_DET
@@ -41,103 +38,66 @@
// timer variables
uint32_t now = 0, next = 0, prev = 0;
uint8_t t_high = 0;
-uint32_t lost = 0, lost_prev = 0;
+uint32_t t_lost = 0, lost_prev = 0;
uint8_t lost_high = 0;
-uint16_t t_sleep = 0;
+uint32_t t_sleep = 0;
// file variables
FILE *fp;
string filename = "/sd/data00.txt";
const char *fn;
+// temperature variables
+PinName _Tpin = PTB1; // Thermometer is connected to the original pin for the RED / GREEN LED
+uint8_t Tdata[9];
+int16_t T_bin;
+float T;
+
// temporal storage for data samples
-vector < uint32_t > Svector;
-uint32_t S;
-vector < uint32_t > Lvector;
-uint32_t L;
+const uint8_t package_size = 16; // Write is per 512 bits: 16 * 32 = 512
+uint32_t tarray[package_size];
+uint32_t t;
+uint32_t Larray[package_size];
float batt;
-float f;
-
-float T;
-BYTE address[8];
-BYTE Tdata[9];
-
int main(void)
{
-
-
-
- if( DEBUG ) {
+ if( DEBUG )
+ {
LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
- OneWire *onewire = new OneWire( PTB1 );
-
-
+ DS1825 *thermometer = new DS1825( _Tpin );
wait(1);
-
-
- while(1) {
+ while(1)
+ {
while( !ldc->is_New_LHR_data() ) { } // wait until data is ready
-
- L = ldc->get_LHR_Data();
-
- // initialization of the Thermometer
- /*
- onewire.reset();
- onewire.write( 0x33 ); // READ ROM
- for (int i = 0; i < 8; i++)
- address[i] = onewire.read();
- if( address[7] != onewire.crc8(address, 7) )
- pc.printf("Address CRC not valid!\r\n");
- pc.printf("\r\n");
- // wait_ms( 750 );
- */
- // Temperature conversion
- onewire->reset();
- onewire->write( 0xCC );
- onewire->write( 0x44, 1 );
- wait_ms( 750 );
-
- // read scratchpad
- onewire->reset();
- onewire->write( 0xCC );
- onewire->write( 0xBE );
- for (int i = 0; i < 9; i++) Tdata[i] = onewire->read();
- if( Tdata[8] != onewire->crc8(Tdata, 8) ) pc.printf("Data CRC not valid!\r\n");
-
- T = ((Tdata[1] << 8) | Tdata[0]) / 16.0;
-
- pc.printf("L: %d; T: %.4f C", L, T);
- //pc.printf( "%d", L );
+ // T = thermometer->getTemperature();
+ pc.printf( "%d", ldc->get_LHR_Data() );
//pc.printf( "%.3f MHz", ldc->get_fsensor()/1000000 );
- //pc.printf( "%.3f KHz", (f - ldc->get_fsensor())/1000 );
-
pc.printf( "\r\n" );
wait( 1 ); // 20 Hz
}
}
-
+ // Provide some feedback, is it working?
+ //bob.flash(2);
- bob.flash(2);
- pc.printf( "check sd: %d\r\n", bob.checkSD() );
// Load SD File system
-
-
- // bob.wakeup();
- bob.SDon();
+ bob.wakeup();
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++) {
+ 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
+ if( fp == NULL ) // read failed so file does not exist
+ {
fp = fopen( filename.c_str(), "w" ); // create it
- if( fp != NULL ) {
+ if( fp != NULL )
+ {
fn = filename.c_str();
fclose( fp );
break;
@@ -148,155 +108,135 @@
fclose( fp );
}
}
+ if( fp == NULL )
+ {
+ pc.printf( "/sd/data00.txt t/m /sd/data99.txt already exist!\n" );
+ }
// 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);
+ while(1)
+ {
+ // Wakeup: SD + LDC1101 on
+ bob.wakeup();
- // SD on
- SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
- mkdir("/sd", 0777); // select folder
-
- // time
+ // what time is it now?
prev = now; // 0 -> 429 496 --> (4 294,96 s) (71 min)
now = (uint32_t) clock(); // 0 -> 429 496 --> (4 294,96 s) (71 min)
- if( now < prev ) t_high++; // 0 -> 255 --> (255*4 294,96 s) (12 days)
- S = now + 429496.7296*t_high + lost + 429496.7296*lost_high; // 0 -> 219 901 952 --> (2 199 019,52 s) (25 days)
+ if( now < prev )
+ 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, PTC7, PTC5, PTC4, C, 16E6); // load sensor (LDC1101) library (this takes 0.81 s)
+ DS1825 *thermometer = new DS1825( _Tpin ); // load thermometer (DS1825) library
+ SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd"); // load SD system on
+ mkdir("/sd", 0777); // select folder
// first time take a comfortably long period
- if( next == 0 ) {
- next = S + (INTERVAL_ON + INTERVAL_OFF)*100;
+ if( next == 0 )
+ {
+ next = t + INTERVAL_FIRST*100;
} else {
- next = S + INTERVAL_ON*100; // 0 -> 219 904 952 --> (2 199 049,52 s) (25 days)
+ next = t + INTERVAL_ON*100; // 0 -> 219 904 952 --> (2 199 049,52 s) (25 days)
}
// Take samples for INTERVAL_ON seconds
- while( S < next ) {
- // Collect a package 16 data points
- while( Lvector.size() < 16 ) { // Write is per 512 bits: 16 * 32 = 512
+ while( t < next )
+ {
+
+ // Collect a package of data points
+ int i = 0;
+ while( i < package_size )
+ {
// 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;
+ // what time is it now?
+ prev = now;
+ now = (uint32_t) clock();
+ if( now < prev )
+ t_high++;
+ t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high;
- // induction
- L = ldc->get_LHR_Data();
-
+ if( !ldc->is_Oscillation_Error() ) // sensor not overloaded, this happends when the target is too close to the coil
+ {
// Store data in temporal memory
- Svector.push_back( S );
- Lvector.push_back( L );
+ tarray[i] = t;
+ Larray[i] = ldc->get_LHR_Data();
+ i++;
}
}
+
+ // get battery level
+ batt = bob.battery();
+
+ // get temperature
+ T = thermometer->getTemperature();
- // battery level
- batt = bob.battery();
+ // Store the package of data points
+ // TODO: create a queue with all the data (t, L, Vbatt, T) --> (Queue<data> queue)
+ // TODO: process this data in packages of 16 in a seperate Thread --> (RTOS?)
+ bob.red();
+ fp = fopen( fn, "a" ); // open file (append)
+ for( int i = 0; i < package_size; i++ )
+ fprintf( fp, "%.2f;%d;%.4f;%.4f\r\n", (float) tarray[i]/100.0, Larray[i], batt, T ); // write to file
+ fclose( fp ); // close file
+ bob.redoff();
- // temperature
- OneWire *onewire = new OneWire( PTB1 );
- onewire->reset();
- onewire->write( 0xCC );
- onewire->write( 0x44, 1 );
- wait_ms( 750 );
- onewire->reset();
- onewire->write( 0xCC );
- onewire->write( 0xBE );
- for (int i = 0; i < 9; i++) Tdata[i] = onewire->read();
- if( Tdata[8] != onewire->crc8(Tdata, 8) ) pc.printf("Data CRC not valid!\r\n");
- T = ((Tdata[1] << 8) | Tdata[0]) / 16.0;
- onewire->depower();
- delete onewire;
- onewire = NULL;
+
+
+ // Print to PC for debugging
+ pc.printf( "%.2f;%d;%.4f;%.4f\r\n", (float) tarray[0]/100.0, Larray[0], batt, T ); // write to console
+
+ // Reset data
+ memset(tarray, 0, package_size);
+ memset(Larray, 0, package_size);
- // Store the package of 16 data points
- bob.red();
- fp = fopen( fn, "a" ); // open file
- for( int i = 0; i < Lvector.size(); i++ )
- fprintf( fp, "%.2f;%d;%.4f;%.4f\r\n", (float) Svector.at(i)/100.0, Lvector.at(i), batt, T ); // write to file
- fclose( fp ); // close file
- bob.redoff();
-
- pc.printf("%.2f;%d;%.4f;%.4f\r\n", (float) Svector.at(0)/100.0, Lvector.at(0), batt, T ); // write to console
-
-
- // Release data
- Lvector.clear();
- Svector.clear();
-
+ // if the battery is almost empty (Vbatt < 3.10 V), the termometer stops
+ // working well and L can not be corrected. So just shut down
+ if( batt < 3.10f )
+ {
+ bob.red();
+ exit(0);
+ }
+
}
- // 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
+ // prepare for sleep, power down the SD and the LDC1101
+ delete sd; sd = NULL;
+ delete ldc; ldc = NULL;
+ delete thermometer; thermometer = NULL;
+ bob.greenoff(); // in current system the thermometer is connected to the old green led. Turning it off might power it down? Well 1,1V remains. So this doesn't work as expected
+ bob.beforesleep();
- // 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
-
-
+ // what time is it now?
+ prev = now;
+ now = (uint32_t) clock();
+ if( now < prev )
+ t_high++;
+ t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high;
// Calculate sleeping time (correction to INTERVAL_OFF)
- prev = now;
- now = (uint32_t) clock();
- if( now < prev ) t_high++;
- S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
-
- t_sleep = ( INTERVAL_OFF*100 - (S - next) );
+ // S has passed the limit of next. Those few ms should be substracted from INTERVAL_OFF.
+ t_sleep = INTERVAL_OFF*100 - (t - next);
// Add lost time to the counter
- lost_prev = lost;
- lost += t_sleep;
- if( lost < lost_prev ) lost_high++;
+ lost_prev = t_lost;
+ t_lost += t_sleep;
+ if( t_lost < lost_prev )
+ lost_high++;
- // Sleep now...
- // pc.printf( "zzz... (%f) \r\n\r\n", (float) (t_sleep / 100.0) );
- bob.sleep( (float) ( t_sleep / 100.0) );
+ // Sleep now (ms), enter low power mode
+ bob.sleep( t_sleep * 10 );
}
}
+
+
+
