18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 15:17:08 +01:00
Files
maszyna/physics/components/setup.cpp
T
Hirek 16568d5695 refactor(physics): the brake valves, cylinders, pressures and positions in physics::Braking
The rest of the braking of physics::Vehicle moves into brakes, with English
names (physics/components/braking.h):

  the valves      Hamulec distributor, Handle driverValve,
                  LocHandle independentValve
  the definition  BrakeSystem kind, BrakeSubsystem subsystem, BrakeValve /
                  BrakeValveSize / BrakeValveParams distributorType / Size /
                  Parameters, BrakeHandle / BrakeLocHandle driverValveType /
                  independentValveType, the Handle_* driverValveSettings,
                  LocalBrake localKind, MaxBrakePress maxCylinderPressure,
                  LoadFlag loadSetting, P2FTrans pressureToForce, NBpA
                  blocksPerAxle, High/Low/DeltaPipePress pipeHigh/LowPressure
                  / pipePressureRange, CntrlPipePress controlReservoirPressure,
                  Spg pipeSection, RapidMult / RapidVel rapidRatio / rapidSpeed,
                  BrakeDelay delays, BrakeCtrlPosNo handlePositions, ...
  cylinders       BrakeCylNo / Radius / Dist / Mult / Spring, BrakeSlckAdj,
                  BrakeRigEff: count, radius, stroke, leverRatio, springForce,
                  slackAdjusterForce, riggingEfficiency
  emergencyValve  EmergencyValveOn / Off / Open / Area / Flow
  pipeLock        LockPipeOn / Off, HandleUnlock, LockPipe, UnlockPipe
  the state       BrakePress cylinderPressure, LocBrakePress
                  independentCylinderPressure, EqvtPipePress
                  equivalentPipePressure, Volume reservoirAir, dpBrake / dpPipe /
                  dpMainValve / dpLocalValve brakeFlow / pipeFlow /
                  driverValveFlow / independentValveFlow, BrakeCtrlPos(R, 2)
                  handlePosition(Real), handleCapPosition, fBrakeCtrlPos
                  handlePositionSmooth, LocalBrakePosA(EIM) independentPosition
                  (Eim), BrakeStatus status, AlarmChainFlag alarmChain,
                  BrakeDelayFlag / BrakeDelays delaySetting / availableDelays,
                  BrakeOpModeFlag / BrakeOpModes operatingMode /
                  availableOperatingModes, UnitBrakeForce unitForce, ...

Renamed in the files of the vehicle itself, and elsewhere only where reached
through -> or .: the driver keeps its own BrakeSystem, the editor its Handle,
the radio its Volume, and the brake valve classes (brakes.*, oerlikonest.*)
their fields of the same names. The C arrays (MaxBrakePress, BrakeCylMult,
the universal buttons) are std::arrays of the same values; the bits of status,
delaySetting and operatingMode stay ints for now.

No change in behaviour: 1149/1149 tests, golden traces exact against staging
62f7a29b.
2026-11-29 01:48:33 +01:00

