-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Signed-off-by: Timm Ruppert <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// Copyright (c) 2024, Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
// SPDX-License-Identifier: MPL-2.0 | ||
// | ||
#include <osi-utilities/tracefile/writer/txthTraceFileWriter.h> | ||
|
||
#include <filesystem> | ||
|
||
#include "osi_sensordata.pb.h" | ||
#include "osi_version.pb.h" | ||
|
||
std::string GenerateTempFilePath() { | ||
const auto path = std::filesystem::temp_directory_path() / "example_txth_writer.txth"; | ||
return path.string(); | ||
} | ||
|
||
int main(int argc, const char** argv) { | ||
std::cout << "Starting TXTH Writer example:" << std::endl; | ||
|
||
// Create writer and open file | ||
auto tracefile_writer = osi3::txthTraceFileWriter(); | ||
const auto tracefile_path = GenerateTempFilePath(); | ||
std::cout << "Creating tracefile at " << tracefile_path << std::endl; | ||
tracefile_writer.Open(tracefile_path); | ||
|
||
// create OSI data to store | ||
const auto osi_version = osi3::InterfaceVersion::descriptor()->file()->options().GetExtension(osi3::current_interface_version); | ||
|
||
osi3::SensorView sensor_view; | ||
sensor_view.mutable_version()->CopyFrom(osi_version); | ||
sensor_view.mutable_sensor_id()->set_value(0); | ||
|
||
auto* const ground_truth = sensor_view.mutable_global_ground_truth(); | ||
ground_truth->mutable_version()->CopyFrom(osi_version); | ||
|
||
auto* const host_vehicle = ground_truth->mutable_moving_object()->Add(); | ||
host_vehicle->mutable_id()->set_value(12); | ||
host_vehicle->mutable_vehicle_classification()->set_type(osi3::MovingObject_VehicleClassification_Type_TYPE_SMALL_CAR); | ||
host_vehicle->mutable_base()->mutable_dimension()->set_length(5); | ||
host_vehicle->mutable_base()->mutable_dimension()->set_width(2); | ||
host_vehicle->mutable_base()->mutable_dimension()->set_height(1.5); | ||
host_vehicle->mutable_base()->mutable_velocity()->set_x(10.0); | ||
|
||
// write the data continuously in a loop | ||
constexpr double kTimeStepSizeS = 0.1; // NOLINT | ||
for (int i = 0; i < 10; ++i) { | ||
// manipulate the data so not every message is the same | ||
auto timestamp = sensor_view.timestamp().seconds() * 1000000000 + sensor_view.timestamp().nanos(); | ||
timestamp += 100000000; | ||
sensor_view.mutable_timestamp()->set_nanos(timestamp % 1000000000); | ||
sensor_view.mutable_timestamp()->set_seconds(timestamp / 1000000000); | ||
ground_truth->mutable_timestamp()->set_nanos(timestamp % 1000000000); | ||
ground_truth->mutable_timestamp()->set_seconds(timestamp / 1000000000); | ||
const auto old_position = host_vehicle->base().position().x(); | ||
const auto new_position = old_position + host_vehicle->base().velocity().x() * kTimeStepSizeS; | ||
host_vehicle->mutable_base()->mutable_position()->set_x(new_position); | ||
|
||
// write the data | ||
tracefile_writer.WriteMessage(sensor_view); | ||
} | ||
|
||
tracefile_writer.Close(); | ||
|
||
std::cout << "Finished TXTH Writer example" << std::endl; | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Copyright (c) 2024, Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
// SPDX-License-Identifier: MPL-2.0 | ||
// | ||
|
||
|
||
#ifndef OSIUTILITIES_TRACEFILE_WRITER_TXTHTRACEFILEWRITER_H_ | ||
#define OSIUTILITIES_TRACEFILE_WRITER_TXTHTRACEFILEWRITER_H_ | ||
|
||
#include "../Writer.h" | ||
#include <google/protobuf/text_format.h> | ||
#include <fstream> | ||
|
||
namespace osi3 { | ||
|
||
class txthTraceFileWriter final : public TraceFileWriter { | ||
Check warning on line 16 in include/osi-utilities/tracefile/writer/txthTraceFileWriter.h GitHub Actions / lintinclude/osi-utilities/tracefile/writer/txthTraceFileWriter.h:16:7 [readability-identifier-naming]
|
||
public: | ||
bool Open(const std::string& filename) override; | ||
void Close() override; | ||
bool SetMetadata(const std::string& name, const std::unordered_map<std::string, std::string>& metadata_entries) { return false; } | ||
Check warning on line 20 in include/osi-utilities/tracefile/writer/txthTraceFileWriter.h GitHub Actions / lintinclude/osi-utilities/tracefile/writer/txthTraceFileWriter.h:20:10 [cppcoreguidelines-explicit-virtual-functions]
Check warning on line 20 in include/osi-utilities/tracefile/writer/txthTraceFileWriter.h GitHub Actions / lintinclude/osi-utilities/tracefile/writer/txthTraceFileWriter.h:20:10 [cppcoreguidelines-explicit-virtual-functions]
|
||
|
||
template <typename T> | ||
bool WriteMessage(T top_level_message); | ||
|
||
private: | ||
std::ofstream trace_file_; | ||
bool file_open_{false}; | ||
}; | ||
|
||
} // namespace osi3 | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// Copyright (c) 2024, Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
// SPDX-License-Identifier: MPL-2.0 | ||
// | ||
|
||
#include "osi-utilities/tracefile/writer/txthTraceFileWriter.h" | ||
|
||
#include "osi_groundtruth.pb.h" | ||
#include "osi_hostvehicledata.pb.h" | ||
#include "osi_motionrequest.pb.h" | ||
#include "osi_sensordata.pb.h" | ||
#include "osi_sensorview.pb.h" | ||
#include "osi_streamingupdate.pb.h" | ||
#include "osi_trafficcommand.pb.h" | ||
#include "osi_trafficcommandupdate.pb.h" | ||
#include "osi_trafficupdate.pb.h" | ||
|
||
namespace osi3 { | ||
|
||
bool txthTraceFileWriter::Open(const std::string& filename) { | ||
if (filename.substr(filename.length() - 5) != ".txth") { | ||
std::cerr << "Error: Filename must end with .txth extension\n"; | ||
return false; | ||
} | ||
|
||
trace_file_.open(filename); | ||
if (trace_file_.is_open()) { | ||
file_open_ = true; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void txthTraceFileWriter::Close() { | ||
if (file_open_) { | ||
trace_file_.close(); | ||
file_open_ = false; | ||
} | ||
} | ||
|
||
template <typename T> | ||
bool txthTraceFileWriter::WriteMessage(T top_level_message) { | ||
if (!file_open_) { | ||
std::cerr << "Error: Cannot write message, file is not open\n"; | ||
return false; | ||
} | ||
|
||
std::string text_output; | ||
if (!google::protobuf::TextFormat::PrintToString(top_level_message, &text_output)) { | ||
std::cerr << "Error: Failed to convert message to text format\n"; | ||
return false; | ||
} | ||
|
||
trace_file_ << text_output; | ||
return true; | ||
} | ||
|
||
// Template instantiations for allowed OSI top-level messages | ||
template bool txthTraceFileWriter::WriteMessage<osi3::GroundTruth>(osi3::GroundTruth); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::SensorData>(osi3::SensorData); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::SensorView>(osi3::SensorView); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::HostVehicleData>(osi3::HostVehicleData); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::TrafficCommand>(osi3::TrafficCommand); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::TrafficCommandUpdate>(osi3::TrafficCommandUpdate); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::TrafficUpdate>(osi3::TrafficUpdate); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::MotionRequest>(osi3::MotionRequest); | ||
template bool txthTraceFileWriter::WriteMessage<osi3::StreamingUpdate>(osi3::StreamingUpdate); | ||
|
||
} // namespace osi3 |