James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 1 | #include <optional> |
| 2 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 3 | #include "absl/flags/flag.h" |
| 4 | #include "absl/flags/usage.h" |
| 5 | #include "absl/log/check.h" |
| 6 | #include "absl/log/log.h" |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 7 | |
| 8 | #include "aos/events/logging/log_reader.h" |
| 9 | #include "aos/init.h" |
| 10 | #include "aos/util/simulation_logger.h" |
| 11 | #include "frc971/input/joystick_state_generated.h" |
| 12 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 13 | ABSL_FLAG(std::string, output_folder, "/tmp/trimmed/", |
| 14 | "Name of the folder to write the trimmed log to."); |
| 15 | ABSL_FLAG(std::string, node, "roborio", ""); |
| 16 | ABSL_FLAG(bool, auto, false, "If set, trim the log to just the auto mode."); |
| 17 | ABSL_FLAG(double, pre_enable_time_sec, 10.0, |
| 18 | "Amount of time to leave in the new log before the first enable " |
| 19 | "signal happens."); |
| 20 | ABSL_FLAG(double, post_enable_time_sec, 1.0, |
| 21 | "Amount of time to leave in the new log after the final enable " |
| 22 | "signal ends."); |
| 23 | ABSL_FLAG(double, force_start_monotonic, -1.0, |
| 24 | "If set, time, in seconds, at which to forcibly trim the start " |
| 25 | "of the log."); |
| 26 | ABSL_FLAG( |
| 27 | double, force_end_monotonic, -1.0, |
James Kuszmaul | ee52599 | 2024-03-15 21:01:16 -0700 | [diff] [blame] | 28 | "If set, time, in seconds, at which to forcibly trim the end of the log."); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 29 | |
| 30 | int main(int argc, char *argv[]) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 31 | absl::SetProgramUsageMessage( |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 32 | "Trims the sections at the start/end of a log where the robot is " |
| 33 | "disabled."); |
| 34 | aos::InitGoogle(&argc, &argv); |
| 35 | const std::vector<aos::logger::LogFile> logfiles = |
| 36 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv)); |
| 37 | std::optional<aos::monotonic_clock::time_point> start_time; |
| 38 | std::optional<aos::monotonic_clock::time_point> end_time; |
| 39 | bool printed_match = false; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 40 | bool force_time_range = absl::GetFlag(FLAGS_force_start_monotonic) > 0; |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 41 | // We need to do two passes through the logfile; one to figure out when the |
| 42 | // start/end times are, one to actually do the trimming. |
James Kuszmaul | ee52599 | 2024-03-15 21:01:16 -0700 | [diff] [blame] | 43 | if (!force_time_range) { |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 44 | aos::logger::LogReader reader(logfiles); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 45 | const aos::Node *roborio = aos::configuration::GetNode( |
| 46 | reader.configuration(), absl::GetFlag(FLAGS_node)); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 47 | reader.Register(); |
| 48 | std::unique_ptr<aos::EventLoop> event_loop = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 49 | reader.event_loop_factory()->MakeEventLoop(absl::GetFlag(FLAGS_node), |
| 50 | roborio); |
| 51 | event_loop->MakeWatcher("/aos", [&start_time, &end_time, &printed_match, |
| 52 | &event_loop]( |
| 53 | const aos::JoystickState &msg) { |
| 54 | if (!printed_match && msg.match_type() != aos::MatchType::kNone) { |
| 55 | LOG(INFO) << "Match Type: " << aos::EnumNameMatchType(msg.match_type()); |
| 56 | LOG(INFO) << "Match #: " << msg.match_number(); |
| 57 | printed_match = true; |
| 58 | } |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 59 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 60 | if (msg.enabled() && (!absl::GetFlag(FLAGS_auto) || msg.autonomous())) { |
| 61 | // Note that time is monotonic, so we don't need to e.g. do min's or |
| 62 | // max's on the start/end time. |
| 63 | if (!start_time.has_value()) { |
| 64 | start_time = event_loop->context().monotonic_event_time; |
| 65 | } |
| 66 | end_time = event_loop->context().monotonic_event_time; |
| 67 | } |
| 68 | }); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 69 | |
| 70 | reader.event_loop_factory()->Run(); |
| 71 | |
| 72 | if (!printed_match) { |
| 73 | LOG(INFO) << "No match info."; |
| 74 | } |
James Kuszmaul | ee52599 | 2024-03-15 21:01:16 -0700 | [diff] [blame] | 75 | if (!start_time.has_value()) { |
| 76 | LOG(WARNING) << "Log does not ontain any JoystickState messages."; |
| 77 | return 1; |
| 78 | } |
| 79 | LOG(INFO) << "First enable at " << start_time.value(); |
| 80 | LOG(INFO) << "Final enable at " << end_time.value(); |
| 81 | start_time.value() -= std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 82 | std::chrono::duration<double>( |
| 83 | absl::GetFlag(FLAGS_pre_enable_time_sec))); |
James Kuszmaul | ee52599 | 2024-03-15 21:01:16 -0700 | [diff] [blame] | 84 | end_time.value() += std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 85 | std::chrono::duration<double>( |
| 86 | absl::GetFlag(FLAGS_post_enable_time_sec))); |
James Kuszmaul | ee52599 | 2024-03-15 21:01:16 -0700 | [diff] [blame] | 87 | } else { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 88 | CHECK_LT(absl::GetFlag(FLAGS_force_start_monotonic), |
| 89 | absl::GetFlag(FLAGS_force_end_monotonic)); |
James Kuszmaul | ee52599 | 2024-03-15 21:01:16 -0700 | [diff] [blame] | 90 | start_time = aos::monotonic_clock::time_point( |
| 91 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 92 | std::chrono::duration<double>( |
| 93 | absl::GetFlag(FLAGS_force_start_monotonic)))); |
James Kuszmaul | ee52599 | 2024-03-15 21:01:16 -0700 | [diff] [blame] | 94 | end_time = aos::monotonic_clock::time_point( |
| 95 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 96 | std::chrono::duration<double>( |
| 97 | absl::GetFlag(FLAGS_force_end_monotonic)))); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 98 | } |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 99 | |
| 100 | { |
| 101 | aos::logger::LogReader reader(logfiles); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 102 | const aos::Node *roborio = aos::configuration::GetNode( |
| 103 | reader.configuration(), absl::GetFlag(FLAGS_node)); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 104 | reader.Register(); |
| 105 | std::unique_ptr<aos::EventLoop> event_loop = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 106 | reader.event_loop_factory()->MakeEventLoop(absl::GetFlag(FLAGS_node), |
| 107 | roborio); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 108 | auto exit_timer = event_loop->AddTimer( |
| 109 | [&reader]() { reader.event_loop_factory()->Exit(); }); |
| 110 | exit_timer->Schedule(start_time.value()); |
| 111 | reader.event_loop_factory()->Run(); |
| 112 | const std::set<std::string> logger_nodes = |
| 113 | aos::logger::LoggerNodes(logfiles); |
| 114 | // Only start up loggers that generated the original set of logfiles. |
| 115 | // This mostly exists to make it so that utilities like log_to_mcap can |
| 116 | // easily auto-detect which node to replay as when consuming the input logs. |
| 117 | auto loggers = aos::util::MakeLoggersForNodes( |
| 118 | reader.event_loop_factory(), {logger_nodes.begin(), logger_nodes.end()}, |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 119 | absl::GetFlag(FLAGS_output_folder)); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 120 | exit_timer->Schedule(end_time.value()); |
| 121 | |
| 122 | reader.event_loop_factory()->Run(); |
| 123 | } |
| 124 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 125 | LOG(INFO) << "Trimmed logs written to " << absl::GetFlag(FLAGS_output_folder); |
James Kuszmaul | 14d7ea1 | 2023-12-09 15:41:14 -0800 | [diff] [blame] | 126 | |
| 127 | return EXIT_SUCCESS; |
| 128 | } |