Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 1 | #include "absl/flags/flag.h" |
James Kuszmaul | ec0c96b | 2024-03-17 17:24:35 -0700 | [diff] [blame] | 2 | |
| 3 | #include "aos/configuration.h" |
| 4 | #include "aos/events/logging/log_reader.h" |
| 5 | #include "aos/events/logging/log_writer.h" |
| 6 | #include "aos/events/simulated_event_loop.h" |
| 7 | #include "aos/init.h" |
| 8 | #include "aos/json_to_flatbuffer.h" |
| 9 | #include "aos/network/team_number.h" |
| 10 | #include "aos/util/simulation_logger.h" |
| 11 | #include "frc971/constants/constants_sender_lib.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain.h" |
| 13 | #include "frc971/control_loops/drivetrain/localization/puppet_localizer.h" |
| 14 | #include "frc971/control_loops/drivetrain/trajectory_generator.h" |
| 15 | #include "frc971/imu_fdcan/dual_imu_blender_lib.h" |
| 16 | #include "y2024/constants/constants_generated.h" |
| 17 | #include "y2024/constants/simulated_constants_sender.h" |
| 18 | #include "y2024/control_loops/drivetrain/drivetrain_base.h" |
| 19 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 20 | ABSL_FLAG(std::string, config, "y2024/aos_config.json", |
| 21 | "Name of the config file to replay using."); |
| 22 | ABSL_FLAG(bool, override_config, false, |
| 23 | "If set, override the logged config with --config."); |
| 24 | ABSL_FLAG(int32_t, team, 971, "Team number to use for logfile replay."); |
| 25 | ABSL_FLAG(std::string, output_folder, "/tmp/replayed", |
| 26 | "Name of the folder to write replayed logs to."); |
| 27 | ABSL_FLAG(std::string, constants_path, "y2024/constants/constants.json", |
| 28 | "Path to the constant file"); |
James Kuszmaul | ec0c96b | 2024-03-17 17:24:35 -0700 | [diff] [blame] | 29 | |
| 30 | int main(int argc, char **argv) { |
| 31 | aos::InitGoogle(&argc, &argv); |
| 32 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 33 | aos::network::OverrideTeamNumber(absl::GetFlag(FLAGS_team)); |
James Kuszmaul | ec0c96b | 2024-03-17 17:24:35 -0700 | [diff] [blame] | 34 | |
| 35 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 36 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
James Kuszmaul | ec0c96b | 2024-03-17 17:24:35 -0700 | [diff] [blame] | 37 | |
| 38 | // sort logfiles |
| 39 | const std::vector<aos::logger::LogFile> logfiles = |
| 40 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv)); |
| 41 | |
| 42 | // open logfiles |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 43 | aos::logger::LogReader reader(logfiles, absl::GetFlag(FLAGS_override_config) |
| 44 | ? &config.message() |
| 45 | : nullptr); |
James Kuszmaul | ec0c96b | 2024-03-17 17:24:35 -0700 | [diff] [blame] | 46 | |
| 47 | reader.RemapLoggedChannel("/imu/constants", "y2024.Constants"); |
| 48 | reader.RemapLoggedChannel("/roborio/constants", "y2024.Constants"); |
| 49 | reader.RemapLoggedChannel("/orin1/constants", "y2024.Constants"); |
| 50 | reader.RemapLoggedChannel("/drivetrain", |
| 51 | "frc971.control_loops.drivetrain.Status"); |
| 52 | reader.RemapLoggedChannel("/drivetrain", |
| 53 | "frc971.control_loops.drivetrain.Output"); |
| 54 | reader.RemapLoggedChannel( |
| 55 | "/drivetrain", "frc971.control_loops.drivetrain.RioLocalizerInputs"); |
| 56 | |
| 57 | auto factory = |
| 58 | std::make_unique<aos::SimulatedEventLoopFactory>(reader.configuration()); |
| 59 | |
| 60 | reader.RegisterWithoutStarting(factory.get()); |
| 61 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 62 | y2024::SendSimulationConstants( |
| 63 | reader.event_loop_factory(), absl::GetFlag(FLAGS_team), |
| 64 | absl::GetFlag(FLAGS_constants_path), {"roborio"}); |
James Kuszmaul | ec0c96b | 2024-03-17 17:24:35 -0700 | [diff] [blame] | 65 | |
| 66 | const aos::Node *node = nullptr; |
| 67 | if (aos::configuration::MultiNode(reader.configuration())) { |
| 68 | node = aos::configuration::GetNode(reader.configuration(), "roborio"); |
| 69 | } |
| 70 | std::vector<std::unique_ptr<aos::util::LoggerState>> loggers; |
| 71 | |
| 72 | std::unique_ptr<aos::EventLoop> drivetrain_event_loop; |
| 73 | std::unique_ptr<::frc971::control_loops::drivetrain::PuppetLocalizer> |
| 74 | localizer; |
| 75 | std::unique_ptr<frc971::control_loops::drivetrain::DrivetrainLoop> drivetrain; |
| 76 | reader.OnStart(node, [&factory, node, &loggers, &drivetrain_event_loop, |
| 77 | &localizer, &drivetrain]() { |
| 78 | aos::NodeEventLoopFactory *node_factory = |
| 79 | factory->GetNodeEventLoopFactory(node); |
| 80 | drivetrain_event_loop = node_factory->MakeEventLoop("drivetrain"); |
| 81 | const auto drivetrain_config = |
| 82 | ::y2024::control_loops::drivetrain::GetDrivetrainConfig( |
| 83 | drivetrain_event_loop.get()); |
| 84 | localizer = |
| 85 | std::make_unique<::frc971::control_loops::drivetrain::PuppetLocalizer>( |
| 86 | drivetrain_event_loop.get(), drivetrain_config); |
| 87 | drivetrain = |
| 88 | std::make_unique<frc971::control_loops::drivetrain::DrivetrainLoop>( |
| 89 | drivetrain_config, drivetrain_event_loop.get(), localizer.get()); |
| 90 | loggers.push_back(std::make_unique<aos::util::LoggerState>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 91 | factory.get(), node, absl::GetFlag(FLAGS_output_folder))); |
James Kuszmaul | ec0c96b | 2024-03-17 17:24:35 -0700 | [diff] [blame] | 92 | // The Trajectory information is NOT_LOGGED, so we need to rerun it against |
| 93 | // the SplineGoals that we have in the log. |
| 94 | node_factory |
| 95 | ->AlwaysStart<frc971::control_loops::drivetrain::TrajectoryGenerator>( |
| 96 | "trajectory_generator", drivetrain_config); |
| 97 | }); |
| 98 | |
| 99 | reader.event_loop_factory()->Run(); |
| 100 | |
| 101 | reader.Deregister(); |
| 102 | |
| 103 | return 0; |
| 104 | } |