blob: 5c5c20b6c5b6808342b97e50f4b5c65af3198aee [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.
Austin Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/flags/flag.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07008
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 Schuh99f7c6a2024-06-25 22:07:44 -070024ABSL_FLAG(std::string, config, "y2020/aos_config.json",
25 "Name of the config file to replay using.");
26ABSL_FLAG(std::string, output_folder, "/tmp/replayed",
27 "Name of the folder to write replayed logs to.");
28ABSL_FLAG(int32_t, team, 971, "Team number to use for logfile replay.");
29ABSL_FLAG(bool, log_all_nodes, false,
30 "Whether to rerun the logger on every node.");
James Kuszmaul4f106fb2021-01-05 20:53:02 -080031
James Kuszmaul4f106fb2021-01-05 20:53:02 -080032// TODO(james): Currently, this replay produces logfiles that can't be read due
33// to time estimation issues. Pending the active refactorings of the
34// timestamp-related code, fix this.
James Kuszmaul022d40e2020-02-11 17:06:18 -080035int main(int argc, char **argv) {
36 aos::InitGoogle(&argc, &argv);
37
Austin Schuh99f7c6a2024-06-25 22:07:44 -070038 aos::network::OverrideTeamNumber(absl::GetFlag(FLAGS_team));
James Kuszmaul022d40e2020-02-11 17:06:18 -080039
James Kuszmaul022d40e2020-02-11 17:06:18 -080040 const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070041 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
James Kuszmaul022d40e2020-02-11 17:06:18 -080042
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080043 // sort logfiles
James Kuszmaul4f106fb2021-01-05 20:53:02 -080044 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuhc1609732023-06-26 11:51:28 -070045 aos::logger::SortParts(aos::logger::FindLogs(argc, argv));
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080046
47 // open logfiles
48 aos::logger::LogReader reader(logfiles, &config.message());
James Kuszmaul022d40e2020-02-11 17:06:18 -080049 // TODO(james): Actually enforce not sending on the same buses as the logfile
50 // spews out.
51 reader.RemapLoggedChannel("/drivetrain",
52 "frc971.control_loops.drivetrain.Status");
53 reader.RemapLoggedChannel("/drivetrain",
54 "frc971.control_loops.drivetrain.Output");
James Kuszmaul5ff8a862021-09-25 17:29:43 -070055 reader.RemapLoggedChannel("/drivetrain",
56 "y2020.control_loops.drivetrain.LocalizerDebug");
James Kuszmaulaca88782021-04-24 16:48:45 -070057 reader.RemapLoggedChannel("/superstructure",
58 "y2020.control_loops.superstructure.Status");
59 reader.RemapLoggedChannel("/superstructure",
60 "y2020.control_loops.superstructure.Output");
James Kuszmaul022d40e2020-02-11 17:06:18 -080061 reader.Register();
62
James Kuszmaule4853542023-05-15 20:35:48 -070063 std::vector<std::unique_ptr<aos::util::LoggerState>> loggers;
Austin Schuh99f7c6a2024-06-25 22:07:44 -070064 if (absl::GetFlag(FLAGS_log_all_nodes)) {
65 loggers = aos::util::MakeLoggersForAllNodes(
66 reader.event_loop_factory(), absl::GetFlag(FLAGS_output_folder));
James Kuszmaul93f4f032021-01-15 19:51:15 -080067 } else {
68 // List of nodes to create loggers for (note: currently just roborio; this
69 // code was refactored to allow easily adding new loggers to accommodate
70 // debugging and potential future changes).
71 const std::vector<std::string> nodes_to_log = {"roborio"};
Austin Schuh99f7c6a2024-06-25 22:07:44 -070072 loggers = aos::util::MakeLoggersForNodes(
73 reader.event_loop_factory(), nodes_to_log,
74 absl::GetFlag(FLAGS_output_folder));
James Kuszmaul4f106fb2021-01-05 20:53:02 -080075 }
76
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080077 const aos::Node *node = nullptr;
78 if (aos::configuration::MultiNode(reader.configuration())) {
79 node = aos::configuration::GetNode(reader.configuration(), "roborio");
80 }
81
James Kuszmaul75a18c52021-03-10 22:02:07 -080082 std::unique_ptr<aos::EventLoop> trajectory_generator_event_loop =
83 reader.event_loop_factory()->MakeEventLoop("trajectory_generator", node);
84 trajectory_generator_event_loop->SkipTimingReport();
85
milind-uba4f09a2021-10-11 17:51:22 -070086 y2020::constants::InitValues();
87
James Kuszmaul75a18c52021-03-10 22:02:07 -080088 frc971::control_loops::drivetrain::TrajectoryGenerator trajectory_generator(
89 trajectory_generator_event_loop.get(),
90 y2020::control_loops::drivetrain::GetDrivetrainConfig());
91
James Kuszmaul022d40e2020-02-11 17:06:18 -080092 std::unique_ptr<aos::EventLoop> drivetrain_event_loop =
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080093 reader.event_loop_factory()->MakeEventLoop("drivetrain", node);
James Kuszmaul022d40e2020-02-11 17:06:18 -080094 drivetrain_event_loop->SkipTimingReport();
95
James Kuszmaul68f7d272020-02-22 20:55:02 -080096 y2020::control_loops::drivetrain::Localizer localizer(
James Kuszmaul022d40e2020-02-11 17:06:18 -080097 drivetrain_event_loop.get(),
98 y2020::control_loops::drivetrain::GetDrivetrainConfig());
99 frc971::control_loops::drivetrain::DrivetrainLoop drivetrain(
100 y2020::control_loops::drivetrain::GetDrivetrainConfig(),
101 drivetrain_event_loop.get(), &localizer);
102
James Kuszmaulaca88782021-04-24 16:48:45 -0700103 std::unique_ptr<aos::EventLoop> superstructure_event_loop =
104 reader.event_loop_factory()->MakeEventLoop("superstructure", node);
105 superstructure_event_loop->SkipTimingReport();
106
107 y2020::control_loops::superstructure::Superstructure superstructure(
108 superstructure_event_loop.get());
109
James Kuszmaul022d40e2020-02-11 17:06:18 -0800110 reader.event_loop_factory()->Run();
111
James Kuszmaul022d40e2020-02-11 17:06:18 -0800112 return 0;
113}