blob: 2213c16b2488f307a036d143e395059d84786cbf [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
Stephan Massaltd021f972020-01-05 20:41:23 -08006#include <cmath>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <cstdio>
8#include <cstring>
Stephan Massaltd021f972020-01-05 20:41:23 -08009#include <functional>
Tyler Chatowbf0609c2021-07-31 16:13:27 -070010#include <memory>
Stephan Massaltd021f972020-01-05 20:41:23 -080011#include <mutex>
12#include <thread>
13
Stephan Massaltd021f972020-01-05 20:41:23 -080014#include "frc971/wpilib/ahal/AnalogInput.h"
15#include "frc971/wpilib/ahal/Counter.h"
16#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
17#include "frc971/wpilib/ahal/DriverStation.h"
18#include "frc971/wpilib/ahal/Encoder.h"
Alex Perryc4691f52020-02-17 19:20:01 -080019#include "frc971/wpilib/ahal/TalonFX.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080020#include "frc971/wpilib/ahal/VictorSP.h"
21#undef ERROR
22
Austin Schuh99f7c6a2024-06-25 22:07:44 -070023#include "absl/flags/flag.h"
Maxwell Henderson1c0843c2023-12-22 16:20:59 -080024#include "ctre/phoenix6/TalonFX.hpp"
Philipp Schrader790cb542023-07-05 21:06:52 -070025
Stephan Massaltd021f972020-01-05 20:41:23 -080026#include "aos/commonmath.h"
27#include "aos/events/event_loop.h"
28#include "aos/events/shm_event_loop.h"
29#include "aos/init.h"
30#include "aos/logging/logging.h"
Austin Schuh83873c32020-02-22 14:58:39 -080031#include "aos/network/team_number.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080032#include "aos/realtime.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080033#include "aos/time/time.h"
34#include "aos/util/log_interval.h"
35#include "aos/util/phased_loop.h"
36#include "aos/util/wrapping_counter.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080037#include "frc971/autonomous/auto_mode_generated.h"
38#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070039#include "frc971/input/robot_state_generated.h"
James Kuszmaul0a981402021-10-09 21:00:34 -070040#include "frc971/wpilib/ADIS16448.h"
James Kuszmaula244a912020-01-18 13:50:50 -080041#include "frc971/wpilib/ADIS16470.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080042#include "frc971/wpilib/buffered_pcm.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/dma.h"
45#include "frc971/wpilib/drivetrain_writer.h"
46#include "frc971/wpilib/encoder_and_potentiometer.h"
47#include "frc971/wpilib/joystick_sender.h"
48#include "frc971/wpilib/logging_generated.h"
49#include "frc971/wpilib/loop_output_handler.h"
50#include "frc971/wpilib/pdp_fetcher.h"
51#include "frc971/wpilib/sensor_reader.h"
52#include "frc971/wpilib/wpilib_robot_base.h"
53#include "y2020/constants.h"
milind-u4b31c4d2021-09-18 16:08:23 -070054#include "y2020/control_loops/superstructure/shooter/shooter_tuning_readings_generated.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080055#include "y2020/control_loops/superstructure/superstructure_output_generated.h"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080056#include "y2020/control_loops/superstructure/superstructure_position_static.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080057
Austin Schuh99f7c6a2024-06-25 22:07:44 -070058ABSL_FLAG(bool, shooter_tuning, true,
59 "If true, reads from ball beambreak sensors and sends shooter "
60 "tuning readings");
milind-u4b31c4d2021-09-18 16:08:23 -070061
Stephan Massaltd021f972020-01-05 20:41:23 -080062using ::aos::monotonic_clock;
63using ::y2020::constants::Values;
64namespace superstructure = ::y2020::control_loops::superstructure;
65namespace chrono = ::std::chrono;
Vinay Sivae52a6b32021-07-10 15:19:26 -070066using std::make_unique;
Stephan Massaltd021f972020-01-05 20:41:23 -080067
Stephan Pleinesf63bde82024-01-13 15:59:33 -080068namespace y2020::wpilib {
Stephan Massaltd021f972020-01-05 20:41:23 -080069namespace {
70
71constexpr double kMaxBringupPower = 12.0;
72
73// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
74// DMA stuff and then removing the * 2.0 in *_translate.
75// The low bit is direction.
76
Stephan Massaltd021f972020-01-05 20:41:23 -080077double drivetrain_translate(int32_t in) {
78 return ((static_cast<double>(in) /
79 Values::kDrivetrainEncoderCountsPerRevolution()) *
80 (2.0 * M_PI)) *
81 Values::kDrivetrainEncoderRatio() *
82 control_loops::drivetrain::kWheelRadius;
83}
84
85double drivetrain_velocity_translate(double in) {
86 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
87 (2.0 * M_PI)) *
88 Values::kDrivetrainEncoderRatio() *
89 control_loops::drivetrain::kWheelRadius;
90}
91
Alex Perryc4691f52020-02-17 19:20:01 -080092double turret_pot_translate(double voltage) {
93 return voltage * Values::kTurretPotRatio() *
94 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
95}
96
Stephan Massaltd021f972020-01-05 20:41:23 -080097constexpr double kMaxFastEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -080098 std::max({Values::kMaxControlPanelEncoderPulsesPerSecond(),
99 Values::kMaxFinisherEncoderPulsesPerSecond(),
100 Values::kMaxAcceleratorEncoderPulsesPerSecond()});
101static_assert(kMaxFastEncoderPulsesPerSecond <= 1000000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -0800102 "fast encoders are too fast");
Alex Perryc4691f52020-02-17 19:20:01 -0800103constexpr double kMaxMediumEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -0800104 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
105 Values::kMaxHoodEncoderPulsesPerSecond(),
106 Values::kMaxIntakeEncoderPulsesPerSecond(),
107 Values::kMaxTurretEncoderPulsesPerSecond()});
Stephan Massaltd021f972020-01-05 20:41:23 -0800108
Austin Schuh9dcd5202020-02-20 20:06:04 -0800109static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -0800110 "medium encoders are too fast");
111
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800112void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) {
113 ctre::phoenix6::configs::TalonFXConfiguration configuration;
114 ctre::phoenix::StatusCode status =
115 talon->GetConfigurator().Refresh(configuration);
116 if (!status.IsOK()) {
117 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
118 status.GetName(), status.GetDescription());
119 }
120 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
121}
122
123void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon,
124 double stator_current_limit, double supply_current_limit) {
125 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
126 current_limits.StatorCurrentLimit = stator_current_limit;
127 current_limits.StatorCurrentLimitEnable = true;
128 current_limits.SupplyCurrentLimit = supply_current_limit;
129 current_limits.SupplyCurrentLimitEnable = true;
130
131 ctre::phoenix6::configs::TalonFXConfiguration configuration;
132 configuration.CurrentLimits = current_limits;
133
134 ctre::phoenix::StatusCode status =
135 talon->GetConfigurator().Apply(configuration);
136 if (!status.IsOK()) {
137 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
138 status.GetName(), status.GetDescription());
139 }
140
141 PrintConfigs(talon);
142}
143
144void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
145 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
146 stop_command.UpdateFreqHz = 0_Hz;
147 stop_command.EnableFOC = true;
148
149 talon->SetControl(stop_command);
150}
151
Stephan Massaltd021f972020-01-05 20:41:23 -0800152} // namespace
153
154// Class to send position messages with sensor readings to our loops.
155class SensorReader : public ::frc971::wpilib::SensorReader {
156 public:
157 SensorReader(::aos::ShmEventLoop *event_loop)
158 : ::frc971::wpilib::SensorReader(event_loop),
159 auto_mode_sender_(
160 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
161 "/autonomous")),
162 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800163 event_loop->MakeSender<superstructure::PositionStatic>(
Stephan Massaltd021f972020-01-05 20:41:23 -0800164 "/superstructure")),
165 drivetrain_position_sender_(
166 event_loop
167 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
milind-u4b31c4d2021-09-18 16:08:23 -0700168 "/drivetrain")),
169 shooter_tuning_readings_sender_(
170 event_loop->MakeSender<superstructure::shooter::TuningReadings>(
171 "/superstructure")) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800172 // Set to filter out anything shorter than 1/4 of the minimum pulse width
173 // we should ever see.
174 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
175 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
milind-u62d4a8e2021-10-11 16:08:41 -0700176
177 constants::InitValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800178 }
179
Alex Perryc4691f52020-02-17 19:20:01 -0800180 // Hood
Alex Perryc4691f52020-02-17 19:20:01 -0800181 void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) {
182 medium_encoder_filter_.Add(encoder.get());
183 hood_encoder_.set_encoder(::std::move(encoder));
184 }
185
186 void set_hood_absolute_pwm(
187 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
188 hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
189 }
190
Ravago Jones937587c2020-12-26 17:21:09 -0800191 void set_hood_single_turn_absolute_pwm(
192 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
193 hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm));
194 }
195
Alex Perryc4691f52020-02-17 19:20:01 -0800196 // Intake
197
198 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
199 medium_encoder_filter_.Add(encoder.get());
200 intake_joint_encoder_.set_encoder(::std::move(encoder));
201 }
202
203 void set_intake_absolute_pwm(
204 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
205 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
206 }
207
208 // Turret
209
210 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
211 medium_encoder_filter_.Add(encoder.get());
212 turret_encoder_.set_encoder(::std::move(encoder));
213 }
214
215 void set_turret_absolute_pwm(
216 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
217 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
218 }
219
220 void set_turret_potentiometer(
221 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
222 turret_encoder_.set_potentiometer(::std::move(potentiometer));
223 }
224
225 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800226 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800227 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800228 finisher_encoder_ = ::std::move(encoder);
229 }
230 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
231 fast_encoder_filter_.Add(encoder.get());
232 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800233 }
234
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800235 void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800236 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800237 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800238 }
239
Stephan Massaltd021f972020-01-05 20:41:23 -0800240 // Auto mode switches.
241 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800242 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800243 autonomous_modes_.at(i) = ::std::move(sensor);
244 }
245
James Kuszmaula244a912020-01-18 13:50:50 -0800246 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
247
milind-u4b31c4d2021-09-18 16:08:23 -0700248 void set_ball_beambreak_inputs(::std::unique_ptr<frc::DigitalInput> sensor1,
249 ::std::unique_ptr<frc::DigitalInput> sensor2) {
250 ball_beambreak_inputs_[0] = ::std::move(sensor1);
251 ball_beambreak_inputs_[1] = ::std::move(sensor2);
Austin Schuh6c053ef2021-09-26 14:32:16 -0700252 ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get());
253 ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get());
milind-u4b31c4d2021-09-18 16:08:23 -0700254 }
255
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700256 void set_ball_intake_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
257 ball_intake_beambreak_ = ::std::move(sensor);
258 }
259
milind-u4b31c4d2021-09-18 16:08:23 -0700260 void Start() override {
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700261 if (absl::GetFlag(FLAGS_shooter_tuning)) {
milind-u4b31c4d2021-09-18 16:08:23 -0700262 AddToDMA(&ball_beambreak_reader_);
263 }
264 }
265
Stephan Massaltd021f972020-01-05 20:41:23 -0800266 void RunIteration() override {
James Kuszmaul0a981402021-10-09 21:00:34 -0700267 if (imu_ != nullptr) {
268 imu_->DoReads();
269 }
James Kuszmaula244a912020-01-18 13:50:50 -0800270
Stephan Massaltd021f972020-01-05 20:41:23 -0800271 {
272 auto builder = drivetrain_position_sender_.MakeBuilder();
273 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
274 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
275 drivetrain_builder.add_left_encoder(
276 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
277 drivetrain_builder.add_left_speed(
278 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
279
280 drivetrain_builder.add_right_encoder(
281 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
282 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
283 drivetrain_right_encoder_->GetPeriod()));
284
milind1f1dca32021-07-03 13:50:07 -0700285 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Stephan Massaltd021f972020-01-05 20:41:23 -0800286 }
Austin Schuh37d2c302021-10-12 22:07:43 -0700287 const constants::Values &values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800288
289 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800290 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
291 superstructure_position_sender_.MakeStaticBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800292
Alex Perryc4691f52020-02-17 19:20:01 -0800293 // TODO(alex): check new absolute encoder api.
294 // Hood
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800295 CopyPosition(hood_encoder_, builder->add_hood(),
Alex Perryc4691f52020-02-17 19:20:01 -0800296 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800297 Values::kHoodEncoderRatio(),
298 Values::kHoodSingleTurnEncoderRatio(), false);
Alex Perryc4691f52020-02-17 19:20:01 -0800299 // Intake
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800300 CopyPosition(intake_joint_encoder_, builder->add_intake_joint(),
Alex Perryc4691f52020-02-17 19:20:01 -0800301 Values::kIntakeEncoderCountsPerRevolution(),
302 Values::kIntakeEncoderRatio(), false);
Alex Perryc4691f52020-02-17 19:20:01 -0800303 // Turret
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800304 CopyPosition(turret_encoder_, builder->add_turret(),
Alex Perryc4691f52020-02-17 19:20:01 -0800305 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800306 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800307 values.turret.potentiometer_offset);
Alex Perryc4691f52020-02-17 19:20:01 -0800308 // Shooter
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800309 y2020::control_loops::superstructure::ShooterPositionStatic *shooter =
310 builder->add_shooter();
311 shooter->set_theta_finisher(
Austin Schuh0ad31d72021-03-06 17:07:04 -0800312 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800313 Values::kFinisherEncoderCountsPerRevolution(),
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800314 Values::kFinisherEncoderRatio()));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800315
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800316 shooter->set_theta_accelerator_left(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800317 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800318 Values::kAcceleratorEncoderCountsPerRevolution(),
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800319 Values::kAcceleratorEncoderRatio()));
320 shooter->set_theta_accelerator_right(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800321 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800322 Values::kAcceleratorEncoderCountsPerRevolution(),
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800323 Values::kAcceleratorEncoderRatio()));
324 builder->set_intake_beambreak_triggered(ball_intake_beambreak_->Get());
Alex Perryc4691f52020-02-17 19:20:01 -0800325
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800326 builder.CheckOk(builder.Send());
Stephan Massaltd021f972020-01-05 20:41:23 -0800327 }
328
329 {
330 auto builder = auto_mode_sender_.MakeBuilder();
331
332 uint32_t mode = 0;
333 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
334 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
335 mode |= 1 << i;
336 }
337 }
338
339 auto auto_mode_builder =
340 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
341
342 auto_mode_builder.add_mode(mode);
343
milind1f1dca32021-07-03 13:50:07 -0700344 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
Stephan Massaltd021f972020-01-05 20:41:23 -0800345 }
milind-u4b31c4d2021-09-18 16:08:23 -0700346
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700347 if (absl::GetFlag(FLAGS_shooter_tuning)) {
milind-u4b31c4d2021-09-18 16:08:23 -0700348 // Distance between beambreak sensors, in meters.
349 constexpr double kDistanceBetweenBeambreaks = 0.4813;
350
Austin Schuh6c053ef2021-09-26 14:32:16 -0700351 if (ball_beambreak_reader_.pulses_detected() > balls_detected_) {
352 balls_detected_ = ball_beambreak_reader_.pulses_detected();
353
354 auto builder = shooter_tuning_readings_sender_.MakeBuilder();
355 auto shooter_tuning_readings_builder =
356 builder.MakeBuilder<superstructure::shooter::TuningReadings>();
357 shooter_tuning_readings_builder.add_velocity_ball(
358 kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width());
milind1f1dca32021-07-03 13:50:07 -0700359 builder.CheckOk(builder.Send(shooter_tuning_readings_builder.Finish()));
Austin Schuh6c053ef2021-09-26 14:32:16 -0700360 }
milind-u4b31c4d2021-09-18 16:08:23 -0700361 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800362 }
363
364 private:
365 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800366 ::aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800367 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
368 drivetrain_position_sender_;
milind-u4b31c4d2021-09-18 16:08:23 -0700369 ::aos::Sender<superstructure::shooter::TuningReadings>
370 shooter_tuning_readings_sender_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800371
Alex Perryc4691f52020-02-17 19:20:01 -0800372 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
373
Ravago Jones937587c2020-12-26 17:21:09 -0800374 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
375
376 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800377
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800378 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
milind-u3d68aa72021-09-26 12:19:03 -0700379 left_accelerator_encoder_, right_accelerator_encoder_;
milind-u9aa62a82021-10-03 20:43:19 -0700380
Stephan Massaltd021f972020-01-05 20:41:23 -0800381 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800382
383 frc971::wpilib::ADIS16470 *imu_ = nullptr;
milind-u4b31c4d2021-09-18 16:08:23 -0700384
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700385 std::unique_ptr<frc::DigitalInput> ball_intake_beambreak_;
386
milind-u4b31c4d2021-09-18 16:08:23 -0700387 // Used to interface with the two beam break sensors that the ball for tuning
388 // shooter parameters has to pass through.
389 // We will time how long it takes to pass between the two sensors to get its
390 // velocity.
391 std::array<std::unique_ptr<frc::DigitalInput>, 2> ball_beambreak_inputs_;
392 frc971::wpilib::DMAPulseSeparationReader ball_beambreak_reader_;
393 int balls_detected_ = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800394};
395
396class SuperstructureWriter
397 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
398 public:
399 SuperstructureWriter(::aos::EventLoop *event_loop)
400 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
401 event_loop, "/superstructure") {}
402
Alex Perryc4691f52020-02-17 19:20:01 -0800403 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
404 hood_victor_ = ::std::move(t);
405 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800406
Alex Perryc4691f52020-02-17 19:20:01 -0800407 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
408 intake_joint_victor_ = ::std::move(t);
409 }
410
411 void set_intake_roller_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800412 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800413 intake_roller_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800414
415 WriteConfigs(intake_roller_falcon_.get(),
416 Values::kIntakeRollerStatorCurrentLimit(),
417 Values::kIntakeRollerSupplyCurrentLimit());
Alex Perryc4691f52020-02-17 19:20:01 -0800418 }
419
420 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
421 turret_victor_ = ::std::move(t);
422 }
423
Ravago Jones3dda5602021-03-10 00:33:13 -0800424 void set_feeder_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800425 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800426 feeder_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800427
428 WriteConfigs(feeder_falcon_.get(), Values::kFeederStatorCurrentLimit(),
429 Values::kFeederSupplyCurrentLimit());
Alex Perryc4691f52020-02-17 19:20:01 -0800430 }
431
milind-u9aa62a82021-10-03 20:43:19 -0700432 void set_washing_machine_control_panel_victor(
433 ::std::unique_ptr<::frc::VictorSP> t) {
434 washing_machine_control_panel_victor_ = ::std::move(t);
435 }
436
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800437 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
438 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800439 }
440
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800441 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
442 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800443 }
444
Austin Schuh0ad31d72021-03-06 17:07:04 -0800445 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
446 finisher_falcon0_ = ::std::move(t);
447 }
448
449 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
450 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800451 }
452
453 void set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800454 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800455 climber_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800456 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
457 current_limits.SupplyCurrentLimit = Values::kClimberSupplyCurrentLimit();
458 current_limits.SupplyCurrentLimitEnable = true;
459
460 ctre::phoenix6::configs::TalonFXConfiguration configuration;
461 configuration.CurrentLimits = current_limits;
462
463 ctre::phoenix::StatusCode status =
464 climber_falcon_->GetConfigurator().Apply(configuration);
465 if (!status.IsOK()) {
466 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
467 status.GetName(), status.GetDescription());
468 }
469
470 PrintConfigs(climber_falcon_.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800471 }
472
473 private:
474 void Write(const superstructure::Output &output) override {
Austin Schuh2efe1682021-03-06 22:47:15 -0800475 hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower,
476 kMaxBringupPower) /
477 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800478
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800479 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
480 -kMaxBringupPower,
481 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800482 12.0);
483
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800484 WriteCan(-output.intake_roller_voltage(), intake_roller_falcon_.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800485
milind-uf70e8e12021-10-02 12:36:00 -0700486 turret_victor_->SetSpeed(std::clamp(output.turret_voltage(),
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800487 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800488 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800489 WriteCan(output.feeder_voltage(), feeder_falcon_.get());
milind-u9aa62a82021-10-03 20:43:19 -0700490 if (washing_machine_control_panel_victor_) {
491 washing_machine_control_panel_victor_->SetSpeed(
492 std::clamp(-output.washing_machine_spinner_voltage(),
493 -kMaxBringupPower, kMaxBringupPower) /
494 12.0);
495 }
Alex Perryc4691f52020-02-17 19:20:01 -0800496
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800497 accelerator_left_falcon_->SetSpeed(
498 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
499 kMaxBringupPower) /
500 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800501
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800502 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800503 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800504 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800505 12.0);
506
Austin Schuh0ad31d72021-03-06 17:07:04 -0800507 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
508 -kMaxBringupPower,
509 kMaxBringupPower) /
510 12.0);
511 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
512 -kMaxBringupPower,
513 kMaxBringupPower) /
514 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800515
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800516 if (climber_falcon_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800517 WriteCan(output.climber_voltage(), climber_falcon_.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800518 }
Alex Perryc4691f52020-02-17 19:20:01 -0800519 }
520
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800521 static void WriteCan(const double voltage,
522 ::ctre::phoenix6::hardware::TalonFX *falcon) {
523 ctre::phoenix6::controls::DutyCycleOut control(
524 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
525 control.UpdateFreqHz = 0_Hz;
526 control.EnableFOC = true;
527
528 falcon->SetControl(control);
529 }
530
Alex Perryc4691f52020-02-17 19:20:01 -0800531 void Stop() override {
532 AOS_LOG(WARNING, "Superstructure output too old.\n");
533 hood_victor_->SetDisabled();
534 intake_joint_victor_->SetDisabled();
535 turret_victor_->SetDisabled();
milind-u9aa62a82021-10-03 20:43:19 -0700536 if (washing_machine_control_panel_victor_) {
537 washing_machine_control_panel_victor_->SetDisabled();
538 }
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800539 accelerator_left_falcon_->SetDisabled();
540 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800541 finisher_falcon0_->SetDisabled();
542 finisher_falcon1_->SetDisabled();
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800543 Disable(feeder_falcon_.get());
544 Disable(intake_roller_falcon_.get());
545 Disable(climber_falcon_.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800546 }
547
548 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
milind-u9aa62a82021-10-03 20:43:19 -0700549 turret_victor_, washing_machine_control_panel_victor_;
Alex Perryc4691f52020-02-17 19:20:01 -0800550
Ravago Jones3dda5602021-03-10 00:33:13 -0800551 ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800552 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800553
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800554 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> intake_roller_falcon_,
555 climber_falcon_, feeder_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800556};
557
558class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
559 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800560 ::std::unique_ptr<frc::Encoder> make_encoder(
561 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800562 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800563 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800564 }
565
566 void Run() override {
567 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800568 aos::configuration::ReadConfig("aos_config.json");
Stephan Massaltd021f972020-01-05 20:41:23 -0800569
570 // Thread 1.
571 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
572 ::frc971::wpilib::JoystickSender joystick_sender(
573 &joystick_sender_event_loop);
574 AddLoop(&joystick_sender_event_loop);
575
576 // Thread 2.
577 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
578 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
579 AddLoop(&pdp_fetcher_event_loop);
580
581 // Thread 3.
582 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
583 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800584 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800585 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
586 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800587 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800588 sensor_reader.set_hood_encoder(
589 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800590
Ravago Jones937587c2020-12-26 17:21:09 -0800591 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
592 sensor_reader.set_hood_single_turn_absolute_pwm(
593 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800594
Sabina Davisf7afd112020-02-23 13:42:14 -0800595 sensor_reader.set_intake_encoder(make_encoder(5));
596 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800597
Sabina Davisf7afd112020-02-23 13:42:14 -0800598 sensor_reader.set_turret_encoder(make_encoder(2));
599 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
600 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800601
Sabina Davisf7afd112020-02-23 13:42:14 -0800602 sensor_reader.set_finisher_encoder(
603 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
604 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
605 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
606
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700607 sensor_reader.set_ball_intake_beambreak(make_unique<frc::DigitalInput>(4));
608
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700609 if (absl::GetFlag(FLAGS_shooter_tuning)) {
milind-u4b31c4d2021-09-18 16:08:23 -0700610 sensor_reader.set_ball_beambreak_inputs(
611 make_unique<frc::DigitalInput>(6), make_unique<frc::DigitalInput>(7));
612 }
613
James Kuszmaul0a981402021-10-09 21:00:34 -0700614 AddLoop(&sensor_reader_event_loop);
615
James Kuszmaul022d40e2020-02-11 17:06:18 -0800616 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
617 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
618 // CS0.
James Kuszmaul0a981402021-10-09 21:00:34 -0700619 // TODO(james): Double check whether the above is still accurate/useful with
620 // the ADIS16448. No reason it shouldn't be.
621 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS1;
Austin Schuh83873c32020-02-22 14:58:39 -0800622 std::unique_ptr<frc::DigitalInput> imu_trigger;
623 std::unique_ptr<frc::DigitalOutput> imu_reset;
624 if (::aos::network::GetTeamNumber() ==
625 constants::Values::kCodingRobotTeamNumber) {
626 imu_trigger = make_unique<frc::DigitalInput>(26);
627 imu_reset = make_unique<frc::DigitalOutput>(27);
628 spi_port = frc::SPI::Port::kOnboardCS0;
629 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800630 imu_trigger = make_unique<frc::DigitalInput>(9);
631 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800632 }
James Kuszmaul0a981402021-10-09 21:00:34 -0700633 ::aos::ShmEventLoop imu_event_loop(&config.message());
634 std::unique_ptr<frc971::wpilib::ADIS16448> old_imu;
635 std::unique_ptr<frc971::wpilib::ADIS16470> new_imu;
636 std::unique_ptr<frc::SPI> imu_spi;
milind1f1dca32021-07-03 13:50:07 -0700637 if (::aos::network::GetTeamNumber() !=
638 constants::Values::kCodingRobotTeamNumber) {
James Kuszmaul0a981402021-10-09 21:00:34 -0700639 old_imu = make_unique<frc971::wpilib::ADIS16448>(
640 &imu_event_loop, spi_port, imu_trigger.get());
641 old_imu->SetDummySPI(frc::SPI::Port::kOnboardCS2);
642 old_imu->set_reset(imu_reset.get());
643 } else {
644 imu_spi = make_unique<frc::SPI>(spi_port);
645 new_imu = make_unique<frc971::wpilib::ADIS16470>(
646 &imu_event_loop, imu_spi.get(), imu_trigger.get(), imu_reset.get());
647 sensor_reader.set_imu(new_imu.get());
648 }
649 AddLoop(&imu_event_loop);
Stephan Massaltd021f972020-01-05 20:41:23 -0800650
651 // Thread 4.
652 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800653 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800654 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
655 drivetrain_writer.set_left_controller0(
656 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
657 drivetrain_writer.set_right_controller0(
658 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
659
660 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800661 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800662 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800663 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800664 superstructure_writer.set_intake_roller_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800665 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800666 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Ravago Jones3dda5602021-03-10 00:33:13 -0800667 superstructure_writer.set_feeder_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800668 make_unique<ctre::phoenix6::hardware::TalonFX>(1));
milind-u9aa62a82021-10-03 20:43:19 -0700669 superstructure_writer.set_washing_machine_control_panel_victor(
670 make_unique<frc::VictorSP>(6));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800671 superstructure_writer.set_accelerator_left_falcon(
672 make_unique<::frc::TalonFX>(5));
673 superstructure_writer.set_accelerator_right_falcon(
674 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800675 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
676 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800677 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800678 superstructure_writer.set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800679 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Stephan Massaltd021f972020-01-05 20:41:23 -0800680
681 AddLoop(&output_event_loop);
682
683 RunLoops();
684 }
685};
686
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800687} // namespace y2020::wpilib
Stephan Massaltd021f972020-01-05 20:41:23 -0800688
689AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);