-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqadcdata.cpp
120 lines (81 loc) · 2.59 KB
/
qadcdata.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "qadcdata.h"
#include <QFile>
#include <QtCore/qmath.h>
#include <QDebug>
#define LDR_PATH "/sys/bus/iio/devices/iio:device0/in_voltage5_raw"
#define ANARES (1.8/4096) // use here a correction factor
qadcdata::qadcdata(QObject *parent) : QObject(parent)
{
}
/*----------------------------------------------------------------------
;
; read ADC voltage data
;
-----------------------------------------------------------------------*/
void qadcdata::readData()
{
QByteArray line;
QFile file( LDR_PATH );
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
//this->statusBar()->showMessage("ADC error");
return;
}
while ( !file.atEnd() ) {
line = file.readLine();
break;
}
file.close();
line = line.simplified();
if( line.size()==0 ) return;
adcData.append( line.toInt() );
// this->statusBar()->showMessage( "ADC = " + line );
// == display voltage and load in percent
float volt = line.toInt() * ANARES * 17.25;
//ui->lcdVb->display( volt );
float perc;
//perc = 100*(volt-BAT_VMIN)/(BAT_VMAX-BAT_VMIN);
if( perc<0.) perc = 0.;
if( perc>100.) perc = 100;
//ui->lcdSoC->display( perc );
}
void qadcdata::filterData()
{
if( adcData.size()==0 ) return;
// == make some stats to filter the outlaiers ==========
float pSum = 0;
float pMax = 0;
float pMin = 0;
float pMean = 0;
float pStdDev = 0;
float pVariance = 0;
pMin = 99999.9;
pMax = -pMin;
for (int i = 0; i < adcData.size(); i++) {
float locCurrentValue = adcData.at(i);
pMin = std::min(locCurrentValue, pMin);
pMax = std::max(locCurrentValue, pMax);
pSum += locCurrentValue;
}
// -- calculate std dev ---------------------
pMean = pSum / adcData.size();
for (int i = 0; i < adcData.size(); i++) {
pVariance += qPow(adcData.at(i) - pMean, 2);
}
pVariance = pVariance / (adcData.size() - 1);
pStdDev = sqrt(pVariance);
qDebug() << adcData << "stdev = " << pStdDev;
// -- remove the outliers, brutal out of 1th StdDev -------------------
redo:
for (int i = 0; i < adcData.size(); i++) {
if( adcData.at(i)<pMean-pStdDev ) { adcData.removeAt(i); goto redo; }
if( adcData.at(i)>pMean+pStdDev ) { adcData.removeAt(i); goto redo; }
}
// == get a cleaner average ============================
qDebug() << adcData;
if(adcData.size()==0) return;
int avg = 0;
for( int n=0; n < adcData.size(); ++ n )
avg += adcData.at(n);
avg = avg / adcData.size();
adcData.clear();
}