blob: 217c6b763593f444ade5ff77b2b58fba39eed798 [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 Kuszmaula244a912020-01-18 13:50:50 -080039#include "frc971/wpilib/ADIS16470.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080040#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/buffered_solenoid.h"
42#include "frc971/wpilib/dma.h"
43#include "frc971/wpilib/drivetrain_writer.h"
44#include "frc971/wpilib/encoder_and_potentiometer.h"
45#include "frc971/wpilib/joystick_sender.h"
46#include "frc971/wpilib/logging_generated.h"
47#include "frc971/wpilib/loop_output_handler.h"
48#include "frc971/wpilib/pdp_fetcher.h"
49#include "frc971/wpilib/sensor_reader.h"
50#include "frc971/wpilib/wpilib_robot_base.h"
milind-u4b31c4d2021-09-18 16:08:23 -070051#include "gflags/gflags.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080052#include "y2020/constants.h"
milind-u4b31c4d2021-09-18 16:08:23 -070053#include "y2020/control_loops/superstructure/shooter/shooter_tuning_readings_generated.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080054#include "y2020/control_loops/superstructure/superstructure_output_generated.h"
55#include "y2020/control_loops/superstructure/superstructure_position_generated.h"
56
milind-u4b31c4d2021-09-18 16:08:23 -070057DEFINE_bool(shooter_tuning, true,
58 "If true, reads from ball beambreak sensors and sends shooter "
59 "tuning readings");
60
Stephan Massaltd021f972020-01-05 20:41:23 -080061using ::aos::monotonic_clock;
62using ::y2020::constants::Values;
63namespace superstructure = ::y2020::control_loops::superstructure;
64namespace chrono = ::std::chrono;
Vinay Sivae52a6b32021-07-10 15:19:26 -070065using std::make_unique;
Stephan Massaltd021f972020-01-05 20:41:23 -080066
67namespace y2020 {
68namespace wpilib {
69namespace {
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
112} // namespace
113
114// Class to send position messages with sensor readings to our loops.
115class SensorReader : public ::frc971::wpilib::SensorReader {
116 public:
117 SensorReader(::aos::ShmEventLoop *event_loop)
118 : ::frc971::wpilib::SensorReader(event_loop),
119 auto_mode_sender_(
120 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
121 "/autonomous")),
122 superstructure_position_sender_(
123 event_loop->MakeSender<superstructure::Position>(
124 "/superstructure")),
125 drivetrain_position_sender_(
126 event_loop
127 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
milind-u4b31c4d2021-09-18 16:08:23 -0700128 "/drivetrain")),
129 shooter_tuning_readings_sender_(
130 event_loop->MakeSender<superstructure::shooter::TuningReadings>(
131 "/superstructure")) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800132 // Set to filter out anything shorter than 1/4 of the minimum pulse width
133 // we should ever see.
134 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
135 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
milind-u62d4a8e2021-10-11 16:08:41 -0700136
137 constants::InitValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800138 }
139
Alex Perryc4691f52020-02-17 19:20:01 -0800140 // Hood
Alex Perryc4691f52020-02-17 19:20:01 -0800141 void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) {
142 medium_encoder_filter_.Add(encoder.get());
143 hood_encoder_.set_encoder(::std::move(encoder));
144 }
145
146 void set_hood_absolute_pwm(
147 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
148 hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
149 }
150
Ravago Jones937587c2020-12-26 17:21:09 -0800151 void set_hood_single_turn_absolute_pwm(
152 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
153 hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm));
154 }
155
Alex Perryc4691f52020-02-17 19:20:01 -0800156 // Intake
157
158 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
159 medium_encoder_filter_.Add(encoder.get());
160 intake_joint_encoder_.set_encoder(::std::move(encoder));
161 }
162
163 void set_intake_absolute_pwm(
164 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
165 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
166 }
167
168 // Turret
169
170 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
171 medium_encoder_filter_.Add(encoder.get());
172 turret_encoder_.set_encoder(::std::move(encoder));
173 }
174
175 void set_turret_absolute_pwm(
176 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
177 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
178 }
179
180 void set_turret_potentiometer(
181 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
182 turret_encoder_.set_potentiometer(::std::move(potentiometer));
183 }
184
185 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800186 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800187 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800188 finisher_encoder_ = ::std::move(encoder);
189 }
190 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
191 fast_encoder_filter_.Add(encoder.get());
192 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800193 }
194
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800195 void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800196 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800197 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800198 }
199
Stephan Massaltd021f972020-01-05 20:41:23 -0800200 // Auto mode switches.
201 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800202 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800203 autonomous_modes_.at(i) = ::std::move(sensor);
204 }
205
James Kuszmaula244a912020-01-18 13:50:50 -0800206 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
207
milind-u4b31c4d2021-09-18 16:08:23 -0700208 void set_ball_beambreak_inputs(::std::unique_ptr<frc::DigitalInput> sensor1,
209 ::std::unique_ptr<frc::DigitalInput> sensor2) {
210 ball_beambreak_inputs_[0] = ::std::move(sensor1);
211 ball_beambreak_inputs_[1] = ::std::move(sensor2);
Austin Schuh6c053ef2021-09-26 14:32:16 -0700212 ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get());
213 ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get());
milind-u4b31c4d2021-09-18 16:08:23 -0700214 }
215
216 void Start() override {
217 if (FLAGS_shooter_tuning) {
218 AddToDMA(&ball_beambreak_reader_);
219 }
220 }
221
Stephan Massaltd021f972020-01-05 20:41:23 -0800222 void RunIteration() override {
James Kuszmaula244a912020-01-18 13:50:50 -0800223 CHECK_NOTNULL(imu_)->DoReads();
224
Stephan Massaltd021f972020-01-05 20:41:23 -0800225 {
226 auto builder = drivetrain_position_sender_.MakeBuilder();
227 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
228 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
229 drivetrain_builder.add_left_encoder(
230 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
231 drivetrain_builder.add_left_speed(
232 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
233
234 drivetrain_builder.add_right_encoder(
235 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
236 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
237 drivetrain_right_encoder_->GetPeriod()));
238
239 builder.Send(drivetrain_builder.Finish());
240 }
Alex Perryc4691f52020-02-17 19:20:01 -0800241 const auto values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800242
243 {
244 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800245
Alex Perryc4691f52020-02-17 19:20:01 -0800246 // TODO(alex): check new absolute encoder api.
247 // Hood
Ravago Jones937587c2020-12-26 17:21:09 -0800248 frc971::AbsoluteAndAbsolutePositionT hood;
Alex Perryc4691f52020-02-17 19:20:01 -0800249 CopyPosition(hood_encoder_, &hood,
250 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800251 Values::kHoodEncoderRatio(),
252 Values::kHoodSingleTurnEncoderRatio(), false);
253 flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset =
254 frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood);
Alex Perryc4691f52020-02-17 19:20:01 -0800255
256 // Intake
257 frc971::AbsolutePositionT intake_joint;
258 CopyPosition(intake_joint_encoder_, &intake_joint,
259 Values::kIntakeEncoderCountsPerRevolution(),
260 Values::kIntakeEncoderRatio(), false);
261 flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset =
262 frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint);
263
264 // Turret
265 frc971::PotAndAbsolutePositionT turret;
266 CopyPosition(turret_encoder_, &turret,
267 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800268 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800269 values.turret.potentiometer_offset);
270 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
271 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
272
273 // Shooter
274 y2020::control_loops::superstructure::ShooterPositionT shooter;
275 shooter.theta_finisher =
Austin Schuh0ad31d72021-03-06 17:07:04 -0800276 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800277 Values::kFinisherEncoderCountsPerRevolution(),
278 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800279
Alex Perryc4691f52020-02-17 19:20:01 -0800280 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800281 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800282 Values::kAcceleratorEncoderCountsPerRevolution(),
283 Values::kAcceleratorEncoderRatio());
284 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800285 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800286 Values::kAcceleratorEncoderCountsPerRevolution(),
287 Values::kAcceleratorEncoderRatio());
288 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
289 shooter_offset =
290 y2020::control_loops::superstructure::ShooterPosition::Pack(
291 *builder.fbb(), &shooter);
292
Austin Schuh18ca45f2020-02-29 22:58:49 -0800293 superstructure::Position::Builder position_builder =
294 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800295 position_builder.add_hood(hood_offset);
296 position_builder.add_intake_joint(intake_joint_offset);
297 position_builder.add_turret(turret_offset);
298 position_builder.add_shooter(shooter_offset);
Alex Perryc4691f52020-02-17 19:20:01 -0800299
Stephan Massaltd021f972020-01-05 20:41:23 -0800300 builder.Send(position_builder.Finish());
301 }
302
303 {
304 auto builder = auto_mode_sender_.MakeBuilder();
305
306 uint32_t mode = 0;
307 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
308 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
309 mode |= 1 << i;
310 }
311 }
312
313 auto auto_mode_builder =
314 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
315
316 auto_mode_builder.add_mode(mode);
317
318 builder.Send(auto_mode_builder.Finish());
319 }
milind-u4b31c4d2021-09-18 16:08:23 -0700320
Austin Schuh6c053ef2021-09-26 14:32:16 -0700321 if (FLAGS_shooter_tuning) {
milind-u4b31c4d2021-09-18 16:08:23 -0700322 // Distance between beambreak sensors, in meters.
323 constexpr double kDistanceBetweenBeambreaks = 0.4813;
324
Austin Schuh6c053ef2021-09-26 14:32:16 -0700325 if (ball_beambreak_reader_.pulses_detected() > balls_detected_) {
326 balls_detected_ = ball_beambreak_reader_.pulses_detected();
327
328 auto builder = shooter_tuning_readings_sender_.MakeBuilder();
329 auto shooter_tuning_readings_builder =
330 builder.MakeBuilder<superstructure::shooter::TuningReadings>();
331 shooter_tuning_readings_builder.add_velocity_ball(
332 kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width());
333 builder.Send(shooter_tuning_readings_builder.Finish());
334 }
milind-u4b31c4d2021-09-18 16:08:23 -0700335 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800336 }
337
338 private:
339 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
340 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
341 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
342 drivetrain_position_sender_;
milind-u4b31c4d2021-09-18 16:08:23 -0700343 ::aos::Sender<superstructure::shooter::TuningReadings>
344 shooter_tuning_readings_sender_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800345
Alex Perryc4691f52020-02-17 19:20:01 -0800346 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
347
Ravago Jones937587c2020-12-26 17:21:09 -0800348 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
349
350 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800351
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800352 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
milind-u3d68aa72021-09-26 12:19:03 -0700353 left_accelerator_encoder_, right_accelerator_encoder_;
milind-u9aa62a82021-10-03 20:43:19 -0700354
Stephan Massaltd021f972020-01-05 20:41:23 -0800355 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800356
357 frc971::wpilib::ADIS16470 *imu_ = nullptr;
milind-u4b31c4d2021-09-18 16:08:23 -0700358
359 // Used to interface with the two beam break sensors that the ball for tuning
360 // shooter parameters has to pass through.
361 // We will time how long it takes to pass between the two sensors to get its
362 // velocity.
363 std::array<std::unique_ptr<frc::DigitalInput>, 2> ball_beambreak_inputs_;
364 frc971::wpilib::DMAPulseSeparationReader ball_beambreak_reader_;
365 int balls_detected_ = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800366};
367
368class SuperstructureWriter
369 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
370 public:
371 SuperstructureWriter(::aos::EventLoop *event_loop)
372 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
373 event_loop, "/superstructure") {}
374
Alex Perryc4691f52020-02-17 19:20:01 -0800375 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
376 hood_victor_ = ::std::move(t);
377 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800378
Alex Perryc4691f52020-02-17 19:20:01 -0800379 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
380 intake_joint_victor_ = ::std::move(t);
381 }
382
383 void set_intake_roller_falcon(
384 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
385 intake_roller_falcon_ = ::std::move(t);
386 intake_roller_falcon_->ConfigSupplyCurrentLimit(
387 {true, Values::kIntakeRollerSupplyCurrentLimit(),
388 Values::kIntakeRollerSupplyCurrentLimit(), 0});
389 intake_roller_falcon_->ConfigStatorCurrentLimit(
390 {true, Values::kIntakeRollerStatorCurrentLimit(),
391 Values::kIntakeRollerStatorCurrentLimit(), 0});
392 }
393
394 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
395 turret_victor_ = ::std::move(t);
396 }
397
Ravago Jones3dda5602021-03-10 00:33:13 -0800398 void set_feeder_falcon(
399 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800400 feeder_falcon_ = ::std::move(t);
Austin Schuh3399d122021-03-20 14:29:20 -0700401 {
402 auto result = feeder_falcon_->ConfigSupplyCurrentLimit(
403 {true, Values::kFeederSupplyCurrentLimit(),
404 Values::kFeederSupplyCurrentLimit(), 0});
405 if (result != ctre::phoenix::OKAY) {
406 LOG(WARNING) << "Failed to configure feeder supply current limit: "
407 << result;
408 }
409 }
410 {
411 auto result = feeder_falcon_->ConfigStatorCurrentLimit(
412 {true, Values::kFeederStatorCurrentLimit(),
413 Values::kFeederStatorCurrentLimit(), 0});
414 if (result != ctre::phoenix::OKAY) {
415 LOG(WARNING) << "Failed to configure feeder stator current limit: "
416 << result;
417 }
418 }
Alex Perryc4691f52020-02-17 19:20:01 -0800419 }
420
milind-u9aa62a82021-10-03 20:43:19 -0700421 void set_washing_machine_control_panel_victor(
422 ::std::unique_ptr<::frc::VictorSP> t) {
423 washing_machine_control_panel_victor_ = ::std::move(t);
424 }
425
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800426 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
427 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800428 }
429
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800430 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
431 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800432 }
433
Austin Schuh0ad31d72021-03-06 17:07:04 -0800434 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
435 finisher_falcon0_ = ::std::move(t);
436 }
437
438 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
439 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800440 }
441
442 void set_climber_falcon(
443 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
444 climber_falcon_ = ::std::move(t);
445 climber_falcon_->ConfigSupplyCurrentLimit(
446 {true, Values::kClimberSupplyCurrentLimit(),
447 Values::kClimberSupplyCurrentLimit(), 0});
448 }
449
450 private:
451 void Write(const superstructure::Output &output) override {
Austin Schuh2efe1682021-03-06 22:47:15 -0800452 hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower,
453 kMaxBringupPower) /
454 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800455
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800456 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
457 -kMaxBringupPower,
458 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800459 12.0);
460
461 intake_roller_falcon_->Set(
462 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800463 std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower,
464 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800465 12.0);
466
milind-uf70e8e12021-10-02 12:36:00 -0700467 turret_victor_->SetSpeed(std::clamp(output.turret_voltage(),
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800468 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800469 12.0);
470
Ravago Jones3dda5602021-03-10 00:33:13 -0800471 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
472 std::clamp(output.feeder_voltage(), -kMaxBringupPower,
473 kMaxBringupPower) /
474 12.0);
milind-u9aa62a82021-10-03 20:43:19 -0700475 if (washing_machine_control_panel_victor_) {
476 washing_machine_control_panel_victor_->SetSpeed(
477 std::clamp(-output.washing_machine_spinner_voltage(),
478 -kMaxBringupPower, kMaxBringupPower) /
479 12.0);
480 }
Alex Perryc4691f52020-02-17 19:20:01 -0800481
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800482 accelerator_left_falcon_->SetSpeed(
483 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
484 kMaxBringupPower) /
485 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800486
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800487 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800488 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800489 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800490 12.0);
491
Austin Schuh0ad31d72021-03-06 17:07:04 -0800492 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
493 -kMaxBringupPower,
494 kMaxBringupPower) /
495 12.0);
496 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
497 -kMaxBringupPower,
498 kMaxBringupPower) /
499 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800500
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800501 if (climber_falcon_) {
502 climber_falcon_->Set(
503 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Tyler Chatow1039e432020-02-28 21:37:50 -0800504 std::clamp(-output.climber_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800505 kMaxBringupPower) /
506 12.0);
507 }
Alex Perryc4691f52020-02-17 19:20:01 -0800508 }
509
510 void Stop() override {
511 AOS_LOG(WARNING, "Superstructure output too old.\n");
512 hood_victor_->SetDisabled();
513 intake_joint_victor_->SetDisabled();
514 turret_victor_->SetDisabled();
milind-u9aa62a82021-10-03 20:43:19 -0700515 if (washing_machine_control_panel_victor_) {
516 washing_machine_control_panel_victor_->SetDisabled();
517 }
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800518 accelerator_left_falcon_->SetDisabled();
519 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800520 finisher_falcon0_->SetDisabled();
521 finisher_falcon1_->SetDisabled();
Ravago Jones3dda5602021-03-10 00:33:13 -0800522 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
523 intake_roller_falcon_->Set(
524 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
525 climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Alex Perryc4691f52020-02-17 19:20:01 -0800526 }
527
528 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
milind-u9aa62a82021-10-03 20:43:19 -0700529 turret_victor_, washing_machine_control_panel_victor_;
Alex Perryc4691f52020-02-17 19:20:01 -0800530
Ravago Jones3dda5602021-03-10 00:33:13 -0800531 ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800532 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800533
534 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
Ravago Jones3dda5602021-03-10 00:33:13 -0800535 intake_roller_falcon_, climber_falcon_, feeder_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800536};
537
538class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
539 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800540 ::std::unique_ptr<frc::Encoder> make_encoder(
541 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800542 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800543 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800544 }
545
546 void Run() override {
547 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
548 aos::configuration::ReadConfig("config.json");
549
550 // Thread 1.
551 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
552 ::frc971::wpilib::JoystickSender joystick_sender(
553 &joystick_sender_event_loop);
554 AddLoop(&joystick_sender_event_loop);
555
556 // Thread 2.
557 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
558 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
559 AddLoop(&pdp_fetcher_event_loop);
560
561 // Thread 3.
562 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
563 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800564 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800565 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
566 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800567 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800568 sensor_reader.set_hood_encoder(
569 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800570
Ravago Jones937587c2020-12-26 17:21:09 -0800571 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
572 sensor_reader.set_hood_single_turn_absolute_pwm(
573 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800574
Sabina Davisf7afd112020-02-23 13:42:14 -0800575 sensor_reader.set_intake_encoder(make_encoder(5));
576 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800577
Sabina Davisf7afd112020-02-23 13:42:14 -0800578 sensor_reader.set_turret_encoder(make_encoder(2));
579 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
580 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800581
Sabina Davisf7afd112020-02-23 13:42:14 -0800582 sensor_reader.set_finisher_encoder(
583 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
584 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
585 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
586
milind-u4b31c4d2021-09-18 16:08:23 -0700587 if (FLAGS_shooter_tuning) {
588 sensor_reader.set_ball_beambreak_inputs(
589 make_unique<frc::DigitalInput>(6), make_unique<frc::DigitalInput>(7));
590 }
591
James Kuszmaul022d40e2020-02-11 17:06:18 -0800592 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
593 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
594 // CS0.
Austin Schuh83873c32020-02-22 14:58:39 -0800595 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS2;
596 std::unique_ptr<frc::DigitalInput> imu_trigger;
597 std::unique_ptr<frc::DigitalOutput> imu_reset;
598 if (::aos::network::GetTeamNumber() ==
599 constants::Values::kCodingRobotTeamNumber) {
600 imu_trigger = make_unique<frc::DigitalInput>(26);
601 imu_reset = make_unique<frc::DigitalOutput>(27);
602 spi_port = frc::SPI::Port::kOnboardCS0;
603 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800604 imu_trigger = make_unique<frc::DigitalInput>(9);
605 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800606 }
607 auto spi = make_unique<frc::SPI>(spi_port);
James Kuszmaula244a912020-01-18 13:50:50 -0800608 frc971::wpilib::ADIS16470 imu(&sensor_reader_event_loop, spi.get(),
609 imu_trigger.get(), imu_reset.get());
610 sensor_reader.set_imu(&imu);
Stephan Massaltd021f972020-01-05 20:41:23 -0800611 AddLoop(&sensor_reader_event_loop);
612
613 // Thread 4.
614 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800615 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800616 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
617 drivetrain_writer.set_left_controller0(
618 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
619 drivetrain_writer.set_right_controller0(
620 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
621
622 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800623 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800624 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800625 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800626 superstructure_writer.set_intake_roller_falcon(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800627 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
628 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Ravago Jones3dda5602021-03-10 00:33:13 -0800629 superstructure_writer.set_feeder_falcon(
630 make_unique<ctre::phoenix::motorcontrol::can::TalonFX>(1));
milind-u9aa62a82021-10-03 20:43:19 -0700631 superstructure_writer.set_washing_machine_control_panel_victor(
632 make_unique<frc::VictorSP>(6));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800633 superstructure_writer.set_accelerator_left_falcon(
634 make_unique<::frc::TalonFX>(5));
635 superstructure_writer.set_accelerator_right_falcon(
636 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800637 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
638 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800639 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800640 superstructure_writer.set_climber_falcon(
Ravago Jones3dda5602021-03-10 00:33:13 -0800641 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Stephan Massaltd021f972020-01-05 20:41:23 -0800642
643 AddLoop(&output_event_loop);
644
645 RunLoops();
646 }
647};
648
649} // namespace wpilib
650} // namespace y2020
651
652AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);