blob: 1acdce4853d85921f3258129d1350c99a73afd18 [file] [log] [blame]
James Kuszmaul022d40e2020-02-11 17:06:18 -08001// This binary allows us to replay the drivetrain code over existing logfile,
2// primarily for use in testing changes to the localizer code.
3// When you run this code, it generates a new logfile with the data all
4// replayed, so that it can then be run through the plotting tool or analyzed
5// in some other way. The original drivetrain status data will be on the
6// /original/drivetrain channel.
7#include "aos/configuration.h"
8#include "aos/events/logging/logger.h"
9#include "aos/events/simulated_event_loop.h"
10#include "aos/init.h"
11#include "aos/json_to_flatbuffer.h"
12#include "aos/network/team_number.h"
13#include "frc971/control_loops/drivetrain/drivetrain.h"
14#include "gflags/gflags.h"
15#include "y2020/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul68f7d272020-02-22 20:55:02 -080016#include "y2020/control_loops/drivetrain/localizer.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080017
James Kuszmaul022d40e2020-02-11 17:06:18 -080018DEFINE_string(config, "y2020/config.json",
19 "Name of the config file to replay using.");
James Kuszmaul4f106fb2021-01-05 20:53:02 -080020DEFINE_string(output_file, "/tmp/replayed",
21 "Name of the folder to write replayed logs to.");
James Kuszmaul022d40e2020-02-11 17:06:18 -080022DEFINE_int32(team, 971, "Team number to use for logfile replay.");
James Kuszmaul4f106fb2021-01-05 20:53:02 -080023
24class LoggerState {
25 public:
26 LoggerState(aos::logger::LogReader *reader, const aos::Node *node)
27 : event_loop_(
28 reader->event_loop_factory()->MakeEventLoop("logger", node)),
29 namer_(std::make_unique<aos::logger::MultiNodeLogNamer>(
30 absl::StrCat(FLAGS_output_file, "/", node->name()->string_view(),
31 "/"),
32 event_loop_->configuration(), node)),
33 logger_(std::make_unique<aos::logger::Logger>(event_loop_.get())) {
34 event_loop_->SkipTimingReport();
35 event_loop_->OnRun([this]() {
36 logger_->StartLogging(std::move(namer_), aos::UUID::Zero().string_view());
37 });
38 }
39
40 private:
41 std::unique_ptr<aos::EventLoop> event_loop_;
42 std::unique_ptr<aos::logger::LogNamer> namer_;
43 std::unique_ptr<aos::logger::Logger> logger_;
44};
45
46// TODO(james): Currently, this replay produces logfiles that can't be read due
47// to time estimation issues. Pending the active refactorings of the
48// timestamp-related code, fix this.
James Kuszmaul022d40e2020-02-11 17:06:18 -080049int main(int argc, char **argv) {
50 aos::InitGoogle(&argc, &argv);
51
52 aos::network::OverrideTeamNumber(FLAGS_team);
53
James Kuszmaul022d40e2020-02-11 17:06:18 -080054 const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
55 aos::configuration::ReadConfig(FLAGS_config);
56
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080057 // find logfiles
58 std::vector<std::string> unsorted_logfiles =
59 aos::logger::FindLogs(argc, argv);
60
61 // sort logfiles
James Kuszmaul4f106fb2021-01-05 20:53:02 -080062 const std::vector<aos::logger::LogFile> logfiles =
63 aos::logger::SortParts(unsorted_logfiles);
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080064
65 // open logfiles
66 aos::logger::LogReader reader(logfiles, &config.message());
James Kuszmaul022d40e2020-02-11 17:06:18 -080067 // TODO(james): Actually enforce not sending on the same buses as the logfile
68 // spews out.
69 reader.RemapLoggedChannel("/drivetrain",
70 "frc971.control_loops.drivetrain.Status");
71 reader.RemapLoggedChannel("/drivetrain",
72 "frc971.control_loops.drivetrain.Output");
73 reader.Register();
74
James Kuszmaul4f106fb2021-01-05 20:53:02 -080075 // List of nodes to create loggers for (note: currently just roborio; this
76 // code was refactored to allow easily adding new loggers to accommodate
77 // debugging and potential future changes).
78 const std::vector<std::string> nodes_to_log = {"roborio"};
79 std::vector<std::unique_ptr<LoggerState>> loggers;
80 for (const std::string& node : nodes_to_log) {
81 loggers.emplace_back(std::make_unique<LoggerState>(
82 &reader,
83 aos::configuration::GetNode(reader.configuration(), node)));
84 }
85
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080086 const aos::Node *node = nullptr;
87 if (aos::configuration::MultiNode(reader.configuration())) {
88 node = aos::configuration::GetNode(reader.configuration(), "roborio");
89 }
90
James Kuszmaul022d40e2020-02-11 17:06:18 -080091 std::unique_ptr<aos::EventLoop> drivetrain_event_loop =
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080092 reader.event_loop_factory()->MakeEventLoop("drivetrain", node);
James Kuszmaul022d40e2020-02-11 17:06:18 -080093 drivetrain_event_loop->SkipTimingReport();
94
James Kuszmaul68f7d272020-02-22 20:55:02 -080095 y2020::control_loops::drivetrain::Localizer localizer(
James Kuszmaul022d40e2020-02-11 17:06:18 -080096 drivetrain_event_loop.get(),
97 y2020::control_loops::drivetrain::GetDrivetrainConfig());
98 frc971::control_loops::drivetrain::DrivetrainLoop drivetrain(
99 y2020::control_loops::drivetrain::GetDrivetrainConfig(),
100 drivetrain_event_loop.get(), &localizer);
101
102 reader.event_loop_factory()->Run();
103
James Kuszmaul022d40e2020-02-11 17:06:18 -0800104 return 0;
105}