'##############
'Engine Script
'##############

'Simulates basic engine behaviour, fuel cunsumption and cooling circuit

'(c) 23.03.2009 Rüdiger Hülsmann
'(c) 09.08.2009 Marcel Kuhnt

'Script Version: 1.5
'Omsi release: 1.0

'Needs:
'- Antrieb
'- Heizung


'Revision History:
'- Marcel Kuhnt		09.08.2009	Added Engine Failure
'- Marcel Kuhnt		10.08.2009	Added Revision History
'- Rüdiger Hülsmann	12.10.2009	Added possibility of idle speed control
'- Rüdiger Hülsmann	11.05.2010	Added "E-Gas" function (throttle override by stop brake)
'- Rüdiger Hülsmann	13.05.2010	Added engine temperature and cooling system
'- Rüdiger Hülsmann	08.07.2010	Bugfix E-Gas
'- Rüdiger Hülsmann	06.11.2010	Improved engine start and stop procedure 
'- Marcel Kuhnt		09.11.2010	refueling included
'- Rüdiger Hülsmann	21.11.2010	thermostat short circuit
'- Rüdiger Hülsmann	22.11.2010	engine coolable by fan heaters
'- Rüdiger Hülsmann	24.12.2010	optional ASR (traction control)
'- Rüdiger Hülsmann	29.12.2010	ASR debugged
'- Rüdiger Hülsmann	01.01.2011	ASR debugged again
'- Rüdiger Hülsmann	05.01.2011	20h-switch E-Gas
'- Marcel Kuhnt		07.01.2011	writing tank_percent variable
'- Rüdiger Hülsmann	10.01.2011	Internal throttle set to 0 during shutdown
'- Rüdiger Hülsmann	23.01.2011	ASR relais sound
'- Kyle Ortiz	        05.07.2011	Added turbochager
'- Kyle Ortiz	        30.07.2011	Added Speed Governer with RPM governer while the bus is in drive
'- Kyle Ortiz	        16.08.2011	Changed engine braking algorithm. 



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


'----------------------
'	Trigger
'----------------------

{trigger:kw_m_engine_startbutton}
	(L.L.engine_on) ! (L.L.antrieb_getr_gangwahl) 1 = (L.L.elec_busbar_main) && &&
	{if}
		1 (S.L.engine_on) (S.L.engine_injection_on)
		(T.L.ev_enginestart)
		0 (S.L.engine_n)
	{endif}	
	1 (S.L.cp_taster_anlasser)
{end}

{trigger:kw_m_engine_startbutton_off}
	0 (S.L.cp_taster_anlasser)
{end}

{trigger:kw_m_enginestart}
	(L.L.engine_starter_antirepeat) !
	{if}
		(L.L.engine_on) !
		{if}	
			(L.L.antrieb_getr_gangwahl) 1 = (L.L.elec_busbar_main) && 
			{if}
				1 (S.L.engine_on) (S.L.engine_injection_on)
				(T.L.ev_enginestart)
				0 (S.L.engine_n)
			{endif}	
			1 (S.L.cp_taster_anlasser) (S.L.engine_starter_antirepeat)
		{else}
			(L.L.elec_busbar_main)
			{if}
				0 (S.L.engine_injection_on)
				1 (S.L.engine_starter_antirepeat)
			{endif}
			1 (S.L.cp_taster_motorabstellung)
		{endif}
	{endif}

{end}

{trigger:kw_m_enginestart_off}
	0 (S.L.cp_taster_motorabstellung) (S.L.cp_taster_anlasser) (S.L.engine_starter_antirepeat)
	1 (S.L.engine_injection_on)
{end}


{trigger:kw_m_engineshutdown}
	(L.L.elec_busbar_main)
	{if}
		0 (S.L.engine_injection_on)
		(M.L.engine_stall)
		0 (S.L.engine_on)
	{endif}
	1 (S.L.cp_taster_motorabstellung)
{end}

{trigger:kw_m_engineshutdown_off}
	0 (S.L.cp_taster_motorabstellung)
	1 (S.L.engine_injection_on)
{end}

{trigger:veh_tank}
	(L.L.engine_tank_content) (L.S.Timegap) (C.L.charge_KW) 3600 / * (L.L.tank_percent) (F.L.tank_charge_curve) * + (C.L.engine_tank_capacity) min (S.L.engine_tank_content)
{end}

{macro:engine_init}
	(C.L.engine_tank_capacity) 0.82 * (C.L.engine_tank_capacity) 0.18 * random + (S.L.engine_tank_content)
	(L.S.Weather_Temperature) (S.L.engine_temperature_envir) 9 random 4 - + (S.L.engine_temperature) 

	20 random (F.L.engine_efficiency_random) (S.L.engine_Random1)
{end}


