GP2Y0A21YK0F IRsensor 用ライブラリ

Dependents:   NHK2019_mae_v6 NHK2019_main_v6 NHK2019_usiro_v6 NHK2019_mae_v6 ... more

IRsensor.h

Committer:
skouki
Date:
2019-09-11
Revision:
7:790cd18896a8
Parent:
6:4d4e1ea317f5

File content as of revision 7:790cd18896a8:

#ifndef IRSENSOR_H
#define IRSENSOR_H

#include"mbed.h"

/**
* @file IRsensor.h
* @brief GP2Y0A21YK0F(SHARP 2Y0A21)を使ったIR距離測定
*
* Example :
* @code
* #include"mbed.h"
* #include"IRsensor.h"
* 
* IRsensor ir1(PC_1);
*
* Serial pc(USBTX,USBRX,115200);
*
* int main()
* {
*    ir1.startAveraging(255);
*    while(1){
*        float dis = ir1.getDistance();
*        float ave_dis = ir1.get_Averagingdistance();
*        pc.printf("dis:%4.4f[cm]||ave_dis:%4.4f[cm]\n\r",dis,ave_dis);
*        
*    }   
* }
* @endcode
*/

/**
* @brief SHARP 2Y0A21を使ったIR距離測定のクラス
*/
class IRsensor{
 public:
    /**
     * @brief コンストラクタ
     * @param pin Pint that can do analogin
     */
    IRsensor(PinName pin);
    
    /**
    * @brief センサの値を取得
    * @return voltage
    */
    float getVoltage();
    
    /**
    * @brief センサの値を距離変換し取得
    * @return originaldistance
    */
    float getDistance();
    
    /**
    * @brief 距離の平均化設定
    * @param averaging_range averaging range 1~255
    */
    void startAveraging(uint8_t averaging_range);
    
    /**
    * @brief 平均化した距離を取得
    * @return distance_average
    */
    float get_Averagingdistance();

private:
    void threadloop_get_distance();
    void threadloop_averaging_distance();
    void compute_distance();
    void getInputvoltage();
    float changeVtoD(float voltage_);
    void computeaverage();
    
    float voltage;
    float originaldistance;
    float* data;
    float voltage_sum;
    float distance_sum;
    float voltage_average;
    float distance_average;
    uint8_t bufferSize;
    uint8_t bufferpoint;
    Thread thread_get_distance;
    Thread thread_averaging_distance;
    AnalogIn a_in;

};

#endif