18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 10:55:01 +01:00

feat(blackbox): the black box on its own, writing the tapes of the user's vehicles at the end

"blackbox yes" in eu07.ini records the black box with the physics logs on or off, "blackbox no" never;
with no entry it goes with "physicslog yes" as before. With the physics logs off, the recorder keeps the
line of the physics log of the driver the user is with, every 0.05 s, and writes it to
physicslog/<vehicle>.dat when the recording closes: the speed recorder's tape of the vehicles the user
was in, without every vehicle keeping a log open and written twenty times a second.

The line and the header of the log are made by one function of the driver, which its own log writes
too, and the file is opened the same way, so the two are the same to the byte: the readers of these
logs (the old Hasler tape viewer, the starter) read them as before.
This commit is contained in:
maj00r
2026-11-14 00:04:55 +01:00
parent 0488519f39
commit 5ca2f7e38f
7 changed files with 126 additions and 54 deletions

View File

@@ -7,10 +7,17 @@ done at the same moment, every command the user gave, when the autopilot took ov
the timetable and how the train kept to it at each station, and where it was told to sound the horn. From it another program (the
starter, a viewer) can replay the session on the map, or set the user's driving against the AI's.
It is written along with the physics logs: `physicslog yes` in `eu07.ini`, not in an editor session.
`blackbox no` turns it off while the physics logs are kept (it is on by default). The file goes next to
them, `physicslog/<scenery>_<YYYY-MM-DD_HH-MM-SS>.blackbox` (the local time the recording started at).
The `.dat` logs stay as they are.
`blackbox yes` in `eu07.ini` writes it, with the physics logs on or off; `blackbox no` never does. With no
`blackbox` entry it is written along with the physics logs, `physicslog yes`. It is never written in an
editor session. The file goes to `physicslog/<scenery>_<YYYY-MM-DD_HH-MM-SS>.blackbox` (the local time the
recording started at), next to the `.dat` logs of the vehicles when those are on; they stay as they are.
With the physics logs off it still gives the `.dat` log of each vehicle the user was in, written when the
recording ends: `physicslog/<vehicle>.dat`, the same file name, the same columns and the same line every
0.05 s as `physicslog yes` would write, but only of the times the user was there. A vehicle with no driver
of its own takes the log of the driver of its train. So the speed recorder's tape can be read from them as
before, without the simulator keeping a log of every vehicle open all the time. A simulator that crashes
leaves the recording, but not these logs.
The recording starts once the scenery is loaded and ends when the simulator closes. Going into the
editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no

View File

