diff --git a/CMakeLists.txt b/CMakeLists.txt index ddffc7402..200adcf30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -571,6 +571,7 @@ set(CORE_SOURCES "vehicle/gaugewrap.cpp" "vehicle/drivingaid.cpp" "vehicle/brakingcurve.cpp" +"vehicle/signalpassing.cpp" "vehicle/handover.cpp" "vehicle/crew.cpp" "vehicle/horn.cpp" diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index b85f0f8cf..321b65479 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -135,7 +135,7 @@ Python 3.10+, no extra packages. Options: `cmake-build-*/bin/Release/eu07*.exe`. The build needs the `--test` and `-ini` command line options. - name one or more cases to run only those, e.g. `run.py ... eu07_td_timetable`. -- `-j N` runs N cases at once. Each case prints its results when it is done. The 22 cases +- `-j N` runs N cases at once. Each case prints its results when it is done. The 42 cases that aren't `slow` take about 30 s with `-j 4` and 2 minutes one at a time, loading included. - `--watch` runs a case the old way, in a visible window in real time (or faster with `--timespeed N`), closed after the case's `duration`; the exit code isn't checked then. For @@ -236,6 +236,11 @@ starting at `vars.velocity` km/h; with `track_friction` 0.03 it locks the wheels and slides past the signal, so those cases set `test.fail_if_stop_overran no`. `td_red_signal_pass.scn` adds the `test_pass` event, which `test_mode_pass` puts as `event0` on `tor1k`, the track in front of the signal. +`td_shunt_red_signal.scn` is the same with no timetable, so the AI shunts (Shunt) towards the signal at +stop, starting `vars.offset` m into `tdo_n1734`: `eu07_td_shunt_red_signal` alone and +`eu07_td_shunt_push_red_signal` pushing three coaches, both from 30 km/h, have to stop before the signal, +151 m from the start (the shunting AI used to coast up to it and pass it at about 28 km/h, fixed +by #409). They stop after 145 m and 141 m. `eu07_td_departure_station_capitals` runs an EU07 from where `td_timetable.scn` puts it, with the W4 of Stacja1 about 35 m ahead, on a timetable of its own (`td_departure_station_capitals.txt`, fragment `td_departure_station.scn`): @@ -259,6 +264,13 @@ standstill of 100 s ends the test although the traffic runs on (counting every v into the test timeout). `test_mode_pass_while_moving` reports the pass from `event2` of `tor1k` (the direction the train runs), as the train runs onto it, with `test.no_movement_before_pass no`, so the test ends with the train still moving. +`test_mode_standing_1m_past_signal` and `test_mode_standing_4m_past_signal` (fragment `td_past_red_signal.scn`) set +the td/rozklad train at a stand with its head 1 m and 4 m past the exit signal at stop (the memory cell of the +signal is 51 m into `tor1k`). The driver finds the signal behind the head at its first look ahead, 13.6 s in, and a +standing train passes nothing, so both run into their timeout. When a signal found up to 5 m behind the head +still counted as one the head had ahead, standing or not, both ended as signals passed at danger at 13.6 s (a +train set 6 m or 20 m past didn't). Starting at 0 km/h the driver doesn't wake up within the case, so they start at +0.1 km/h, as `td_red_signal.scn` does. The `en57_2000_automatic_coupler_*` cases (issue #86) share `td_automatic_coupler_hit.scn`: an EN57-2000 unit without a driver runs at `vars.velocity` km/h into a standing one, from diff --git a/tests/scenarios/cases/eu07_td_shunt_push_red_signal.json b/tests/scenarios/cases/eu07_td_shunt_push_red_signal.json new file mode 100644 index 000000000..243d37641 --- /dev/null +++ b/tests/scenarios/cases/eu07_td_shunt_push_red_signal.json @@ -0,0 +1,24 @@ +{ + "description": "EU07 shunting on td.scn (no timetable, so its AI drives under Shunt), pushing three coaches with no route set, the first coach starting at 30 km/h about 150 m before the exit signal stacja1_j at stop: stops before the signal and stays there.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_shunt_red_signal.scn", + "vars": { + "offset": "0.0", + "velocity": "30", + "consist": [ + "node -1 0 {{vehicle}}_w1 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608068-6 BDHPUMN_TAPICERKA_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}}_w2 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608011-6 BDHPUMN_NG_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}}_w3 dynamic pkp\\bdhpumn_v3 bdhpumn_50512608068-6 BDHPUMN_TAPICERKA_1 0 nobody 115 0 enddynamic", + "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + ] + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "max_speed", "limit": 40 }, + { "type": "stops", "moving_speed": 5, "stop_speed": 0.5, "hold": 5 }, + { "type": "stays_stopped", "moving_speed": 5, "stop_speed": 0.5 }, + { "type": "distance", "max": 150 } + ] +} diff --git a/tests/scenarios/cases/eu07_td_shunt_red_signal.json b/tests/scenarios/cases/eu07_td_shunt_red_signal.json new file mode 100644 index 000000000..bc23046cd --- /dev/null +++ b/tests/scenarios/cases/eu07_td_shunt_red_signal.json @@ -0,0 +1,19 @@ +{ + "description": "EU07 shunting on td.scn (no timetable, so its AI drives under Shunt) with no route set, starting at 30 km/h about 150 m before the exit signal stacja1_j at stop: stops before the signal and stays there.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_shunt_red_signal.scn", + "vars": { + "offset": "0.0", + "velocity": "30", + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "max_speed", "limit": 40 }, + { "type": "stops", "moving_speed": 5, "stop_speed": 0.5, "hold": 5 }, + { "type": "stays_stopped", "moving_speed": 5, "stop_speed": 0.5 }, + { "type": "distance", "max": 150 } + ] +} diff --git a/tests/scenarios/cases/td_past_red_signal.scn b/tests/scenarios/cases/td_past_red_signal.scn new file mode 100644 index 000000000..e5e68da5f --- /dev/null +++ b/tests/scenarios/cases/td_past_red_signal.scn @@ -0,0 +1,7 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed. +// the timetable train of td.scn (td/rozklad) set at a stand past the exit signal stacja1_j, which stays at stop +// (no route): the head {{offset}} m into tor1k, whose memory cell of the signal is 51 m into the track. the train +// starts at 0.1 km/h, as in td_red_signal.scn: set at 0 its AI driver doesn't wake up within the case +trainset td/rozklad tor1k {{offset}} 0.1 +{{consist}} +endtrainset diff --git a/tests/scenarios/cases/td_shunt_red_signal.scn b/tests/scenarios/cases/td_shunt_red_signal.scn new file mode 100644 index 000000000..649427247 --- /dev/null +++ b/tests/scenarios/cases/td_shunt_red_signal.scn @@ -0,0 +1,7 @@ +// added by tests/scenarios/run.py to td.scn, whose own trainsets were removed. +// like td_red_signal_moving.scn (no route, the exit signal stacja1_j about 150 m ahead of the start of +// tdo_n1734 stays at stop), but the train has no timetable, so its AI driver shunts (Shunt), and it +// starts {{offset}} m into tdo_n1734, already moving at {{velocity}} km/h. +trainset none tdo_n1734 {{offset}} {{velocity}} +{{consist}} +endtrainset diff --git a/tests/scenarios/cases/test_mode_standing_1m_past_signal.json b/tests/scenarios/cases/test_mode_standing_1m_past_signal.json new file mode 100644 index 000000000..4157dc52d --- /dev/null +++ b/tests/scenarios/cases/test_mode_standing_1m_past_signal.json @@ -0,0 +1,16 @@ +{ + "description": "Test mode, no signal passed at danger for a train set past a signal: EU07 on td.scn standing with its head 1 m past the exit signal stacja1_j at stop (no route). Its driver finds the signal behind the head at its first look ahead, about 13 s in; a standing train passes nothing, so it isn't a signal passed at danger, and the case runs into its timeout.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_past_red_signal.scn", + "vars": { + "offset": "52.0", + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "duration": 60, + "timeout": 600, + "checks": [ + { "type": "max_speed", "limit": 1 }, + { "type": "distance", "max": 1 } + ] +} diff --git a/tests/scenarios/cases/test_mode_standing_4m_past_signal.json b/tests/scenarios/cases/test_mode_standing_4m_past_signal.json new file mode 100644 index 000000000..b0b668873 --- /dev/null +++ b/tests/scenarios/cases/test_mode_standing_4m_past_signal.json @@ -0,0 +1,16 @@ +{ + "description": "Test mode, no signal passed at danger for a train set past a signal: EU07 on td.scn standing with its head 4 m past the exit signal stacja1_j at stop (no route). Its driver finds the signal behind the head at its first look ahead, about 13 s in; a standing train passes nothing, so it isn't a signal passed at danger, and the case runs into its timeout.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_past_red_signal.scn", + "vars": { + "offset": "55.0", + "consist": "node -1 0 {{vehicle}} dynamic pkp\\4e_v1 4e-004 4e 0 headdriver 0 0 enddynamic" + }, + "duration": 60, + "timeout": 600, + "checks": [ + { "type": "max_speed", "limit": 1 }, + { "type": "distance", "max": 1 } + ] +} diff --git a/tests/unit/test_signal_passing.cpp b/tests/unit/test_signal_passing.cpp new file mode 100644 index 000000000..16d3cd9eb --- /dev/null +++ b/tests/unit/test_signal_passing.cpp @@ -0,0 +1,128 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/signalpassing.h" + +// when a signal in the AI's speed table counts as passed by the train (TController::CheckSignalPassed): the test +// mode reports a signal at stop passed that way as "passed at danger", the blackbox records every passed signal. a +// train set or standing past a signal at the start of a scenario didn't pass it; within 5 m of the signal the test +// mode used to report it all the same. + +namespace { + +// a point of the speed table the way TSpeedPos keeps it: found by the scan of the route, then updated as the head +// moves on (TController::TableCheck: UpdateDistance, Update, CheckSignalPassed) +struct table_point { + double distance; // ahead of the head [m], negative behind it + bool seen_ahead; + bool elapsed{false}; + int passes{0}; // reports of the point passed + + table_point(double const Distance, bool const Standing) + : distance(Distance), seen_ahead(signal_passing::seen_ahead_when_found(Distance, Standing)) { + move(0.0); // the first update comes with the next check of the table, the train hasn't moved yet + } + + // the head moves Metres on, and the table updates the point + void move(double const Metres) { + distance -= Metres; + auto const elapsedbefore{elapsed}; + elapsed = elapsed || distance < 0.0; + seen_ahead = signal_passing::seen_ahead_on_update(distance, seen_ahead); + if (signal_passing::passed_now(elapsedbefore, elapsed, seen_ahead)) { + ++passes; + } + } + + // the head moves Metres on in steps of a metre + void run(double const Metres) { + for (int moved{0}; moved < Metres; ++moved) { + move(1.0); + } + } +}; + +auto constexpr standing{true}; +auto constexpr moving{false}; + +} // namespace + +TEST_CASE("signal passing: a train standing past a signal when its table is traced hasn't passed it") { + auto behind{0.0}; + // 1-4.5 m: reported as passed at danger before; 20 m: the train standing across the signal of #409 + SUBCASE("0.5 m") { behind = 0.5; } + SUBCASE("1 m") { behind = 1.0; } + SUBCASE("2 m") { behind = 2.0; } + SUBCASE("3 m") { behind = 3.0; } + SUBCASE("4 m") { behind = 4.0; } + SUBCASE("4.9 m") { behind = 4.9; } + SUBCASE("20 m") { behind = 20.0; } + CAPTURE(behind); + + table_point signal{-behind, standing}; + CHECK_FALSE(signal.seen_ahead); + // standing there a while, then pulling away from it + for (auto check{0}; check < 10; ++check) { + signal.move(0.0); + } + signal.run(200.0); + CHECK(signal.passes == 0); +} + +TEST_CASE("signal passing: a train standing short of a signal passes it when it moves on") { + auto ahead{0.0}; + SUBCASE("at the signal") { ahead = 0.0; } + SUBCASE("0.5 m") { ahead = 0.5; } + SUBCASE("10 m") { ahead = 10.0; } + SUBCASE("150 m") { ahead = 150.0; } + CAPTURE(ahead); + + table_point signal{ahead, standing}; + CHECK(signal.seen_ahead); + signal.move(0.0); + CHECK(signal.passes == 0); + signal.run(ahead + 50.0); + CHECK(signal.passes == 1); +} + +TEST_CASE("signal passing: a moving train passing a signal its table finds just behind the head passes it") { + // the table traced anew in motion, the head right at the signal: the distance is found in steps of a metre, + // so a signal just ahead may come out a little behind + auto behind{0.0}; + SUBCASE("0.5 m") { behind = 0.5; } + SUBCASE("3 m") { behind = 3.0; } + SUBCASE("at the tolerance") { behind = signal_passing::moving_tolerance; } + CAPTURE(behind); + + table_point signal{-behind, moving}; + CHECK(signal.seen_ahead); + CHECK(signal.passes == 1); + signal.run(100.0); + CHECK(signal.passes == 1); +} + +TEST_CASE("signal passing: a signal found further behind a moving train than the tolerance wasn't passed by it") { + table_point signal{-(signal_passing::moving_tolerance + 0.5), moving}; + CHECK_FALSE(signal.seen_ahead); + signal.run(100.0); + CHECK(signal.passes == 0); +} + +TEST_CASE("signal passing: a train that overruns a signal and stops past it passes it once") { + // the overrun counts while the train moves; a table traced again with the train standing 3 m past the signal + // (a change of direction and back, a new order) doesn't count it again + table_point approach{150.0, moving}; + approach.run(153.0); + CHECK(approach.passes == 1); + + table_point traced_again{-3.0, standing}; + traced_again.run(50.0); + CHECK(traced_again.passes == 0); +} + +TEST_CASE("signal passing: the report of a pass needs the point to become elapsed in this very update") { + CHECK(signal_passing::passed_now(false, true, true)); + CHECK_FALSE(signal_passing::passed_now(true, true, true)); // elapsed before + CHECK_FALSE(signal_passing::passed_now(false, false, true)); // still ahead + CHECK_FALSE(signal_passing::passed_now(false, true, false)); // the head never had it ahead +} diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 99c6a951e..a25ed392c 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/brakesetting.h" #include "vehicle/brakingcurve.h" #include "vehicle/handover.h" +#include "vehicle/signalpassing.h" #include "vehicle/timetableservice.h" #include "world/mtable.h" #include "world/stationnames.h" @@ -330,9 +331,7 @@ bool TSpeedPos::Update() // trzeba zazanaczyć, że minięty iFlags |= spElapsed; } - else { - bSeenAhead = true; - } + bSeenAhead = signal_passing::seen_ahead_on_update( fDist, bSeenAhead ); if (iFlags & spTrack) { // road/track if (trTrack) { @@ -418,18 +417,14 @@ bool TSpeedPos::IsProperSemaphor(TOrders order) return false; // true gdy zatrzymanie, wtedy nie ma po co skanować dalej } -// how far behind the head an event found in the scan may lie and still count as one the head had ahead [m] -constexpr double SeenAheadTolerance { 5.0 }; - -bool TSpeedPos::Set(basic_event *event, double dist, double length, TOrders order) +bool TSpeedPos::Set(basic_event *event, double dist, double length, TOrders order, bool const Standing) { // zapamiętanie zdarzenia fDist = dist; - // an event already behind the head when found (under a train set across a signal) wasn't passed by it. the - // distance is found in steps of a metre from the position of the memory cell, so a signal just ahead of the head - // may come out a few metres behind it: those still count as seen ahead. the price is that a train set less than - // the tolerance past a signal at stop is still reported to pass it; a train standing that close to a signal can't - // be told from one stopped just short of it, which really passes the signal if it goes on - bSeenAhead = ( dist >= -SeenAheadTolerance ); + // an event already behind the head when found (under a train set across a signal) wasn't passed by it. a moving + // train may be just passing it, and the distance is found in steps of a metre, so for a moving train an event a + // little behind the head still counts as seen ahead. a standing train passes nothing: an event found behind its + // head counts only once an update finds it ahead + bSeenAhead = signal_passing::seen_ahead_when_found( dist, Standing ); iFlags = spEvent; evEvent = event; vPos = event->input_location(); // współrzędne eventu albo komórki pamięci (zrzutować na tor?) @@ -646,7 +641,7 @@ void TController::TableTraceRoute(double fDistance, TDynamicObject *pVehicle) fCurrentDistance + ProjectEventOnTrack( pEvent, pTrack, fLastDir ), OrderCurrentGet() ) ) { */ - if (newspeedpoint.Set(pEvent, GetDistanceToEvent(pTrack, pEvent, fLastDir, fCurrentDistance), fLength, OrderCurrentGet())) + if (newspeedpoint.Set(pEvent, GetDistanceToEvent(pTrack, pEvent, fLastDir, fCurrentDistance), fLength, OrderCurrentGet(), mvOccupied->Vel < EU07_AI_NOMOVEMENT)) { fDistance = newspeedpoint.fDist; // jeśli sygnał stop, to nie ma potrzeby dalej skanować @@ -922,9 +917,8 @@ std::uint8_t passing_flags_of( TSpeedPos const &Point, double const Speedatsigna void TController::CheckSignalPassed( TSpeedPos const &Point, bool const Passedbefore ) const { - if( Passedbefore - || false == TestFlag( Point.iFlags, spElapsed ) - || false == Point.bSeenAhead // found already behind the head: the train stood across it, it didn't pass it + // not bSeenAhead: found already behind the head, the train stood across it or past it, it didn't pass it + if( false == signal_passing::passed_now( Passedbefore, TestFlag( Point.iFlags, spElapsed ), Point.bSeenAhead ) || ( mvOccupied->CategoryFlag & 1 ) == 0 ) { // railway only return; } diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 0aaf70707..87dccd030 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -184,7 +184,8 @@ class TSpeedPos void UpdateDistance( double dist ) { fDist -= dist; } - bool Set(basic_event *e, double d, double length, TOrders order = Wait_for_orders); + // Standing: the train stands, so an event found behind its head wasn't passed by it + bool Set(basic_event *e, double d, double length, TOrders order = Wait_for_orders, bool Standing = false); void Set(TTrack *t, double d, int f); std::string TableText() const; std::string GetName() const; diff --git a/vehicle/signalpassing.cpp b/vehicle/signalpassing.cpp new file mode 100644 index 000000000..58d691760 --- /dev/null +++ b/vehicle/signalpassing.cpp @@ -0,0 +1,30 @@ +/* +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/signalpassing.h" + +namespace signal_passing { + +bool seen_ahead_when_found( double const Distance, bool const Standing ) { + + return Distance >= ( Standing ? 0.0 : -moving_tolerance ); +} + +bool seen_ahead_on_update( double const Distance, bool const Seenahead ) { + + return Seenahead || Distance >= 0.0; +} + +bool passed_now( bool const Elapsedbefore, bool const Elapsed, bool const Seenahead ) { + + return Elapsed && !Elapsedbefore && Seenahead; +} + +} // namespace signal_passing diff --git a/vehicle/signalpassing.h b/vehicle/signalpassing.h new file mode 100644 index 000000000..33f2e5265 --- /dev/null +++ b/vehicle/signalpassing.h @@ -0,0 +1,38 @@ +/* +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 + +// when a point of the AI's speed table (a signal above all) counts as passed by the train: the test mode's +// "signal passed at danger" and the blackbox record of passed signals (TController::CheckSignalPassed) +namespace signal_passing { + +// how far behind the head a point found by the scan of a moving train may lie and still count as one the head +// had ahead [m]. GetDistanceToEvent() finds the distance in steps of a metre from the position of the memory +// cell, so a signal just ahead of the head may come out a little behind it +inline constexpr double moving_tolerance { 5.0 }; + +// whether a point the scan finds Distance [m] ahead of the head (negative: behind it) counts as one the head had +// ahead. a moving train may be just passing it, so a point up to moving_tolerance behind counts. a standing train +// isn't passing anything: a point found behind its head was passed before (a train set or stopped past a signal), +// and counts only once an update finds it ahead. +// known gap: the distance can be off by about half a metre (GetDistanceToEvent() takes the nearest of samples a +// metre apart), so a train standing a few decimetres short of a signal may find it just behind the head, and if it +// then runs past it at stop, that isn't reported. a tolerance for a standing train wouldn't close it: the first +// update marks a point behind the head elapsed (TSpeedPos::Update()), so such a train would be reported as passing +// the signal at once, while it stands short of it. the AI stops 8-15 m short of a signal +bool seen_ahead_when_found( double Distance, bool Standing ); + +// whether the head had the point ahead, after an update finds it Distance [m] ahead of the head +bool seen_ahead_on_update( double Distance, bool Seenahead ); + +// whether an update of a point reports it as passed: it became elapsed in this update, and the head had it ahead +bool passed_now( bool Elapsedbefore, bool Elapsed, bool Seenahead ); + +} // namespace signal_passing