diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index b619ba7de..7cdf004f4 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2498,6 +2498,7 @@ class TMoverParameters // ScndCtrlResistiveLock: the shunt controller drops to 0 once the master controller is at zero or on a resistive position, so it // can't stay engaged there, locked in place (eu07/maszyna#402) void ScndCtrlResistiveLockCheck(); + // NOTE: with the cruise control, either end of the secondary controller, as before the split of the two (eu07/maszyna#69) bool IsScndCtrlNoPowerPos() const; bool IsScndCtrlMaxPowerPos() const; @@ -2743,6 +2744,14 @@ class TMoverParameters void CheckEIMIC(double dt); // sprawdzenie i zmiana nastawy zintegrowanego nastawnika jazdy/hamowania void CheckSpeedCtrl(double dt); + // whether the cruise control has a switch of its own: the secondary controller doubles as its switch unless it + // shunts the field of the traction motors already, or there's none (the 6Dg, the ST45; eu07/maszyna#69) + bool IsSpeedCtrlSeparate() const; + // whether the cruise control is switched on: by its own switch, or by the secondary controller off its zero position + bool IsSpeedCtrlEngaged() const; + // the driver's switch of the cruise control: on, to hold the speed of the moment, or off. its own switch, or the + // secondary controller doubling as one (first position on, back to zero off); returns whether it did anything + bool SpeedCtrlSwitch(bool State); void SpeedCtrlButton(int button); void SpeedCtrlInc(); void SpeedCtrlDec(); diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 825137675..dacb9dd34 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -2577,7 +2577,7 @@ bool TMoverParameters::IncMainCtrl(int CtrlSpeed) OK = true; if (EIMCtrlType == 0 && ( SpeedCtrlAutoTurnOffFlag & 1 ) == 1 && MainCtrlActualPos != MainCtrlPos) { - DecScndCtrl(2); + SpeedCtrlSwitch(false); SpeedCtrlUnit.IsActive = false; } break; @@ -2755,7 +2755,7 @@ bool TMoverParameters::DecMainCtrl(int CtrlSpeed) OK = true; if (EIMCtrlType == 0 && (SpeedCtrlAutoTurnOffFlag & 1) == 1 && MainCtrlActualPos != MainCtrlPos) { - DecScndCtrl(2); + SpeedCtrlSwitch(false); SpeedCtrlUnit.IsActive = false; } } @@ -8043,7 +8043,7 @@ void TMoverParameters::CheckSpeedCtrl(double dt) } if ((SpeedCtrlAutoTurnOffFlag & 2) == 2 && Hamulec->GetEDBCP() > 0.25) { - DecScndCtrl(2); + SpeedCtrlSwitch(false); SpeedCtrlUnit.IsActive = false; } } @@ -8056,9 +8056,47 @@ void TMoverParameters::CheckSpeedCtrl(double dt) } } +bool TMoverParameters::IsSpeedCtrlSeparate() const +{ + // no secondary controller, or one of these engines', which shunts the field of the traction motors (eu07/maszyna#69) + return SpeedCtrl && (ScndCtrlPosNo == 0 || EngineType == TEngineType::ElectricSeriesMotor || EngineType == TEngineType::DieselElectric); +} + +bool TMoverParameters::IsSpeedCtrlEngaged() const +{ + return IsSpeedCtrlSeparate() ? SpeedCtrlUnit.IsActive : ScndCtrlPos > 0; +} + +bool TMoverParameters::SpeedCtrlSwitch(bool const State) +{ + if (false == IsSpeedCtrlSeparate()) + { + return State ? IncScndCtrl(1) : DecScndCtrl(2); + } + // its own switch, the secondary controller left to the field shunting + if (State) + { + if (CabActive == 0 || SpeedCtrlUnit.IsActive) + { + return false; + } + SpeedCtrlValue = static_cast(Round(Vel)); + SpeedCtrlUnit.IsActive = true; + return true; + } + auto const wasactive{SpeedCtrlUnit.IsActive}; + SpeedCtrlValue = 0; + SpeedCtrlUnit.IsActive = false; + if (wasactive && SpeedCtrlUnit.ManualStateOverride) + { + eimic = 0.0; + } + return wasactive; +} + void TMoverParameters::SpeedCtrlButton(int button) { - if (SpeedCtrl && ScndCtrlPos > 0) + if (SpeedCtrl && IsSpeedCtrlEngaged()) { SpeedCtrlValue = SpeedCtrlButtons[button]; } @@ -8066,7 +8104,7 @@ void TMoverParameters::SpeedCtrlButton(int button) void TMoverParameters::SpeedCtrlInc() { - if (SpeedCtrl && ScndCtrlPos > 0) + if (SpeedCtrl && IsSpeedCtrlEngaged()) { double x = floor(SpeedCtrlValue / SpeedCtrlUnit.VelocityStep) + 1.0; SpeedCtrlValue = std::min(x * SpeedCtrlUnit.VelocityStep, SpeedCtrlUnit.MaxVelocity); @@ -8075,7 +8113,7 @@ void TMoverParameters::SpeedCtrlInc() void TMoverParameters::SpeedCtrlDec() { - if (SpeedCtrl && ScndCtrlPos > 0) + if (SpeedCtrl && IsSpeedCtrlEngaged()) { double x = ceil(SpeedCtrlValue / SpeedCtrlUnit.VelocityStep) - 1.0; SpeedCtrlValue = std::max(x * SpeedCtrlUnit.VelocityStep, SpeedCtrlUnit.MinVelocity); @@ -8084,7 +8122,7 @@ void TMoverParameters::SpeedCtrlDec() bool TMoverParameters::SpeedCtrlPowerInc() { - if (!(SpeedCtrl && ScndCtrlPos > 0)) + if (!(SpeedCtrl && IsSpeedCtrlEngaged())) return false; if (SpeedCtrlUnit.DesiredPower == SpeedCtrlUnit.MaxPower) @@ -8096,7 +8134,7 @@ bool TMoverParameters::SpeedCtrlPowerInc() bool TMoverParameters::SpeedCtrlPowerDec() { - if (!(SpeedCtrl && ScndCtrlPos > 0)) + if (!(SpeedCtrl && IsSpeedCtrlEngaged())) return false; if (SpeedCtrlUnit.DesiredPower == SpeedCtrlUnit.MinPower) diff --git a/tests/unit/data/fiz/6dg_v1/6dg.json b/tests/unit/data/fiz/6dg_v1/6dg.json new file mode 100644 index 000000000..9ad99dec4 --- /dev/null +++ b/tests/unit/data/fiz/6dg_v1/6dg.json @@ -0,0 +1,55 @@ +{ + "_source": "pkp/6dg_v1/6dg.fiz", + "_comment": "converted by test_fiz.cpp from the game's definition with the files it includes, keeping what the game's loader reads", + "Param": {"Category": "train", "M": "70000", "Mred": "10000", "Vmax": "90", "PWR": "708", "SandCap": "300"}, + "Dimensions": {"L": "14.24", "H": "4.285", "W": "3.170", "Cx": "0.2"}, + "Wheels": {"D": "1.100", "AIM": "4000", "Tw": "1.435", "Axle": "Bo'Bo'", "Ad": "2.6", "Bd": "7.524", "Rmin": "80"}, + "Brake": {"BrakeValve": "LSt", "NBpA": "4", "MBF": "85.0", "MaxBP": "6.2", "MaxASBP": "2.0", "BCN": "4", "BCR": "0.1143", "BCD": "0.05", "BCM": "6.68", "HiPP": "5.0", "LoPP": "3.5", "Vv": "0.8", "MinCP": "7.5", "MaxCP": "8.5", "CompressorSpeed": "0.05", "CompressorPower": "Converter", "BVV": "200", "BRE": "0.9", "UBB1": "1", "UBB2": "12", "UBB3": "16", "HandlePipeUnlockPos": "-0.5", "LPOn": "2.75", "LPOff": "4.5"}, + "BuffCoupl": {"ControlType": "6Dg", "CType": "Screw", "kB": "9200", "DmaxB": "0.15", "FmaxB": "740", "kC": "4800", "DmaxC": "0.15", "FmaxC": "780", "beta": "0.58", "AllowedFlag": "39"}, + "Cntrl": {"BrakeSystem": "Pneumatic", "BCPN": "10", "BDelay1": "15", "BDelay2": "3", "BDelay3": "35", "BDelay4": "36", "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": "6", "SCPN": "1", "AutoRelay": "Yes", "IniCDelay": "0.5", "SCDelay": "0.1", "BrakeDelays": "GP", "BrakeHandle": "MHZ_K8P", "LocBrakeHandle": "FD1", "ConverterStart": "Automatic", "ConverterStartDelay": "10", "FuelStart": "Automatic", "OilStart": "Automatic", "BrakeOpModes": "PN", "EIMCtrlType": "3", "DirChangeMaxPos": "3", "HAO": "No", "HMO": "Yes", "HGDP1": "2", "rows": [ + ["-1","0.7","-1","15","Pneumatic"], + ["0","0.5","-1","9","Pneumatic"], + ["1","0.46","-1","9","Pneumatic"], + ["2","0.42","-1","9","Pneumatic"], + ["3","0.38","-1","9","Pneumatic"], + ["4","0.34","-1","9","Pneumatic"], + ["5","0.29","-1","9","Pneumatic"], + ["6","0","-1","15","Pneumatic"] + ]}, + "Security": {"AwareSystem": "Active,CabSignal", "AwareDelay": "60", "SoundSignalDelay": "5", "MaxHoldTime": "1.0", "EmergencyBrakeDelay": "6", "RadioStop": "Yes"}, + "Light": {"Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": "110", "LMaxCurrent": "91"}, + "Engine": {"AIM": "0.55", "EngineType": "DumbDE", "Trans": "17:75", "Ftmax": "219000", "Vhyp": "7", "Vadd": "5.5", "Cr": "1", "ShuntMode": "0", "WaterCoolingTemperature": "81", "WaterCoolingFanSpeed": "-1200", "WaterMaxTemperature": "85", "OilMinPressure": "0.15", "OilMinTemperature": "25", "OilMaxTemperature": "90", "HeaterMinTemperature": "55", "HeaterMaxTemperature": "60", "HeatKFS": "35", "HeatKFO": "30", "HeatKFE": "7", "HeatKW": "0.01", "HeatKV": "0.3", "HeatKFO2": "100"}, + "MotorParamTable": {"rows": [ + ["0","17.567","64","1500","15","800","1320"], + ["1","15","183.3","2000","49","800","1320"] + ]}, + "SpringBrake": {"Volume": "8", "MBF": "5", "MaxSP": "6", "ResetP": "4.0", "MinFP": "3.5", "PressOff": "4.5", "PressOn": "3.5", "ValveOnArea": "0.0002", "ValveOffArea": "0.0002", "ValvePNBArea": "0.002", "MTC": "4"}, + "WWList": {"Size": "6", "rows": [ + ["596","0","0","0"], + ["840","120","324","1150"], + ["1080","240","468","1350"], + ["1320","360","612","1550"], + ["1560","480","756","1750"], + ["1800","600","900","2000"], + ["1800","600","900","2000"] + ]}, + "SpeedControl": {"SpeedCtrl": "Yes", "OverrideManual": "Yes", "InitPwr": "0.3", "MaxPwrVel": "5", "StartVel": "2", "MinPwr": "0.3", "MaxPwr": "1.0", "MinVel": "0", "MaxVel": "120", "Offset": "-0.5", "kPpos": "0.1", "kPneg": "0.4", "kIpos": "0.06", "kIneg": "0.06", "BrakeIntervention": "Yes"}, + "LightsList": {"Size": "7", "Wrap": "no", "Default": "1", "rows": [ + ["0","0"], + ["34","34"], + ["38","34"], + ["22","34"], + ["16","1"], + ["1","16"], + ["21","34"] + ]}, + "UCList": {"Size": "6", "IntegratedBrakePN": "no", "IntegratedLocBrake": "yes", "IntegratedBrake": "no", "CtrlValStep": "0.05", "rows": [ + ["0","0","-1","0","-1","0.15","0.15","0","1","1"], + ["1","0","-1","0","0","0.00","0.00","1","3","1"], + ["2","0","-1","0","0","0.15","0.00","1","3","1"], + ["3","0","0","0","0","0.00","0.00","1","5","3"], + ["4","0","0","1","0","0.00","0.15","3","5","5"], + ["5","0","0","1","0","0.00","0.00","3","6","5"], + ["6","0","0","1","1","0.15","0.15","5","6","5"] + ]} +} diff --git a/tests/unit/data/fiz/README.md b/tests/unit/data/fiz/README.md index 514fb6287..e29a85dbb 100644 --- a/tests/unit/data/fiz/README.md +++ b/tests/unit/data/fiz/README.md @@ -20,6 +20,7 @@ Every `.json` here is a vehicle picked up by `test_fiz.cpp`: record the paramete | `3wl_v2/3wl.fiz` | 3WL | - | ESt3 | - | ManualBrake | Screw | | `430w_v2/430w.fiz` | 430W | - | ESt3AL2HBG300 | - | ManualBrake | Screw | | `4e_v1/4e.fiz` | 4E | ElectricSeriesMotor | LSt | FV4a | PneumaticBrake | Screw | +| `6dg_v1/6dg.fiz` | 6Dg | DumbDE | LSt | MHZ_K8P | PneumaticBrake | Screw | | `bdhpumn_v3/bdhpumn.fiz` | BDHPUMN | - | KE | - | ManualBrake | Screw | | `bhp_v1/bhp-b.fiz` | BHP | - | ESt3AL2 | - | ManualBrake | Articulated | | `br285_v1/br285.fiz` | BR285 | DieselElectric | KE | MHZ_6P | PneumaticBrake | Screw | @@ -63,4 +64,4 @@ Every `.json` here is a vehicle picked up by `test_fiz.cpp`: record the paramete | `wmb10_v1/wmb10.fiz` | WMB10 | DieselEngine | LSt | FV4a | PneumaticBrake | Screw | | `z1_v2/152a.fiz` | Z1 | - | KE | - | ManualBrake | Screw | -The paths are those of the game's `.fiz` files. `elf_v1/inverter.fiz` is a part the Elf units include, not a vehicle. The 218M (V2NList), the SA134 (HTCList) and the 22WEc (ffBrakeList) are there for lists the others lack, the EP05 for its camshaft. The 214MA stands for the 214M railbus, whose own file repeats a section (TurboPos and MotorParamTable twice), which the definitions here can't hold. +The paths are those of the game's `.fiz` files. `elf_v1/inverter.fiz` is a part the Elf units include, not a vehicle. The 218M (V2NList), the SA134 (HTCList) and the 22WEc (ffBrakeList) are there for lists the others lack, the EP05 for its camshaft. The 6Dg is there for its universal controller (UCList) setting the power in steps (CtrlValStep), converted alone from the game data of 2026-11 (SVN r9630, which added the key), the others staying as they were. The 214MA stands for the 214M railbus, whose own file repeats a section (TurboPos and MotorParamTable twice), which the definitions here can't hold. diff --git a/tests/unit/test_speed_control.cpp b/tests/unit/test_speed_control.cpp new file mode 100644 index 000000000..a141f41a6 --- /dev/null +++ b/tests/unit/test_speed_control.cpp @@ -0,0 +1,395 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scenario.h" +#include "drive.h" + +#include + +// the cruise control (tempomat, SpeedCtrl) and the secondary controller (ScndCtrl) of the vehicles which have one +// of them or both (eu07/maszyna#69). on the vehicles with the cruise control alone the secondary controller is +// its switch, as it always was; on those whose secondary controller shunts the field of the traction motors +// (the 6Dg, the ST45 and the BR285 among them) the cruise control has a switch of its own, apart from it + +using test::scenario; +using namespace test::channels; + +namespace { + +std::vector cruise() { + return { + {"SpeedCtrlActive", [](auto const &M) { return M.SpeedCtrlUnit.IsActive ? 1.0 : 0.0; }}, + {"SpeedCtrlStandby", [](auto const &M) { return M.SpeedCtrlUnit.Standby ? 1.0 : 0.0; }}, + {"SpeedCtrlValue", [](auto const &M) { return M.SpeedCtrlValue; }}, + {"eimic", [](auto const &M) { return M.eimic; }}, + {"eimicSpeedCtrl", [](auto const &M) { return M.eimicSpeedCtrl; }}, + {"eimic_real", [](auto const &M) { return M.eimic_real; }}, + }; +} + +// the universal controller (EIMCtrlType 3) of the 6Dg: 3 no power, 4 lowers the demand, 5 holds it, 6 raises it +constexpr int dg_no_power{3}; +constexpr int dg_lower{4}; +constexpr int dg_hold{5}; +constexpr int dg_raise{6}; + +void move_to(TMoverParameters &Vehicle, int const Position) { + while (Vehicle.MainCtrlPos < Position && Vehicle.IncMainCtrl(1)) { + ; + } + while (Vehicle.MainCtrlPos > Position && Vehicle.DecMainCtrl(1)) { + ; + } + REQUIRE(Vehicle.MainCtrlPos == Position); +} + +// the 6Dg started up, its brakes released, its controller through the no power and lowering positions to holding, +// for the line contactors to close +void get_6dg_going(scenario &Scenario) { + auto &loco{Scenario.cab()}; + REQUIRE(Scenario.start_up()); + Scenario.get_going(); + move_to(loco, dg_no_power); + for (int second = 0; second < 180 && loco.BrakePress > 0.1; ++second) { + Scenario.wait(1.0); + } + REQUIRE(loco.BrakePress <= 0.1); + move_to(loco, dg_lower); + Scenario.wait(1.0); + move_to(loco, dg_hold); + Scenario.wait(5.0); + REQUIRE(loco.StLinFlag); +} + +} // namespace + +// the vehicles with the field shunting alone, and those with the cruise control alone: as before the split + +TEST_CASE("speed control: the field shunting of the vehicles without a cruise control, as before") { + // the EU07's and the SM42's shunting by hand, the SU45's by itself (RelayType 45, from position 11 of its + // controller on); none has a cruise control, and the secondary controller shunts the field + SUBCASE("EU07 and SM42, by hand") { + for (auto const *path : {"4e_v1/4e", "sm42_v1/6d1"}) { + CAPTURE(path); + scenario scenario{std::string{"speed control/shunting/"} + path, motion() + traction() + cruise()}; + scenario.add(path, 1); + auto &loco{scenario.cab()}; + REQUIRE_FALSE(loco.SpeedCtrl); + REQUIRE_FALSE(loco.AutoRelayFlag); + REQUIRE(scenario.start_up()); + scenario.get_going(); + scenario.full_power(60.0); + auto const speed{loco.Vel}; + scenario.act("shunt"); + CHECK(loco.IncScndCtrl(1)); + CHECK(loco.ScndCtrlPos == 1); + scenario.wait(10.0); + CHECK(loco.Vel > speed); + CHECK(loco.ScndCtrlPos == 1); + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + CHECK(loco.SpeedCtrlValue == 0.0); + scenario.act("shunt off"); + CHECK(loco.DecScndCtrl(2)); + CHECK(loco.ScndCtrlPos == 0); + scenario.wait(5.0); + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + scenario.check(); + } + } + SUBCASE("SU45, by itself") { + scenario scenario{"speed control/shunting/su45_v2/301d", motion() + traction() + cruise()}; + scenario.add("su45_v2/301d", 1); + auto &loco{scenario.cab()}; + REQUIRE_FALSE(loco.SpeedCtrl); + REQUIRE(loco.AutoRelayFlag); + REQUIRE(scenario.start_up()); + scenario.get_going(); + scenario.full_power(5.0); + auto most{0}; + for (int i = 0; i < 18; ++i) { + scenario.wait(10.0); + most = std::max(most, loco.ScndCtrlPos); + } + CHECK(most == loco.ScndCtrlPosNo); + // the driver can't move it + CHECK_FALSE(loco.DecScndCtrl(2)); + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + scenario.act("notch back"); + scenario.notch_back(0); + scenario.wait(5.0); + CHECK(loco.ScndCtrlPos == 0); + scenario.check(); + } +} + +TEST_CASE("speed control: the vehicles whose secondary controller is the cruise control's switch, as before") { + SUBCASE("Impuls: on at the speed of the moment, off by the controller") { + scenario scenario{"speed control/cruise/impuls_v1/31we-a_pr", motion() + traction() + cruise()}; + scenario.add("impuls_v1/31we-a_pr", 1).add("impuls_v1/31we-b_pr").add("impuls_v1/31we-c_pr").add("impuls_v1/31we-d_pr"); + auto &unit{scenario.cab()}; + REQUIRE(unit.SpeedCtrl); + REQUIRE(unit.ScndCtrlPosNo == 1); + REQUIRE(scenario.start_up()); + scenario.get_going(); + scenario.full_power(40.0); + scenario.act("cruise control on"); + CHECK(unit.IncScndCtrl(1)); + CHECK(unit.ScndCtrlPos == 1); + CHECK(unit.SpeedCtrlUnit.IsActive); + CHECK(unit.SpeedCtrlValue == doctest::Approx(unit.Vel)); + auto const held{unit.SpeedCtrlValue}; + scenario.wait(60.0); + CHECK(unit.Vel == doctest::Approx(held).epsilon(0.05)); + scenario.act("cruise control off"); + CHECK(unit.DecScndCtrl(2)); + CHECK(unit.ScndCtrlPos == 0); + CHECK_FALSE(unit.SpeedCtrlUnit.IsActive); + CHECK(unit.SpeedCtrlValue == 0.0); + scenario.wait(5.0); + scenario.check(); + } + SUBCASE("EU47: the speed raised by tens while the controller is held up, set when it's let go") { + scenario scenario{"speed control/cruise/e186_v2/eu47", motion() + traction() + cruise()}; + scenario.add("e186_v2/eu47", 1); + auto &loco{scenario.cab()}; + REQUIRE(loco.SpeedCtrl); + REQUIRE(loco.SpeedCtrlTypeTime); + REQUIRE(loco.ScndCtrlPosNo == 4); + REQUIRE(scenario.start_up()); + scenario.get_going(); + scenario.act("raise"); + while (loco.ScndCtrlPos < 3 && loco.IncScndCtrl(1)) { + ; + } + REQUIRE(loco.ScndCtrlPos == 3); + scenario.wait(10.0); + CHECK(loco.NewSpeed > 0.0); + scenario.act("set"); + CHECK(loco.DecScndCtrl(1)); + scenario.wait(1.0); + CHECK(loco.SpeedCtrlValue == doctest::Approx(loco.NewSpeed)); + CHECK(loco.SpeedCtrlUnit.IsActive); + scenario.full_power(60.0); + CHECK(loco.Vel == doctest::Approx(loco.SpeedCtrlValue).epsilon(0.1)); + scenario.act("off"); + CHECK(loco.DecScndCtrl(2)); + scenario.wait(1.0); + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + scenario.check(); + } +} + +// the vehicles with both: the field shunting as before, the cruise control on a switch of its own + +TEST_CASE("speed control: the field shunting of the vehicles with a cruise control besides, as before") { + SUBCASE("6Dg") { + scenario scenario{"speed control/both/6dg_v1/6dg", motion() + traction() + cruise(), test::track{}}; + scenario.add("6dg_v1/6dg", 1); + auto &loco{scenario.cab()}; + REQUIRE(loco.SpeedCtrl); + REQUIRE(loco.AutoRelayFlag); + REQUIRE(loco.ScndCtrlPosNo == 1); + get_6dg_going(scenario); + scenario.act("raise"); + move_to(loco, dg_raise); + // the shunt engages by itself at about 40 km/h (RelayType 0, by the current of the motors) + scenario.wait(10.0); + CHECK(loco.Vel < 35.0); + CHECK(loco.ScndCtrlPos == 0); + scenario.wait(110.0); + CHECK(loco.Vel > 100.0); + CHECK(loco.ScndCtrlPos == 1); + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + CHECK(loco.eimic_real == doctest::Approx(loco.eimic)); + // the driver can't move the shunt + CHECK_FALSE(loco.IncScndCtrl(1)); + CHECK_FALSE(loco.DecScndCtrl(2)); + scenario.check(); + } + SUBCASE("ST45") { + scenario scenario{"speed control/both/st45_v2/301dd", motion() + traction() + cruise(), test::track{}}; + scenario.add("st45_v2/301dd", 1); + auto &loco{scenario.cab()}; + REQUIRE(loco.SpeedCtrl); + REQUIRE(loco.AutoRelayFlag); + REQUIRE(scenario.start_up()); + scenario.get_going(); + scenario.full_power(5.0); + // its relays (RelayType 45) engage the shunt from position 11 of the controller on, which the universal + // controller of the ST45, of 4 positions, never gets to: the shunt stays off + auto most{0}; + for (int i = 0; i < 9; ++i) { + scenario.wait(10.0); + most = std::max(most, loco.ScndCtrlPos); + } + CHECK(loco.Vel > 130.0); + CHECK(most == 0); + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + CHECK(loco.eimic_real == doctest::Approx(loco.eimic)); + // the speed buttons do nothing with the cruise control off + loco.SpeedCtrlButton(4); + loco.SpeedCtrlInc(); + CHECK(loco.SpeedCtrlValue == 0.0); + CHECK_FALSE(loco.IncScndCtrl(1)); + scenario.check(); + } +} + +TEST_CASE("speed control apart: which vehicles' cruise control has a switch of its own") { + struct kind { + char const *path; + bool separate; + }; + for (auto const &[path, separate] : { + // the secondary controller shunts the field: a switch of its own + kind{"6dg_v1/6dg", true}, kind{"st45_v2/301dd", true}, kind{"br285_v1/br285", true}, + // the secondary controller is the switch, the speed setter or the gears' selector of the automatic gearbox + kind{"21xm_v1/214ma", false}, kind{"21xm_v1/218ma_a_kd", false}, kind{"sa134_v1/218mc_a", false}, kind{"impuls_v1/31we-a_pr", false}, + kind{"elf_v1/21we-a", false}, kind{"e186_v2/eu47", false}, kind{"e6act_v2/e6acta", false}, kind{"en57akl_v1/akl_ra", false}, + // no cruise control + kind{"4e_v1/4e", false}, kind{"sm42_v1/6d1", false}, kind{"su45_v2/301d", false}, kind{"ep09_v2/104e", false}}) { + CAPTURE(path); + test::vehicle vehicle{test::definition::fiz(path)}; + CHECK(vehicle->IsSpeedCtrlSeparate() == separate); + CHECK_FALSE(vehicle->IsSpeedCtrlEngaged()); + } + // with a cruise control and no secondary controller, the switch is its own too + test::vehicle eu07{test::definition::fiz("4e_v1/4e").set("Cntrl", "SCPN", 0).set("SpeedControl", "SpeedCtrl", "Yes")}; + CHECK(eu07->IsSpeedCtrlSeparate()); +} + +TEST_CASE("speed control apart: the 6Dg's cruise control on its own switch, the field shunting going on by itself") { + scenario scenario{"speed control apart/6dg_v1/6dg", motion() + traction() + cruise(), test::track{}}; + scenario.add("6dg_v1/6dg", 1); + auto &loco{scenario.cab()}; + get_6dg_going(scenario); + scenario.act("raise"); + move_to(loco, dg_raise); + scenario.wait(30.0); + REQUIRE(loco.Vel > 50.0); + REQUIRE(loco.ScndCtrlPos == 1); // the shunt in + // the shunt is no cruise control: its buttons do nothing yet, the secondary controller can't switch it on + CHECK_FALSE(loco.IsSpeedCtrlEngaged()); + loco.SpeedCtrlButton(2); + CHECK(loco.SpeedCtrlValue == 0.0); + CHECK_FALSE(loco.IncScndCtrl(1)); + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + + scenario.act("cruise control on"); + CHECK(loco.SpeedCtrlSwitch(true)); + CHECK_FALSE(loco.SpeedCtrlSwitch(true)); // on already + CHECK(loco.SpeedCtrlUnit.IsActive); + CHECK(loco.IsSpeedCtrlEngaged()); + CHECK(loco.SpeedCtrlValue == doctest::Approx(std::round(loco.Vel)).epsilon(0.02)); + CHECK(loco.ScndCtrlPos == 1); // the shunt as it was + auto const held{loco.SpeedCtrlValue}; + // the shunt goes by itself all the while, by the current of the motors, which the cruise control's demand sets + auto shunted{0}; + auto unshunted{0}; + auto const run{[&](int const Seconds) { + for (int second = 0; second < Seconds; ++second) { + scenario.wait(1.0); + ++(loco.ScndCtrlPos > 0 ? shunted : unshunted); + } + }}; + run(60); + CHECK(loco.Vel == doctest::Approx(held).epsilon(0.03)); + CHECK_FALSE(loco.SpeedCtrlUnit.Standby); + CHECK(loco.eimic_real < loco.eimic); // the cruise control holding the power back + + scenario.act("two steps up"); + loco.SpeedCtrlInc(); + loco.SpeedCtrlInc(); + auto const higher{loco.SpeedCtrlValue}; + CHECK(higher == doctest::Approx(held + 10.0).epsilon(0.1)); + run(60); + CHECK(loco.Vel == doctest::Approx(higher).epsilon(0.03)); + CHECK(loco.StLinFlag); + CHECK(shunted > 0); + CHECK(unshunted > 0); + CHECK_FALSE(loco.IncScndCtrl(1)); // still not the driver's + + scenario.act("button 30"); + loco.SpeedCtrlButton(0); + CHECK(loco.SpeedCtrlValue == doctest::Approx(30.0)); + scenario.wait(60.0); + // it brakes down with the air (BrakeIntervention), which opens the line contactors (the pressure switch), and + // they close again only through the first power position of the controller, as without the cruise control + CHECK(loco.Vel < 35.0); + CHECK_FALSE(loco.StLinFlag); + scenario.act("controller to the first power position and back up"); + move_to(loco, dg_lower); + scenario.wait(2.0); + move_to(loco, dg_raise); + scenario.wait(60.0); + CHECK(loco.StLinFlag); + CHECK(loco.Vel == doctest::Approx(30.0).epsilon(0.05)); + CHECK(loco.ScndCtrlPos == 0); // out at this speed + + scenario.act("cruise control off"); + CHECK(loco.SpeedCtrlSwitch(false)); + CHECK_FALSE(loco.SpeedCtrlSwitch(false)); // off already + CHECK_FALSE(loco.SpeedCtrlUnit.IsActive); + CHECK(loco.SpeedCtrlValue == 0.0); + scenario.wait(5.0); + CHECK(loco.eimic_real == doctest::Approx(loco.eimic)); // the controller's demand again + scenario.check(); +} + +TEST_CASE("speed control apart: the ST45's cruise control on its own switch, with its buttons and the power limit") { + scenario scenario{"speed control apart/st45_v2/301dd", motion() + traction() + cruise(), test::track{}}; + scenario.add("st45_v2/301dd", 1); + auto &loco{scenario.cab()}; + REQUIRE(scenario.start_up()); + scenario.get_going(); + scenario.full_power(0.0); + REQUIRE(loco.Vel < 50.0); + scenario.act("cruise control on, button 60"); + CHECK(loco.SpeedCtrlSwitch(true)); + CHECK(loco.SpeedCtrlUnit.IsActive); + loco.SpeedCtrlButton(4); + CHECK(loco.SpeedCtrlValue == doctest::Approx(60.0)); + scenario.wait(90.0); + CHECK(loco.Vel == doctest::Approx(60.0).epsilon(0.03)); + CHECK(loco.StLinFlag); + scenario.act("step up, power down"); + loco.SpeedCtrlInc(); + CHECK(loco.SpeedCtrlValue == doctest::Approx(65.0)); + CHECK(loco.SpeedCtrlPowerDec()); + CHECK(loco.SpeedCtrlUnit.DesiredPower == doctest::Approx(0.9)); + scenario.wait(60.0); + CHECK(loco.Vel == doctest::Approx(65.0).epsilon(0.03)); + CHECK(loco.eimic_real <= 0.9 + 1e-9); + scenario.act("cruise control off"); + CHECK(loco.SpeedCtrlSwitch(false)); + // off, its buttons do nothing + loco.SpeedCtrlButton(6); + loco.SpeedCtrlInc(); + CHECK_FALSE(loco.SpeedCtrlPowerInc()); + CHECK(loco.SpeedCtrlValue == 0.0); + scenario.wait(5.0); + CHECK(loco.ScndCtrlPos == 0); + scenario.check(); +} + +TEST_CASE("speed control apart: the switch of a cruise control without one of its own is the secondary controller, as before") { + // the 214M railbus: on, its first position; off, back to zero + scenario scenario{"speed control apart/21xm_v1/214ma", motion() + traction() + cruise(), test::track{}}; + scenario.add("21xm_v1/214ma", 1); + auto &railbus{scenario.cab()}; + REQUIRE(scenario.start_up()); + scenario.get_going(); + scenario.full_power(20.0); + scenario.act("cruise control on"); + CHECK(railbus.SpeedCtrlSwitch(true)); + CHECK(railbus.ScndCtrlPos == 1); + CHECK(railbus.SpeedCtrlUnit.IsActive); + CHECK(railbus.IsSpeedCtrlEngaged()); + scenario.wait(10.0); + scenario.act("cruise control off"); + CHECK(railbus.SpeedCtrlSwitch(false)); + CHECK(railbus.ScndCtrlPos == 0); + CHECK_FALSE(railbus.SpeedCtrlUnit.IsActive); + CHECK_FALSE(railbus.IsSpeedCtrlEngaged()); + scenario.wait(5.0); + scenario.check(); +} diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index eb74344bf..594f660ce 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -4474,7 +4474,16 @@ void TController::SpeedCntrl(double DesiredSpeed) { if( false == mvControlling->SpeedCtrl ) { return; } - if (mvControlling->EngineType == TEngineType::DieselEngine) + if (mvControlling->IsSpeedCtrlSeparate()) + { + // a switch of its own, the secondary controller left to the field shunting (eu07/maszyna#69) + if (DesiredSpeed < 0.1) { + DesiredSpeed = 0; + } + mvControlling->SpeedCtrlSwitch(DesiredSpeed > 0); + mvControlling->RunCommand("SpeedCntrl", DesiredSpeed, mvControlling->CabActive); + } + else if (mvControlling->EngineType == TEngineType::DieselEngine) { if (DesiredSpeed < 0.1) { mvControlling->DecScndCtrl(2); @@ -4498,7 +4507,7 @@ void TController::SpeedCntrl(double DesiredSpeed) while( mvControlling->ScndCtrlPos < DesiredPos && true == mvControlling->IncScndCtrl(1) ) { ; } // all work is done in the condition loop } - if( mvControlling->SpeedCtrlUnit.PowerStep > 0 && mvControlling->ScndCtrlPos > 0 ) { + if( mvControlling->SpeedCtrlUnit.PowerStep > 0 && mvControlling->IsSpeedCtrlEngaged() ) { while (mvControlling->SpeedCtrlUnit.DesiredPower < mvControlling->SpeedCtrlUnit.MaxPower) { mvControlling->SpeedCtrlPowerInc(); diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 50659934c..72aa6b480 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -1447,8 +1447,8 @@ void TTrain::OnCommand_secondcontrollerincrease(TTrain *Train, command_data cons auto const neutralposition{Train->ggScndCtrl.is_toggle() ? 0.5f : 0.f}; Train->ggScndCtrl.UpdateValue(Command.action == GLFW_RELEASE ? neutralposition : activeposition, Train->dsbSwitch); } - // potentially animate tempomat button - if (Train->ggScndCtrlButton.is_push() && Train->mvControlled->ScndCtrlPos <= 1) + // potentially animate tempomat button, unless the cruise control has its own + if (Train->ggScndCtrlButton.is_push() && Train->mvControlled->ScndCtrlPos <= 1 && false == Train->mvControlled->IsSpeedCtrlSeparate()) { Train->ggScndCtrlButton.UpdateValue(Command.action == GLFW_RELEASE ? 0.f : 1.f, Train->dsbSwitch); } @@ -1511,17 +1511,17 @@ void TTrain::OnCommand_tempomattoggle(TTrain *Train, command_data const &Command return; } // glfw_press - if (Train->mvControlled->ScndCtrlPos == 0) + if (false == Train->mvControlled->IsSpeedCtrlEngaged()) { // turn on if it's not active - Train->mvControlled->IncScndCtrl(1); + Train->mvControlled->SpeedCtrlSwitch(true); // visual feedback Train->ggScndCtrlButton.UpdateValue(1.0, Train->dsbSwitch); } else { // otherwise turn off - Train->mvControlled->DecScndCtrl(2); + Train->mvControlled->SpeedCtrlSwitch(false); // visual feedback if (Train->m_controlmapper.contains("tempomatoff_sw:")) { @@ -1541,17 +1541,17 @@ void TTrain::OnCommand_tempomattoggle(TTrain *Train, command_data const &Command return; } - if (Train->mvControlled->ScndCtrlPos == 0) + if (false == Train->mvControlled->IsSpeedCtrlEngaged()) { // turn on - Train->mvControlled->IncScndCtrl(1); + Train->mvControlled->SpeedCtrlSwitch(true); // visual feedback Train->ggScndCtrlButton.UpdateValue(1.0, Train->dsbSwitch); } else { // turn off - Train->mvControlled->DecScndCtrl(2); + Train->mvControlled->SpeedCtrlSwitch(false); // visual feedback Train->ggScndCtrlButton.UpdateValue(0.0, Train->dsbSwitch); } @@ -1653,8 +1653,8 @@ void TTrain::OnCommand_secondcontrollerdecrease(TTrain *Train, command_data cons auto const neutralposition{Train->ggScndCtrl.is_toggle() ? 0.5f : 0.f}; Train->ggScndCtrl.UpdateValue(Command.action == GLFW_RELEASE ? neutralposition : activeposition, Train->dsbSwitch); } - // potentially animate tempomat button - if (Train->ggScndCtrlButton.is_push() && Train->mvControlled->ScndCtrlPos <= 1) + // potentially animate tempomat button, unless the cruise control has its own + if (Train->ggScndCtrlButton.is_push() && Train->mvControlled->ScndCtrlPos <= 1 && false == Train->mvControlled->IsSpeedCtrlSeparate()) { if (Train->m_controlmapper.contains("tempomatoff_sw:")) { @@ -9725,7 +9725,7 @@ bool TTrain::Update(double const Deltatime) { if (ggScndCtrlButton.is_toggle()) { - ggScndCtrlButton.UpdateValue(mvControlled->ScndCtrlPos > 0 ? 1.f : 0.f, dsbSwitch); + ggScndCtrlButton.UpdateValue(mvControlled->IsSpeedCtrlEngaged() ? 1.f : 0.f, dsbSwitch); } ggScndCtrlButton.Update(lowvoltagepower); } @@ -11993,7 +11993,7 @@ void TTrain::set_cab_controls(int const Cab) // tempomat if (false == ggScndCtrlButton.is_push()) { - ggScndCtrlButton.PutValue(mvControlled->ScndCtrlPos > 0 ? 1.f : 0.f); + ggScndCtrlButton.PutValue(mvControlled->IsSpeedCtrlEngaged() ? 1.f : 0.f); } // sandbox if (ggAutoSandButton.type() != TGaugeType::push)