Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IRsensor_sample 2019NHK_A_sensor 200_yotsuba_21
IRsensor.cpp
00001 #include "IRsensor.h" 00002 00003 IRsensor::IRsensor(PinName pin): 00004 a_in(pin) 00005 { 00006 thread_get_distance.start(callback(this, &IRsensor::threadloop_get_distance)); 00007 } 00008 00009 void IRsensor::threadloop_get_distance() 00010 { 00011 while(true){ 00012 compute_distance(); 00013 } 00014 } 00015 00016 void IRsensor::compute_distance(){ 00017 getInputvoltage(); 00018 changeVtoD(); 00019 } 00020 00021 void IRsensor::getInputvoltage(){ 00022 voltage = a_in.read()*3.3f; 00023 } 00024 00025 void IRsensor::changeVtoD() 00026 { 00027 if (voltage > 2.285f) { 00028 originaldistance = 0.0f; 00029 } else if (voltage > 1.645f) { 00030 originaldistance = -7.8125f * voltage + 27.8515f; 00031 } else if (voltage > 1.305f) { 00032 originaldistance = -14.7058f * voltage + 39.1911f; 00033 } else if (voltage > 1.08f) { 00034 originaldistance = -22.222f * voltage + 49.0f; 00035 } else if (voltage > 0.928f) { 00036 originaldistance = -32.89473f * voltage + 60.526f; 00037 } else if (voltage > 0.835f) { 00038 originaldistance = -53.7634f * voltage + 79.89247f; 00039 } else if (voltage > 0.737f) { 00040 originaldistance = -51.02040f * voltage + 77.60204f; 00041 } else if (voltage > 0.673f) { 00042 originaldistance = -78.12500f * voltage + 97.57812f; 00043 } else if (voltage > 0.608f) { 00044 originaldistance = -76.92307f * voltage + 96.76923f; 00045 } else if (voltage > 0.562f) { 00046 originaldistance = -108.6956f * voltage + 116.0869f; 00047 } else if (voltage > 0.515f) { 00048 originaldistance = -106.3829f * voltage + 114.7872f; 00049 } else if (voltage > 0.474f) { 00050 originaldistance = -121.9512f * voltage + 122.8048f; 00051 } else if (voltage > 0.447f) { 00052 originaldistance = -185.1851f * voltage + 152.7777f; 00053 } else if (voltage > 0.432f) { 00054 originaldistance = -333.333f * voltage + 219.0f; 00055 } else { 00056 originaldistance = 9999.9f; 00057 } 00058 } 00059 00060 void IRsensor::startAveraging(uint8_t averaging_range) 00061 { 00062 bufferSize = averaging_range; 00063 data = new float[bufferSize]; 00064 thread_averaging_distance.start(callback(this, &IRsensor::threadloop_averaging_distance)); 00065 } 00066 void IRsensor::threadloop_averaging_distance() 00067 { 00068 while(true){ 00069 computeaverage(); 00070 } 00071 } 00072 void IRsensor::computeaverage() 00073 { 00074 bufferpoint++; 00075 data[bufferpoint % bufferSize] = originaldistance; 00076 distance_sum = 0; 00077 for(int i = 0;i<bufferSize;i++) 00078 distance_sum += data[i]; 00079 distance_average = distance_sum / bufferSize; 00080 } 00081 float IRsensor::getDistance() 00082 { 00083 return originaldistance; 00084 } 00085 float IRsensor::get_Averagingdistance() 00086 { 00087 return distance_average; 00088 } 00089 00090 float IRsensor::getVoltage() 00091 { 00092 return voltage; 00093 }
Generated on Mon Jul 18 2022 06:21:00 by
1.7.2