blob: 2eb9cce79eb520fde7424fc568b44cb1c2e8d72c [file] [log] [blame]
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -07001#include "y2024_swerve/swerve_publisher_lib.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -07002
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -07003y2024_swerve::SwervePublisher::SwervePublisher(aos::EventLoop *event_loop,
4 aos::ExitHandle *exit_handle,
5 const std::string &filename,
6 double duration)
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -07007 : drivetrain_output_sender_(
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -07008 event_loop->MakeSender<frc971::control_loops::swerve::Output>(
9 "/drivetrain")) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070010 event_loop
11 ->AddTimer([this, filename]() {
12 auto output_builder = drivetrain_output_sender_.MakeBuilder();
13
14 auto drivetrain_output =
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070015 aos::JsonFileToFlatbuffer<frc971::control_loops::swerve::Output>(
16 filename);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070017
18 auto copied_flatbuffer =
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070019 aos::CopyFlatBuffer<frc971::control_loops::swerve::Output>(
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070020 drivetrain_output, output_builder.fbb());
21 CHECK(drivetrain_output.Verify());
22
23 output_builder.CheckOk(output_builder.Send(copied_flatbuffer));
24 })
25 ->Schedule(event_loop->monotonic_now(),
26 std::chrono::duration_cast<aos::monotonic_clock::duration>(
27 std::chrono::milliseconds(5)));
28 event_loop
29 ->AddTimer([this, exit_handle]() {
30 auto builder = drivetrain_output_sender_.MakeBuilder();
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070031 frc971::control_loops::swerve::SwerveModuleOutput::Builder
32 swerve_module_builder = builder.MakeBuilder<
33 frc971::control_loops::swerve::SwerveModuleOutput>();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070034
35 swerve_module_builder.add_rotation_current(0.0);
36 swerve_module_builder.add_translation_current(0.0);
37
38 auto swerve_module_offset = swerve_module_builder.Finish();
39
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070040 frc971::control_loops::swerve::Output::Builder
41 drivetrain_output_builder =
42 builder.MakeBuilder<frc971::control_loops::swerve::Output>();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070043
44 drivetrain_output_builder.add_front_left_output(swerve_module_offset);
45 drivetrain_output_builder.add_front_right_output(swerve_module_offset);
46 drivetrain_output_builder.add_back_left_output(swerve_module_offset);
47 drivetrain_output_builder.add_back_right_output(swerve_module_offset);
48
49 builder.CheckOk(builder.Send(drivetrain_output_builder.Finish()));
50
51 exit_handle->Exit();
52 })
53 ->Schedule(event_loop->monotonic_now() +
54 std::chrono::milliseconds((int)duration));
55}