Add drivetrain replay application
This adds a basic example application for replaying the 2019 drivetrain.
I also make it so that the LogReader will stop event processing once it
is done.
Change-Id: Icd0187f240191b8d23e31608eb6e5a5a49fd068c
diff --git a/aos/events/logging/logger.cc b/aos/events/logging/logger.cc
index 486cda9..d71c2d2 100644
--- a/aos/events/logging/logger.cc
+++ b/aos/events/logging/logger.cc
@@ -307,6 +307,10 @@
}
timer_handler_ = event_loop_->AddTimer([this]() {
+ if (sorted_message_reader_.active_channel_count() == 0u) {
+ event_loop_factory_->Exit();
+ return;
+ }
monotonic_clock::time_point channel_timestamp;
int channel_index;
FlatbufferVector<MessageHeader> channel_data =
@@ -358,6 +362,11 @@
if (sorted_message_reader_.active_channel_count() > 0u) {
timer_handler_->Setup(sorted_message_reader_.oldest_message().first);
+ } else {
+ // Set a timer up immediately after now to die. If we don't do this, then
+ // the senders waiting on the message we just read will never get called.
+ timer_handler_->Setup(monotonic_now + event_loop_factory_->send_delay() +
+ std::chrono::nanoseconds(1));
}
});
diff --git a/aos/events/simulated_event_loop.cc b/aos/events/simulated_event_loop.cc
index 1dfb3af..bfe0de7 100644
--- a/aos/events/simulated_event_loop.cc
+++ b/aos/events/simulated_event_loop.cc
@@ -467,6 +467,10 @@
}
}
+std::chrono::nanoseconds SimulatedEventLoopFactory::send_delay() const {
+ return send_delay_;
+}
+
void SimulatedEventLoop::MakeRawWatcher(
const Channel *channel,
std::function<void(const Context &channel, const void *message)> watcher) {
diff --git a/aos/events/simulated_event_loop.h b/aos/events/simulated_event_loop.h
index db0b840..99450a3 100644
--- a/aos/events/simulated_event_loop.h
+++ b/aos/events/simulated_event_loop.h
@@ -71,6 +71,7 @@
// Sets the simulated send delay for the factory.
void set_send_delay(std::chrono::nanoseconds send_delay);
+ std::chrono::nanoseconds send_delay() const;
// Returns the node that this factory is running as, or nullptr if this is a
// single node setup.
diff --git a/y2019/control_loops/drivetrain/BUILD b/y2019/control_loops/drivetrain/BUILD
index 1575584..bc76d7a 100644
--- a/y2019/control_loops/drivetrain/BUILD
+++ b/y2019/control_loops/drivetrain/BUILD
@@ -214,3 +214,21 @@
"//frc971/control_loops/drivetrain:drivetrain_test_lib",
],
)
+
+cc_binary(
+ name = "drivetrain_replay",
+ srcs = ["drivetrain_replay.cc"],
+ data = ["//y2019:config.json"],
+ deps = [
+ ":drivetrain_base",
+ ":event_loop_localizer",
+ "//aos:configuration",
+ "//aos:init",
+ "//aos/events:shm_event_loop",
+ "//aos/events:simulated_event_loop",
+ "//aos/events/logging:logger",
+ "//frc971/control_loops/drivetrain:drivetrain_lib",
+ "@com_github_gflags_gflags//:gflags",
+ "@com_github_google_glog//:glog",
+ ],
+)
diff --git a/y2019/control_loops/drivetrain/drivetrain_replay.cc b/y2019/control_loops/drivetrain/drivetrain_replay.cc
new file mode 100644
index 0000000..cb07b68
--- /dev/null
+++ b/y2019/control_loops/drivetrain/drivetrain_replay.cc
@@ -0,0 +1,62 @@
+#include <iostream>
+
+#include "aos/configuration.h"
+#include "aos/events/logging/logger.h"
+#include "aos/events/simulated_event_loop.h"
+#include "aos/init.h"
+#include "aos/json_to_flatbuffer.h"
+#include "aos/network/team_number.h"
+#include "frc971/control_loops/drivetrain/drivetrain.h"
+#include "gflags/gflags.h"
+#include "y2019/control_loops/drivetrain/drivetrain_base.h"
+#include "y2019/control_loops/drivetrain/event_loop_localizer.h"
+
+DEFINE_string(logfile, "/tmp/logfile.bfbs",
+ "Name of the logfile to read from.");
+DEFINE_string(config, "y2019/config.json",
+ "Name of the config file to replay using.");
+DEFINE_string(output_file, "/tmp/replayed.bfbs",
+ "Name of the logfile to write replayed data to.");
+DEFINE_int32(team, 971, "Team number to use for logfile replay.");
+int main(int argc, char **argv) {
+ aos::InitGoogle(&argc, &argv);
+
+ aos::network::OverrideTeamNumber(FLAGS_team);
+
+ aos::InitCreate();
+
+ const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
+ aos::configuration::ReadConfig(FLAGS_config);
+
+ aos::logger::LogReader reader(FLAGS_logfile, &config.message());
+ // TODO(james): Actually enforce not sending on the same buses as the logfile
+ // spews out.
+ reader.RemapLoggedChannel("/drivetrain",
+ "frc971.control_loops.drivetrain.Status");
+ reader.RemapLoggedChannel("/drivetrain",
+ "frc971.control_loops.drivetrain.Output");
+ reader.Register();
+
+ aos::logger::DetachedBufferWriter file_writer(FLAGS_output_file);
+ std::unique_ptr<aos::EventLoop> log_writer_event_loop =
+ reader.event_loop_factory()->MakeEventLoop("log_writer");
+ log_writer_event_loop->SkipTimingReport();
+ CHECK(nullptr == log_writer_event_loop->node());
+ aos::logger::Logger writer(&file_writer, log_writer_event_loop.get());
+
+ std::unique_ptr<aos::EventLoop> drivetrain_event_loop =
+ reader.event_loop_factory()->MakeEventLoop("drivetrain");
+ drivetrain_event_loop->SkipTimingReport();
+
+ y2019::control_loops::drivetrain::EventLoopLocalizer localizer(
+ drivetrain_event_loop.get(),
+ y2019::control_loops::drivetrain::GetDrivetrainConfig());
+ frc971::control_loops::drivetrain::DrivetrainLoop drivetrain(
+ y2019::control_loops::drivetrain::GetDrivetrainConfig(),
+ drivetrain_event_loop.get(), &localizer);
+
+ reader.event_loop_factory()->Run();
+
+ aos::Cleanup();
+ return 0;
+}