blob: 59c2a52b07e88fa1be47c2ed1a40668c8d388f3f [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.");
20DEFINE_string(output_file, "/tmp/replayed.bfbs",
21 "Name of the logfile to write replayed data to.");
22DEFINE_int32(team, 971, "Team number to use for logfile replay.");
23int main(int argc, char **argv) {
24 aos::InitGoogle(&argc, &argv);
25
26 aos::network::OverrideTeamNumber(FLAGS_team);
27
James Kuszmaul022d40e2020-02-11 17:06:18 -080028 const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
29 aos::configuration::ReadConfig(FLAGS_config);
30
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080031 // find logfiles
32 std::vector<std::string> unsorted_logfiles =
33 aos::logger::FindLogs(argc, argv);
34
35 // sort logfiles
36 const std::vector<aos::logger::LogFile> logfiles = aos::logger::SortParts(unsorted_logfiles);
37
38 // open logfiles
39 aos::logger::LogReader reader(logfiles, &config.message());
James Kuszmaul022d40e2020-02-11 17:06:18 -080040 // TODO(james): Actually enforce not sending on the same buses as the logfile
41 // spews out.
42 reader.RemapLoggedChannel("/drivetrain",
43 "frc971.control_loops.drivetrain.Status");
44 reader.RemapLoggedChannel("/drivetrain",
45 "frc971.control_loops.drivetrain.Output");
46 reader.Register();
47
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080048 const aos::Node *node = nullptr;
49 if (aos::configuration::MultiNode(reader.configuration())) {
50 node = aos::configuration::GetNode(reader.configuration(), "roborio");
51 }
52
James Kuszmaul022d40e2020-02-11 17:06:18 -080053 std::unique_ptr<aos::EventLoop> log_writer_event_loop =
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080054 reader.event_loop_factory()->MakeEventLoop("log_writer", node);
James Kuszmaul022d40e2020-02-11 17:06:18 -080055 log_writer_event_loop->SkipTimingReport();
Brian Silverman1f345222020-09-24 21:14:48 -070056 aos::logger::Logger writer(log_writer_event_loop.get());
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080057
58
59 std::unique_ptr<aos::logger::LogNamer> log_namer;
60 log_namer = std::make_unique<aos::logger::MultiNodeLogNamer>(
61 absl::StrCat(FLAGS_output_file, "/"),
62 log_writer_event_loop->configuration(),
63 log_writer_event_loop->node());
64
65 aos::logger::Logger logger(log_writer_event_loop.get());
66 log_writer_event_loop->OnRun([&log_namer, &logger]() {
67 logger.StartLogging(std::move(log_namer));
68 });
James Kuszmaul022d40e2020-02-11 17:06:18 -080069
70 std::unique_ptr<aos::EventLoop> drivetrain_event_loop =
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080071 reader.event_loop_factory()->MakeEventLoop("drivetrain", node);
James Kuszmaul022d40e2020-02-11 17:06:18 -080072 drivetrain_event_loop->SkipTimingReport();
73
James Kuszmaul68f7d272020-02-22 20:55:02 -080074 y2020::control_loops::drivetrain::Localizer localizer(
James Kuszmaul022d40e2020-02-11 17:06:18 -080075 drivetrain_event_loop.get(),
76 y2020::control_loops::drivetrain::GetDrivetrainConfig());
77 frc971::control_loops::drivetrain::DrivetrainLoop drivetrain(
78 y2020::control_loops::drivetrain::GetDrivetrainConfig(),
79 drivetrain_event_loop.get(), &localizer);
80
81 reader.event_loop_factory()->Run();
82
James Kuszmaul022d40e2020-02-11 17:06:18 -080083 return 0;
84}