18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 11:47:06 +01:00
Files
maszyna/tests/unit/test_drivingaid.cpp
Hirek 23b695752a refactor(physics): the demand of the integrated controller is private to the vehicle
eimic_real, the demand of the integrated controller the vehicle goes by with
the speed control counted, which TDynamicObject and the consist braking set
for the vehicle that leads, is private: EimicReal() and SetEimicReal(). The
field of the same name of drivingaid::input stays as it is.

No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
2026-11-29 13:39:07 +01:00

177 lines
7.5 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "vehicle.h"
#include "real_vehicles.h"
#include "vehicle/drivingaid.h"
#include "physics/brakes.h"
#include <algorithm>
#include <cmath>
#include <tuple>
// the numbers of the driving aid panel's throttle and brakes lines: the ED braking shows (eu07/maszyna#558) and
// the local brake is left out where the cab has no control of it (eu07/maszyna#555)
using test::definition;
using test::vehicle;
namespace {
// the cab controls of the vehicles here, as their cab definitions (.mmd) have them: the Vectron has a local brake
// lever and a joint controller whose braking range is its ED lever, the Traxx and the EU07 a local brake lever,
// the SM42 a joint controller whose braking range is the local brake, none of the 45 Elf cabs either of them
constexpr drivingaid::cab_controls vectron_cab{true, true};
constexpr drivingaid::cab_controls lever_cab{true, false};
constexpr drivingaid::cab_controls jointctrl_cab{false, true};
constexpr drivingaid::cab_controls elf_cab{false, false};
drivingaid::readout read(vehicle &Vehicle, drivingaid::cab_controls const &Cab) {
return drivingaid::compute(drivingaid::from_vehicles(Vehicle.physics(), Vehicle.physics(), Cab));
}
// what the panel showed before: the throttle and brakes numbers, and the local brake always
drivingaid::readout before(physics::Vehicle const &Physics) {
drivingaid::readout result;
result.throttle_first = Physics.EIMCtrlType > 0 ? std::max(0, static_cast<int>(100.4 * Physics.EimicReal())) : Physics.MainCtrlPos();
result.throttle_second = Physics.EIMCtrlType > 0 ? Physics.MainCtrlPos() : Physics.ScndCtrlPos();
result.brake_first = Physics.UniCtrlIntegratedBrakeCtrl ? std::max(0.0, -100.0 * Physics.EimicReal()) : Physics.brakes.HandlePositionSmooth();
result.local_brake = Physics.brakes.IndependentPosition() * LocalBrakePosNo;
return result;
}
test::track const line{0.15, 0.0, test::catenary_3kv};
} // namespace
TEST_CASE("driving aid: the Vectron's ED lever shows as a negative throttle step, and its ED braking") {
vehicle vectron{definition::fiz("vectron_v1/wariant_7w"), 80.0 /* km/h */, "vectron", 1};
REQUIRE(vectron->brakes.electrodynamic.splitFromPneumatic);
REQUIRE(vectron->brakes.electrodynamic.controllerPositions == 15);
REQUIRE(vectron->EIMCtrlType == 0);
// lever at zero: the master controller, as before, and no ED braking
auto const idle{read(vectron, vectron_cab)};
CHECK(idle.throttle_first == vectron->MainCtrlPos());
CHECK_FALSE(idle.ed_percent.has_value());
CHECK(idle.show_local_brake);
// six steps of the ED lever, as the cab's joint controller shows them
for (int step = 0; step < 6; ++step) {
REQUIRE(vectron->IncDynamicBrakeLevel(1.f));
}
vectron.run(1.0, line);
REQUIRE(vectron->Vel > 15.0); // above Vh1, where the ED brake has its full demand
auto const braking{read(vectron, vectron_cab)};
CHECK(braking.throttle_first == -6);
REQUIRE(vectron->EimicReal() == doctest::Approx(-0.4));
REQUIRE(braking.ed_percent.has_value());
CHECK(*braking.ed_percent == 40);
// the train brake handle and the local brake, as before
CHECK(braking.brake_first == doctest::Approx(vectron->brakes.HandlePositionSmooth()));
CHECK(braking.show_local_brake);
CHECK(braking.local_brake == doctest::Approx(0.0));
// the joint controller's braking range is the ED lever, so it isn't a local brake control
CHECK_FALSE(read(vectron, jointctrl_cab).show_local_brake);
}
TEST_CASE("driving aid: the Traxx's ED braking from its master controller") {
vehicle traxx{definition::fiz("e186_v2/eu47"), 80.0 /* km/h */, "traxx", 1};
REQUIRE(traxx->EIMCtrlType == 1);
REQUIRE_FALSE(traxx->brakes.electrodynamic.splitFromPneumatic);
CHECK_FALSE(read(traxx, lever_cab).ed_percent.has_value());
// B+: the braking demand ramps towards full
traxx->SetMainCtrlPos(0);
traxx.run(3.0, line);
REQUIRE(traxx->EimicReal() < -0.2);
auto const braking{read(traxx, lever_cab)};
REQUIRE(braking.ed_percent.has_value());
CHECK(*braking.ed_percent == std::lround(-100.0 * traxx->EimicReal()));
// the drive demand stays at zero while braking, with the controller position next to it, as before
CHECK(braking.throttle_first == 0);
CHECK(braking.throttle_second == 0);
CHECK(braking.show_local_brake);
}
TEST_CASE("driving aid: the Elf leaves out the local brake it has no control of, and shows its ED braking") {
vehicle elf{definition::fiz("elf_v1/21we-a"), 60.0 /* km/h */, "elf", 1};
REQUIRE(elf->EIMCtrlType == 2);
// the definition has a local brake (LocalBrake=PneumaticBrake, LocBrakeHandle=FD1), the cab no control of it
REQUIRE(elf->brakes.config.localKind == TLocalBrake::PneumaticBrake);
auto const idle{read(elf, elf_cab)};
CHECK_FALSE(idle.show_local_brake);
CHECK_FALSE(idle.ed_percent.has_value());
// the controller at its lowest braking position: the demand ramps towards full braking
elf->SetMainCtrlPos(0);
elf.run(2.0, line);
REQUIRE(elf->EimicReal() < -0.1);
auto const braking{read(elf, elf_cab)};
REQUIRE(braking.ed_percent.has_value());
CHECK(*braking.ed_percent == std::lround(-100.0 * elf->EimicReal()));
CHECK(braking.throttle_first == 0);
CHECK(braking.throttle_second == 0);
CHECK_FALSE(braking.show_local_brake);
// applied after all (the AI, or the keys), it shows
elf->brakes.SetIndependentPosition(0.3);
auto const applied{read(elf, elf_cab)};
CHECK(applied.show_local_brake);
CHECK(applied.local_brake == doctest::Approx(3.0));
}
TEST_CASE("driving aid: a vehicle without an EIM controller shows what it did") {
vehicle eu07{definition::fiz("4e_v1/4e"), 60.0 /* km/h */, "eu07", 1};
eu07->SetMainCtrlPos(5);
eu07->brakes.SetIndependentPosition(0.2);
auto const readout{read(eu07, lever_cab)};
CHECK(readout.throttle_first == 5);
CHECK(readout.throttle_second == eu07->ScndCtrlPos());
CHECK(readout.local_brake == doctest::Approx(2.0));
CHECK(readout.show_local_brake);
CHECK_FALSE(readout.ed_percent.has_value());
// the SM42's local brake is the braking range of its joint controller
vehicle sm42{definition::fiz("sm42_v1/6d1"), 0.0, "sm42", 1};
CHECK(read(sm42, jointctrl_cab).show_local_brake);
CHECK_FALSE(read(sm42, elf_cab).show_local_brake);
}
TEST_CASE("driving aid: the integrated universal controller shows the ED demand as its brake number, once") {
drivingaid::input input;
input.eim = true;
input.eimctrltype = 3;
input.integratedbrake = true;
input.eimic_real = -0.35;
input.brakectrlpos = 2.0;
auto const readout{drivingaid::compute(input)};
CHECK(readout.brake_first == doctest::Approx(35.0));
CHECK_FALSE(readout.ed_percent.has_value());
CHECK(readout.throttle_first == 0);
}
TEST_CASE("driving aid: every real vehicle with a local brake control shows the numbers it showed before") {
// with the ED lever of a split ED brake at zero, the change only adds the ED part and leaves out the local brake
// of a cab without a control of it
for (auto const &path : test::real_vehicles()) {
CAPTURE(path);
test::reseed_random();
vehicle real{definition::fiz(path), 40.0 /* km/h */, "real", 1};
for (auto const &[mainctrl, localbrake, eimic] : {std::tuple{0, 0.0, 0.0}, std::tuple{1, 0.4, -0.25}, std::tuple{1, 0.0, 0.6}}) {
real->SetMainCtrlPos(std::min(mainctrl, real->MainCtrlPosNo));
real->brakes.SetIndependentPosition(localbrake);
real->SetEimicReal(eimic);
auto const now{read(real, lever_cab)};
auto const was{before(real.physics())};
CHECK(now.throttle_first == was.throttle_first);
CHECK(now.throttle_second == was.throttle_second);
CHECK(now.brake_first == doctest::Approx(was.brake_first));
CHECK(now.local_brake == doctest::Approx(was.local_brake));
CHECK(now.show_local_brake);
}
}
}