blob: 4667f253615773d479ce6c352edcb17fea6c75e7 [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
23#include "aos/commonmath.h"
24#include "aos/events/event_loop.h"
25#include "aos/events/shm_event_loop.h"
26#include "aos/init.h"
27#include "aos/logging/logging.h"
Austin Schuh83873c32020-02-22 14:58:39 -080028#include "aos/network/team_number.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080029#include "aos/realtime.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080030#include "aos/time/time.h"
31#include "aos/util/log_interval.h"
32#include "aos/util/phased_loop.h"
33#include "aos/util/wrapping_counter.h"
Alex Perryc4691f52020-02-17 19:20:01 -080034#include "ctre/phoenix/motorcontrol/can/TalonFX.h"
Ravago Jones3dda5602021-03-10 00:33:13 -080035#include "ctre/phoenix/motorcontrol/can/VictorSPX.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080036#include "frc971/autonomous/auto_mode_generated.h"
37#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070038#include "frc971/input/robot_state_generated.h"
James Kuszmaul0a981402021-10-09 21:00:34 -070039#include "frc971/wpilib/ADIS16448.h"
James Kuszmaula244a912020-01-18 13:50:50 -080040#include "frc971/wpilib/ADIS16470.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080041#include "frc971/wpilib/buffered_pcm.h"
42#include "frc971/wpilib/buffered_solenoid.h"
43#include "frc971/wpilib/dma.h"
44#include "frc971/wpilib/drivetrain_writer.h"
45#include "frc971/wpilib/encoder_and_potentiometer.h"
46#include "frc971/wpilib/joystick_sender.h"
47#include "frc971/wpilib/logging_generated.h"
48#include "frc971/wpilib/loop_output_handler.h"
49#include "frc971/wpilib/pdp_fetcher.h"
50#include "frc971/wpilib/sensor_reader.h"
51#include "frc971/wpilib/wpilib_robot_base.h"
milind-u4b31c4d2021-09-18 16:08:23 -070052#include "gflags/gflags.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080053#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
113} // namespace
114
115// Class to send position messages with sensor readings to our loops.
116class SensorReader : public ::frc971::wpilib::SensorReader {
117 public:
118 SensorReader(::aos::ShmEventLoop *event_loop)
119 : ::frc971::wpilib::SensorReader(event_loop),
120 auto_mode_sender_(
121 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
122 "/autonomous")),
123 superstructure_position_sender_(
124 event_loop->MakeSender<superstructure::Position>(
125 "/superstructure")),
126 drivetrain_position_sender_(
127 event_loop
128 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
milind-u4b31c4d2021-09-18 16:08:23 -0700129 "/drivetrain")),
130 shooter_tuning_readings_sender_(
131 event_loop->MakeSender<superstructure::shooter::TuningReadings>(
132 "/superstructure")) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800133 // Set to filter out anything shorter than 1/4 of the minimum pulse width
134 // we should ever see.
135 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
136 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
milind-u62d4a8e2021-10-11 16:08:41 -0700137
138 constants::InitValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800139 }
140
Alex Perryc4691f52020-02-17 19:20:01 -0800141 // Hood
Alex Perryc4691f52020-02-17 19:20:01 -0800142 void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) {
143 medium_encoder_filter_.Add(encoder.get());
144 hood_encoder_.set_encoder(::std::move(encoder));
145 }
146
147 void set_hood_absolute_pwm(
148 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
149 hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
150 }
151
Ravago Jones937587c2020-12-26 17:21:09 -0800152 void set_hood_single_turn_absolute_pwm(
153 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
154 hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm));
155 }
156
Alex Perryc4691f52020-02-17 19:20:01 -0800157 // Intake
158
159 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
160 medium_encoder_filter_.Add(encoder.get());
161 intake_joint_encoder_.set_encoder(::std::move(encoder));
162 }
163
164 void set_intake_absolute_pwm(
165 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
166 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
167 }
168
169 // Turret
170
171 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
172 medium_encoder_filter_.Add(encoder.get());
173 turret_encoder_.set_encoder(::std::move(encoder));
174 }
175
176 void set_turret_absolute_pwm(
177 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
178 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
179 }
180
181 void set_turret_potentiometer(
182 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
183 turret_encoder_.set_potentiometer(::std::move(potentiometer));
184 }
185
186 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800187 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800188 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800189 finisher_encoder_ = ::std::move(encoder);
190 }
191 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
192 fast_encoder_filter_.Add(encoder.get());
193 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800194 }
195
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800196 void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800197 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800198 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800199 }
200
Stephan Massaltd021f972020-01-05 20:41:23 -0800201 // Auto mode switches.
202 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800203 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800204 autonomous_modes_.at(i) = ::std::move(sensor);
205 }
206
James Kuszmaula244a912020-01-18 13:50:50 -0800207 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
208
milind-u4b31c4d2021-09-18 16:08:23 -0700209 void set_ball_beambreak_inputs(::std::unique_ptr<frc::DigitalInput> sensor1,
210 ::std::unique_ptr<frc::DigitalInput> sensor2) {
211 ball_beambreak_inputs_[0] = ::std::move(sensor1);
212 ball_beambreak_inputs_[1] = ::std::move(sensor2);
Austin Schuh6c053ef2021-09-26 14:32:16 -0700213 ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get());
214 ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get());
milind-u4b31c4d2021-09-18 16:08:23 -0700215 }
216
217 void Start() override {
218 if (FLAGS_shooter_tuning) {
219 AddToDMA(&ball_beambreak_reader_);
220 }
221 }
222
Stephan Massaltd021f972020-01-05 20:41:23 -0800223 void RunIteration() override {
James Kuszmaul0a981402021-10-09 21:00:34 -0700224 if (imu_ != nullptr) {
225 imu_->DoReads();
226 }
James Kuszmaula244a912020-01-18 13:50:50 -0800227
Stephan Massaltd021f972020-01-05 20:41:23 -0800228 {
229 auto builder = drivetrain_position_sender_.MakeBuilder();
230 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
231 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
232 drivetrain_builder.add_left_encoder(
233 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
234 drivetrain_builder.add_left_speed(
235 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
236
237 drivetrain_builder.add_right_encoder(
238 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
239 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
240 drivetrain_right_encoder_->GetPeriod()));
241
242 builder.Send(drivetrain_builder.Finish());
243 }
Austin Schuh37d2c302021-10-12 22:07:43 -0700244 const constants::Values &values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800245
246 {
247 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800248
Alex Perryc4691f52020-02-17 19:20:01 -0800249 // TODO(alex): check new absolute encoder api.
250 // Hood
Ravago Jones937587c2020-12-26 17:21:09 -0800251 frc971::AbsoluteAndAbsolutePositionT hood;
Alex Perryc4691f52020-02-17 19:20:01 -0800252 CopyPosition(hood_encoder_, &hood,
253 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800254 Values::kHoodEncoderRatio(),
255 Values::kHoodSingleTurnEncoderRatio(), false);
256 flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset =
257 frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood);
Alex Perryc4691f52020-02-17 19:20:01 -0800258
259 // Intake
260 frc971::AbsolutePositionT intake_joint;
261 CopyPosition(intake_joint_encoder_, &intake_joint,
262 Values::kIntakeEncoderCountsPerRevolution(),
263 Values::kIntakeEncoderRatio(), false);
264 flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset =
265 frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint);
266
267 // Turret
268 frc971::PotAndAbsolutePositionT turret;
269 CopyPosition(turret_encoder_, &turret,
270 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800271 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800272 values.turret.potentiometer_offset);
273 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
274 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
275
276 // Shooter
277 y2020::control_loops::superstructure::ShooterPositionT shooter;
278 shooter.theta_finisher =
Austin Schuh0ad31d72021-03-06 17:07:04 -0800279 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800280 Values::kFinisherEncoderCountsPerRevolution(),
281 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800282
Alex Perryc4691f52020-02-17 19:20:01 -0800283 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800284 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800285 Values::kAcceleratorEncoderCountsPerRevolution(),
286 Values::kAcceleratorEncoderRatio());
287 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800288 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800289 Values::kAcceleratorEncoderCountsPerRevolution(),
290 Values::kAcceleratorEncoderRatio());
291 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
292 shooter_offset =
293 y2020::control_loops::superstructure::ShooterPosition::Pack(
294 *builder.fbb(), &shooter);
295
Austin Schuh18ca45f2020-02-29 22:58:49 -0800296 superstructure::Position::Builder position_builder =
297 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800298 position_builder.add_hood(hood_offset);
299 position_builder.add_intake_joint(intake_joint_offset);
300 position_builder.add_turret(turret_offset);
301 position_builder.add_shooter(shooter_offset);
Alex Perryc4691f52020-02-17 19:20:01 -0800302
Stephan Massaltd021f972020-01-05 20:41:23 -0800303 builder.Send(position_builder.Finish());
304 }
305
306 {
307 auto builder = auto_mode_sender_.MakeBuilder();
308
309 uint32_t mode = 0;
310 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
311 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
312 mode |= 1 << i;
313 }
314 }
315
316 auto auto_mode_builder =
317 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
318
319 auto_mode_builder.add_mode(mode);
320
321 builder.Send(auto_mode_builder.Finish());
322 }
milind-u4b31c4d2021-09-18 16:08:23 -0700323
Austin Schuh6c053ef2021-09-26 14:32:16 -0700324 if (FLAGS_shooter_tuning) {
milind-u4b31c4d2021-09-18 16:08:23 -0700325 // Distance between beambreak sensors, in meters.
326 constexpr double kDistanceBetweenBeambreaks = 0.4813;
327
Austin Schuh6c053ef2021-09-26 14:32:16 -0700328 if (ball_beambreak_reader_.pulses_detected() > balls_detected_) {
329 balls_detected_ = ball_beambreak_reader_.pulses_detected();
330
331 auto builder = shooter_tuning_readings_sender_.MakeBuilder();
332 auto shooter_tuning_readings_builder =
333 builder.MakeBuilder<superstructure::shooter::TuningReadings>();
334 shooter_tuning_readings_builder.add_velocity_ball(
335 kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width());
336 builder.Send(shooter_tuning_readings_builder.Finish());
337 }
milind-u4b31c4d2021-09-18 16:08:23 -0700338 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800339 }
340
341 private:
342 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
343 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
344 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
345 drivetrain_position_sender_;
milind-u4b31c4d2021-09-18 16:08:23 -0700346 ::aos::Sender<superstructure::shooter::TuningReadings>
347 shooter_tuning_readings_sender_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800348
Alex Perryc4691f52020-02-17 19:20:01 -0800349 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
350
Ravago Jones937587c2020-12-26 17:21:09 -0800351 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
352
353 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800354
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800355 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
milind-u3d68aa72021-09-26 12:19:03 -0700356 left_accelerator_encoder_, right_accelerator_encoder_;
milind-u9aa62a82021-10-03 20:43:19 -0700357
Stephan Massaltd021f972020-01-05 20:41:23 -0800358 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800359
360 frc971::wpilib::ADIS16470 *imu_ = nullptr;
milind-u4b31c4d2021-09-18 16:08:23 -0700361
362 // Used to interface with the two beam break sensors that the ball for tuning
363 // shooter parameters has to pass through.
364 // We will time how long it takes to pass between the two sensors to get its
365 // velocity.
366 std::array<std::unique_ptr<frc::DigitalInput>, 2> ball_beambreak_inputs_;
367 frc971::wpilib::DMAPulseSeparationReader ball_beambreak_reader_;
368 int balls_detected_ = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800369};
370
371class SuperstructureWriter
372 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
373 public:
374 SuperstructureWriter(::aos::EventLoop *event_loop)
375 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
376 event_loop, "/superstructure") {}
377
Alex Perryc4691f52020-02-17 19:20:01 -0800378 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
379 hood_victor_ = ::std::move(t);
380 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800381
Alex Perryc4691f52020-02-17 19:20:01 -0800382 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
383 intake_joint_victor_ = ::std::move(t);
384 }
385
386 void set_intake_roller_falcon(
387 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
388 intake_roller_falcon_ = ::std::move(t);
389 intake_roller_falcon_->ConfigSupplyCurrentLimit(
390 {true, Values::kIntakeRollerSupplyCurrentLimit(),
391 Values::kIntakeRollerSupplyCurrentLimit(), 0});
392 intake_roller_falcon_->ConfigStatorCurrentLimit(
393 {true, Values::kIntakeRollerStatorCurrentLimit(),
394 Values::kIntakeRollerStatorCurrentLimit(), 0});
395 }
396
397 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
398 turret_victor_ = ::std::move(t);
399 }
400
Ravago Jones3dda5602021-03-10 00:33:13 -0800401 void set_feeder_falcon(
402 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800403 feeder_falcon_ = ::std::move(t);
Austin Schuh3399d122021-03-20 14:29:20 -0700404 {
405 auto result = feeder_falcon_->ConfigSupplyCurrentLimit(
406 {true, Values::kFeederSupplyCurrentLimit(),
407 Values::kFeederSupplyCurrentLimit(), 0});
408 if (result != ctre::phoenix::OKAY) {
409 LOG(WARNING) << "Failed to configure feeder supply current limit: "
410 << result;
411 }
412 }
413 {
414 auto result = feeder_falcon_->ConfigStatorCurrentLimit(
415 {true, Values::kFeederStatorCurrentLimit(),
416 Values::kFeederStatorCurrentLimit(), 0});
417 if (result != ctre::phoenix::OKAY) {
418 LOG(WARNING) << "Failed to configure feeder stator current limit: "
419 << result;
420 }
421 }
Alex Perryc4691f52020-02-17 19:20:01 -0800422 }
423
milind-u9aa62a82021-10-03 20:43:19 -0700424 void set_washing_machine_control_panel_victor(
425 ::std::unique_ptr<::frc::VictorSP> t) {
426 washing_machine_control_panel_victor_ = ::std::move(t);
427 }
428
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800429 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
430 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800431 }
432
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800433 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
434 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800435 }
436
Austin Schuh0ad31d72021-03-06 17:07:04 -0800437 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
438 finisher_falcon0_ = ::std::move(t);
439 }
440
441 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
442 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800443 }
444
445 void set_climber_falcon(
446 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
447 climber_falcon_ = ::std::move(t);
448 climber_falcon_->ConfigSupplyCurrentLimit(
449 {true, Values::kClimberSupplyCurrentLimit(),
450 Values::kClimberSupplyCurrentLimit(), 0});
451 }
452
453 private:
454 void Write(const superstructure::Output &output) override {
Austin Schuh2efe1682021-03-06 22:47:15 -0800455 hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower,
456 kMaxBringupPower) /
457 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800458
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800459 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
460 -kMaxBringupPower,
461 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800462 12.0);
463
464 intake_roller_falcon_->Set(
465 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800466 std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower,
467 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800468 12.0);
469
milind-uf70e8e12021-10-02 12:36:00 -0700470 turret_victor_->SetSpeed(std::clamp(output.turret_voltage(),
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800471 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800472 12.0);
473
Ravago Jones3dda5602021-03-10 00:33:13 -0800474 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
475 std::clamp(output.feeder_voltage(), -kMaxBringupPower,
476 kMaxBringupPower) /
477 12.0);
milind-u9aa62a82021-10-03 20:43:19 -0700478 if (washing_machine_control_panel_victor_) {
479 washing_machine_control_panel_victor_->SetSpeed(
480 std::clamp(-output.washing_machine_spinner_voltage(),
481 -kMaxBringupPower, kMaxBringupPower) /
482 12.0);
483 }
Alex Perryc4691f52020-02-17 19:20:01 -0800484
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800485 accelerator_left_falcon_->SetSpeed(
486 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
487 kMaxBringupPower) /
488 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800489
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800490 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800491 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800492 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800493 12.0);
494
Austin Schuh0ad31d72021-03-06 17:07:04 -0800495 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
496 -kMaxBringupPower,
497 kMaxBringupPower) /
498 12.0);
499 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
500 -kMaxBringupPower,
501 kMaxBringupPower) /
502 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800503
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800504 if (climber_falcon_) {
505 climber_falcon_->Set(
506 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Tyler Chatow1039e432020-02-28 21:37:50 -0800507 std::clamp(-output.climber_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800508 kMaxBringupPower) /
509 12.0);
510 }
Alex Perryc4691f52020-02-17 19:20:01 -0800511 }
512
513 void Stop() override {
514 AOS_LOG(WARNING, "Superstructure output too old.\n");
515 hood_victor_->SetDisabled();
516 intake_joint_victor_->SetDisabled();
517 turret_victor_->SetDisabled();
milind-u9aa62a82021-10-03 20:43:19 -0700518 if (washing_machine_control_panel_victor_) {
519 washing_machine_control_panel_victor_->SetDisabled();
520 }
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800521 accelerator_left_falcon_->SetDisabled();
522 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800523 finisher_falcon0_->SetDisabled();
524 finisher_falcon1_->SetDisabled();
Ravago Jones3dda5602021-03-10 00:33:13 -0800525 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
526 intake_roller_falcon_->Set(
527 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
528 climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Alex Perryc4691f52020-02-17 19:20:01 -0800529 }
530
531 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
milind-u9aa62a82021-10-03 20:43:19 -0700532 turret_victor_, washing_machine_control_panel_victor_;
Alex Perryc4691f52020-02-17 19:20:01 -0800533
Ravago Jones3dda5602021-03-10 00:33:13 -0800534 ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800535 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800536
537 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
Ravago Jones3dda5602021-03-10 00:33:13 -0800538 intake_roller_falcon_, climber_falcon_, feeder_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800539};
540
541class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
542 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800543 ::std::unique_ptr<frc::Encoder> make_encoder(
544 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800545 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800546 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800547 }
548
549 void Run() override {
550 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
551 aos::configuration::ReadConfig("config.json");
552
553 // Thread 1.
554 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
555 ::frc971::wpilib::JoystickSender joystick_sender(
556 &joystick_sender_event_loop);
557 AddLoop(&joystick_sender_event_loop);
558
559 // Thread 2.
560 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
561 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
562 AddLoop(&pdp_fetcher_event_loop);
563
564 // Thread 3.
565 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
566 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800567 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800568 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
569 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800570 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800571 sensor_reader.set_hood_encoder(
572 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800573
Ravago Jones937587c2020-12-26 17:21:09 -0800574 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
575 sensor_reader.set_hood_single_turn_absolute_pwm(
576 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800577
Sabina Davisf7afd112020-02-23 13:42:14 -0800578 sensor_reader.set_intake_encoder(make_encoder(5));
579 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800580
Sabina Davisf7afd112020-02-23 13:42:14 -0800581 sensor_reader.set_turret_encoder(make_encoder(2));
582 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
583 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800584
Sabina Davisf7afd112020-02-23 13:42:14 -0800585 sensor_reader.set_finisher_encoder(
586 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
587 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
588 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
589
milind-u4b31c4d2021-09-18 16:08:23 -0700590 if (FLAGS_shooter_tuning) {
591 sensor_reader.set_ball_beambreak_inputs(
592 make_unique<frc::DigitalInput>(6), make_unique<frc::DigitalInput>(7));
593 }
594
James Kuszmaul0a981402021-10-09 21:00:34 -0700595 AddLoop(&sensor_reader_event_loop);
596
James Kuszmaul022d40e2020-02-11 17:06:18 -0800597 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
598 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
599 // CS0.
James Kuszmaul0a981402021-10-09 21:00:34 -0700600 // TODO(james): Double check whether the above is still accurate/useful with
601 // the ADIS16448. No reason it shouldn't be.
602 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS1;
Austin Schuh83873c32020-02-22 14:58:39 -0800603 std::unique_ptr<frc::DigitalInput> imu_trigger;
604 std::unique_ptr<frc::DigitalOutput> imu_reset;
605 if (::aos::network::GetTeamNumber() ==
606 constants::Values::kCodingRobotTeamNumber) {
607 imu_trigger = make_unique<frc::DigitalInput>(26);
608 imu_reset = make_unique<frc::DigitalOutput>(27);
609 spi_port = frc::SPI::Port::kOnboardCS0;
610 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800611 imu_trigger = make_unique<frc::DigitalInput>(9);
612 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800613 }
James Kuszmaul0a981402021-10-09 21:00:34 -0700614 ::aos::ShmEventLoop imu_event_loop(&config.message());
615 std::unique_ptr<frc971::wpilib::ADIS16448> old_imu;
616 std::unique_ptr<frc971::wpilib::ADIS16470> new_imu;
617 std::unique_ptr<frc::SPI> imu_spi;
618 if (::aos::network::GetTeamNumber() ==
619 constants::Values::kCompTeamNumber) {
620 old_imu = make_unique<frc971::wpilib::ADIS16448>(
621 &imu_event_loop, spi_port, imu_trigger.get());
622 old_imu->SetDummySPI(frc::SPI::Port::kOnboardCS2);
623 old_imu->set_reset(imu_reset.get());
624 } else {
625 imu_spi = make_unique<frc::SPI>(spi_port);
626 new_imu = make_unique<frc971::wpilib::ADIS16470>(
627 &imu_event_loop, imu_spi.get(), imu_trigger.get(), imu_reset.get());
628 sensor_reader.set_imu(new_imu.get());
629 }
630 AddLoop(&imu_event_loop);
Stephan Massaltd021f972020-01-05 20:41:23 -0800631
632 // Thread 4.
633 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800634 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800635 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
636 drivetrain_writer.set_left_controller0(
637 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
638 drivetrain_writer.set_right_controller0(
639 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
640
641 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800642 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800643 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800644 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800645 superstructure_writer.set_intake_roller_falcon(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800646 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
647 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Ravago Jones3dda5602021-03-10 00:33:13 -0800648 superstructure_writer.set_feeder_falcon(
649 make_unique<ctre::phoenix::motorcontrol::can::TalonFX>(1));
milind-u9aa62a82021-10-03 20:43:19 -0700650 superstructure_writer.set_washing_machine_control_panel_victor(
651 make_unique<frc::VictorSP>(6));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800652 superstructure_writer.set_accelerator_left_falcon(
653 make_unique<::frc::TalonFX>(5));
654 superstructure_writer.set_accelerator_right_falcon(
655 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800656 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
657 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800658 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800659 superstructure_writer.set_climber_falcon(
Ravago Jones3dda5602021-03-10 00:33:13 -0800660 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Stephan Massaltd021f972020-01-05 20:41:23 -0800661
662 AddLoop(&output_event_loop);
663
664 RunLoops();
665 }
666};
667
668} // namespace wpilib
669} // namespace y2020
670
671AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);