forked from RebelTechnology/OpenWare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibration.hpp
211 lines (178 loc) · 5.81 KB
/
calibration.hpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef __CALIBRATION_HPP__
#define __CALIBRATION_HPP__
#include "ApplicationSettings.h"
#include "ProgramVector.h"
#define VOLTS_LO 1.0f
#define VOLTS_HI 3.0f
#define SAMPLE_LO -0.5f
#define SAMPLE_HI 0.5f
#define MIN_BUFFER_WRITES 8192
// Amount of bytes we want to send before reading calibration.
// We don't count buffers to avoid measuring input too early due to short buffer size.
class BaseCalibration {
protected:
int32_t scalar;
int32_t offset;
void calculateScalarAndOffset(float sample1, float sample2, float volts1, float volts2){
/*----------------------------------------------------------
This is based on V/oct conversion code:
Volts / multiplier + offset = sample
We can derive from this the following:
multiplier = (volts1 - volts2) / (sample1 - sample2)
To get scalar variable, we divide multiplier by UINT16_MAX
----------------------------------------------------------*/
float f_scalar = ((volts1 - volts2) / (sample1 - sample2) * UINT16_MAX);
scalar = f_scalar;
/*----------------------------------------------------------
Now we use this formula:
offset = sample - volts / multiplier
This also has to be multiplied by UINT16_MAX to get integer
value.
----------------------------------------------------------*/
offset = ((sample1 - volts1 * UINT16_MAX / f_scalar) * UINT16_MAX);
}
public:
enum CalibrationMode {
CAL_INPUT, CAL_OUTPUT
};
enum CalibrationState {
CAL_LO, CAL_HI, CAL_DONE
};
enum CalibrationResults {
CAL_SAVE, CAL_DISCARD
};
CalibrationResults results;
float samples[2];
CalibrationState state;
BaseCalibration() {
reset();
}
int32_t getScalar(){
return scalar;
}
int32_t getOffset(){
return offset;
}
virtual void calibrate() = 0;
virtual bool readSample() = 0;
// readSample is boolean as we'll be writing data first and
// reading it later for output calibration.
virtual void storeResults() = 0;
void reset() {
samples[0] = 0.0f;
samples[1] = 0.0f;
state = CAL_LO;
}
bool isDone(){
return state == CAL_DONE;
}
float getInput() {
/*--------------------------------------------------------------
We want to use whole buffer for measurement, but we only have
8 bits left in ADC input (assuming 24 bit sample data stored as
32 bit value. So we'll use minimum of 128 samples or buffer size.
We can't use more in unsigned 32 bit variable as it would overflow.
--------------------------------------------------------------*/
ProgramVector* pv = getProgramVector();
int32_t tmp_data = 0;
uint8_t shift = 0;
uint16_t num_samples = 1;
while (num_samples < 128 && num_samples < settings.audio_blocksize) {
num_samples = num_samples << 1;
shift++;
}
uint16_t block_step = max(1, settings.audio_blocksize / num_samples);
// Block step is used to skip some samples when audio buffer size > 128
for (int i = 0; i < settings.audio_blocksize; i = i + block_step)
// We read only left channel (* 2) and we do it every block_step samples.
tmp_data += ((int32_t)((pv->audio_input[i * 2]) << 8)) >> shift;
return (float)tmp_data / 2147483648.0f;
}
virtual void nextState(){
switch (state){
case CAL_LO:
state = CAL_HI;
break;
case CAL_HI:
state = CAL_DONE;
break;
case CAL_DONE:
break;
}
}
};
class InputCalibration : public BaseCalibration {
public:
void calibrate(){
// Calibrate based on known voltage and measured samples
calculateScalarAndOffset(samples[0], samples[1], VOLTS_LO, VOLTS_HI);
}
void reset(){
BaseCalibration::reset();
results = CAL_SAVE;
scalar = settings.input_scalar;
offset = settings.input_offset;
}
bool readSample(){
samples[(state == CAL_LO)?0:1] = getInput();
return true;
}
void storeResults() {
settings.input_scalar = scalar;
settings.input_offset = offset;
settings.saveToFlash();
}
};
class OutputCalibration : public BaseCalibration {
uint32_t data_written;
float current_sample;
public:
void calibrate(){
// First measure voltage from calibrated input, then use it to determine scaling
float input_multiplier = (float)(int32_t)settings.input_scalar / UINT16_MAX;
float input_offset = (float)(int32_t)settings.input_offset / UINT16_MAX;
/*----------------------------------------------------------
We use input offset/scalar for measuring voltage here
volts = (sample - offset) * multiplier
Note that we're using samples from input, not output
----------------------------------------------------------*/
float volts1 = (samples[0] - input_offset) * input_multiplier;
float volts2 = (samples[1] - input_offset) * input_multiplier;
// Samples from output are converted to normalized float values
calculateScalarAndOffset(SAMPLE_LO, SAMPLE_HI, volts1, volts2);
};
void reset(){
BaseCalibration::reset();
scalar = settings.output_scalar;
offset = settings.output_offset;
data_written = 0;
current_sample = SAMPLE_LO;
};
bool readSample(){
if (data_written < MIN_BUFFER_WRITES) {
// First we must write a certain of data to output buffer
ProgramVector* pv = getProgramVector();
for (uint16_t i = 0; i < pv->audio_blocksize; i++) {
pv->audio_output[i * 2] = (int32_t)(current_sample * 2147483648.0f) >> 8;
data_written++;
}
return false;
}
else {
// Ready to read results previously written
samples[(state == CAL_LO)?0:1] = getInput();
return true;
}
}
void storeResults() {
settings.output_scalar = scalar;
settings.output_offset = offset;
settings.saveToFlash();
}
void nextState(){
BaseCalibration::nextState();
current_sample = SAMPLE_HI;
data_written = 0;
}
};
#endif