-
Notifications
You must be signed in to change notification settings - Fork 5
/
sensorinit.h
597 lines (522 loc) · 17.5 KB
/
sensorinit.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#ifndef __SAMOVAR_S_I_H_
#define __SAMOVAR_S_I_H_
#include <Arduino.h>
#include <DallasTemperature.h>
#include "Samovar.h"
#include "pumppwm.h"
#ifdef USE_BME680
#define BME_STRING "BME680"
#include <Adafruit_BME680.h>
Adafruit_BME680 bme; // I2C
#endif
#ifdef USE_BMP180
#include <Adafruit_BMP085_U.h>
#define BME_STRING "BMP180"
Adafruit_BMP085_Unified bme; // I2C
#endif
#ifdef USE_BMP280
#include <Adafruit_BMP280.h>
#define BME_STRING "BMP280"
Adafruit_BMP280 bme; // I2C
#endif
#ifdef USE_BME280
#include <Adafruit_BME280.h>
#define BME_STRING "BME280"
Adafruit_BME280 bme; // I2C
#endif
String get_Samovar_Status();
void set_power(bool On);
void clok();
void clok1();
void getjson(void);
String append_data();
void stopService(void);
void startService(void);
void CopyDSAddress(const uint8_t* DevSAddress, uint8_t* DevTAddress);
void set_beer_program(String WProgram);
void set_program(String WProgram);
void set_dist_program(String WProgram);
void set_nbk_program(String WProgram);
String getDSAddress(DeviceAddress deviceAddress);
void setupOpenLog(void);
void createFile(char* fileName);
void set_capacity(uint8_t cap);
void init_pump_pwm(uint8_t pin, int freq);
void set_pump_pwm(float duty);
//**************************************************************************************************************
// Функции для работы с сенсорами
//**************************************************************************************************************
void printAddress(DeviceAddress deviceAddress);
void reset_sensor_counter(void);
//***************************************************************************************************************
// считываем параметры с датчика BME680
//***************************************************************************************************************
void BME_getvalue(bool fl) {
if (!bmefound) {
bme_temp = -1;
bme_pressure = -1;
return;
}
if (xSemaphoreTake(xI2CSemaphore, (TickType_t)(30 / portTICK_RATE_MS)) == pdTRUE) {
#ifdef USE_BME680
// Tell BME680 to begin measurement.
if (bme.beginReading() == 0) {
return;
}
if (!bme.endReading()) {
return;
}
bme_temp = bme.temperature;
bme_pressure = bme.pressure / 100 * 0.75;
//bme_humidity = bme.humidity;
//filtered_val += (val - filtered_val) * 0.01;
#endif
#ifdef USE_BMP180
sensors_event_t event;
bme.getEvent(&event);
if (event.pressure) {
bme_pressure = event.pressure * 0.75;
float temp;
bme.getTemperature(&temp);
bme_temp = temp;
}
#endif
#ifdef USE_BMP280
bme_temp = bme.readTemperature();
bme_pressure = bme.readPressure() / 100 * 0.75;
#endif
#ifdef USE_BME280
bme_temp = bme.readTemperature();
bme_pressure = bme.readPressure() / 100 * 0.75;
#endif
xSemaphoreGive(xI2CSemaphore);
}
}
//***************************************************************************************************************
// считываем параметры с датчика XGZP6897D или MPX5010D
//***************************************************************************************************************
#if defined(USE_PRESSURE_XGZ) || defined(USE_PRESSURE_MPX)
void pressure_sensor_get() {
if (!use_pressure_sensor) {
pressure_value = -1;
return;
}
float t;
#ifdef USE_PRESSURE_XGZ
if (xSemaphoreTake(xI2CSemaphore, (TickType_t)(30 / portTICK_RATE_MS)) == pdTRUE) {
pressure_sensor.readSensor(t, pressure_value);
xSemaphoreGive(xI2CSemaphore);
}
pressure_value = pressure_value / 133.32; //переводим паскали в мм. рт. столба
#elif defined(USE_PRESSURE_MPX)
pressure_value = (analogRead(LUA_PIN) - 36.7) / 12;
#else
pressure_value = -1;
#endif
}
#endif
//***************************************************************************************************************
// считываем температуры с датчиков DS18B20
//***************************************************************************************************************
void DS_getvalue(void) {
// SteamSensor.avgTemp += 0.1;
// PipeSensor.avgTemp = 50;
// WaterSensor.avgTemp += 0.1;
// if (WaterSensor.avgTemp > 4) WaterSensor.avgTemp = 25;
// if (TankSensor.avgTemp < 90) TankSensor.avgTemp = 90;
// else {
// if (!boil_started)TankSensor.avgTemp += 0.002;
// else TankSensor.avgTemp += 0.01;
// }
//
// SteamSensor.avgTemp += SamSetup.DeltaSteamTemp;
// PipeSensor.avgTemp += SamSetup.DeltaPipeTemp;
// WaterSensor.avgTemp += SamSetup.DeltaWaterTemp;
// TankSensor.avgTemp += SamSetup.DeltaTankTemp;
// ACPSensor.avgTemp += SamSetup.DeltaACPTemp;
//
// return;
float ss, ps, ws, ts, acp;
float correctT = 0;
//Считаем корректировку температуры от атмосферного давления
if (bme_pressure > 0 && PowerOn) {
correctT = (760 - bme_pressure) * 0.037;
}
ss = correctT + sensors.getTempC(SteamSensor.Sensor); // считываем температуру с датчика 0
ps = correctT + sensors.getTempC(PipeSensor.Sensor); // считываем температуру с датчика 1
ws = sensors.getTempC(WaterSensor.Sensor); // считываем температуру с датчика 2
ts = correctT + sensors.getTempC(TankSensor.Sensor); // считываем температуру с датчика 3
acp = sensors.getTempC(ACPSensor.Sensor); // считываем температуру с датчика 4
#ifdef USE_PRESSURE_1WIRE
float pv;
uint8_t p_addr[8] = USE_PRESSURE_1WIRE;
pv = sensors.getTempC(p_addr); // считываем давление с расширителя по 1Wire
if (pv > -120) {
pressure_value = pv;
use_pressure_sensor = true;
} else {
//pressure_value = -1;
//use_pressure_sensor = false;
}
#endif
// float randNumber;
// randNumber = random(100) / float(500);
// if (TankSensor.avgTemp < 87) TankSensor.avgTemp = 89;
// if (WaterSensor.avgTemp < 47) WaterSensor.avgTemp = 49;
// static float coef = 0;
// coef = heater_state ? 0.1 + randNumber : -0.1 - randNumber;
// //if (coef > 0.5) coef = 0.5;
// //if (coef < -0.5) coef = -0.5;
// ts = TankSensor.avgTemp + coef;
// ws = WaterSensor.avgTemp + coef;
sensors.requestTemperatures();
if (ss > -10) {
SteamSensor.avgTemp = ss + SamSetup.DeltaSteamTemp;
SteamSensor.PrevTemp = SteamSensor.avgTemp;
SteamSensor.ErrCount = 0;
} else {
if (SteamSensor.PrevTemp > 0) SteamSensor.ErrCount++;
}
if (ps > -10) {
PipeSensor.avgTemp = ps + SamSetup.DeltaPipeTemp;
PipeSensor.PrevTemp = PipeSensor.avgTemp;
PipeSensor.ErrCount = 0;
} else {
if (PipeSensor.PrevTemp > 0) PipeSensor.ErrCount++;
}
if (ws > -10) {
WaterSensor.avgTemp = ws + SamSetup.DeltaWaterTemp;
WaterSensor.PrevTemp = WaterSensor.avgTemp;
WaterSensor.ErrCount = 0;
} else {
if (WaterSensor.PrevTemp > 0) WaterSensor.ErrCount++;
}
if (ts > -10) {
TankSensor.avgTemp = ts + SamSetup.DeltaTankTemp;
TankSensor.PrevTemp = TankSensor.avgTemp;
TankSensor.ErrCount = 0;
} else {
if (TankSensor.PrevTemp > 0) TankSensor.ErrCount++;
}
if (acp > -10) {
ACPSensor.avgTemp = acp + SamSetup.DeltaACPTemp;
ACPSensor.PrevTemp = ACPSensor.avgTemp;
ACPSensor.ErrCount = 0;
} else {
if (ACPSensor.PrevTemp > 0) ACPSensor.ErrCount++;
}
#ifdef __SAMOVAR_DEBUG1
SteamSensor.avgTemp = SamSetup.DeltaSteamTemp;
PipeSensor.avgTemp = SamSetup.DeltaPipeTemp;
WaterSensor.avgTemp = SamSetup.DeltaWaterTemp;
TankSensor.avgTemp = SamSetup.DeltaTankTemp;
ACPSensor.avgTemp = SamSetup.DeltaACPTemp;
#endif
}
void scan_ds_adress() {
sensors.begin(); // стартуем датчики температуры
uint8_t dc = 0;
while (sensors.getAddress(DSAddr[dc], dc)) {
sensors.setResolution(DSAddr[dc], 12); // устанавливаем разрешение для датчика
dc++;
if (dc > 5) break;
}
DScnt = dc;
// определяем устройства на шине
#ifdef __SAMOVAR_DEBUG
Serial.print("Locating DS18B20...");
Serial.print("Found ");
Serial.print(DScnt, DEC);
Serial.println(" devices.");
#endif
#ifdef __SAMOVAR_DEBUG
Serial.print("1 Sensor Address: "); // пишем адрес датчика 0
printAddress(DSAddr[0]);
Serial.println();
Serial.print("2 Sensor Address: "); // пишем адрес датчика 1
printAddress(DSAddr[1]);
Serial.println();
Serial.print("3 Sensor Address: "); // пишем адрес датчика 2
printAddress(DSAddr[2]);
Serial.println();
Serial.print("4 Sensor Address: "); // пишем адрес датчика 3
printAddress(DSAddr[3]);
Serial.println();
Serial.print("5 Sensor Address: "); // пишем адрес датчика 4
printAddress(DSAddr[4]);
Serial.println();
Serial.print("6 Sensor Address: "); // пишем адрес датчика 5
printAddress(DSAddr[5]);
Serial.println();
#endif
sensors.setWaitForConversion(false); // работаем в асинхронном режиме
sensors.requestTemperatures();
//delay(750);
#ifdef __SAMOVAR_DEBUG
Serial.print("1 Sensor Resolution: "); // пишем разрешение для датчика 0
Serial.print(sensors.getResolution(DSAddr[0]), DEC);
Serial.println();
Serial.print("2 Sensor Resolution: "); // пишем разрешение для датчика 1
Serial.print(sensors.getResolution(DSAddr[1]), DEC);
Serial.println();
Serial.print("3 Sensor Resolution: "); // пишем разрешение для датчика 2
Serial.print(sensors.getResolution(DSAddr[2]), DEC);
Serial.println();
Serial.print("4 Sensor Resolution: "); // пишем разрешение для датчика 3
Serial.print(sensors.getResolution(DSAddr[3]), DEC);
Serial.println();
Serial.print("5 Sensor Resolution: "); // пишем разрешение для датчика 3
Serial.print(sensors.getResolution(DSAddr[4]), DEC);
Serial.println();
#endif
}
void sensor_init(void) {
#ifdef __SAMOVAR_DEBUG
Serial.println("Pressure sensor initialization");
#endif
#ifdef BME_STRING
writeString((String)BME_STRING + " init... ", 3);
delay(800);
#ifdef USE_BMP280_ALT
if (!bme.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID)) {
#else
#ifdef USE_BMP180
if (!bme.begin(BMP085_MODE_STANDARD)) {
#else
if (!bme.begin()) {
#endif
#endif
writeString((String)BME_STRING + " not found ", 3);
#ifdef __SAMOVAR_DEBUG
Serial.println((String)BME_STRING + " not found");
#endif
bmefound = false;
//Serial.println(F("Could not find a valid BME680 sensor, check wiring!"));
} else {
#ifdef USE_BME680
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_2X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
#endif
#ifdef USE_BMP280_1
bme.setSampling(Adafruit_BMP280::MODE_FORCED, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X1, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X4, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X2, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
#endif
#ifdef USE_BME280_1
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X4, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF);
#endif
}
#else
bmefound = false;
#endif
writeString("DS1820 init... ", 3);
scan_ds_adress();
writeString("Found " + (String)DScnt + " ", 4);
//Для шагового двигателя устанавливаем режим работы - следовать до позиции
// stepper.setRunMode(FOLLOW_POS);
// установка макс. скорости в шагах/сек
stepper.setMaxSpeed(STEPPER_MAX_SPEED);
//stepper.setSpeed(0);
//Драйвер выключится по достижении позиции
stepper.autoPower(true);
#ifdef STEPPER_REVERSE
stepper.reverse(true);
#endif
#ifdef USE_WATERSENSOR
//Настраиваем датчик потока
pinMode(WATERSENSOR_PIN, INPUT);
#endif
// set_program("H;3;1;1;0;45\nB;5;2;1;0;45\nH;6;3;1;0;45\n");
if (Samovar_Mode == SAMOVAR_BEER_MODE || Samovar_Mode == SAMOVAR_SUVID_MODE) {
set_beer_program("M;45;0;1^-1^2^2;0\nP;45;1;1^-1^2^3;0\nP;60;1;1^-1^2^3;0\nW;0;0;1^-1^2^3;0\nB;0;1;1^-1^2^3;0\nC;30;0;1^-1^2^3;0\n");
} else if (Samovar_Mode == SAMOVAR_DISTILLATION_MODE) {
set_dist_program("A;80.00;1;0\nS;0.50;2;0\nS;0.30;3;0\n");
} else if (Samovar_Mode == SAMOVAR_NBK_MODE) {
set_nbk_program("H;3;0;3000;0\nS;18;0;2400;0\nT;20;0;0;0\nP;0;20;-100;0\nW;0;0;0;0\n");
} else {
set_program("H;450;0.1;1;0;45\nB;450;1;1;0;45\nH;450;0.1;1;0;45\n");
}
#ifdef SAMOVAR_USE_SEM_AVR
//Если SEM_AVR иницииурем порт
#ifdef __SAMOVAR_DEBUG
Serial.println("Init SEM_AVR");
#endif
xSemaphoreAVR = xSemaphoreCreateBinaryStatic(&xSemaphoreBufferAVR);
xSemaphoreGive(xSemaphoreAVR);
Serial2.setTimeout(500);
//Serial2.setRxBufferSize(12);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
#define USE_SERIAL
#endif
#ifdef SAMOVAR_USE_RMVK
#ifndef USE_SERIAL
#ifdef __SAMOVAR_DEBUG
Serial.println("Init RMVK");
#endif
//Иначе работаем с RMVK
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
Serial2.end();
RMVK_init();
#define USE_SERIAL
#endif
#endif
#ifdef SAMOVAR_USE_POWER
#ifndef USE_SERIAL
#ifdef __SAMOVAR_DEBUG
Serial.println("Init KVIC");
#endif
Serial2.setTimeout(300);
Serial2.setRxBufferSize(25);
#ifdef KVIC_USE_9600
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
#else
Serial2.begin(38400, SERIAL_8N1, RXD2, TXD2);
#endif
#define USE_SERIAL
#endif
#endif
#ifdef USE_WATER_PUMP
init_pump_pwm(WATER_PUMP_PIN, PUMP_PWM_FREQ);
set_pump_pwm(0);
#endif
use_pressure_sensor = false;
#ifdef USE_PRESSURE_XGZ
#ifdef __SAMOVAR_DEBUG
Serial.println("Init pressure sensor");
#endif
if (!pressure_sensor.begin()) // initialize and check the device
{
use_pressure_sensor = false;
#ifdef __SAMOVAR_DEBUG
Serial.println("Device not responding.");
#endif
} else use_pressure_sensor = true;
#endif
#ifdef USE_PRESSURE_MPX
use_pressure_sensor = true;
#endif
#ifdef USE_PRESSURE_1WIRE
use_pressure_sensor = true;
#endif
reset_sensor_counter();
// regulator.hysteresis = 0.3;
// regulator.k = 0.3;
// regulator.dT = 4000; //система инерционная, считаем скорость раз в четыре секунды
// regulator.setLimits(0, WindowSize);
// regulator.setDirection(REVERSE);
heaterPID.SetSampleTime(1000);
heaterPID.SetOutputLimits(0, 100);
heaterPID.SetTunings(SamSetup.Kp, SamSetup.Ki, SamSetup.Kd);
}
//Обнуляем все счетчики
void reset_sensor_counter(void) {
sam_command_sync = SAMOVAR_NONE;
stopService();
stepper.setMaxSpeed(0);
//stepper.setSpeed(0);
stepper.brake();
stepper.disable();
stepper.setCurrent(0);
stepper.setTarget(0);
set_capacity(0);
alarm_h_min = 0;
alarm_t_min = 0;
t_min = 0;
b_t_temp_prev = 0;
b_t_time_min = 0;
b_t_time_delay = 0;
#ifdef SAMOVAR_USE_POWER
alarm_c_min = 0;
alarm_c_low_min = 0;
prev_target_power_volt = 0;
#endif
d_s_time_min = 0;
d_s_temp_finish = 0;
current_power_volt = 0.0;
target_power_volt = 0.0;
ProgramNum = 0;
SteamSensor.BodyTemp = 0;
PipeSensor.BodyTemp = 0;
WaterSensor.BodyTemp = 0;
TankSensor.BodyTemp = 0;
ACPSensor.BodyTemp = 0;
SteamSensor.PrevTemp = 0;
PipeSensor.PrevTemp = 0;
WaterSensor.PrevTemp = 0;
TankSensor.PrevTemp = 0;
ACPSensor.PrevTemp = 0;
ActualVolumePerHour = 0;
SamovarStatusInt = 0;
startval = 0;
PauseOn = false;
program_Wait = false;
SteamSensor.Start_Pressure = 0;
WthdrwlProgress = 0;
TargetStepps = 0;
begintime = 0;
d_s_temp_prev = 0;
is_self_test = false;
if (fileToAppend) {
fileToAppend.close();
}
if (bme_pressure < 100) BME_getvalue(false);
start_pressure = bme_pressure;
boil_started = false;
boil_temp = 0;
alcohol_s = 0;
b_t_time_delay = 0;
if (xSemaphore != NULL) xSemaphoreGive(xSemaphore);
#ifdef SAMOVAR_USE_SEM_AVR
if (xSemaphoreAVR != NULL) xSemaphoreGive(xSemaphoreAVR);
#endif
set_power(false);
sam_command_sync = SAMOVAR_NONE;
get_Samovar_Status();
bk_pwm = PWM_LOW_VALUE * 40;
#ifdef SAMOVAR_USE_POWER
power_err_cnt = 0;
#endif
}
inline String format_float(float v, int d) {
char outstr[15];
dtostrf(v, 1, d, outstr);
return outstr;
}
void printAddress(DeviceAddress deviceAddress) // функция печати адреса DS18B20
{
Serial.print(getDSAddress(deviceAddress));
}
String getDSAddress(DeviceAddress deviceAddress) {
String dsaddr;
for (uint8_t j = 0; j < 8; j++) {
if (deviceAddress[j] < 16) dsaddr += "0";
dsaddr += String(deviceAddress[j], HEX);
}
return dsaddr;
}
String get_DSAddressList(String Address) {
String s = "<option value='-1'>NONE</option>";
String dsaddr = "";
for (uint8_t i = 0; i != DScnt; i++) {
dsaddr = getDSAddress(DSAddr[i]);
s += "<option value='" + String(i) + "'";
if (Address == dsaddr) s = s + " " + "selected";
s = s + ">" + dsaddr + "</option>";
}
return s;
}
void CopyDSAddress(const uint8_t* DevSAddress, uint8_t* DevTAddress) {
for (uint8_t dsj = 0; dsj < 8; dsj++) {
DevTAddress[dsj] = DevSAddress[dsj];
}
}
#endif