@@ -42,6 +42,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,7 +234,8 @@ 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) || Global.editor_session)
return;
open();
}
@@ -246,6 +248,11 @@ void blackbox_recorder::update(double const Deltatime) {
write_cab();
m_cabdue = std::max(m_cabdue + cabinterval, m_time);
}
// with the physics logs on, each driver writes its own all the time
if (false == WriteLogFlag && m_time >= m_tapedue) {
keep_tape_line();
m_tapedue = std::max(m_tapedue + tapeinterval, m_time);
}
if (m_time >= m_cameradue) {
write_camera();
m_cameradue = std::max(m_cameradue + camerainterval, m_time);
@@ -273,6 +280,7 @@ void blackbox_recorder::close() {
m_writer->end(m_time);
m_writer.reset();
m_file.close();
write_tapes();
using milliseconds = std::chrono::duration<double, std::milli>;
WriteLog(std::format("Black box: recording closed; {} looks around, {:.3f} ms each on average, the longest {:.3f} ms", m_looks,
m_looks > 0 ? milliseconds{m_looktime}.count() / m_looks : 0.0, milliseconds{m_looklongest}.count()));
@@ -478,6 +486,38 @@ 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. in a vehicle without one, the driver of its train
auto const *driver{vehicle->Mechanik != nullptr ? vehicle->Mechanik : vehicle->ctOwner};
if (driver == nullptr)
return;
m_tapes[driver->physicslog_name()] += driver->physicslog_line();
}
void blackbox_recorder::write_tapes() {
for (auto const &[name, lines] : m_tapes) {
// opened as the driver opens its log, so the file is the same to the byte
auto const filename{"physicslog/" + name + ".dat"};
std::fstream file{filename, std::ios::in | std::ios::out | std::ios::trunc};
if (false == file.is_open()) {
ErrorLog(std::format("Black box: can't create the physics log \"{}\"", filename));
continue;
}
file << TController::physicslog_header() << lines;
file.close();
if (file.fail())
ErrorLog(std::format("Black box: the physics log \"{}\" wasn't written whole", filename));
else
WriteLog(std::format("Black box: the physics log \"{}\" written from the recording", filename));
}
m_tapes.clear();
}
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

@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include <chrono>
#include <cstdint>
#include <fstream>
#include <map>
#include <memory>
#include <mutex>
#include <string>
@@ -86,6 +87,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, kept for the end
void keep_tape_line();
// the kept lines, each driver's to its log, physicslog/<vehicle>.dat, as the physics logs would have it
void write_tapes();
// the clock, the user's vehicle, the vehicles, the switches and the signals
void look_around(bool Keyframe);
void look_at_vehicles(bool Keyframe);
@@ -109,6 +114,9 @@ private:
bool m_started{false}; // tried to start, made or not
double m_time{0.0};
double m_cabdue{0.0};
double m_tapedue{0.0};
// the lines of the physics log by the name of its file, of the times the user was with its driver
std::map<std::string, 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

@@ -540,7 +540,9 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string&
if (token == "blackbox")
{
ParseOne(Parser, BlackBox);
bool blackbox{false};
ParseOne(Parser, blackbox);
BlackBox = blackbox;
return true;
}
@@ -1735,7 +1737,9 @@ global_settings::export_as_text( std::ostream &Output ) const {
export_as_text( Output, "sound.volume.ambient", EnvironmentAmbientVolume );
export_as_text( Output, "sound.asyncload", AudioAsyncLoad );
export_as_text( Output, "physicslog", WriteLogFlag );
export_as_text( Output, "blackbox", BlackBox );
if( BlackBox ) {
export_as_text( Output, "blackbox", *BlackBox );
}
export_as_text( Output, "fullphysics", FullPhysics );
export_as_text( Output, "debuglog", iWriteLogEnabled );
export_as_text( Output, "multiplelogs", MultipleLogs );

View File

@@ -113,7 +113,9 @@ struct global_settings {
bool priorityLoadText3D{false}; // ladowanie T3D priorytetowo
int iWriteLogEnabled{ 3 }; // maska bitowa: 1-zapis do pliku, 2-okienko, 4-nazwy torów
bool MultipleLogs{ false };
bool BlackBox{ true }; // with the physics logs on, the black box of the session is written as well (blackbox_doc.md)
// the black box of the session (blackbox_doc.md): "blackbox yes" writes it whatever the physics logs, "blackbox no"
// never, and with no entry it is written along with the physics logs
std::optional<bool> BlackBox;
bool ShowSystemConsole{ false }; // toggle visibility of the OS console window with log output
unsigned int DisabledLogTypes{ 0 };
bool ParserLogIncludes{ true };

View File

@@ -2344,16 +2344,7 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche,
LogFile.open( std::string( "physicslog/" + VehicleName + ".dat" ),
std::ios::in | std::ios::out | std::ios::trunc );
#if LOGPRESS == 0
LogFile
<< "Time[s] Velocity[m/s] Acceleration[m/ss] "
<< "Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] BrakeForce[kN] "
<< "BrakePress[MPa] PipePress[MPa] MotorCurrent[A] "
<< "MCP SCP BCP LBP Direction Command CVal1 CVal2 "
<< "Security Wheelslip "
<< "EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg] "
<< "VehicleVelocity[km/h]"
<< "\r\n";
LogFile << std::fixed << std::setprecision( 4 );
LogFile << physicslog_header();
#endif
#if LOGPRESS == 1
LogFile << string( "t\tVel\tAcc\tPP\tVVP\tBP\tBVP\tCVP" ).c_str() << "\n";
@@ -5367,47 +5358,59 @@ bool TController::PutCommand( std::string NewCommand, double NewValue1, double N
return false; // nierozpoznana - wysłać bezpośrednio do pojazdu
};
std::string_view TController::physicslog_header() {
// the columns of the readers of these logs, the Hasler tape of the starter among them, stay where they are:
// a new one goes at the end
return "Time[s] Velocity[m/s] Acceleration[m/ss] "
"Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] BrakeForce[kN] "
"BrakePress[MPa] PipePress[MPa] MotorCurrent[A] "
"MCP SCP BCP LBP Direction Command CVal1 CVal2 "
"Security Wheelslip "
"EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg] "
"VehicleVelocity[km/h]"
"\r\n";
}
std::string TController::physicslog_line() const {
std::ostringstream line;
line << std::fixed << std::setprecision( 4 )
<< ElapsedTime << " "
<< fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot) << " "
<< mvControlling->AccS << " "
<< mvOccupied->Couplers[1].Dist << " "
<< mvOccupied->Couplers[1].CForce << " "
<< mvOccupied->Ft << " "
<< mvOccupied->Ff << " "
<< mvOccupied->Fb << " "
<< mvOccupied->BrakePress << " "
<< mvOccupied->PipePress << " "
<< mvControlling->Im << " "
<< int(mvControlling->MainCtrlPos) << " "
<< int(mvControlling->ScndCtrlPos) << " "
<< mvOccupied->fBrakeCtrlPos << " "
<< mvOccupied->LocalBrakePosA << " "
<< int(mvControlling->DirActive) << " "
<< ( mvOccupied->CommandIn.Command.empty() ? "none" : mvOccupied->CommandIn.Command.c_str() ) << " "
<< mvOccupied->CommandIn.Value1 << " "
<< mvOccupied->CommandIn.Value2 << " "
<< int(mvControlling->SecuritySystem.is_blinking()) << " "
<< int(mvControlling->SlippingWheels) << " "
<< mvControlling->dizel_heat.Ts << " "
<< mvControlling->dizel_heat.To << " "
<< mvControlling->dizel_heat.temperatura1 << " "
<< mvControlling->dizel_heat.temperatura2 << " "
// vehicle speed [km/h], the second column is the wheel speed, which stays at 0 while the wheels are locked in a skid
<< mvOccupied->V * 3.6
<< "\r\n";
return line.str();
}
void TController::PhysicsLog()
{ // zapis logu - na razie tylko wypisanie parametrów
if (LogFile.is_open())
{
#if LOGPRESS == 0
/*
<< "Time[s] Velocity[m/s] Acceleration[m/ss] "
<< "Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] BrakeForce[kN] "
<< "BrakePress[MPa] PipePress[MPa] MotorCurrent[A] "
<< "MCP SCP BCP LBP DmgFlag Command CVal1 CVal2 "
<< "EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg]"
*/
LogFile
<< ElapsedTime << " "
<< fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot) << " "
<< mvControlling->AccS << " "
<< mvOccupied->Couplers[1].Dist << " "
<< mvOccupied->Couplers[1].CForce << " "
<< mvOccupied->Ft << " "
<< mvOccupied->Ff << " "
<< mvOccupied->Fb << " "
<< mvOccupied->BrakePress << " "
<< mvOccupied->PipePress << " "
<< mvControlling->Im << " "
<< int(mvControlling->MainCtrlPos) << " "
<< int(mvControlling->ScndCtrlPos) << " "
<< mvOccupied->fBrakeCtrlPos << " "
<< mvOccupied->LocalBrakePosA << " "
<< int(mvControlling->DirActive) << " "
<< ( mvOccupied->CommandIn.Command.empty() ? "none" : mvOccupied->CommandIn.Command.c_str() ) << " "
<< mvOccupied->CommandIn.Value1 << " "
<< mvOccupied->CommandIn.Value2 << " "
<< int(mvControlling->SecuritySystem.is_blinking()) << " "
<< int(mvControlling->SlippingWheels) << " "
<< mvControlling->dizel_heat.Ts << " "
<< mvControlling->dizel_heat.To << " "
<< mvControlling->dizel_heat.temperatura1 << " "
<< mvControlling->dizel_heat.temperatura2 << " "
// vehicle speed [km/h], the second column is the wheel speed, which stays at 0 while the wheels are locked in a skid
<< mvOccupied->V * 3.6
<< "\r\n";
LogFile << physicslog_line();
#endif
#if LOGPRESS == 1
LogFile << ElapsedTime << "\t" << fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot)

View File

@@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/.
#pragma once
#include <string>
#include <string_view>
#include "utilities/Classes.h"
#include "utilities/utilities.h"
#include "MOVER.h"
@@ -730,6 +731,13 @@ private:
public:
TDynamicObject const *Vehicle() const {
return pVehicle; }
// the physics log (physicslog/<vehicle>.dat): the name of its file without the extension, its first line and
// a line of it as the vehicle is now, each with its line end. the black box keeps the lines of the vehicle the
// user is in, to write its log at the end when the logs of all the vehicles are off
std::string const &physicslog_name() const {
return VehicleName; }
static std::string_view physicslog_header();
std::string physicslog_line() const;
TDynamicObject *Vehicle( end const Side ) const {
return pVehicles[ Side ]; }
private: