blob: e6280e45a28fa21c1584b4c8bb511c360589d119 [file] [log] [blame]
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001#include "absl/flags/flag.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -07002#include "ctre/phoenix/cci/Diagnostics_CCI.h"
3#include "ctre/phoenix6/TalonFX.hpp"
4
5#include "aos/events/shm_event_loop.h"
6#include "aos/init.h"
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -07007#include "frc971/constants/constants_sender_lib.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -07008#include "frc971/control_loops/control_loops_generated.h"
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -07009#include "frc971/control_loops/swerve/swerve_drivetrain_can_position_static.h"
10#include "frc971/control_loops/swerve/swerve_drivetrain_position_static.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070011#include "frc971/wpilib/can_sensor_reader.h"
James Kuszmaulf40b6772024-06-22 16:04:03 -070012#include "frc971/wpilib/joystick_sender.h"
13#include "frc971/wpilib/pdp_fetcher.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070014#include "frc971/wpilib/sensor_reader.h"
15#include "frc971/wpilib/swerve/swerve_drivetrain_writer.h"
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080016#include "frc971/wpilib/talonfx.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070017#include "frc971/wpilib/wpilib_robot_base.h"
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070018#include "y2024_swerve/constants.h"
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070019#include "y2024_swerve/constants/constants_generated.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070020
Austin Schuh99f7c6a2024-06-25 22:07:44 -070021ABSL_FLAG(bool, ctre_diag_server, false,
22 "If true, enable the diagnostics server for interacting with "
23 "devices on the CAN bus using Phoenix Tuner");
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070024
25using frc971::wpilib::CANSensorReader;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080026using frc971::wpilib::TalonFX;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070027using frc971::wpilib::swerve::DrivetrainWriter;
28using frc971::wpilib::swerve::SwerveModule;
29
30namespace drivetrain = frc971::control_loops::drivetrain;
31
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070032namespace y2024_swerve::wpilib {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070033namespace {
34
35template <class T>
36T value_or_exit(std::optional<T> optional) {
37 CHECK(optional.has_value());
38 return optional.value();
39}
40
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070041constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
42 constants::Values::kMaxDrivetrainEncoderPulsesPerSecond(),
43});
44static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
45 "fast encoders are too fast");
46} // namespace
47
48class SensorReader : public ::frc971::wpilib::SensorReader {
49 public:
50 SensorReader(aos::ShmEventLoop *event_loop,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070051 std::shared_ptr<const constants::Values> values,
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070052 const Constants *robot_constants,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070053 frc971::wpilib::swerve::SwerveModules modules)
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070054 : ::frc971::wpilib::SensorReader(event_loop),
55 values_(values),
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070056 robot_constants_(robot_constants),
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070057 drivetrain_position_sender_(
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070058 event_loop
59 ->MakeSender<frc971::control_loops::swerve::PositionStatic>(
60 "/drivetrain")),
61 modules_(modules) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070062 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
63 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
64 }
65
66 void RunIteration() override {
67 {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070068 auto builder = drivetrain_position_sender_.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070069
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070070 auto swerve_position_constants =
71 robot_constants_->common()->swerve_positions_constants();
72
73 modules_.front_left->PopulatePosition(builder->add_front_left(),
74 swerve_position_constants);
75 modules_.front_right->PopulatePosition(builder->add_front_right(),
76 swerve_position_constants);
77 modules_.back_left->PopulatePosition(builder->add_back_left(),
78 swerve_position_constants);
79 modules_.back_right->PopulatePosition(builder->add_back_right(),
80 swerve_position_constants);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070081
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070082 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070083 }
84 }
85
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070086 void set_front_left_encoder(std::unique_ptr<frc::Encoder> encoder,
87 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070088 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070089 modules_.front_left->set_rotation_encoder(std::move(encoder),
90 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070091 }
92
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070093 void set_front_right_encoder(
94 std::unique_ptr<frc::Encoder> encoder,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070095 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070096 fast_encoder_filter_.Add(encoder.get());
97 modules_.front_right->set_rotation_encoder(std::move(encoder),
98 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070099 }
100
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700101 void set_back_left_encoder(std::unique_ptr<frc::Encoder> encoder,
102 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700103 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700104 modules_.back_left->set_rotation_encoder(std::move(encoder),
105 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700106 }
107
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700108 void set_back_right_encoder(std::unique_ptr<frc::Encoder> encoder,
109 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700110 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700111 modules_.back_right->set_rotation_encoder(std::move(encoder),
112 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700113 }
114
115 private:
116 std::shared_ptr<const constants::Values> values_;
117
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -0700118 const Constants *robot_constants_;
119
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700120 aos::Sender<frc971::control_loops::swerve::PositionStatic>
121 drivetrain_position_sender_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700122
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700123 frc971::wpilib::swerve::SwerveModules modules_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700124};
125
126class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
127 public:
128 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
129 return std::make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
130 frc::Encoder::k4X);
131 }
132 void Run() override {
133 std::shared_ptr<const constants::Values> values =
134 std::make_shared<const constants::Values>(constants::MakeValues());
135
136 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
137 aos::configuration::ReadConfig("aos_config.json");
138
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -0700139 frc971::constants::WaitForConstants<y2024_swerve::Constants>(
140 &config.message());
141
142 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
143
144 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
145 &constant_fetcher_event_loop);
146
147 const Constants *robot_constants = &constants_fetcher.constants();
148
149 AddLoop(&constant_fetcher_event_loop);
150
James Kuszmaulf40b6772024-06-22 16:04:03 -0700151 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
152 ::frc971::wpilib::JoystickSender joystick_sender(
153 &joystick_sender_event_loop);
154 AddLoop(&joystick_sender_event_loop);
155
156 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
157 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
158 AddLoop(&pdp_fetcher_event_loop);
159
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700160 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800161 std::vector<std::shared_ptr<TalonFX>> falcons;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700162
163 // TODO(max): Change the CanBus names with TalonFX software.
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700164 frc971::wpilib::swerve::SwerveModules modules{
165 .front_left = std::make_shared<SwerveModule>(
James Kuszmaul4f1a5502024-06-22 16:04:36 -0700166 frc971::wpilib::TalonFXParams{6, true},
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700167 frc971::wpilib::TalonFXParams{5, false}, "Drivetrain Bus",
168 &signals_registry,
169 constants::Values::kDrivetrainStatorCurrentLimit(),
170 constants::Values::kDrivetrainSupplyCurrentLimit()),
171 .front_right = std::make_shared<SwerveModule>(
James Kuszmaul4f1a5502024-06-22 16:04:36 -0700172 frc971::wpilib::TalonFXParams{3, true},
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700173 frc971::wpilib::TalonFXParams{4, false}, "Drivetrain Bus",
174 &signals_registry,
175 constants::Values::kDrivetrainStatorCurrentLimit(),
176 constants::Values::kDrivetrainSupplyCurrentLimit()),
177 .back_left = std::make_shared<SwerveModule>(
James Kuszmaul4f1a5502024-06-22 16:04:36 -0700178 frc971::wpilib::TalonFXParams{7, true},
179 frc971::wpilib::TalonFXParams{8, false}, "Drivetrain Bus",
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700180 &signals_registry,
181 constants::Values::kDrivetrainStatorCurrentLimit(),
182 constants::Values::kDrivetrainSupplyCurrentLimit()),
183 .back_right = std::make_shared<SwerveModule>(
James Kuszmaul4f1a5502024-06-22 16:04:36 -0700184 frc971::wpilib::TalonFXParams{2, true},
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700185 frc971::wpilib::TalonFXParams{1, false}, "Drivetrain Bus",
186 &signals_registry,
187 constants::Values::kDrivetrainStatorCurrentLimit(),
188 constants::Values::kDrivetrainSupplyCurrentLimit())};
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700189
190 // Thread 1
191 aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
192 can_sensor_reader_event_loop.set_name("CANSensorReader");
193
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700194 modules.PopulateFalconsVector(&falcons);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700195
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700196 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>
197 can_position_sender =
198 can_sensor_reader_event_loop
199 .MakeSender<frc971::control_loops::swerve::CanPositionStatic>(
200 "/drivetrain");
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700201
202 CANSensorReader can_sensor_reader(
203 &can_sensor_reader_event_loop, std::move(signals_registry), falcons,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700204 [this, falcons, modules,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700205 &can_position_sender](ctre::phoenix::StatusCode status) {
206 // TODO(max): use status properly in the flatbuffer.
207 (void)status;
208
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700209 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>::
210 StaticBuilder builder = can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700211
212 for (auto falcon : falcons) {
213 falcon->RefreshNontimesyncedSignals();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700214 }
215
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700216 modules.front_left->PopulateCanPosition(builder->add_front_left());
217 modules.front_right->PopulateCanPosition(builder->add_front_right());
218 modules.back_left->PopulateCanPosition(builder->add_back_left());
219 modules.back_right->PopulateCanPosition(builder->add_back_right());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700220
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800221 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700222 });
223
224 AddLoop(&can_sensor_reader_event_loop);
225
226 // Thread 2
227 // Setup CAN
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700228 if (!absl::GetFlag(FLAGS_ctre_diag_server)) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700229 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
230 c_Phoenix_Diagnostics_Dispose();
231 }
232
233 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
234 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
235 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
236 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
237
238 aos::ShmEventLoop drivetrain_writer_event_loop(&config.message());
239 drivetrain_writer_event_loop.set_name("DrivetrainWriter");
240
241 DrivetrainWriter drivetrain_writer(
242 &drivetrain_writer_event_loop,
243 constants::Values::kDrivetrainWriterPriority, 12);
244
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700245 drivetrain_writer.set_talonfxs(modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700246
247 AddLoop(&drivetrain_writer_event_loop);
248
249 // Thread 3
250 aos::ShmEventLoop sensor_reader_event_loop(&config.message());
251 sensor_reader_event_loop.set_name("SensorReader");
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -0700252 SensorReader sensor_reader(&sensor_reader_event_loop, values,
253 robot_constants, modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700254
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700255 sensor_reader.set_front_left_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700256 make_encoder(3), std::make_unique<frc::DigitalInput>(3));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700257
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700258 sensor_reader.set_front_right_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700259 make_encoder(1), std::make_unique<frc::DigitalInput>(1));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700260
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700261 sensor_reader.set_back_left_encoder(make_encoder(4),
262 std::make_unique<frc::DigitalInput>(4));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700263
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700264 sensor_reader.set_back_right_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700265 make_encoder(0), std::make_unique<frc::DigitalInput>(0));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700266
267 AddLoop(&sensor_reader_event_loop);
268
269 RunLoops();
270 }
271};
272
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700273} // namespace y2024_swerve::wpilib
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700274
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700275AOS_ROBOT_CLASS(::y2024_swerve::wpilib::WPILibRobot)