blob: 9bd3ae2818e46b38b5f068394cb07432dcd41f7e [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.
6#include "aos/events/logging/log_reader.h"
7#include "aos/events/logging/log_writer.h"
8#include "aos/events/simulated_event_loop.h"
9#include "aos/init.h"
10#include "aos/json_to_flatbuffer.h"
11#include "aos/logging/log_message_generated.h"
12#include "aos/network/team_number.h"
13#include "gflags/gflags.h"
14#include "y2023/constants.h"
15#include "y2023/control_loops/superstructure/superstructure.h"
16
17DEFINE_int32(team, 971, "Team number to use for logfile replay.");
18DEFINE_string(output_folder, "/tmp/superstructure_replay/",
19 "Logs all channels to the provided logfile.");
Maxwell Hendersonb392b742023-03-05 07:53:51 -080020DEFINE_string(arm_trajectories, "arm/arm_trajectories_generated.bfbs",
21 "The path to the generated arm trajectories bfbs file.");
Maxwell Hendersonad312342023-01-10 12:07:47 -080022
23int main(int argc, char **argv) {
24 aos::InitGoogle(&argc, &argv);
25
26 aos::network::OverrideTeamNumber(FLAGS_team);
27
28 // open logfiles
29 aos::logger::LogReader reader(
30 aos::logger::SortParts(aos::logger::FindLogs(argc, argv)));
31 // TODO(james): Actually enforce not sending on the same buses as the logfile
32 // spews out.
33 reader.RemapLoggedChannel("/superstructure",
34 "y2023.control_loops.superstructure.Status");
35 reader.RemapLoggedChannel("/superstructure",
36 "y2023.control_loops.superstructure.Output");
37
38 aos::SimulatedEventLoopFactory factory(reader.configuration());
39 reader.Register(&factory);
40
41 aos::NodeEventLoopFactory *roborio =
42 factory.GetNodeEventLoopFactory("roborio");
43
44 unlink(FLAGS_output_folder.c_str());
45 std::unique_ptr<aos::EventLoop> logger_event_loop =
46 roborio->MakeEventLoop("logger");
47 auto logger = std::make_unique<aos::logger::Logger>(logger_event_loop.get());
48 logger->StartLoggingOnRun(FLAGS_output_folder);
49
Maxwell Hendersonb392b742023-03-05 07:53:51 -080050 auto trajectories =
51 y2023::control_loops::superstructure::Superstructure::GetArmTrajectories(
52 FLAGS_arm_trajectories);
53
54 roborio->OnStartup([roborio, &trajectories]() {
Maxwell Hendersonad312342023-01-10 12:07:47 -080055 roborio->AlwaysStart<y2023::control_loops::superstructure::Superstructure>(
Maxwell Hendersonb392b742023-03-05 07:53:51 -080056 "superstructure",
57 std::make_shared<y2023::constants::Values>(
58 y2023::constants::MakeValues()),
59 trajectories);
Maxwell Hendersonad312342023-01-10 12:07:47 -080060 });
61
62 std::unique_ptr<aos::EventLoop> print_loop = roborio->MakeEventLoop("print");
63 print_loop->SkipAosLog();
64 print_loop->MakeWatcher(
65 "/aos", [&print_loop](const aos::logging::LogMessageFbs &msg) {
66 LOG(INFO) << print_loop->context().monotonic_event_time << " "
67 << aos::FlatbufferToJson(&msg);
68 });
69 print_loop->MakeWatcher(
70 "/superstructure",
71 [&](const y2023::control_loops::superstructure::Status &status) {
72 if (status.estopped()) {
73 LOG(ERROR) << "Estopped";
74 }
75 });
76
77 factory.Run();
78
79 reader.Deregister();
80
81 return 0;
82}