blob: d43b2afb34fe5642153f54df5dd5acf4a64a582d [file] [log] [blame]
James Kuszmaulec0c96b2024-03-17 17:24:35 -07001#include "gflags/gflags.h"
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
20DEFINE_string(config, "y2024/aos_config.json",
21 "Name of the config file to replay using.");
22DEFINE_bool(override_config, false,
23 "If set, override the logged config with --config.");
24DEFINE_int32(team, 971, "Team number to use for logfile replay.");
25DEFINE_string(output_folder, "/tmp/replayed",
26 "Name of the folder to write replayed logs to.");
27DEFINE_string(constants_path, "y2024/constants/constants.json",
28 "Path to the constant file");
29
30int main(int argc, char **argv) {
31 aos::InitGoogle(&argc, &argv);
32
33 aos::network::OverrideTeamNumber(FLAGS_team);
34
35 const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
36 aos::configuration::ReadConfig(FLAGS_config);
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
43 aos::logger::LogReader reader(
44 logfiles, FLAGS_override_config ? &config.message() : nullptr);
45
46 reader.RemapLoggedChannel("/imu/constants", "y2024.Constants");
47 reader.RemapLoggedChannel("/roborio/constants", "y2024.Constants");
48 reader.RemapLoggedChannel("/orin1/constants", "y2024.Constants");
49 reader.RemapLoggedChannel("/drivetrain",
50 "frc971.control_loops.drivetrain.Status");
51 reader.RemapLoggedChannel("/drivetrain",
52 "frc971.control_loops.drivetrain.Output");
53 reader.RemapLoggedChannel(
54 "/drivetrain", "frc971.control_loops.drivetrain.RioLocalizerInputs");
55
56 auto factory =
57 std::make_unique<aos::SimulatedEventLoopFactory>(reader.configuration());
58
59 reader.RegisterWithoutStarting(factory.get());
60
61 y2024::SendSimulationConstants(reader.event_loop_factory(), FLAGS_team,
62 FLAGS_constants_path, {"roborio"});
63
64 const aos::Node *node = nullptr;
65 if (aos::configuration::MultiNode(reader.configuration())) {
66 node = aos::configuration::GetNode(reader.configuration(), "roborio");
67 }
68 std::vector<std::unique_ptr<aos::util::LoggerState>> loggers;
69
70 std::unique_ptr<aos::EventLoop> drivetrain_event_loop;
71 std::unique_ptr<::frc971::control_loops::drivetrain::PuppetLocalizer>
72 localizer;
73 std::unique_ptr<frc971::control_loops::drivetrain::DrivetrainLoop> drivetrain;
74 reader.OnStart(node, [&factory, node, &loggers, &drivetrain_event_loop,
75 &localizer, &drivetrain]() {
76 aos::NodeEventLoopFactory *node_factory =
77 factory->GetNodeEventLoopFactory(node);
78 drivetrain_event_loop = node_factory->MakeEventLoop("drivetrain");
79 const auto drivetrain_config =
80 ::y2024::control_loops::drivetrain::GetDrivetrainConfig(
81 drivetrain_event_loop.get());
82 localizer =
83 std::make_unique<::frc971::control_loops::drivetrain::PuppetLocalizer>(
84 drivetrain_event_loop.get(), drivetrain_config);
85 drivetrain =
86 std::make_unique<frc971::control_loops::drivetrain::DrivetrainLoop>(
87 drivetrain_config, drivetrain_event_loop.get(), localizer.get());
88 loggers.push_back(std::make_unique<aos::util::LoggerState>(
89 factory.get(), node, FLAGS_output_folder));
90 // The Trajectory information is NOT_LOGGED, so we need to rerun it against
91 // the SplineGoals that we have in the log.
92 node_factory
93 ->AlwaysStart<frc971::control_loops::drivetrain::TrajectoryGenerator>(
94 "trajectory_generator", drivetrain_config);
95 });
96
97 reader.event_loop_factory()->Run();
98
99 reader.Deregister();
100
101 return 0;
102}