'##############
'Скрипт отопления
'##############

'- Сергей Wizard	06.11.2013	Написана симуляция кондиционера Konvekta
'------------------------------------------------------------------------------------------


'-----------------------------------
'	Инициализация переменных
'-----------------------------------
{macro:heizung_init}

'Загружаем данные о температуре окружающего воздуха и сохраняем его в температуре салона
'Так как при спауне автобуса температура воздуха будет сохраняться в салоне, имхо логичнее некуда

	(L.S.Weather_Temperature) (S.L.Cabinair_Temp)
	(L.S.Weather_AbsHum) (S.L.Cabinair_absHum)

'Комфортная температура в салоне для автоматического режима кондиционера 
'должна составлять 21 градус по цельсию
'Сохраним же её!
	(L.L.elec_busbar_main)
	21 (S.L.Konvekta_temp_adjust) $IntToStr (S.$.Konvekta_display)

	 0 (L.L.Konvekta_circulation_sw)

{end}


{macro:heizung_frame}

'А тут мы загружаем в память макросы, отвечающие за прогрев салона и за автономный отопитель

	(M.L.standheizung_frame)	
	(M.L.cabinair_frame)
	(M.L.Konvekta_lights_frame)
{end}



'------------------------------------------------------------------------------------------
'Симуляция автокондиционера Konvecta KL40T
'------------------------------------------------------------------------------------------

'Кнопки панели управления автокондиционером

{trigger:Konvekta_temp_up}

(L.L.elec_busbar_main)
	{if}
(L.L.Konvekta_temp_adjust) 1 + (S.L.Konvekta_temp_adjust)

(L.L.Konvekta_temp_adjust) 25 >
	{if}
	(L.L.Konvekta_temp_adjust) 1 - (S.L.Konvekta_temp_adjust)
	{endif}

{endif}

(T.L.ev_kippschalter_aus)

(L.L.Konvekta_temp_adjust) $IntToStr (S.$.Konvekta_display)

{end}

{trigger:Konvekta_temp_down}

(L.L.elec_busbar_main)
	{if}
(L.L.Konvekta_temp_adjust) 1 - (S.L.Konvekta_temp_adjust)

(L.L.Konvekta_temp_adjust) 18 <
	{if}
	(L.L.Konvekta_temp_adjust) 1 + (S.L.Konvekta_temp_adjust)
	{endif}
{endif}

(L.L.Konvekta_temp_adjust) $IntToStr (S.$.Konvekta_display)

(T.L.ev_kippschalter_aus)

{end}

'------------------------------------------------------------------------------------------

{trigger:Konvekta_temp_interior}

1 (S.L.Konvekta_temp_interior_sw)

	(L.L.Cabinair_Temp) s0 trunc " 3" $IntToStrEnh "." $+ 
	l0 abs l0 abs trunc - 10 * trunc " 1" $IntToStrEnh $+ "C" $+
	(S.$.Konvekta_display)
	
(T.L.ev_kippschalter_aus)

{end}


{trigger:Konvekta_temp_exterior}

1 (S.L.Konvekta_temp_exterior_sw)

	(L.S.Weather_Temperature) s0 trunc " 3" $IntToStrEnh "." $+ 
	l0 abs l0 abs trunc - 10 * trunc " 1" $IntToStrEnh $+ "C" $+
	(S.$.Konvekta_display)

(T.L.ev_kippschalter_aus)

{end}

'------------------------------------------------------------------------------------------

{trigger:Konvekta_klimator_50}

(L.L.Konvekta_klimator_50_sw) ! (S.L.Konvekta_klimator_50_sw)

	(L.L.elec_busbar_main)
	(L.L.Konvekta_klimator_50_sw) &&
		{if}
	0.5 (S.L.cp_klimator_sw)
		{else}
	0 (S.L.cp_klimator_sw)
		{endif}
	
(T.L.ev_kippschalter_aus)

{end}

{trigger:Konvekta_klimator_100}

(L.L.Konvekta_klimator_100_sw) ! (S.L.Konvekta_klimator_100_sw)

	(L.L.elec_busbar_main)
	(L.L.Konvekta_klimator_100_sw) &&
		{if}
	1 (S.L.cp_klimator_sw)
		{else}
	0 (S.L.cp_klimator_sw)
		{endif}	

(T.L.ev_kippschalter_aus)

{end}

'------------------------------------------------------------------------------------------

{trigger:Konvekta_auto_mode}

(L.L.Konvekta_auto_mode_sw) ! (S.L.Konvekta_auto_mode_sw)
	
(T.L.ev_kippschalter_aus)

{end}

'------------------------------------------------------------------------------------------

{trigger:Konvekta_webasto}

