18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 04:35:02 +01:00

Merge pull request #385 from MaSzyna-EU07/feat/blackbox-standalone

feat(blackbox): the black box on its own, lighter physics logs, physicslog.trains
This commit is contained in:
2026-11-16 16:01:52 +00:00
committed by GitHub
18 changed files with 860 additions and 93 deletions

View File

@@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/.
#include "world/Track.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/physicslog.h"
#include "utilities/utilities.h"
#include <algorithm>
@@ -42,6 +43,7 @@ namespace {
// [s] between the samples of the user's train, the looks at everything else and the full looks at the vehicles
constexpr double cabinterval{0.1};
constexpr double tapeinterval{0.05}; // as the physics logs (deltalog of Driver.cpp)
constexpr double lookinterval{1.0};
constexpr double slowlook{20.0}; // [ms] a look around taking longer is told in the log, part by part
constexpr double keyframeinterval{60.0};
@@ -233,15 +235,25 @@ void blackbox_recorder::update(double const Deltatime) {
if (false == simulation::is_ready)
return;
m_started = true;
if (false == WriteLogFlag || false == Global.BlackBox || Global.editor_session)
// "blackbox yes" or "no" of eu07.ini decides by itself, with no entry it goes with the physics logs
if (false == Global.BlackBox.value_or(WriteLogFlag || WriteTrainLogFlag) || Global.editor_session)
return;
m_taping = true;
open();
}
if (m_writer == nullptr)
if (false == m_taping)
return;
std::scoped_lock lock{m_mutex};
m_time += Deltatime;
// with the physics logs on, each driver writes its own all the time. the tapes are files of their own: they go on
// after the recording ends in the editor, and with a recording which couldn't be made
if (false == WriteLogFlag && m_time >= m_tapedue) {
keep_tape_line();
m_tapedue = std::max(m_tapedue + tapeinterval, m_time);
}
if (m_writer == nullptr)
return;
if (m_time >= m_cabdue) {
write_cab();
m_cabdue = std::max(m_cabdue + cabinterval, m_time);
@@ -410,6 +422,13 @@ void blackbox_recorder::write_cab() {
sample.vehicle = vehicle_id(vehicle);
sample.position = vehicle->GetPosition();
sample.speed_kmh = static_cast<float>(occupied->V * 3.6);
// of the wheels as they turn, which the speedometer shows: apart from that of the vehicle when they slip or skid.
// those of the powered vehicle, as the speedometer has it (TTrain::DynamicSet), which in a unit driven from an
// unpowered car is another one, maybe facing the other way: signed as the occupied one's speed
// the vehicle itself, if none in the unit is powered
auto const *powered{train->Dynamic()->FindPowered()};
auto const facing{powered->DirectionGet() * vehicle->DirectionGet()};
sample.wheel_speed_kmh = static_cast<float>(facing * M_PI * powered->MoverParameters->WheelDiameter * powered->MoverParameters->nrot * 3.6);
sample.acceleration = static_cast<float>(controlling->AccS);
sample.lateral_acceleration = static_cast<float>(occupied->AccN);
if (owner != nullptr && owner->is_train()) {
@@ -478,6 +497,27 @@ void blackbox_recorder::write_cab() {
m_writer->cab(sample);
}
void blackbox_recorder::keep_tape_line() {
auto const *train{simulation::Train};
if (train == nullptr || train->Dynamic() == nullptr)
return;
auto const *vehicle{train->Dynamic()};
// the driver of the vehicle, whose log it would be: it keeps the name of its first vehicle, so a change of the cab
// in a unit goes on in the same log. a passive one stands still (its clock too), so then the driver of the train
auto const *driver{vehicle->Mechanik != nullptr && vehicle->Mechanik->primary() ? vehicle->Mechanik : vehicle->ctOwner};
if (driver == nullptr)
return;
// a driver writing its own, of a train with "physicslog.trains yes", isn't written twice
if (driver->writes_physicslog())
return;
auto const &name{driver->physicslog_name()};
if (m_tapes.insert(name).second) {
PhysicsLogs.begin(name, TController::physicslog_header());
WriteLog(std::format("Black box: the physics log \"{}\" begun", PhysicsLogs.shown(name)));
}
PhysicsLogs.append(name, driver->physicslog_line());
}
void blackbox_recorder::look_around(bool const Keyframe) {
auto const &time{simulation::Time.data()};
m_writer->clock(m_time, static_cast<std::uint16_t>(time.wYear), static_cast<std::uint16_t>(simulation::Time.year_day()), second_of_day());

View File

@@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/.
#include <fstream>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
@@ -31,13 +32,14 @@ enum class user_command;
namespace simulation {
// the black box of the session (see blackbox_doc.md): written along with the physics logs, in driver mode,
// from the moment the scenario is loaded until the simulator closes or the editor is entered
// from the moment the scenario is loaded until the simulator closes or the editor is entered. the tapes of the user's
// vehicles, with the physics logs off, go on after the editor until the simulator closes
class blackbox_recorder {
public:
// starts the recording once the scenario is ready, if it is to be made, then records what is due.
// Deltatime: simulation time passed since the last call
void update(double Deltatime);
// writes the end of the recording and closes the file. a recording closed isn't started again
// writes the end of the recording and closes the file. a recording closed isn't started again; its tapes go on
void close();
// what happens between the looks, from wherever it happens; the cabs may run on threads of their own.
@@ -86,6 +88,10 @@ private:
void write_map();
static std::vector<std::pair<glm::dvec3, float>> stop_point_headings();
void write_cab();
// with the physics logs off, the line of the log of the driver the user is with, to its log, physicslog/<vehicle>.dat,
// as the physics logs would have it. a driver's log is begun at the first line given for that driver, and goes on
// while the user looks around outside or is in another vehicle of that driver; back with it, after the gap
void keep_tape_line();
// the clock, the user's vehicle, the vehicles, the switches and the signals
void look_around(bool Keyframe);
void look_at_vehicles(bool Keyframe);
@@ -107,8 +113,12 @@ private:
std::ofstream m_file;
std::unique_ptr<blackbox::writer> m_writer;
bool m_started{false}; // tried to start, made or not
bool m_taping{false}; // the recording was to be made: its tapes are kept from then on, the file made or not, closed or not
double m_time{0.0};
double m_cabdue{0.0};
double m_tapedue{0.0};
// the names of the logs begun by the recording
std::set<std::string> m_tapes;
double m_lookdue{0.0};
double m_keyframedue{0.0};
std::unordered_map<TDynamicObject const *, vehicle_entry> m_vehicles;

View File

@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include "simulation/simulation.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/physicslog.h"
#include "utilities/utilities.h"
#include "vehicle/DynObj.h"
#include "world/Event.h"
@@ -261,7 +262,9 @@ void test_manager::check_wall_clock(double const Elapsed)
// but which doesn't close ends with its result
if (false == conclude(test_result::timeout, std::format("still running after {:.0f} s of real time (test.wall_timeout)", limit)))
ErrorLog(std::format("test: the simulator was still running {:.0f} s of real time after the start, ended", limit));
// what was logged last gets written, unless the log is held up by a hung thread
// what was logged last gets written, unless the log is held up by a hung thread, or the physics logs by a writer
// holding their files or a disk which doesn't answer: the run ends anyway, two seconds later at most
PhysicsLogs.flush_for(std::chrono::seconds{2});
StopLogService();
std::this_thread::sleep_for(std::chrono::milliseconds(250));
std::_Exit(exit_code());