blob: f317b5969db4b2412237a0121c274a86f63fc2a4 [file] [log] [blame]
Maxwell Henderson0169d112023-12-25 13:15:16 -08001#include <map>
2#include <string_view>
3
4#include "frc971/can_configuration_generated.h"
Maxwell Henderson0169d112023-12-25 13:15:16 -08005#include "frc971/wpilib/loop_output_handler.h"
Maxwell Henderson10ed5c32024-01-09 12:40:54 -08006#include "frc971/wpilib/talonfx.h"
Maxwell Henderson0169d112023-12-25 13:15:16 -08007
8namespace frc971 {
9namespace wpilib {
10
11/// This class uses a callback whenever it writes so that the caller can use any
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080012/// flatbuffer to write to the talonfx motor.
Maxwell Henderson0169d112023-12-25 13:15:16 -080013template <typename T>
14class GenericCANWriter : public LoopOutputHandler<T> {
15 public:
16 GenericCANWriter(
17 ::aos::EventLoop *event_loop,
Niko Sohmerse8279552024-01-20 14:47:15 -080018 std::function<void(
19 const T &output,
20 std::map<std::string_view, std::shared_ptr<TalonFX>> talonfx_map)>
Maxwell Henderson0169d112023-12-25 13:15:16 -080021 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 Henderson10ed5c32024-01-09 12:40:54 -080030 for (auto &[_, talonfx] : talonfx_map_) {
31 talonfx->PrintConfigs();
Maxwell Henderson0169d112023-12-25 13:15:16 -080032 }
33
34 if (configuration.reapply()) {
35 WriteConfigs();
36 }
37 }
38
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080039 void add_talonfx(std::string_view name, std::shared_ptr<TalonFX> talonfx) {
40 talonfx_map_.insert({name, std::move(talonfx)});
Maxwell Henderson0169d112023-12-25 13:15:16 -080041 }
42
43 static constexpr int kGenericCANWriterPriority = 35;
44
45 private:
46 void WriteConfigs() {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080047 for (auto &[_, talonfx] : talonfx_map_) {
48 talonfx->WriteConfigs();
Maxwell Henderson0169d112023-12-25 13:15:16 -080049 }
50 }
51
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080052 void Write(const T &output) override {
53 write_callback_(output, talonfx_map_);
54 }
Maxwell Henderson0169d112023-12-25 13:15:16 -080055
56 void Stop() override {
57 AOS_LOG(WARNING, "Generic CAN output too old.\n");
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080058 for (auto &[_, talonfx] : talonfx_map_) {
59 talonfx->WriteVoltage(0);
Maxwell Henderson0169d112023-12-25 13:15:16 -080060 }
61 }
62
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080063 // 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 Henderson0169d112023-12-25 13:15:16 -080066
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080067 std::function<void(
68 const T &output,
Niko Sohmerse8279552024-01-20 14:47:15 -080069 std::map<std::string_view, std::shared_ptr<TalonFX>> talonfx_map)>
Maxwell Henderson0169d112023-12-25 13:15:16 -080070 write_callback_;
71};
72
73} // namespace wpilib
74} // namespace frc971