James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 1 | // 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 Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 7 | #include "absl/flags/flag.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 9 | #include "aos/configuration.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 10 | #include "aos/events/logging/log_reader.h" |
James Kuszmaul | aca8878 | 2021-04-24 16:48:45 -0700 | [diff] [blame] | 11 | #include "aos/events/logging/log_writer.h" |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 12 | #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 Kuszmaul | e485354 | 2023-05-15 20:35:48 -0700 | [diff] [blame] | 16 | #include "aos/util/simulation_logger.h" |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/drivetrain.h" |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 18 | #include "frc971/control_loops/drivetrain/trajectory_generator.h" |
milind-u | ba4f09a | 2021-10-11 17:51:22 -0700 | [diff] [blame] | 19 | #include "y2020/constants.h" |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 20 | #include "y2020/control_loops/drivetrain/drivetrain_base.h" |
James Kuszmaul | 68f7d27 | 2020-02-22 20:55:02 -0800 | [diff] [blame] | 21 | #include "y2020/control_loops/drivetrain/localizer.h" |
James Kuszmaul | aca8878 | 2021-04-24 16:48:45 -0700 | [diff] [blame] | 22 | #include "y2020/control_loops/superstructure/superstructure.h" |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 24 | ABSL_FLAG(std::string, config, "y2020/aos_config.json", |
| 25 | "Name of the config file to replay using."); |
| 26 | ABSL_FLAG(std::string, output_folder, "/tmp/replayed", |
| 27 | "Name of the folder to write replayed logs to."); |
| 28 | ABSL_FLAG(int32_t, team, 971, "Team number to use for logfile replay."); |
| 29 | ABSL_FLAG(bool, log_all_nodes, false, |
| 30 | "Whether to rerun the logger on every node."); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 31 | |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 32 | // 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 Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 35 | int main(int argc, char **argv) { |
| 36 | aos::InitGoogle(&argc, &argv); |
| 37 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 38 | aos::network::OverrideTeamNumber(absl::GetFlag(FLAGS_team)); |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 39 | |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 40 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 41 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 42 | |
Milind Upadhyay | 6d758aa | 2020-12-26 16:11:15 -0800 | [diff] [blame] | 43 | // sort logfiles |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 44 | const std::vector<aos::logger::LogFile> logfiles = |
Austin Schuh | c160973 | 2023-06-26 11:51:28 -0700 | [diff] [blame] | 45 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv)); |
Milind Upadhyay | 6d758aa | 2020-12-26 16:11:15 -0800 | [diff] [blame] | 46 | |
| 47 | // open logfiles |
| 48 | aos::logger::LogReader reader(logfiles, &config.message()); |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 49 | // 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 Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 55 | reader.RemapLoggedChannel("/drivetrain", |
| 56 | "y2020.control_loops.drivetrain.LocalizerDebug"); |
James Kuszmaul | aca8878 | 2021-04-24 16:48:45 -0700 | [diff] [blame] | 57 | reader.RemapLoggedChannel("/superstructure", |
| 58 | "y2020.control_loops.superstructure.Status"); |
| 59 | reader.RemapLoggedChannel("/superstructure", |
| 60 | "y2020.control_loops.superstructure.Output"); |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 61 | reader.Register(); |
| 62 | |
James Kuszmaul | e485354 | 2023-05-15 20:35:48 -0700 | [diff] [blame] | 63 | std::vector<std::unique_ptr<aos::util::LoggerState>> loggers; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 64 | if (absl::GetFlag(FLAGS_log_all_nodes)) { |
| 65 | loggers = aos::util::MakeLoggersForAllNodes( |
| 66 | reader.event_loop_factory(), absl::GetFlag(FLAGS_output_folder)); |
James Kuszmaul | 93f4f03 | 2021-01-15 19:51:15 -0800 | [diff] [blame] | 67 | } 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 Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 72 | loggers = aos::util::MakeLoggersForNodes( |
| 73 | reader.event_loop_factory(), nodes_to_log, |
| 74 | absl::GetFlag(FLAGS_output_folder)); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 75 | } |
| 76 | |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 77 | const aos::Node *node = nullptr; |
| 78 | if (aos::configuration::MultiNode(reader.configuration())) { |
| 79 | node = aos::configuration::GetNode(reader.configuration(), "roborio"); |
| 80 | } |
| 81 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 82 | 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-u | ba4f09a | 2021-10-11 17:51:22 -0700 | [diff] [blame] | 86 | y2020::constants::InitValues(); |
| 87 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 88 | frc971::control_loops::drivetrain::TrajectoryGenerator trajectory_generator( |
| 89 | trajectory_generator_event_loop.get(), |
| 90 | y2020::control_loops::drivetrain::GetDrivetrainConfig()); |
| 91 | |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 92 | std::unique_ptr<aos::EventLoop> drivetrain_event_loop = |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 93 | reader.event_loop_factory()->MakeEventLoop("drivetrain", node); |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 94 | drivetrain_event_loop->SkipTimingReport(); |
| 95 | |
James Kuszmaul | 68f7d27 | 2020-02-22 20:55:02 -0800 | [diff] [blame] | 96 | y2020::control_loops::drivetrain::Localizer localizer( |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 97 | 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 Kuszmaul | aca8878 | 2021-04-24 16:48:45 -0700 | [diff] [blame] | 103 | 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 Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 110 | reader.event_loop_factory()->Run(); |
| 111 | |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 112 | return 0; |
| 113 | } |