Kenta Tanabe / TRP105F_Spline_for_child

Fork of TRP105F_Spline by Akifumi Takahashi

Revision:
4:701f958d137a
Parent:
3:b56e933bebc2
Child:
6:d2363b50aeaf
diff -r b56e933bebc2 -r 701f958d137a TRP105F_Spline.cpp
--- a/TRP105F_Spline.cpp	Wed Jun 01 05:38:46 2016 +0000
+++ b/TRP105F_Spline.cpp	Mon Jun 06 10:27:42 2016 +0000
@@ -1,15 +1,13 @@
 #define DEBUG
+//#define DEBUG2
 #include "TRP105F_Spline.h"
 
 //  To get ytage of TRP105F
-AnalogIn* g_Sensor_Voltage;
-//  To get ytage of TRP105F with spi
-//SPI spi(p5, p6, p7); // mosi(out), miso(in), sclk(clock)
-//DigitalOut cs(p8); // cs (the chip select signal)
+AnalogIn*   g_Sensor_Voltage;
 //  To get sample distance via seral com
-Serial g_Serial_Signal(USBTX, USBRX);
+Serial      g_Serial_Signal(USBTX, USBRX);
 
-LocalFileSystem local("local");  // マウントポイントを定義(ディレクトリパスになる)
+LocalFileSystem local("local");  // define Mount Point(which becomes Direcory Path)
 // for debug
 #ifdef DEBUG
 DigitalOut led1(LED1);
@@ -26,6 +24,11 @@
     _Sample_Set = (VDset *)malloc(_Sample_Num * sizeof(VDset));
     _u_spline   = (double*)malloc(_Sample_Num * sizeof(double));
     g_Sensor_Voltage = new AnalogIn(p16);
+
+    for(int i = 0; i < _Sample_Num; i++) {
+        _Sample_Set[i].x = _Sample_Set[i].y = 0;
+        _u_spline[i] = 0.0;
+    }
 }
 
 TRP105FS::TRP105FS(
@@ -39,6 +42,11 @@
     _Sample_Set = (VDset *)malloc(_Sample_Num * sizeof(VDset));
     _u_spline   = (double*)malloc(_Sample_Num * sizeof(double));
     g_Sensor_Voltage = new AnalogIn(p16);
+
+    for(int i = 0; i < _Sample_Num; i++) {
+        _Sample_Set[i].x = _Sample_Set[i].y = 0;
+        _u_spline[i] = 0.0;
+    }
 }
 
 TRP105FS::TRP105FS(
@@ -53,6 +61,11 @@
     _Sample_Set = (VDset *)malloc(_Sample_Num * sizeof(VDset));
     _u_spline   = (double*)malloc(_Sample_Num * sizeof(double));
     g_Sensor_Voltage = new AnalogIn(p16);
+
+    for(int i = 0; i < _Sample_Num; i++) {
+        _Sample_Set[i].x = _Sample_Set[i].y = 0;
+        _u_spline[i] = 0.0;
+    }
 }
 
 TRP105FS::TRP105FS(
@@ -68,6 +81,11 @@
     _Sample_Set = (VDset *)malloc(_Sample_Num * sizeof(VDset));
     _u_spline   = (double*)malloc(_Sample_Num * sizeof(double));
     g_Sensor_Voltage = new AnalogIn(pin);
+
+    for(int i = 0; i < _Sample_Num; i++) {
+        _Sample_Set[i].x = _Sample_Set[i].y = 0;
+        _u_spline[i] = 0.0;
+    }
 }
 
 //  Destructor
@@ -75,6 +93,7 @@
 {
     free(_Sample_Set);
     free(_u_spline);
+    delete g_Sensor_Voltage;
 }
 
 
@@ -136,8 +155,9 @@
         return cidx;
 }
 
