blob: 3d370d04ef6b9d47e5e145da22d1982ff15ef22b [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"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070012#include "frc971/wpilib/sensor_reader.h"
13#include "frc971/wpilib/swerve/swerve_drivetrain_writer.h"
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080014#include "frc971/wpilib/talonfx.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070015#include "frc971/wpilib/wpilib_robot_base.h"
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070016#include "y2024_swerve/constants.h"
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070017#include "y2024_swerve/constants/constants_generated.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070018
Austin Schuh99f7c6a2024-06-25 22:07:44 -070019ABSL_FLAG(bool, ctre_diag_server, false,
20 "If true, enable the diagnostics server for interacting with "
21 "devices on the CAN bus using Phoenix Tuner");
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070022
23using frc971::wpilib::CANSensorReader;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080024using frc971::wpilib::TalonFX;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070025using frc971::wpilib::swerve::DrivetrainWriter;
26using frc971::wpilib::swerve::SwerveModule;
27
28namespace drivetrain = frc971::control_loops::drivetrain;
29
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070030namespace y2024_swerve::wpilib {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070031namespace {
32
33template <class T>
34T value_or_exit(std::optional<T> optional) {
35 CHECK(optional.has_value());
36 return optional.value();
37}
38
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070039constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
40 constants::Values::kMaxDrivetrainEncoderPulsesPerSecond(),
41});
42static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
43 "fast encoders are too fast");
44} // namespace
45
46class SensorReader : public ::frc971::wpilib::SensorReader {
47 public:
48 SensorReader(aos::ShmEventLoop *event_loop,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070049 std::shared_ptr<const constants::Values> values,
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070050 const Constants *robot_constants,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070051 frc971::wpilib::swerve::SwerveModules modules)
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070052 : ::frc971::wpilib::SensorReader(event_loop),
53 values_(values),
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070054 robot_constants_(robot_constants),
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070055 drivetrain_position_sender_(
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070056 event_loop
57 ->MakeSender<frc971::control_loops::swerve::PositionStatic>(
58 "/drivetrain")),
59 modules_(modules) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070060 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
61 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
62 }
63
64 void RunIteration() override {
65 {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070066 auto builder = drivetrain_position_sender_.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070067
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -070068 auto swerve_position_constants =
69 robot_constants_->common()->swerve_positions_constants();
70
71 modules_.front_left->PopulatePosition(builder->add_front_left(),
72 swerve_position_constants);
73 modules_.front_right->PopulatePosition(builder->add_front_right(),
74 swerve_position_constants);
75 modules_.back_left->PopulatePosition(builder->add_back_left(),
76 swerve_position_constants);
77 modules_.back_right->PopulatePosition(builder->add_back_right(),
78 swerve_position_constants);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070079
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070080 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070081 }
82 }
83
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070084 void set_front_left_encoder(std::unique_ptr<frc::Encoder> encoder,
85 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070086 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070087 modules_.front_left->set_rotation_encoder(std::move(encoder),
88 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070089 }
90
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070091 void set_front_right_encoder(
92 std::unique_ptr<frc::Encoder> encoder,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070093 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070094 fast_encoder_filter_.Add(encoder.get());
95 modules_.front_right->set_rotation_encoder(std::move(encoder),
96 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070097 }
98
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070099 void set_back_left_encoder(std::unique_ptr<frc::Encoder> encoder,
100 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700101 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700102 modules_.back_left->set_rotation_encoder(std::move(encoder),
103 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700104 }
105
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700106 void set_back_right_encoder(std::unique_ptr<frc::Encoder> encoder,
107 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700108 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700109 modules_.back_right->set_rotation_encoder(std::move(encoder),
110 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700111 }
112
113 private:
114 std::shared_ptr<const constants::Values> values_;
115
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -0700116 const Constants *robot_constants_;
117
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700118 aos::Sender<frc971::control_loops::swerve::PositionStatic>
119 drivetrain_position_sender_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700120
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700121 frc971::wpilib::swerve::SwerveModules modules_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700122};
123
124class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
125 public:
126 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
127 return std::make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
128 frc::Encoder::k4X);
129 }
130 void Run() override {
131 std::shared_ptr<const constants::Values> values =
132 std::make_shared<const constants::Values>(constants::MakeValues());
133
134 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
135 aos::configuration::ReadConfig("aos_config.json");
136
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -0700137 frc971::constants::WaitForConstants<y2024_swerve::Constants>(
138 &config.message());
139
140 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
141
142 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
143 &constant_fetcher_event_loop);
144
145 const Constants *robot_constants = &constants_fetcher.constants();
146
147 AddLoop(&constant_fetcher_event_loop);
148
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700149 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800150 std::vector<std::shared_ptr<TalonFX>> falcons;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700151
152 // TODO(max): Change the CanBus names with TalonFX software.
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700153 frc971::wpilib::swerve::SwerveModules modules{
154 .front_left = std::make_shared<SwerveModule>(
155 frc971::wpilib::TalonFXParams{6, false},
156 frc971::wpilib::TalonFXParams{5, false}, "Drivetrain Bus",
157 &signals_registry,
158 constants::Values::kDrivetrainStatorCurrentLimit(),
159 constants::Values::kDrivetrainSupplyCurrentLimit()),
160 .front_right = std::make_shared<SwerveModule>(
161 frc971::wpilib::TalonFXParams{3, false},
162 frc971::wpilib::TalonFXParams{4, false}, "Drivetrain Bus",
163 &signals_registry,
164 constants::Values::kDrivetrainStatorCurrentLimit(),
165 constants::Values::kDrivetrainSupplyCurrentLimit()),
166 .back_left = std::make_shared<SwerveModule>(
167 frc971::wpilib::TalonFXParams{8, false},
168 frc971::wpilib::TalonFXParams{7, false}, "Drivetrain Bus",
169 &signals_registry,
170 constants::Values::kDrivetrainStatorCurrentLimit(),
171 constants::Values::kDrivetrainSupplyCurrentLimit()),
172 .back_right = std::make_shared<SwerveModule>(
173 frc971::wpilib::TalonFXParams{2, false},
174 frc971::wpilib::TalonFXParams{1, false}, "Drivetrain Bus",
175 &signals_registry,
176 constants::Values::kDrivetrainStatorCurrentLimit(),
177 constants::Values::kDrivetrainSupplyCurrentLimit())};
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700178
179 // Thread 1
180 aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
181 can_sensor_reader_event_loop.set_name("CANSensorReader");
182
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700183 modules.PopulateFalconsVector(&falcons);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700184
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700185 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>
186 can_position_sender =
187 can_sensor_reader_event_loop
188 .MakeSender<frc971::control_loops::swerve::CanPositionStatic>(
189 "/drivetrain");
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700190
191 CANSensorReader can_sensor_reader(
192 &can_sensor_reader_event_loop, std::move(signals_registry), falcons,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700193 [this, falcons, modules,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700194 &can_position_sender](ctre::phoenix::StatusCode status) {
195 // TODO(max): use status properly in the flatbuffer.
196 (void)status;
197
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700198 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>::
199 StaticBuilder builder = can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700200
201 for (auto falcon : falcons) {
202 falcon->RefreshNontimesyncedSignals();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700203 }
204
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700205 modules.front_left->PopulateCanPosition(builder->add_front_left());
206 modules.front_right->PopulateCanPosition(builder->add_front_right());
207 modules.back_left->PopulateCanPosition(builder->add_back_left());
208 modules.back_right->PopulateCanPosition(builder->add_back_right());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700209
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800210 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700211 });
212
213 AddLoop(&can_sensor_reader_event_loop);
214
215 // Thread 2
216 // Setup CAN
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700217 if (!absl::GetFlag(FLAGS_ctre_diag_server)) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700218 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
219 c_Phoenix_Diagnostics_Dispose();
220 }
221
222 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
223 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
224 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
225 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
226
227 aos::ShmEventLoop drivetrain_writer_event_loop(&config.message());
228 drivetrain_writer_event_loop.set_name("DrivetrainWriter");
229
230 DrivetrainWriter drivetrain_writer(
231 &drivetrain_writer_event_loop,
232 constants::Values::kDrivetrainWriterPriority, 12);
233
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700234 drivetrain_writer.set_talonfxs(modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700235
236 AddLoop(&drivetrain_writer_event_loop);
237
238 // Thread 3
239 aos::ShmEventLoop sensor_reader_event_loop(&config.message());
240 sensor_reader_event_loop.set_name("SensorReader");
Nikolai Sohmersce9ee6e2024-07-06 17:16:20 -0700241 SensorReader sensor_reader(&sensor_reader_event_loop, values,
242 robot_constants, modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700243
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700244 sensor_reader.set_front_left_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700245 make_encoder(3), std::make_unique<frc::DigitalInput>(3));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700246
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700247 sensor_reader.set_front_right_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700248 make_encoder(1), std::make_unique<frc::DigitalInput>(1));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700249
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700250 sensor_reader.set_back_left_encoder(make_encoder(4),
251 std::make_unique<frc::DigitalInput>(4));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700252
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700253 sensor_reader.set_back_right_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700254 make_encoder(0), std::make_unique<frc::DigitalInput>(0));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700255
256 AddLoop(&sensor_reader_event_loop);
257
258 RunLoops();
259 }
260};
261
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700262} // namespace y2024_swerve::wpilib
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700263
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700264AOS_ROBOT_CLASS(::y2024_swerve::wpilib::WPILibRobot)