{macro:engine_frame}

	(M.L.speed_governor)

	(L.L.engine_last_n) 0 >=
	(L.L.engine_n) -50 < &&
	{if}
		(M.L.engine_stall)
		0 (S.L.engine_on)
	{endif}
	
	
	(L.L.engine_n) (S.L.engine_last_n)

	
	(L.L.engine_n) 0 <= 
	(L.L.engine_injection_on) ! ||
	{if}
		0 (S.L.engine_n) 
	{endif}

	(L.L.bremse_kompressor)	(L.L.engine_on) &&
	{if}	
		0 (S.L.engine_M_additional_load)
	{else}
		0 (S.L.engine_M_additional_load)
	{endif}

	(M.L.engine_temperature)

	(M.L.engine_consumption)

	(M.L.turbochanger)

	(L.S.Year) (C.L.engine_Year) - 0 max 8 min (S.L.engine_YearX) 

	(L.S.Weather_Temperature) 25 - 0 min (S.L.engine_TempX)

	(L.L.engine_n) (F.L.engine_consumption_per_rpm) 1 (L.L.engine_YearX) (L.L.engine_Random1) * 0.04 * + * 1 (L.L.engine_TempX) 0.0032 * + * (S.L.engine_consumption1)

	1000 (L.L.engine_n) (F.L.engine_consumption_per_rpm) 1000 - - 1 (L.L.engine_YearX) (L.L.engine_Random1) * 0.01 * - * 1 (L.L.engine_TempX) 0.0025 * - * (S.L.engine_consumptionX)

	(L.L.ice_mode) 
	{if}
		0.65 (S.L.engine_throttle_X)
	{else}
		1 (S.L.engine_throttle_X)
	{endif}

'	TEST ELEC BREAK
	(L.L.brake) 0 >
	(L.L.velocity) 3 > &&
	(L.L.antrieb_getr_aktugang) 0 = ! 
	(L.L.antrieb_getr_gangwahl) 0 = ! || &&
	{if}
		(L.L.brake) (F.L.engine_M_bremse) (L.L.engine_n) (F.L.engine_M_maxThrottle) * (L.L.velocity) (F.L.engine_M_bremseF) * (L.L.engine_throttle_X) * (S.L.engine_M_additional_load)
	{else}
		0 (S.L.engine_M_additional_load)
	{endif}


	(L.L.throttle) (F.L.engine_efficiency_throttle) (L.L.engine_n) (F.L.engine_efficiency_rpm) * (S.L.engine_internal_throttle)

'MCQ: calculating tank content percent and write into pre defined variable for publishing on red line:
	(L.L.engine_tank_content) (C.L.engine_tank_capacity) / (S.L.tank_percent)

'	Motormanagement durch ASR-Eingriff, sofern vorhanden

	(L.L.elec_busbar_main) 
	(L.L.ASR_off) ! &&
	{if}
		(L.L.Wheel_RotationSpeed_1_L) (L.L.Wheel_RotationSpeed_1_R) + 2 / abs (L.L.Wheel_RotationSpeed_0_L) (L.L.Wheel_RotationSpeed_0_R) + 2 / 10 + abs >
		(L.L.Wheel_RotationSpeed_1_L) 1 > &&
		(L.L.Wheel_RotationSpeed_1_R) 1 > &&
		(L.L.engine_internal_throttle) 0 > &&
		{if}
			(L.L.engine_ASR_eingriff) !
			{if}
				(T.L.ev_bremse_ABS_0R_on)
			{endif}	
			1 (S.L.engine_ASR_eingriff)
		{endif}
		(L.L.Wheel_RotationSpeed_1_L) (L.L.Wheel_RotationSpeed_1_R) + 2 / abs (L.L.Wheel_RotationSpeed_0_L) (L.L.Wheel_RotationSpeed_0_R) + 2 / 8 + abs <
		{if}
			(L.L.engine_ASR_eingriff)
			{if}
				(T.L.ev_bremse_ABS_0R_off)
			{endif}	
			0 (S.L.engine_ASR_eingriff)
		{endif}

		(L.L.engine_ASR_eingriff)
		{if}
			0 (S.L.engine_internal_throttle)
		{endif}

	{else}
