Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 1 | #include <map> |
| 2 | #include <string_view> |
| 3 | |
| 4 | #include "frc971/can_configuration_generated.h" |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 5 | #include "frc971/wpilib/loop_output_handler.h" |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 6 | #include "frc971/wpilib/talonfx.h" |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 7 | |
| 8 | namespace frc971 { |
| 9 | namespace wpilib { |
| 10 | |
| 11 | /// This class uses a callback whenever it writes so that the caller can use any |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 12 | /// flatbuffer to write to the talonfx motor. |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 13 | template <typename T> |
| 14 | class GenericCANWriter : public LoopOutputHandler<T> { |
| 15 | public: |
| 16 | GenericCANWriter( |
| 17 | ::aos::EventLoop *event_loop, |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 18 | std::function<void(const T &output, |
| 19 | const std::map<std::string_view, |
| 20 | std::shared_ptr<TalonFX>> &talonfx_map)> |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 21 | write_callback) |
| 22 | : LoopOutputHandler<T>(event_loop, "/superstructure"), |
| 23 | write_callback_(write_callback) { |
| 24 | event_loop->SetRuntimeRealtimePriority(kGenericCANWriterPriority); |
| 25 | |
| 26 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 27 | } |
| 28 | |
| 29 | void HandleCANConfiguration(const CANConfiguration &configuration) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 30 | for (auto &[_, talonfx] : talonfx_map_) { |
| 31 | talonfx->PrintConfigs(); |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | if (configuration.reapply()) { |
| 35 | WriteConfigs(); |
| 36 | } |
| 37 | } |
| 38 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 39 | void add_talonfx(std::string_view name, std::shared_ptr<TalonFX> talonfx) { |
| 40 | talonfx_map_.insert({name, std::move(talonfx)}); |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static constexpr int kGenericCANWriterPriority = 35; |
| 44 | |
| 45 | private: |
| 46 | void WriteConfigs() { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 47 | for (auto &[_, talonfx] : talonfx_map_) { |
| 48 | talonfx->WriteConfigs(); |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 52 | void Write(const T &output) override { |
| 53 | write_callback_(output, talonfx_map_); |
| 54 | } |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 55 | |
| 56 | void Stop() override { |
| 57 | AOS_LOG(WARNING, "Generic CAN output too old.\n"); |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 58 | for (auto &[_, talonfx] : talonfx_map_) { |
| 59 | talonfx->WriteVoltage(0); |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 63 | // Maps each name to a talonfx to let the caller retreive them when |
| 64 | // writing |
| 65 | std::map<std::string_view, std::shared_ptr<TalonFX>> talonfx_map_; |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 66 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 67 | std::function<void( |
| 68 | const T &output, |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 69 | const std::map<std::string_view, std::shared_ptr<TalonFX>> &talonfx_map)> |
Maxwell Henderson | 0169d11 | 2023-12-25 13:15:16 -0800 | [diff] [blame] | 70 | write_callback_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace wpilib |
Niko Sohmers | 1259b2a | 2024-01-29 18:00:37 -0800 | [diff] [blame] | 74 | } // namespace frc971 |