Kenta Tanabe / TRP105F_Spline_for_child

Fork of TRP105F_Spline by Akifumi Takahashi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TRP105F_Spline.h Source File

TRP105F_Spline.h

00001 /**
00002  *  TRP105F_Spline.h,.cpp
00003  *
00004  *  Author: aktk, aktk.j.uec@gmail.com
00005  *  Tokyo, Japan.
00006  *
00007  *  -   This library is for providing a distance from TRP105F, photo-reflect proximity sensor,
00008  *  to whatever reflect the flash.
00009  *  -   Deistance is derived as a voltage signal of TRP105F.
00010  *  -   An object of TRPFS class has an array of sets of
00011  *  distance(16 bit-value) and voltage(16bit-value) at the distance.
00012  *  -   The range of distances that an array has in sets is [LIDX:RIDX] by 1.
00013  *  -   an array is derived from a cubic spline curve model.
00014  *  -   In order to derive spline curve, some value sets should be got
00015  *  at the first calibration.
00016  *
00017  *  LOG:
00018  *  ver.1   2015/10/19 - 2015/10/22
00019  *  ver.2   2015/10/22 -
00020  *      Distance data type has become unsigned short from int.
00021  *      Distance data range : [2:20] by 1 -> [0:1024] by 1.
00022  *  ver.2.1 2016/02/12 - 2016/02/16
00023  *      Distance data range : [0:1024] -> [0:255].
00024  *  ver.3.0 2016/05/23 - 2016/06/08
00025  *      Some Functions modified a lot. Even same name function might become uncompatible!!!!!
00026  *      (in order to make it be extensivly)
00027  *      Following are Implemented: 
00028  *          -Functions relating to save/load data with serial
00029  *          -Some New Members: _ai, or so on.
00030  *          -enum UseType
00031  *      
00032  */
00033  
00034 #ifndef TRP105F_SPLINE_H
00035 #define TRP105F_SPLINE_H
00036 
00037 
00038 #include "mbed.h"
00039 #include "CODE_SELECTIVE.h"
00040 
00041 
00042 //  Set of voltage-distance
00043 typedef struct {
00044     unsigned short x; //  distance
00045     unsigned short y; //  voltage
00046 } VDset;
00047 
00048 //  Type of modality to input data for calibration
00049 enum UseType {
00050     AsDEBUG,
00051     AsMODULE
00052 } ;
00053 
00054 //
00055 //  TRP105FS Class for get distance from voltage
00056 //
00057 class TRP105FS
00058 {
00059 public:
00060     //  Constraction
00061     TRP105FS();
00062     TRP105FS(unsigned short);
00063     TRP105FS(unsigned short, UseType);
00064     TRP105FS(unsigned short, PinName);
00065     TRP105FS(unsigned short, UseType, PinName);
00066     //TRP105FS(unsigned int arg_num, DataInType arg_dit, unsigned int channel);
00067     //  Destraction
00068     ~TRP105FS();
00069     //  Functions
00070     unsigned short getDistance(unsigned short); // alias of getX
00071     unsigned short getDistance();   // get voltage from PinName and getX()
00072     unsigned short getVoltage();    // get voltage
00073     unsigned short getX(unsigned short);    //  the fuction to get distance.
00074     unsigned short getY(unsigned short);    //  get y-spline_model value of x(argument)
00075     unsigned short getSampleNum();
00076     void    calibrateSensor();
00077     //void    calibrateSensor(VDset* arg_set, unsigned int arg_num);
00078     void    calibrate();
00079     void    setSample(unsigned short,unsigned short);
00080 #ifdef HAS_LOCAL_FILE_SYSTEM
00081     void    printOutData(const char* filename);
00082     void    saveSetting();
00083     void    loadSetting();
00084     void    saveSetting(const char* filename);
00085     void    loadSetting(const char* filename);
00086     //void    saveSetting_fromSirial(const char* filename, Serial* com);
00087     //void    loadSetting_intoSerial(const char* filename, Serial* com);
00088 #endif
00089     void    saveSetting_intoSerial(Serial *com);
00090     void    loadSetting_fromSerial(Serial *com);
00091 #ifdef HAS_COM_TO_CONSOLE
00092     void    printThresholds();
00093 #endif
00094 
00095 private:
00096     //
00097     //  Defining Constants
00098     //
00099     enum {
00100         _LIDX = 0,
00101         _RIDX = 255,
00102         _ENUM = _RIDX - _LIDX + 1
00103     };
00104     //
00105     //  Variables
00106     //
00107     //  Debug or Module
00108     UseType         _useType;
00109     //  For data sampling and making spline model
00110     unsigned short  _Sample_Num;    // the number of samples for derive spline
00111     unsigned short  _snum;          //  for counting set sample data by _setSample(us,us);
00112     VDset*          _Sample_Set;
00113     double*         _u_spline;
00114     //  For comvert voltage -> physical quantity e.g. distance
00115     VDset           _Set[_ENUM];
00116     unsigned short  _Threshold[_ENUM]; //_Threshold[18] is not used virtually.
00117     //  For get voltage
00118     AnalogIn        _ai;
00119     //
00120     //
00121     //  For calibration
00122     //
00123 #ifdef HAS_COM_TO_CONSOLE
00124     void    _sampleData();
00125 #endif
00126     void    _setSample(unsigned short,unsigned short);
00127     void    _setSamples(VDset* arg_set, unsigned int arg_num);
00128     unsigned short _getSplineYof(double arg_x);
00129     void    _makeSpline();  //  Cubic spline
00130     //
00131     //  For get distance
00132     //
00133     int     _getNearest(int, int, unsigned short);
00134     //
00135     //  For debug
00136     //
00137 #ifdef HAS_LOCAL_FILE_SYSTEM
00138     void    _printOutData(unsigned short    *arg, int num, char* name);
00139     void    _printOutData(VDset             *arg, int num, char* name);
00140     void    _printOutData(double            *arg, int num, char* name);
00141 #endif
00142 };
00143 
00144 //inline implemeted
00145 
00146 inline unsigned short TRP105FS::getDistance(unsigned short arg_y)
00147 {
00148     return getX(arg_y);
00149 }
00150 
00151 inline unsigned short TRP105FS::getDistance()
00152 {
00153     return getX(_ai.read_u16());
00154 }
00155 
00156 inline unsigned short TRP105FS::getVoltage()
00157 {
00158     return _ai.read_u16();
00159 }
00160 
00161 #endif