18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 03:37:09 +01:00
Files
maszyna/physics/components/drive.cpp
T
Hirek d0387753f8 refactor(physics): TMoverParameters becomes physics::Vehicle, the mover is gone
The physics of a vehicle loses the name it had from the Pascal of 2001. Done by
tools/rename/mover_to_physics.py, committed with it:

  physics/mover.h                  physics/vehicle.h
  TMoverParameters                 physics::Vehicle (in namespace physics)
  TDynamicObject::MoverParameters  physics
  mvOccupied, mvControlled         occupied, controlled (TTrain, TController)
  mvSecond, mvThird                secondUnit, thirdUnit
  mvPantographUnit                 pantographUnit
  mover, Mover, movers             physics, Physics, vehicles (locals, params)
  m_mover, othermover, currentMover, from_movers, renderMoverEventSounds
                                   m_physics, otherPhysics, currentPhysics,
                                   from_vehicles, renderPhysicsEventSounds

The code and the comments are renamed, the string literals are not: no text
the game shows, logs or looks up has changed (none had the names). The movers
of the editor, the tracks a slide moves, keep their name.

By hand: two member functions of TDynamicObject (gather_off_track_terrain,
begin_off_track) had a local mover aliasing their own MoverParameters, which
the rename made physics { *physics }; the alias is gone, they use physics->.
No other function has a renamed local meeting a renamed member (checked over
all the functions of the code before the rename).

For the branches made before it: run the script on the branch (python
tools/rename/mover_to_physics.py, from the root of the repository) before
merging it with this one, and the names don't conflict. It can be run again;
the second time it changes nothing.

The class is the same as it was; breaking it into its parts (braking,
electrics, controls, load) and making its data private are the next stages.

No change in behaviour: 1149/1149 tests, golden traces exact against staging
62f7a29b.
2026-11-28 23:56:46 +01:00

576 lines
19 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.Mains)
{
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.PantographsRequireMains ? vehicle.Power24vIsAvailable || vehicle.Power110vIsAvailable : 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.MainCtrlPos;
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.MainCtrlPos--;
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.Power24vIsAvailable;
}
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.PantographVoltage)};
// 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.PantographsRequireMains || vehicle.NoVoltRelay || vehicle.MainCtrlPowerPos() <= 1};
auto const &source{vehicle.EnginePowerSource};
vehicle.NoVoltRelay = source.SourceType != TPowerSource::CurrentCollector || (voltage >= source.CollectorParameters.MinV && novoltrelaypicksup);
vehicle.OvervoltageRelay = 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)
vehicle.EventFlag |= true == vehicle.Mains && ((false == vehicle.NoVoltRelay && false == vehicle.PantographsRequireMains) || false == vehicle.OvervoltageRelay) &&
vehicle.MainSwitch(false, vehicle.TrainType == train_type::emu ? range_t::unit : range_t::local); // TODO: check whether we need to send this EMU-wide
}
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.LastRelayTime += dt;
// potem ulepszyc! pantogtrafy!
// Ra 2014-03: uwzględnienie kierunku jazdy w napięciu na silnikach, a powinien być zdefiniowany nawrotnik
vehicle.EngineVoltage = vehicle.Mains ? 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.Mains && ((vehicle.TractionSupplyVoltage() < vehicle.TractionSupplyMinVoltage() && false == vehicle.PantographsRequireMains) ||
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.MainCtrlPos;
}
vehicle.CompressorAllow = vehicle.MainCtrlPowerPos() > 0;
return true;
}
bool DecreaseMainController(physics::Vehicle &vehicle, int const speed) const override
{
if (speed == 1)
{
vehicle.MainCtrlPos--;
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.Power24vIsAvailable;
}
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.Mains && 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.Mains)
{
// 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.Power24vIsAvailable;
}
void CheckVoltageRelays(physics::Vehicle &vehicle) const override
{
// TODO: move this to the auto relay check when the electric engine code paths are unified
vehicle.StLinFlag |= vehicle.Mains && false == vehicle.StLinFlag && vehicle.MainCtrlPowerPos() == 1;
vehicle.StLinFlag &= vehicle.MotorConnectorsCheck();
vehicle.StLinFlag &= vehicle.MainCtrlPowerPos() > 0;
}
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;
}