18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 16:36:53 +01:00
Files
maszyna/physics/components/lowvoltage.h
T
Hirek 160ffe2147 refactor(physics): the low voltage, the train heating and the pantographs keep their state to themselves
As the parts of the main circuit: the battery and the low voltage circuits,
the train heating, the circuit of the pantographs and their air become classes
with the definition in `config` and the state behind queries and operations.

The step of the air of the pantographs (the small compressor, the three-way
cock, the leaks, the pressure switch) moves into PantographAir::Update(), which
gets what it needs as plain values (PantographAirSupply) and reports a trip of
the pressure switch, so the vehicle opens its line breaker or switches the
converter of a unit off as before. The drain of the battery and its limits move
into LowVoltageSupply. The latch of the held battery button of the cab moves
from the physics to TTrain, where the button is.

The gauges of the cab bind to the values they show through const pointers
(TGauge::Assign*() take them), so the queries they bind to return const
references and the cab reads the state live without being able to change it.

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

130 lines
4.3 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 low voltage supply and the train heating of a vehicle, held by physics::Vehicle as lowVoltage and trainHeating.
// as the parts of the main circuit (maincircuit.h): the definition in `config`, the state behind queries and operations
#pragma once
#include "physics/vehicletypes.h"
namespace physics
{
// the battery and the low voltage circuits
class LowVoltageSupply
{
public:
struct Config
{
float nominalBatteryVoltage{0.f};
start_t batteryStart{start_t::manual};
// the battery switch of the cab
bool batteryButtonImpulse{false}; // the switch works as a single button
bool batteryButtonHeld{false}; // the button has to be held to switch the battery on or off
float batteryButtonHoldTime{1.f}; // how long it has to be held [s]
};
Config config;
// the battery of the definition (LMaxVoltage), charged
void SetBattery(double const Voltage)
{
batteryVoltage = Voltage;
config.nominalBatteryVoltage = static_cast<float>(Voltage);
}
bool IsBatteryOn() const { return batteryOn; }
// the battery switch, where the battery is switched by hand; returns whether that changed it
bool SwitchBattery(bool const State)
{
auto const initialstate{batteryOn};
if (config.batteryStart == start_t::manual)
{
batteryOn = State;
}
return batteryOn != initialstate;
}
// the battery cut off whatever its switch, as for a vehicle prepared for hauling
void DisconnectBattery() { batteryOn = false; }
// the battery as the vehicle is placed: on, unless the vehicle has none
void ResetBattery() { batteryOn = config.batteryStart != start_t::disabled; }
// for the tests: the battery on or off, whatever its switch
void SetBatteryOnForTest(bool const State) { batteryOn = State; }
double BatteryVoltage() const { return batteryVoltage; }
// the battery drained by the given amount [V]; returns whether its voltage fell too low for the line breaker to hold.
// it neither charges above its nominal voltage nor drops to nothing
bool DrainBattery(double const Drain)
{
batteryVoltage -= Drain;
auto const toolow{config.nominalBatteryVoltage / batteryVoltage > 1.57};
if (batteryVoltage > config.nominalBatteryVoltage)
{
batteryVoltage = config.nominalBatteryVoltage; // wstrzymanie ładowania pow. 110V
}
if (batteryVoltage < 0.01)
{
batteryVoltage = 0.01;
}
return toolow;
}
// the battery kept at its nominal voltage, where its drain isn't modelled
void KeepBatteryCharged() { batteryVoltage = config.nominalBatteryVoltage; }
// the low voltage circuits, as the last step found them
bool const &Is24vAvailable() const { return power24vAvailable; }
bool const &Is110vAvailable() const { return power110vAvailable; }
// either of them
bool IsAvailable() const { return power24vAvailable || power110vAvailable; }
// the voltage of the battery circuit
double Voltage24v() const { return power24vVoltage; }
// the circuits in the step: the 24 V one from its voltage, the 110 V one as given
void SetSupply(double const Voltage24v, bool const Available110v)
{
power24vVoltage = Voltage24v;
power24vAvailable = power24vVoltage > 0;
power110vAvailable = Available110v;
}
// for the tests of the parts the circuits feed: the power of a circuit as given, whatever the voltage
void Set24vAvailableForTest(bool const State) { power24vAvailable = State; }
void Set110vAvailableForTest(bool const State) { power110vAvailable = State; }
private:
bool batteryOn{false};
double batteryVoltage{0.0};
bool power110vAvailable{false};
bool power24vAvailable{false};
double power24vVoltage{0.0};
};
// the heating of the train fed by the vehicle
class TrainHeating
{
public:
struct Config
{
bool switchImpulse{false}; // the switch springs back to its middle position
double power{0.0};
double engineRpm{0.0}; // the revolutions of the engine guaranteed with the heating on
};
Config config;
bool IsOn() const { return on; }
void SetOn(bool const State) { on = State; }
// the heating switch. TODO: wrap the heating in a basic device
bool IsAllowed() const { return allow; }
void Allow(bool const State) { allow = State; }
private:
bool on{false};
bool allow{false};
};
} // namespace physics