blob: 8373258cec6c6f9c191b6b1056ff3e0f0b49bd5a [file] [log] [blame]
James Kuszmaul022d40e2020-02-11 17:06:18 -08001// This binary allows us to replay the drivetrain code over existing logfile,
2// primarily for use in testing changes to the localizer code.
3// When you run this code, it generates a new logfile with the data all
4// replayed, so that it can then be run through the plotting tool or analyzed
5// in some other way. The original drivetrain status data will be on the
6// /original/drivetrain channel.
7#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -08008#include "aos/events/logging/log_reader.h"
James Kuszmaulaca88782021-04-24 16:48:45 -07009#include "aos/events/logging/log_writer.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080010#include "aos/events/simulated_event_loop.h"
11#include "aos/init.h"
12#include "aos/json_to_flatbuffer.h"
13#include "aos/network/team_number.h"
James Kuszmaule4853542023-05-15 20:35:48 -070014#include "aos/util/simulation_logger.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080015#include "frc971/control_loops/drivetrain/drivetrain.h"
James Kuszmaul75a18c52021-03-10 22:02:07 -080016#include "frc971/control_loops/drivetrain/trajectory_generator.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080017#include "gflags/gflags.h"
milind-uba4f09a2021-10-11 17:51:22 -070018#include "y2020/constants.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080019#include "y2020/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul68f7d272020-02-22 20:55:02 -080020#include "y2020/control_loops/drivetrain/localizer.h"
James Kuszmaulaca88782021-04-24 16:48:45 -070021#include "y2020/control_loops/superstructure/superstructure.h"
James Kuszmaul022d40e2020-02-11 17:06:18 -080022
Austin Schuhc5fa6d92022-02-25 14:36:28 -080023DEFINE_string(config, "y2020/aos_config.json",
James Kuszmaul022d40e2020-02-11 17:06:18 -080024 "Name of the config file to replay using.");
Austin Schuh375bcbb2021-01-30 16:59:59 -080025DEFINE_string(output_folder, "/tmp/replayed",
James Kuszmaul4f106fb2021-01-05 20:53:02 -080026 "Name of the folder to write replayed logs to.");
James Kuszmaul022d40e2020-02-11 17:06:18 -080027DEFINE_int32(team, 971, "Team number to use for logfile replay.");
James Kuszmaul93f4f032021-01-15 19:51:15 -080028DEFINE_bool(log_all_nodes, false, "Whether to rerun the logger on every node.");
James Kuszmaul4f106fb2021-01-05 20:53:02 -080029
James Kuszmaul4f106fb2021-01-05 20:53:02 -080030// TODO(james): Currently, this replay produces logfiles that can't be read due
31// to time estimation issues. Pending the active refactorings of the
32// timestamp-related code, fix this.
James Kuszmaul022d40e2020-02-11 17:06:18 -080033int main(int argc, char **argv) {
34 aos::InitGoogle(&argc, &argv);
35
36 aos::network::OverrideTeamNumber(FLAGS_team);
37
James Kuszmaul022d40e2020-02-11 17:06:18 -080038 const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
39 aos::configuration::ReadConfig(FLAGS_config);
40
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080041 // find logfiles
42 std::vector<std::string> unsorted_logfiles =
43 aos::logger::FindLogs(argc, argv);
44
45 // sort logfiles
James Kuszmaul4f106fb2021-01-05 20:53:02 -080046 const std::vector<aos::logger::LogFile> logfiles =
47 aos::logger::SortParts(unsorted_logfiles);
Milind Upadhyay6d758aa2020-12-26 16:11:15 -080048
49 // open logfiles
50 aos::logger::LogReader reader(logfiles, &config.message());
James Kuszmaul022d40e2020-02-11 17:06:18 -080051 // TODO(james): Actually enforce not sending on the same buses as the logfile
52 // spews out.
53 reader.RemapLoggedChannel("/drivetrain",
54 "frc971.control_loops.drivetrain.Status");
55 reader.RemapLoggedChannel("/drivetrain",
56 "frc971.control_loops.drivetrain.Output");
James Kuszmaul5ff8a862021-09-25 17:29:43 -070057 reader.RemapLoggedChannel("/drivetrain",
58 "y2020.control_loops.drivetrain.LocalizerDebug");
James Kuszmaulaca88782021-04-24 16:48:45 -070059 reader.RemapLoggedChannel("/superstructure",
60 "y2020.control_loops.superstructure.Status");
61 reader.RemapLoggedChannel("/superstructure",
62 "y2020.control_loops.superstructure.Output");
James Kuszmaul022d40e2020-02-11 17:06:18 -080063 reader.Register();
64
James Kuszmaule4853542023-05-15 20:35:48 -070065 std::vector<std::unique_ptr<aos::util::LoggerState>> loggers;
James Kuszmaul93f4f032021-01-15 19:51:15 -080066 if (FLAGS_log_all_nodes) {
James Kuszmaule4853542023-05-15 20:35:48 -070067 loggers = aos::util::MakeLoggersForAllNodes(reader.event_loop_factory(),
68 FLAGS_output_folder);
James Kuszmaul93f4f032021-01-15 19:51:15 -080069 } else {
70 // List of nodes to create loggers for (note: currently just roborio; this
71 // code was refactored to allow easily adding new loggers to accommodate
72 // debugging and potential future changes).
73 const std::vector<std::string> nodes_to_log = {"roborio"};
James Kuszmaule4853542023-05-15 20:35:48 -070074 loggers = aos::util::MakeLoggersForNodes(reader.event_loop_factory(),
75 nodes_to_log, FLAGS_output_folder);
James Kuszmaul4f106fb2021-01-05 20:53:02 -080076 }
77
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080078 const aos::Node *node = nullptr;
79 if (aos::configuration::MultiNode(reader.configuration())) {
80 node = aos::configuration::GetNode(reader.configuration(), "roborio");
81 }
82
James Kuszmaul75a18c52021-03-10 22:02:07 -080083 std::unique_ptr<aos::EventLoop> trajectory_generator_event_loop =
84 reader.event_loop_factory()->MakeEventLoop("trajectory_generator", node);
85 trajectory_generator_event_loop->SkipTimingReport();
86
milind-uba4f09a2021-10-11 17:51:22 -070087 y2020::constants::InitValues();
88
James Kuszmaul75a18c52021-03-10 22:02:07 -080089 frc971::control_loops::drivetrain::TrajectoryGenerator trajectory_generator(
90 trajectory_generator_event_loop.get(),
91 y2020::control_loops::drivetrain::GetDrivetrainConfig());
92
James Kuszmaul022d40e2020-02-11 17:06:18 -080093 std::unique_ptr<aos::EventLoop> drivetrain_event_loop =
James Kuszmaul5f6d1d42020-03-01 18:10:07 -080094 reader.event_loop_factory()->MakeEventLoop("drivetrain", node);
James Kuszmaul022d40e2020-02-11 17:06:18 -080095 drivetrain_event_loop->SkipTimingReport();
96
James Kuszmaul68f7d272020-02-22 20:55:02 -080097 y2020::control_loops::drivetrain::Localizer localizer(
James Kuszmaul022d40e2020-02-11 17:06:18 -080098 drivetrain_event_loop.get(),
99 y2020::control_loops::drivetrain::GetDrivetrainConfig());
100 frc971::control_loops::drivetrain::DrivetrainLoop drivetrain(
101 y2020::control_loops::drivetrain::GetDrivetrainConfig(),
102 drivetrain_event_loop.get(), &localizer);
103
James Kuszmaulaca88782021-04-24 16:48:45 -0700104 std::unique_ptr<aos::EventLoop> superstructure_event_loop =
105 reader.event_loop_factory()->MakeEventLoop("superstructure", node);
106 superstructure_event_loop->SkipTimingReport();
107
108 y2020::control_loops::superstructure::Superstructure superstructure(
109 superstructure_event_loop.get());
110
James Kuszmaul022d40e2020-02-11 17:06:18 -0800111 reader.event_loop_factory()->Run();
112
James Kuszmaul022d40e2020-02-11 17:06:18 -0800113 return 0;
114}