blob: 853164fc3fe5a2682f1df364e828459689dc0060 [file] [log] [blame]
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -07001#include "ctre/phoenix/cci/Diagnostics_CCI.h"
2#include "ctre/phoenix6/TalonFX.hpp"
3
4#include "aos/events/shm_event_loop.h"
5#include "aos/init.h"
6#include "frc971/control_loops/control_loops_generated.h"
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -07007#include "frc971/control_loops/swerve/swerve_drivetrain_can_position_static.h"
8#include "frc971/control_loops/swerve/swerve_drivetrain_position_static.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -07009#include "frc971/wpilib/can_sensor_reader.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070010#include "frc971/wpilib/sensor_reader.h"
11#include "frc971/wpilib/swerve/swerve_drivetrain_writer.h"
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080012#include "frc971/wpilib/talonfx.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070013#include "frc971/wpilib/wpilib_robot_base.h"
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070014#include "y2024_swerve/constants.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070015
16DEFINE_bool(ctre_diag_server, false,
17 "If true, enable the diagnostics server for interacting with "
18 "devices on the CAN bus using Phoenix Tuner");
19
20using frc971::wpilib::CANSensorReader;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080021using frc971::wpilib::TalonFX;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070022using frc971::wpilib::swerve::DrivetrainWriter;
23using frc971::wpilib::swerve::SwerveModule;
24
25namespace drivetrain = frc971::control_loops::drivetrain;
26
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070027namespace y2024_swerve::wpilib {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070028namespace {
29
30template <class T>
31T value_or_exit(std::optional<T> optional) {
32 CHECK(optional.has_value());
33 return optional.value();
34}
35
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070036constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
37 constants::Values::kMaxDrivetrainEncoderPulsesPerSecond(),
38});
39static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
40 "fast encoders are too fast");
41} // namespace
42
43class SensorReader : public ::frc971::wpilib::SensorReader {
44 public:
45 SensorReader(aos::ShmEventLoop *event_loop,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070046 std::shared_ptr<const constants::Values> values,
47 frc971::wpilib::swerve::SwerveModules modules)
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070048 : ::frc971::wpilib::SensorReader(event_loop),
49 values_(values),
50 drivetrain_position_sender_(
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070051 event_loop
52 ->MakeSender<frc971::control_loops::swerve::PositionStatic>(
53 "/drivetrain")),
54 modules_(modules) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070055 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
56 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
57 }
58
59 void RunIteration() override {
60 {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070061 auto builder = drivetrain_position_sender_.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070062
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070063 modules_.front_left->PopulatePosition(builder->add_front_left());
64 modules_.front_right->PopulatePosition(builder->add_front_right());
65 modules_.back_left->PopulatePosition(builder->add_back_left());
66 modules_.back_right->PopulatePosition(builder->add_back_right());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070067
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070068 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070069 }
70 }
71
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070072 void set_front_left_encoder(std::unique_ptr<frc::Encoder> encoder,
73 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070074 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070075 modules_.front_left->set_rotation_encoder(std::move(encoder),
76 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070077 }
78
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070079 void set_front_right_encoder(
80 std::unique_ptr<frc::Encoder> encoder,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070081 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070082 fast_encoder_filter_.Add(encoder.get());
83 modules_.front_right->set_rotation_encoder(std::move(encoder),
84 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070085 }
86
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070087 void set_back_left_encoder(std::unique_ptr<frc::Encoder> encoder,
88 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070089 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070090 modules_.back_left->set_rotation_encoder(std::move(encoder),
91 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070092 }
93
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070094 void set_back_right_encoder(std::unique_ptr<frc::Encoder> encoder,
95 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070096 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070097 modules_.back_right->set_rotation_encoder(std::move(encoder),
98 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070099 }
100
101 private:
102 std::shared_ptr<const constants::Values> values_;
103
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700104 aos::Sender<frc971::control_loops::swerve::PositionStatic>
105 drivetrain_position_sender_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700106
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700107 frc971::wpilib::swerve::SwerveModules modules_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700108};
109
110class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
111 public:
112 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
113 return std::make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
114 frc::Encoder::k4X);
115 }
116 void Run() override {
117 std::shared_ptr<const constants::Values> values =
118 std::make_shared<const constants::Values>(constants::MakeValues());
119
120 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
121 aos::configuration::ReadConfig("aos_config.json");
122
123 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800124 std::vector<std::shared_ptr<TalonFX>> falcons;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700125
126 // TODO(max): Change the CanBus names with TalonFX software.
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700127 frc971::wpilib::swerve::SwerveModules modules{
128 .front_left = std::make_shared<SwerveModule>(
129 frc971::wpilib::TalonFXParams{6, false},
130 frc971::wpilib::TalonFXParams{5, false}, "Drivetrain Bus",
131 &signals_registry,
132 constants::Values::kDrivetrainStatorCurrentLimit(),
133 constants::Values::kDrivetrainSupplyCurrentLimit()),
134 .front_right = std::make_shared<SwerveModule>(
135 frc971::wpilib::TalonFXParams{3, false},
136 frc971::wpilib::TalonFXParams{4, false}, "Drivetrain Bus",
137 &signals_registry,
138 constants::Values::kDrivetrainStatorCurrentLimit(),
139 constants::Values::kDrivetrainSupplyCurrentLimit()),
140 .back_left = std::make_shared<SwerveModule>(
141 frc971::wpilib::TalonFXParams{8, false},
142 frc971::wpilib::TalonFXParams{7, false}, "Drivetrain Bus",
143 &signals_registry,
144 constants::Values::kDrivetrainStatorCurrentLimit(),
145 constants::Values::kDrivetrainSupplyCurrentLimit()),
146 .back_right = std::make_shared<SwerveModule>(
147 frc971::wpilib::TalonFXParams{2, false},
148 frc971::wpilib::TalonFXParams{1, false}, "Drivetrain Bus",
149 &signals_registry,
150 constants::Values::kDrivetrainStatorCurrentLimit(),
151 constants::Values::kDrivetrainSupplyCurrentLimit())};
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700152
153 // Thread 1
154 aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
155 can_sensor_reader_event_loop.set_name("CANSensorReader");
156
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700157 modules.PopulateFalconsVector(&falcons);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700158
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700159 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>
160 can_position_sender =
161 can_sensor_reader_event_loop
162 .MakeSender<frc971::control_loops::swerve::CanPositionStatic>(
163 "/drivetrain");
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700164
165 CANSensorReader can_sensor_reader(
166 &can_sensor_reader_event_loop, std::move(signals_registry), falcons,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700167 [this, falcons, modules,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700168 &can_position_sender](ctre::phoenix::StatusCode status) {
169 // TODO(max): use status properly in the flatbuffer.
170 (void)status;
171
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700172 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>::
173 StaticBuilder builder = can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700174
175 for (auto falcon : falcons) {
176 falcon->RefreshNontimesyncedSignals();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700177 }
178
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700179 modules.front_left->PopulateCanPosition(builder->add_front_left());
180 modules.front_right->PopulateCanPosition(builder->add_front_right());
181 modules.back_left->PopulateCanPosition(builder->add_back_left());
182 modules.back_right->PopulateCanPosition(builder->add_back_right());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700183
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800184 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700185 });
186
187 AddLoop(&can_sensor_reader_event_loop);
188
189 // Thread 2
190 // Setup CAN
191 if (!FLAGS_ctre_diag_server) {
192 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
193 c_Phoenix_Diagnostics_Dispose();
194 }
195
196 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
197 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
198 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
199 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
200
201 aos::ShmEventLoop drivetrain_writer_event_loop(&config.message());
202 drivetrain_writer_event_loop.set_name("DrivetrainWriter");
203
204 DrivetrainWriter drivetrain_writer(
205 &drivetrain_writer_event_loop,
206 constants::Values::kDrivetrainWriterPriority, 12);
207
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700208 drivetrain_writer.set_talonfxs(modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700209
210 AddLoop(&drivetrain_writer_event_loop);
211
212 // Thread 3
213 aos::ShmEventLoop sensor_reader_event_loop(&config.message());
214 sensor_reader_event_loop.set_name("SensorReader");
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700215 SensorReader sensor_reader(&sensor_reader_event_loop, values, modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700216
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700217 sensor_reader.set_front_left_encoder(
218 make_encoder(1), std::make_unique<frc::DigitalInput>(1));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700219
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700220 sensor_reader.set_front_right_encoder(
221 make_encoder(0), std::make_unique<frc::DigitalInput>(0));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700222
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700223 sensor_reader.set_back_left_encoder(make_encoder(2),
224 std::make_unique<frc::DigitalInput>(2));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700225
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700226 sensor_reader.set_back_right_encoder(
227 make_encoder(3), std::make_unique<frc::DigitalInput>(3));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700228
229 AddLoop(&sensor_reader_event_loop);
230
231 RunLoops();
232 }
233};
234
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700235} // namespace y2024_swerve::wpilib
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700236
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700237AOS_ROBOT_CLASS(::y2024_swerve::wpilib::WPILibRobot)