18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 07:07:05 +01:00
Files
maszyna/physics/consistbraking.cpp
T
jerrrrycho 1996301a7b perf(physics): the MED log is tried once per vehicle when it can't be opened
A vehicle blending ED and EP brakes (ConsistBrakeBlending) opens MEDLOGS/<name>_<n>.csv once it brakes
while moving. Without the MEDLOGS folder, which the game doesn't come with, the open failed and was tried
again on every step of the braking: a path built and a CreateFile call each time, with <n> counted up.
The failure is now remembered and the log isn't tried again for that vehicle. With the folder there the
first attempt opens it, and what is written is the same as before.

Tomaszewo (raj40821) 600 s in --test, seed 12345: OpenLog was 0.18% of the main thread there (few ED
consists brake in it), the run time is within noise (sim 268.3 s median of 3 before, 269.8 s after);
the 14 physics logs and the event log are byte-identical, unit tests 1205/1205 with exact golden traces.
2026-11-30 03:35:29 +01:00

423 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/.
*/
#include "stdafx.h"
#include "physics/consistbraking.h"
#include <algorithm>
#include <cmath>
#include <string>
#include "physics/vehicle.h"
#include "physics/brakes.h"
#include "utilities/utilities.h"
// the state shared by the steps of the blending
struct physics::ConsistBrakeBlending::Step {
int np { 0 };
double masa { 0.0 };
double FrED { 0.0 };
double masamax { 0.0 };
double FmaxPN { 0.0 };
double FfulED { 0.0 };
double FmaxED { 0.0 };
int osie { 0 };
double eimic { 0.0 };
double LBR { 0.0 };
double eim_lb { 0.0 };
double amax { 0.0 };
bool doorisopen { false };
double Fzad { 0.0 };
double FzadED { 0.0 };
double FzadPN { 0.0 };
float nPrzekrF { 0.f };
};
// the consist with induction motors driven and braked as a whole, its braking split between the ED and the EP brakes
double physics::ConsistBrakeBlending::Update(Vehicle &Lead, std::vector<Vehicle *> const &Consist, Inputs const &Given, double const dt1)
{
Step state;
SetControl( state, Lead, Given, dt1 );
SumForces( state, Consist );
ComputeDeceleration( state, Lead );
UpdateShuntMode( state, Lead );
ComputeDemand( state, Lead, dt1 );
OpenLog( state, Lead );
ComputeElectrodynamicDemand( state, Lead, dt1 );
DistributeForces( state, Lead, Consist, dt1 );
Record( state, Lead, Consist, dt1 );
lastEdDemand = state.FzadED;
return state.amax;
}
// step 0a of the blending: the demand of the integrated controller read and passed down the consist
void physics::ConsistBrakeBlending::SetControl(Step &state, Vehicle &Lead, Inputs const &Given, double const dt1)
{
// 0a. ustal aktualna nastawe zadania sily napedowej i hamowania
if( Lead.Power < 1
&& Given.owner != nullptr ) {
Lead.SetMainCtrlPos(Given.owner->MainCtrlPos()*Lead.MainCtrlPosNo / std::max(1, Given.owner->MainCtrlPosNo));
Lead.SpeedCtrlValue = Given.owner->SpeedCtrlValue;
Lead.SpeedCtrlUnit.IsActive = Given.owner->SpeedCtrlUnit.IsActive;
}
Lead.CheckEIMIC(dt1);
Lead.CheckSpeedCtrl(dt1);
state.eimic = std::min(Lead.eimic, Lead.eimicSpeedCtrl());
Lead.SetEimicReal(state.eimic);
// Elf: full braking at once in the lowest position, and in the emergency brake position below it
if (Lead.EIMCtrlType == 2 && Lead.MainCtrlPos() <= Lead.EIMCtrlEmergencyPositions())
state.eimic = -1.0;
Lead.SendCtrlToNext(VehicleCommand::integratedController, std::max(0., state.eimic), Lead.CabActive);
state.LBR = std::max(-state.eimic, 0.);
state.eim_lb = Given.aiDriving || !Lead.brakes.config.independentTraxx ? 0 : Lead.eim_localbrake;
}
// steps 1-2 of the blending: the masses and the most braking forces, pneumatic and ED, summed over the consist
void physics::ConsistBrakeBlending::SumForces(Step &state, std::vector<Vehicle *> const &Consist)
{
auto FED { 0.0 };
auto Frj { 0.0 };
// 1. ustal wymagana sile hamowania calego pociagu
// - opoznienie moze byc ustalane na podstawie charakterystyki
// - opoznienie moze byc ustalane na podstawie mas i cisnien granicznych
//
// 2. ustal mozliwa do realizacji sile hamowania ED
// - w szczegolnosci powinien brac pod uwage rozne sily hamowania
for (auto *vehicle : Consist)
{
++state.np;
state.masamax +=
vehicle->brakes.config.maxPressureMass
+ ( vehicle->brakes.config.maxPressureMass > 1.0 ?
0.0 :
vehicle->Mass() )
+ vehicle->Mred();
auto const Nmax = ((vehicle->brakes.config.pressureToForce * vehicle->brakes.config.maxCylinderPressure[0] -
vehicle->brakes.config.cylinders.springForce) *
vehicle->brakes.config.cylinders.leverRatio[0] -
vehicle->brakes.config.cylinders.slackAdjusterForce) *
vehicle->brakes.config.cylinders.count * vehicle->brakes.config.cylinders.riggingEfficiency;
state.FmaxPN += Nmax * vehicle->brakes.distributor->GetFC(
Nmax / (vehicle->NAxles() * vehicle->brakes.config.blocksPerAxle),
vehicle->brakes.blending.referenceSpeed) *
1000; // sila hamowania pn
state.FmaxED += vehicle->lineBreaker.IsClosed() && vehicle->DirActive != 0 && vehicle->inductionMotor.inverterRatio == 1.0 &&
vehicle->inductionMotor.braking.maxForce * vehicle->NPoweredAxles() > 0 ?
vehicle->inductionMotor.braking.maxForce * 1000 :
0; // chwilowy max ED -> do rozdzialu sil
FED -= std::min(vehicle->inductionMotor.state.maxForce, 0.0) *
1000; // chwilowy max ED -> do rozdzialu sil
state.FfulED = std::min(vehicle->inductionMotor.state.fullForce, 0.0) *
1000; // chwilowy max ED -> do rozdzialu sil
state.FrED -= std::min(vehicle->inductionMotor.state.maxForce, 0.0) *
1000; // chwilowo realizowane ED -> do pneumatyki
Frj += std::max(vehicle->inductionMotor.state.maxForce, 0.0) *
1000;// chwilowo realizowany napęd -> do utrzymującego
state.masa += vehicle->TotalMass();
state.osie += vehicle->NAxles();
}
}
// the most deceleration of the service braking of the consist, given to the driver too
void physics::ConsistBrakeBlending::ComputeDeceleration(Step &state, Vehicle const &Lead)
{
double RapidMult = 1.0;
if ((Lead.brakes.config.availableDelays & bdelay_P + bdelay_R) == bdelay_P + bdelay_R
&& Lead.brakes.delaySetting & bdelay_P)
RapidMult = Lead.brakes.config.rapidRatio;
state.amax = RapidMult * std::min(state.FmaxPN / state.masamax, Lead.brakes.blending.maxDeceleration);
}
// the doors open or closed, and from them, the demand and the speed the shunting mode (the holding brake) of the consist
void physics::ConsistBrakeBlending::UpdateShuntMode(Step &state, Vehicle &Lead)
{
state.doorisopen =
false == Lead.Doors.instances[VehicleSide::left].is_closed
|| false == Lead.Doors.instances[VehicleSide::right].is_closed
|| ( Lead.Doors.permit_needed
&& ( Lead.Doors.instances[ VehicleSide::left ].open_permit
|| Lead.Doors.instances[ VehicleSide::right ].open_permit ) );
state.doorisopen &= !(Lead.brakes.spring.releasesParkingWithDoorOpen && Lead.brakes.spring.isActive);
if (Lead.Vel < 0.5 && (state.eimic < 0 || state.doorisopen || Lead.brakes.distributor->GetEDBCP()))
{
Lead.ShuntMode = true;
}
if (Lead.ShuntMode)
{
Lead.ShuntModeAllow = false == state.doorisopen &&
state.LBR < 0.01;
}
if( Lead.Vel > 1 && false == state.doorisopen )
{
Lead.ShuntMode = false;
Lead.ShuntModeAllow = Lead.brakes.cylinderPressure > 0.2 &&
state.LBR < 0.01;
}
}
// the braking force demanded of the whole consist
void physics::ConsistBrakeBlending::ComputeDemand(Step &state, Vehicle &Lead, double const dt1)
{
state.Fzad = state.amax * state.LBR * state.masa;
if ((Lead.brakes.HandlePosition() == Lead.brakes.driverValve->GetPos(bh_EB)
&& Lead.inductionMotor.braking.cylinderPressureGain < 0.001) ||
Lead.brakes.emergencyValve.flow > 0)
state.Fzad = state.amax * state.masa; //pętla bezpieczeństwa - pełne służbowe
if (Lead.ScndS &&
Lead.Vel > Lead.inductionMotor.braking.fullSpeed && state.FmaxED > 0)
{
state.Fzad = std::min(state.LBR * state.FmaxED, state.FfulED);
}
// the holding brake lets go once power has been demanded for its delay (SBDReleaseDelay, none by default)
auto const holdingbrakereleased { Lead.UpdateStopBrakeRelease( state.eimic, dt1 ) };
if (Lead.ShuntMode && false == holdingbrakereleased || state.doorisopen /*||
(Lead.V() * Lead.DirAbsolute < -0.2)*/)
{
auto const sbd { ( Lead.brakes.spring.isActive && Lead.brakes.spring.releasesParking ? 0.0 : Lead.brakes.holding.deceleration ) };
state.Fzad = std::max( state.Fzad, sbd * state.masa );
}
}
// the log of the braking of the consist opened, once it brakes while moving
void physics::ConsistBrakeBlending::OpenLog(Step const &state, Vehicle const &Lead)
{
if (state.Fzad > 1 && !log.is_open() && false == logUnavailable && Lead.Vel > 1)
{
log.open(
"MEDLOGS/" + Lead.Name + "_" + std::to_string( ++logCount ) + ".csv",
std::ios::in | std::ios::out | std::ios::trunc );
if( false == log.is_open() ) {
// no MEDLOGS folder (the usual case, the game doesn't come with one): not tried again for this vehicle.
// an attempt costs a file system call, and it used to be made on every step of the braking
logUnavailable = true;
return;
}
log << "t\tVel\tMasa\tOsie\tFmaxPN\tFmaxED\tFfulED\tFrED\tFzad\tFzadED\tFzadPN";
for(int k=1;k<=state.np;k++)
{
log << "\tBP" << k;
}
log << "\n";
log.flush();
logInactiveTime = 0;
logTime = 0;
}
}
// the part of the demand the ED brake takes, brought in with a delay, and the rest left to the pneumatic brake
void physics::ConsistBrakeBlending::ComputeElectrodynamicDemand(Step &state, Vehicle &Lead, double const dt1) const
{
state.FzadED = 0.0;
if( state.LBR > Lead.brakes.blending.minEdDemand
&& ( Lead.brakes.config.driverValveType == TBrakeHandle::MHZ_EN57 ?
(Lead.brakes.operatingMode & bom_MED) != 0 :
Lead.EpFuse() ) ) {
state.FzadED = std::min( state.Fzad, state.FmaxED );
}
/*/ELF - wdrazanie ED po powrocie na utrzymanie hamowania - do usuniecia
if (Lead.EIMCtrlType == 2 && Lead.MainCtrlPos() < 2 && Lead.eimic > -0.999)
{
FzadED = std::min(FzadED, lastEdDemand);
} //*/
//opoznienie wdrazania ED
if (state.FzadED > lastEdDemand)
{
if (Lead.brakes.blending.edDelayTimer <= 0) {
Lead.brakes.blending.edDelayTimer += dt1;
if (Lead.brakes.blending.edDelayTimer > 0) {
}
else {
state.FzadED = std::min(state.FzadED, lastEdDemand);
}
}
else
{
state.FzadED = std::min(state.FzadED, lastEdDemand);
Lead.brakes.blending.edDelayTimer = state.FrED > 0 ? -Lead.brakes.blending.edNextDelay : -Lead.brakes.blending.edFirstDelay;
}
}
if (Lead.brakes.HandlePosition() == Lead.brakes.driverValve->GetPos(bh_EB)
&& Lead.inductionMotor.braking.cylinderPressureGain < 0.001)
state.FzadED = 0; //pętla bezpieczeństwa - bez ED
state.FzadPN = state.Fzad - state.FrED * Lead.brakes.blending.edForceFactor;
}
// steps 3-6 of the blending: the ED and the pneumatic braking forces set to the vehicles of the consist
void physics::ConsistBrakeBlending::DistributeForces(Step &state, Vehicle const &Lead, std::vector<Vehicle *> const &Consist, double const dt1)
{
std::vector<bool> PrzekrF( state.np );
state.nPrzekrF = 0;
bool test = true;
std::vector<float> FzED( state.np );
std::vector<float> FzEP( state.np );
std::vector<float> FmaxEP( state.np );
// 3. ustaw pojazdom sile hamowania ED
// - proporcjonalnie do mozliwosci
// 4. ustal potrzebne dohamowanie pneumatyczne
// - od sily zadanej trzeba odjac realizowana przez ED
// 5. w razie potrzeby wlacz hamulec utrzymujacy
// - gdy zahamowany ma ponizej 2 km/h
// 6. ustaw pojazdom sile hamowania ep
// - proporcjonalnie do masy, do liczby osi, rowne cisnienia - jak
// bedzie, tak bedzie dobrze
float Fpoj = 0; // Lead.CabOccupied() < 0
////ALGORYTM 2 - KAZDEMU PO ROWNO, ale nie wiecej niz eped * masa
// 1. najpierw daj kazdemu tyle samo
int i = 0;
for (auto *vehicle : Consist)
{
auto const Nmax = ((vehicle->brakes.config.pressureToForce * vehicle->brakes.config.maxCylinderPressure[0] -
vehicle->brakes.config.cylinders.springForce) *
vehicle->brakes.config.cylinders.leverRatio[0] -
vehicle->brakes.config.cylinders.slackAdjusterForce) *
vehicle->brakes.config.cylinders.count * vehicle->brakes.config.cylinders.riggingEfficiency;
FmaxEP[i] = Nmax *
vehicle->brakes.distributor->GetFC(
Nmax / (vehicle->NAxles() * vehicle->brakes.config.blocksPerAxle),
vehicle->brakes.blending.referenceSpeed) *
1000; // sila hamowania pn
PrzekrF[i] = false;
FzED[i] = state.FmaxED > 0 ? state.FzadED / state.FmaxED : 0;
vehicle->SetAnPos(Lead.ScndS ? state.LBR : FzED[i]);
FzEP[ i ] = static_cast<float>( state.FzadPN * vehicle->NAxles() ) / static_cast<float>( state.osie );
++i;
vehicle->ShuntMode = Lead.ShuntMode;
vehicle->ShuntModeAllow = Lead.ShuntModeAllow;
}
while (test)
{
test = false;
i = 0;
float przek = 0;
for (auto *vehicle : Consist)
{
if (FzEP[i] > 0.01 &&
FzEP[i] > vehicle->TotalMass() * vehicle->inductionMotor.braking.maxEpDeceleration + std::min(vehicle->inductionMotor.state.maxForce, 0.) * 1000 &&
!PrzekrF[i])
{
float przek1 = -std::min(vehicle->inductionMotor.state.maxForce, 0.) * 1000 +
FzEP[i] -
vehicle->TotalMass() *
vehicle->inductionMotor.braking.maxEpDeceleration * 0.999;
PrzekrF[i] = true;
test = true;
state.nPrzekrF++;
przek1 = std::min(przek1, FzEP[i]);
FzEP[i] -= przek1;
if (FzEP[i] < 0)
FzEP[i] = 0;
przek += przek1;
}
++i;
}
i = 0;
przek = przek / (state.np - state.nPrzekrF);
for (auto *vehicle : Consist)
{
if (!PrzekrF[i])
{
FzEP[i] += przek;
}
++i;
}
}
i = 0;
for (auto *vehicle : Consist)
{
float Nmax = ((vehicle->brakes.config.pressureToForce * vehicle->brakes.config.maxCylinderPressure[0] -
vehicle->brakes.config.cylinders.springForce) *
vehicle->brakes.config.cylinders.leverRatio[0] -
vehicle->brakes.config.cylinders.slackAdjusterForce) *
vehicle->brakes.config.cylinders.count * vehicle->brakes.config.cylinders.riggingEfficiency;
if (state.FrED > 0.1)
vehicle->brakes.blending.epCorrectionTimer = 0;
else
vehicle->brakes.blending.epCorrectionTimer += dt1;
bool EPVC = vehicle->brakes.blending.epCorrection && (vehicle->brakes.blending.epCorrectionTime < 0 || vehicle->brakes.blending.epCorrectionTimer < vehicle->brakes.blending.epCorrectionTime);
float VelC = EPVC ? safe_clamp(vehicle->Vel, vehicle->brakes.blending.minSpeed, vehicle->brakes.blending.maxSpeed) : vehicle->brakes.blending.referenceSpeed;//korekcja EP po prędkości
float FmaxPoj = Nmax *
vehicle->brakes.distributor->GetFC(
Nmax / (vehicle->NAxles() * vehicle->brakes.config.blocksPerAxle), VelC) *
1000; // sila hamowania pn
vehicle->brakes.SetIndependentPositionEim(FzEP[i] / FmaxPoj);
if (vehicle->brakes.IndependentPositionEim()>0.009)
if (vehicle->brakes.config.pressureToForce * vehicle->brakes.config.cylinders.leverRatio[0] *
vehicle->brakes.config.maxCylinderPressure[0] != 0)
{
float x = (vehicle->brakes.config.cylinders.slackAdjusterForce / vehicle->brakes.config.cylinders.leverRatio[0] +
vehicle->brakes.config.cylinders.springForce) / (vehicle->brakes.config.pressureToForce *
vehicle->brakes.config.maxCylinderPressure[0]);
vehicle->brakes.SetIndependentPositionEim(x + (1 - x) * vehicle->brakes.IndependentPositionEim());
}
else
vehicle->brakes.SetIndependentPositionEim(vehicle->brakes.IndependentPositionEim());
else
vehicle->brakes.SetIndependentPositionEim(0);
if (vehicle->brakes.config.independentTraxx)
{
vehicle->eim_localbrake = state.eim_lb;
vehicle->brakes.SetIndependentPositionEim(std::max(vehicle->brakes.IndependentPositionEim(), state.eim_lb));
}
++i;
}
}
// the state of the blending kept for the debug screen, and written to the log of the braking while it is open
void physics::ConsistBrakeBlending::Record(Step const &state, Vehicle const &Lead, std::vector<Vehicle *> const &Consist, double const dt1)
{
figures[0] = state.masa*0.001;
figures[1] = state.amax;
figures[2] = state.Fzad*0.001;
figures[3] = state.FmaxPN*0.001;
figures[4] = state.FmaxED*0.001;
figures[5] = state.FrED*0.001;
figures[6] = state.FzadPN*0.001;
figures[7] = state.nPrzekrF;
if (log.is_open())
{
log << logTime << "\t" << Lead.Vel << "\t" << state.masa*0.001 << "\t" << state.osie << "\t" << state.FmaxPN*0.001 << "\t" << state.FmaxED*0.001 << "\t"
<< state.FfulED*0.001 << "\t" << state.FrED*0.001 << "\t" << state.Fzad*0.001 << "\t" << state.FzadED*0.001 << "\t" << state.FzadPN*0.001;
for (auto *vehicle : Consist)
{
log << "\t" << vehicle->brakes.cylinderPressure;
}
log << "\n";
if (floor(logTime + dt1) > floor(logTime))
{
log.flush();
}
logTime += dt1;
if (Lead.Vel < 0.1 || Lead.MainCtrlPowerPos() > 0)
{
logInactiveTime += dt1;
}
else
{
logInactiveTime = 0;
}
if (logInactiveTime > 5)
{
log.flush();
log.close();
}
}
}