GP2Y0A21YK0F IRsensor 用ライブラリ

Dependents:   NHK2019_mae_v6 NHK2019_main_v6 NHK2019_usiro_v6 NHK2019_mae_v6 ... more

Files at this revision

API Documentation at this revision

Comitter:
skouki
Date:
Wed Sep 11 01:59:10 2019 +0000
Parent:
6:4d4e1ea317f5
Commit message:
change how to averaging

Changed in this revision

IRsensor.cpp Show annotated file Show diff for this revision Revisions of this file
IRsensor.h Show annotated file Show diff for this revision Revisions of this file
diff -r 4d4e1ea317f5 -r 790cd18896a8 IRsensor.cpp
--- a/IRsensor.cpp	Thu Aug 29 02:32:49 2019 +0000
+++ b/IRsensor.cpp	Wed Sep 11 01:59:10 2019 +0000
@@ -15,46 +15,49 @@
 
 void IRsensor::compute_distance(){
     getInputvoltage();
-    changeVtoD();
+    originaldistance = changeVtoD(voltage);
 }
 
 void IRsensor::getInputvoltage(){
     voltage = a_in.read()*3.3f;
 }
 
-void IRsensor::changeVtoD()
+float IRsensor::changeVtoD(float voltage_)
 {
-    if (voltage > 2.285f) {
-        originaldistance = 0.0f;
-    } else if (voltage > 1.645f) {
-        originaldistance = -7.8125f * voltage + 27.8515f;
-    } else if (voltage > 1.305f) {
-        originaldistance = -14.7058f * voltage + 39.1911f;
-    } else if (voltage > 1.08f) {
-        originaldistance = -22.222f * voltage + 49.0f;
-    } else if (voltage > 0.928f) {
-        originaldistance = -32.89473f * voltage + 60.526f;
-    } else if (voltage > 0.835f) {
-        originaldistance = -53.7634f * voltage + 79.89247f;
-    } else if (voltage > 0.737f) {
-        originaldistance = -51.02040f * voltage + 77.60204f;
-    } else if (voltage > 0.673f) {
-        originaldistance = -78.12500f * voltage + 97.57812f;
-    } else if (voltage > 0.608f) {
-        originaldistance = -76.92307f * voltage + 96.76923f;
-    } else if (voltage > 0.562f) {
-        originaldistance = -108.6956f * voltage + 116.0869f;
-    } else if (voltage > 0.515f) {
-        originaldistance = -106.3829f * voltage + 114.7872f;
-    } else if (voltage > 0.474f) {
-        originaldistance = -121.9512f * voltage + 122.8048f;
-    } else if (voltage > 0.447f) {
-        originaldistance = -185.1851f * voltage + 152.7777f;
-    } else if (voltage > 0.432f) {
-        originaldistance = -333.333f * voltage + 219.0f;
+    float distance_;
+    
+    if (voltage_ > 2.28f) {
+        distance_ = 0.00f;
+    } else if (voltage_ > 1.64f) {
+        distance_ = -7.81f * voltage_ + 27.85f;
+    } else if (voltage_ > 1.30f) {
+        distance_ = -14.70f * voltage_ + 39.19f;
+    } else if (voltage_ > 1.08f) {
+        distance_ = -22.2f * voltage_ + 49.00f;
+    } else if (voltage_ > 0.92f) {
+        distance_ = -32.89f * voltage_ + 60.52f;
+    } else if (voltage_ > 0.83f) {
+        distance_ = -53.76f * voltage_ + 79.89f;
+    } else if (voltage_ > 0.73f) {
+        distance_ = -51.02f * voltage_ + 77.60f;
+    } else if (voltage_ > 0.67f) {
+        distance_ = -78.12f * voltage_ + 97.57f;
+    } else if (voltage_ > 0.60f) {
+        distance_ = -76.92f * voltage_ + 96.76f;
+    } else if (voltage_ > 0.56f) {
+        distance_ = -108.69f * voltage_ + 116.08f;
+    } else if (voltage_ > 0.51f) {
+        distance_ = -106.38f * voltage_ + 114.78f;
+    } else if (voltage_ > 0.47f) {
+        distance_ = -121.95f * voltage_ + 122.80f;
+    } else if (voltage_ > 0.44f) {
+        distance_ = -185.18f * voltage_ + 152.77f;
+    } else if (voltage_ > 0.43f) {
+        distance_ = -333.33f * voltage_ + 219.00f;
     } else {
-        originaldistance = 9999.9f;
+        distance_ = 99.99f;
     }
+    return distance_;
 }
 
 void IRsensor::startAveraging(uint8_t averaging_range)
@@ -72,11 +75,14 @@
 void IRsensor::computeaverage()
 {
     bufferpoint++;
-    data[bufferpoint % bufferSize] = originaldistance;
-    distance_sum = 0;
+    getInputvoltage();
+    data[bufferpoint % bufferSize] = voltage;
+    voltage_sum = 0;
     for(int i = 0;i<bufferSize;i++)
-        distance_sum += data[i];
-    distance_average = distance_sum / bufferSize;
+        voltage_sum += data[i];
+    voltage_average = voltage_sum / bufferSize;
+    distance_average = changeVtoD(voltage_average);
+    
 }
 float IRsensor::getDistance()
 {
diff -r 4d4e1ea317f5 -r 790cd18896a8 IRsensor.h
--- a/IRsensor.h	Thu Aug 29 02:32:49 2019 +0000
+++ b/IRsensor.h	Wed Sep 11 01:59:10 2019 +0000
@@ -69,13 +69,15 @@
     void threadloop_averaging_distance();
     void compute_distance();
     void getInputvoltage();
-    void changeVtoD();
+    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;