(L.L.elec_busbar_main)
	{if}
(L.L.Konvekta_webasto_sw) ! (S.L.Konvekta_webasto_sw)
	{endif}
	
(T.L.ev_kippschalter_aus)

{end}

{trigger:Konvekta_circulation}

(L.L.Konvekta_circulation_sw) ! (S.L.Konvekta_circulation_sw)

'(L.L.elec_busbar_main)
'(L.L.Konvekta_circulation_sw) &&
'	{if}
'	(L.L.Cabinair_Temp) (S.L.fanheater_t_target)
'{else}
'	(L.S.Weather_Temperature) (S.L.fanheater_t_target)
'	{endif}

	
(T.L.ev_kippschalter_aus)

{end}





{macro:cabinair_frame}


'	Вентиляторы отопителей
	(L.L.cp_heizluefter3_sw) (L.L.elec_busbar_main) &&
	{if}
		(L.L.vent1_time) 5 <
		{if}
			(L.L.vent1_time) (L.S.Timegap) + (S.L.vent1_time)
		{endif}
		(L.L.vent1_time) (F.L.vent_speed_on) (S.L.vent1_speed)
		1 (S.L.Konvekta_klimator_50_sw)
	{else}
		(L.L.vent1_time) 0 >
		{if}
			(L.L.vent1_time) (L.S.Timegap) - (S.L.vent1_time)
		{endif}
		(L.L.vent1_time) (F.L.vent_speed_off) (S.L.vent1_speed)
		0 (S.L.Konvekta_klimator_50_sw)
	{endif}
	
	(L.L.otopitel_vent_1) (L.S.Timegap) (L.L.vent1_speed) * + (S.L.otopitel_vent_1)	
	
	(L.L.cp_heizluefter4_sw) (L.L.elec_busbar_main) &&
	{if}
		(L.L.vent2_time) 5 <
		{if}
			(L.L.vent2_time) (L.S.Timegap) + (S.L.vent2_time)
		{endif}
		(L.L.vent2_time) (F.L.vent_speed_on) (S.L.vent2_speed)
		1 (S.L.Konvekta_circulation_sw)
	{else}
		(L.L.vent2_time) 0 >
		{if}
			(L.L.vent2_time) (L.S.Timegap) - (S.L.vent2_time)
		{endif}
		(L.L.vent2_time) (F.L.vent_speed_off) (S.L.vent2_speed)
		0 (S.L.Konvekta_circulation_sw)
	{endif}
	
	(L.L.otopitel_vent_2) (L.S.Timegap) (L.L.vent2_speed) * + (S.L.otopitel_vent_2)


'------------------------------------------------------------------------------------------

'	Основной расчет:
	(L.L.Cabinair_Temp) (L.S.Weather_Temperature) - (C.L.cabinair_Qrate_envir_factor) * (S.L.cabinair_Qrate_envir)

'	Прямой забор теплого воздуха из двигателя:
	(L.L.engine_temperature_envir) (L.L.Cabinair_Temp) - (C.L.cabinair_Qrate_engine_factor) * (S.L.cabinair_Qrate_engine)


'	Расчет температуры или откуда забирать воздух:
'	(L.L.cockpit_heizregler_ventilation_mode) s0 0 =
'	{if}
'		Стандартное отопление с регулятором температуры + воздух берем из КАБИНЫ и умножаем на эффективность теплообменника
'		(L.L.Cabinair_Temp) (L.L.engine_temperature) (L.L.Cabinair_Temp) - (L.L.cockpit_heizregler_temp) * (C.L.heat_exchanger_effectivity) * + (S.L.fanheater_t_target)
'	{else}
'		l0 2 =
'		{if}
'			Стандартное отопление с регулятором температуры + воздух берем с УЛИЦЫ и умножаем на эффективность теплообменника
'			(L.S.Weather_Temperature) (L.L.engine_temperature) (L.S.Weather_Temperature) - (L.L.cockpit_heizregler_temp) * (C.L.heat_exchanger_effectivity) * + (S.L.fanheater_t_target)
'		{else}
'			Прямой забор температуры воздуха из окружающей среды: 
'			(L.S.Weather_Temperature) (S.L.fanheater_t_target)
'		{endif}		
'	{endif}

'------------------------------------------------------------------------------------------
'Основные регулировки и расчеты температуры в зависимости от выбранных параметров
'------------------------------------------------------------------------------------------

	(L.L.Cabinair_Temp) (L.L.Konvekta_temp_adjust) >
	{if}
	(L.S.Weather_Temperature) (C.L.coolant_exchanger_effectivity) * /-/ (S.L.fanheater_t_target)
	{else}
		(L.L.Cabinair_Temp) (L.L.Konvekta_temp_adjust) >
		(L.L.Konvekta_circulation_sw) &&
			{if}
			(L.L.Cabinair_Temp) (C.L.coolant_exchanger_effectivity) * /-/ (S.L.fanheater_t_target)
				{else}
				(L.S.Weather_Temperature) (L.L.engine_temperature) (L.S.Weather_Temperature) - 0.7 * (C.L.heat_exchanger_effectivity) * + (S.L.fanheater_t_target)
	{endif}
	{endif}


