18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 14:07:06 +01:00
Files
maszyna/physics/components/drive.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

589 lines
20 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 drives of the vehicles, one class for each kind of engine
#include "stdafx.h"
#include "physics/components/drive.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include "physics/vehicle.h"
#include "utilities/utilities.h"
// the defaults: a drive with no engine model and no engine of revolutions does nothing in the steps
double TractionDrive::UpdateEnginePower(physics::Vehicle & /*vehicle*/) const
{
return 0.0;
}
void TractionDrive::BeforeTractionForce(physics::Vehicle & /*vehicle*/, double /*dt*/) const
{
// nothing before the force
}
void TractionDrive::UpdateTractionForce(physics::Vehicle & /*vehicle*/, DriveStep const & /*step*/) const
{
// no force
}
void TractionDrive::AfterTractionForce(physics::Vehicle & /*vehicle*/) const
{
// nothing after the force
}
void TractionDrive::UpdateVentilators(physics::Vehicle & /*vehicle*/, double /*dt*/) const
{
// no ventilators
}
double TractionDrive::ShowCurrent(physics::Vehicle const &vehicle, int const ammeter) const
{
return vehicle.ShowCurrentP(ammeter);
}
double TractionDrive::EngineRpmRatio(physics::Vehicle const & /*vehicle*/) const
{
return 1.0;
}
double TractionDrive::EngineIdleRpm(physics::Vehicle const & /*vehicle*/) const
{
return std::numeric_limits<double>::max();
}
double TractionDrive::EngineMaxRpm(physics::Vehicle const & /*vehicle*/) const
{
return std::numeric_limits<double>::max();
}
double TractionDrive::UpdateEngineRevolutions(physics::Vehicle &vehicle, double /*dt*/, double & /*transmission*/, double & /*moment*/) const
{
vehicle.enrot = vehicle.Transmision.Ratio * vehicle.nrot();
return 0.0;
}
bool TractionDrive::IncreaseMainController(physics::Vehicle & /*vehicle*/, int /*speed*/) const
{
return false;
}
bool TractionDrive::DecreaseMainController(physics::Vehicle & /*vehicle*/, int /*speed*/) const
{
return false;
}
bool TractionDrive::MainsPowerAvailable(physics::Vehicle const & /*vehicle*/) const
{
return true;
}
void TractionDrive::CheckVoltageRelays(physics::Vehicle & /*vehicle*/) const
{
// no voltage relays
}
bool TractionDrive::UpdateEngine(physics::Vehicle & /*vehicle*/, double /*dt*/) const
{
return false;
}
void TractionDrive::UpdateEngineVoltage(physics::Vehicle &vehicle, double /*dt*/) const
{
vehicle.EngineVoltage = vehicle.Power > 1.0 ? vehicle.TractionSupplyVoltage() : 0.0;
}
namespace
{
// the radiator fans of a diesel engine follow their demanded revolutions while the engine runs, and run down without it
void updateRadiatorFans(physics::Vehicle &vehicle, double const dt)
{
auto &heat{vehicle.diesel.heat};
if (true == vehicle.lineBreaker.IsClosed())
{
heat.rpmw += static_cast<float>(std::clamp(heat.rpmwz - heat.rpmw, -100.f, 50.f) * dt);
heat.rpmw2 += static_cast<float>(std::clamp(heat.rpmwz2 - heat.rpmw2, -100.f, 50.f) * dt);
}
else
{
heat.rpmw *= static_cast<float>(std::max(0.0, 1.0 - heat.rpmw * dt));
heat.rpmw2 *= static_cast<float>(std::max(0.0, 1.0 - heat.rpmw2 * dt));
}
}
// the line breaker of the drives fed from the line: closed ahead of the pantographs it runs on the low voltage circuits,
// else it needs the voltage of the source
bool linePowerAvailable(physics::Vehicle const &vehicle)
{
return vehicle.lineBreaker.config.pantographsRequireIt ? vehicle.lowVoltage.Is24vAvailable() || vehicle.lowVoltage.Is110vAvailable() : vehicle.EnginePowerSourceVoltage() > 0.5 * vehicle.EnginePowerSource.MaxVoltage;
}
// a notch of the master controller of the drives without steps of their own (no engine, Dumb, diesel-electric,
// induction): one position at a time, the speed control let go when the position leaves the one it holds
bool increaseNotch(physics::Vehicle &vehicle, int const speed)
{
if (speed > 1)
{
return vehicle.IncMainCtrl(1) && vehicle.IncMainCtrl(speed - 1); // a fail will propagate up the recursion chain. should this be || instead?
}
vehicle.SetMainCtrlPos(vehicle.MainCtrlPos() + 1);
if (vehicle.EIMCtrlType == 0 && (vehicle.SpeedCtrlAutoTurnOffFlag & 1) == 1 && vehicle.MainCtrlActualPos != vehicle.MainCtrlPos())
{
vehicle.SpeedCtrlSwitch(false);
vehicle.SpeedCtrlUnit.IsActive = false;
}
return true;
}
bool decreaseNotch(physics::Vehicle &vehicle, int const speed)
{
if (speed == 1)
{
vehicle.SetMainCtrlPos(vehicle.MainCtrlPos() - 1);
if (vehicle.EIMCtrlType == 0 && (vehicle.SpeedCtrlAutoTurnOffFlag & 1) == 1 && vehicle.MainCtrlActualPos != vehicle.MainCtrlPos())
{
vehicle.SpeedCtrlSwitch(false);
vehicle.SpeedCtrlUnit.IsActive = false;
}
return true;
}
if (speed > 1)
{
return vehicle.DecMainCtrl(1) && vehicle.DecMainCtrl(speed - 1);
}
return false;
}
// a vehicle without an engine (a wagon, a control car): its controller still steps, for the vehicles it controls
class NoEngineDrive final : public TractionDrive
{
public:
bool IncreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return decreaseNotch(vehicle, speed);
}
};
// an engine with no model at all (steam, Main): nothing happens in the steps
class UnmodelledDrive final : public TractionDrive
{
};
// an engine with no model: the power follows the share of the controller positions
class DumbDrive final : public TractionDrive
{
public:
double UpdateEnginePower(physics::Vehicle &vehicle) const override
{
auto const posRatio{(vehicle.MainCtrlPos() + vehicle.ScndCtrlPos()) / (vehicle.MainCtrlPosNo + vehicle.ScndCtrlPosNo() + 0.01)};
vehicle.EnginePower = /*1000.0 **/ vehicle.Power * posRatio;
return posRatio;
}
void UpdateTractionForce(physics::Vehicle &vehicle, DriveStep const &step) const override
{
vehicle.DumbTractionForce(step.posRatio);
}
bool MainsPowerAvailable(physics::Vehicle const &vehicle) const override
{
return vehicle.lowVoltage.Is24vAvailable();
}
bool IncreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return decreaseNotch(vehicle, speed);
}
};
// a draisine and the like, the wheels driven by hand
class WheelsDrivenDrive final : public TractionDrive
{
public:
void UpdateTractionForce(physics::Vehicle &vehicle, DriveStep const &step) const override
{
vehicle.WheelsDrivenTractionForce(step.dt);
}
bool IncreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return vehicle.AddPulseForce(speed);
}
};
// series wound direct current motors, started through resistors
class SeriesMotorDrive final : public TractionDrive
{
public:
void UpdateTractionForce(physics::Vehicle &vehicle, DriveStep const &step) const override
{
vehicle.SeriesMotorTractionForce(step.dt);
}
bool IncreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return vehicle.IncMainCtrlSeriesMotor(speed);
}
bool DecreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return vehicle.DecMainCtrlSeriesMotor(speed);
}
void UpdateVentilators(physics::Vehicle &vehicle, double const dt) const override
{
if (vehicle.MainCircuitPowered())
{
vehicle.UpdateResistorBlowers(dt);
} // mains
else
{
vehicle.RventRot *= std::max(0.0, 1.0 - vehicle.RVentSpeed * dt);
}
}
bool MainsPowerAvailable(physics::Vehicle const &vehicle) const override
{
return linePowerAvailable(vehicle);
}
void CheckVoltageRelays(physics::Vehicle &vehicle) const override
{
// update the state of voltage relays
auto const voltage{std::max(vehicle.GetTrainsetHighVoltage(), vehicle.pantographCircuit.SupplyVoltage())};
// where the line breaker holds without voltage (closed ahead of the pantographs) the undervoltage relay opens the
// line contactors instead, see MotorConnectorsCheck(). it picks up again only with the master controller back
// where a start begins, as the line breaker it stands in for wouldn't close on a running position either: at
// zero, or at the first position, which is the first resistance step, where the line contactors close on any
// start, and where the AI waits for them rather than going back to zero
auto const novoltrelaypicksup{false == vehicle.lineBreaker.config.pantographsRequireIt || vehicle.protection.UndervoltageHolds() || vehicle.MainCtrlPowerPos() <= 1};
auto const &source{vehicle.EnginePowerSource};
vehicle.protection.SetVoltageRelays(
source.SourceType != TPowerSource::CurrentCollector || (voltage >= source.CollectorParameters.MinV && novoltrelaypicksup),
source.SourceType != TPowerSource::CurrentCollector || voltage <= source.CollectorParameters.MaxV || false == source.CollectorParameters.OVP);
// wywalanie szybkiego z powodu niewłaściwego napięcia
// (a line breaker closed ahead of the pantographs holds without voltage)
if (true == vehicle.lineBreaker.IsClosed() && ((false == vehicle.protection.UndervoltageHolds() && false == vehicle.lineBreaker.config.pantographsRequireIt) || false == vehicle.protection.OvervoltageHolds()) &&
vehicle.MainSwitch(false, vehicle.TrainType() == train_type::emu ? range_t::unit : range_t::local)) // TODO: check whether we need to send this EMU-wide
{
vehicle.RaiseEventFlag();
}
}
bool UpdateEngine(physics::Vehicle &vehicle, double /*dt*/) const override
{
// automatyczny rozruch
return vehicle.AutoRelayCheck();
}
void UpdateEngineVoltage(physics::Vehicle &vehicle, double const dt) const override
{
vehicle.lineContactors.AdvanceSwitchTimer(dt);
// potem ulepszyc! pantogtrafy!
// Ra 2014-03: uwzględnienie kierunku jazdy w napięciu na silnikach, a powinien być zdefiniowany nawrotnik
vehicle.EngineVoltage = vehicle.lineBreaker.IsClosed() ? vehicle.EnginePowerSourceVoltage() : 0.0;
if (vehicle.CabActive == 0)
{
vehicle.EngineVoltage *= vehicle.DirActive;
}
else
{
vehicle.EngineVoltage *= vehicle.DirAbsolute; // the reverser of the active cab, relative to the couplers
}
}
};
// asynchronous motors fed by inverters
class InductionMotorDrive final : public TractionDrive
{
public:
void UpdateTractionForce(physics::Vehicle &vehicle, DriveStep const &step) const override
{
vehicle.InductionMotorTractionForce(step.dt);
}
bool IncreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return decreaseNotch(vehicle, speed);
}
double ShowCurrent(physics::Vehicle const &vehicle, int const ammeter) const override
{
switch (ammeter)
{ // do asynchronicznych
case 1:
return vehicle.WindingRes() * vehicle.Mm / vehicle.Vadd;
case 2:
return vehicle.inductionMotor.forceShare * vehicle.WindingRes();
default:
return vehicle.ShowCurrentP(ammeter);
}
}
bool MainsPowerAvailable(physics::Vehicle const &vehicle) const override
{
return linePowerAvailable(vehicle);
}
void CheckVoltageRelays(physics::Vehicle &vehicle) const override
{
// TODO: check if we can use instead the code for electricseriesmotor
// nie wchodzić w funkcję bez potrzeby
// (a line breaker closed ahead of the pantographs holds without voltage)
// (the source by the traction selector of a dual-mode vehicle: the pantographs or the genset)
if (vehicle.lineBreaker.IsClosed() && ((vehicle.TractionSupplyVoltage() < vehicle.TractionSupplyMinVoltage() && false == vehicle.lineBreaker.config.pantographsRequireIt) ||
vehicle.TractionSupplyVoltage() > vehicle.EnginePowerSource.CollectorParameters.MaxV + 200))
{
vehicle.MainSwitch(false, vehicle.TrainType() == train_type::emu ? range_t::unit : range_t::local); // TODO: check whether we need to send this EMU-wide
}
}
};
// a diesel engine driving the wheels through a mechanical or hydraulic transmission
class DieselMechanicalDrive final : public TractionDrive
{
public:
double UpdateEnginePower(physics::Vehicle &vehicle) const override
{
vehicle.diesel.power = vehicle.Mm * vehicle.enrot * (2.0 * M_PI / 1000.0);
vehicle.EnginePower = (vehicle.diesel.standingMoment + vehicle.Mm) * vehicle.enrot * (2.0 * M_PI / 1000.0);
if (vehicle.MainCtrlPowerPos() > 1)
{
// dodatkowe opory z powodu sprezarki}
// dmoment -= diesel.standingMoment * ( 0.2 * enrot / diesel.maxRevolutions ); //yB: skąd to w ogóle się bierze?!
}
return 0.0;
}
void UpdateTractionForce(physics::Vehicle &vehicle, DriveStep const &step) const override
{
vehicle.DieselEngineTractionForce(step.dt, step.moment, step.transmission);
}
double UpdateEngineRevolutions(physics::Vehicle &vehicle, double const dt, double &transmission, double &moment) const override
{
if (vehicle.ShuntMode) // dodatkowa przekładnia np. dla 2Ls150
transmission = vehicle.AnPos() * vehicle.Transmision.Ratio * vehicle.motor_param(vehicle.ScndCtrlActualPos).mIsat;
else
transmission = vehicle.Transmision.Ratio * vehicle.motor_param(vehicle.ScndCtrlActualPos).mIsat;
moment = vehicle.dizel_Momentum(vehicle.diesel.fill, transmission * vehicle.nrot() * vehicle.DirActive, dt); // oblicza tez enrot
return 0.0;
}
bool IncreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
if (speed > 1)
{
while (vehicle.MainCtrlPos() < vehicle.MainCtrlPosNo && vehicle.IncMainCtrl(1))
{
// all work is done in the loop header
}
}
else
{
vehicle.SetMainCtrlPos(vehicle.MainCtrlPos() + 1);
}
vehicle.air.compressor.Allow(vehicle.MainCtrlPowerPos() > 0);
return true;
}
bool DecreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
if (speed == 1)
{
vehicle.SetMainCtrlPos(vehicle.MainCtrlPos() - 1);
return true;
}
if (speed > 1)
{
while (vehicle.MainCtrlPos() > 0 || vehicle.RList[vehicle.MainCtrlPos()].Mn > 0)
vehicle.DecMainCtrl(1);
return true;
}
return false;
}
void UpdateVentilators(physics::Vehicle &vehicle, double const dt) const override
{
// NOTE: we update only radiator fans, as vehicles with diesel engine don't have other ventilators
updateRadiatorFans(vehicle, dt);
}
double EngineRpmRatio(physics::Vehicle const &vehicle) const override
{
return std::clamp(std::abs(vehicle.enrot) / vehicle.nmax, 0.0, 1.0);
}
double EngineIdleRpm(physics::Vehicle const &vehicle) const override
{
return vehicle.diesel.minRevolutions * 60;
}
double EngineMaxRpm(physics::Vehicle const &vehicle) const override
{
return vehicle.diesel.maxRevolutions * 60;
}
bool MainsPowerAvailable(physics::Vehicle const &vehicle) const override
{
return vehicle.lowVoltage.Is24vAvailable();
}
bool UpdateEngine(physics::Vehicle &vehicle, double const dt) const override
{
return vehicle.dizel_Update(dt);
}
};
// a diesel engine driving a generator which feeds the traction motors
class DieselElectricDrive final : public TractionDrive
{
public:
double UpdateEnginePower(physics::Vehicle &vehicle) const override
{
vehicle.EnginePower = 0; // the actual calculation is done in two steps later in the method
return 0.0;
}
void BeforeTractionForce(physics::Vehicle &vehicle, double const dt) const override
{
if (vehicle.DEDynamicBrake.source != physics::Vehicle::diesel_electric_dynamic_brake::control::none)
{
vehicle.update_dieselelectric_dynamic_brake(dt);
}
}
void UpdateTractionForce(physics::Vehicle &vehicle, DriveStep const &step) const override
{
vehicle.DieselElectricTractionForce(step.revolutions);
if (vehicle.DEDynamicBrake.level > 0.0)
{
// braking: no power asked for, so the drive above gave no force
vehicle.apply_dieselelectric_dynamic_brake(step.dt);
}
}
double UpdateEngineRevolutions(physics::Vehicle &vehicle, double const dt, double & /*transmission*/, double & /*moment*/) const override
{
double target{0.0};
if (true == vehicle.lineBreaker.IsClosed() && true == vehicle.FuelPump.is_active)
{
target = vehicle.DieselElectricTargetRevolutions();
}
else
{
target = 0.0;
vehicle.diesel.fill = 0.0;
}
if (vehicle.enrot != target)
{
vehicle.enrot = safe_clamp(vehicle.enrot + dt / vehicle.diesel.shaftInertia * (vehicle.enrot < target ? 1.0 : -1.0 * vehicle.diesel.revolutionsDecreaseRate), // NOTE: revolutions typically drop faster than they rise
0.0, std::max(target, vehicle.enrot));
if (std::abs(target - vehicle.enrot) < 0.001)
{
vehicle.enrot = target;
}
}
return target;
}
bool IncreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
return decreaseNotch(vehicle, speed);
}
void AfterTractionForce(physics::Vehicle &vehicle) const override
{
// rough approximation of extra effort to overcome friction etc
vehicle.EnginePower += EngineRpmRatio(vehicle) * 0.15 * vehicle.DElist[vehicle.MainCtrlPosNo].GenPower;
}
void UpdateVentilators(physics::Vehicle &vehicle, double const dt) const override
{
// NOTE: for this type RventRot is the speed of motor blowers; we also update radiator fans while at it
if (true == vehicle.lineBreaker.IsClosed())
{
// TBD, TODO: currently ignores RVentType, fix this?
vehicle.RventRot += std::clamp(vehicle.enrot - vehicle.RventRot, -100.0, 50.0) * dt;
}
else
{
vehicle.RventRot *= std::max(0.0, 1.0 - vehicle.RVentSpeed * dt);
}
updateRadiatorFans(vehicle, dt);
}
double ShowCurrent(physics::Vehicle const &vehicle, int const /*ammeter*/) const override
{
return std::fabs(vehicle.Im);
}
double EngineRpmRatio(physics::Vehicle const &vehicle) const override
{
return std::clamp(60.0 * std::abs(vehicle.enrot) / vehicle.DElist[vehicle.MainCtrlPosNo].RPM, 0.0, 1.0);
}
double EngineIdleRpm(physics::Vehicle const &vehicle) const override
{
return vehicle.DElist[vehicle.MainCtrlNoPowerPos()].RPM;
}
double EngineMaxRpm(physics::Vehicle const &vehicle) const override
{
return vehicle.DElist[vehicle.MainCtrlPosNo].RPM;
}
bool MainsPowerAvailable(physics::Vehicle const &vehicle) const override
{
return vehicle.lowVoltage.Is24vAvailable();
}
void CheckVoltageRelays(physics::Vehicle &vehicle) const override
{
// TODO: move this to the auto relay check when the electric engine code paths are unified
if (vehicle.lineBreaker.IsClosed() && false == vehicle.lineContactors.IsClosed() && vehicle.MainCtrlPowerPos() == 1)
{
vehicle.lineContactors.Close();
}
if (false == vehicle.MotorConnectorsCheck())
{
vehicle.lineContactors.Open();
}
if (false == (vehicle.MainCtrlPowerPos() > 0))
{
vehicle.lineContactors.Open();
}
}
bool UpdateEngine(physics::Vehicle &vehicle, double const dt) const override
{
return vehicle.dizel_Update(dt);
}
};
NoEngineDrive const noEngineDrive{};
UnmodelledDrive const unmodelledDrive{};
DumbDrive const dumbDrive{};
WheelsDrivenDrive const wheelsDrivenDrive{};
SeriesMotorDrive const seriesMotorDrive{};
InductionMotorDrive const inductionMotorDrive{};
DieselMechanicalDrive const dieselMechanicalDrive{};
DieselElectricDrive const dieselElectricDrive{};
} // namespace
TractionDrive const &DriveOf(TEngineType const type)
{
switch (type)
{
case TEngineType::Dumb:
return dumbDrive;
case TEngineType::WheelsDriven:
return wheelsDrivenDrive;
case TEngineType::ElectricSeriesMotor:
return seriesMotorDrive;
case TEngineType::ElectricInductionMotor:
return inductionMotorDrive;
case TEngineType::DieselEngine:
return dieselMechanicalDrive;
case TEngineType::DieselElectric:
return dieselElectricDrive;
case TEngineType::None:
return noEngineDrive;
case TEngineType::SteamEngine:
case TEngineType::Main:
return unmodelledDrive;
}
return unmodelledDrive;
}