Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1 | // This binary allows us to replay the superstructure code over existing |
| 2 | // logfile. When you run this code, it generates a new logfile with the data all |
| 3 | // replayed, so that it can then be run through the plotting tool or analyzed |
| 4 | // in some other way. The original superstructure status data will be on the |
| 5 | // /original/superstructure channel. |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 6 | #include "absl/flags/flag.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 8 | #include "aos/events/logging/log_reader.h" |
| 9 | #include "aos/events/logging/log_writer.h" |
| 10 | #include "aos/events/simulated_event_loop.h" |
| 11 | #include "aos/init.h" |
| 12 | #include "aos/json_to_flatbuffer.h" |
| 13 | #include "aos/logging/log_message_generated.h" |
| 14 | #include "aos/network/team_number.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 15 | #include "y2023/constants.h" |
| 16 | #include "y2023/control_loops/superstructure/superstructure.h" |
| 17 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 18 | ABSL_FLAG(int32_t, team, 971, "Team number to use for logfile replay."); |
| 19 | ABSL_FLAG(std::string, output_folder, "/tmp/superstructure_replay/", |
| 20 | "Logs all channels to the provided logfile."); |
| 21 | ABSL_FLAG(std::string, arm_trajectories, "arm/arm_trajectories_generated.bfbs", |
| 22 | "The path to the generated arm trajectories bfbs file."); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 23 | |
| 24 | int main(int argc, char **argv) { |
| 25 | aos::InitGoogle(&argc, &argv); |
| 26 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 27 | aos::network::OverrideTeamNumber(absl::GetFlag(FLAGS_team)); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 28 | |
| 29 | // open logfiles |
| 30 | aos::logger::LogReader reader( |
| 31 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
| 32 | // TODO(james): Actually enforce not sending on the same buses as the logfile |
| 33 | // spews out. |
| 34 | reader.RemapLoggedChannel("/superstructure", |
| 35 | "y2023.control_loops.superstructure.Status"); |
| 36 | reader.RemapLoggedChannel("/superstructure", |
| 37 | "y2023.control_loops.superstructure.Output"); |
| 38 | |
| 39 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 40 | reader.Register(&factory); |
| 41 | |
| 42 | aos::NodeEventLoopFactory *roborio = |
| 43 | factory.GetNodeEventLoopFactory("roborio"); |
| 44 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 45 | unlink(absl::GetFlag(FLAGS_output_folder).c_str()); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 46 | std::unique_ptr<aos::EventLoop> logger_event_loop = |
| 47 | roborio->MakeEventLoop("logger"); |
| 48 | auto logger = std::make_unique<aos::logger::Logger>(logger_event_loop.get()); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 49 | logger->StartLoggingOnRun(absl::GetFlag(FLAGS_output_folder)); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 50 | |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 51 | auto trajectories = |
| 52 | y2023::control_loops::superstructure::Superstructure::GetArmTrajectories( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 53 | absl::GetFlag(FLAGS_arm_trajectories)); |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 54 | |
| 55 | roborio->OnStartup([roborio, &trajectories]() { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 56 | roborio->AlwaysStart<y2023::control_loops::superstructure::Superstructure>( |
Maxwell Henderson | b392b74 | 2023-03-05 07:53:51 -0800 | [diff] [blame] | 57 | "superstructure", |
| 58 | std::make_shared<y2023::constants::Values>( |
| 59 | y2023::constants::MakeValues()), |
| 60 | trajectories); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 61 | }); |
| 62 | |
| 63 | std::unique_ptr<aos::EventLoop> print_loop = roborio->MakeEventLoop("print"); |
| 64 | print_loop->SkipAosLog(); |
| 65 | print_loop->MakeWatcher( |
| 66 | "/aos", [&print_loop](const aos::logging::LogMessageFbs &msg) { |
| 67 | LOG(INFO) << print_loop->context().monotonic_event_time << " " |
| 68 | << aos::FlatbufferToJson(&msg); |
| 69 | }); |
| 70 | print_loop->MakeWatcher( |
| 71 | "/superstructure", |
| 72 | [&](const y2023::control_loops::superstructure::Status &status) { |
| 73 | if (status.estopped()) { |
| 74 | LOG(ERROR) << "Estopped"; |
| 75 | } |
| 76 | }); |
| 77 | |
| 78 | factory.Run(); |
| 79 | |
| 80 | reader.Deregister(); |
| 81 | |
| 82 | return 0; |
| 83 | } |