blob: 2f7b1840c9004c80c706de29dec3eceaf640c104 [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
Maxwell Henderson1c0843c2023-12-22 16:20:59 -080023#include "ctre/phoenix6/TalonFX.hpp"
Philipp Schrader790cb542023-07-05 21:06:52 -070024#include "gflags/gflags.h"
25
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"
56#include "y2020/control_loops/superstructure/superstructure_position_generated.h"
57
milind-u4b31c4d2021-09-18 16:08:23 -070058DEFINE_bool(shooter_tuning, true,
59 "If true, reads from ball beambreak sensors and sends shooter "
60 "tuning readings");
61
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
68namespace y2020 {
69namespace wpilib {
70namespace {
71
72constexpr double kMaxBringupPower = 12.0;
73
74// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
75// DMA stuff and then removing the * 2.0 in *_translate.
76// The low bit is direction.
77
Stephan Massaltd021f972020-01-05 20:41:23 -080078double drivetrain_translate(int32_t in) {
79 return ((static_cast<double>(in) /
80 Values::kDrivetrainEncoderCountsPerRevolution()) *
81 (2.0 * M_PI)) *
82 Values::kDrivetrainEncoderRatio() *
83 control_loops::drivetrain::kWheelRadius;
84}
85
86double drivetrain_velocity_translate(double in) {
87 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
88 (2.0 * M_PI)) *
89 Values::kDrivetrainEncoderRatio() *
90 control_loops::drivetrain::kWheelRadius;
91}
92
Alex Perryc4691f52020-02-17 19:20:01 -080093double turret_pot_translate(double voltage) {
94 return voltage * Values::kTurretPotRatio() *
95 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
96}
97
Stephan Massaltd021f972020-01-05 20:41:23 -080098constexpr double kMaxFastEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -080099 std::max({Values::kMaxControlPanelEncoderPulsesPerSecond(),
100 Values::kMaxFinisherEncoderPulsesPerSecond(),
101 Values::kMaxAcceleratorEncoderPulsesPerSecond()});
102static_assert(kMaxFastEncoderPulsesPerSecond <= 1000000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -0800103 "fast encoders are too fast");
Alex Perryc4691f52020-02-17 19:20:01 -0800104constexpr double kMaxMediumEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -0800105 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
106 Values::kMaxHoodEncoderPulsesPerSecond(),
107 Values::kMaxIntakeEncoderPulsesPerSecond(),
108 Values::kMaxTurretEncoderPulsesPerSecond()});
Stephan Massaltd021f972020-01-05 20:41:23 -0800109
Austin Schuh9dcd5202020-02-20 20:06:04 -0800110static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -0800111 "medium encoders are too fast");
112
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800113void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) {
114 ctre::phoenix6::configs::TalonFXConfiguration configuration;
115 ctre::phoenix::StatusCode status =
116 talon->GetConfigurator().Refresh(configuration);
117 if (!status.IsOK()) {
118 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
119 status.GetName(), status.GetDescription());
120 }
121 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
122}
123
124void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon,
125 double stator_current_limit, double supply_current_limit) {
126 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
127 current_limits.StatorCurrentLimit = stator_current_limit;
128 current_limits.StatorCurrentLimitEnable = true;
129 current_limits.SupplyCurrentLimit = supply_current_limit;
130 current_limits.SupplyCurrentLimitEnable = true;
131
132 ctre::phoenix6::configs::TalonFXConfiguration configuration;
133 configuration.CurrentLimits = current_limits;
134
135 ctre::phoenix::StatusCode status =
136 talon->GetConfigurator().Apply(configuration);
137 if (!status.IsOK()) {
138 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
139 status.GetName(), status.GetDescription());
140 }
141
142 PrintConfigs(talon);
143}
144
145void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
146 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
147 stop_command.UpdateFreqHz = 0_Hz;
148 stop_command.EnableFOC = true;
149
150 talon->SetControl(stop_command);
151}
152
Stephan Massaltd021f972020-01-05 20:41:23 -0800153} // namespace
154
155// Class to send position messages with sensor readings to our loops.
156class SensorReader : public ::frc971::wpilib::SensorReader {
157 public:
158 SensorReader(::aos::ShmEventLoop *event_loop)
159 : ::frc971::wpilib::SensorReader(event_loop),
160 auto_mode_sender_(
161 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
162 "/autonomous")),
163 superstructure_position_sender_(
164 event_loop->MakeSender<superstructure::Position>(
165 "/superstructure")),
166 drivetrain_position_sender_(
167 event_loop
168 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
milind-u4b31c4d2021-09-18 16:08:23 -0700169 "/drivetrain")),
170 shooter_tuning_readings_sender_(
171 event_loop->MakeSender<superstructure::shooter::TuningReadings>(
172 "/superstructure")) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800173 // Set to filter out anything shorter than 1/4 of the minimum pulse width
174 // we should ever see.
175 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
176 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
milind-u62d4a8e2021-10-11 16:08:41 -0700177
178 constants::InitValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800179 }
180
Alex Perryc4691f52020-02-17 19:20:01 -0800181 // Hood
Alex Perryc4691f52020-02-17 19:20:01 -0800182 void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) {
183 medium_encoder_filter_.Add(encoder.get());
184 hood_encoder_.set_encoder(::std::move(encoder));
185 }
186
187 void set_hood_absolute_pwm(
188 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
189 hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
190 }
191
Ravago Jones937587c2020-12-26 17:21:09 -0800192 void set_hood_single_turn_absolute_pwm(
193 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
194 hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm));
195 }
196
Alex Perryc4691f52020-02-17 19:20:01 -0800197 // Intake
198
199 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
200 medium_encoder_filter_.Add(encoder.get());
201 intake_joint_encoder_.set_encoder(::std::move(encoder));
202 }
203
204 void set_intake_absolute_pwm(
205 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
206 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
207 }
208
209 // Turret
210
211 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
212 medium_encoder_filter_.Add(encoder.get());
213 turret_encoder_.set_encoder(::std::move(encoder));
214 }
215
216 void set_turret_absolute_pwm(
217 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
218 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
219 }
220
221 void set_turret_potentiometer(
222 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
223 turret_encoder_.set_potentiometer(::std::move(potentiometer));
224 }
225
226 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800227 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800228 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800229 finisher_encoder_ = ::std::move(encoder);
230 }
231 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
232 fast_encoder_filter_.Add(encoder.get());
233 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800234 }
235
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800236 void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800237 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800238 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800239 }
240
Stephan Massaltd021f972020-01-05 20:41:23 -0800241 // Auto mode switches.
242 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800243 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800244 autonomous_modes_.at(i) = ::std::move(sensor);
245 }
246
James Kuszmaula244a912020-01-18 13:50:50 -0800247 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
248
milind-u4b31c4d2021-09-18 16:08:23 -0700249 void set_ball_beambreak_inputs(::std::unique_ptr<frc::DigitalInput> sensor1,
250 ::std::unique_ptr<frc::DigitalInput> sensor2) {
251 ball_beambreak_inputs_[0] = ::std::move(sensor1);
252 ball_beambreak_inputs_[1] = ::std::move(sensor2);
Austin Schuh6c053ef2021-09-26 14:32:16 -0700253 ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get());
254 ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get());
milind-u4b31c4d2021-09-18 16:08:23 -0700255 }
256
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700257 void set_ball_intake_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
258 ball_intake_beambreak_ = ::std::move(sensor);
259 }
260
milind-u4b31c4d2021-09-18 16:08:23 -0700261 void Start() override {
262 if (FLAGS_shooter_tuning) {
263 AddToDMA(&ball_beambreak_reader_);
264 }
265 }
266
Stephan Massaltd021f972020-01-05 20:41:23 -0800267 void RunIteration() override {
James Kuszmaul0a981402021-10-09 21:00:34 -0700268 if (imu_ != nullptr) {
269 imu_->DoReads();
270 }
James Kuszmaula244a912020-01-18 13:50:50 -0800271
Stephan Massaltd021f972020-01-05 20:41:23 -0800272 {
273 auto builder = drivetrain_position_sender_.MakeBuilder();
274 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
275 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
276 drivetrain_builder.add_left_encoder(
277 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
278 drivetrain_builder.add_left_speed(
279 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
280
281 drivetrain_builder.add_right_encoder(
282 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
283 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
284 drivetrain_right_encoder_->GetPeriod()));
285
milind1f1dca32021-07-03 13:50:07 -0700286 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Stephan Massaltd021f972020-01-05 20:41:23 -0800287 }
Austin Schuh37d2c302021-10-12 22:07:43 -0700288 const constants::Values &values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800289
290 {
291 auto builder = superstructure_position_sender_.MakeBuilder();
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
Ravago Jones937587c2020-12-26 17:21:09 -0800295 frc971::AbsoluteAndAbsolutePositionT hood;
Alex Perryc4691f52020-02-17 19:20:01 -0800296 CopyPosition(hood_encoder_, &hood,
297 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800298 Values::kHoodEncoderRatio(),
299 Values::kHoodSingleTurnEncoderRatio(), false);
300 flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset =
301 frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood);
Alex Perryc4691f52020-02-17 19:20:01 -0800302
303 // Intake
304 frc971::AbsolutePositionT intake_joint;
305 CopyPosition(intake_joint_encoder_, &intake_joint,
306 Values::kIntakeEncoderCountsPerRevolution(),
307 Values::kIntakeEncoderRatio(), false);
308 flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset =
309 frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint);
310
311 // Turret
312 frc971::PotAndAbsolutePositionT turret;
313 CopyPosition(turret_encoder_, &turret,
314 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800315 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800316 values.turret.potentiometer_offset);
317 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
318 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
319
320 // Shooter
321 y2020::control_loops::superstructure::ShooterPositionT shooter;
322 shooter.theta_finisher =
Austin Schuh0ad31d72021-03-06 17:07:04 -0800323 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800324 Values::kFinisherEncoderCountsPerRevolution(),
325 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800326
Alex Perryc4691f52020-02-17 19:20:01 -0800327 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800328 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800329 Values::kAcceleratorEncoderCountsPerRevolution(),
330 Values::kAcceleratorEncoderRatio());
331 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800332 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800333 Values::kAcceleratorEncoderCountsPerRevolution(),
334 Values::kAcceleratorEncoderRatio());
335 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
336 shooter_offset =
337 y2020::control_loops::superstructure::ShooterPosition::Pack(
338 *builder.fbb(), &shooter);
339
Austin Schuh18ca45f2020-02-29 22:58:49 -0800340 superstructure::Position::Builder position_builder =
341 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800342 position_builder.add_hood(hood_offset);
343 position_builder.add_intake_joint(intake_joint_offset);
344 position_builder.add_turret(turret_offset);
345 position_builder.add_shooter(shooter_offset);
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700346 position_builder.add_intake_beambreak_triggered(
Austin Schuh02e14962021-10-13 20:32:24 -0700347 ball_intake_beambreak_->Get());
Alex Perryc4691f52020-02-17 19:20:01 -0800348
milind1f1dca32021-07-03 13:50:07 -0700349 builder.CheckOk(builder.Send(position_builder.Finish()));
Stephan Massaltd021f972020-01-05 20:41:23 -0800350 }
351
352 {
353 auto builder = auto_mode_sender_.MakeBuilder();
354
355 uint32_t mode = 0;
356 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
357 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
358 mode |= 1 << i;
359 }
360 }
361
362 auto auto_mode_builder =
363 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
364
365 auto_mode_builder.add_mode(mode);
366
milind1f1dca32021-07-03 13:50:07 -0700367 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
Stephan Massaltd021f972020-01-05 20:41:23 -0800368 }
milind-u4b31c4d2021-09-18 16:08:23 -0700369
Austin Schuh6c053ef2021-09-26 14:32:16 -0700370 if (FLAGS_shooter_tuning) {
milind-u4b31c4d2021-09-18 16:08:23 -0700371 // Distance between beambreak sensors, in meters.
372 constexpr double kDistanceBetweenBeambreaks = 0.4813;
373
Austin Schuh6c053ef2021-09-26 14:32:16 -0700374 if (ball_beambreak_reader_.pulses_detected() > balls_detected_) {
375 balls_detected_ = ball_beambreak_reader_.pulses_detected();
376
377 auto builder = shooter_tuning_readings_sender_.MakeBuilder();
378 auto shooter_tuning_readings_builder =
379 builder.MakeBuilder<superstructure::shooter::TuningReadings>();
380 shooter_tuning_readings_builder.add_velocity_ball(
381 kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width());
milind1f1dca32021-07-03 13:50:07 -0700382 builder.CheckOk(builder.Send(shooter_tuning_readings_builder.Finish()));
Austin Schuh6c053ef2021-09-26 14:32:16 -0700383 }
milind-u4b31c4d2021-09-18 16:08:23 -0700384 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800385 }
386
387 private:
388 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
389 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
390 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
391 drivetrain_position_sender_;
milind-u4b31c4d2021-09-18 16:08:23 -0700392 ::aos::Sender<superstructure::shooter::TuningReadings>
393 shooter_tuning_readings_sender_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800394
Alex Perryc4691f52020-02-17 19:20:01 -0800395 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
396
Ravago Jones937587c2020-12-26 17:21:09 -0800397 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
398
399 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800400
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800401 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
milind-u3d68aa72021-09-26 12:19:03 -0700402 left_accelerator_encoder_, right_accelerator_encoder_;
milind-u9aa62a82021-10-03 20:43:19 -0700403
Stephan Massaltd021f972020-01-05 20:41:23 -0800404 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800405
406 frc971::wpilib::ADIS16470 *imu_ = nullptr;
milind-u4b31c4d2021-09-18 16:08:23 -0700407
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700408 std::unique_ptr<frc::DigitalInput> ball_intake_beambreak_;
409
milind-u4b31c4d2021-09-18 16:08:23 -0700410 // Used to interface with the two beam break sensors that the ball for tuning
411 // shooter parameters has to pass through.
412 // We will time how long it takes to pass between the two sensors to get its
413 // velocity.
414 std::array<std::unique_ptr<frc::DigitalInput>, 2> ball_beambreak_inputs_;
415 frc971::wpilib::DMAPulseSeparationReader ball_beambreak_reader_;
416 int balls_detected_ = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800417};
418
419class SuperstructureWriter
420 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
421 public:
422 SuperstructureWriter(::aos::EventLoop *event_loop)
423 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
424 event_loop, "/superstructure") {}
425
Alex Perryc4691f52020-02-17 19:20:01 -0800426 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
427 hood_victor_ = ::std::move(t);
428 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800429
Alex Perryc4691f52020-02-17 19:20:01 -0800430 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
431 intake_joint_victor_ = ::std::move(t);
432 }
433
434 void set_intake_roller_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800435 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800436 intake_roller_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800437
438 WriteConfigs(intake_roller_falcon_.get(),
439 Values::kIntakeRollerStatorCurrentLimit(),
440 Values::kIntakeRollerSupplyCurrentLimit());
Alex Perryc4691f52020-02-17 19:20:01 -0800441 }
442
443 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
444 turret_victor_ = ::std::move(t);
445 }
446
Ravago Jones3dda5602021-03-10 00:33:13 -0800447 void set_feeder_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800448 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800449 feeder_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800450
451 WriteConfigs(feeder_falcon_.get(), Values::kFeederStatorCurrentLimit(),
452 Values::kFeederSupplyCurrentLimit());
Alex Perryc4691f52020-02-17 19:20:01 -0800453 }
454
milind-u9aa62a82021-10-03 20:43:19 -0700455 void set_washing_machine_control_panel_victor(
456 ::std::unique_ptr<::frc::VictorSP> t) {
457 washing_machine_control_panel_victor_ = ::std::move(t);
458 }
459
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800460 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
461 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800462 }
463
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800464 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
465 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800466 }
467
Austin Schuh0ad31d72021-03-06 17:07:04 -0800468 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
469 finisher_falcon0_ = ::std::move(t);
470 }
471
472 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
473 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800474 }
475
476 void set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800477 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800478 climber_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800479 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
480 current_limits.SupplyCurrentLimit = Values::kClimberSupplyCurrentLimit();
481 current_limits.SupplyCurrentLimitEnable = true;
482
483 ctre::phoenix6::configs::TalonFXConfiguration configuration;
484 configuration.CurrentLimits = current_limits;
485
486 ctre::phoenix::StatusCode status =
487 climber_falcon_->GetConfigurator().Apply(configuration);
488 if (!status.IsOK()) {
489 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
490 status.GetName(), status.GetDescription());
491 }
492
493 PrintConfigs(climber_falcon_.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800494 }
495
496 private:
497 void Write(const superstructure::Output &output) override {
Austin Schuh2efe1682021-03-06 22:47:15 -0800498 hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower,
499 kMaxBringupPower) /
500 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800501
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800502 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
503 -kMaxBringupPower,
504 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800505 12.0);
506
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800507 WriteCan(-output.intake_roller_voltage(), intake_roller_falcon_.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800508
milind-uf70e8e12021-10-02 12:36:00 -0700509 turret_victor_->SetSpeed(std::clamp(output.turret_voltage(),
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800510 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800511 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800512 WriteCan(output.feeder_voltage(), feeder_falcon_.get());
milind-u9aa62a82021-10-03 20:43:19 -0700513 if (washing_machine_control_panel_victor_) {
514 washing_machine_control_panel_victor_->SetSpeed(
515 std::clamp(-output.washing_machine_spinner_voltage(),
516 -kMaxBringupPower, kMaxBringupPower) /
517 12.0);
518 }
Alex Perryc4691f52020-02-17 19:20:01 -0800519
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800520 accelerator_left_falcon_->SetSpeed(
521 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
522 kMaxBringupPower) /
523 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800524
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800525 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800526 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800527 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800528 12.0);
529
Austin Schuh0ad31d72021-03-06 17:07:04 -0800530 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
531 -kMaxBringupPower,
532 kMaxBringupPower) /
533 12.0);
534 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
535 -kMaxBringupPower,
536 kMaxBringupPower) /
537 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800538
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800539 if (climber_falcon_) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800540 WriteCan(output.climber_voltage(), climber_falcon_.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800541 }
Alex Perryc4691f52020-02-17 19:20:01 -0800542 }
543
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800544 static void WriteCan(const double voltage,
545 ::ctre::phoenix6::hardware::TalonFX *falcon) {
546 ctre::phoenix6::controls::DutyCycleOut control(
547 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
548 control.UpdateFreqHz = 0_Hz;
549 control.EnableFOC = true;
550
551 falcon->SetControl(control);
552 }
553
Alex Perryc4691f52020-02-17 19:20:01 -0800554 void Stop() override {
555 AOS_LOG(WARNING, "Superstructure output too old.\n");
556 hood_victor_->SetDisabled();
557 intake_joint_victor_->SetDisabled();
558 turret_victor_->SetDisabled();
milind-u9aa62a82021-10-03 20:43:19 -0700559 if (washing_machine_control_panel_victor_) {
560 washing_machine_control_panel_victor_->SetDisabled();
561 }
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800562 accelerator_left_falcon_->SetDisabled();
563 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800564 finisher_falcon0_->SetDisabled();
565 finisher_falcon1_->SetDisabled();
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800566 Disable(feeder_falcon_.get());
567 Disable(intake_roller_falcon_.get());
568 Disable(climber_falcon_.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800569 }
570
571 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
milind-u9aa62a82021-10-03 20:43:19 -0700572 turret_victor_, washing_machine_control_panel_victor_;
Alex Perryc4691f52020-02-17 19:20:01 -0800573
Ravago Jones3dda5602021-03-10 00:33:13 -0800574 ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800575 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800576
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800577 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> intake_roller_falcon_,
578 climber_falcon_, feeder_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800579};
580
581class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
582 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800583 ::std::unique_ptr<frc::Encoder> make_encoder(
584 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800585 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800586 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800587 }
588
589 void Run() override {
590 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800591 aos::configuration::ReadConfig("aos_config.json");
Stephan Massaltd021f972020-01-05 20:41:23 -0800592
593 // Thread 1.
594 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
595 ::frc971::wpilib::JoystickSender joystick_sender(
596 &joystick_sender_event_loop);
597 AddLoop(&joystick_sender_event_loop);
598
599 // Thread 2.
600 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
601 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
602 AddLoop(&pdp_fetcher_event_loop);
603
604 // Thread 3.
605 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
606 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800607 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800608 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
609 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800610 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800611 sensor_reader.set_hood_encoder(
612 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800613
Ravago Jones937587c2020-12-26 17:21:09 -0800614 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
615 sensor_reader.set_hood_single_turn_absolute_pwm(
616 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800617
Sabina Davisf7afd112020-02-23 13:42:14 -0800618 sensor_reader.set_intake_encoder(make_encoder(5));
619 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800620
Sabina Davisf7afd112020-02-23 13:42:14 -0800621 sensor_reader.set_turret_encoder(make_encoder(2));
622 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
623 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800624
Sabina Davisf7afd112020-02-23 13:42:14 -0800625 sensor_reader.set_finisher_encoder(
626 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
627 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
628 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
629
Ravago Jonesf8b7bfe2021-10-09 16:25:29 -0700630 sensor_reader.set_ball_intake_beambreak(make_unique<frc::DigitalInput>(4));
631
milind-u4b31c4d2021-09-18 16:08:23 -0700632 if (FLAGS_shooter_tuning) {
633 sensor_reader.set_ball_beambreak_inputs(
634 make_unique<frc::DigitalInput>(6), make_unique<frc::DigitalInput>(7));
635 }
636
James Kuszmaul0a981402021-10-09 21:00:34 -0700637 AddLoop(&sensor_reader_event_loop);
638
James Kuszmaul022d40e2020-02-11 17:06:18 -0800639 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
640 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
641 // CS0.
James Kuszmaul0a981402021-10-09 21:00:34 -0700642 // TODO(james): Double check whether the above is still accurate/useful with
643 // the ADIS16448. No reason it shouldn't be.
644 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS1;
Austin Schuh83873c32020-02-22 14:58:39 -0800645 std::unique_ptr<frc::DigitalInput> imu_trigger;
646 std::unique_ptr<frc::DigitalOutput> imu_reset;
647 if (::aos::network::GetTeamNumber() ==
648 constants::Values::kCodingRobotTeamNumber) {
649 imu_trigger = make_unique<frc::DigitalInput>(26);
650 imu_reset = make_unique<frc::DigitalOutput>(27);
651 spi_port = frc::SPI::Port::kOnboardCS0;
652 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800653 imu_trigger = make_unique<frc::DigitalInput>(9);
654 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800655 }
James Kuszmaul0a981402021-10-09 21:00:34 -0700656 ::aos::ShmEventLoop imu_event_loop(&config.message());
657 std::unique_ptr<frc971::wpilib::ADIS16448> old_imu;
658 std::unique_ptr<frc971::wpilib::ADIS16470> new_imu;
659 std::unique_ptr<frc::SPI> imu_spi;
milind1f1dca32021-07-03 13:50:07 -0700660 if (::aos::network::GetTeamNumber() !=
661 constants::Values::kCodingRobotTeamNumber) {
James Kuszmaul0a981402021-10-09 21:00:34 -0700662 old_imu = make_unique<frc971::wpilib::ADIS16448>(
663 &imu_event_loop, spi_port, imu_trigger.get());
664 old_imu->SetDummySPI(frc::SPI::Port::kOnboardCS2);
665 old_imu->set_reset(imu_reset.get());
666 } else {
667 imu_spi = make_unique<frc::SPI>(spi_port);
668 new_imu = make_unique<frc971::wpilib::ADIS16470>(
669 &imu_event_loop, imu_spi.get(), imu_trigger.get(), imu_reset.get());
670 sensor_reader.set_imu(new_imu.get());
671 }
672 AddLoop(&imu_event_loop);
Stephan Massaltd021f972020-01-05 20:41:23 -0800673
674 // Thread 4.
675 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800676 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800677 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
678 drivetrain_writer.set_left_controller0(
679 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
680 drivetrain_writer.set_right_controller0(
681 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
682
683 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800684 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800685 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800686 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800687 superstructure_writer.set_intake_roller_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800688 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800689 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Ravago Jones3dda5602021-03-10 00:33:13 -0800690 superstructure_writer.set_feeder_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800691 make_unique<ctre::phoenix6::hardware::TalonFX>(1));
milind-u9aa62a82021-10-03 20:43:19 -0700692 superstructure_writer.set_washing_machine_control_panel_victor(
693 make_unique<frc::VictorSP>(6));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800694 superstructure_writer.set_accelerator_left_falcon(
695 make_unique<::frc::TalonFX>(5));
696 superstructure_writer.set_accelerator_right_falcon(
697 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800698 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
699 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800700 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800701 superstructure_writer.set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800702 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Stephan Massaltd021f972020-01-05 20:41:23 -0800703
704 AddLoop(&output_event_loop);
705
706 RunLoops();
707 }
708};
709
710} // namespace wpilib
711} // namespace y2020
712
713AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);