18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 00:22:42 +01:00
Files
maszyna/physics/components/diesel.cpp
T
Hirek 23b695752a refactor(physics): the demand of the integrated controller is private to the vehicle
eimic_real, the demand of the integrated controller the vehicle goes by with
the speed control counted, which TDynamicObject and the consist braking set
for the vehicle that leads, is private: EimicReal() and SetEimicReal(). The
field of the same name of drivingaid::input stays as it is.

No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
2026-11-29 13:39:07 +01:00

1488 lines
55 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
// the diesel engine: its revolutions, the gearbox, the clutch, the hydraulic transmission, its heat and pumps
#include "stdafx.h"
#include "physics/vehicle.h"
#include "vehicle/DynObj.h"
#include "physics/oerlikonest.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/Timer.h"
#include "utilities/parser.h"
#include "simulation/simulation.h"
#include "vehicle/fieldshunting.h"
#include <algorithm>
#include <charconv>
#include <cmath>
#include <format>
#include <functional>
#include <limits>
#include <utility>
#include <vector>
#include "physics/components/internal.h"
#include "physics/components/drive.h"
double physics::Vehicle::EngineRPMRatio() const
{
return Drive().EngineRpmRatio(*this);
}
double physics::Vehicle::EngineIdleRPM() const
{
return Drive().EngineIdleRpm(*this);
}
double physics::Vehicle::EngineMaxRPM() const
{
return Drive().EngineMaxRpm(*this);
}
// water pump status check
void physics::Vehicle::WaterPumpCheck(double const /*Timestep*/)
{
// NOTE: breaker override with start type is sm42 specific hack, replace with ability to define the presence of the breaker
WaterPump.is_active = true == (lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable()) && true == WaterPump.breaker && false == WaterPump.is_disabled &&
(true == WaterPump.is_active || true == WaterPump.is_enabled || WaterPump.start_type == start_t::battery);
}
// water heater status check
void physics::Vehicle::WaterHeaterCheck(double const /*Timestep*/)
{
WaterHeater.is_active = false == WaterHeater.is_damaged && true == (lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable()) && true == WaterHeater.is_enabled && true == WaterHeater.breaker &&
(WaterHeater.is_active || WaterHeater.config.temp_min < 0 || diesel.heat.temperatura1 < WaterHeater.config.temp_min);
WaterHeater.is_damaged = true == WaterHeater.is_damaged || (true == WaterHeater.is_active && false == WaterPump.is_active);
if (WaterHeater.config.temp_max > 0 && diesel.heat.temperatura1 > WaterHeater.config.temp_max)
{
WaterHeater.is_active = false;
}
}
// fuel pump status update
void physics::Vehicle::FuelPumpCheck(double const /*Timestep*/)
{
using enum start_t;
FuelPump.is_active = true == (lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable()) && false == FuelPump.is_disabled &&
(FuelPump.is_active || (FuelPump.start_type == manual ? FuelPump.is_enabled :
FuelPump.start_type == automatic ? diesel.startupRequested || lineBreaker.IsClosed() :
FuelPump.start_type == manualwithautofallback ? FuelPump.is_enabled || diesel.startupRequested || lineBreaker.IsClosed() :
false)); // shouldn't ever get this far but, eh
}
// oil pump status update
void physics::Vehicle::OilPumpCheck(double const Timestep)
{
OilPump.is_active = true == (lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable()) && false == lineBreaker.IsClosed() && false == OilPump.is_disabled &&
(OilPump.is_active || (OilPump.start_type == start_t::manual ? OilPump.is_enabled :
OilPump.start_type == start_t::automatic ? diesel.startupRequested :
OilPump.start_type == start_t::manualwithautofallback ? OilPump.is_enabled || diesel.startupRequested :
false)); // shouldn't ever get this far but, eh
auto const minpressure{OilPump.pressure_minimum > 0.f ? OilPump.pressure_minimum : 0.15f}; // arbitrary fallback value
OilPump.pressure_target = enrot > 0.1 ? std::lerp( minpressure, OilPump.pressure_maximum, static_cast<float>( EngineRPMRatio() ) ) * OilPump.resource_amount :
true == OilPump.is_active ? std::min( minpressure + 0.1f, OilPump.pressure_maximum ) : // slight pressure margin to give time to switch off the pump and start the engine
0.f;
if (OilPump.pressure < OilPump.pressure_target)
{
// TODO: scale change rate from 0.01-0.05 with oil/engine temperature/idle time
OilPump.pressure = std::min<float>(OilPump.pressure_target, static_cast<float>(OilPump.pressure + (enrot > 5.0 ? 0.05 : 0.035) * Timestep));
}
if (OilPump.pressure > OilPump.pressure_target)
{
OilPump.pressure = std::max<float>(OilPump.pressure_target, static_cast<float>(OilPump.pressure - (enrot > 5.0 ? 0.05 : 0.035) * 0.5 * Timestep));
}
OilPump.pressure = std::clamp(OilPump.pressure, 0.f, 1.5f);
}
// water pump breaker state toggle
bool physics::Vehicle::WaterPumpBreakerSwitch(bool State, range_t const Notify)
{
/*
if( FuelPump.start_type == start::automatic ) {
// automatic fuel pump ignores 'manual' state commands
return false;
}
*/
bool const initialstate{WaterPump.breaker};
WaterPump.breaker = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::waterPumpBreakerSwitch, WaterPump.breaker ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return WaterPump.breaker != initialstate;
}
// water pump state toggle
bool physics::Vehicle::WaterPumpSwitch(bool State, range_t const Notify)
{
if (WaterPump.start_type == start_t::battery)
{
// automatic fuel pump ignores 'manual' state commands
return false;
}
bool const initialstate{WaterPump.is_enabled};
WaterPump.is_enabled = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::waterPumpSwitch, WaterPump.is_enabled ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return WaterPump.is_enabled != initialstate;
}
// water pump state toggle
bool physics::Vehicle::WaterPumpSwitchOff(bool State, range_t const Notify)
{
if (WaterPump.start_type == start_t::battery)
{
// automatic fuel pump ignores 'manual' state commands
return false;
}
bool const initialstate{WaterPump.is_disabled};
WaterPump.is_disabled = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::waterPumpSwitchOff, WaterPump.is_disabled ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return WaterPump.is_disabled != initialstate;
}
// water heater breaker state toggle
bool physics::Vehicle::WaterHeaterBreakerSwitch(bool State, range_t const Notify)
{
/*
if( FuelPump.start_type == start::automatic ) {
// automatic fuel pump ignores 'manual' state commands
return false;
}
*/
bool const initialstate{WaterHeater.breaker};
WaterHeater.breaker = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::waterHeaterBreakerSwitch, WaterHeater.breaker ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return WaterHeater.breaker != initialstate;
}
// water heater state toggle
bool physics::Vehicle::WaterHeaterSwitch(bool State, range_t const Notify)
{
/*
if( FuelPump.start_type == start::automatic ) {
// automatic fuel pump ignores 'manual' state commands
return false;
}
*/
bool const initialstate{WaterHeater.is_enabled};
WaterHeater.is_enabled = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::waterHeaterSwitch, WaterHeater.is_enabled ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return WaterHeater.is_enabled != initialstate;
}
// water circuits link state toggle
bool physics::Vehicle::WaterCircuitsLinkSwitch(bool State, range_t const Notify)
{
if (false == diesel.heat.auxiliary_water_circuit)
{
// can't link the circuits if the vehicle only has one
return false;
}
bool const initialstate{waterCircuitsLink};
waterCircuitsLink = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::waterCircuitsLinkSwitch, waterCircuitsLink ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return waterCircuitsLink != initialstate;
}
// fuel pump state toggle
bool physics::Vehicle::FuelPumpSwitch(bool State, range_t const Notify)
{
if (FuelPump.start_type == start_t::automatic)
{
// automatic fuel pump ignores 'manual' state commands
return false;
}
bool const initialstate{FuelPump.is_enabled};
FuelPump.is_enabled = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::fuelPumpSwitch, FuelPump.is_enabled ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return FuelPump.is_enabled != initialstate;
}
bool physics::Vehicle::FuelPumpSwitchOff(bool State, range_t const Notify)
{
if (FuelPump.start_type == start_t::automatic)
{
// automatic fuel pump ignores 'manual' state commands
return false;
}
bool const initialstate{FuelPump.is_disabled};
FuelPump.is_disabled = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::fuelPumpSwitchOff, FuelPump.is_disabled ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return FuelPump.is_disabled != initialstate;
}
// oil pump state toggle
bool physics::Vehicle::OilPumpSwitch(bool State, range_t const Notify)
{
if (OilPump.start_type == start_t::automatic)
{
// automatic pump ignores 'manual' state commands
return false;
}
bool const initialstate{OilPump.is_enabled};
OilPump.is_enabled = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::oilPumpSwitch, OilPump.is_enabled ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return OilPump.is_enabled != initialstate;
}
bool physics::Vehicle::OilPumpSwitchOff(bool State, range_t const Notify)
{
if (OilPump.start_type == start_t::automatic)
{
// automatic pump ignores 'manual' state commands
return false;
}
bool const initialstate{OilPump.is_disabled};
OilPump.is_disabled = State;
if (Notify != range_t::local)
{
SendCtrlToNext(VehicleCommand::oilPumpSwitchOff, OilPump.is_disabled ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control);
}
return OilPump.is_disabled != initialstate;
}
// the revolutions of the engine; returns those a diesel-electric engine aims at [1/s], 0 for the others.
// for a diesel engine, the ratio of the transmission and the engine's moment
double physics::Vehicle::UpdateEngineRevolutions(double dt, double &dtrans, double &dmoment)
{
return Drive().UpdateEngineRevolutions(*this, dt, dtrans, dmoment);
}
// the revolutions a diesel-electric's engine aims at [1/s], by the controller and the heating; the fill shown by the smoke
double physics::Vehicle::DieselElectricTargetRevolutions()
{
double tmp;
if (EIMCtrlType > 0) // sterowanie cyfrowe
tmp = (DElist[0].RPM + (DElist[MainCtrlPosNo].RPM - DElist[0].RPM) * std::max(0.0, eimicReal)) / 60.0;
else
tmp = DElist[protection.PressureSwitchTripped() ? MainCtrlNoPowerPos() : mainCtrlPos].RPM / 60.0;
if (true == trainHeating.IsAllowed() && trainHeating.config.power > 0 && trainHeating.config.engineRpm > 0)
{
// bump engine revolutions up if needed, when heating is on
tmp = std::max(tmp, std::min(EngineMaxRPM(), trainHeating.config.engineRpm) / 60.0);
}
// NOTE: fake diesel.fill calculation for the sake of smoke emitter which uses this parameter to determine smoke opacity
diesel.fill = std::clamp(0.2 + 0.35 * (tmp - enrot) + 0.5 * (std::abs(Im) / DElist[MainCtrlPosNo].Imax), 0.05, 1.0);
return tmp;
}
void physics::Vehicle::DieselEngineTractionForce(double dt, double const dmoment, double const dtrans)
{
Mw = dmoment * dtrans * Transmision.Efficiency; // dmoment i dtrans policzone przy okazji enginerotation
if (retarder.present && retarder.placement == 0)
Mw -= dizel_MomentumRetarder(nrotValue * Transmision.Ratio, dt) * Transmision.Ratio * Transmision.Efficiency;
Fw = Mw * 2.0 / wheelDiameter / nPoweredAxles;
ft = Fw * nPoweredAxles; // sila trakcyjna
ft = ft * DirAbsolute; // DirActive*CabActive;
}
// tmp: the revolutions the engine aims at [1/s], see UpdateEngineRevolutions()
void physics::Vehicle::DieselElectricTractionForce(double tmp)
{
double PosRatio;
// tmpV:=V*CabActive*DirActive;
auto const tmpV{nrotValue * Pirazy2 * 0.5 * wheelDiameter * DirAbsolute}; //*CabActive*DirActive;
auto tempUmax = 0.0;
auto tempImax = 0.0;
// jazda manewrowa
if (EIMCtrlType > 0) // sterowanie cyfrowe
{
tmp = DieselElectricDigitalForce(tmpV, tempUmax, tempImax);
PosRatio = tmp / DElist[MainCtrlPosNo].GenPower;
}
else if (true == ShuntMode)
{
DieselElectricShuntForce(tmpV, tmp);
PosRatio = 1;
}
else // jazda ciapongowa
{
auto power = Power;
tempImax = DElist[mainCtrlPos].Imax;
tempUmax = DElist[mainCtrlPos].Umax;
if (true == trainHeating.IsOn())
{
power -= trainHeating.config.power;
}
power = std::max(power, 0.0);
// NOTE: very crude way to approximate power generated at current rpm instead of instant top output
// NOTE, TODO: doesn't take into account potentially increased revolutions if heating is on, fix it
auto const currentgenpower{(DElist[mainCtrlPos].RPM > 0 ? DElist[mainCtrlPos].GenPower * (60.0 * enrot / DElist[mainCtrlPos].RPM) : 0.0)};
tmp = std::min(power, currentgenpower);
PosRatio = currentgenpower / DElist[MainCtrlPosNo].GenPower;
// stosunek mocy teraz do mocy max
// NOTE: Mains in this context is working diesel engine
if (true == lineContactors.IsClosed())
ft = DieselElectricForce(tmpV, power, PosRatio, tmp);
else
ft = 0; // jak nastawnik na zero, to sila tez zero
PosRatio = tmp / DElist[MainCtrlPosNo].GenPower;
}
if (FuseFlag)
ft = 0;
else
ft = ft * DirAbsolute; // DirActive * CabActive; //zwrot sily i jej wartosc
Fw = ft / nPoweredAxles; // sila na obwodzie kola
Mw = Fw * wheelDiameter / 2.0; // moment na osi kola
motorMoment = Mw / Transmision.Ratio; // moment silnika trakcyjnego
Im = dieselelectric_motor_current(motorMoment);
if (ShuntMode)
{
DieselElectricShuntPower(tmp);
}
else
{
DieselElectricGenerator(tmp, tempUmax, tempImax);
}
if (Imax > 1 && Im > Imax)
FuseOff();
if (FuseFlag)
EngineVoltage = 0;
// przekazniki bocznikowania, kazdy inny dla kazdej pozycji
if (auto const fieldshuntingcontrol{fieldshunting::diesel_electric(autoRelayType, fieldShuntSwitch, autoRelayFlag)};
false == lineContactors.IsClosed() || ShuntMode || fieldshuntingcontrol == fieldshunting::control::off)
{
scndCtrlPos = 0;
}
else if (autoRelayFlag)
{
// the shunting relays set the field shunting (fieldshunting::control::automatic)
ShuntFieldRelaysCheck(tmpV, PosRatio, tmp);
}
}
// the tractive force of a diesel-electric shunting, by the generator's table SST and the shunting controller anPos;
// tmp, the power the generator gives
void physics::Vehicle::DieselElectricShuntForce(double const tmpV, double &tmp)
{
if (true == lineContactors.IsClosed())
{
EngineVoltage = SST[mainCtrlPos].Umax * anPos + SST[mainCtrlPos].Umin * (1.0 - anPos);
// NOTE: very crude way to approximate power generated at current rpm instead of instant top output
// NOTE, TODO: doesn't take into account potentially increased revolutions if heating is on, fix it
tmp = EngineRPMRatio() * (SST[mainCtrlPos].Pmax * anPos) + SST[mainCtrlPos].Pmin * (1.0 - anPos);
ft = tmp * 1000.0 / (abs(tmpV) + 1.6);
}
else
{
EngineVoltage = 0;
ft = 0;
}
}
// the tractive force of a diesel-electric set by the integrated controller, at the speed tmpV; the generator's
// voltage and current limits too. returns the power the generator gives
double physics::Vehicle::DieselElectricDigitalForce(double const tmpV, double &tempUmax, double &tempImax)
{
auto eimic_positive = std::max(0.0, eimicReal);
auto const rpmratio{EngineRPMRatio()};
tempImax = DElist[MainCtrlPosNo].Imax * eimic_positive;
tempUmax = DElist[MainCtrlPosNo].Umax * std::min(eimic_positive, rpmratio);
auto const tempPmax = DElist[MainCtrlPosNo].GenPower * std::min(eimic_positive, rpmratio);
if (true == lineContactors.IsClosed())
ft = DieselElectricForce(tmpV, tempPmax, eimic_positive, tempPmax);
else
ft = 0; // jak nastawnik na zero, to sila tez zero
return tempPmax;
}
// the tractive force of a diesel-electric at the speed tmpV: on the straight part of its characteristic under the speed
// Vhyp gives at the power LinePower, at the share Ratio of it, on the hyperbola of the power HyperbolaPower over it
double physics::Vehicle::DieselElectricForce(double const tmpV, double const LinePower, double const Ratio, double const HyperbolaPower) const
{
if (tmpV < Vhyp * LinePower / DElist[MainCtrlPosNo].GenPower)
{
// czy na czesci prostej, czy na hiperboli
return (Ftmax - (Ftmax - 1000.0 * DElist[MainCtrlPosNo].GenPower / (Vhyp + Vadd)) * (tmpV / Vhyp) / PowerCorRatio) * Ratio; // posratio - bo sila jakos tam sie rozklada
}
// na hiperboli
// 1.107 - wspolczynnik sredniej nadwyzki Ft w symku nad charakterystyka
return 1000.0 * HyperbolaPower / (tmpV + Vadd) / PowerCorRatio; // tu jest zawarty stosunek mocy
}
// in the shunting mode, the generator's power limited to tmp, the power the engine gives
void physics::Vehicle::DieselElectricShuntPower(double const tmp)
{
EnginePower = EngineVoltage * Im / 1000.0;
if (EnginePower > tmp)
{
EnginePower = tmp;
EngineVoltage = EnginePower * 1000.0 / Im;
}
if (EnginePower < tmp)
{
ft *= EnginePower / tmp;
}
}
// the generator's current limited to tempImax and its voltage by its characteristic, under tempUmax and the power tmp
void physics::Vehicle::DieselElectricGenerator(double const tmp, double const tempUmax, double const tempImax)
{
if (abs(Im) > tempImax)
{ // nie ma nadmiarowego, tylko Imax i zwarcie na pradnicy
ft = ft / Im * tempImax;
Im = tempImax;
}
if (Im > 0)
{
// jak pod obciazeniem
if (true == Flat)
{
// ograniczenie napiecia w pradnicy - plaszczak u gory
EngineVoltage = 1000.0 * tmp / std::abs(Im);
}
else
{
auto tempMCP = EIMCtrlType > 0 ? 1 + 99 * std::max(1.0, eimicReal) : mainCtrlPos;
auto tempMCPN = EIMCtrlType > 0 ? 100 : MainCtrlPosNo;
// charakterystyka pradnicy obcowzbudnej (elipsa) - twierdzenie Pitagorasa
EngineVoltage = std::sqrt(std::abs(square(tempUmax) - square(tempUmax * Im / tempImax))) * (tempMCP - 1) + (1.0 - Im / tempImax) * tempUmax * (tempMCPN - tempMCP);
EngineVoltage /= tempMCPN - 1;
EngineVoltage = safe_clamp(EngineVoltage, Im * 0.05, 1000.0 * tmp / std::abs(Im));
}
}
if (EngineVoltage > tempUmax || Im == 0)
{
// gdy wychodzi za duze napiecie albo przy biegu jalowym (jest cos takiego?)
EngineVoltage = tempUmax * (converter.IsRunning() ? 1 : 0);
}
EnginePower = EngineVoltage * Im / 1000.0;
/*
// power curve drop
// NOTE: disabled for the time being due to side-effects
if( ( tmpV > 1 ) && ( EnginePower < tmp ) ) {
ft = std::lerp(
ft, EnginePower / tmp,
std::clamp( tmpV - 1.0, 0.0, 1.0 ) );
}
*/
}
// the current of a diesel-electric vehicle's traction motors giving the moment [N m] of one motor
double physics::Vehicle::dieselelectric_motor_current(double const Moment) const
{
// with MotorParam[scndCtrlPos] do
if (abs(Moment) > motor_param(scndCtrlPos).fi)
return nPoweredAxles * abs(abs(Moment) / motor_param(scndCtrlPos).mfi + motor_param(scndCtrlPos).mIsat);
else
return nPoweredAxles * sqrt(abs(Moment * motor_param(scndCtrlPos).Isat));
}
// the effort of a diesel-electric vehicle's dynamic brake the driver asks for, 0..1
double physics::Vehicle::dieselelectric_dynamic_brake_demand() const
{
switch (DEDynamicBrake.source)
{
case diesel_electric_dynamic_brake::control::controller:
return std::clamp(-eimicReal, 0.0, 1.0);
case diesel_electric_dynamic_brake::control::lever:
return DynamicBrakeRatio();
default:
return 0.0;
}
}
// the force [N] of a diesel-electric vehicle's dynamic brake at full effort at the speed: the most the motors take
// (the current), above the speed where the resistors' power takes over, the power; under EDBrakeV1 it fades, down to
// nothing at EDBrakeV0. a brake of direct current motors, whose force grows with the speed (the voltage they generate
// into the resistors), fades from 0 to the speed of its whole force; an inverter's only at the stop
double physics::Vehicle::dieselelectric_dynamic_brake_limit() const
{
auto const &brake{DEDynamicBrake};
auto limit{brake.max_force};
if (brake.max_power > 0.0 && std::abs(travelSpeed) > 0.0)
{
limit = std::min(limit, brake.max_power / std::abs(travelSpeed));
}
if (brake.fade_high > brake.fade_low)
{
limit *= std::clamp((Vel - brake.fade_low) / (brake.fade_high - brake.fade_low), 0.0, 1.0);
}
else if (Vel < brake.fade_low)
{
limit = 0.0;
}
return limit;
}
// the dynamic brake of a diesel-electric vehicle, before the drive's force: the effort follows the demand, the force
// is the share of the most at the speed. the motors brake while the engine runs (it excites them, or keeps the
// control of the inverters going), with the direction set, the overload relay closed, no power asked for and the
// wheels rolling; otherwise there's no brake at once
void physics::Vehicle::update_dieselelectric_dynamic_brake(double const dt)
{
auto &brake{DEDynamicBrake};
auto const nopower{brake.source == diesel_electric_dynamic_brake::control::controller ? eimicReal <= 0.0 : IsMainCtrlNoPowerPos()};
if (lineBreaker.IsClosed() && DirActive != 0 && false == FuseFlag && false == slippingWheels && nopower)
{
brake.level += std::clamp(dieselelectric_dynamic_brake_demand() - brake.level, -brake.rate * dt, brake.rate * dt);
}
else
{
brake.level = 0.0;
}
brake.force = brake.level * dieselelectric_dynamic_brake_limit();
brakes.electrodynamic.on = brake.force > 0.0;
if (brake.blending)
{
// the train brake of the locomotive lets off by the share of the dynamic brake's force (LSt, EStED)
brakes.distributor->SetED(brake.max_force > 0.0 ? brake.force / brake.max_force : 0.0);
}
}
// the force of a diesel-electric vehicle's dynamic brake, against the motion, in place of the drive's one: the motors
// work as generators into the resistors, the ammeter shows their current, the main generator gives nothing. near the
// stop no more than what stops the vehicle within the step dt: with no fade (EDBrakeV0 = EDBrakeV1 = 0) the whole force
// would turn round each time the speed crosses zero, and in FTrain it never lets the stop of ComputeMovement() catch it
void physics::Vehicle::apply_dieselelectric_dynamic_brake(double const dt)
{
auto const force{dt > 0.0 ? std::min(DEDynamicBrake.force, totalMass * std::abs(travelSpeed) / dt) : DEDynamicBrake.force};
ft = travelSpeed > 0.0 ? -force : travelSpeed < 0.0 ? force : 0.0;
Fw = ft / nPoweredAxles;
Mw = Fw * wheelDiameter / 2.0;
motorMoment = Mw / Transmision.Ratio;
Im = dieselelectric_motor_current(motorMoment);
EngineVoltage = 0.0;
EnginePower = 0.0;
}
// *************************************************************************************************
// Q: 20160715
// Zmienia parametr do którego dąży sprzęgło
// *************************************************************************************************
bool physics::Vehicle::dizel_EngageSwitch(double state)
{
if (EngineType == TEngineType::DieselEngine && state <= 1 && state >= 0 && state != diesel.clutchState)
{
diesel.clutchState = state;
return true;
}
else
return false;
}
// *************************************************************************************************
// Q: 20160715
// Zmienia parametr do którego dąży sprzęgło
// *************************************************************************************************
bool physics::Vehicle::dizel_EngageChange(double dt)
{
double engagespeed = 0; // OK:boolean;
bool DEC;
DEC = false;
if (diesel.clutchPressure - diesel.clutchState > 0)
engagespeed = engagedownspeed;
else
engagespeed = engageupspeed;
if (dt > 0.2)
dt = 0.1;
if (abs(diesel.clutchPressure - diesel.clutchState) < 0.11)
{
if (diesel.clutchPressure != diesel.clutchState)
{
DEC = true;
diesel.clutchPressure = diesel.clutchState;
}
// else OK:=false; //już jest false
}
else
{
diesel.clutchPressure = diesel.clutchPressure + engagespeed * dt * (diesel.clutchState - diesel.clutchPressure);
// OK:=false;
}
// dizel_EngageChange:=OK;
return DEC;
}
// *************************************************************************************************
// Q: 20160715
// Automatyczna zmiana biegów gdy prędkość przekroczy widełki
// *************************************************************************************************
bool physics::Vehicle::dizel_AutoGearCheck(void)
{
auto OK{false};
auto const VelUp{(motor_param(ScndCtrlActualPos).mfi0 != 0.0 ?
motor_param(ScndCtrlActualPos).mfi0 + (motor_param(ScndCtrlActualPos).mfi - motor_param(ScndCtrlActualPos).mfi0) * std::max(0.0, eimicReal) :
motor_param(ScndCtrlActualPos).mfi)};
auto const VelDown{(motor_param(ScndCtrlActualPos).fi0 != 0.0 && eimicReal <= 0.0 ? motor_param(ScndCtrlActualPos).fi0 : motor_param(ScndCtrlActualPos).fi)};
if (motor_param(ScndCtrlActualPos).AutoSwitch && lineBreaker.IsClosed())
{
OK = dizel_GearShiftCheck(VelUp, VelDown);
}
if (lineBreaker.IsClosed())
{
dizel_ClutchCheck();
}
return OK;
}
// the automatic gearbox: a gear up over VelUp, down under VelDown, changed once the clutch is out; returns whether it acted
bool physics::Vehicle::dizel_GearShiftCheck(double const VelUp, double const VelDown)
{
auto OK{false};
if (RList[mainCtrlPos].Mn == 0 && !torqueConverter.present)
{
if (diesel.clutchState > 0)
dizel_EngageSwitch(0);
if (IsMainCtrlNoPowerPos() && ScndCtrlActualPos > 0)
diesel.automaticGearShift = -1;
}
else
{
OK = dizel_GearShiftRequest(VelUp, VelDown);
}
if (diesel.clutchPressure < 0.1 && diesel.automaticGearShift != 0)
{
if (diesel.automaticGearShift == 1)
ScndCtrlActualPos++;
else
ScndCtrlActualPos--;
diesel.automaticGearShift = 0;
dizel_EngageSwitch(1.0);
OK = true;
}
return OK;
}
// a gear up asked over VelUp, down under VelDown, where the gearbox shifts by itself; returns whether one is
bool physics::Vehicle::dizel_GearShiftRequest(double const VelUp, double const VelDown)
{
auto OK{false};
if (motor_param(ScndCtrlActualPos).AutoSwitch && diesel.automaticGearShift == 0) // sprawdz czy zmienic biegi
{
if (Vel > VelUp)
{
// shift up
if (ScndCtrlActualPos < scndCtrlPosNo)
{
diesel.automaticGearShift = 1;
OK = true;
}
}
else if (Vel < VelDown)
{
// shift down
if (ScndCtrlActualPos > 0)
{
diesel.automaticGearShift = -1;
OK = true;
}
}
}
return OK;
}
// the pressure in the clutch's cylinder, by the controller's position or the integrated controller
void physics::Vehicle::dizel_ClutchCheck()
{
if (EIMCtrlType > 0) // sterowanie komputerowe
{
dizel_ClutchByIntegratedController();
}
else if (diesel.automaticGearShift == 0) // ustaw cisnienie w silowniku sprzegla}
dizel_ClutchByController();
else
dizel_EngageSwitch(0.0);
if (!(motor_param(ScndCtrlActualPos).mIsat > 0))
dizel_EngageSwitch(0.0); // wylacz sprzeglo na pozycjach neutralnych
if (!autoRelayFlag)
ScndCtrlActualPos = scndCtrlPos;
}
// the clutch by the controller's position: half or fully in, fully over the speed of the full engagement
void physics::Vehicle::dizel_ClutchByController()
{
switch (RList[mainCtrlPos].Mn)
{
case 1:
dizel_EngageSwitch(0.5);
break;
case 2:
dizel_EngageSwitch(1.0);
break;
case 3:
if (Vel > diesel.fullEngageMinVelocity)
dizel_EngageSwitch(1.0);
else
dizel_EngageSwitch(0.5);
break;
case 4:
if (Vel > diesel.fullEngageMinVelocity)
dizel_EngageSwitch(1.0);
else
dizel_EngageSwitch(0.66);
break;
case 5:
if (Vel > diesel.fullEngageMinVelocity)
dizel_EngageSwitch(1.0);
else
dizel_EngageSwitch(0.35 * (1 + RList[mainCtrlPos].R) * RList[mainCtrlPos].R);
break;
default:
if (torqueConverter.present && torqueConverter.fill > 0.01)
dizel_EngageSwitch(1.0);
else
dizel_EngageSwitch(0.0);
}
}
// the clutch of the integrated controller: in with the converter filled or the power asked, half in for the retarder
void physics::Vehicle::dizel_ClutchByIntegratedController()
{
if (diesel.automaticGearShift == 0)
{
if ((torqueConverter.present && torqueConverter.fill > 0.01) || eimicReal > 0.005)
dizel_EngageSwitch(1.0);
else if (Vel > retarder.engageVelocity && retarder.present && retarder.fill > 0.01)
dizel_EngageSwitch(0.5);
else
dizel_EngageSwitch(0.0);
}
else
dizel_EngageSwitch(0.0);
}
// performs diesel engine startup procedure; potentially clears startup switch; returns: true if the engine can be started, false otherwise
bool physics::Vehicle::dizel_StartupCheck()
{
auto engineisready{true}; // make inital optimistic presumption, then watch the reality crush it
// test the fuel pump
// TODO: add fuel pressure check
if (false == FuelPump.is_active || (EngineType == TEngineType::DieselEngine && RList[mainCtrlPos].R == 0.0))
{
engineisready = false;
// if( FuelPump.start_type == start_t::manual ) {
// with manual pump control startup procedure is done only once per starter switch press
diesel.startupRequested = false;
// }
}
// test the oil pump
if (false == OilPump.is_active || OilPump.pressure < OilPump.pressure_minimum)
{
engineisready = false;
if (OilPump.start_type == start_t::manual)
{
// with manual pump control startup procedure is done only once per starter switch press
diesel.startupRequested = false;
}
}
// test the water circuits and water temperature
if (true == diesel.heat.PA)
{
engineisready = false;
// TBD, TODO: reset startup procedure depending on pump and heater control mode
diesel.startupRequested = false;
}
return engineisready;
}
// *************************************************************************************************
// Q: 20160715
// Aktualizacja stanu silnika
// *************************************************************************************************
bool physics::Vehicle::dizel_Update(double dt)
{
WaterPumpCheck(dt);
WaterHeaterCheck(dt);
OilPumpCheck(dt);
FuelPumpCheck(dt);
// the engine being started fires once the starter has turned it a while; one stopped meanwhile starts over
diesel.firingCountdown = (lineBreaker.IsClosed() ? std::max(0.0, diesel.firingCountdown - dt) : 0.0);
if (true == diesel.startupRequested && true == dizel_StartupCheck())
{
diesel.ignitionRequested = true;
}
if (true == diesel.ignitionRequested && LastSwitchingTime >= initialCtrlDelay)
{
diesel.startupRequested = false;
diesel.ignitionRequested = false;
// TODO: split engine and main circuit state indicator in two separate flags
LastSwitchingTime = 0;
Timer::subsystem.switching.note(Timer::switching_counter::diesel, this, &TypeName, &Name);
lineBreaker.Close();
diesel.spinningUp = true;
diesel.firingCountdown = diesel.smokeDelay;
enrot = std::max(enrot,
0.35 * ( // TODO: dac zaleznie od temperatury i baterii
EngineType == TEngineType::DieselEngine ? diesel.minRevolutions : DElist[0].RPM / 60.0));
}
diesel.spinningUp = diesel.spinningUp && lineBreaker.IsClosed() && enrot < 0.95 * (EngineType == TEngineType::DieselEngine ? diesel.minRevolutions : DElist[0].RPM / 60.0);
if (true == lineBreaker.IsClosed() && false == FuelPump.is_active)
{
// knock out the engine if the fuel pump isn't feeding it
// TBD, TODO: grace period before the engine is starved for fuel and knocked out
// an engine started on its own stops on its own
MainSwitch(false, MainSwitchRange());
}
bool DU{false};
if (EngineType == TEngineType::DieselEngine)
{
dizel_EngageChange(dt);
DU = dizel_AutoGearCheck();
double const fillspeed{2};
diesel.fill = diesel.fill + fillspeed * dt * (dizel_fillcheck(mainCtrlPos, dt) - diesel.fill);
}
dizel_Heat(dt);
return DU;
}
// *************************************************************************************************
// Q: 20160715
// oblicza napelnienie, uzwglednia regulator obrotow
// *************************************************************************************************
double physics::Vehicle::dizel_fillcheck(int mcp, double dt)
{
auto realfill{0.0};
if (true == lineBreaker.IsClosed() && MainCtrlPosNo > 0 && true == FuelPump.is_active)
{
if (true == diesel.ignitionRequested && LastSwitchingTime >= 0.9 * initialCtrlDelay)
{
// wzbogacenie przy rozruchu
// NOTE: ignition flag is reset before this code is executed
// TODO: sort this out
realfill = 1;
}
else
{
// napelnienie zalezne od mainCtrlPos
if (EIMCtrlType > 0)
{
realfill = dizel_DigitalFill(dt);
}
else
{
diesel.governorMaxRevolutions = diesel.maxRevolutions;
realfill = RList[mcp].R;
}
}
if (diesel.cutoffRevolutions > 0)
{
realfill = dizel_GovernorFill(mcp, realfill);
}
}
return std::clamp(realfill, 0.0, 1.0);
}
// the fill set by the integrated controller, and the idle and top revolutions the governor keeps to
double physics::Vehicle::dizel_DigitalFill(double dt)
{
double realfill;
realfill = std::max(0.0, std::min(eimicReal, 1 - motor_param(ScndCtrlActualPos).Isat));
if (eimicReal > 0.005 && !torqueConverter.lockedUp)
{
diesel.governorMinRevolutions = std::min(diesel.governorMinRevolutions + 2.5 * dt, diesel.hydraulicMinRevolutions + eimicReal * diesel.hydraulicMinRevolutionsFactor);
}
else
{
if (Vel < retarder.engageVelocity && retarder.present && retarder.fill > 0.01)
diesel.governorMinRevolutions = std::min(diesel.governorMinRevolutions + 5.0 * dt, diesel.retarderRevolutions);
else
diesel.governorMinRevolutions = diesel.minRevolutions;
}
if (!diesel.speedMaxRevolutionsTable.empty() && !torqueConverter.lockedUp)
{
diesel.governorMaxRevolutions = std::min(std::min(diesel.governorMaxRevolutions, enrot) + diesel.governorAcceleration * dt, TableInterpolation(diesel.speedMaxRevolutionsTable, Vel));
}
else
{
diesel.governorMaxRevolutions = diesel.maxRevolutions;
}
return realfill;
}
// the fill cut off over the revolutions the governor allows at the controller's position, or full under the idle ones
double physics::Vehicle::dizel_GovernorFill(int const mcp, double realfill) const
{
auto nreg{0.0};
if (EIMCtrlType > 0)
nreg = eimicReal > 0.005 ? diesel.governorMaxRevolutions : diesel.minRevolutions;
else
nreg = dizel_GovernorRevolutions(mcp, realfill);
if (enrot > nreg) // nad predkoscia regulatora zeruj dawke
realfill = 0;
if (enrot < diesel.governorMinRevolutions && RList[mcp].R > 0.001) // jesli ponizej biegu jalowego i niezerowa dawka, to dawaj pelna
realfill = 1;
return realfill;
}
// the revolutions the governor allows at the controller's position, the gearbox and the speed; the fill cut off at
// a position the governor doesn't know
double physics::Vehicle::dizel_GovernorRevolutions(int const mcp, double &realfill) const
{
auto nreg{0.0};
switch (RList[mainCtrlPos].Mn)
{
case 0:
case 1:
nreg = diesel.minRevolutions;
break;
case 2:
if (diesel.automaticGearShift == 0 && true /*(!torqueConverter.present) || (diesel.clutchPressure>diesel.fill)*/)
nreg = diesel.governorMaxRevolutions;
else
nreg = diesel.minRevolutions;
break;
case 3:
if (diesel.automaticGearShift == 0 && Vel > diesel.fullEngageMinVelocity)
nreg = diesel.governorMaxRevolutions;
else
nreg = diesel.minRevolutions;
break;
case 4:
if (diesel.automaticGearShift == 0 && Vel > diesel.fullEngageMinVelocity)
nreg = diesel.maxRevolutions;
else
nreg = diesel.minRevolutions * 0.75 + diesel.governorMaxRevolutions * 0.25;
break;
case 5:
if (Vel > diesel.fullEngageMinVelocity)
nreg = diesel.governorMaxRevolutions;
else
nreg = diesel.minRevolutions + 0.8 * (diesel.governorMaxRevolutions - diesel.minRevolutions) * RList[mcp].R;
break;
default:
realfill = 0; // sluczaj
break;
}
return nreg;
}
// *************************************************************************************************
// Q: 20160715
// Oblicza moment siły wytwarzany przez silnik spalinowy
// *************************************************************************************************
double physics::Vehicle::dizel_Momentum(double Fill, double n, double dt)
{ // liczy moment sily wytwarzany przez silnik spalinowy}
double Moment = 0;
double enMoment = 0;
double gearMoment = 0;
double eps = 0;
double newn = 0;
double friction = 0;
double TorqueH = 0;
double TorqueL = 0;
double TorqueC = 0;
n = n * CabActive;
if (motor_param(ScndCtrlActualPos).mIsat < 0.001 || DirActive == 0)
n = enrot;
friction = diesel.clutchFriction;
torqueConverter.revolutionsIn = enrot; // wal wejsciowy przetwornika momentu
torqueConverter.revolutionsOut = diesel.previousRevolutions; // wal wyjsciowy przetwornika momentu
if (enrot > 0)
{
if (diesel.momentTable.size() > 1)
{
Moment = TableInterpolation(diesel.momentTable, enrot) * Fill - diesel.standingMoment;
}
else
{
Moment = (diesel.maxMoment - (diesel.maxMoment - diesel.maxRevolutionsMoment) * square((enrot - diesel.maxMomentRevolutions) / (diesel.maxMomentRevolutions - diesel.maxRevolutions))) * Fill - diesel.standingMoment;
}
motorMoment = Moment;
diesel.fuelConsumptionActual = diesel.fuelConsumption * enrot * Fill;
diesel.fuelConsumedTotal += diesel.fuelConsumptionActual * dt / 3600.0;
if (retarder.present && retarder.placement == 2)
Moment -= dizel_MomentumRetarder(enrot, dt);
}
else
{
Moment = -diesel.standingMoment;
}
if (enrot < diesel.minRevolutions / 10.0 && eAngleValue < M_PI_2)
{
// wstrzymywanie przy malych obrotach
Moment -= diesel.standingMoment;
}
if (true == diesel.spinningUp)
Moment += diesel.standingMoment / (0.3 + std::max(0.0, enrot / diesel.minRevolutions)); // rozrusznik
diesel.torque = Moment;
hydro_TCFillCheck(dt);
// obliczanie momentow poszczegolnych sprzegiel
// sprzeglo glowne (skrzynia biegow)
TorqueC = diesel.clutchMaxForce * diesel.clutchPressure * diesel.clutchDiameter * friction;
if (torqueConverter.present) // jesli hydro
{
hydro_TCTorque();
TorqueH = torqueConverter.torqueOut;
TorqueL = torqueConverter.lockupTorque * torqueConverter.lockupRate;
}
else
{
TorqueH = 0; // brak przetwornika oznacza brak momentu
TorqueL = 1 + TorqueC * 2; // zabezpieczenie, polaczenie trwale
}
// sprawdzanie dociskow poszczegolnych sprzegiel
if (abs(Moment) > std::min(TorqueC, TorqueL + abs(torqueConverter.torqueIn)) || abs(diesel.previousRevolutions - enrot) > 0.1) // slizga sie z powodu roznic predkosci albo przekroczenia momentu
{
diesel.clutchSlip = enrot - diesel.previousRevolutions;
if (TorqueC > TorqueL)
{
if (TorqueC > TorqueL + abs(TorqueH))
{
torqueConverter.revolutionsOut = n;
gearMoment = TorqueL + abs(TorqueH) * sign(diesel.clutchSlip);
enMoment = Moment - (TorqueL + abs(torqueConverter.torqueIn)) * sign(diesel.clutchSlip);
}
else
{
torqueConverter.revolutionsOut = enrot - (n - enrot) * (TorqueC - TorqueL) / TorqueH; // slizganie proporcjonalne, zeby przetwornik nadrabial
gearMoment = TorqueC * sign(diesel.clutchSlip);
enMoment = Moment - gearMoment;
}
}
else
{
torqueConverter.revolutionsOut = enrot;
gearMoment = TorqueC*sign(diesel.clutchSlip);
enMoment = Moment - gearMoment;
}
eps = enMoment / diesel.shaftInertia;
newn = enrot + eps * dt;
if ((newn - n) * (enrot - diesel.previousRevolutions) < 0 && TorqueC > 0.1) // przejscie przez zero - slizgalo sie i przestało
newn = n;
if (newn * enrot <= 0 && eps * enrot < 0) // przejscie przez zero obrotow
newn = 0;
enrot = newn;
}
else // nie slizga sie (jeszcze)
{
diesel.clutchSlip = 0;
gearMoment = Moment;
double enrot_min = enrot - (std::min(TorqueC, TorqueL + abs(torqueConverter.torqueIn)) - Moment) / diesel.shaftInertia * dt;
double enrot_max = enrot + (std::min(TorqueC, TorqueL + abs(torqueConverter.torqueIn)) + Moment) / diesel.shaftInertia * dt;
enrot = safe_clamp(n, enrot_min, enrot_max);
}
if (retarder.present && retarder.placement == 1)
gearMoment -= dizel_MomentumRetarder(torqueConverter.revolutionsOut, dt);
if (enrot <= 0 && false == diesel.spinningUp)
{
// an engine stalling stops those started together with it, once. one already stopped, from its own switch or
// another vehicle's, or not started at all, has nothing to stop: it stays quiet, rather than stop every frame
// the engines started on their own or from the separate switches (eu07/maszyna#80)
if (true == lineBreaker.IsClosed())
{
MainSwitch(false, MainSwitchRange());
}
enrot = 0;
}
diesel.previousRevolutions = n; // obecna predkosc katowa na potrzeby kolejnej klatki
return gearMoment;
}
// the torque converter filled and emptied, and its lockup clutch engaged and released, by the power and the speed
void physics::Vehicle::hydro_TCFillCheck(double dt)
{
if (torqueConverter.present) // jesli przetwornik momentu
{
// napelnianie przetwornika
bool IsPower = EIMCtrlType > 0 ? eimicReal > 0.005 : MainCtrlPowerPos() > 0;
if (IsPower && lineBreaker.IsClosed() && enrot > diesel.minRevolutions * 0.9)
torqueConverter.fill += torqueConverter.fillRateIncrease * dt;
// oproznianie przetwornika
if ((!IsPower && Vel < diesel.converterReleaseVelocity) || !lineBreaker.IsClosed() || enrot < diesel.minRevolutions * 0.8)
torqueConverter.fill -= torqueConverter.fillRateDecrease * dt;
// obcinanie zakresu
torqueConverter.fill = std::clamp(torqueConverter.fill, 0.0, 1.0);
// blokowanie sprzegla blokującego
if (Vel > torqueConverter.lockupSpeed && lineBreaker.IsClosed() && enrot > 0.9 * diesel.minRevolutions && IsPower)
{
torqueConverter.lockedUp = true;
torqueConverter.lockupRate += torqueConverter.fillRateIncrease * dt;
}
// luzowanie sprzegla blokujacego
if (Vel < (IsPower ? torqueConverter.lockupSpeed : torqueConverter.unlockSpeed) || !lineBreaker.IsClosed() || enrot < 0.8 * diesel.minRevolutions)
{
torqueConverter.lockedUp = false;
torqueConverter.lockupRate -= torqueConverter.fillRateDecrease * dt;
}
// obcinanie zakresu
torqueConverter.lockupRate = std::clamp(torqueConverter.lockupRate, 0.0, 1.0);
}
else
{
torqueConverter.fill = 0.0;
torqueConverter.lockupRate = 0.0;
}
}
// the torque converter's moments on its input and output shafts, by their revolutions and its fill
void physics::Vehicle::hydro_TCTorque()
{
double HydroTorque = 0;
HydroTorque += torqueConverter.revolutionsIn * torqueConverter.revolutionsIn * torqueConverter.torqueInIn;
HydroTorque += (torqueConverter.revolutionsIn - torqueConverter.revolutionsOut) * torqueConverter.torqueInOut;
HydroTorque += torqueConverter.revolutionsOut * torqueConverter.revolutionsOut * torqueConverter.torqueOutOut;
double nOut2In = torqueConverter.revolutionsOut / std::max(0.01, torqueConverter.revolutionsIn);
if (torqueConverter.table.size() > 1)
{
torqueConverter.torqueRatio = TableInterpolation(torqueConverter.table, nOut2In);
torqueConverter.torqueOut = HydroTorque * torqueConverter.fill * torqueConverter.torqueRatio;
torqueConverter.torqueIn = HydroTorque * torqueConverter.fill * std::min(1.0, torqueConverter.torqueRatio);
}
else
{
if (nOut2In < torqueConverter.couplingPoint)
{
torqueConverter.torqueRatio = 1 + (torqueConverter.maxTorqueRatio - 1) * square(1 - nOut2In / torqueConverter.couplingPoint);
torqueConverter.torqueIn = HydroTorque * torqueConverter.fill;
torqueConverter.torqueOut = HydroTorque * torqueConverter.fill * torqueConverter.torqueRatio;
}
else
{
torqueConverter.torqueRatio = (1 - nOut2In) / (1 - torqueConverter.couplingPoint);
torqueConverter.torqueIn = HydroTorque * torqueConverter.fill * torqueConverter.torqueRatio;
torqueConverter.torqueOut = HydroTorque * torqueConverter.fill * torqueConverter.torqueRatio;
}
}
}
double physics::Vehicle::dizel_MomentumRetarder(double n, double dt)
{
double RetarderRequest = lineBreaker.IsClosed() ? std::max(0.0, -eimicReal) : 0;
if (retarder.withIndividual)
RetarderRequest = LocalBrakeRatio();
if (Vel < retarder.minVelocity)
RetarderRequest = 0;
if (retarder.placement == 2 && enrot < diesel.minRevolutions)
{
RetarderRequest = 0;
}
retarder.clutchActive = !retarder.hasClutch || RetarderRequest > 0;
if (!retarder.hasClutch || (retarder.clutchActive && retarder.clutchSpeed == 0))
{
retarder.revolutions = n * 60;
}
else if (retarder.clutchActive)
{
retarder.revolutions = sign(n) * std::min(std::abs(retarder.revolutions + retarder.clutchSpeed * dt), std::abs(n * 60));
}
else
{
retarder.revolutions = 0;
}
n = retarder.revolutions / 60.f;
if (retarder.fill < RetarderRequest) // gdy zadane hamowanie
{
retarder.fill = std::min(retarder.fill + retarder.fillRateIncrease * dt, RetarderRequest);
}
else
{
retarder.fill = std::max(retarder.fill - retarder.fillRateDecrease * dt, RetarderRequest);
}
double Moment = retarder.maxTorque;
if (double pwr = Moment * std::abs(n) * M_PI * 2 * 0.001; pwr > retarder.maxPower)
Moment = Moment * retarder.maxPower / pwr;
double moment_in = n * n * retarder.torqueInIn;
Moment = std::min(moment_in, Moment * retarder.fill);
retarder.torque = Moment;
return Moment;
}
// sets component temperatures to specified value
void physics::Vehicle::dizel_HeatSet(float const Value)
{
// TODO: don't include ambient temperature, pull it from environment data instead
diesel.heat.Te = Value;
diesel.heat.Ts = Value;
diesel.heat.To = Value;
diesel.heat.Tsr = Value;
diesel.heat.Twy = Value;
diesel.heat.Tsr2 = Value;
diesel.heat.Twy2 = Value;
diesel.heat.temperatura1 = Value;
diesel.heat.temperatura2 = Value;
}
// calculates diesel engine temperature and heat transfers
// adapted from scripts written by adamst
// NOTE: originally executed twice per second
void physics::Vehicle::dizel_Heat(double const dt)
{
auto const qs{44700.0};
auto const Cs{11000.0};
auto const Cw{4.189};
auto const Co{1.885};
auto const gwmin{400.0};
auto const gwmax{4000.0};
auto const gwmin2{400.0};
auto const gwmax2{4000.0};
diesel.heat.Te = Global.AirTemperature;
auto const engineon{(lineBreaker.IsClosed() ? 1 : 0)};
auto const engineoff{(lineBreaker.IsClosed() ? 0 : 1)};
auto const rpm{enrot * 60};
// TODO: calculate this once and cache for further use, instead of doing it repeatedly all over the place
auto const revolutionsfactor{EngineRPMRatio()};
auto const waterpump{WaterPump.is_active ? 1 : 0};
auto const gw = engineon * std::lerp(gwmin, gwmax, revolutionsfactor) + waterpump * 1000 + engineoff * 200;
auto const gw2 = engineon * std::lerp(gwmin2, gwmax2, revolutionsfactor) + waterpump * 1000 + engineoff * 200;
auto const gwO = std::lerp(gwmin, gwmax, revolutionsfactor);
diesel.heat.water.is_cold = diesel.heat.water.config.temp_min > 0 && diesel.heat.temperatura1 < diesel.heat.water.config.temp_min - (lineBreaker.IsClosed() ? 5 : 0);
diesel.heat.water.is_hot = diesel.heat.water.config.temp_max > 0 && diesel.heat.temperatura1 > diesel.heat.water.config.temp_max - (diesel.heat.water.is_hot ? 8 : 0);
diesel.heat.water_aux.is_cold = diesel.heat.water_aux.config.temp_min > 0 && diesel.heat.temperatura2 < diesel.heat.water_aux.config.temp_min - (lineBreaker.IsClosed() ? 5 : 0);
diesel.heat.water_aux.is_hot = diesel.heat.water_aux.config.temp_max > 0 && diesel.heat.temperatura2 > diesel.heat.water_aux.config.temp_max - (diesel.heat.water_aux.is_hot ? 8 : 0);
diesel.heat.oil.is_cold = diesel.heat.oil.config.temp_min > 0 && diesel.heat.To < diesel.heat.oil.config.temp_min - (lineBreaker.IsClosed() ? 5 : 0);
diesel.heat.oil.is_hot = diesel.heat.oil.config.temp_max > 0 && diesel.heat.To > diesel.heat.oil.config.temp_max - (diesel.heat.oil.is_hot ? 8 : 0);
// engine overheat check
diesel.heat.engine_is_hot = diesel.heat.engine_max_temp > 0 && diesel.heat.Ts > diesel.heat.engine_max_temp - (diesel.heat.engine_is_hot ? 8 : 0);
auto const PT = false == diesel.heat.water.is_cold && false == diesel.heat.water.is_hot && false == diesel.heat.water_aux.is_cold && false == diesel.heat.water_aux.is_hot &&
false == diesel.heat.oil.is_cold && false == diesel.heat.oil.is_hot && false == diesel.heat.engine.is_hot /* && ( false == awaria_termostatow ) */
/* || PTp */;
auto const PPT = false == PT /* && ( false == PPTp ) */;
diesel.heat.PA = /* ( ( !zamkniecie or niedomkniecie ) and !WBD ) || */ PPT /* || nurnik || ( woda < 7 ) */;
// engine heat transfers
auto const Ge{engineon * (0.21 * diesel.heat.powerfactor * EnginePower + 12) / 3600};
// TODO: replace fixed heating power cost with more accurate calculation
auto const obciazenie{engineon * (diesel.heat.powerfactor * EnginePower / 950 + (trainHeating.IsOn() ? trainHeating.config.power : 0) + 70)};
auto const Qd{qs * Ge - obciazenie};
// silnik oddaje czesc ciepla do wody chlodzacej, a takze pewna niewielka czesc do otoczenia, modyfikowane przez okienko
auto const Qs{(Qd - diesel.heat.kfs * (diesel.heat.Ts - diesel.heat.Tsr) - diesel.heat.kfe * /* ( 0.3 + 0.7 * ( diesel.heat.okienko ? 1 : 0 ) ) * */ (diesel.heat.Ts - diesel.heat.Te))};
auto const dTss{Qs / Cs};
diesel.heat.Ts += static_cast<float>(dTss * dt);
// oil heat transfers
// olej oddaje cieplo do wody gdy krazy przez wymiennik ciepla == wlaczona pompka lub silnik
auto const dTo{(diesel.heat.auxiliary_water_circuit ? (diesel.heat.kfo * (diesel.heat.Ts - diesel.heat.To) - diesel.heat.kfo2 * (diesel.heat.To - diesel.heat.Tsr2)) / (gwO * Co) :
(diesel.heat.kfo * (diesel.heat.Ts - diesel.heat.To) - diesel.heat.kfo2 * (diesel.heat.To - diesel.heat.Tsr)) / (gwO * Co))};
diesel.heat.To += static_cast<float>(dTo * dt);
// heater
/*
if( typ == "SP45" )
Qp = (float)( podgrzewacz and ( true == WaterPump.is_active ) and ( Twy < 55 ) and ( Twy2 < 55 ) ) * 1000;
else
*/
auto const Qp = (true == WaterHeater.is_active && true == WaterPump.is_active && diesel.heat.Twy < 60 && diesel.heat.Twy2 < 60 ? 1 : 0) * 1000;
auto const kurek07{1}; // unknown/unimplemented device TBD, TODO: identify and implement?
if (true == diesel.heat.auxiliary_water_circuit)
{
// auxiliary water circuit setup
diesel.heat.water_aux.is_warm = true == diesel.heat.cooling ||
(true == lineBreaker.IsClosed() && lowVoltage.BatteryVoltage() > 0.75 * lowVoltage.config.nominalBatteryVoltage /* && !bezpompy && !awaria_chlodzenia && !WS10 */
&& diesel.heat.water_aux.config.temp_cooling > 0 && diesel.heat.temperatura2 > diesel.heat.water_aux.config.temp_cooling - (diesel.heat.water_aux.is_warm ? 8 : 0));
auto const PTC2{(diesel.heat.water_aux.is_warm /*or PTC2p*/ ? 1 : 0)};
diesel.heat.rpmwz2 = static_cast<float>(PTC2 * (diesel.heat.fan_speed >= 0 ? rpm * diesel.heat.fan_speed : diesel.heat.fan_speed * -1));
diesel.heat.zaluzje2 = diesel.heat.water_aux.config.shutters ? PTC2 == 1 : true; // no shutters is an equivalent to having them open
auto const zaluzje2{(diesel.heat.zaluzje2 ? 1 : 0)};
// auxiliary water circuit heat transfer values
auto const kf2{kurek07 * (diesel.heat.kw * (0.3 + 0.7 * zaluzje2) * diesel.heat.rpmw2 + diesel.heat.kv * (0.3 + 0.7 * zaluzje2) * Vel / 3.6) + 2};
auto const dTs2{diesel.heat.kfo2 * (diesel.heat.To - diesel.heat.Tsr2) / (gw2 * Cw)};
// przy otwartym kurku B ma³y obieg jest dogrzewany przez du¿y - stosujemy przy korzystaniu z podgrzewacza oraz w zimie
auto const Qch2{-kf2 * (diesel.heat.Tsr2 - diesel.heat.Te) + 80 * (true == waterCircuitsLink ? 1 : 0) * (diesel.heat.Twy - diesel.heat.Tsr2)};
auto const dTch2{Qch2 / (gw2 * Cw)};
// auxiliary water circuit heat transfers finalization
// NOTE: since primary circuit doesn't read data from the auxiliary one, we can pretty safely finalize auxiliary updates before touching the primary circuit
auto const Twe2{diesel.heat.Twy2 + dTch2 * dt};
diesel.heat.Twy2 = static_cast<float>(Twe2 + dTs2 * dt);
diesel.heat.Tsr2 = static_cast<float>(0.5 * (diesel.heat.Twy2 + Twe2));
diesel.heat.temperatura2 = diesel.heat.Twy2;
}
// primary water circuit setup
diesel.heat.water.is_flowing = diesel.heat.water.config.temp_flow < 0 || diesel.heat.temperatura1 > diesel.heat.water.config.temp_flow - (diesel.heat.water.is_flowing ? 5 : 0);
auto const obieg{(diesel.heat.water.is_flowing ? 1 : 0)};
diesel.heat.water.is_warm =
true == diesel.heat.cooling || (true == lineBreaker.IsClosed() && lowVoltage.BatteryVoltage() > 0.75 * lowVoltage.config.nominalBatteryVoltage /* && !bezpompy && !awaria_chlodzenia && !WS10 */
&& diesel.heat.water.config.temp_cooling > 0 && diesel.heat.temperatura1 > diesel.heat.water.config.temp_cooling - (diesel.heat.water.is_warm ? 8 : 0));
auto const PTC1{(diesel.heat.water.is_warm /*or PTC1p*/ ? 1 : 0)};
diesel.heat.rpmwz = static_cast<float>(PTC1 * (diesel.heat.fan_speed >= 0 ? rpm * diesel.heat.fan_speed : diesel.heat.fan_speed * -1));
diesel.heat.zaluzje1 = diesel.heat.water.config.shutters ? PTC1 == 1 : true; // no shutters is an equivalent to having them open
auto const zaluzje1{(diesel.heat.zaluzje1 ? 1 : 0)};
// primary water circuit heat transfer values
auto const kf{obieg * kurek07 * (diesel.heat.kw * (0.3 + 0.7 * zaluzje1) * diesel.heat.rpmw + diesel.heat.kv * (0.3 + 0.7 * zaluzje1) * Vel / 3.6 + 3) + 2};
auto const dTs{(diesel.heat.auxiliary_water_circuit ? diesel.heat.kfs * (diesel.heat.Ts - diesel.heat.Tsr) / (gw * Cw) :
(diesel.heat.kfs * (diesel.heat.Ts - diesel.heat.Tsr) + diesel.heat.kfo2 * (diesel.heat.To - diesel.heat.Tsr)) / (gw * Cw))};
auto const Qch{-kf * (diesel.heat.Tsr - diesel.heat.Te) + Qp};
auto const dTch{Qch / (gw * Cw)};
// primary water circuit heat transfers finalization
auto const Twe{diesel.heat.Twy + dTch * dt};
diesel.heat.Twy = static_cast<float>(Twe + dTs * dt);
diesel.heat.Tsr = static_cast<float>(0.5 * (diesel.heat.Twy + Twe));
diesel.heat.temperatura1 = diesel.heat.Twy;
/*
fuelConsumed = fuelConsumed + ( Ge * 0.5 );
while( fuelConsumed >= 0.83 ) {
fuelConsumed = fuelConsumed - 0.83;
fuelQueue.DestroyProductMatching( null, 1 );
}//if
if( engineon )
temp_turbo = temp_turbo + 0.3 * ( t_pozycja );
if( t_pozycja == 0 and cisnienie > 0.04 )
temp_turbo = temp_turbo - 1;
if( temp_turbo > 400 )
temp_turbo = 400;
if( temp_turbo < 0 )
temp_turbo = 0;
if( temp_turbo > 50 and cisnienie < 0.05 )
timer_turbo = timer_turbo + 1;
if( temp_turbo == 0 )
timer_turbo = 0;
if( timer_turbo > 360 ) {
awaria_turbo = true;
timer_turbo = 400;
}
if( Ts < 50 )
p_odpal = 3;
if( Ts > 49 and Ts < 76 )
p_odpal = 4;
if( Ts > 75 )
p_odpal = 7;
stukanie = stukanie or awaria_oleju;
if( awaria_oleju == true and ilosc_oleju > 0 ) {
ilosc_oleju = ilosc_oleju - ( 0.002 * rpm / 1500 );
}
if( awaria_oleju == true and cisnienie < 0.06 )
damage = 1;
*/
}