'Расчеты эфективной общей поверхности обмена воздуха:
'короче при открытых дверях идет потеря тепла, сколько, расчитывается ниже
	(L.L.door_0) (L.L.door_1) (L.L.door_2) (L.L.door_3) + + + (C.L.cabinair_A_perdoor) *
	(L.L.cp_f1_l_pos) (L.L.cp_f2_l_pos) (L.L.cp_f3_l_pos) + +
	(L.L.cp_f1_r_pos) (L.L.cp_f2_r_pos) + +
'	(L.L.cp_fortochka_L1) (L.L.cp_fortochka_L2) + +
'	(L.L.cp_fortochka_R1) (L.L.cp_fortochka_R2) + + (C.L.cabinair_A_perwindow) * + 
'	(C.L.cabinair_A_driverwindow) (L.L.cp_fahrerfenster_pos) * +

'Berechnung des Gesamtvolumenstroms in die AuЯenwelt und umgekehrt (V zдhlt je Richtung, d.h. insgesamt wird 2*V bewegt)
	(L.L.velocity) 20 / abs (C.L.cabinair_windowdoor_minkmh) + (C.L.cabinair_windowdoor_effectivity) * 3.6 / * (S.L.cabinair_Vrate_doorwindow)

'Berechnung des Volumenstroms durchs Frontheizgerдt
	(L.L.cockpit_heizregler_ventilation_mode) 1 =
	{if}
		(C.L.cabinair_A_frontheater) (L.L.velocity) 20 / abs (C.L.cabinair_windowdoor_minkmh) + (C.L.cabinair_windowdoor_effectivity) * 3.6 / * (S.L.cabinair_Vrate_fanheater)
	{else}
		(C.L.cabinair_A_frontheater) (L.L.cp_klimator_RPM) 100 / * (S.L.cabinair_Vrate_fanheater)
	{endif}

'Berechnung des Volumenstroms durch die Raumheizgerдte
	(C.L.cabinair_A_cabinheater) (L.L.cp_heizluefter3_sw) 1 = * (L.L.elec_busbar_main) 1 = * (S.L.cabinair_Vrate_cabinheater)
	(C.L.cabinair_A_cabinheater) (L.L.cp_heizluefter_sw) 1 = * (L.L.elec_busbar_main) 1 = * (S.L.cabinair_Vrate_cabinheater)
	(C.L.cabinair_A_cabinheater) (L.L.cp_heizluefter2_sw) 1 = * (L.L.elec_busbar_main) 1 = * (S.L.cabinair_Vrate_cabinheater)


'Berechnung der Temperaturrate durch Motor und Abstrahlung an die Umwelt...
	(L.L.cabinair_Qrate_engine) (L.L.cabinair_Qrate_envir) - (C.L.cabinair_cvm) /
'...und Temperaturrate durch Passagiere...
	(C.L.cabinair_T_passenger) (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (C.L.cabinair_Vrate_passenger) * (L.L.humans_count) * +
'...und dieser durch die Volumenstrцme durch Fenster und Tьren:
	(L.S.Weather_Temperature) (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_doorwindow) * +
'...und des Frontheizgerдtes...
	(L.L.fanheater_t_target) (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_fanheater) * +
'...und der Raumheizgerдte...
	(L.L.engine_temperature) (C.L.heat_exchanger_effectivity) * (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_cabinheater) * +
'...aufintegrieren:
	(L.S.Timegap) * (L.L.Cabinair_Temp) + (S.L.Cabinair_Temp)

'Berechnung der Motorkьhlrate durch die Wдrmetauscher in den Heizgerдten:

	(L.L.fanheater_t_target) (L.L.Cabinair_Temp) - 0 max (C.L.cabinair_V) / (L.L.cabinair_Vrate_fanheater) * 
	(L.L.engine_temperature) (C.L.heat_exchanger_effectivity) * (L.L.Cabinair_Temp) max (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_cabinheater) * +
	(C.L.cabinair_cvm) * 8 * (S.L.cabinair_Qrate_engine_fanheatcooling)



'Berechnung der Feuchtigkeitsrate durch Volstrцme mit den Passagieren:
	(C.L.cabinair_absHum_passenger) (L.L.Cabinair_absHum) - (C.L.cabinair_V) / (C.L.cabinair_Vrate_passenger) * (L.L.humans_count) *
'Berechnung der Feuchtigkeitsrate durch Volumenstrцme mit der AuЯenwelt:
	(L.S.Weather_AbsHum) (L.L.Cabinair_absHum) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_doorwindow) * +
