diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f34ad61f..a5e467d93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -464,6 +464,7 @@ set(CORE_SOURCES "scripting/ladderlogic.cpp" "vehicle/handover.cpp" "vehicle/horn.cpp" +"vehicle/linebreaker.cpp" "vehicle/smoke.cpp" ) add_library(eu07_core STATIC ${CORE_SOURCES}) diff --git a/tests/unit/test_linebreaker.cpp b/tests/unit/test_linebreaker.cpp new file mode 100644 index 000000000..126e3307c --- /dev/null +++ b/tests/unit/test_linebreaker.cpp @@ -0,0 +1,114 @@ +#include "stdafx.h" +#include "doctest.h" +#include "consist.h" +#include "drive.h" + +#include "vehicle/linebreaker.h" + +// the line breaker (WS) controls of the cab (eu07/maszyna#394) + +using test::consist; +using test::definition; + +namespace { + +// the line breaker lever of the EP05 (main_sw), a single two-state lever, the way TTrain works it: the state +// the cab keeps of the line breaker follows the vehicle, the breaker closes while the lever is on and can close +struct lever { + TMoverParameters &mover; + int breaker{0}; // as TTrain::m_linebreakerstate: 0 open, 1 closed + bool on{false}; + + void press() { + switch (linebreaker::pressed(breaker, true, on)) { + case linebreaker::toggle::close: + on = true; + break; + case linebreaker::toggle::open: + on = false; + if (breaker == 1 && mover.MainSwitch(false)) { + breaker = 0; + } + break; + default: + break; + } + } + // TTrain::Update() + void update() { + if (breaker == 0 && mover.Mains) { + breaker = 1; + } + if (breaker == 1 && false == mover.Mains) { + breaker = 0; + } + if (on && breaker == 0 && mover.MainSwitchCheck()) { + breaker = mover.MainSwitch(true) ? 1 : 0; + } + } +}; + +} // namespace + +TEST_CASE("line breaker: a press of the toggle closes an open breaker and opens a closed one") { + using enum linebreaker::toggle; + for (auto const latching : {false, true}) { + CHECK(linebreaker::pressed(0, latching, false) == close); + CHECK(linebreaker::pressed(1, latching, true) == open); + CHECK(linebreaker::pressed(1, latching, false) == open); + // freshly closed, the cab finishes closing it first + CHECK(linebreaker::pressed(2, latching, true) == none); + } + // a lever left on over an open breaker goes off; push buttons have no position to keep + CHECK(linebreaker::pressed(0, true, true) == open); + CHECK(linebreaker::pressed(0, false, true) == close); +} + +TEST_CASE("line breaker: the EP05's lever goes off with the converter overload relay tripped (eu07/maszyna#394)") { + consist train; + auto &ep05{train.add(definition::fiz("ep05_v2/ep05"), 1).mover()}; + test::track const line{0.15, 0.0, test::catenary_3kv}; + auto const driver{test::driving(train)}; + REQUIRE(test::start_up(driver, line)); + REQUIRE(ep05.Mains); + REQUIRE(ep05.ConverterFlag); + lever ws{ep05, 1, true}; + + // the converter overload relay knocks the line breaker out, as the cab does it when the converter is + // switched on together with the compressor; the lever stays on + ep05.ConvOvldFlag = true; + ep05.MainSwitch(false, range_t::local); + ws.update(); + REQUIRE_FALSE(ep05.Mains); + REQUIRE(ws.breaker == 0); + CHECK(ws.on); + // with the relay tripped the breaker doesn't close + test::drive(driver, line, 5.0); + CHECK_FALSE(ep05.MainSwitchCheck()); + + // nothing holds the lever: it goes off, and on again, though the breaker stays open + ws.press(); + CHECK_FALSE(ws.on); + ws.press(); + CHECK(ws.on); + ws.update(); + CHECK_FALSE(ep05.Mains); + ws.press(); + CHECK_FALSE(ws.on); + + // the relay reset, with the converter switched off, the lever put on closes the breaker again + ep05.ConverterSwitch(false, range_t::local); + test::drive(driver, line, 1.0); + CHECK(ep05.RelayReset(relay_t::primaryconverteroverload, range_t::local)); + CHECK_FALSE(ep05.ConvOvldFlag); + ws.update(); + CHECK_FALSE(ep05.Mains); // the lever is off + ws.press(); + ws.update(); + CHECK(ep05.Mains); + CHECK(ws.breaker == 1); + // and the next press opens it + ws.press(); + CHECK_FALSE(ws.on); + CHECK_FALSE(ep05.Mains); +} diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index e30635f4f..9fdb97bf9 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/Train.h" #include "vehicle/horn.h" +#include "vehicle/linebreaker.h" #include "utilities/Globals.h" #include "simulation/simulation.h" @@ -3652,16 +3653,22 @@ void TTrain::OnCommand_linebreakertoggle(TTrain *Train, command_data const &Comm if (Command.action == GLFW_PRESS) { // press or hold... - if (Train->m_linebreakerstate == 0) + // a single two-state lever stays where it was put, also when the line breaker gets knocked out + auto const latchinglever{Train->ggMainOnButton.SubModel == nullptr && Train->ggMainOffButton.SubModel == nullptr && Train->ggMainButton.SubModel != nullptr && + Train->ggMainButton.type() == TGaugeType::toggle}; + switch (linebreaker::pressed(Train->m_linebreakerstate, latchinglever, Train->ggMainButton.GetDesiredValue() > 0.5)) { + case linebreaker::toggle::close: // ...to close the circuit // NOTE: bit of a dirty shortcut here OnCommand_linebreakerclose(Train, Command); - } - else if (Train->m_linebreakerstate == 1) - { - // ...to open the circuit + break; + case linebreaker::toggle::open: + // ...to open the circuit, or to put the lever off if the circuit was already opened OnCommand_linebreakeropen(Train, Command); + break; + default: + break; } } else if (Command.action == GLFW_RELEASE) diff --git a/vehicle/linebreaker.cpp b/vehicle/linebreaker.cpp new file mode 100644 index 000000000..97eae0325 --- /dev/null +++ b/vehicle/linebreaker.cpp @@ -0,0 +1,26 @@ +/* +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/. +*/ + +#include "stdafx.h" +#include "vehicle/linebreaker.h" + +namespace linebreaker { + +toggle pressed(int const Breakerstate, bool const Latchinglever, bool const Leveron) { + if (Breakerstate == 1) { + return toggle::open; + } + if (Breakerstate != 0) { + return toggle::none; + } + // the line breaker is open; a lever left on goes off first + return Latchinglever && Leveron ? toggle::open : toggle::close; +} + +} // namespace linebreaker diff --git a/vehicle/linebreaker.h b/vehicle/linebreaker.h new file mode 100644 index 000000000..370e992cc --- /dev/null +++ b/vehicle/linebreaker.h @@ -0,0 +1,28 @@ +/* +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/. +*/ + +#pragma once + +// the line breaker (main switch, WS) controls of the cab +namespace linebreaker { + +enum class toggle : int { + none, // the press does nothing + close, // the control goes to its on position, and the line breaker closes if it can + open // the control goes to its off position, and the line breaker opens +}; + +// what a press of the line breaker toggle does. Breakerstate is the state the cab keeps of the line +// breaker (0: open, 1: closed, 2: freshly closed). Latchinglever is a single two-state switch which +// stays where it was put, Leveron that it stands in its on position. such a lever doesn't follow the +// line breaker: knocked out (by the converter overload relay, say) the breaker opens with the lever +// left on, and the lever has to go off before it can be put on again (eu07/maszyna#394) +toggle pressed(int Breakerstate, bool Latchinglever, bool Leveron); + +} // namespace linebreaker