blob: 1f13aebdfd8bc5a571b1d7c6f48bd972fc64f987 [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001// 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 Schuh99f7c6a2024-06-25 22:07:44 -07006#include "absl/flags/flag.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007
Maxwell Hendersonad312342023-01-10 12:07:47 -08008#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 Hendersonad312342023-01-10 12:07:47 -080015#include "y2023/constants.h"
16#include "y2023/control_loops/superstructure/superstructure.h"
17
Austin Schuh99f7c6a2024-06-25 22:07:44 -070018ABSL_FLAG(int32_t, team, 971, "Team number to use for logfile replay.");
19ABSL_FLAG(std::string, output_folder, "/tmp/superstructure_replay/",
20 "Logs all channels to the provided logfile.");
21ABSL_FLAG(std::string, arm_trajectories, "arm/arm_trajectories_generated.bfbs",
22 "The path to the generated arm trajectories bfbs file.");
Maxwell Hendersonad312342023-01-10 12:07:47 -080023
24int main(int argc, char **argv) {
25 aos::InitGoogle(&argc, &argv);
26
Austin Schuh99f7c6a2024-06-25 22:07:44 -070027 aos::network::OverrideTeamNumber(absl::GetFlag(FLAGS_team));
Maxwell Hendersonad312342023-01-10 12:07:47 -080028
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 Schuh99f7c6a2024-06-25 22:07:44 -070045 unlink(absl::GetFlag(FLAGS_output_folder).c_str());
Maxwell Hendersonad312342023-01-10 12:07:47 -080046 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 Schuh99f7c6a2024-06-25 22:07:44 -070049 logger->StartLoggingOnRun(absl::GetFlag(FLAGS_output_folder));
Maxwell Hendersonad312342023-01-10 12:07:47 -080050
Maxwell Hendersonb392b742023-03-05 07:53:51 -080051 auto trajectories =
52 y2023::control_loops::superstructure::Superstructure::GetArmTrajectories(
Austin Schuh99f7c6a2024-06-25 22:07:44 -070053 absl::GetFlag(FLAGS_arm_trajectories));
Maxwell Hendersonb392b742023-03-05 07:53:51 -080054
55 roborio->OnStartup([roborio, &trajectories]() {
Maxwell Hendersonad312342023-01-10 12:07:47 -080056 roborio->AlwaysStart<y2023::control_loops::superstructure::Superstructure>(
Maxwell Hendersonb392b742023-03-05 07:53:51 -080057 "superstructure",
58 std::make_shared<y2023::constants::Values>(
59 y2023::constants::MakeValues()),
60 trajectories);
Maxwell Hendersonad312342023-01-10 12:07:47 -080061 });
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}