From 3ccd4fe73ff4a9cdd2f5fe12c1b6854e9d9fd03a Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 25 Nov 2026 05:15:27 +0100 Subject: [PATCH] fix(ai): read the motor parameters of ESMVelocity() within the table (#486) * fix(ai): read the motor parameters of ESMVelocity() within the table TController::ESMVelocity() indexed MotorParam with the field shunting step of the next master controller position. For a position with a step of its own that index is RList[].ScndAct, the sixth column of the RList rows of the .fiz, which readRList() takes as it is. A step of 11 or more read past the 11 entries of MotorParam. The pole flux is now read through motor_param(), which keeps the index inside the table, as MomentumF() in the same loop and Momentum() already do. Nothing changes for a .fiz with valid steps. The unit tests show the loader keeping an out-of-range ScndAct and motor_param() clamping it; ESMVelocity() itself is in Driver.cpp, which the unit tests don't build. * test(scenarios): a Traxx, with a brake handle with a time chamber, starting up None of the cases had a driver's brake handle with a time chamber (MHZ_K5P, MHZ_6P, M394, Knorr, St113, FVel6, FVE408), for which the AI getting the vehicle ready has a rule of its own for releasing the train brake (5775493b2). Three cases run a Traxx (E186, MHZ_6P) on td.scn: the timetable run from a trainset ready to depart, the same from a cold start, and the locomotive standing ready to depart with nobody in it until a driver is put in. In each the driver puts the handle to running and leaves. * test: read the EP09 definition into a json with '=', not braces GCC takes nlohmann::ordered_json in braces for an initializer list and makes an array holding the parsed object, so at("RList") threw on the Linux build (type_error 304); MSVC kept the object. --- tests/scenarios/README.md | 10 ++++ tests/scenarios/cases/traxx_td_crew_add.json | 31 ++++++++++ tests/scenarios/cases/traxx_td_timetable.json | 17 ++++++ .../cases/traxx_td_timetable_cold.json | 20 +++++++ tests/unit/test_motor_param_bounds.cpp | 57 +++++++++++++++++++ vehicle/Driver.cpp | 6 +- 6 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 tests/scenarios/cases/traxx_td_crew_add.json create mode 100644 tests/scenarios/cases/traxx_td_timetable.json create mode 100644 tests/scenarios/cases/traxx_td_timetable_cold.json create mode 100644 tests/unit/test_motor_param_bounds.cpp diff --git a/tests/scenarios/README.md b/tests/scenarios/README.md index bf5467018..5ddc77dca 100644 --- a/tests/scenarios/README.md +++ b/tests/scenarios/README.md @@ -242,6 +242,16 @@ stop, starting `vars.offset` m into `tdo_n1734`: `eu07_td_shunt_red_signal` alon 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. +The `traxx_*` cases run a Traxx (E186, `p160dc`), whose driver's brake handle (MHZ_6P) has a time +chamber, like the MHZ_K5P, M394, Knorr, St113, FVel6 and FVE408 handles, which no other case has: the +AI getting such a locomotive ready has a rule of its own for releasing the train brake (5775493b2, +#444). `traxx_td_timetable` is the timetable run from a trainset ready to depart, +`traxx_td_timetable_cold` the same from a cold start (`td_cold_timetable.scn`, the handle in its +holding position, the brake pipe low), and `traxx_td_crew_add` the locomotive standing ready to depart +with nobody in it, the handle in its holding position and the brake pipe charged, until a driver is +put in (`td_crew.scn`, as `eu07_td_crew_add`). In all three the driver puts the handle to running +within a second of starting and leaves. + `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`): its first row, the station of departure, gives no arrival time and is written in capitals (`STACJA1`), the relation diff --git a/tests/scenarios/cases/traxx_td_crew_add.json b/tests/scenarios/cases/traxx_td_crew_add.json new file mode 100644 index 000000000..9d6200a1b --- /dev/null +++ b/tests/scenarios/cases/traxx_td_crew_add.json @@ -0,0 +1,31 @@ +{ + "description": "As eu07_td_crew_add, with a Traxx (E186, p160dc) standing ready to depart (trainset velocity 0.1) with nobody in it: its brake handle, one with a time chamber (MHZ_6P), in the holding position and the brake pipe and the control reservoir at running pressure. The AI driver put in cab A 20 s after the start gets the locomotive ready, puts the handle to running and leaves through the exit signal.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_crew.scn", + "vars": { + "trainsets": [ + "trainset none tdo_n1734 0.0 0.1", + "node -1 0 {{vehicle}} dynamic pkp/e186_v2 146_226-6 p160dc 0 nobody 0 0 enddynamic", + "endtrainset" + ], + "crew": [ + "event autotest_crew_put putvalues 0 none 0 0 0 Crew:{{vehicle}} 1 0 endevent", + "event autotest_crew_on multiple 20 none autotest_crew_put autotest_crew_waits endevent", + "node -1 0 autotest_crew_on_launcher eventlauncher 0 0 0 -1 none 1034 autotest_crew_on end" + ] + }, + "track_events": { + "tdo_n1734": "event0 autotest_wake" + }, + "duration": 300, + "timeout": 600, + "checks": [ + { "type": "log_contains", "pattern": "Crew: \\[autotest_traxx_td_crew_add\\] an AI driver takes cab A" }, + { "type": "driver_changes", "vehicle": "{{vehicle}}", "to": "ai", "after": 15, "before": 30 }, + { "type": "starts", "min_speed": 20, "within": 240 }, + { "type": "moves", "vehicle": "{{vehicle}}", "direction": "front", "min_speed": 20 }, + { "type": "log_lacks", "pattern": "derailed" }, + { "type": "log_lacks", "pattern": "collided" } + ] +} diff --git a/tests/scenarios/cases/traxx_td_timetable.json b/tests/scenarios/cases/traxx_td_timetable.json new file mode 100644 index 000000000..51d84860b --- /dev/null +++ b/tests/scenarios/cases/traxx_td_timetable.json @@ -0,0 +1,17 @@ +{ + "description": "Traxx (E186, p160dc) on td.scn, AI driver on the td/rozklad timetable: a driver's brake handle with a time chamber (MHZ_6P), for which the AI getting the locomotive ready has a rule of its own for releasing the train brake (5775493b2, #444). Starts from Stacja1, keeps to the speed limit, brakes to a stop at Przystanek.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp/e186_v2 146_226-6 p160dc 0 headdriver 0 0 enddynamic" + }, + "duration": 240, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/scenarios/cases/traxx_td_timetable_cold.json b/tests/scenarios/cases/traxx_td_timetable_cold.json new file mode 100644 index 000000000..bfdb833a2 --- /dev/null +++ b/tests/scenarios/cases/traxx_td_timetable_cold.json @@ -0,0 +1,20 @@ +{ + "description": "As traxx_td_timetable, from a cold start: the trainset stands with velocity 0, so the locomotive starts braked, its brake handle (MHZ_6P, with a time chamber) in the holding position and the brake pipe below running pressure. The driver gets the order to run as a train, starts the locomotive, puts the handle to running, charges the brake pipe and leaves on time.", + "base_scenery": "td.scn", + "time": "10:34", + "fragment": "td_cold_timetable.scn", + "vars": { + "consist": "node -1 0 {{vehicle}} dynamic pkp/e186_v2 146_226-6 p160dc 0 headdriver 0 0 enddynamic" + }, + "track_events": { + "tdo_n1734": "event0 autotest_wake" + }, + "duration": 300, + "timeout": 600, + "checks": [ + { "type": "starts", "min_speed": 20, "within": 150 }, + { "type": "max_speed", "limit": 90, "tolerance": 3 }, + { "type": "brakes", "min_pressure": 0.5 }, + { "type": "stops", "moving_speed": 20, "stop_speed": 1.0, "hold": 0, "min_distance": 1380, "max_distance": 1420 } + ] +} diff --git a/tests/unit/test_motor_param_bounds.cpp b/tests/unit/test_motor_param_bounds.cpp new file mode 100644 index 000000000..5d44c7331 --- /dev/null +++ b/tests/unit/test_motor_param_bounds.cpp @@ -0,0 +1,57 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +#include + +#include + +using test::definition; +using test::vehicle; + +// the field shunting step a position of the master controller selects (RList, its sixth column ScndAct) indexes the +// motor parameter table, MotorParam, which has MotorParametersArraySize + 1 entries. The loader takes ScndAct from the +// .fiz as it is, so the readers of the table (TMoverParameters::Momentum(), MomentumF(), TController::ESMVelocity()) +// go through motor_param(), which keeps the index inside the table + +namespace { + +// the RList rows of the EP09 with the ScndAct of the first position replaced +nlohmann::ordered_json ep09_rlist_with_scndact(std::string const &Scndact) { + std::ifstream file{std::string{EU07_UNIT_TESTS_DATA} + "/fiz/ep09_v2/104e.json"}; + REQUIRE(file.good()); + // '=' rather than braces: GCC takes a json in braces for an initializer list, an array holding it + auto const ep09 = nlohmann::ordered_json::parse(file); + auto rows = ep09.at("RList").at("rows"); + REQUIRE(rows.size() > 1); + rows[1][5] = Scndact; + return rows; +} + +} // namespace + +TEST_CASE("motor parameters: the ScndAct of an RList row is read from the .fiz as it is") { + vehicle ep09{definition::fiz("ep09_v2/104e")}; + // the first position of the EP09 runs on field shunting step 2 + CHECK(ep09->RList[1].ScndAct == 2); + CHECK(&ep09->motor_param(ep09->RList[1].ScndAct) == &ep09->MotorParam[2]); + + // a .fiz giving a step beyond the table: nothing checks it on loading + vehicle broken{definition::fiz("ep09_v2/104e").set("RList", "rows", ep09_rlist_with_scndact("20"))}; + CHECK(broken->RList[1].ScndAct == 20); + CHECK(broken->RList[1].ScndAct > MotorParametersArraySize); +} + +TEST_CASE("motor parameters: motor_param() keeps a step from the .fiz inside the table") { + vehicle broken{definition::fiz("ep09_v2/104e").set("RList", "rows", ep09_rlist_with_scndact("20"))}; + auto const &table{broken->MotorParam}; + // past the end: the last entry, rather than memory beyond the table + CHECK(&broken->motor_param(broken->RList[1].ScndAct) == &table[MotorParametersArraySize]); + CHECK(&broken->motor_param(MotorParametersArraySize + 1) == &table[MotorParametersArraySize]); + // below the start: the first entry + CHECK(&broken->motor_param(-1) == &table[0]); + // inside the table: the entry itself + for (int step = 0; step <= MotorParametersArraySize; ++step) { + CHECK(&broken->motor_param(step) == &table[step]); + } +} diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 5630cdea1..086ff43ad 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2726,8 +2726,10 @@ double TController::ESMVelocity(bool Main) } IF = std::min(IF, mvControlling->Imax*fCurrentCoeff); double R = mvControlling->RList[MCPN].R + mvControlling->CircuitRes + mvControlling->RList[MCPN].Mn*mvControlling->WindingRes; - double pole = mvControlling->MotorParam[SCPN].fi * - std::max(abs(IF) / (abs(IF) + mvControlling->MotorParam[SCPN].Isat) - mvControlling->MotorParam[SCPN].fi0, 0.0); + // SCPN may come from the .fiz (RList ScndAct) unchecked: read it with the bounds-safe accessor, as MomentumF() does + auto const &motorparam{mvControlling->motor_param(SCPN)}; + double pole = motorparam.fi * + std::max(abs(IF) / (abs(IF) + motorparam.Isat) - motorparam.fi0, 0.0); double Us = abs(mvControlling->EngineVoltage) - IF*R; double ns = std::max(0.0, Us / (pole*mvControlling->RList[MCPN].Mn)); ESMVel = ns * mvControlling->WheelDiameter*M_PI*3.6/mvControlling->Transmision.Ratio;