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 13:14:17 2016 +0000
Revision:
17:f6ec7796dafd
Parent:
16:c2b0f3ca63dd
Child:
18:b046bba4ffa7
Some comment

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 17:f6ec7796dafd 12 * - The range of distances that an array has in sets is [LIDX:RIDX] 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 17:f6ec7796dafd 22 * ver.2.1 2016/02/12 - 2016/02/16
aktk 1:2053662b1167 23 * Distance data range : [0:1024] -> [0:255].
aktk 17:f6ec7796dafd 24 * ver.3.0 2016/05/23 - 2016/06/08
aktk 17:f6ec7796dafd 25 * Some Functions modified a lot. Even same name function might become uncompatible!!!!!
aktk 17:f6ec7796dafd 26 * (in order to make it be extensivly)
aktk 17:f6ec7796dafd 27 * Following are Implemented:
aktk 17:f6ec7796dafd 28 * -Functions relating to save/load data with serial
aktk 17:f6ec7796dafd 29 * -Some New Members: _ai, or so on.
aktk 17:f6ec7796dafd 30 * -enum UseType
aktk 17:f6ec7796dafd 31 *
aktk 0:e85788b14028 32 */
aktk 11:d60fb729eacf 33
aktk 11:d60fb729eacf 34 #ifndef TRP105F_SPLINE_H
aktk 11:d60fb729eacf 35 #define TRP105F_SPLINE_H
aktk 0:e85788b14028 36
aktk 9:ec1ee4a6b6a4 37
aktk 0:e85788b14028 38 #include "mbed.h"
aktk 11:d60fb729eacf 39 #include "CODE_SELECTIVE.h"
aktk 0:e85788b14028 40
aktk 3:b56e933bebc2 41
aktk 0:e85788b14028 42 // Set of voltage-distance
aktk 0:e85788b14028 43 typedef struct {
aktk 3:b56e933bebc2 44 unsigned short x; // distance
aktk 3:b56e933bebc2 45 unsigned short y; // voltage
aktk 0:e85788b14028 46 } VDset;
aktk 0:e85788b14028 47
aktk 1:2053662b1167 48 // Type of modality to input data for calibration
aktk 3:b56e933bebc2 49 enum UseType {
aktk 3:b56e933bebc2 50 AsDEBUG,
aktk 3:b56e933bebc2 51 AsMODULE
aktk 0:e85788b14028 52 } ;
aktk 0:e85788b14028 53
aktk 0:e85788b14028 54 //
aktk 0:e85788b14028 55 // TRP105FS Class for get distance from voltage
aktk 2:40ae18445079 56 //
aktk 0:e85788b14028 57 class TRP105FS
aktk 0:e85788b14028 58 {
aktk 0:e85788b14028 59 public:
aktk 1:2053662b1167 60 // Constraction
aktk 0:e85788b14028 61 TRP105FS();
aktk 0:e85788b14028 62 TRP105FS(unsigned int);
aktk 3:b56e933bebc2 63 TRP105FS(unsigned int, UseType);
aktk 10:b50e4bb40571 64 TRP105FS(unsigned int, PinName);
aktk 3:b56e933bebc2 65 TRP105FS(unsigned int, UseType, PinName);
aktk 3:b56e933bebc2 66 //TRP105FS(unsigned int arg_num, DataInType arg_dit, unsigned int channel);
aktk 1:2053662b1167 67 // Destraction
aktk 0:e85788b14028 68 ~TRP105FS();
aktk 1:2053662b1167 69 // Functions
aktk 3:b56e933bebc2 70 unsigned short getDistance(unsigned short); // alias of getX
aktk 16:c2b0f3ca63dd 71 unsigned short getDistance(); // get voltage from PinName and getX()
aktk 16:c2b0f3ca63dd 72 unsigned short getVoltage(); // get voltage
aktk 16:c2b0f3ca63dd 73 unsigned short getX(unsigned short); // the fuction to get distance.
aktk 16:c2b0f3ca63dd 74 unsigned short getY(unsigned short); // get y-spline_model value of x(argument)
aktk 14:a99bf22b919d 75 unsigned short getSampleNum();
aktk 0:e85788b14028 76 void calibrateSensor();
aktk 3:b56e933bebc2 77 //void calibrateSensor(VDset* arg_set, unsigned int arg_num);
aktk 3:b56e933bebc2 78 void calibrate();
aktk 3:b56e933bebc2 79 void setSample(unsigned short,unsigned short);
aktk 14:a99bf22b919d 80 #ifdef HAS_LOCAL_FILE_SYSTEM
aktk 14:a99bf22b919d 81 void printOutData(const char* filename);
aktk 0:e85788b14028 82 void saveSetting();
aktk 0:e85788b14028 83 void loadSetting();
aktk 14:a99bf22b919d 84 void saveSetting(const char* filename);
aktk 14:a99bf22b919d 85 void loadSetting(const char* filename);
aktk 14:a99bf22b919d 86 //void saveSetting_fromSirial(const char* filename, Serial* com);
aktk 14:a99bf22b919d 87 //void loadSetting_intoSerial(const char* filename, Serial* com);
aktk 14:a99bf22b919d 88 #endif
aktk 14:a99bf22b919d 89 void saveSetting_intoSerial(Serial *com);
aktk 12:db5110d9d494 90 void loadSetting_fromSerial(Serial *com);
aktk 11:d60fb729eacf 91 #ifdef HAS_COM_TO_CONSOLE
aktk 0:e85788b14028 92 void printThresholds();
aktk 11:d60fb729eacf 93 #endif
aktk 0:e85788b14028 94
aktk 0:e85788b14028 95 private:
aktk 1:2053662b1167 96 //
aktk 1:2053662b1167 97 // Defining Constants
aktk 1:2053662b1167 98 //
aktk 13:16fc61b41eff 99 enum {
aktk 0:e85788b14028 100 _LIDX = 0,
aktk 1:2053662b1167 101 _RIDX = 255,
aktk 0:e85788b14028 102 _ENUM = _RIDX - _LIDX + 1
aktk 0:e85788b14028 103 };
aktk 1:2053662b1167 104 //
aktk 1:2053662b1167 105 // Variables
aktk 1:2053662b1167 106 //
aktk 9:ec1ee4a6b6a4 107 // Debug or Module
aktk 3:b56e933bebc2 108 UseType _useType;
aktk 9:ec1ee4a6b6a4 109 // For data sampling and making spline model
aktk 13:16fc61b41eff 110 unsigned short _Sample_Num; // the number of samples for derive spline
aktk 3:b56e933bebc2 111 VDset* _Sample_Set;
aktk 9:ec1ee4a6b6a4 112 double* _u_spline;
aktk 9:ec1ee4a6b6a4 113 // For comvert voltage -> physical quantity e.g. distance
aktk 3:b56e933bebc2 114 VDset _Set[_ENUM];
aktk 3:b56e933bebc2 115 unsigned short _Threshold[_ENUM]; //_Threshold[18] is not used virtually.
aktk 9:ec1ee4a6b6a4 116 // For get voltage
aktk 9:ec1ee4a6b6a4 117 AnalogIn _ai;
aktk 3:b56e933bebc2 118 //
aktk 0:e85788b14028 119 //
aktk 0:e85788b14028 120 // For calibration
aktk 0:e85788b14028 121 //
aktk 11:d60fb729eacf 122 #ifdef HAS_COM_TO_CONSOLE
aktk 0:e85788b14028 123 void _sampleData();
aktk 11:d60fb729eacf 124 #endif
aktk 3:b56e933bebc2 125 void _setSample(unsigned short,unsigned short);
aktk 3:b56e933bebc2 126 void _setSamples(VDset* arg_set, unsigned int arg_num);
aktk 3:b56e933bebc2 127 unsigned short _getSplineYof(double arg_x);
aktk 0:e85788b14028 128 void _makeSpline(); // Cubic spline
aktk 0:e85788b14028 129 //
aktk 0:e85788b14028 130 // For get distance
aktk 0:e85788b14028 131 //
aktk 0:e85788b14028 132 int _getNearest(int, int, unsigned short);
aktk 0:e85788b14028 133 //
aktk 1:2053662b1167 134 // For debug
aktk 1:2053662b1167 135 //
aktk 14:a99bf22b919d 136 #ifdef HAS_LOCAL_FILE_SYSTEM
aktk 0:e85788b14028 137 void _printOutData(unsigned short *arg, int num, char* name);
aktk 0:e85788b14028 138 void _printOutData(VDset *arg, int num, char* name);
aktk 0:e85788b14028 139 void _printOutData(double *arg, int num, char* name);
aktk 14:a99bf22b919d 140 #endif
aktk 0:e85788b14028 141 };
aktk 16:c2b0f3ca63dd 142
aktk 16:c2b0f3ca63dd 143 //inline implemeted
aktk 16:c2b0f3ca63dd 144
aktk 16:c2b0f3ca63dd 145 inline unsigned short TRP105FS::getDistance(unsigned short arg_y)
aktk 16:c2b0f3ca63dd 146 {
aktk 16:c2b0f3ca63dd 147 return getX(arg_y);
aktk 16:c2b0f3ca63dd 148 }
aktk 16:c2b0f3ca63dd 149
aktk 16:c2b0f3ca63dd 150 inline unsigned short TRP105FS::getDistance()
aktk 16:c2b0f3ca63dd 151 {
aktk 16:c2b0f3ca63dd 152 return getX(_ai.read_u16());
aktk 16:c2b0f3ca63dd 153 }
aktk 16:c2b0f3ca63dd 154
aktk 16:c2b0f3ca63dd 155 inline unsigned short TRP105FS::getVoltage()
aktk 16:c2b0f3ca63dd 156 {
aktk 16:c2b0f3ca63dd 157 return _ai.read_u16();
aktk 16:c2b0f3ca63dd 158 }
aktk 16:c2b0f3ca63dd 159
aktk 0:e85788b14028 160 #endif