'		Wenn ASR aus, dann Dauerleuchten
		(L.L.elec_busbar_main) 
		{if}
			(L.L.engine_ASR_eingriff) !
			{if}
				(T.L.ev_bremse_ABS_0R_on)
			{endif}	
			1 (S.L.engine_ASR_eingriff)
		{else}
			(L.L.engine_ASR_eingriff)
			{if}
				(T.L.ev_bremse_ABS_0R_off)
			{endif}	
			0 (S.L.engine_ASR_eingriff)
		{endif}
	{endif}


	
{end}


{macro:engine_moment}

	

'	Reaktionszeit der Einspritzpumpe
	(L.L.engine_internal_throttle) (L.L.engine_throttle_injection) >
	{if}
		(L.L.engine_throttle_injection) (L.S.Timegap) 3 * + (L.L.engine_internal_throttle) min (S.L.engine_throttle_injection)
	{else}
		(L.L.engine_throttle_injection) (L.S.Timegap) 3 * - (L.L.engine_internal_throttle) max (S.L.engine_throttle_injection)
	{endif}
 

	(L.L.Velocity) (L.L.V_S_E) - (F.L.governor) (S.L.engine_spdgov)

	(L.L.antrieb_getr_aktugang) 0 = 
	(L.L.antrieb_getr_gangwahl) 0 = ||
	(L.L.engine_on) ! ||
	{if}
		(L.L.engine_n) 0 - (F.L.rpmgov_map) (S.L.RPM_Governor)
		0 (S.L.engine_n)
	{else}
		(L.L.engine_n) (C.L.rpmgov) - (F.L.rpmgov_map) (S.L.RPM_Governor)
	{endif}

	(L.L.engine_throttle_injection) (L.L.engine_spdgov) * (L.L.RPM_Governor) * (S.L.engine_throttle_injection)


	(L.L.engine_injection_on) (L.L.engine_on) &&
	{if}
		(L.L.antrieb_getr_fest)
		{if}
		(L.L.engine_throttle_injection) (F.L.engine_braking) (L.L.engine_n) (F.L.engine_M_minThrottle) * s5
		{else}
		(L.L.engine_n) (F.L.engine_M_minThrottle)  s5
		{endif}

		(L.L.engine_throttle_injection) s1 (L.L.engine_n) s0 (F.L.engine_M_maxThrottle) * 1 l1 - l5 * + (L.L.engine_throttle_X) * (S.L.engine_M)


'Regulates the amount of turbocharger boost into the engine 
	(L.L.engine_throttle_injection) (F.L.turbo_limit_thorttle_1) (S.L.turbo_limit_thorttle)

'Defines the amount of inerita and psi the turbocharger uses.
	
	(L.L.turbo_psi) (F.L.turbo_inertia) (L.L.throttle) (F.L.turbo_M_inertia_throttle) * (L.L.Velocity) (F.L.engine_airflow) * (S.L.turbo_inertia)

	(L.L.engine_n) (F.L.turbo_engine_rpm_max_psi) (S.L.turbo_max_psi)

'Regulates the amount of turbocharger boost based on throttle position 
	(L.L.turbo_limit_thorttle) (L.L.turbo_psi) >
	{if}	
		(L.L.turbo_psi) (L.S.Timegap) (L.L.turbo_inertia) * + (L.L.turbo_max_psi) min (S.L.turbo_psi)
	{else}	
		(L.L.turbo_limit_thorttle) (L.L.turbo_psi) <
		{if}	
			(L.L.turbo_psi) (L.S.Timegap) (L.L.turbo_psi) (F.L.turbo_inertia_dec) * - 0 max (S.L.turbo_psi)
		{else}
			(L.L.turbo_psi) < 0
			{if}
			0 (S.L.turbo_psi)
			{endif}
		{endif}
	{endif}




		(L.L.turbo_psi) (F.L.turbo_M_max) (S.L.turbo_M)
		(L.L.engine_M) -10 >
		{if}
		(L.L.turbo_M) (L.L.engine_M) * (S.L.engine_M) 
		{endif}
	{else}
		(L.L.engine_n) -51 >
		(L.L.engine_on) 1 = &&
		{if}
			-3000 (S.L.engine_M)
		{else}
			0 (S.L.engine_M)
		{endif}
	{endif}

{end}


{macro:turbochanger}	


	

	
		
{end}


{macro:engine_acceleration}


'	Berechnung der Drehzahlbeschleunigung:
' 	Calculation of speed acceleration:

	(L.L.engine_M) (L.L.antrieb_getr_M_an) (L.L.engine_M_additional_load) + - (C.L.engine_J) /

'	... Berechnung der Motordrehzahl anhand der Drehzahlbeschleunigung:

	(L.S.Timegap) * 30 * pi / (L.L.engine_n) + (S.L.engine_n)
{end}





