blob: 0035fb34909fd443e15726525ddffcd107f32581 [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 // find logfiles
43 std::vector<std::string> unsorted_logfiles =
44 aos::logger::FindLogs(argc, argv);
45
46 // sort logfiles
James Kuszmaul4f106fb2021-01-05 20:53:02 -080047 const std::vector<aos::logger::LogFile> logfiles =
48 aos::logger::SortParts(unsorted_logfiles);
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080049
50 // open logfiles
51 aos::logger::LogReader reader(logfiles, &config.message());
James Kuszmaul022d40e2020-02-11 17:06:18 -080052 // TODO(james): Actually enforce not sending on the same buses as the logfile
53 // spews out.
54 reader.RemapLoggedChannel("/drivetrain",
55 "frc971.control_loops.drivetrain.Status");
56 reader.RemapLoggedChannel("/drivetrain",
57 "frc971.control_loops.drivetrain.Output");
James Kuszmaul5ff8a862021-09-25 17:29:43 -070058 reader.RemapLoggedChannel("/drivetrain",
59 "y2020.control_loops.drivetrain.LocalizerDebug");
James Kuszmaulaca88782021-04-24 16:48:45 -070060 reader.RemapLoggedChannel("/superstructure",
61 "y2020.control_loops.superstructure.Status");
62 reader.RemapLoggedChannel("/superstructure",
63 "y2020.control_loops.superstructure.Output");
James Kuszmaul022d40e2020-02-11 17:06:18 -080064 reader.Register();
65
James Kuszmaule4853542023-05-15 20:35:48 -070066 std::vector<std::unique_ptr<aos::util::LoggerState>> loggers;
James Kuszmaul93f4f032021-01-15 19:51:15 -080067 if (FLAGS_log_all_nodes) {
James Kuszmaule4853542023-05-15 20:35:48 -070068 loggers = aos::util::MakeLoggersForAllNodes(reader.event_loop_factory(),
69 FLAGS_output_folder);
James Kuszmaul93f4f032021-01-15 19:51:15 -080070 } else {
71 // List of nodes to create loggers for (note: currently just roborio; this
72 // code was refactored to allow easily adding new loggers to accommodate
73 // debugging and potential future changes).
74 const std::vector<std::string> nodes_to_log = {"roborio"};
James Kuszmaule4853542023-05-15 20:35:48 -070075 loggers = aos::util::MakeLoggersForNodes(reader.event_loop_factory(),
76 nodes_to_log, FLAGS_output_folder);
James Kuszmaul4f106fb2021-01-05 20:53:02 -080077 }
78
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080079 const aos::Node *node = nullptr;
80 if (aos::configuration::MultiNode(reader.configuration())) {
81 node = aos::configuration::GetNode(reader.configuration(), "roborio");
82 }
83
James Kuszmaul75a18c52021-03-10 22:02:07 -080084 std::unique_ptr<aos::EventLoop> trajectory_generator_event_loop =
85 reader.event_loop_factory()->MakeEventLoop("trajectory_generator", node);
86 trajectory_generator_event_loop->SkipTimingReport();
87
milind-uba4f09a2021-10-11 17:51:22 -070088 y2020::constants::InitValues();
89
James Kuszmaul75a18c52021-03-10 22:02:07 -080090 frc971::control_loops::drivetrain::TrajectoryGenerator trajectory_generator(
91 trajectory_generator_event_loop.get(),
92 y2020::control_loops::drivetrain::GetDrivetrainConfig());
93
James Kuszmaul022d40e2020-02-11 17:06:18 -080094 std::unique_ptr<aos::EventLoop> drivetrain_event_loop =
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080095 reader.event_loop_factory()->MakeEventLoop("drivetrain", node);
James Kuszmaul022d40e2020-02-11 17:06:18 -080096 drivetrain_event_loop->SkipTimingReport();
97
James Kuszmaul68f7d272020-02-22 20:55:02 -080098 y2020::control_loops::drivetrain::Localizer localizer(
James Kuszmaul022d40e2020-02-11 17:06:18 -080099 drivetrain_event_loop.get(),
100 y2020::control_loops::drivetrain::GetDrivetrainConfig());
101 frc971::control_loops::drivetrain::DrivetrainLoop drivetrain(
102 y2020::control_loops::drivetrain::GetDrivetrainConfig(),
103 drivetrain_event_loop.get(), &localizer);
104
James Kuszmaulaca88782021-04-24 16:48:45 -0700105 std::unique_ptr<aos::EventLoop> superstructure_event_loop =
106 reader.event_loop_factory()->MakeEventLoop("superstructure", node);
107 superstructure_event_loop->SkipTimingReport();
108
109 y2020::control_loops::superstructure::Superstructure superstructure(
110 superstructure_event_loop.get());
111
James Kuszmaul022d40e2020-02-11 17:06:18 -0800112 reader.event_loop_factory()->Run();
113
James Kuszmaul022d40e2020-02-11 17:06:18 -0800114 return 0;
115}