-
Notifications
You must be signed in to change notification settings - Fork 23
/
Valden_HeatPumpController.ino
3184 lines (2867 loc) · 102 KB
/
Valden_HeatPumpController.ino
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Valden Heat Pump.
Heat Pump Controller firmware.
https://github.com/OpenHP/
Copyright (C) 2018-2021 [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//-----------------------USER OPTIONS-----------------------
//#define SELFTEST_RELAYS_LEDS_SPEAKER //speaker and relays QA test, uncomment to enable
//#define SELFTEST_EEV //EEV QA test, uncomment to enable
//#define SELFTEST_T_SENSORS //temperature sensors QA test, uncomment to enable
//communication protocol with external world
//#define RS485_JSON 1 //json, external systems integration
//#define RS485_HUMAN 2 //RS485 is used in the same way as the local console, warning: Use only if 2 devices (server and this controller) connected to the same RS485 line
#define RS485_MODBUS 3 //default, MODBUS via RS485, connection to the display (both sensor or 1602, see https://GitHub.com/OpenHP/Display/) or connection to any other MODBUS application or device
//system type, comment both if HP with EEV
//#define EEV_ONLY //Valden controller as EEV controller: NO target T sensor. No relays. Oly EEV. Sensors required: Tae, Tbe, current sensor. Additional T sensors can be used but not required.
//#define NO_EEV //capillary tube or TXV, EEV not used
//which sensor is used to check setpoint, uncomment one of options
#define SETPOINT_THI //"warm floor" scheme: "hot in" (Thi) temperature used as setpoint
//#define SETPOINT_TS1 //"swimming pool" or "water tank heater" scheme: "sensor 1" (Ts1) is used as setpoint and located somewhere in a water tank
#define HUMAN_AUTOINFO 30000 //print stats to console, every milliseconds
#define WATCHDOG //disable for older bootloaders
//-----------------------USER OPTIONS END-----------------------
//-----------------------Fine Tuning OPTIONS-----------------------
//next sections: advanced options
//-----------------------T Sensors -----------------------
//temperature sensors used in a system, comment to disable
#define T_cold_in; //cold side (heat source) inlet sensor
#define T_cold_out; //cold side outlet sensor
#define T_before_evaporator; //"before" and "after evaporator" sensors required to control EEV, both "EEV_ONLY" and "full" schemes
#define T_after_evaporator; //"before" and "after evaporator" sensors required to control EEV, both "EEV_ONLY" and "full" schemes
//#define T_separator_gas; //no longer used (PCB 1.3 MI +) artifact from experimental scheme with separator
//#define T_separator_liquid; //no longer used (PCB 1.3 MI +) artifact from experimental scheme with separator
//#define T_before_valve; //no longer used (PCB 1.3 MI +) artifact from experimental scheme with separator
//#define T_suction; //no longer used (PCB 1.3 MI +) artifact from experimental scheme with separator
#ifdef SETPOINT_TS1
#define T_sensor_1; //T values from the additional sensor S1 used as a "setpoint" in "pool" or "water tank heater" schemes
#endif
//!!!
#define T_sensor_2; //additional sensor, any source; for example, outdoor temperature, in-case temperature, and so on
#define T_crc; //if defined, enables the crankcase T sensor and crankcase heater on the relay "Crankcase heater"
//#define T_regenerator; //an additional sensor, the regenerator temperature sensor (inlet or outlet or housing); used only to obtain a temperature data if necessary
#define T_afrer_condenser; //after condenser (and before valve)
//!!!#define T_before_condenser; //before condenser (discharge)
#define T_hot_out; //hot side outlet
//In full scheme Hot IN required! Optional in "EEV_ONLY" scheme (see "EEV_ONLY" option),
#define T_hot_in; //hot side inlet
//-----------------------TEMPERATURES-----------------------
#define MAGIC 0x66; //change this value if you want to rewrite the T setpoint in EEPROM
#define T_SETPOINT 26.0; //This is a predefined target temperature value (start temperature). EEPROM-saved. Ways to change this value: 1. Console command 2. Change the "setpoint" on a display 3. Change value here AND change "magic number" 4. JSON command
#define T_SETPOINT_MAX 48.0; //maximum "setpoint" temperature that an ordinary user can set
#define T_SETPOINT_MIN 10.0; //min. "setpoint" temperature that an ordinary user can set, lower values not recommended until antifreeze fluids at hot side used.
#define T_CRANKCASE_MIN 8.0; //compressor (crankcase) min. temperature, HP will not start if T lower
#define T_CRANKCASE_MAX 110.0; //compressor (crankcase) max. temperature, overheating protection, HP will stop if T higher
#define T_CRANKCASE_HEAT_THRESHOLD 16.0; //crankcase heater threshold, the compressor heater will be powered on if T lower
#define T_WORKINGOK_CRANKCASE_MIN 25.0; //compressor temperature: additional check. HP will stop if T is lower than this value after 5 minutes of work. Do not set the value too high to ensure normal operation after long pauses.
#define T_BEFORE_CONDENSER_MAX 108.0; //discharge MAX, system stops if discharge higher
#define T_COLDREF_MIN -14.0; //suction min., HP stops if T lower, cold side (glycol) loop freeze protection and compressor protection against liquid
#define T_BEFORE_EVAP_WORK_MIN -25.5; //!!!before evaporator (after valve) min. T; can be very low for a few minutes after a startup, ex: capillary tube in some conditions; and for all systems: after long shut-off, lack of refrigerant, 1st starts, and many others
#define T_COLD_MIN -15.5; //cold side (glycol) loop freeze protection: HP stops if inlet or outlet temperature lower
#define T_HOT_MAX 50.0; //hot loop: HP stops if hot side inlet or outlet temperature higher than this threshold
//#define T_REG_HEAT_THRESHOLD 17.0; //no longer used (PCB 1.3 MI +) artifact from experimental scheme with separator
//#define T_HOTCIRCLE_DELTA_MIN 2.0; //not used since ~FW v1.6, "water heater with intermediate heat exchanger" scheme, where Ts1 == "sensor in water"; hot side CP will be switched on if "Ts1 - hot_out > T_HOTCIRCLE_DELTA_MIN"
//-----------------------WATTS AND CYCLES TIMES-----------------------
//time: milliseconds, power: watts
#define MAX_WATTS 1000.0 + 70.0 + 80.0 //power limit, watt, HP stops if exceeded, examples: // installation1: compressor 165: 920 Watts, + 35 watts 25/4 circ. pump at 1st speed + 53 watts 25/4 circ. pump at 2nd speed
// installation2: compressor unk: ~1000 + hot CP 70 + cold CP 80 = 1150 watts
// installation3: and so on
#define POWERON_PAUSE 300000 //after power on: wait 5 minutes before starting HP (power faults protection)
#define MINCYCLE_POWEROFF 600000 //after a normal compressor stop: 10 minutes pause (max 99999 seconds)
#define MINCYCLE_POWERON 3600000 //after compressor start: minimum compressor operation time, i.e. work time is not less than this value (or more, depending on the setpoint temperature) 60 minutes = 3.6 KK 120mins = 5.4 kK.
#define POWERON_HIGHTIME 7000 //after compressor start: defines time when power consumption can be 3 times greater than normal, 7 sec. by default
#define COLDCIRCLE_PREPARE 90000 //before compressor start: power on cold CP and wait 90 sec.; if false start: CP will off twice this time; and (hotcircle_stop_after - this_value) must be > hotcircle_check_prepare or HP will go sleep cycle instead of start
#define DEFFERED_STOP_HOTCIRCLE 1200000 //after compressor stop: wait 20 minutes, if no need to start compressor: stop hot WP; value must be > 0
#define HOTCIRCLE_START_EVERY 2400000 //while pauses: pump on "hot side" starts every 40 minutes (by default) (max 9999 seconds) to circulate water and get exact temperature reading, option used if "warm floor" installation (Thi as setpoint)...
#define HOTCIRCLE_CHECK_PREPARE 150000 //while pauses: ...and wait for temperature stabilization 2.5 minutes (by default), after that do setpoint checks...
#define HOTCIRCLE_STOP_AFTER (HOTCIRCLE_CHECK_PREPARE + COLDCIRCLE_PREPARE + 30000) //...and then stop after few minutes of circulating, if temperature is high and no need to start compressor; value must be check_prepare + coldcircle_prepare + 30 seconds (or more)
//-----------------------EEV-----------------------
//If you are using a capillary tube or TXV: simply skip next section.
//Depending on how many milliseconds allocated per step, the speed of automatic tuning will change.
//Remember that your refrigeration system reaction on every step is not immediate. The system reacts after a few minutes, sometimes after tens of minutes.
#define EEV_MAXPULSES 250 //max steps, 250 is tested for sanhua 1.3
//steps tuning: milliseconds per fast and slow (precise) steps
#define EEV_PULSE_FCLOSE_MILLIS 20 //(20 tube evaporator) fast closing, closing on danger (milliseconds per step)
#define EEV_PULSE_CLOSE_MILLIS 45000 //(50000 tube evaporator) accurate closing while the compressor works (milliseconds per step)
#define EEV_PULSE_WOPEN_MILLIS 20 //(20 tube evaporator) standby (waiting) pos. set (milliseconds per step)
#define EEV_PULSE_FOPEN_MILLIS 1400 //(1300 tube evaporator) fast opening, fast search (milliseconds per step)
#define EEV_PULSE_OPEN_MILLIS 30000 //(60000 tube evaporator) accurate opening while the compressor works (milliseconds per step)
#define EEV_STOP_HOLD 500 //0.1..1sec for Sanhua hold time (milliseconds per step)
#define EEV_CLOSEEVERY 86400000 //86400000: EEV full close (zero calibration) every 24 hours, executed while HP is NOT working (milliseconds per cycle)
//positions
#define EEV_CLOSE_ADD_PULSES 8 //read below, additional steps after zero position while full closing
#define EEV_OPEN_AFTER_CLOSE 45 //0 - set the zero position, then add EEV_CLOSE_ADD_PULSES (zero insurance, read EEV guides for this value) and stop, EEV will be in zero position.
//N - set the zero position, then add EEV_CLOSE_ADD_PULSES, than open EEV on EEV_OPEN_AFTER_CLOSE pulses
//i.e. it's a "waiting position" while HP isn't working, value must be <= MINWORKPOS
#define EEV_MINWORKPOS 50 //position will be not less during normal work, open EEV to this position after compressor start
//temperatures
#define EEV_PRECISE_START 8.6 //(8.6 tube evaporator) precise tuning threshold: make slower pulses if (real_diff-target_diff) less than this value. Used for fine auto-tuning
#define EEV_EMERG_DIFF 1.7 //(2.5 tube evaporator) liquid at suction threshold: if dangerous condition occurred, real_diff =< (target_diff - EEV_EMERG_DIFF) then EEV will be closed to min. work position //Ex: EEV_EMERG_DIFF = 2.0, target diff 5.0, if real_diff =< (5.0 - 2.0) then EEV will be closed to EEV_MINWORKPOS
#define EEV_HYSTERESIS 0.45 //(0.6 tube evaporator) hysteresis, to stop fine tuning: must be less than EEV_PRECISE_START, ex: target difference = 4.0, hysteresis = 0.3, no EEV pulses will be done while real difference in range 4.0..4.3
#define EEV_TARGET_TEMP_DIFF 3.6 //(3.6 tube evaporator) target difference between Before Evaporator and After Evaporator, the head of the whole algorithm
//additional options
#define EEV_REOPENLAST 1 ///1 = reopen to last position on compressor start, useful for ordinary schemes with everyday working cycles, 0 = not
#define EEV_REOPENMINTIME 40000 //after system start: min. delay between "min. work pos." (must be > 0 in this case and > waiting position) set and reopening start
//#define EEV_MANUAL //comment to disable, manual set of EEV position via a console; warning: this option will stop all EEV auto-activities, including zero position find procedure; so this option not recommended: switch auto/manual mode from a console
//do not use next option if you're not sure what are you doing
//#define EEV_DEBUG //debug, useful during system fine-tuning, works both with local serial and RS485_HUMAN
//-----------------------ADDRESSES-----------------------
const char devID = 0x45; //used only if JSON communication, does not matter for MODBUS and Valden display https://github.com/OpenHP/Display/
const char hostID = 0x30; //used only if JSON communication, not used for MODBUS
//-----------------------OTHER-----------------------
#define MAX_SEQUENTIAL_ERRORS 15 //max cycles to wait auto-clean error, ex: T sensor appears, stop compressor after counter exceeded (millis_cycle * MAX_SEQUENTIAL_ERRORS)
//-----------------------Fine Tuning OPTIONS END -----------------------
//-----------------------changelog-----------------------
/*
v1.0, 01 Sep 2019:
+ initial version, hardware and software branch ready
v1.1: 21 Sep 2019:
+ Dev and Host ID to header
v1.2: 20 Dec 2019:
+- ?seems to be fixed minor bug while HP stopped: wattage is 0, if tCrc < T_CRANKCASE_HEAT_THRESHOLD and may be few sensors absence
+ min_user_t/max_user_t to header
v1.3: 05 Jan 2020:
+ manual EEV mode (high priority, ex: new system 1st starts and charge)
+ rs485_modbus
+ reopen to last EEV value at startup
v1.4: 22 Jan 2020
+ crankcase naming
v1.5: 05 Jun 2020
+ minor modbus updates
v1.6: 09 Dec 2020
+ NO_EEV option
+ some variables renames
+ Tho instead of Thi (stop conditions) bugfix
+ Last Start Message added
v1.7: 03 Feb 2021
+ 1.3 PCB revision support, previous revisions also supported
+ enable cold circle if tci < col_min (circulate ground loop, if outdoor installation and very cold and deep freeze)
+ inputs support
+ add option "Thi" and "Ts1" to header, enable Ts1 by this option
+ temperature check after start of hot side circle + 5 mins for Thi target
v1.8: 06 Feb 2021
+ very rare case: 0.0 readings, 2-3 attempts then pass 0.0
+ countdown for compressor relay after cold CP start (stab. cold loop T)
+ self-test options to header
v1.9-1.11: 25-27 Feb 2021:
+ lot of small workflow logic and user terminal changes
v1.12: 21 Mar 2021:
+ TS1/THO #define way fix
+ CWP and HWP prepare optimisation
v1.13: 26 Mar 2021:
+ rounding error via Modbus found and fixed
//TODO:
? lower bit resolution for all sensors, except Tbe, Tae, Ts1 ?
? poss. DoS: infinite read to nowhere, fix it, set finite counter (ex: 200)
? add "heater start" and "cold circle start" and "not start HP" if t_crc < t_coldin/coldout(?)/tae/tbe(?) + 2.0
? ref. migration protection for summer season with long waiting periods: start cold circle and crankcase heater if tCrc =< tci+1, add option to header
? EEV manual mode and position by RS485 python or modbus command ?
? add speaker and err code for ""ERR: no Tae or Tbe for EEV!""
? deffered HWP stop: check HP stop cause, stop HWP if protective/error stop
? wclose and fclose to EEV
? valve_4way
? rewite re-init proc from MAGIC to another way
? EEV: target to EEPROM (?? no need ?)
? EEV: define maximum working position
*/
//-----------------------changelog END-----------------------
// DS18B20 pins: GND DATA VDD
//Connections:
//DS18B20 Pinout (Left to Right, pins down, flat side toward you)
//- Left = Ground
//- Center = Signal (Pin N of arduino): (with 3.3K to 4.7K resistor to +5 or 3.3 )
//- Right = +5 or +3.3 V
//
//Speaker
//
// high volume scheme: +---- +5V (12V not tested)
// |
// +----+
// 1MOhm piezo
// +----+
// |(C)
// pin -> 1.6 kOhms -> (B) 2n2222 < front here
// |(E)
// +--- GND
//
/*
scheme SCT-013-000:
2 pins used: tip and sleeve, center (ring) not used http://cms.35g.tw/coding/wp-content/uploads/2014/09/SCT-013-000_UNO-1.jpg
pins are interchangeable due to AC
32 Ohms (22+10) between sensor pins (35 == ideal)
Pin1:
- via elect. cap. to GND
- via ~10K..470K resistor to GND
- via ~10K..470K resistor to +5 (same as prev.)
if 10K+10K used: current is 25mA
use 100K+100K for 3 phases
Pin2:
- to analog pin
- via 32..35 Ohms resistor to Pin1
+5 -------------------------+
|
|
# R1 10K+
|
|
|~2.5 at this point
+---------------+--------------------------------------+----+
| | | |
#_ elect. cap. # R2 10K+ (same as R1) SCT-013-000 $ # R3 = 35 Ohms (ideal case), 32 used
| | | |
GND --------+---------------+ +----+--------> to Analog pin
WARNING: calibrate 3 sensors together, from different sellers, due to case of incorrectly worked 1 of 3 sensor
P(watts)=220*220/R(Ohms)
*/
//
//MAX 485 voltage - 5V
//
// use resistor at RS-485 GND
// 1st test: 10k result lot of issues
// 2nd test: 1k, issues
// 3rd test: 100, see discussions
//16-ch Multiplexer EN pin: active LOW, connect to GND
/*
relay 1: heat pump
relay 2: hot side circulator pump
relay 3: cold side circulator pump
relay 4: crankcase heater
relay 5: (1.3+: not used anymore)
relay 6: reserved
relay 7: reserved
T sensors:
0 cold_in;
1 cold_out;
2 before_evaporator;
3 after_evaporator;
4 separator_gas; //if flooded evaporator: separator out
5 separator_liquid; //if flooded evaporator: separator out
6 before_valve; //before expansion valve, if regenerator used
7 suction; //compressor suction, if regenerator
8 sensor_1; //additional sensor 1
9 sensor_2; //additional sensor 2
A crankcase; //compressor case
B regenerator;
C afrer_condenser;
D before_condenser;
E hot_out;
F hot_in;
*/
String fw_version = "1.13";
//hardware resources
#define RELAY_HEATPUMP A2
#define RELAY_HOTSIDE_CIRCLE A1
#define PR_LOW A6
#define PR_HIGH A7
#define OW_BUS_ALLTSENSORS 9
#define speakerOut 6
#define em_pin1 A3
String hw_version = "v1.1+";
#define LATCH_595 3
#define CLK_595 2
#define DATA_595 7
#define OE_595 4
//---------------------------memory debug
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
int freeMemory() {
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
//---------------------------memory debug END
#include <avr/wdt.h>
#include <EEPROM.h>
#define SEED 0xFFFF
#define POLY 0xA001
unsigned int crc16;
int cf;
#define MODBUS_MR 50 //50 ok now
#include <SoftwareSerial.h>
#define SerialRX 12 //RX connected to RO - Receiver Output
#define SerialTX 11 //TX connected to DI - Driver Output Pin
#define SerialTxControl 13 //RS485 Direction control DE and RE to this pin
#define RS485Transmit HIGH
#define RS485Receive LOW
SoftwareSerial RS485Serial(SerialRX, SerialTX); // RX, TX
#include <OneWire.h>
#include <DallasTemperature.h>
//library's DEVICE_DISCONNECTED_C -127.0
OneWire ow_ALLTSENSORS(OW_BUS_ALLTSENSORS);
DallasTemperature s_allTsensors(&ow_ALLTSENSORS);
DeviceAddress dev_addr; //temp
//short names used to prevent unreadeable source
#ifdef T_cold_in
bool TciE = 1;
#else
bool TciE = 0;
#endif
double Tci = -127.0;
#ifdef T_cold_out
bool TcoE = 1;
#else
bool TcoE = 0;
#endif
double Tco = -127.0;
#ifdef T_before_evaporator
bool TbeE = 1;
#else
bool TbeE = 0;
#endif
double Tbe = -127.0;
#ifdef T_after_evaporator
bool TaeE = 1;
#else
bool TaeE = 0;
#endif
double Tae = -127.0;
/*
#ifdef T_separator_gas
bool TsgE = 1;
#else
bool TsgE = 0;
#endif
double Tsg = -127.0;
#ifdef T_separator_liquid
bool TslE = 1;
#else
bool TslE = 0;
#endif
double Tsl = -127.0;
#ifdef T_before_valve
bool TbvE = 1;
#else
bool TbvE = 0;
#endif
double Tbv = -127.0;
#ifdef T_suction
bool TsucE = 1;
#else
bool TsucE = 0;
#endif
double Tsuc = -127.0;
*/
#ifdef T_sensor_1
bool Ts1E = 1;
#else
bool Ts1E = 0;
#endif
double Ts1 = -127.0;
#ifdef T_sensor_2
bool Ts2E = 1;
#else
bool Ts2E = 0;
#endif
double Ts2 = -127.0;
#ifdef T_crc
bool TcrcE = 1;
#else
bool TcrcE = 0;
#endif
double Tcrc = -127.0;
#ifdef T_regenerator
bool TregE = 1;
#else
bool TregE = 0;
#endif
double Treg = -127.0;
#ifdef T_afrer_condenser
bool TacE = 1;
#else
bool TacE = 0;
#endif
double Tac = -127.0;
#ifdef T_before_condenser
bool TbcE = 1;
#else
bool TbcE = 0;
#endif
double Tbc = -127.0;
#ifdef T_hot_out
bool ThoE = 1;
#else
bool ThoE = 0;
#endif
double Tho = -127.0;
#ifdef T_hot_in
bool ThiE = 1;
#else
bool ThiE = 0;
#endif
double Thi = -127.0;
double T_setpoint = T_SETPOINT;
double T_setpoint_lastsaved = T_setpoint;
double T_EEV_setpoint = EEV_TARGET_TEMP_DIFF;
double T_EEV_dt = 0.0; //real, used during run
const double cT_setpoint_max = T_SETPOINT_MAX;
const double cT_setpoint_min = T_SETPOINT_MIN;
//const double cT_hotcircle_delta_min = T_HOTCIRCLE_DELTA_MIN;
const double cT_crc_min = T_CRANKCASE_MIN;
const double cT_crc_max = T_CRANKCASE_MAX;
const double cT_crc_heat_threshold = T_CRANKCASE_HEAT_THRESHOLD;
//const double cT_reg_heat_threshold = T_REG_HEAT_THRESHOLD;
const double cT_before_condenser_max = T_BEFORE_CONDENSER_MAX;
const double cT_coldref_min = T_COLDREF_MIN;
const double cT_before_evap_work_min = T_BEFORE_EVAP_WORK_MIN;
const double cT_cold_min = T_COLD_MIN;
const double cT_hot_max = T_HOT_MAX;
//const double cT_workingOK_cold_delta_min = 0.5; // 0.7 - 1st try, 2nd try 0.5
//const double cT_workingOK_hot_delta_min = 0.5;
const double cT_workingOK_crc_min = T_WORKINGOK_CRANKCASE_MIN; //need to be not very high to normal start after deep freeze
const double c_wattage_max = MAX_WATTS; //FUNAI: 1000W seems to be normal working wattage INCLUDING 1(one) CR25/4 at 3rd speed
//PH165X1CY : 920 Watts, 4.2 A
const double c_workingOK_wattage_min = c_wattage_max/5; //
unsigned int pr_low_state_anal = 0; //sensors are NC for spec. conditions, so 1 == ok, 0 == error
unsigned int pr_high_state_anal = 0; //
bool pr_low_state_bool = 1; //sensors are NC for spec. conditions, so 1 == ok, 0 == error
bool pr_high_state_bool = 1; //
bool heatpump_state = 0;
bool hotside_circle_state = 0;
bool coldside_circle_state = 0;
bool crc_heater_state = 0;
//bool reg_heater_state = 0;
//bool relay6_state = 0;
//bool relay7_state = 0;
bool LED_OK_state = 0;
bool LED_ERR_state = 0;
bool S0_state = 0;
bool S1_state = 0;
bool S2_state = 0;
bool S3_state = 0;
bool EEV1_state = 0;
bool EEV2_state = 0;
bool EEV3_state = 0;
bool EEV4_state = 0;
const long poweron_pause = POWERON_PAUSE ; //default 5 mins
const long mincycle_poweroff = MINCYCLE_POWEROFF; //default 5 mins
const long mincycle_poweron = MINCYCLE_POWERON ; //default 60 mins
bool _1st_start_sleeped = 0;
//??? TODO: periodical start ?
//const long floor_circle_maxhalted = 6000000; //circle NOT works max 100 minutes
const long deffered_stop_hotcircle = DEFFERED_STOP_HOTCIRCLE;
int EEV_cur_pos = 0;
int EEV_reopen_pos = 0;
bool EEV_must_reopen_flag = 0;
int EEV_apulses = 0; //for async
bool EEV_adonotcare = 0;
const unsigned char EEV_steps[4] = {0b1010, 0b0110, 0b0101, 0b1001};
char EEV_cur_step = 0;
bool EEV_fast = 0;
#ifdef EEV_MANUAL
bool EEV_manual = 1;
#else
bool EEV_manual = 0;
#endif
const bool c_EEV_reopenlast = EEV_REOPENLAST;
//main cycle vars
unsigned long millis_prev = 0;
unsigned long millis_now = 0;
unsigned long millis_cycle = 1000;
unsigned long millis_last_heatpump_on = 0;
unsigned long millis_last_heatpump_off = 0;
unsigned long millis_last_hotWP_on = 0;
unsigned long millis_last_hotWP_off = 0;
unsigned long millis_last_coldWP_off = 0;
unsigned long millis_notification = 0;
unsigned long millis_notification_interval = 33000;
unsigned long millis_displ_update = 0;
unsigned long millis_displ_update_interval = 10000;
unsigned long millis_escinput_485 = 0;
unsigned long millis_charinput_485 = 0;
unsigned long millis_escinput_local = 0;
unsigned long millis_charinput_local = 0;
unsigned long millis_lasteesave = 0;
unsigned long millis_last_printstats = 0;
unsigned long millis_eev_last_close = 0;
unsigned long millis_eev_last_on = 0;
unsigned long millis_eev_last_step = 0;
unsigned long millis_eev_minworkpos_time = 0;
unsigned long millis_eev_last_work = 0;
unsigned long tmic1 = 0;
unsigned long tmic2 = 0;
int skipchars_485 = 0;
int skipchars_local = 0;
#define BUFSIZE 150
unsigned char dataBuf[BUFSIZE+1]; // Allocate some space for the string, do not change that size!
char inChar= -1; // space to store the character read
byte index = 0; // Index into array; where to store the character
//-------------temporary variables
char temp[10];
int i = 0;
int u = 0;
int z = 0;
int x = 0;
int y = 0;
double tempdouble = 0.0;
double tempdouble_intpart = 0.0;
int tempint = 0;
bool tempbool = 0;
char fp_integer = 0;
char fp_fraction = 0;
String outString;
String lastStopCauseTxt; //20 reserved, but use 12 chars of text max
bool fl_printSS_lastStopCauseTxt = 0; //flag to call printSS
#define LSCint_normal 0
#define LSCint_protective 1
#define LSCint_error 2
int LSCint = LSCint_normal; //0 = normal, 1 = protective, 2 = error
String lastStartMsgTxt; //same as LSC
bool fl_printSS_lastStartMsgTxt = 0; //flag to call printSS
String t_sensorErrString;
char convBuf[13];
//-------------EEPROM
int eeprom_magic_read = 0x00;
int eeprom_addr = 0x00;
//initial values, saved to EEPROM and can be modified later
//CHANGE eeprom_magic after correction!
const int eeprom_magic = MAGIC;
//-------------ERROR states
#define ERR_OK 0
#define ERR_T_SENSOR 1
#define ERR_P_HI 2
#define ERR_P_LO 3
int errorcode = 0;
unsigned char sequential_errors = 0;
//--------------------------- for wattage
#define ADC_BITS 10 //10 fo regular arduino
#define ADC_COUNTS (1<<ADC_BITS)
float em_calibration = 62.5;
int em_samplesnum = 2960; // Calculate Irms only 1480 == full 14 periods for 50Hz, 2960 = 28, 4440 = 42
//double Irms = 0; //for tests with original procedure
int supply_voltage = 0;
int em_i = 0;
//phase 1
int sampleI_1 = 0;
double filteredI_1 = 0;
double offsetI_1 = ADC_COUNTS>>1; //Low-pass filter output
double sqI_1,sumI_1 = 0; //sq = squared, sum = Sum, inst = instantaneous
double async_Irms_1 = 0;
double async_wattage = 0;
//--------------------------- for wattage END
const char str1[] PROGMEM = "Valden Heat Pump Controller, https://github.com/OpenHP/\n\r\n\rCommands: \n\r(?) help\n\r(-) decrease setpoint T\n\r\n\r(+) increase setpoint T";
const char str2[] PROGMEM = "(<) decrease EEV T diff \n\r(>) increase EEV T diff\n\r\n\r(M) manual EEV mode\n\r(A) auto EEV mode\n\r\n\r(z) -1 EEV\t(Z) -10 EEV\n\r(x) +1 EEV\t(X) +10 EEV\n\r(G) get stats";
const char str3[] PROGMEM = "EEV:auto";
const char str4[] PROGMEM = "EEV:manual";
const char str5[] PROGMEM = "N/A,auto";
const char str6[] PROGMEM = "+10 ok";
const char str7[] PROGMEM = "-10 ok";
const char str8[] PROGMEM = "+1 ok";
const char str9[] PROGMEM = "-1 ok";
const char str10[] PROGMEM = "Max!";
const char str11[] PROGMEM = "Min!";
const char str12[] PROGMEM = "HWP ON by Setp. update";
const char str13[] PROGMEM = "EE->mem";
const char str14[] PROGMEM = "mem->EE";
const char str15[] PROGMEM = "OK:E.T.Sens.";
const char str16[] PROGMEM = "OK:Pr.Cold";
const char str17[] PROGMEM = "OK:Pr.Hot";
const char str18[] PROGMEM = "HWP_ON";
const char str19[] PROGMEM = "unkn_F";
PGM_P const const_strs[] PROGMEM = {
str1, str2, str3, str4, str5, str6, str7, str8, str9, str10,
str11, str12, str13, str14, str15, str16, str17, str18, str19
};
#define IDX_HELP1 0
#define IDX_HELP2 1
#define IDX_EEVAUTO 2
#define IDX_EEVMANUAL 3
#define IDX_NAAUTO 4
#define IDX_PLUS10_OK 5
#define IDX_MINUS10_OK 6
#define IDX_PLUS1_OK 7
#define IDX_MINUS1_OK 8
#define IDX_MAX 9
#define IDX_MIN 10
#define IDX_HWP_ONBYUPD 11
#define IDX_EEtoMEM 12
#define IDX_MEMtoEE 13
#define IDX_OK_ETSENS 14
#define IDX_OK_PRCOLD 15
#define IDX_OK_PRHOT 16
#define IDX_HWPON 17
#define IDX_UNKNF 18
//--------------------------- functions
long ReadVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high<<8) | low;
//constant NOT same as in battery controller!
result = 1126400L / result; // Calculate Vcc (in mV); (me: !!) 1125300 (!!) = 1.1*1023*1000
return result; // Vcc in millivolts
}
/*void PrintS (String str) {
#ifdef RS485_HUMAN
char *outChar=&str[0];
digitalWrite(SerialTxControl, RS485Transmit);
halifise();
delay(1);
RS485Serial.print(outChar);
RS485Serial.println();
RS485Serial.flush();
digitalWrite(SerialTxControl, RS485Receive);
#endif
}*/
void PrintSS (String str) {
char *outChar=&str[0];
if (str == "") {
return;
}
#ifdef RS485_HUMAN
digitalWrite(SerialTxControl, RS485Transmit);
halifise();
delay(1);
RS485Serial.print(outChar);
RS485Serial.println();
RS485Serial.flush();
digitalWrite(SerialTxControl, RS485Receive);
#endif
Serial.println(outChar);
Serial.flush();
}
void PrintSSch(char idx) {
strcpy_P(dataBuf, (PGM_P)pgm_read_word(&const_strs[idx]));
Serial.println((const char *) dataBuf);
#ifdef RS485_HUMAN
digitalWrite(SerialTxControl, RS485Transmit);
halifise();
delay(1);
RS485Serial.print((const char *) dataBuf);
RS485Serial.println();
RS485Serial.flush();
digitalWrite(SerialTxControl, RS485Receive);
#endif
}
void PrintSS_SaD (double num) { //global string + double
#ifdef RS485_HUMAN
digitalWrite(SerialTxControl, RS485Transmit);
halifise();
delay(1);
RS485Serial.print(outString);
RS485Serial.println(num);
RS485Serial.flush();
digitalWrite(SerialTxControl, RS485Receive);
#endif
Serial.print(outString);
Serial.println(num);
Serial.flush();
}
void PrintSS_SaBl (bool num) {
#ifdef RS485_HUMAN
digitalWrite(SerialTxControl, RS485Transmit);
halifise();
delay(1);
RS485Serial.print(outString);
RS485Serial.println(num);
RS485Serial.flush();
digitalWrite(SerialTxControl, RS485Receive);
#endif
Serial.print(outString);
Serial.println(num);
Serial.flush();
}
void ApToOut_D (double num) {
outString += String(num);
}
void PrintSS_SaI (int num) {
#ifdef RS485_HUMAN
digitalWrite(SerialTxControl, RS485Transmit);
halifise();
delay(1);
RS485Serial.print(outString);
RS485Serial.println(num);
RS485Serial.flush();
digitalWrite(SerialTxControl, RS485Receive);
#endif
Serial.print(outString);
Serial.println(num);
Serial.flush();
}
/*void PrintSS_SaI (int num) { //global string + double
#ifdef RS485_HUMAN
digitalWrite(SerialTxControl, RS485Transmit);
halifise();
delay(1);
RS485Serial.print(outString);
RS485Serial.println(num);
RS485Serial.flush();
digitalWrite(SerialTxControl, RS485Receive);
#endif
Serial.print(outString);
Serial.println(num);
Serial.flush();
}*/
void _PrintHelp(void) {
PrintSS("fw: " + fw_version + " board: "+ hw_version);
PrintSSch(IDX_HELP1);
#ifndef NO_EEV
PrintSSch(IDX_HELP2);
#endif
}
void PrintSS_double (double double_to_print) {
dtostrf(double_to_print,1,2,temp);
PrintSS(temp);
}
void Add_Double_To_Buf_IntFract (double float_to_convert) { //uses tempdouble tempdouble_intpart fp_integer fp_fraction
if (float_to_convert > 255.0 || float_to_convert < -127.0) {
fp_integer = -127;
fp_fraction = 0;
} else {
tempdouble = modf (float_to_convert , &tempdouble_intpart);
fp_integer = trunc(tempdouble_intpart);
tempdouble = tempdouble * 100;
fp_fraction = round(tempdouble);
}
dataBuf[i] = fp_integer;
i++;
dataBuf[i] = fp_fraction;
i++;
/*
Serial.println(float_to_convert);
Serial.println(fp_integer, DEC);
Serial.println(fp_fraction, DEC);*/
}
void IntFract_to_tempdouble (char _int_to_convert, char _fract_to_convert) { //fract is also signed now!
tempdouble = (double) _fract_to_convert / 100;
tempdouble += _int_to_convert;
/*Serial.println(_int_to_convert);
Serial.println(_fract_to_convert);
Serial.println(tempdouble);*/
}
void _ProcessInChar(void){
//remote commands +,-,G,0x20/?/Enter/A/M/x/X/z/Z
switch (inChar) {
case 0x00:
break;
case 0x20:
_PrintHelp();
break;
case 0x3F:
_PrintHelp();
break;
case 0x0D:
_PrintHelp();
break;
case 0x2B:
Inc_T();
break;
case 0x2D:
Dec_T();
break;
#ifndef NO_EEV
case 0x3C:
Dec_E();
break;
case 0x3E:
Inc_E();
break;
case 0x41:
EEV_manual = 0;
PrintSSch(IDX_EEVAUTO);
break;
#endif
case 0x47:
PrintStats_SS();
millis_last_printstats = millis_now;
break;
#ifndef NO_EEV
case 0x4D:
EEV_manual = 1;
PrintSSch(IDX_EEVMANUAL);
break;
case 0x58: //+10
if (EEV_manual != 1){
PrintSSch(IDX_NAAUTO);
break;
}
EEV_apulses += 10;
EEV_fast = 1;
PrintSSch(IDX_PLUS10_OK);
break;
case 0x5A: //-10
if (EEV_manual != 1){
PrintSSch(IDX_NAAUTO);
break;
}
EEV_apulses -= 10;
EEV_fast = 1;
PrintSSch(IDX_MINUS10_OK);
break;
case 0x78: //+1
if (EEV_manual != 1){
PrintSSch(IDX_NAAUTO);
break;
}
EEV_apulses += 1;
EEV_fast = 1;
PrintSSch(IDX_PLUS1_OK);
break;
case 0x7A: //-1
if (EEV_manual != 1){
PrintSSch(IDX_NAAUTO);