/
multiZAP_calib.h
316 lines (227 loc) · 6.11 KB
/
multiZAP_calib.h
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* multiZAP_calib.h
*
* Created on: 24 dec 2017
* Author: elektros230
*/
#ifndef CALIB_H_
#define CALIB_H_
#include <Arduino.h>
//#include "bioZAP_func.h"
//#include "DS1803.h"
//#include "AD9850.h"
#include "multiZAP_def.h"
#define EEPROM_VAMPL_ADDRESS 1019 // v_ampl - multiZAP
#define EEPROM_VMIN_ADDRESS 1021 // v_min - multiZAP
#define VMIN_MIN 50 //vmin & vampl extremes
#define VMIN_MAX 400
#define VAMPL_MIN 100
#define VAMPL_MAX 800
/*************************************************************************************/
//Function prototypes
byte calib_gain_wiper_ampl(int v_ampl=400, long freq=100000);
byte calib_setp_wiper_vmin(int v_min);
byte calib_flat_wiper_vmin(int v_min);
int last_v_ampl = 0;
int last_v_min = 0;
/*************************************************************************************/
void get_v_EEPROM(){
EEPROM.get( EEPROM_VAMPL_ADDRESS, last_v_ampl );
EEPROM.get( EEPROM_VMIN_ADDRESS, last_v_min );
//For new devices were EEPROM memory is not set properly
last_v_ampl= constrain( last_v_ampl, VAMPL_MIN, VAMPL_MAX );
last_v_min = constrain( last_v_min, VMIN_MIN, VMIN_MAX );
}
void set_v_EEPROM(){
EEPROM.put( EEPROM_VAMPL_ADDRESS, last_v_ampl );
EEPROM.put( EEPROM_VMIN_ADDRESS, last_v_min );
}
int _getVampl(){
//Delay for extremely low frequency
int _delay =0;
if (ad9850.get_frequency() < 2000){
_delay = 1;
}
//First start point read
int x = analogRead(outVoltagePin);
//Starting to measure values
float _vmin= x * ONE_GRADE;
float _vmax= x * ONE_GRADE;
//Measuring ampl. pk-pk value
for( int i=0; i<200; i++){
x = analogRead(outVoltagePin);
#ifdef SERIAL_DEBUG
/*
Serial.print("i:");
Serial.println(i);
Serial.print(" _vmin:");
Serial.println(_vmin);
Serial.print(" _vmax:");
Serial.println(_vmax);
Serial.print(" x:");
Serial.println(x);
*/
#endif
if ( x * ONE_GRADE > _vmax) {
_vmax = x * ONE_GRADE;
}
if ( x * ONE_GRADE < _vmin) {
_vmin = x * ONE_GRADE;
}
if(_delay>0) delayMicroseconds(659);
//Against harmonics measurement method
//if (i>100) delayMicroseconds(random(200));
if (i>150) {
delayMicroseconds(17);
} else if (i>100) {
delayMicroseconds(37);
} else if (i>50) {
delayMicroseconds(83);
}
}
return (_vmax-_vmin) * 100.0;
}
int _getVMin(){
//Delay for extremely low frequency
int _delay = 0;
if (ad9850.get_frequency() < 2000){
_delay = 1;
}
//First start point read
int x = analogRead(outVoltagePin);
//Starting to measure values
float _vmin = x * ONE_GRADE;
//Measuring vmin value
for( int i=0; i<200; i++){
x = analogRead(outVoltagePin);
if ( x * ONE_GRADE < _vmin) {
_vmin = x * ONE_GRADE;
}
if(_delay>0) delayMicroseconds(659);
//Against harmonics measurement method
//if (i>100) delayMicroseconds(random(200));
if (i>150) {
delayMicroseconds(17);
} else if (i>100) {
delayMicroseconds(37);
} else if (i>50) {
delayMicroseconds(83);
}
}
return _vmin * 100;
}
byte calib_gain_wiper_ampl(int v_ampl, long freq){
// Find v_min level suitable to begin calibration
// Work with proper gain and frequency set
//Searching point range
byte i_min = 0;
byte i_max = 255;
byte i = 128; //ds1803.get_wiper0();
//if (i>i_max) i_max=i;
if (v_ampl<=800 && v_ampl>=100){
last_v_ampl = v_ampl;
if (v_ampl>=600){
//Calibrate v_ampl with v_min=100 - 1.0[V]
ds1803.set_wiper1( calib_flat_wiper_vmin(100) );
} else {
//Calibrate v_ampl with v_min=200 - 2.0[V]
ds1803.set_wiper1( calib_flat_wiper_vmin(200) );
}
//Set work frequency - up to 100kHz
ad9850.set_frequency(freq); // 1000 = 10.00Hz
//TODO: Do support for more than 100kHz
ds1803.set_wiper0(i);
do {
int a =_getVampl();
#ifdef SERIAL_DEBUG
Serial.println(a);
#endif
//Check
if ( v_ampl > a ){
i_min=i;
} else {
i_max=i;
}
//Calculate next point
i = i_min + round((i_max-i_min)/2.0);
ds1803.set_wiper0(i); //Set calculated value to setpoint wiper
//delay(20);
} while ( abs(i_max-i_min) > 1 ); //1 or 0 are good enough
return i;
} else {
return 0;
}
}
byte calib_setp_wiper_vmin(int v_min){
// Find v_min level suitable to begin calibration
// Work with proper gain and frequency set
//Searching point range
byte i_min = 0;
byte i_max = 100;
byte i = ds1803.get_wiper1();
if ( i>i_max ) i_max=i;
v_min = constrain( v_min, VMIN_MIN, VMIN_MAX );
//if ( v_min<=400 && v_min>=50 ){
last_v_min = v_min;
do {
//Check
if ( v_min > _getVMin() ){
i_min=i;
} else {
i_max=i;
}
//Calculate next point
i=i_min+round( (i_max-i_min)/2.0 );
ds1803.set_wiper1(i); //Set calculated value to setpoint wiper
} while ( abs(i_max-i_min)>1 ); //1 or 0 are good enough
if ( i_min > 0){
//Linearity v_min
if ( v_min<=100 && last_v_ampl<=200 ) {
ds1803.set_wiper1(i_min+5);
i=i_min+5;
}
//DONE: remove form calib.h
//wiper0 = ds1803.get_wiper0();
//wiper1 = ds1803.get_wiper1();
return i;
} else {
return 0;
}
return i;
/* } else {
return 0;
}*/
}
byte calib_flat_wiper_vmin(int v_min){
//Find v_min level suitable to begin calibration
ds1803.set_wiper0(0); // Gain to 0
ad9850.powerDown(); // Generator to off
unsigned int aim = v_min/(ONE_GRADE*100); //Pin outVoltagePin needed level
//Searching point range
byte i_min = 0;
byte i_max = 100;
byte i = 50;
//last_v_min = v_min;
//Averaging algorithm
unsigned int x=0;
byte probes=10;
ds1803.set_wiper1(i);
do {
//Averaging
x=0;
for (byte j=0; j<probes; j++){
x+=analogRead(outVoltagePin)/probes;
}
//Check
if (aim > x){
i_min=i;
} else {
i_max=i;
}
//Calculate next point
i=i_min+round((i_max-i_min)/2);
ds1803.set_wiper1(i); //Set calculated value to set point wiper
} while (abs(i_max-i_min)>1); //1 or 0 are good enough
return i;
}
#endif /* CALIB_H_ */