This lib is supposed to be used as a sensor's calibration or control program. This makes Cubic Spline Model from some sample plots(sets of (value, voltage)), and then discretize the model (dividing the range of voltage into some steps) in order to use the calibrated model data without getting the INVERSE function.

Committer:
aktk
Date:
Wed Jun 08 11:32:29 2016 +0000
Revision:
16:c2b0f3ca63dd
Parent:
14:a99bf22b919d
Child:
17:f6ec7796dafd
getDistance(any), getVoltage() have come to be inline function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:e85788b14028 1 /**
aktk 0:e85788b14028 2 * TRP105F_Spline.h,.cpp
aktk 0:e85788b14028 3 *
aktk 2:40ae18445079 4 * Author: aktk, aktk.j.uec@gmail.com
aktk 2:40ae18445079 5 * Tokyo, Japan.
aktk 2:40ae18445079 6 *
aktk 2:40ae18445079 7 * - This library is for providing a distance from TRP105F, photo-reflect proximity sensor,
aktk 0:e85788b14028 8 * to whatever reflect the flash.
aktk 2:40ae18445079 9 * - Deistance is derived as a voltage signal of TRP105F.
aktk 0:e85788b14028 10 * - An object of TRPFS class has an array of sets of
aktk 0:e85788b14028 11 * distance(16 bit-value) and voltage(16bit-value) at the distance.
aktk 2:40ae18445079 12 * - The range of distances that an array has in sets is [0:255] by 1.
aktk 2:40ae18445079 13 * - an array is derived from a cubic spline curve model.
aktk 0:e85788b14028 14 * - In order to derive spline curve, some value sets should be got
aktk 0:e85788b14028 15 * at the first calibration.
aktk 0:e85788b14028 16 *
aktk 0:e85788b14028 17 * LOG:
aktk 0:e85788b14028 18 * ver.1 2015/10/19 - 2015/10/22
aktk 0:e85788b14028 19 * ver.2 2015/10/22 -
aktk 0:e85788b14028 20 * Distance data type has become unsigned short from int.
aktk 0:e85788b14028 21 * Distance data range : [2:20] by 1 -> [0:1024] by 1.
aktk 2:40ae18445079 22 * ver.2.1 2016/02.12 - 2016/02/16
aktk 1:2053662b1167 23 * Distance data range : [0:1024] -> [0:255].
aktk 11:d60fb729eacf 24 * ver.3.0 2016/05.23 -
aktk 0:e85788b14028 25 */
aktk 11:d60fb729eacf 26
aktk 11:d60fb729eacf 27 #ifndef TRP105F_SPLINE_H
aktk 11:d60fb729eacf 28 #define TRP105F_SPLINE_H
aktk 0:e85788b14028 29
aktk 9:ec1ee4a6b6a4 30
aktk 0:e85788b14028 31 #include "mbed.h"
aktk 11:d60fb729eacf 32 #include "CODE_SELECTIVE.h"
aktk 0:e85788b14028 33
aktk 3:b56e933bebc2 34
aktk 0:e85788b14028 35 // Set of voltage-distance
aktk 0:e85788b14028 36 typedef struct {
aktk 3:b56e933bebc2 37 unsigned short x; // distance
aktk 3:b56e933bebc2 38 unsigned short y; // voltage
aktk 0:e85788b14028 39 } VDset;
aktk 0:e85788b14028 40
aktk 1:2053662b1167 41 // Type of modality to input data for calibration
aktk 3:b56e933bebc2 42 enum UseType {
aktk 3:b56e933bebc2 43 AsDEBUG,
aktk 3:b56e933bebc2 44 AsMODULE
aktk 0:e85788b14028 45 } ;
aktk 0:e85788b14028 46
aktk 0:e85788b14028 47 //
aktk 0:e85788b14028 48 // TRP105FS Class for get distance from voltage
aktk 2:40ae18445079 49 //
aktk 0:e85788b14028 50 class TRP105FS
aktk 0:e85788b14028 51 {
aktk 0:e85788b14028 52 public:
aktk 1:2053662b1167 53 // Constraction
aktk 0:e85788b14028 54 TRP105FS();
aktk 0:e85788b14028 55 TRP105FS(unsigned int);
aktk 3:b56e933bebc2 56 TRP105FS(unsigned int, UseType);
aktk 10:b50e4bb40571 57 TRP105FS(unsigned int, PinName);
aktk 3:b56e933bebc2 58 TRP105FS(unsigned int, UseType, PinName);
aktk 3:b56e933bebc2 59 //TRP105FS(unsigned int arg_num, DataInType arg_dit, unsigned int channel);
aktk 1:2053662b1167 60 // Destraction
aktk 0:e85788b14028 61 ~TRP105FS();
aktk 1:2053662b1167 62 // Functions
aktk 3:b56e933bebc2 63 unsigned short getDistance(unsigned short); // alias of getX
aktk 16:c2b0f3ca63dd 64 unsigned short getDistance(); // get voltage from PinName and getX()
aktk 16:c2b0f3ca63dd 65 unsigned short getVoltage(); // get voltage
aktk 16:c2b0f3ca63dd 66 unsigned short getX(unsigned short); // the fuction to get distance.
aktk 16:c2b0f3ca63dd 67 unsigned short getY(unsigned short); // get y-spline_model value of x(argument)
aktk 14:a99bf22b919d 68 unsigned short getSampleNum();
aktk 0:e85788b14028 69 void calibrateSensor();
aktk 3:b56e933bebc2 70 //void calibrateSensor(VDset* arg_set, unsigned int arg_num);
aktk 3:b56e933bebc2 71 void calibrate();
aktk 3:b56e933bebc2 72 void setSample(unsigned short,unsigned short);
aktk 14:a99bf22b919d 73 #ifdef HAS_LOCAL_FILE_SYSTEM
aktk 14:a99bf22b919d 74 void printOutData(const char* filename);
aktk 0:e85788b14028 75 void saveSetting();
aktk 0:e85788b14028 76 void loadSetting();
aktk 14:a99bf22b919d 77 void saveSetting(const char* filename);
aktk 14:a99bf22b919d 78 void loadSetting(const char* filename);
aktk 14:a99bf22b919d 79 //void saveSetting_fromSirial(const char* filename, Serial* com);
aktk 14:a99bf22b919d 80 //void loadSetting_intoSerial(const char* filename, Serial* com);
aktk 14:a99bf22b919d 81 #endif
aktk 14:a99bf22b919d 82 void saveSetting_intoSerial(Serial *com);
aktk 12:db5110d9d494 83 void loadSetting_fromSerial(Serial *com);
aktk 11:d60fb729eacf 84 #ifdef HAS_COM_TO_CONSOLE
aktk 0:e85788b14028 85 void printThresholds();
aktk 11:d60fb729eacf 86 #endif
aktk 0:e85788b14028 87
aktk 0:e85788b14028 88 private:
aktk 1:2053662b1167 89 //
aktk 1:2053662b1167 90 // Defining Constants
aktk 1:2053662b1167 91 //
aktk 13:16fc61b41eff 92 enum {
aktk 0:e85788b14028 93 _LIDX = 0,
aktk 1:2053662b1167 94 _RIDX = 255,
aktk 0:e85788b14028 95 _ENUM = _RIDX - _LIDX + 1
aktk 0:e85788b14028 96 };
aktk 1:2053662b1167 97 //
aktk 1:2053662b1167 98 // Variables
aktk 1:2053662b1167 99 //
aktk 9:ec1ee4a6b6a4 100 // Debug or Module
aktk 3:b56e933bebc2 101 UseType _useType;
aktk 9:ec1ee4a6b6a4 102 // For data sampling and making spline model
aktk 13:16fc61b41eff 103 unsigned short _Sample_Num; // the number of samples for derive spline
aktk 3:b56e933bebc2 104 VDset* _Sample_Set;
aktk 9:ec1ee4a6b6a4 105 double* _u_spline;
aktk 9:ec1ee4a6b6a4 106 // For comvert voltage -> physical quantity e.g. distance
aktk 3:b56e933bebc2 107 VDset _Set[_ENUM];
aktk 3:b56e933bebc2 108 unsigned short _Threshold[_ENUM]; //_Threshold[18] is not used virtually.
aktk 9:ec1ee4a6b6a4 109 // For get voltage
aktk 9:ec1ee4a6b6a4 110 AnalogIn _ai;
aktk 3:b56e933bebc2 111 //
aktk 0:e85788b14028 112 //
aktk 0:e85788b14028 113 // For calibration
aktk 0:e85788b14028 114 //
aktk 11:d60fb729eacf 115 #ifdef HAS_COM_TO_CONSOLE
aktk 0:e85788b14028 116 void _sampleData();
aktk 11:d60fb729eacf 117 #endif
aktk 3:b56e933bebc2 118 void _setSample(unsigned short,unsigned short);
aktk 3:b56e933bebc2 119 void _setSamples(VDset* arg_set, unsigned int arg_num);
aktk 3:b56e933bebc2 120 unsigned short _getSplineYof(double arg_x);
aktk 0:e85788b14028 121 void _makeSpline(); // Cubic spline
aktk 0:e85788b14028 122 //
aktk 0:e85788b14028 123 // For get distance
aktk 0:e85788b14028 124 //
aktk 0:e85788b14028 125 int _getNearest(int, int, unsigned short);
aktk 0:e85788b14028 126 //
aktk 1:2053662b1167 127 // For debug
aktk 1:2053662b1167 128 //
aktk 14:a99bf22b919d 129 #ifdef HAS_LOCAL_FILE_SYSTEM
aktk 0:e85788b14028 130 void _printOutData(unsigned short *arg, int num, char* name);
aktk 0:e85788b14028 131 void _printOutData(VDset *arg, int num, char* name);
aktk 0:e85788b14028 132 void _printOutData(double *arg, int num, char* name);
aktk 14:a99bf22b919d 133 #endif
aktk 0:e85788b14028 134 };
aktk 16:c2b0f3ca63dd 135
aktk 16:c2b0f3ca63dd 136 //inline implemeted
aktk 16:c2b0f3ca63dd 137
aktk 16:c2b0f3ca63dd 138 inline unsigned short TRP105FS::getDistance(unsigned short arg_y)
aktk 16:c2b0f3ca63dd 139 {
aktk 16:c2b0f3ca63dd 140 return getX(arg_y);
aktk 16:c2b0f3ca63dd 141 }
aktk 16:c2b0f3ca63dd 142
aktk 16:c2b0f3ca63dd 143 inline unsigned short TRP105FS::getDistance()
aktk 16:c2b0f3ca63dd 144 {
aktk 16:c2b0f3ca63dd 145 return getX(_ai.read_u16());
aktk 16:c2b0f3ca63dd 146 }
aktk 16:c2b0f3ca63dd 147
aktk 16:c2b0f3ca63dd 148 inline unsigned short TRP105FS::getVoltage()
aktk 16:c2b0f3ca63dd 149 {
aktk 16:c2b0f3ca63dd 150 return _ai.read_u16();
aktk 16:c2b0f3ca63dd 151 }
aktk 16:c2b0f3ca63dd 152
aktk 0:e85788b14028 153 #endif