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