blob: 326c184693dc259272e9d357409a3c09762dc3fe [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.
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "gflags/gflags.h"
8
James Kuszmaul022d40e2020-02-11 17:06:18 -08009#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -080010#include "aos/events/logging/log_reader.h"
James Kuszmaulaca88782021-04-24 16:48:45 -070011#include "aos/events/logging/log_writer.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080012#include "aos/events/simulated_event_loop.h"
13#include "aos/init.h"
14#include "aos/json_to_flatbuffer.h"
15#include "aos/network/team_number.h"
James Kuszmaule4853542023-05-15 20:35:48 -070016#include "aos/util/simulation_logger.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080017#include "frc971/control_loops/drivetrain/drivetrain.h"
James Kuszmaul75a18c52021-03-10 22:02:07 -080018#include "frc971/control_loops/drivetrain/trajectory_generator.h"
milind-uba4f09a2021-10-11 17:51:22 -070019#include "y2020/constants.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080020#include "y2020/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul68f7d272020-02-22 20:55:02 -080021#include "y2020/control_loops/drivetrain/localizer.h"
James Kuszmaulaca88782021-04-24 16:48:45 -070022#include "y2020/control_loops/superstructure/superstructure.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080023
Austin Schuhc5fa6d92022-02-25 14:36:28 -080024DEFINE_string(config, "y2020/aos_config.json",
James Kuszmaul022d40e2020-02-11 17:06:18 -080025 "Name of the config file to replay using.");
Austin Schuh375bcbb2021-01-30 16:59:59 -080026DEFINE_string(output_folder, "/tmp/replayed",
James Kuszmaul4f106fb2021-01-05 20:53:02 -080027 "Name of the folder to write replayed logs to.");
James Kuszmaul022d40e2020-02-11 17:06:18 -080028DEFINE_int32(team, 971, "Team number to use for logfile replay.");
James Kuszmaul93f4f032021-01-15 19:51:15 -080029DEFINE_bool(log_all_nodes, false, "Whether to rerun the logger on every node.");
James Kuszmaul4f106fb2021-01-05 20:53:02 -080030
James Kuszmaul4f106fb2021-01-05 20:53:02 -080031// TODO(james): Currently, this replay produces logfiles that can't be read due
32// to time estimation issues. Pending the active refactorings of the
33// timestamp-related code, fix this.
James Kuszmaul022d40e2020-02-11 17:06:18 -080034int main(int argc, char **argv) {
35 aos::InitGoogle(&argc, &argv);
36
37 aos::network::OverrideTeamNumber(FLAGS_team);
38
James Kuszmaul022d40e2020-02-11 17:06:18 -080039 const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
40 aos::configuration::ReadConfig(FLAGS_config);
41
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080042 // sort logfiles
James Kuszmaul4f106fb2021-01-05 20:53:02 -080043 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuhc1609732023-06-26 11:51:28 -070044 aos::logger::SortParts(aos::logger::FindLogs(argc, argv));
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080045
46 // open logfiles
47 aos::logger::LogReader reader(logfiles, &config.message());
James Kuszmaul022d40e2020-02-11 17:06:18 -080048 // TODO(james): Actually enforce not sending on the same buses as the logfile
49 // spews out.
50 reader.RemapLoggedChannel("/drivetrain",
51 "frc971.control_loops.drivetrain.Status");
52 reader.RemapLoggedChannel("/drivetrain",
53 "frc971.control_loops.drivetrain.Output");
James Kuszmaul5ff8a862021-09-25 17:29:43 -070054 reader.RemapLoggedChannel("/drivetrain",
55 "y2020.control_loops.drivetrain.LocalizerDebug");
James Kuszmaulaca88782021-04-24 16:48:45 -070056 reader.RemapLoggedChannel("/superstructure",
57 "y2020.control_loops.superstructure.Status");
58 reader.RemapLoggedChannel("/superstructure",
59 "y2020.control_loops.superstructure.Output");
James Kuszmaul022d40e2020-02-11 17:06:18 -080060 reader.Register();
61
James Kuszmaule4853542023-05-15 20:35:48 -070062 std::vector<std::unique_ptr<aos::util::LoggerState>> loggers;
James Kuszmaul93f4f032021-01-15 19:51:15 -080063 if (FLAGS_log_all_nodes) {
James Kuszmaule4853542023-05-15 20:35:48 -070064 loggers = aos::util::MakeLoggersForAllNodes(reader.event_loop_factory(),
65 FLAGS_output_folder);
James Kuszmaul93f4f032021-01-15 19:51:15 -080066 } else {
67 // List of nodes to create loggers for (note: currently just roborio; this
68 // code was refactored to allow easily adding new loggers to accommodate
69 // debugging and potential future changes).
70 const std::vector<std::string> nodes_to_log = {"roborio"};
James Kuszmaule4853542023-05-15 20:35:48 -070071 loggers = aos::util::MakeLoggersForNodes(reader.event_loop_factory(),
72 nodes_to_log, FLAGS_output_folder);
James Kuszmaul4f106fb2021-01-05 20:53:02 -080073 }
74
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080075 const aos::Node *node = nullptr;
76 if (aos::configuration::MultiNode(reader.configuration())) {
77 node = aos::configuration::GetNode(reader.configuration(), "roborio");
78 }
79
James Kuszmaul75a18c52021-03-10 22:02:07 -080080 std::unique_ptr<aos::EventLoop> trajectory_generator_event_loop =
81 reader.event_loop_factory()->MakeEventLoop("trajectory_generator", node);
82 trajectory_generator_event_loop->SkipTimingReport();
83
milind-uba4f09a2021-10-11 17:51:22 -070084 y2020::constants::InitValues();
85
James Kuszmaul75a18c52021-03-10 22:02:07 -080086 frc971::control_loops::drivetrain::TrajectoryGenerator trajectory_generator(
87 trajectory_generator_event_loop.get(),
88 y2020::control_loops::drivetrain::GetDrivetrainConfig());
89
James Kuszmaul022d40e2020-02-11 17:06:18 -080090 std::unique_ptr<aos::EventLoop> drivetrain_event_loop =
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080091 reader.event_loop_factory()->MakeEventLoop("drivetrain", node);
James Kuszmaul022d40e2020-02-11 17:06:18 -080092 drivetrain_event_loop->SkipTimingReport();
93
James Kuszmaul68f7d272020-02-22 20:55:02 -080094 y2020::control_loops::drivetrain::Localizer localizer(
James Kuszmaul022d40e2020-02-11 17:06:18 -080095 drivetrain_event_loop.get(),
96 y2020::control_loops::drivetrain::GetDrivetrainConfig());
97 frc971::control_loops::drivetrain::DrivetrainLoop drivetrain(
98 y2020::control_loops::drivetrain::GetDrivetrainConfig(),
99 drivetrain_event_loop.get(), &localizer);
100
James Kuszmaulaca88782021-04-24 16:48:45 -0700101 std::unique_ptr<aos::EventLoop> superstructure_event_loop =
102 reader.event_loop_factory()->MakeEventLoop("superstructure", node);
103 superstructure_event_loop->SkipTimingReport();
104
105 y2020::control_loops::superstructure::Superstructure superstructure(
106 superstructure_event_loop.get());
107
James Kuszmaul022d40e2020-02-11 17:06:18 -0800108 reader.event_loop_factory()->Run();
109
James Kuszmaul022d40e2020-02-11 17:06:18 -0800110 return 0;
111}