blob: d3393245382ed904de60377a89dae9c6709995c6 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08008namespace frc971::wpilib {
Maxwell Henderson0169d112023-12-25 13:15:16 -08009
10/// This class uses a callback whenever it writes so that the caller can use any
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080011/// flatbuffer to write to the talonfx motor.
Maxwell Henderson0169d112023-12-25 13:15:16 -080012template <typename T>
13class GenericCANWriter : public LoopOutputHandler<T> {
14 public:
15 GenericCANWriter(
16 ::aos::EventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -080017 std::function<void(const T &output,
18 const std::map<std::string_view,
19 std::shared_ptr<TalonFX>> &talonfx_map)>
Maxwell Henderson0169d112023-12-25 13:15:16 -080020 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 Henderson10ed5c32024-01-09 12:40:54 -080029 for (auto &[_, talonfx] : talonfx_map_) {
30 talonfx->PrintConfigs();
Maxwell Henderson0169d112023-12-25 13:15:16 -080031 }
32
33 if (configuration.reapply()) {
34 WriteConfigs();
35 }
36 }
37
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080038 void add_talonfx(std::string_view name, std::shared_ptr<TalonFX> talonfx) {
39 talonfx_map_.insert({name, std::move(talonfx)});
Maxwell Henderson0169d112023-12-25 13:15:16 -080040 }
41
42 static constexpr int kGenericCANWriterPriority = 35;
43
44 private:
45 void WriteConfigs() {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080046 for (auto &[_, talonfx] : talonfx_map_) {
47 talonfx->WriteConfigs();
Maxwell Henderson0169d112023-12-25 13:15:16 -080048 }
49 }
50
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080051 void Write(const T &output) override {
52 write_callback_(output, talonfx_map_);
53 }
Maxwell Henderson0169d112023-12-25 13:15:16 -080054
55 void Stop() override {
56 AOS_LOG(WARNING, "Generic CAN output too old.\n");
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080057 for (auto &[_, talonfx] : talonfx_map_) {
58 talonfx->WriteVoltage(0);
Maxwell Henderson0169d112023-12-25 13:15:16 -080059 }
60 }
61
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080062 // 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 Henderson0169d112023-12-25 13:15:16 -080065
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080066 std::function<void(
67 const T &output,
Niko Sohmers1259b2a2024-01-29 18:00:37 -080068 const std::map<std::string_view, std::shared_ptr<TalonFX>> &talonfx_map)>
Maxwell Henderson0169d112023-12-25 13:15:16 -080069 write_callback_;
70};
71
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080072} // namespace frc971::wpilib