18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 03:37:09 +01:00
Files
maszyna/physics/components/setup.cpp
T
Hirek 5879c228dc refactor(physics): what the rest of the simulator only reads of a vehicle is private to it
53 fields of physics::Vehicle that only the vehicle itself writes, and the rest
of the simulator reads, are private, with a query each, named as the field was
(Vmax(), TotalMass(), Ft(), nrot(), NAxles(), PhysicActivation(), ...): the
compiler now keeps the cab, the AI, the scenery, the renderer and the tests from
changing what the definition gives or the physics works out. The fields were
picked by what is never written outside the class, of plain types, with no
other member of the same name to confuse; inside the class they keep their
meaning under camelCase names. The one place outside that cleared a flag of
the AI through a reference does it through ClearAIFlag().

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

471 lines
18 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/.
*/
// setting the vehicle up after reading its definition: the checks, the brake valves, the handles and the pipes
#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"
// *************************************************************************************************
// Q: 20160717
// *************************************************************************************************
bool physics::Vehicle::CheckLocomotiveParameters(bool ReadyFlag, int /*Dir*/)
{
WriteLog("check locomotive parameters...");
bool OK = true;
autoRelayFlag = autoRelayType == 1;
if (lowVoltage.config.nominalBatteryVoltage == 0.0)
{
lowVoltage.config.batteryStart = start_t::disabled;
}
Sand = SandCapacity;
// NOTE: for diesel-powered vehicles we automatically convert legacy "main" power source to more accurate "engine"
if (air.compressor.config.supply == CompressorSupply::mainCircuit && (EngineType == TEngineType::DieselEngine || EngineType == TEngineType::DieselElectric))
{
air.compressor.config.supply = CompressorSupply::engine;
}
// for these engine types secondary controller positions index MotorParam table
if ((EngineType == TEngineType::ElectricSeriesMotor || EngineType == TEngineType::DieselEngine || EngineType == TEngineType::DieselElectric) &&
scndCtrlPosNo > MotorParametersArraySize)
{
WriteLog("SCPN=" + std::to_string(scndCtrlPosNo) + " exceeds motor parameter table size, limited to " + std::to_string(MotorParametersArraySize));
scndCtrlPosNo = MotorParametersArraySize;
}
// WriteLog("aa = " + AxleArangement + " " + std::string( Pos("o", AxleArangement)) );
if (contains(AxleArangement, "o") && EngineType == TEngineType::ElectricSeriesMotor)
{
// test poprawnosci ilosci osi indywidualnie napedzanych
OK = RList[1].Bn * RList[1].Mn == nPoweredAxles;
// WriteLogSS("aa ok", BoolToYN(OK));
}
if (brakes.config.kind == TBrakeSystem::Individual && brakes.config.subsystem != TBrakeSubSystem::ss_None)
OK = false; //!
if (brakes.config.valveVolume == 0 && brakes.config.maxCylinderPressure[3] > 0 && brakes.config.kind != TBrakeSystem::Individual)
{
brakes.config.valveVolume = brakes.config.maxCylinderPressure[3] / (5.0 - brakes.config.maxCylinderPressure[3]) * (brakes.config.cylinders.radius * brakes.config.cylinders.radius * brakes.config.cylinders.stroke * brakes.config.cylinders.count * M_PI) * 1000;
}
if (brakes.config.valveVolume == 0.0)
{
brakes.config.valveVolume = 0.01;
}
// WriteLog("BVV = " + FloatToStr(BrakeVVolume));
CreateBrakeValve();
CreateBrakeHandles();
CreateBrakePipes();
if (LightsPosNo > 0)
lightsPos = LightsDefPos;
// set default wiper switch position
wiperSwitchPos = WiperDefaultPos;
air.compressor.ResetListPosition();
// the genset of a dual-mode vehicle is a second source of the traction circuit of an induction motor vehicle
if (Genset.fitted() && (EngineType != TEngineType::ElectricInductionMotor || EnginePowerSource.SourceType != TPowerSource::CurrentCollector))
{
WriteLog("GensetPower ignored: a genset works only with EngineType=ElectricInductionMotor and EnginePower=CurrentCollector");
Genset = genset_device{};
}
// NOTE: legacy compatibility behaviour for vehicles without defined heating power source
if (EnginePowerSource.SourceType == TPowerSource::CurrentCollector && HeatingPowerSource.SourceType == TPowerSource::NotDefined)
{
HeatingPowerSource.SourceType = TPowerSource::Main;
}
if (HeatingPowerSource.SourceType == TPowerSource::NotDefined && trainHeating.config.power > 0)
{
HeatingPowerSource.SourceType = TPowerSource::PowerCable;
HeatingPowerSource.PowerType = TPowerType::ElectricPower;
}
// checking ready flag
// to dac potem do init
if (ReadyFlag) // gotowy do drogi
{
SetReadyToDepart();
}
else
{ // zahamowany}
SetBraked();
}
actFlowSpeed = 0.0;
brakes.SetHandlePositionReal(brakes.HandlePosition());
if (brakes.config.independentValveType == TBrakeHandle::Knorr)
brakes.SetIndependentPosition(0.5);
brakePipe.reservoir->CreatePress(brakePipe.Pressure());
mainPipe.reservoir->CreatePress(mainPipe.Pressure());
brakePipe.reservoir->Act();
mainPipe.reservoir->Act();
brakes.equivalentPipePressure = brakePipe.Pressure();
brakes.driverValve->Init(brakePipe.Pressure());
ComputeConstans();
if (brakes.loadSetting > 0)
{
if (loadAmount < maxLoad * 0.45)
{
IncBrakeMult();
IncBrakeMult();
DecBrakeMult(); // TODO: przeinesiono do physics.cpp
if (loadAmount < maxLoad * 0.35)
DecBrakeMult();
}
else
{
IncBrakeMult(); // TODO: przeinesiono do physics.cpp
if (loadAmount >= maxLoad * 0.55)
IncBrakeMult();
}
}
// taki mini automat - powinno byc ladnie dobrze :)
brakes.delaySetting = bdelay_P;
if (TestFlag(brakes.config.availableDelays, bdelay_G) && !TestFlag(brakes.config.availableDelays, bdelay_R))
brakes.delaySetting = bdelay_G;
if (TestFlag(brakes.config.availableDelays, bdelay_R) && !TestFlag(brakes.config.availableDelays, bdelay_G))
brakes.delaySetting = bdelay_R;
/*
// disabled, as test mode is used in specific situations and not really a default
if (BrakeOpModes & bom_PS)
BrakeOpModeFlag = bom_PS;
else
*/
brakes.operatingMode = bom_PN;
// yB: jesli pojazdy nie maja zadeklarowanych czasow, to wsadz z przepisow +-16,(6)%
std::array<int, 8> const DefBrakeTable{15, 4, 25, 25, 13, 3, 12, 2};
for (int b = 1; b < 4; b++)
{
if (brakes.config.delays[b] == 0)
brakes.config.delays[b] = DefBrakeTable[b];
brakes.config.delays[b] = brakes.config.delays[b] * (2.5 + Random(0.0, 0.2)) / 3.0;
}
brakes.distributor->Init(brakePipe.Pressure(), brakes.config.pipeHighPressure, brakes.config.pipeLowPressure, brakes.cylinderPressure, brakes.delaySetting);
/*
ScndPipePress = Compressor;
*/
// WriteLogSS("OK=", BoolTo10(OK));
// WriteLog("");
if (EIMCtrlType == 3)
{
for (auto idx = 0; idx < MainCtrlPosNo; ++idx)
{
if (UniCtrlList[idx].MaxCtrlVal > 0.0)
{
UniCtrlNoPowerPos = std::max(0, idx - 1);
break;
}
}
}
// security system
// by default place the magnet in the vehicle centre
if (SecuritySystem.MagnetLocation == 0)
{
SecuritySystem.MagnetLocation = Dim.L / 2 - 0.5;
}
SecuritySystem.MagnetLocation = safe_clamp(SecuritySystem.MagnetLocation, 0.0, Dim.L);
return OK;
}
// the distributor valve of the vehicle's kind
void physics::Vehicle::CreateBrakeValve()
{
switch (brakes.config.distributorType)
{
case TBrakeValve::W:
case TBrakeValve::K:
{
WriteLog("XBT W, K");
brakes.distributor = std::make_shared<TWest>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
if (brakes.config.maxPressureMass < 2) // jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.config.maxCylinderPressure[3], 0);
else
brakes.distributor->SetLP(Mass, brakes.config.maxPressureMass, brakes.config.maxCylinderPressure[1]);
break;
}
case TBrakeValve::KE:
{
WriteLog("XBT WKE");
brakes.distributor = std::make_shared<TKE>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
brakes.distributor->SetRM(brakes.config.rapidRatio);
if (brakes.config.maxPressureMass < 2) // jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.config.maxCylinderPressure[3], 0);
else
brakes.distributor->SetLP(Mass, brakes.config.maxPressureMass, brakes.config.maxCylinderPressure[1]);
break;
}
case TBrakeValve::NESt3:
case TBrakeValve::ESt3:
case TBrakeValve::ESt3AL2:
case TBrakeValve::ESt4:
{
WriteLog("XBT NESt3, ESt3, ESt3AL2, ESt4");
brakes.distributor = std::make_shared<TNESt3>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
static_cast<TNESt3 *>(brakes.distributor.get())->SetSize(brakes.config.distributorSize, brakes.config.distributorParameters);
if (brakes.config.maxPressureMass < 2) // jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.config.maxCylinderPressure[3], 0);
else
brakes.distributor->SetLP(Mass, brakes.config.maxPressureMass, brakes.config.maxCylinderPressure[1]);
break;
}
case TBrakeValve::LSt:
{
WriteLog("XBT LSt");
brakes.distributor = std::make_shared<TLSt>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
brakes.distributor->SetRM(brakes.config.rapidRatio);
break;
}
case TBrakeValve::EStED:
{
WriteLog("XBT EStED");
brakes.distributor = std::make_shared<TEStED>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
brakes.distributor->SetRM(brakes.config.rapidRatio);
if (brakes.config.maxPressureMass < 2)
{
// jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.config.maxCylinderPressure[3], 0);
}
else
{
brakes.distributor->SetLP(Mass, brakes.config.maxPressureMass, brakes.config.maxCylinderPressure[1]);
}
break;
}
case TBrakeValve::EP2:
{
WriteLog("XBT EP2");
brakes.distributor = std::make_shared<TEStEP2>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
brakes.distributor->SetLP(Mass, brakes.config.maxPressureMass, brakes.config.maxCylinderPressure[1]);
break;
}
case TBrakeValve::EP1:
{
WriteLog("XBT EP1");
brakes.distributor = std::make_shared<TEStEP1>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
brakes.distributor->SetLP(Mass, brakes.config.maxPressureMass, brakes.config.maxCylinderPressure[1]);
brakes.distributor->SetRM(brakes.config.rapidRatio);
break;
}
case TBrakeValve::CV1:
{
WriteLog("XBT CV1");
brakes.distributor = std::make_shared<TCV1>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
break;
}
case TBrakeValve::CV1_L_TR:
{
WriteLog("XBT CV1_L_T");
brakes.distributor = std::make_shared<TCV1L_TR>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
break;
}
default:
brakes.distributor = std::make_shared<TBrake>(brakes.config.maxCylinderPressure[3], brakes.config.cylinders.radius, brakes.config.cylinders.stroke, brakes.config.valveVolume, brakes.config.cylinders.count, brakes.config.availableDelays, brakes.config.method, nAxles, brakes.config.blocksPerAxle);
}
brakes.distributor->SetASBP(brakes.config.maxCylinderPressure[4]);
brakes.distributor->SetRV(brakes.config.rapidSpeed);
}
// the driver's handle and the local brake handle of the vehicle's kinds
void physics::Vehicle::CreateBrakeHandles()
{
switch (brakes.config.driverValveType)
{
using enum TBrakeHandle;
case FV4a:
brakes.driverValve = std::make_shared<TFV4aM>();
break;
case MHZ_EN57:
case MHZ_K8P:
brakes.driverValve = std::make_shared<TMHZ_EN57>();
break;
case FVel6:
brakes.driverValve = std::make_shared<TFVel6>();
break;
case FVE408:
brakes.driverValve = std::make_shared<TFVE408>();
break;
case testH:
brakes.driverValve = std::make_shared<Ttest>();
break;
case M394:
brakes.driverValve = std::make_shared<TM394>();
break;
case Knorr:
brakes.driverValve = std::make_shared<TH14K1>();
break;
case St113:
brakes.driverValve = std::make_shared<TSt113>();
break;
case MHZ_K5P:
brakes.driverValve = std::make_shared<TMHZ_K5P>();
break;
case MHZ_6P:
brakes.driverValve = std::make_shared<TMHZ_6P>();
break;
default:
brakes.driverValve = std::make_shared<TDriverHandle>();
}
brakes.driverValve->SetParams(brakes.config.driverValveSettings.automaticOverload, brakes.config.driverValveSettings.manualOverload, brakes.config.driverValveSettings.genericParameter1, brakes.config.driverValveSettings.genericParameter2, brakes.config.driverValveSettings.overloadMaxPressure, brakes.config.driverValveSettings.overloadPressureDecrease);
switch (brakes.config.independentValveType)
{
case TBrakeHandle::FD1:
{
brakes.independentValve = std::make_shared<TFD1>();
brakes.independentValve->Init(brakes.config.maxCylinderPressure[0]);
if (trainType == train_type::emu)
{
dynamic_cast<TFD1 *>(brakes.independentValve.get())->SetSpeed(3.5);
}
break;
}
case TBrakeHandle::Knorr:
{
brakes.independentValve = std::make_shared<TH1405>();
brakes.independentValve->Init(brakes.config.maxCylinderPressure[0]);
break;
}
default:
brakes.independentValve = std::make_shared<TDriverHandle>();
}
}
// the brake pipe, the main pipe and the spring brake's cylinder
void physics::Vehicle::CreateBrakePipes()
{
if (true == TestFlag(brakes.config.availableDelays, bdelay_G) && (false == TestFlag(brakes.config.availableDelays, bdelay_R) || Power > 1.0)) // ustalanie srednicy przewodu glownego (lokomotywa lub napędowy
brakes.config.pipeSection = 0.792;
else
brakes.config.pipeSection = 0.507;
// WriteLog("SPG = " + FloatToStr(Spg));
brakePipe.reservoir = std::make_shared<TReservoir>();
mainPipe.reservoir = std::make_shared<TReservoir>(); // zabezpieczenie, bo sie PG wywala... :(
brakePipe.reservoir->CreateCap((std::max(Dim.L, 14.0) + 0.5) * brakes.config.pipeSection * 1); // dlugosc x przekroj x odejscia i takie tam
mainPipe.reservoir->CreateCap((std::max(Dim.L, 14.0) + 0.5) * brakes.config.pipeSection * 1);
if (!brakes.spring.cylinder)
{
brakes.spring.cylinder = std::make_shared<TReservoir>();
brakes.spring.cylinder->CreateCap(1);
}
}
// the pressure of the main pipe at the start: the main reservoir's, or HosePressure fed through a main hose, or none
double physics::Vehicle::InitialScndPipePress(double const HosePressure) const
{
if (air.config.mainReservoirVolume > 0.0)
{
return air.CompressedVolume() / air.config.mainReservoirVolume;
}
if (Couplers[VehicleEnd::front].AllowedFlag.HasAny(Coupling::mainHose) || Couplers[VehicleEnd::rear].AllowedFlag.HasAny(Coupling::mainHose))
{
return HosePressure;
}
return 0.0;
}
// the air and the controls of a vehicle ready to depart
void physics::Vehicle::SetReadyToDepart()
{
WriteLog("Ready to depart");
air.Fill(air.config.mainReservoirVolume * air.compressor.config.minPressure * 9.8 / 10.0);
mainPipe.SetPressure(InitialScndPipePress(5.0));
brakePipe.SetPressure(brakes.controlReservoirPressure);
brakes.cylinderPressure = 0.0;
brakes.SetIndependentPosition(0.0);
if (CabActive == 0)
brakes.SetHandlePosition(static_cast<int>(brakes.driverValve->GetPos(bh_NP)));
else
brakes.SetHandlePosition(static_cast<int>(brakes.driverValve->GetPos(bh_RP)));
/*
// NOTE: disabled and left up to the driver, if there's any
MainSwitch( false );
PantFront( true );
PantRear( true );
MainSwitch( true );
*/
DirActive = 0; // Dir; //nastawnik kierunkowy - musi być ustawiane osobno!
DirAbsolute = DirActive * CabActive; // kierunek jazdy względem sprzęgów
brakes.pipeLimiterPressure = brakes.controlReservoirPressure;
lowVoltage.ResetBattery();
}
// the air and the brakes of a vehicle standing braked
void physics::Vehicle::SetBraked()
{
WriteLog("Braked");
brakes.reservoirAir = brakes.config.valveVolume * brakes.config.maxCylinderPressure[3];
air.Fill(air.config.mainReservoirVolume * air.compressor.config.minPressure * 0.55);
/*
ScndPipePress = 5.1;
*/
mainPipe.SetPressure(InitialScndPipePress(5.1));
brakePipe.SetPressure(brakes.config.pipeLowPressure);
brakes.pipeCylinderPressure = brakes.config.maxCylinderPressure[3] * 0.5;
brakes.cylinderPressure = brakes.config.maxCylinderPressure[3] * 0.5;
brakes.SetIndependentPosition(0.0);
brakes.SetHandlePosition(static_cast<int>(brakes.driverValve->GetPos(bh_NP)));
brakes.pipeLimiterPressure = brakes.config.pipeLowPressure;
if (brakes.config.localKind == TLocalBrake::ManualBrake || brakes.config.hasHandBrake == true)
{
IncManualBrakeLevel(ManualBrakePosNo);
}
if (brakes.spring.maxForce > 0.0)
{
brakes.spring.activate = true;
}
}