Maxwell Henderson | f63a0d9 | 2023-06-24 14:49:51 -0700 | [diff] [blame^] | 1 | #include "y2023_bot4/swerve_publisher_lib.h" |
| 2 | |
| 3 | y2023_bot4::SwervePublisher::SwervePublisher(aos::EventLoop *event_loop, |
| 4 | aos::ExitHandle *exit_handle, |
| 5 | const std::string &filename, |
| 6 | double duration) |
| 7 | : drivetrain_output_sender_( |
| 8 | event_loop->MakeSender<drivetrain::swerve::Output>("/drivetrain")) { |
| 9 | event_loop |
| 10 | ->AddTimer([this, filename]() { |
| 11 | auto output_builder = drivetrain_output_sender_.MakeBuilder(); |
| 12 | |
| 13 | auto drivetrain_output = |
| 14 | aos::JsonFileToFlatbuffer<drivetrain::swerve::Output>(filename); |
| 15 | |
| 16 | auto copied_flatbuffer = |
| 17 | aos::CopyFlatBuffer<drivetrain::swerve::Output>( |
| 18 | drivetrain_output, output_builder.fbb()); |
| 19 | CHECK(drivetrain_output.Verify()); |
| 20 | |
| 21 | output_builder.CheckOk(output_builder.Send(copied_flatbuffer)); |
| 22 | }) |
| 23 | ->Schedule(event_loop->monotonic_now(), |
| 24 | std::chrono::duration_cast<aos::monotonic_clock::duration>( |
| 25 | std::chrono::milliseconds(5))); |
| 26 | event_loop |
| 27 | ->AddTimer([this, exit_handle]() { |
| 28 | auto builder = drivetrain_output_sender_.MakeBuilder(); |
| 29 | drivetrain::swerve::SwerveModuleOutput::Builder swerve_module_builder = |
| 30 | builder.MakeBuilder<drivetrain::swerve::SwerveModuleOutput>(); |
| 31 | |
| 32 | swerve_module_builder.add_rotation_current(0.0); |
| 33 | swerve_module_builder.add_translation_current(0.0); |
| 34 | |
| 35 | auto swerve_module_offset = swerve_module_builder.Finish(); |
| 36 | |
| 37 | drivetrain::swerve::Output::Builder drivetrain_output_builder = |
| 38 | builder.MakeBuilder<drivetrain::swerve::Output>(); |
| 39 | |
| 40 | drivetrain_output_builder.add_front_left_output(swerve_module_offset); |
| 41 | drivetrain_output_builder.add_front_right_output(swerve_module_offset); |
| 42 | drivetrain_output_builder.add_back_left_output(swerve_module_offset); |
| 43 | drivetrain_output_builder.add_back_right_output(swerve_module_offset); |
| 44 | |
| 45 | builder.CheckOk(builder.Send(drivetrain_output_builder.Finish())); |
| 46 | |
| 47 | exit_handle->Exit(); |
| 48 | }) |
| 49 | ->Schedule(event_loop->monotonic_now() + |
| 50 | std::chrono::milliseconds((int)duration)); |
| 51 | } |