18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 13:06:46 +01:00

refactor(physics): the revolutions of the engine and the steps of the master controller in the drives

Two more switches on EngineType go into the classes of the drives:

- TractionDrive::UpdateEngineRevolutions(): the diesel-electric engine
  follows the revolutions it aims for, the diesel engine with a mechanical
  transmission gives its ratio and its moment, the others turn with the
  wheels (UpdateEngineRevolutions() before)
- TractionDrive::IncreaseMainController(), DecreaseMainController(): a notch
  of the master controller as the kind of the engine takes it: the series
  motors through their own steps, the diesel engine running to the end at
  a fast step, the draisine by a push, the others one position at a time
  (IncMainCtrlByEngine(), DecMainCtrlByEngine() before)

NoDrive splits in two: NoEngineDrive for a vehicle without an engine (its
controller steps, for the vehicles it controls) and UnmodelledDrive for the
steam engine and Main, which did nothing in these steps and still don't.

No change in behaviour: 1144/1144 tests, golden traces exact against staging
62f7a29b.
This commit is contained in:
2026-11-28 16:02:59 +01:00
parent ff739d2328
commit 95e7b55495
4 changed files with 185 additions and 154 deletions
+3 -104
View File
@@ -28,6 +28,7 @@ http://mozilla.org/MPL/2.0/.
#include <utility>
#include <vector>
#include "physics/components/internal.h"
#include "physics/components/drive.h"
// *************************************************************************************************
@@ -108,62 +109,7 @@ bool TMoverParameters::IncMainCtrl(int CtrlSpeed)
// a notch up, as the kind of the engine takes it; returns whether the controller moved
bool TMoverParameters::IncMainCtrlByEngine(int const CtrlSpeed)
{
bool OK = false;
switch (EngineType)
{
case TEngineType::None:
case TEngineType::Dumb:
case TEngineType::DieselElectric:
case TEngineType::ElectricInductionMotor:
{
if (CtrlSpeed > 1)
{
OK = IncMainCtrl(1) && IncMainCtrl(CtrlSpeed - 1); // a fail will propagate up the recursion chain. should this be || instead?
break;
}
++MainCtrlPos;
OK = true;
if (EIMCtrlType == 0 && ( SpeedCtrlAutoTurnOffFlag & 1 ) == 1 && MainCtrlActualPos != MainCtrlPos)
{
SpeedCtrlSwitch(false);
SpeedCtrlUnit.IsActive = false;
}
break;
}
case TEngineType::ElectricSeriesMotor:
{
OK = IncMainCtrlSeriesMotor(CtrlSpeed);
break;
}
case TEngineType::DieselEngine:
{
if (CtrlSpeed > 1)
{
while (MainCtrlPos < MainCtrlPosNo && IncMainCtrl(1))
{
// all work is done in the loop header
}
}
else
{
++MainCtrlPos;
}
CompressorAllow = MainCtrlPowerPos() > 0;
OK = true;
break;
}
case TEngineType::WheelsDriven:
{
OK = AddPulseForce(CtrlSpeed);
break;
}
default:
break;
} // switch EngineType of
return OK;
return Drive().IncreaseMainController(*this, CtrlSpeed);
}
// a notch up of the series motors' controller: CtrlSpeed over 1 runs it to the first position without resistors;
@@ -292,54 +238,7 @@ bool TMoverParameters::DecMainCtrl(int CtrlSpeed)
// a notch down, as the kind of the engine takes it; returns whether the controller moved
bool TMoverParameters::DecMainCtrlByEngine(int const CtrlSpeed)
{
bool OK = false;
switch (EngineType)
{
case TEngineType::None:
case TEngineType::Dumb:
case TEngineType::DieselElectric:
case TEngineType::ElectricInductionMotor:
{
if (CtrlSpeed == 1)
{
MainCtrlPos--;
OK = true;
if (EIMCtrlType == 0 && (SpeedCtrlAutoTurnOffFlag & 1) == 1 && MainCtrlActualPos != MainCtrlPos)
{
SpeedCtrlSwitch(false);
SpeedCtrlUnit.IsActive = false;
}
}
else if (CtrlSpeed > 1)
OK = DecMainCtrl(1) && DecMainCtrl(CtrlSpeed - 1); // CtrlSpeed-1);
break;
}
case TEngineType::ElectricSeriesMotor:
{
OK = DecMainCtrlSeriesMotor(CtrlSpeed);
break;
}
case TEngineType::DieselEngine:
{
if (CtrlSpeed == 1)
{
MainCtrlPos--;
OK = true;
}
else if (CtrlSpeed > 1)
{
while (MainCtrlPos > 0 || RList[MainCtrlPos].Mn > 0)
DecMainCtrl(1);
OK = true;
}
break;
}
default:
break;
} // switch EngineType
return OK;
return Drive().DecreaseMainController(*this, CtrlSpeed);
}
// a notch down of the series motors' controller: CtrlSpeed over 1 runs it down to the first position without resistors
+1 -45
View File
@@ -328,51 +328,7 @@ bool TMoverParameters::OilPumpSwitchOff(bool State, range_t const Notify)
// for a diesel engine, the ratio of the transmission and the engine's moment
double TMoverParameters::UpdateEngineRevolutions(double dt, double &dtrans, double &dmoment)
{
double tmp{0.0};
// youBy
switch (EngineType)
{
case TEngineType::DieselElectric:
{
if (true == Mains && true == FuelPump.is_active)
{
tmp = DieselElectricTargetRevolutions();
}
else
{
tmp = 0.0;
diesel.fill = 0.0;
}
if (enrot != tmp)
{
enrot = safe_clamp(enrot + dt / diesel.shaftInertia * (enrot < tmp ? 1.0 : -1.0 * diesel.revolutionsDecreaseRate), // NOTE: revolutions typically drop faster than they rise
0.0, std::max(tmp, enrot));
if (std::abs(tmp - enrot) < 0.001)
{
enrot = tmp;
}
}
break;
}
case TEngineType::DieselEngine:
{
if (ShuntMode) // dodatkowa przekładnia np. dla 2Ls150
dtrans = AnPos * Transmision.Ratio * motor_param(ScndCtrlActualPos).mIsat;
else
dtrans = Transmision.Ratio * motor_param(ScndCtrlActualPos).mIsat;
dmoment = dizel_Momentum(diesel.fill, dtrans * nrot * DirActive, dt); // oblicza tez enrot
break;
}
default:
{
enrot = Transmision.Ratio * nrot;
break;
}
}
return tmp;
return Drive().UpdateEngineRevolutions(*this, dt, dtrans, dmoment);
}
// the revolutions a diesel-electric's engine aims at [1/s], by the controller and the heating; the fill shown by the smoke
+175 -5
View File
@@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/.
#include <limits>
#include "physics/mover.h"
#include "utilities/utilities.h"
// the defaults: a drive with no engine model and no engine of revolutions does nothing in the steps
@@ -65,6 +66,22 @@ double TractionDrive::EngineMaxRpm(TMoverParameters const & /*vehicle*/) const
return std::numeric_limits<double>::max();
}
double TractionDrive::UpdateEngineRevolutions(TMoverParameters &vehicle, double /*dt*/, double & /*transmission*/, double & /*moment*/) const
{
vehicle.enrot = vehicle.Transmision.Ratio * vehicle.nrot;
return 0.0;
}
bool TractionDrive::IncreaseMainController(TMoverParameters & /*vehicle*/, int /*speed*/) const
{
return false;
}
bool TractionDrive::DecreaseMainController(TMoverParameters & /*vehicle*/, int /*speed*/) const
{
return false;
}
namespace
{
@@ -84,8 +101,58 @@ void updateRadiatorFans(TMoverParameters &vehicle, double const dt)
}
}
// a vehicle without an engine (a wagon), or of an engine with no model (steam, Main)
class NoDrive final : public TractionDrive
// 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(TMoverParameters &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(TMoverParameters &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(TMoverParameters &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(TMoverParameters &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
{
};
@@ -103,6 +170,14 @@ public:
{
vehicle.DumbTractionForce(step.posRatio);
}
bool IncreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return decreaseNotch(vehicle, speed);
}
};
// a draisine and the like, the wheels driven by hand
@@ -113,6 +188,10 @@ public:
{
vehicle.WheelsDrivenTractionForce(step.dt);
}
bool IncreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return vehicle.AddPulseForce(speed);
}
};
// series wound direct current motors, started through resistors
@@ -123,6 +202,14 @@ public:
{
vehicle.SeriesMotorTractionForce(step.dt);
}
bool IncreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return vehicle.IncMainCtrlSeriesMotor(speed);
}
bool DecreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return vehicle.DecMainCtrlSeriesMotor(speed);
}
void UpdateVentilators(TMoverParameters &vehicle, double const dt) const override
{
if (vehicle.MainCircuitPowered())
@@ -144,6 +231,14 @@ public:
{
vehicle.InductionMotorTractionForce(step.dt);
}
bool IncreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return decreaseNotch(vehicle, speed);
}
double ShowCurrent(TMoverParameters const &vehicle, int const ammeter) const override
{
switch (ammeter)
@@ -177,6 +272,47 @@ public:
{
vehicle.DieselEngineTractionForce(step.dt, step.moment, step.transmission);
}
double UpdateEngineRevolutions(TMoverParameters &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(TMoverParameters &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(TMoverParameters &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(TMoverParameters &vehicle, double const dt) const override
{
// NOTE: we update only radiator fans, as vehicles with diesel engine don't have other ventilators
@@ -221,6 +357,38 @@ public:
vehicle.apply_dieselelectric_dynamic_brake(step.dt);
}
}
double UpdateEngineRevolutions(TMoverParameters &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(TMoverParameters &vehicle, int const speed) const override
{
return increaseNotch(vehicle, speed);
}
bool DecreaseMainController(TMoverParameters &vehicle, int const speed) const override
{
return decreaseNotch(vehicle, speed);
}
void AfterTractionForce(TMoverParameters &vehicle) const override
{
// rough approximation of extra effort to overcome friction etc
@@ -258,7 +426,8 @@ public:
}
};
NoDrive const noDrive{};
NoEngineDrive const noEngineDrive{};
UnmodelledDrive const unmodelledDrive{};
DumbDrive const dumbDrive{};
WheelsDrivenDrive const wheelsDrivenDrive{};
SeriesMotorDrive const seriesMotorDrive{};
@@ -285,9 +454,10 @@ TractionDrive const &DriveOf(TEngineType const type)
case TEngineType::DieselElectric:
return dieselElectricDrive;
case TEngineType::None:
return noEngineDrive;
case TEngineType::SteamEngine:
case TEngineType::Main:
return noDrive;
return unmodelledDrive;
}
return noDrive;
return unmodelledDrive;
}
+6
View File
@@ -50,6 +50,12 @@ public:
// the revolutions of the engine idling and at most [rpm]; the largest double with no engine of revolutions
virtual double EngineIdleRpm(TMoverParameters const &vehicle) const;
virtual double EngineMaxRpm(TMoverParameters const &vehicle) const;
// the revolutions of the engine in the step (enrot); returns the revolutions a diesel-electric engine aims for, 0 for
// the others. a diesel engine with a mechanical transmission gives its ratio and its moment through the references
virtual double UpdateEngineRevolutions(TMoverParameters &vehicle, double dt, double &transmission, double &moment) const;
// a notch up and down of the master controller, as the kind of the engine takes it; return whether it moved
virtual bool IncreaseMainController(TMoverParameters &vehicle, int speed) const;
virtual bool DecreaseMainController(TMoverParameters &vehicle, int speed) const;
};
// the drive of the given kind of engine