18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 21:15:20 +01:00
Files
maszyna/physics/components/airsupply.h
T
Hirek 6b9558f588 refactor(physics): the faults of the equipment are a named set the physics sets
The faults the scenery or the network sets on a vehicle (TDynamicObject::
Damage() with the number of a message, held as EngDmgFlag and tested as its
bits 1, 4) are EquipmentFault and EquipmentFaultSet, and what each does moves
out of TDynamicObject into physics::Vehicle::SetEquipmentFaults(). The number of
a message comes in through FromBits() and goes back out through Bits(), as it
was.

The faults of the control and of the camshaft still hold the start and the
steps of the controller by delays moved out of reach by 100000001 and back,
the very same arithmetic, now named and in one place. Tests of it are added
(test_equipment_faults.cpp), as nothing reached it before.

The setters of the parts that took a parameter named as one of their queries
(Pressure, Voltage) take Value instead.

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

327 lines
12 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 supply of compressed air of a vehicle: the compressor and the main reservoir, held by physics::Vehicle as air; and
// the pipes along the train, held by it as brakePipe and mainPipe. as the parts of the main circuit (maincircuit.h): the
// definition in `config`, the state behind queries and operations
#pragma once
#include <algorithm>
#include <array>
#include <memory>
#include "physics/vehicletypes.h"
class TReservoir;
namespace physics
{
// a pipe along the train, coupled by the hoses between the vehicles: the brake pipe, the main reservoir pipe
class TrainPipe
{
public:
std::shared_ptr<TReservoir> reservoir;
// the pressure in the pipe [bar], as it was last taken from its reservoir
double const &Pressure() const { return pressure; }
// the pressure taken from the reservoir of the pipe, after the flows of a step
void SetPressure(double const Value) { pressure = Value; }
// the pipe stepped, its exchange of air through the hoses is yet to come (UpdatePipeCouplers())
bool CouplersPending() const { return couplersPending; }
void SetCouplersPending(bool const State) { couplersPending = State; }
private:
double pressure{0.0};
bool couplersPending{false};
};
// where the compressor gets its power from (CompressorPower of the .fiz), in the order of the numbers it had before
enum class CompressorSupply
{
mainCircuit, // Main
converterManual, // the default: from the converter, switched by hand
converter, // Converter: from the converter, always
engine, // Engine: driven by the diesel engine
frontUnitConverter, // Coupler1: from the converter of the unit in front (a motor car of an EMU)
rearUnitConverter, // Coupler2: from the converter of the unit behind
};
// what the compressor needs to know in the step
struct CompressorInputs
{
bool converterAllowed{false}; // the converter of the vehicle may run
bool mainCircuitPowered{false}; // the main circuit has its power
bool lowVoltage110v{false}; // the 110 V circuit has its power
bool coupledGovernorLock{false}; // a vehicle coupled permanently has its compressor stopped by the governor
bool switchingDelayPassed{false}; // the vehicle may switch something on again (LastSwitchingTime > CtrlDelay)
double engineRpmRatio{0.0}; // the revolutions of the engine against the most at its position, for a compressor it drives
};
// what the step of the compressor did, for the vehicle to follow it up
struct CompressorStep
{
bool switched{false}; // it started or stopped
bool vented{false}; // the reservoir let air out over the top pressure
};
// the compressor of the main reservoir
class AirCompressor
{
public:
struct Config
{
// the definition (the Brake section of the .fiz). where the pressures follow the occupied cab (cabDependent) the
// compressor sets minPressure and maxPressure to those of the cab in each step
double minPressure{0.0}; // MinCP: it starts below this pressure of the main reservoir
double maxPressure{0.0}; // MaxCP: it stops above this pressure
double minPressureCabA{0.0}; // the pressures of each cab, where they differ (MinCP_B, MaxCP_B given)
double maxPressureCabA{0.0};
double minPressureCabB{0.0}; // MinCP_B
double maxPressureCabB{0.0}; // MaxCP_B
bool cabDependent{false}; // the pressures follow the occupied cab
double speed{0.0}; // CompressorSpeed: how fast it fills the main reservoir
CompressorSupply supply{CompressorSupply::converterManual}; // CompressorPower
double startDelay{0.0}; // CompressorStartDelay: how long it waits after getting its power before it can start, after the engine starts say [s]
bool tankValve{false}; // CompressorTankValve: the pressure over the top is let out of its tank at once
start_t start{start_t::manual}; // CompressorStart: whether it is started by hand, or another way
// the programmer of the compressors of a unit (the CompressorList section), indexed by the cl_* rows
std::array<std::array<int, 9>, 4> list{};
int listPositions{0}; // Size: the positions of its switch
int listDefaultPosition{1}; // Default
bool listWraps{false}; // Wrap
};
Config config;
// a compressor of no output doesn't count
bool IsFitted() const { return config.speed != 0.0; }
bool IsRunning() const { return running; }
void Stop() { running = false; }
// it may start, as its switch (or what drives it) has it
bool IsAllowed() const { return allow; }
void Allow(bool const State) { allow = State; }
// the switch of the vehicle itself; most have none, so it allows
bool LocalSwitchOn() const { return allowLocal; }
void SetLocalSwitch(bool const State) { allowLocal = State; }
// the governor switched it off at the cut-out pressure
bool IsGovernorLocked() const { return governorLock; }
// the position of the switch of the programmer
int ListPosition() const { return listPosition; }
void SetListPosition(int const Position) { listPosition = std::min(std::max(Position, 0), config.listPositions); }
// the programmer at its default position, as the vehicle is placed
void ResetListPosition()
{
if (config.listPositions > 0)
{
listPosition = config.listDefaultPosition;
}
}
// the pressures of the occupied cab, where they follow it
void SelectCab(int const Cab)
{
if (false == config.cabDependent)
{
return;
}
if (Cab > 0)
{
config.minPressure = config.minPressureCabA;
config.maxPressure = config.maxPressureCabA;
}
if (Cab < 0)
{
config.minPressure = config.minPressureCabB;
config.maxPressure = config.maxPressureCabB;
}
}
// the fault the scenery can set (the damage events): its pressures moved out of reach, so it never starts; and back
void SetFault(bool const State)
{
constexpr double faultoffset{100000001};
if (State)
{
if (config.minPressure > 0)
config.minPressure -= faultoffset;
if (config.maxPressure > 0)
config.maxPressure -= faultoffset;
}
else
{
if (config.minPressure < 0)
config.minPressure += faultoffset;
if (config.maxPressure < 0)
config.maxPressure += faultoffset;
}
}
// the step of the compressor, which works the reservoir of the given volume of compressed air at the given pressure
CompressorStep Update(CompressorInputs const &Inputs, double &CompressedVolume, double const ReservoirPressure, double const dt)
{
CompressorStep step;
// assorted operational logic
auto const maxpressure{config.list[cl_MaxFactor][listPosition] * config.maxPressure};
auto const minpressure{config.list[cl_MinFactor][listPosition] * config.minPressure};
auto const speed{config.list[cl_SpeedFactor][listPosition] * config.speed};
// checking the impact on the compressor allowance
if (auto const allowfactor{config.list[cl_Allow][listPosition]}; allowfactor > 0.5)
{
allow = allowfactor > 1.5;
}
switch (config.supply)
{
case CompressorSupply::converter:
allow = Inputs.converterAllowed;
break;
case CompressorSupply::engine:
// HACK: make sure compressor coupled with diesel engine is always ready for work
config.start = start_t::automatic;
break;
default:
break;
}
// the main circuit, also for a compressor driven by the engine, otherwise the low voltage
auto const power{config.supply == CompressorSupply::mainCircuit || config.supply == CompressorSupply::engine ? Inputs.mainCircuitPowered : Inputs.lowVoltage110v};
powerTime = power ? powerTime + dt : 0.0;
auto const ready{power && powerTime >= config.startDelay};
// TBD: split CompressorAllow into separate enable/disable flags, inherit compressor from basic_device
auto const enable{allowLocal && (config.start == start_t::automatic || allow)};
auto const pressuretoolow{ReservoirPressure < minpressure};
auto const pressuretoohigh{ReservoirPressure > maxpressure};
// TBD, TODO: break the lock with no low voltage power?
auto const governorlockpresent{maxpressure - minpressure > 0.0001};
governorLock = governorlockpresent && false == pressuretoolow // unlock if pressure drops below minimal threshold
&& (pressuretoohigh || governorLock); // lock if pressure goes above maximum threshold
// for these multi-unit engines compressors turn off whenever any of them was affected by the governor
// NOTE: this is crude implementation, limited only to adjacent vehicles
// TODO: re-implement when a more elegant/flexible system is in place
auto const locked{governorLock || Inputs.coupledGovernorLock};
auto const wasrunning{running};
running = ready && enable && (false == locked || config.supply == CompressorSupply::engine) && (running || (enable && Inputs.switchingDelayPassed));
step.switched = running != wasrunning;
if (false == running)
{
return step;
}
// working compressor adds air to the air reservoir
if (config.supply == CompressorSupply::engine)
{
// the compressor is coupled with the diesel engine, engine revolutions affect the output
CompressedVolume += speed * (2.0 * maxpressure - ReservoirPressure) / maxpressure * Inputs.engineRpmRatio * dt * (governorLock ? 0.0 : 1.0); // with the lock active air is vented out
}
else
{
// the compressor is a stand-alone device, working at steady pace
CompressedVolume += speed * (2.0 * maxpressure - ReservoirPressure) / maxpressure * dt;
}
if (pressuretoohigh && (false == governorlockpresent || config.supply == CompressorSupply::engine))
{
// vent some air out if there's no governor lock to stop the compressor from exceeding acceptable pressure level
step.vented = true;
auto ventedfraction{0.999}; // HACK: drop a tiny bit so the sound doesn't trigger repeatedly
if (false == governorlockpresent)
{
ventedfraction = 0.80; // arbitrary amount
}
else if (config.tankValve)
{
ventedfraction = minpressure / maxpressure; // drop to mincompressor level
}
CompressedVolume *= ventedfraction;
if (false == governorlockpresent || config.tankValve)
{
governorLock = false;
}
}
return step;
}
private:
bool running{false};
bool allow{false};
bool allowLocal{true};
bool governorLock{false};
double powerTime{0.0}; // how long it has had its power [s]
int listPosition{0};
};
// the supply of compressed air
class AirSupply
{
public:
struct Config
{
double mainReservoirVolume{0.0}; // Vv: the volume of the main reservoir
double leakRate{0.01}; // AirLeakRate: the base rate of the air leaking from the components of the brakes (0.001 = 1 l/s)
};
Config config;
AirCompressor compressor;
// the pressure in the main reservoir [bar], as the last step found it
double const &MainReservoirPressure() const { return mainReservoirPressure; }
// the pressure taken from the air in the reservoir; an empty one of no volume has none, and stops the compressor
void UpdatePressure()
{
if (config.mainReservoirVolume > 0)
{
mainReservoirPressure = compressedVolume / config.mainReservoirVolume;
}
else
{
mainReservoirPressure = 0;
compressor.Stop();
}
}
// the compressed air in the supply
double CompressedVolume() const { return compressedVolume; }
// air let into the reservoir, or out of it with a negative volume
void AddAir(double const Volume) { compressedVolume += Volume; }
// the reservoir filled with the given volume of compressed air, as the vehicle is placed
void Fill(double const Volume) { compressedVolume = Volume; }
// no air below nothing
void ClampEmpty()
{
if (compressedVolume < 0.0)
{
compressedVolume = 0.0;
}
}
// the step of the reservoir and its compressor: the leaks, then the compressor, which fills it
CompressorStep Update(CompressorInputs const &Inputs, double const dt)
{
compressedVolume = std::max(0.0, compressedVolume - dt * config.leakRate * 0.1); // nieszczelności: 0.001=1l/s
mainReservoirPressure = compressedVolume / config.mainReservoirVolume;
return compressor.Update(Inputs, compressedVolume, mainReservoirPressure, dt);
}
// for the tests: the pressure in the reservoir as given, whatever its air
void SetPressureForTest(double const Value) { mainReservoirPressure = Value; }
private:
double mainReservoirPressure{0.0};
double compressedVolume{0.0};
};
} // namespace physics