{macro:engine_consumption}

	(L.L.engine_n) (C.L.rpmgov) 50 - > 
	(L.L.throttle) 0.1 >= &&
	{if}
		(L.L.engine_M) 0 <= 
		{if}
			(L.L.engine_M) (L.L.engine_n) * 0.10472 * (C.L.engine_power_idle) + s0
			(L.L.brake) (F.L.engine_M_bremse) (L.L.engine_N) (F.L.engine_M_maxThrottle) * (L.L.velocity) (F.L.engine_M_bremseF) * (L.L.engine_throttle_X) * (L.L.engine_n) * 0.10472 1.05 * * (C.L.engine_power_idle) + (S.L.engine_M3)
		{else}
			(L.L.engine_M) (L.L.engine_n) * 0.10472 * (C.L.engine_power_idle) + s0
			0 (S.L.engine_M3)
		{endif}
	{else}
		(L.L.engine_M) 0 <= 
		{if}
			(L.L.engine_M) (L.L.engine_n) * 0.10472 * s0
			(L.L.brake) (F.L.engine_M_bremse) (L.L.engine_N) (F.L.engine_M_maxThrottle) * (L.L.velocity) (F.L.engine_M_bremseF) * (L.L.engine_throttle_X) * (L.L.engine_n) * 0.10472 1.05 * * (S.L.engine_M3)
		{else}
			(L.L.engine_M) (L.L.engine_n) * 0.10472 * s0
			0 (S.L.engine_M3)
		{endif}
	{endif}

	(L.L.elec_busbar_main)
	{if}
		(L.L.engine_on)
		{if}
			(L.L.lights_stand)
			{if}
			(L.L.cp_klimator_RPM) 100 / (C.L.engine_power_idle2) 4 / * 0.16 * 1800 + (C.L.engine_power_idle2) 4 / + (S.L.engine_power_idle3)
			{else}
			(L.L.cp_klimator_RPM) 100 / (C.L.engine_power_idle2) 4 / * 0.16 * 1250 + (C.L.engine_power_idle2) 4 / + (S.L.engine_power_idle3)
			{endif}
		{else}
			(L.L.cp_klimator_RPM) 100 / (C.L.engine_power_idle2) 4 / * 0.16 * 800 + (C.L.engine_power_idle2) 4 / + (S.L.engine_power_idle3)
		{endif}
	{else}
		(C.L.engine_power_idle2) 4 / (S.L.engine_power_idle3)
	{endif}
	


	(L.L.engine_on)
	(L.L.cp_dummy_sw01) 
	(L.L.cp_taster_standheizung_target) || &&
	{if}
		l0 (L.L.engine_M3) - (S.L.engine_power)

		(L.L.bremse_kompressor) 
		(L.L.engine_on) && 
		{if}
			4000 (L.L.engine_power_idle3) + (S.L.engine_idle_power_2)
		{else}
			(L.L.engine_power_idle3) (S.L.engine_idle_power_2)
		{endif}
	{else}
		l0 (L.L.engine_M3) - (S.L.engine_power)

		(L.L.bremse_kompressor) 
		(L.L.engine_on) && 
		{if}
			4000 (L.L.engine_power_idle3) + (C.L.engine_power_idle2) 4 / - (S.L.engine_idle_power_2)
		{else}
			(L.L.engine_power_idle3) (C.L.engine_power_idle2) 4 / - (S.L.engine_idle_power_2)
		{endif}
	{endif}
	
	(L.L.engine_energytransfer) (L.L.engine_power) (L.S.Timegap) * 1000 / 3600 / + (S.L.engine_energytransfer)
	(L.L.engine_energy_used) (L.L.engine_power) (L.S.Timegap) * 1000 / 3600 / s0 + (S.L.engine_energy_used)
	(L.L.engine_idle_power_2) (L.S.Timegap) * 1000 / 3600 / (S.L.engine_energy_used_idle2)


	(C.L.engine_consumption_curve_available)
	{if}
		(L.L.engine_power) 0 >
		{if}
		(L.L.engine_consumption1) 1000 / s2
		{else}
		(L.L.engine_consumptionX) 1000 / s2
		{endif}
		1 l2 / (C.L.engine_fuel_value2) * s2
		1 (L.L.engine_consumption1) 1000 / / (C.L.engine_fuel_value2) * (S.L.engine_idle_fuel_used)
		(L.L.engine_fuel_used) l0 l2 / (L.L.engine_energy_used_idle2) (L.L.engine_idle_fuel_used) / + s1 + (S.L.engine_fuel_used)
		{endif}
		
	{else}
		(L.L.engine_fuel_used) l0 (C.L.engine_fuel_value) / s1 + (S.L.engine_fuel_used)
	{endif}
	
	l1 (L.S.Timegap) / 3600 * (S.L.engine_consumption_h)

	(L.L.engine_tank_content) l1 - 0 max (C.L.engine_tank_capacity) min (S.L.engine_tank_content)
	0 <= 
	(L.L.engine_failure_general) ||
	{if}
		0 (S.L.engine_on)
	{endif}
	