-void TRP105FS::setSample(unsigned short arg_x, unsigned short arg_y){
-    _setSample(unsigned short arg_x, unsigned short arg_y);
+void TRP105FS::setSample(unsigned short arg_x, unsigned short arg_y)
+{
+    _setSample(arg_x, arg_y);
 }
 
 void TRP105FS::_setSample(unsigned short arg_x, unsigned short arg_y)
@@ -147,12 +167,16 @@
     int     tmp;
     VDset tmp_set[_ENUM];   // for bucket sort
 
-    // Increment it if call this function.
+    // Increment it if this function called.
     snum++;
+#ifdef DEBUG
+    g_Serial_Signal.printf("snum       : %d\n", snum);
+    g_Serial_Signal.printf("(%d,%d)\n",arg_x, arg_y);
+#endif
 
     //  fit to smaller
-    if ( _Sample_Num < snum) {
-        num = _Sample_Num;
+    if (snum < _Sample_Num) {
+        num = snum;
     } else {
         //  To reclloc memories if snum is bigger than _Sample_Num.
         //  When realloc is failed, snum is back to porevius.
@@ -165,12 +189,16 @@
         } else {
             snum--;
             num = snum = _Sample_Num ;
+#ifdef DEBUG
+            g_Serial_Signal.printf("failed to realloc\n", snum);
+#endif
         }
     }
 
     _Sample_Set[num - 1].x = arg_x;
     _Sample_Set[num - 1].y = arg_y;
-    if((unsigned short)_RIDX < _Sample_Set[num - 1].x)_Sample_Set[num - 1].x = (unsigned short)_RIDX;
+    if((unsigned short)_RIDX < _Sample_Set[num - 1].x)
+        _Sample_Set[num - 1].x = (unsigned short)_RIDX;
 
     //
     //  Sort set data array in distanceAscending order
@@ -179,7 +207,7 @@
     for(int i = 0; i < _ENUM; i++)
         tmp_set[i].x = 0xaaaa;
     tmp = 0;
-    for(int i = 0; i < _Sample_Num; i++) {
+    for(int i = 0; i < num; i++) {
         //  use x as index for x range [_LIDX,_RIDX]
         if (tmp_set[_Sample_Set[i].x].x == 0xaaaa) {
             tmp_set[_Sample_Set[i].x].x = _Sample_Set[i].x;
@@ -196,6 +224,7 @@
 #endif
     //  substruct tmp from number of sample.
     _Sample_Num -= tmp;
+    snum -= tmp;
 #ifdef DEBUG
     g_Serial_Signal.printf("-----------------\n");
     g_Serial_Signal.printf("  _Sample_num: %d\n", _Sample_Num );
@@ -241,7 +270,7 @@
                 }
             } while (!(sig == 0x0a || sig == 0x0d));
             g_Serial_Signal.putc('\n');
-        } else { //_Data_Input_Type == SYSTEM
+        } else {
             _Sample_Set[i].x = g_Serial_Signal.getc();
         }
 
@@ -468,8 +497,8 @@
 
     for(int i = 0; i < _ENUM; i++) {
         _Set[i].x     = i;
-        _Set[i].x     = _getSplineYof((double)(_Set[i].x));
-        _Threshold[i]   = _getSplineYof((double)(_Set[i].x) + 0.5);
+        _Set[i].y     = _getSplineYof((double)(_Set[i].x));
+        _Threshold[i] = _getSplineYof((double)(_Set[i].x) + 0.5);
 #ifdef DEBUG2
         g_Serial_Signal.printf("(get...threashold:%d)\n", _Threshold[i]);
 #endif
@@ -478,12 +507,19 @@
 
 void TRP105FS::calibrate()
 {
+#ifdef DEBUG2
+    g_Serial_Signal.printf("Sample Data Set\n");
+    for(int i = 0; i < _Sample_Num; i++)
+        g_Serial_Signal.printf("(%d,%d)\n", _Sample_Set[i].x, _Sample_Set[i].y);
+#endif
+
     _makeSpline();
 
     for(int i = 0; i < _ENUM; i++) {
         _Set[i].x     = i;
-        _Set[i].x     = _getSplineYof((double)(_Set[i].x));
-        _Threshold[i]   = _getSplineYof((double)(_Set[i].x) + 0.5);
+        _Set[i].y     = _getSplineYof((double)(_Set[i].x));
+        _Threshold[i] = _getSplineYof((double)(_Set[i].x) + 0.5);
+
 #ifdef DEBUG2
         g_Serial_Signal.printf("(get...threashold:%d)\n", _Threshold[i]);
 #endif
@@ -611,6 +647,7 @@
 
     sprintf(filepath, "/local/%s", filename);
     fp = fopen(filepath, "rb");
+    
     for(int i = 0; i < _ENUM; i++) {
 
         fread(&_Set[i].x,     sizeof(unsigned short), 1, fp);
@@ -655,9 +692,18 @@
     free(filepath);
 }
 
-void TRP105FS::printOutData()
+void TRP105FS::printOutData(
+    const char *filename)
 {
     FILE *fp;
+    char *filepath;
+    char tmp;
+    int fnnum = 0;
+
+    while (filename[fnnum] != 0) fnnum++;
+    filepath = (char *)malloc((fnnum + 8) * sizeof(char)); // "/local/" are 7 char and \0 is 1 char.
+    sprintf(filepath, "/local/%s", filename);
+    fp = fopen(filepath, "rb");
 
     fp = fopen("/local/log.txt", "w");  // open file in writing mode
     fprintf(fp, "x, y,(threshold)\n");
@@ -668,9 +714,11 @@
     for(int i = 0; i < _Sample_Num; i++) {
         fprintf(fp, "%d,%d\n", _Sample_Set[i].x, _Sample_Set[i].y);
     }
+    free(filepath);
     fclose(fp);
 
 }
+
 void TRP105FS::_printOutData(unsigned short *arg, int num, char* name)
 {
     FILE *fp;