//#define SELFTEST_RELAYS_LEDS_SPEAKER //speaker and relays QA test, uncomment to enable
#define BOARD_TYPE_G //Type "G", PCB from github.com/gonzho000/chpc/
//#define SELFTEST_EEV //EEV QA test, uncomment to enable
//#define BOARD_TYPE_F //Type "F"
//#define SELFTEST_T_SENSORS //temperature sensors QA test, uncomment to enable
//#define BOARD_TYPE_G9 //Type "G9" or "G-MAX", current testing
//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
//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 MAGIC 0x66; //change this value if you want to rewrite the T setpoint in EEPROM
#define T_SETPOINT_MAX 45.0; //defines max temperature that ordinary user can set
#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_HOTCIRCLE_DELTA_MIN 2.0; //useful for "water heater vith intermediate heat exchanger" scheme, Target == sensor in water, hot side CP will be switched on if "target - hot_out > T_HOTCIRCLE_DELTA_MIN"
#define T_SETPOINT_MAX 48.0; //maximum "setpoint" temperature that an ordinary user can set
#define T_SUMP_MIN 9.0; //HP will not start if T lower
#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_SUMP_MAX 110.0; //HP will stop if T higher
#define T_CRANKCASE_MIN 8.0; //compressor (crankcase) min. temperature, HP will not start if T lower
#define T_SUMP_HEAT_THRESHOLD 16.0; //sump heater will be powered on 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_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_AFTER_EVAPORATOR_MIN -7.0; //suction MIN, HP stops if lower, anti-freeze and anti-liquid at suction protection
#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 -8.0; //cold loop anti-freeze: stop if inlet or outlet temperature lower
#define T_COLD_MIN -15.5; //cold side (glycol) loop freeze protection: HP stops if inlet or outlet temperature lower
#define T_HOTOUT_MAX 50.0; //hot loop: stop if outlet temperature higher than this
#define T_HOT_MAX 50.0; //hot loop: HP stops if hot side inlet or outlet temperature higher than this threshold
#define T_WORKINGOK_SUMP_MIN 30.0; //compressor MIN temperature, HP stops if it lower after 5 minutes of pumping, need to be not very high to normal start after deep freeze
//#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
#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)
//If you are using a capillary tube or TXV: simply skip next section.
#define MAX_WATTS 1170.0 //user for power protection
//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
#define DEFFERED_STOP_HOTCIRCLE 3000000 //50 mins
//steps tuning: milliseconds per fast and slow (precise) steps
#define POWERON_PAUSE 300000 //5 mins
#define EEV_PULSE_FCLOSE_MILLIS 20 //(20 tube evaporator) fast closing, closing on danger (milliseconds per step)
#define MINCYCLE_POWEROFF 300000 //5 mins
#define EEV_PULSE_CLOSE_MILLIS 45000 //(50000 tube evaporator) accurate closing while the compressor works (milliseconds per step)
#define MINCYCLE_POWERON 3600000 //60 mins
#define EEV_PULSE_WOPEN_MILLIS 20 //(20 tube evaporator) standby (waiting) pos. set (milliseconds per step)
#define POWERON_HIGHTIME 10000 //10 sec, defines time after start when power consumption can be 2 times greater than normal
#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
//EEV
#define EEV_CLOSE_ADD_PULSES 8 //read below, additional steps after zero position while full closing
#define EEV_MAXPULSES 480
#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_PULSE_FCLOSE_MILLIS 20 //fast close, set waiting pos., close on danger
#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_PULSE_CLOSE_MILLIS 50000 //precise close
#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_PULSE_WOPEN_MILLIS 20 //waiting pos. set
#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_PULSE_FOPEN_MILLIS 1300 //fast open, fast search
#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
#define EEV_PULSE_OPEN_MILLIS 60000 //precise open
//additional options
#define EEV_STOP_HOLD 500 //0.1..1sec for Sanhua
#define EEV_REOPENLAST 1 ///1 = reopen to last position on compressor start, useful for ordinary schemes with everyday working cycles, 0 = not
#define EEV_CLOSE_ADD_PULSES 8 //read below, close algo
#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_OPEN_AFTER_CLOSE 47 //0 - close to zero position, than close on EEV_CLOSE_ADD_PULSES (close insurance, read EEV manuals for this value)
//#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
//N - close to zero position, than close on EEV_CLOSE_ADD_PULSES, than open on EEV_OPEN_AFTER_CLOSE pulses
//i.e. it is "waiting position" while HP not working
#define EEV_MINWORKPOS 52 //position will be not less during normal work, set after compressor start
#define EEV_PRECISE_START 8.6 //T difference, threshold: make slower pulses if (real_diff-target_diff) less than this value. Used for fine auto-tuning.
#define EEV_EMERG_DIFF 2.5 //if dangerous condition: real_diff =< (target_diff - EEV_EMERG_DIFF) occured 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) than EEV will be closed
#define EEV_HYSTERESIS 0.6 //must be less than EEV_PRECISE_START, ex: target difference = 4.0, hysteresis = 0.1, when difference in range 4.0..4.1 no EEV pulses will be done;
#define EEV_CLOSEEVERY 86400000 //86400000: EEV will be closed (calibrated) every 24 hours, done while HP is NOT working
#define EEV_TARGET_TEMP_DIFF 4.0 //target difference between Before Evaporator and After Evaporator, the head of whole algo
//#define EEV_DEBUG //debug, usefull during system fine tuning, "RS485_HUMAN" only
//do not use next option if you're not sure what are you doing
#define MAGIC 0x55 //change if u want to reinit T sensors
//#define EEV_DEBUG //debug, useful during system fine-tuning, works both with local serial and RS485_HUMAN
//-----------------------USER OPTIONS END -----------------------
const double cT_after_evaporator_min = T_AFTER_EVAPORATOR_MIN; // working evaporation presure ~= -10, it is constant due to large evaporator volume // waterhouse v1: -12 is too high