blob: e6863b957eb2ea7630b01227b093964e06f63dec [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"
7#include "frc971/control_loops/control_loops_generated.h"
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -07008#include "frc971/control_loops/swerve/swerve_drivetrain_can_position_static.h"
9#include "frc971/control_loops/swerve/swerve_drivetrain_position_static.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070010#include "frc971/wpilib/can_sensor_reader.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070011#include "frc971/wpilib/sensor_reader.h"
12#include "frc971/wpilib/swerve/swerve_drivetrain_writer.h"
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080013#include "frc971/wpilib/talonfx.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070014#include "frc971/wpilib/wpilib_robot_base.h"
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070015#include "y2024_swerve/constants.h"
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070016
Austin Schuh99f7c6a2024-06-25 22:07:44 -070017ABSL_FLAG(bool, ctre_diag_server, false,
18 "If true, enable the diagnostics server for interacting with "
19 "devices on the CAN bus using Phoenix Tuner");
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070020
21using frc971::wpilib::CANSensorReader;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080022using frc971::wpilib::TalonFX;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070023using frc971::wpilib::swerve::DrivetrainWriter;
24using frc971::wpilib::swerve::SwerveModule;
25
26namespace drivetrain = frc971::control_loops::drivetrain;
27
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -070028namespace y2024_swerve::wpilib {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070029namespace {
30
31template <class T>
32T value_or_exit(std::optional<T> optional) {
33 CHECK(optional.has_value());
34 return optional.value();
35}
36
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070037constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
38 constants::Values::kMaxDrivetrainEncoderPulsesPerSecond(),
39});
40static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
41 "fast encoders are too fast");
42} // namespace
43
44class SensorReader : public ::frc971::wpilib::SensorReader {
45 public:
46 SensorReader(aos::ShmEventLoop *event_loop,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070047 std::shared_ptr<const constants::Values> values,
48 frc971::wpilib::swerve::SwerveModules modules)
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070049 : ::frc971::wpilib::SensorReader(event_loop),
50 values_(values),
51 drivetrain_position_sender_(
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070052 event_loop
53 ->MakeSender<frc971::control_loops::swerve::PositionStatic>(
54 "/drivetrain")),
55 modules_(modules) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070056 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
57 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
58 }
59
60 void RunIteration() override {
61 {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070062 auto builder = drivetrain_position_sender_.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070063
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070064 modules_.front_left->PopulatePosition(builder->add_front_left());
65 modules_.front_right->PopulatePosition(builder->add_front_right());
66 modules_.back_left->PopulatePosition(builder->add_back_left());
67 modules_.back_right->PopulatePosition(builder->add_back_right());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070068
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070069 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070070 }
71 }
72
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070073 void set_front_left_encoder(std::unique_ptr<frc::Encoder> encoder,
74 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070075 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070076 modules_.front_left->set_rotation_encoder(std::move(encoder),
77 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070078 }
79
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070080 void set_front_right_encoder(
81 std::unique_ptr<frc::Encoder> encoder,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070082 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070083 fast_encoder_filter_.Add(encoder.get());
84 modules_.front_right->set_rotation_encoder(std::move(encoder),
85 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070086 }
87
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070088 void set_back_left_encoder(std::unique_ptr<frc::Encoder> encoder,
89 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070090 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070091 modules_.back_left->set_rotation_encoder(std::move(encoder),
92 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070093 }
94
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070095 void set_back_right_encoder(std::unique_ptr<frc::Encoder> encoder,
96 std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -070097 fast_encoder_filter_.Add(encoder.get());
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -070098 modules_.back_right->set_rotation_encoder(std::move(encoder),
99 std::move(absolute_pwm));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700100 }
101
102 private:
103 std::shared_ptr<const constants::Values> values_;
104
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700105 aos::Sender<frc971::control_loops::swerve::PositionStatic>
106 drivetrain_position_sender_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700107
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700108 frc971::wpilib::swerve::SwerveModules modules_;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700109};
110
111class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
112 public:
113 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
114 return std::make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
115 frc::Encoder::k4X);
116 }
117 void Run() override {
118 std::shared_ptr<const constants::Values> values =
119 std::make_shared<const constants::Values>(constants::MakeValues());
120
121 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
122 aos::configuration::ReadConfig("aos_config.json");
123
124 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800125 std::vector<std::shared_ptr<TalonFX>> falcons;
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700126
127 // TODO(max): Change the CanBus names with TalonFX software.
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700128 frc971::wpilib::swerve::SwerveModules modules{
129 .front_left = std::make_shared<SwerveModule>(
130 frc971::wpilib::TalonFXParams{6, false},
131 frc971::wpilib::TalonFXParams{5, false}, "Drivetrain Bus",
132 &signals_registry,
133 constants::Values::kDrivetrainStatorCurrentLimit(),
134 constants::Values::kDrivetrainSupplyCurrentLimit()),
135 .front_right = std::make_shared<SwerveModule>(
136 frc971::wpilib::TalonFXParams{3, false},
137 frc971::wpilib::TalonFXParams{4, false}, "Drivetrain Bus",
138 &signals_registry,
139 constants::Values::kDrivetrainStatorCurrentLimit(),
140 constants::Values::kDrivetrainSupplyCurrentLimit()),
141 .back_left = std::make_shared<SwerveModule>(
142 frc971::wpilib::TalonFXParams{8, false},
143 frc971::wpilib::TalonFXParams{7, false}, "Drivetrain Bus",
144 &signals_registry,
145 constants::Values::kDrivetrainStatorCurrentLimit(),
146 constants::Values::kDrivetrainSupplyCurrentLimit()),
147 .back_right = std::make_shared<SwerveModule>(
148 frc971::wpilib::TalonFXParams{2, false},
149 frc971::wpilib::TalonFXParams{1, false}, "Drivetrain Bus",
150 &signals_registry,
151 constants::Values::kDrivetrainStatorCurrentLimit(),
152 constants::Values::kDrivetrainSupplyCurrentLimit())};
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700153
154 // Thread 1
155 aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
156 can_sensor_reader_event_loop.set_name("CANSensorReader");
157
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700158 modules.PopulateFalconsVector(&falcons);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700159
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700160 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>
161 can_position_sender =
162 can_sensor_reader_event_loop
163 .MakeSender<frc971::control_loops::swerve::CanPositionStatic>(
164 "/drivetrain");
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700165
166 CANSensorReader can_sensor_reader(
167 &can_sensor_reader_event_loop, std::move(signals_registry), falcons,
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700168 [this, falcons, modules,
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700169 &can_position_sender](ctre::phoenix::StatusCode status) {
170 // TODO(max): use status properly in the flatbuffer.
171 (void)status;
172
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700173 aos::Sender<frc971::control_loops::swerve::CanPositionStatic>::
174 StaticBuilder builder = can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700175
176 for (auto falcon : falcons) {
177 falcon->RefreshNontimesyncedSignals();
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700178 }
179
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700180 modules.front_left->PopulateCanPosition(builder->add_front_left());
181 modules.front_right->PopulateCanPosition(builder->add_front_right());
182 modules.back_left->PopulateCanPosition(builder->add_back_left());
183 modules.back_right->PopulateCanPosition(builder->add_back_right());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700184
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800185 builder.CheckOk(builder.Send());
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700186 });
187
188 AddLoop(&can_sensor_reader_event_loop);
189
190 // Thread 2
191 // Setup CAN
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700192 if (!absl::GetFlag(FLAGS_ctre_diag_server)) {
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700193 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
194 c_Phoenix_Diagnostics_Dispose();
195 }
196
197 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
198 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
199 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
200 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
201
202 aos::ShmEventLoop drivetrain_writer_event_loop(&config.message());
203 drivetrain_writer_event_loop.set_name("DrivetrainWriter");
204
205 DrivetrainWriter drivetrain_writer(
206 &drivetrain_writer_event_loop,
207 constants::Values::kDrivetrainWriterPriority, 12);
208
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700209 drivetrain_writer.set_talonfxs(modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700210
211 AddLoop(&drivetrain_writer_event_loop);
212
213 // Thread 3
214 aos::ShmEventLoop sensor_reader_event_loop(&config.message());
215 sensor_reader_event_loop.set_name("SensorReader");
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700216 SensorReader sensor_reader(&sensor_reader_event_loop, values, modules);
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700217
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700218 sensor_reader.set_front_left_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700219 make_encoder(3), std::make_unique<frc::DigitalInput>(3));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700220
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700221 sensor_reader.set_front_right_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700222 make_encoder(1), std::make_unique<frc::DigitalInput>(1));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700223
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700224 sensor_reader.set_back_left_encoder(make_encoder(4),
225 std::make_unique<frc::DigitalInput>(4));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700226
Nikolai Sohmers3f2a5072024-06-08 14:05:59 -0700227 sensor_reader.set_back_right_encoder(
James Kuszmaule4a8c6c2024-06-14 18:27:21 -0700228 make_encoder(0), std::make_unique<frc::DigitalInput>(0));
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700229
230 AddLoop(&sensor_reader_event_loop);
231
232 RunLoops();
233 }
234};
235
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700236} // namespace y2024_swerve::wpilib
Maxwell Hendersonf63a0d92023-06-24 14:49:51 -0700237
Nikolai Sohmers3cc1fc22024-05-04 12:27:58 -0700238AOS_ROBOT_CLASS(::y2024_swerve::wpilib::WPILibRobot)