{end}

{macro:engine_stall}
	
	(T.L.ev_engineshutdown)
	
	0 (S.L.engine_on) 
{end}

{macro:speed_governor}



 
{end}


{macro:engine_temperature}

	(C.L.Max_Power) 1000 * (S.L.engine_Max_Power)
	(L.L.engine_power) abs 1.2 * (C.L.engine_Qrate_engine_factor) * (S.L.engine_Qrate_engine)


	(L.L.elec_busbar_main) (L.L.engine_on) &&
	{if}
		(L.L.engine_temperature) (C.L.luefterthermostat_opn) >
		(L.L.cp_motorkuehlung_sw) ||
		{if}
			1 (S.L.engine_luefter)
		{else}
			(L.L.engine_temperature) (C.L.luefterthermostat_cls) <
			(L.L.cp_motorkuehlung_sw) ! &&
			{if}
			0 (S.L.engine_luefter)
			{endif}
		{endif}
	{else}
		0 (S.L.engine_luefter)
	{endif}

	(L.L.engine_temperature) (C.L.kuehlerthermostat_opn) >
	{if}
		1 (S.L.engine_thermostat_open)
	{else}
		(L.L.engine_temperature) (C.L.kuehlerthermostat_cls) <
		{if}
			0 (S.L.engine_thermostat_open)
		{endif}
	{endif}

	
'	--------------------------------------------------
'	Berechnung der Raten:

	(L.L.engine_temperature) (L.L.engine_temperature_envir) - (C.L.engine_Qrate_cooling_factor) * (S.L.engine_Qrate_cooling)
	(L.L.engine_temperature_envir) (L.S.Weather_Temperature) - (C.L.engine_Qrate_Veloc_factor_basic) (C.L.engine_Qrate_veloc_factor) (L.L.velocity) abs * + * (S.L.engine_Qrate_veloc)

	(L.L.engine_temperature) (L.S.Weather_Temperature) - (C.L.engine_Qrate_fan_factor) * (C.L.rpmgov) 50 - 1.2 *  * 125 / (L.L.engine_Max_Power) * 1.2 * (S.L.engine_Qrate_fan)


	(L.L.engine_thermostat_open)
	{if}
		(L.L.engine_temperature) (C.L.luefterthermostat_cls) >
		{if}
			(L.L.engine_Qrate_fan) 1.2 * (S.L.engine_Qrate_fan)
		{else}
			(L.L.engine_temperature) (C.L.luefterthermostat_cls) >
			{if}
				(L.L.engine_Qrate_fan) 0.8 * (S.L.engine_Qrate_fan)
			{else}
				(L.L.engine_temperature) (C.L.kuehlerthermostat_opn) >
				{if}
					(L.L.engine_Qrate_fan) 0.5 * (S.L.engine_Qrate_fan)
				{else}
					(L.L.engine_temperature) (C.L.kuehlerthermostat_cls) >
					{if}
						(L.L.engine_Qrate_fan) 0.2 * (S.L.engine_Qrate_fan)
					{endif}
				{endif}
			{endif}
		{endif}
	{else}			
'		Nullwirkung des Lüfters durch Kühlwasserkurzschluss	
		0 (S.L.engine_Qrate_fan)
	{endif}


'	--------------------------------------------------
'	Berechnung der Temperaturveränderungen:

	(L.L.engine_Qrate_engine) (L.L.engine_Qrate_cooling) - (L.L.engine_Qrate_fan) - (L.L.cabinair_Qrate_engine_fanheatcooling) - (C.L.engine_cvm_engine) / (L.S.Timegap) * (L.L.engine_temperature) + (S.L.engine_temperature) 
	(L.L.engine_Qrate_cooling) (L.L.engine_Qrate_veloc) - (C.L.engine_cvm_envir) / (L.S.Timegap) * (L.L.engine_temperature_envir) + (S.L.engine_temperature_envir) 


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




'	Kochendes Kühlwasser
	(L.L.engine_temperature) (F.L.kuehlwassersmoke) (S.L.engine_kuehlwasser_freq)


	

	
{end}


