18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 08:17:09 +01:00

refactor(physics): the drives of the vehicles are classes, one for each kind of engine

What a vehicle does in the step of its traction depended on a switch on
EngineType in each place: the traction force, the ventilators, the power of
the engine, the ammeters, the revolutions of the engine. Each kind of drive
is now a class of its own in physics/mover/drive.h/.cpp, behind the
interface TractionDrive:

  NoDrive                no engine, or one with no model (steam, Main)
  DumbDrive              an engine with no model (Dumb)
  WheelsDrivenDrive      the wheels driven by hand
  SeriesMotorDrive       series wound DC motors
  InductionMotorDrive    asynchronous motors and inverters
  DieselMechanicalDrive  a diesel engine and a mechanical or hydraulic gear
  DieselElectricDrive    a diesel engine, a generator and traction motors

TMoverParameters::Drive() gives the one of the vehicle's EngineType.
TractionForce(), UpdateVentilators(), UpdateEnginePower(), ShowCurrent(),
EngineRPMRatio(), EngineIdleRPM() and EngineMaxRPM() call it in the same
order the switches went. The drives hold no state yet: the state of each
drive moves into its class next.

No change in behaviour: 1144/1144 tests, golden traces exact against staging
62f7a29b.
This commit is contained in:
2026-11-27 21:36:53 +01:00
parent cf73ee05a6
commit c07eb76e51
6 changed files with 374 additions and 175 deletions
+1
View File
@@ -746,6 +746,7 @@ set(CORE_SOURCES
"physics/brakes.cpp"
"physics/vehiclecommand.cpp"
"physics/mover/mover.cpp"
"physics/mover/drive.cpp"
"physics/mover/cab.cpp"
"physics/mover/collisions.cpp"
"physics/mover/commands.cpp"
+4
View File
@@ -23,6 +23,8 @@ http://mozilla.org/MPL/2.0/.
#include "vehicle/odometerreading.h"
#include "physics/vehicletypes.h"
class TractionDrive;
class cParser;
/*
MaSzyna EU07 locomotive simulator
@@ -2246,6 +2248,8 @@ class TMoverParameters
void FuseOff(void); // wylaczenie nadmiarowego
bool UniversalResetButton(int const Button, range_t const Notify = range_t::consist);
bool RelayReset(int const Relays, range_t const Notify = range_t::consist); // resets specified relays
// the drive of the kind of engine of the vehicle (physics/mover/drive.h)
TractionDrive const &Drive() const;
double ShowCurrent(int AmpN) const; // pokazuje bezwgl. wartosc pradu na wybranym amperomierzu
double ShowCurrentP(int AmpN) const; // pokazuje bezwgl. wartosc pradu w wybranym pojezdzie //Q 20160722
+4 -13
View File
@@ -28,31 +28,22 @@ http://mozilla.org/MPL/2.0/.
#include <utility>
#include <vector>
#include "physics/mover/internal.h"
#include "physics/mover/drive.h"
double TMoverParameters::EngineRPMRatio() const
{
return std::clamp(EngineType == TEngineType::DieselElectric ? 60.0 * std::abs(enrot) / DElist[MainCtrlPosNo].RPM :
EngineType == TEngineType::DieselEngine ? std::abs(enrot) / nmax :
1.0, // shouldn't ever get here but, eh
0.0, 1.0);
return Drive().EngineRpmRatio(*this);
}
double TMoverParameters::EngineIdleRPM() const
{
return EngineType == TEngineType::DieselEngine ? dizel_nmin * 60 :
EngineType == TEngineType::DieselElectric ? DElist[MainCtrlNoPowerPos()].RPM :
std::numeric_limits<double>::max(); // shouldn't ever get here but, eh
return Drive().EngineIdleRpm(*this);
}
double TMoverParameters::EngineMaxRPM() const
{
return EngineType == TEngineType::DieselEngine ? dizel_nmax * 60 :
EngineType == TEngineType::DieselElectric ? DElist[MainCtrlPosNo].RPM :
std::numeric_limits<double>::max(); // shouldn't ever get here but, eh
return Drive().EngineMaxRpm(*this);
}
// water pump status check
+293
View File
@@ -0,0 +1,293 @@
/*
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/mover/drive.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include "physics/mover.h"
// the defaults: a drive with no engine model and no engine of revolutions does nothing in the steps
double TractionDrive::UpdateEnginePower(TMoverParameters & /*vehicle*/) const
{
return 0.0;
}
void TractionDrive::BeforeTractionForce(TMoverParameters & /*vehicle*/, double /*dt*/) const
{
// nothing before the force
}
void TractionDrive::UpdateTractionForce(TMoverParameters & /*vehicle*/, DriveStep const & /*step*/) const
{
// no force
}
void TractionDrive::AfterTractionForce(TMoverParameters & /*vehicle*/) const
{
// nothing after the force
}
void TractionDrive::UpdateVentilators(TMoverParameters & /*vehicle*/, double /*dt*/) const
{
// no ventilators
}
double TractionDrive::ShowCurrent(TMoverParameters const &vehicle, int const ammeter) const
{
return vehicle.ShowCurrentP(ammeter);
}
double TractionDrive::EngineRpmRatio(TMoverParameters const & /*vehicle*/) const
{
return 1.0;
}
double TractionDrive::EngineIdleRpm(TMoverParameters const & /*vehicle*/) const
{
return std::numeric_limits<double>::max();
}
double TractionDrive::EngineMaxRpm(TMoverParameters const & /*vehicle*/) const
{
return std::numeric_limits<double>::max();
}
namespace
{
// the radiator fans of a diesel engine follow their demanded revolutions while the engine runs, and run down without it
void updateRadiatorFans(TMoverParameters &vehicle, double const dt)
{
auto &heat{vehicle.dizel_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));
}
}
// a vehicle without an engine (a wagon), or of an engine with no model (steam, Main)
class NoDrive 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(TMoverParameters &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(TMoverParameters &vehicle, DriveStep const &step) const override
{
vehicle.DumbTractionForce(step.posRatio);
}
};
// a draisine and the like, the wheels driven by hand
class WheelsDrivenDrive final : public TractionDrive
{
public:
void UpdateTractionForce(TMoverParameters &vehicle, DriveStep const &step) const override
{
vehicle.WheelsDrivenTractionForce(step.dt);
}
};
// series wound direct current motors, started through resistors
class SeriesMotorDrive final : public TractionDrive
{
public:
void UpdateTractionForce(TMoverParameters &vehicle, DriveStep const &step) const override
{
vehicle.SeriesMotorTractionForce(step.dt);
}
void UpdateVentilators(TMoverParameters &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);
}
}
};
// asynchronous motors fed by inverters
class InductionMotorDrive final : public TractionDrive
{
public:
void UpdateTractionForce(TMoverParameters &vehicle, DriveStep const &step) const override
{
vehicle.InductionMotorTractionForce(step.dt);
}
double ShowCurrent(TMoverParameters const &vehicle, int const ammeter) const override
{
switch (ammeter)
{ // do asynchronicznych
case 1:
return vehicle.WindingRes * vehicle.Mm / vehicle.Vadd;
case 2:
return vehicle.eimv_pr * vehicle.WindingRes;
default:
return vehicle.ShowCurrentP(ammeter);
}
}
};
// a diesel engine driving the wheels through a mechanical or hydraulic transmission
class DieselMechanicalDrive final : public TractionDrive
{
public:
double UpdateEnginePower(TMoverParameters &vehicle) const override
{
vehicle.dizel_Power = vehicle.Mm * vehicle.enrot * (2.0 * M_PI / 1000.0);
vehicle.EnginePower = (vehicle.dizel_Mstand + vehicle.Mm) * vehicle.enrot * (2.0 * M_PI / 1000.0);
if (vehicle.MainCtrlPowerPos() > 1)
{
// dodatkowe opory z powodu sprezarki}
// dmoment -= dizel_Mstand * ( 0.2 * enrot / dizel_nmax ); //yB: skąd to w ogóle się bierze?!
}
return 0.0;
}
void UpdateTractionForce(TMoverParameters &vehicle, DriveStep const &step) const override
{
vehicle.DieselEngineTractionForce(step.dt, step.moment, step.transmission);
}
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
updateRadiatorFans(vehicle, dt);
}
double EngineRpmRatio(TMoverParameters const &vehicle) const override
{
return std::clamp(std::abs(vehicle.enrot) / vehicle.nmax, 0.0, 1.0);
}
double EngineIdleRpm(TMoverParameters const &vehicle) const override
{
return vehicle.dizel_nmin * 60;
}
double EngineMaxRpm(TMoverParameters const &vehicle) const override
{
return vehicle.dizel_nmax * 60;
}
};
// a diesel engine driving a generator which feeds the traction motors
class DieselElectricDrive final : public TractionDrive
{
public:
double UpdateEnginePower(TMoverParameters &vehicle) const override
{
vehicle.EnginePower = 0; // the actual calculation is done in two steps later in the method
return 0.0;
}
void BeforeTractionForce(TMoverParameters &vehicle, double const dt) const override
{
if (vehicle.DEDynamicBrake.source != TMoverParameters::diesel_electric_dynamic_brake::control::none)
{
vehicle.update_dieselelectric_dynamic_brake(dt);
}
}
void UpdateTractionForce(TMoverParameters &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);
}
}
void AfterTractionForce(TMoverParameters &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(TMoverParameters &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(TMoverParameters const &vehicle, int const /*ammeter*/) const override
{
return std::fabs(vehicle.Im);
}
double EngineRpmRatio(TMoverParameters const &vehicle) const override
{
return std::clamp(60.0 * std::abs(vehicle.enrot) / vehicle.DElist[vehicle.MainCtrlPosNo].RPM, 0.0, 1.0);
}
double EngineIdleRpm(TMoverParameters const &vehicle) const override
{
return vehicle.DElist[vehicle.MainCtrlNoPowerPos()].RPM;
}
double EngineMaxRpm(TMoverParameters const &vehicle) const override
{
return vehicle.DElist[vehicle.MainCtrlPosNo].RPM;
}
};
NoDrive const noDrive{};
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:
case TEngineType::SteamEngine:
case TEngineType::Main:
return noDrive;
}
return noDrive;
}
+56
View File
@@ -0,0 +1,56 @@
/*
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 drive of a vehicle, by the kind of its engine (EngineType, the Engine section of the .fiz): what each kind of
// drive does in the step of the vehicle, in place of a switch on the engine type in each of those places. the drives
// hold no state of their own yet (it is in TMoverParameters): there is one of each kind, shared by the vehicles
#pragma once
#include "physics/vehicletypes.h"
class TMoverParameters;
// what the step of the traction gives the drive
struct DriveStep
{
double dt{0.0}; // the length of the step [s]
double posRatio{0.0}; // the share of the controller positions, of the drive of no engine model (Dumb)
double revolutions{0.0}; // the revolutions of the engine
double moment{0.0}; // the moment of the diesel engine
double transmission{0.0}; // the ratio of the transmission of the diesel engine
};
class TractionDrive
{
public:
virtual ~TractionDrive() = default;
// the power of the engine for the controller positions; returns the share of the positions (Dumb), 0 for the others
virtual double UpdateEnginePower(TMoverParameters &vehicle) const;
// before the traction force, whether the reverser is set or not
virtual void BeforeTractionForce(TMoverParameters &vehicle, double dt) const;
// the traction force of the step (Ft), the reverser set
virtual void UpdateTractionForce(TMoverParameters &vehicle, DriveStep const &step) const;
// after the traction force, whether the reverser is set or not
virtual void AfterTractionForce(TMoverParameters &vehicle) const;
// the blowers of the starting resistors and the fans of the radiators
virtual void UpdateVentilators(TMoverParameters &vehicle, double dt) const;
// the current the given ammeter of the cab shows
virtual double ShowCurrent(TMoverParameters const &vehicle, int ammeter) const;
// the revolutions of the engine against the most it gives at the present controller position, 0..1; 1 with no
// engine of revolutions
virtual double EngineRpmRatio(TMoverParameters const &vehicle) const;
// 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 drive of the given kind of engine
TractionDrive const &DriveOf(TEngineType type);
+16 -162
View File
@@ -28,6 +28,7 @@ http://mozilla.org/MPL/2.0/.
#include <utility>
#include <vector>
#include "physics/mover/internal.h"
#include "physics/mover/drive.h"
double TableInterpolation(std::map<double, double> &Map, double Parameter)
@@ -390,25 +391,7 @@ bool TMoverParameters::MotorBlowerPowerIsAvailable(cooling_fan const &Blower) co
double TMoverParameters::ShowCurrent(int AmpN) const
{ // Odczyt poboru prądu na podanym amperomierzu
switch (EngineType)
{
case TEngineType::ElectricInductionMotor:
switch (AmpN)
{ // do asynchronicznych
case 1:
return WindingRes * Mm / Vadd;
case 2:
return eimv_pr * WindingRes;
default:
return ShowCurrentP(AmpN); // T_MoverParameters::
}
break;
case TEngineType::DieselElectric:
return fabs(Im);
break;
default:
return ShowCurrentP(AmpN); // T_MoverParameters::
}
return Drive().ShowCurrent(*this, AmpN);
};
bool TMoverParameters::MotorBlowersSwitch(bool State, end const Side, range_t const Notify)
@@ -585,7 +568,6 @@ bool TMoverParameters::DynamicBrakeSwitch(bool Switch)
// *************************************************************************************************
double TMoverParameters::TractionForce(double dt)
{
using enum TEngineType;
Ft = 0;
double dtrans{0.0};
double dmoment{0.0};
@@ -602,65 +584,17 @@ double TMoverParameters::TractionForce(double dt)
// hunter-091012: przeniesione z if DirActive<>0 (zeby po zejsciu z kierunku dalej spadala predkosc wentylatorow)
// wentylatory rozruchowe
// TBD, TODO: move this to update, it doesn't exactly have much to do with traction
UpdateVentilators(dt);
auto const PosRatio{UpdateEnginePower()};
auto const &drive{Drive()};
drive.UpdateVentilators(*this, dt);
auto const PosRatio{drive.UpdateEnginePower(*this)};
VoltageRelaysCheck();
if (EngineType == DieselElectric && DEDynamicBrake.source != diesel_electric_dynamic_brake::control::none)
{
update_dieselelectric_dynamic_brake(dt);
}
drive.BeforeTractionForce(*this, dt);
if (DirActive != 0)
switch (EngineType)
{
case Dumb:
{
DumbTractionForce(PosRatio);
break;
}
case WheelsDriven:
{
WheelsDrivenTractionForce(dt);
break;
}
case ElectricSeriesMotor:
{
SeriesMotorTractionForce(dt);
break;
}
case DieselEngine:
{
DieselEngineTractionForce(dt, dmoment, dtrans);
break;
}
case DieselElectric: // youBy
{
DieselElectricTractionForce(revolutions);
if (DEDynamicBrake.level > 0.0)
{
// braking: no power asked for, so the drive above gave no force
apply_dieselelectric_dynamic_brake(dt);
}
break;
}
case ElectricInductionMotor:
{
InductionMotorTractionForce(dt);
break;
}
case None:
default:
{
break;
}
} // case EngineType
if (EngineType == DieselElectric)
{
// rough approximation of extra effort to overcome friction etc
EnginePower += EngineRPMRatio() * 0.15 * DElist[MainCtrlPosNo].GenPower;
drive.UpdateTractionForce(*this, DriveStep{dt, PosRatio, revolutions, dmoment, dtrans});
}
drive.AfterTractionForce(*this);
return Ft;
}
@@ -668,62 +602,7 @@ double TMoverParameters::TractionForce(double dt)
// the starting resistors' blowers, and the diesels' radiator fans
void TMoverParameters::UpdateVentilators(double dt)
{
switch (EngineType)
{
case TEngineType::ElectricSeriesMotor:
{
if (MainCircuitPowered())
{
UpdateResistorBlowers(dt);
} // mains
else
{
RventRot *= std::max(0.0, 1.0 - RVentSpeed * dt);
}
break;
}
case TEngineType::DieselElectric:
{
// NOTE: for this type RventRot is the speed of motor blowers; we also update radiator fans while at it
if (true == Mains)
{
// TBD, TODO: currently ignores RVentType, fix this?
RventRot += std::clamp(enrot - RventRot, -100.0, 50.0) * dt;
dizel_heat.rpmw += static_cast<float>(std::clamp(dizel_heat.rpmwz - dizel_heat.rpmw, -100.f, 50.f) * dt);
dizel_heat.rpmw2 += static_cast<float>(std::clamp(dizel_heat.rpmwz2 - dizel_heat.rpmw2, -100.f, 50.f) * dt);
}
else
{
RventRot *= std::max(0.0, 1.0 - RVentSpeed * dt);
dizel_heat.rpmw *= static_cast<float>(std::max(0.0, 1.0 - dizel_heat.rpmw * dt));
dizel_heat.rpmw2 *= static_cast<float>(std::max(0.0, 1.0 - dizel_heat.rpmw2 * dt));
}
break;
}
case TEngineType::DieselEngine:
{
// NOTE: we update only radiator fans, as vehicles with diesel engine don't have other ventilators
if (true == Mains)
{
dizel_heat.rpmw += static_cast<float>(std::clamp(dizel_heat.rpmwz - dizel_heat.rpmw, -100.f, 50.f) * dt);
dizel_heat.rpmw2 += static_cast<float>(std::clamp(dizel_heat.rpmwz2 - dizel_heat.rpmw2, -100.f, 50.f) * dt);
}
else
{
dizel_heat.rpmw *= static_cast<float>(std::max(0.0, 1.0 - dizel_heat.rpmw * dt));
dizel_heat.rpmw2 *= static_cast<float>(std::max(0.0, 1.0 - dizel_heat.rpmw2 * dt));
}
break;
}
default:
{
break;
}
}
Drive().UpdateVentilators(*this, dt);
}
// the starting resistors' blowers of the series motors with the line breaker closed, worked by hand or by the current
@@ -778,38 +657,7 @@ void TMoverParameters::UpdateResistorBlowers(double dt)
// the power of a dumb or diesel engine; returns the ratio of the power a dumb engine is set to
double TMoverParameters::UpdateEnginePower()
{
double PosRatio{0.0};
switch (EngineType)
{
case TEngineType::Dumb:
{
PosRatio = (MainCtrlPos + ScndCtrlPos) / (MainCtrlPosNo + ScndCtrlPosNo + 0.01);
EnginePower = /*1000.0 **/ Power * PosRatio;
break;
}
case TEngineType::DieselEngine:
{
dizel_Power = Mm * enrot * (2.0 * M_PI / 1000.0);
EnginePower = (dizel_Mstand + Mm) * enrot * (2.0 * M_PI / 1000.0);
if (MainCtrlPowerPos() > 1)
{
// dodatkowe opory z powodu sprezarki}
// dmoment -= dizel_Mstand * ( 0.2 * enrot / dizel_nmax ); //yB: skąd to w ogóle się bierze?!
}
break;
}
case TEngineType::DieselElectric:
{
EnginePower = 0; // the actual calculation is done in two steps later in the method
break;
}
default:
{
break;
}
}
return PosRatio;
return Drive().UpdateEnginePower(*this);
}
// the line breaker opened by a voltage out of the motors' range, and the line contactors of a diesel-electric
@@ -1879,3 +1727,9 @@ double TMoverParameters::ShowCurrentP(int AmpN) const
return current;
}
}
// the drive of the kind of engine of the vehicle
TractionDrive const &TMoverParameters::Drive() const
{
return DriveOf(EngineType);
}