'Berechnung der Feuchtigkeitsrate durch Frontheizgerдt
	(L.S.Weather_AbsHum) (L.L.Cabinair_absHum) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_fanheater) * (L.L.cockpit_heizregler_ventilation_mode) 1 >= * + (S.L.cabinair_test)
'... aufintegrieren:
	(L.S.Timegap) * (L.L.Cabinair_absHum) + (S.L.Cabinair_absHum)



{end}




{macro:Konvekta_lights_frame}

	(L.L.Konvekta_klimator_50_sw) (L.L.elec_busbar_main) &&
	(S.L.Konvekta_light_klimator_50)

	(L.L.Konvekta_klimator_100_sw) (L.L.elec_busbar_main) &&
	(S.L.Konvekta_light_klimator_100)
	
	(L.L.Konvekta_webasto_sw) (L.L.elec_busbar_main) &&
	(S.L.Konvekta_light_webasto)
	
	(L.L.Konvekta_circulation_sw) (L.L.elec_busbar_main) &&
	(S.L.Konvekta_light_circulation)


{end}



{macro:standheizung_frame}


	(L.L.Konvekta_webasto_sw) (L.L.elec_busbar_avail) &&
	{if}
		(L.L.standheizung_running) !
		{if}
			0 (S.L.standheizung_volume_stufe1) (S.L.standheizung_volume_stufe2)
			1 (S.L.standheizung_running)  
			0 (S.L.standheizung_stufe1_time_elapsed)
			1 (S.L.standheizung_stufe1) 
		{endif}
		1 (S.L.standheizung_target)
	
	{else}
		0 (S.L.standheizung_target)
	{endif}
	



'	Raufrцdeln der Timer:
	(L.L.standheizung_running)
	{if}
		(L.L.standheizung_stufe1_time_elapsed) (L.S.Timegap) + (S.L.standheizung_stufe1_time_elapsed)
		(L.L.standheizung_stufe2_time_elapsed) (L.S.Timegap) + (S.L.standheizung_stufe2_time_elapsed)
	{endif}

'	Einschalten der 2. Stufe:
	(L.L.standheizung_target) (L.L.standheizung_stufe1_time_elapsed) (C.L.standheizung_vorlauf) > &&
	(L.L.engine_temperature) (C.L.standheizung_brenner_t_ein) < &&
	{if}
		(L.L.standheizung_stufe2) !
		{if}
			0 (S.L.standheizung_stufe2_time_elapsed)
		{endif}
		1 (S.L.standheizung_stufe2)
	{endif}

'	Ausschalten der 2. Stufe:
	(L.L.standheizung_target) ! (L.L.standheizung_stufe2_time_elapsed) (C.L.standheizung_minlauf) > &&
	(L.L.engine_temperature) (C.L.standheizung_brenner_t_aus) > ||
	{if}
		(L.L.standheizung_stufe2)
		{if}
			0 (S.L.standheizung_stufe1_time_elapsed)
		{endif}
		0 (S.L.standheizung_stufe2) 
	{endif}

'	Ausschalten der 1. Stufe:
	(L.L.standheizung_target) ! (L.L.standheizung_stufe1_time_elapsed) (C.L.standheizung_nachlauf) > && 
	{if}
		0 (S.L.standheizung_stufe1)
	{endif}

	(L.L.standheizung_stufe1) 
	{if}
		(L.L.standheizung_volume_stufe1) (L.S.Timegap) 3 / + 1 min (S.L.standheizung_volume_stufe1)
	{else}
		(L.L.standheizung_volume_stufe1) (L.S.Timegap) 3 / - 0 max (S.L.standheizung_volume_stufe1)
		0.01 <
		{if}
			0 (S.L.standheizung_running)
		{endif}
	{endif}

	(L.L.standheizung_stufe2) 
	{if}
		(L.L.standheizung_volume_stufe2) (L.S.Timegap) 4 / + 1 min (S.L.standheizung_volume_stufe2)

'		Berechnung der Temperaturverдnderung:
		(C.L.heizung_Qrate_standheizung) (L.L.cabinair_Qrate_envir) - (C.L.engine_cvm_engine) / (L.S.Timegap) * (L.L.engine_temperature) + (S.L.engine_temperature) 


	{else}
		(L.L.standheizung_volume_stufe2) (L.S.Timegap) 4 / - 0 max (S.L.standheizung_volume_stufe2)
	{endif}



{end}