472 lines
16 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 (NominalBatteryVoltage == 0.0)
{
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.supply == CompressorSupply::mainCircuit && (EngineType == TEngineType::DieselEngine || EngineType == TEngineType::DieselElectric))
{
air.compressor.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.kind == TBrakeSystem::Individual && brakes.subsystem != TBrakeSubSystem::ss_None)
OK = false; //!
if (brakes.valveVolume == 0 && brakes.maxCylinderPressure[3] > 0 && brakes.kind != TBrakeSystem::Individual)
{
brakes.valveVolume = brakes.maxCylinderPressure[3] / (5.0 - brakes.maxCylinderPressure[3]) * (brakes.cylinders.radius * brakes.cylinders.radius * brakes.cylinders.stroke * brakes.cylinders.count * M_PI) * 1000;
}
if (brakes.valveVolume == 0.0)
{
brakes.valveVolume = 0.01;
}
// WriteLog("BVV = " + FloatToStr(BrakeVVolume));
CreateBrakeValve();
CreateBrakeHandles();
CreateBrakePipes();
if (LightsPosNo > 0)
LightsPos = LightsDefPos;
// set default wiper switch position
wiperSwitchPos = WiperDefaultPos;
if (air.compressor.listPositions > 0)
air.compressor.listPosition = air.compressor.listDefaultPosition;
// 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 && HeatingPower > 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.handlePositionReal = brakes.handlePosition;
if (brakes.independentValveType == TBrakeHandle::Knorr)
brakes.independentPosition = 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.availableDelays, bdelay_G) && !TestFlag(brakes.availableDelays, bdelay_R))
brakes.delaySetting = bdelay_G;
if (TestFlag(brakes.availableDelays, bdelay_R) && !TestFlag(brakes.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.delays[b] == 0)
brakes.delays[b] = DefBrakeTable[b];
brakes.delays[b] = brakes.delays[b] * (2.5 + Random(0.0, 0.2)) / 3.0;
}
brakes.distributor->Init(brakePipe.pressure, brakes.pipeHighPressure, brakes.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.distributorType)
{
case TBrakeValve::W:
case TBrakeValve::K:
{
WriteLog("XBT W, K");
brakes.distributor = std::make_shared<TWest>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
if (brakes.maxPressureMass < 2) // jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.maxCylinderPressure[3], 0);
else
brakes.distributor->SetLP(Mass, brakes.maxPressureMass, brakes.maxCylinderPressure[1]);
break;
}
case TBrakeValve::KE:
{
WriteLog("XBT WKE");
brakes.distributor = std::make_shared<TKE>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
brakes.distributor->SetRM(brakes.rapidRatio);
if (brakes.maxPressureMass < 2) // jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.maxCylinderPressure[3], 0);
else
brakes.distributor->SetLP(Mass, brakes.maxPressureMass, brakes.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.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
static_cast<TNESt3 *>(brakes.distributor.get())->SetSize(brakes.distributorSize, brakes.distributorParameters);
if (brakes.maxPressureMass < 2) // jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.maxCylinderPressure[3], 0);
else
brakes.distributor->SetLP(Mass, brakes.maxPressureMass, brakes.maxCylinderPressure[1]);
break;
}
case TBrakeValve::LSt:
{
WriteLog("XBT LSt");
brakes.distributor = std::make_shared<TLSt>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
brakes.distributor->SetRM(brakes.rapidRatio);
break;
}
case TBrakeValve::EStED:
{
WriteLog("XBT EStED");
brakes.distributor = std::make_shared<TEStED>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
brakes.distributor->SetRM(brakes.rapidRatio);
if (brakes.maxPressureMass < 2)
{
// jesli przystawka wazaca
brakes.distributor->SetLP(0, brakes.maxCylinderPressure[3], 0);
}
else
{
brakes.distributor->SetLP(Mass, brakes.maxPressureMass, brakes.maxCylinderPressure[1]);
}
break;
}
case TBrakeValve::EP2:
{
WriteLog("XBT EP2");
brakes.distributor = std::make_shared<TEStEP2>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
brakes.distributor->SetLP(Mass, brakes.maxPressureMass, brakes.maxCylinderPressure[1]);
break;
}
case TBrakeValve::EP1:
{
WriteLog("XBT EP1");
brakes.distributor = std::make_shared<TEStEP1>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
brakes.distributor->SetLP(Mass, brakes.maxPressureMass, brakes.maxCylinderPressure[1]);
brakes.distributor->SetRM(brakes.rapidRatio);
break;
}
case TBrakeValve::CV1:
{
WriteLog("XBT CV1");
brakes.distributor = std::make_shared<TCV1>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
break;
}
case TBrakeValve::CV1_L_TR:
{
WriteLog("XBT CV1_L_T");
brakes.distributor = std::make_shared<TCV1L_TR>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
break;
}
default:
brakes.distributor = std::make_shared<TBrake>(brakes.maxCylinderPressure[3], brakes.cylinders.radius, brakes.cylinders.stroke, brakes.valveVolume, brakes.cylinders.count, brakes.availableDelays, brakes.method, NAxles, brakes.blocksPerAxle);
}
brakes.distributor->SetASBP(brakes.maxCylinderPressure[4]);
brakes.distributor->SetRV(brakes.rapidSpeed);
}
// the driver's handle and the local brake handle of the vehicle's kinds
void physics::Vehicle::CreateBrakeHandles()
{
switch (brakes.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.driverValveSettings.automaticOverload, brakes.driverValveSettings.manualOverload, brakes.driverValveSettings.genericParameter1, brakes.driverValveSettings.genericParameter2, brakes.driverValveSettings.overloadMaxPressure, brakes.driverValveSettings.overloadPressureDecrease);
switch (brakes.independentValveType)
{
case TBrakeHandle::FD1:
{
brakes.independentValve = std::make_shared<TFD1>();
brakes.independentValve->Init(brakes.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.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.availableDelays, bdelay_G) && (false == TestFlag(brakes.availableDelays, bdelay_R) || Power > 1.0)) // ustalanie srednicy przewodu glownego (lokomotywa lub napędowy
brakes.pipeSection = 0.792;
else
brakes.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.pipeSection * 1); // dlugosc x przekroj x odejscia i takie tam
mainPipe.reservoir->CreateCap((std::max(Dim.L, 14.0) + 0.5) * brakes.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.mainReservoirVolume > 0.0)
{
return air.compressedVolume / air.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.compressedVolume = air.mainReservoirVolume * air.compressor.minPressure * 9.8 / 10.0;
mainPipe.pressure = InitialScndPipePress(5.0);
brakePipe.pressure = brakes.controlReservoirPressure;
brakes.cylinderPressure = 0.0;
brakes.independentPosition = 0.0;
if (CabActive == 0)
brakes.handlePosition = static_cast<int>(brakes.driverValve->GetPos(bh_NP));
else
brakes.handlePosition = 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;
Battery = BatteryStart != start_t::disabled;
}
// the air and the brakes of a vehicle standing braked
void physics::Vehicle::SetBraked()
{
WriteLog("Braked");
brakes.reservoirAir = brakes.valveVolume * brakes.maxCylinderPressure[3];
air.compressedVolume = air.mainReservoirVolume * air.compressor.minPressure * 0.55;
/*
ScndPipePress = 5.1;
*/
mainPipe.pressure = InitialScndPipePress(5.1);
brakePipe.pressure = brakes.pipeLowPressure;
brakes.pipeCylinderPressure = brakes.maxCylinderPressure[3] * 0.5;
brakes.cylinderPressure = brakes.maxCylinderPressure[3] * 0.5;
brakes.independentPosition = 0.0;
brakes.handlePosition = static_cast<int>(brakes.driverValve->GetPos(bh_NP));
brakes.pipeLimiterPressure = brakes.pipeLowPressure;
if (brakes.localKind == TLocalBrake::ManualBrake || brakes.hasHandBrake == true)
{
IncManualBrakeLevel(ManualBrakePosNo);
}
if (brakes.spring.maxForce > 0.0)
{
brakes.spring.activate = true;
}
}