blob: fb46d7126ab4d396ce8224002277d141ec0fd1ac [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);
136 }
137
Alex Perryc4691f52020-02-17 19:20:01 -0800138 // Hood
Alex Perryc4691f52020-02-17 19:20:01 -0800139 void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) {
140 medium_encoder_filter_.Add(encoder.get());
141 hood_encoder_.set_encoder(::std::move(encoder));
142 }
143
144 void set_hood_absolute_pwm(
145 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
146 hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
147 }
148
Ravago Jones937587c2020-12-26 17:21:09 -0800149 void set_hood_single_turn_absolute_pwm(
150 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
151 hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm));
152 }
153
Alex Perryc4691f52020-02-17 19:20:01 -0800154 // Intake
155
156 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
157 medium_encoder_filter_.Add(encoder.get());
158 intake_joint_encoder_.set_encoder(::std::move(encoder));
159 }
160
161 void set_intake_absolute_pwm(
162 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
163 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
164 }
165
166 // Turret
167
168 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
169 medium_encoder_filter_.Add(encoder.get());
170 turret_encoder_.set_encoder(::std::move(encoder));
171 }
172
173 void set_turret_absolute_pwm(
174 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
175 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
176 }
177
178 void set_turret_potentiometer(
179 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
180 turret_encoder_.set_potentiometer(::std::move(potentiometer));
181 }
182
183 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800184 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800185 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800186 finisher_encoder_ = ::std::move(encoder);
187 }
188 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
189 fast_encoder_filter_.Add(encoder.get());
190 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800191 }
192
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800193 void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800194 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800195 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800196 }
197
Stephan Massaltd021f972020-01-05 20:41:23 -0800198 // Auto mode switches.
199 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800200 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800201 autonomous_modes_.at(i) = ::std::move(sensor);
202 }
203
James Kuszmaula244a912020-01-18 13:50:50 -0800204 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
205
milind-u4b31c4d2021-09-18 16:08:23 -0700206 void set_ball_beambreak_inputs(::std::unique_ptr<frc::DigitalInput> sensor1,
207 ::std::unique_ptr<frc::DigitalInput> sensor2) {
208 ball_beambreak_inputs_[0] = ::std::move(sensor1);
209 ball_beambreak_inputs_[1] = ::std::move(sensor2);
Austin Schuh6c053ef2021-09-26 14:32:16 -0700210 ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get());
211 ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get());
milind-u4b31c4d2021-09-18 16:08:23 -0700212 }
213
214 void Start() override {
215 if (FLAGS_shooter_tuning) {
216 AddToDMA(&ball_beambreak_reader_);
217 }
218 }
219
Stephan Massaltd021f972020-01-05 20:41:23 -0800220 void RunIteration() override {
James Kuszmaula244a912020-01-18 13:50:50 -0800221 CHECK_NOTNULL(imu_)->DoReads();
222
Stephan Massaltd021f972020-01-05 20:41:23 -0800223 {
224 auto builder = drivetrain_position_sender_.MakeBuilder();
225 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
226 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
227 drivetrain_builder.add_left_encoder(
228 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
229 drivetrain_builder.add_left_speed(
230 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
231
232 drivetrain_builder.add_right_encoder(
233 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
234 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
235 drivetrain_right_encoder_->GetPeriod()));
236
237 builder.Send(drivetrain_builder.Finish());
238 }
Alex Perryc4691f52020-02-17 19:20:01 -0800239 const auto values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800240
241 {
242 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800243
Alex Perryc4691f52020-02-17 19:20:01 -0800244 // TODO(alex): check new absolute encoder api.
245 // Hood
Ravago Jones937587c2020-12-26 17:21:09 -0800246 frc971::AbsoluteAndAbsolutePositionT hood;
Alex Perryc4691f52020-02-17 19:20:01 -0800247 CopyPosition(hood_encoder_, &hood,
248 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800249 Values::kHoodEncoderRatio(),
250 Values::kHoodSingleTurnEncoderRatio(), false);
251 flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset =
252 frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood);
Alex Perryc4691f52020-02-17 19:20:01 -0800253
254 // Intake
255 frc971::AbsolutePositionT intake_joint;
256 CopyPosition(intake_joint_encoder_, &intake_joint,
257 Values::kIntakeEncoderCountsPerRevolution(),
258 Values::kIntakeEncoderRatio(), false);
259 flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset =
260 frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint);
261
262 // Turret
263 frc971::PotAndAbsolutePositionT turret;
264 CopyPosition(turret_encoder_, &turret,
265 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800266 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800267 values.turret.potentiometer_offset);
268 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
269 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
270
271 // Shooter
272 y2020::control_loops::superstructure::ShooterPositionT shooter;
273 shooter.theta_finisher =
Austin Schuh0ad31d72021-03-06 17:07:04 -0800274 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800275 Values::kFinisherEncoderCountsPerRevolution(),
276 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800277
Alex Perryc4691f52020-02-17 19:20:01 -0800278 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800279 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800280 Values::kAcceleratorEncoderCountsPerRevolution(),
281 Values::kAcceleratorEncoderRatio());
282 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800283 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800284 Values::kAcceleratorEncoderCountsPerRevolution(),
285 Values::kAcceleratorEncoderRatio());
286 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
287 shooter_offset =
288 y2020::control_loops::superstructure::ShooterPosition::Pack(
289 *builder.fbb(), &shooter);
290
Austin Schuh18ca45f2020-02-29 22:58:49 -0800291 superstructure::Position::Builder position_builder =
292 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800293 position_builder.add_hood(hood_offset);
294 position_builder.add_intake_joint(intake_joint_offset);
295 position_builder.add_turret(turret_offset);
296 position_builder.add_shooter(shooter_offset);
Alex Perryc4691f52020-02-17 19:20:01 -0800297
Stephan Massaltd021f972020-01-05 20:41:23 -0800298 builder.Send(position_builder.Finish());
299 }
300
301 {
302 auto builder = auto_mode_sender_.MakeBuilder();
303
304 uint32_t mode = 0;
305 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
306 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
307 mode |= 1 << i;
308 }
309 }
310
311 auto auto_mode_builder =
312 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
313
314 auto_mode_builder.add_mode(mode);
315
316 builder.Send(auto_mode_builder.Finish());
317 }
milind-u4b31c4d2021-09-18 16:08:23 -0700318
Austin Schuh6c053ef2021-09-26 14:32:16 -0700319 if (FLAGS_shooter_tuning) {
milind-u4b31c4d2021-09-18 16:08:23 -0700320 // Distance between beambreak sensors, in meters.
321 constexpr double kDistanceBetweenBeambreaks = 0.4813;
322
Austin Schuh6c053ef2021-09-26 14:32:16 -0700323 if (ball_beambreak_reader_.pulses_detected() > balls_detected_) {
324 balls_detected_ = ball_beambreak_reader_.pulses_detected();
325
326 auto builder = shooter_tuning_readings_sender_.MakeBuilder();
327 auto shooter_tuning_readings_builder =
328 builder.MakeBuilder<superstructure::shooter::TuningReadings>();
329 shooter_tuning_readings_builder.add_velocity_ball(
330 kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width());
331 builder.Send(shooter_tuning_readings_builder.Finish());
332 }
milind-u4b31c4d2021-09-18 16:08:23 -0700333 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800334 }
335
336 private:
337 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
338 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
339 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
340 drivetrain_position_sender_;
milind-u4b31c4d2021-09-18 16:08:23 -0700341 ::aos::Sender<superstructure::shooter::TuningReadings>
342 shooter_tuning_readings_sender_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800343
Alex Perryc4691f52020-02-17 19:20:01 -0800344 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
345
Ravago Jones937587c2020-12-26 17:21:09 -0800346 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
347
348 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800349
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800350 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
milind-u3d68aa72021-09-26 12:19:03 -0700351 left_accelerator_encoder_, right_accelerator_encoder_;
milind-u9aa62a82021-10-03 20:43:19 -0700352
Stephan Massaltd021f972020-01-05 20:41:23 -0800353 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800354
355 frc971::wpilib::ADIS16470 *imu_ = nullptr;
milind-u4b31c4d2021-09-18 16:08:23 -0700356
357 // Used to interface with the two beam break sensors that the ball for tuning
358 // shooter parameters has to pass through.
359 // We will time how long it takes to pass between the two sensors to get its
360 // velocity.
361 std::array<std::unique_ptr<frc::DigitalInput>, 2> ball_beambreak_inputs_;
362 frc971::wpilib::DMAPulseSeparationReader ball_beambreak_reader_;
363 int balls_detected_ = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800364};
365
366class SuperstructureWriter
367 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
368 public:
369 SuperstructureWriter(::aos::EventLoop *event_loop)
370 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
371 event_loop, "/superstructure") {}
372
Alex Perryc4691f52020-02-17 19:20:01 -0800373 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
374 hood_victor_ = ::std::move(t);
375 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800376
Alex Perryc4691f52020-02-17 19:20:01 -0800377 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
378 intake_joint_victor_ = ::std::move(t);
379 }
380
381 void set_intake_roller_falcon(
382 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
383 intake_roller_falcon_ = ::std::move(t);
384 intake_roller_falcon_->ConfigSupplyCurrentLimit(
385 {true, Values::kIntakeRollerSupplyCurrentLimit(),
386 Values::kIntakeRollerSupplyCurrentLimit(), 0});
387 intake_roller_falcon_->ConfigStatorCurrentLimit(
388 {true, Values::kIntakeRollerStatorCurrentLimit(),
389 Values::kIntakeRollerStatorCurrentLimit(), 0});
390 }
391
392 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
393 turret_victor_ = ::std::move(t);
394 }
395
Ravago Jones3dda5602021-03-10 00:33:13 -0800396 void set_feeder_falcon(
397 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800398 feeder_falcon_ = ::std::move(t);
Austin Schuh3399d122021-03-20 14:29:20 -0700399 {
400 auto result = feeder_falcon_->ConfigSupplyCurrentLimit(
401 {true, Values::kFeederSupplyCurrentLimit(),
402 Values::kFeederSupplyCurrentLimit(), 0});
403 if (result != ctre::phoenix::OKAY) {
404 LOG(WARNING) << "Failed to configure feeder supply current limit: "
405 << result;
406 }
407 }
408 {
409 auto result = feeder_falcon_->ConfigStatorCurrentLimit(
410 {true, Values::kFeederStatorCurrentLimit(),
411 Values::kFeederStatorCurrentLimit(), 0});
412 if (result != ctre::phoenix::OKAY) {
413 LOG(WARNING) << "Failed to configure feeder stator current limit: "
414 << result;
415 }
416 }
Alex Perryc4691f52020-02-17 19:20:01 -0800417 }
418
milind-u9aa62a82021-10-03 20:43:19 -0700419 void set_washing_machine_control_panel_victor(
420 ::std::unique_ptr<::frc::VictorSP> t) {
421 washing_machine_control_panel_victor_ = ::std::move(t);
422 }
423
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800424 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
425 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800426 }
427
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800428 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
429 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800430 }
431
Austin Schuh0ad31d72021-03-06 17:07:04 -0800432 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
433 finisher_falcon0_ = ::std::move(t);
434 }
435
436 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
437 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800438 }
439
440 void set_climber_falcon(
441 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
442 climber_falcon_ = ::std::move(t);
443 climber_falcon_->ConfigSupplyCurrentLimit(
444 {true, Values::kClimberSupplyCurrentLimit(),
445 Values::kClimberSupplyCurrentLimit(), 0});
446 }
447
448 private:
449 void Write(const superstructure::Output &output) override {
Austin Schuh2efe1682021-03-06 22:47:15 -0800450 hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower,
451 kMaxBringupPower) /
452 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800453
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800454 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
455 -kMaxBringupPower,
456 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800457 12.0);
458
459 intake_roller_falcon_->Set(
460 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800461 std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower,
462 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800463 12.0);
464
milind-uf70e8e12021-10-02 12:36:00 -0700465 turret_victor_->SetSpeed(std::clamp(output.turret_voltage(),
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800466 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800467 12.0);
468
Ravago Jones3dda5602021-03-10 00:33:13 -0800469 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
470 std::clamp(output.feeder_voltage(), -kMaxBringupPower,
471 kMaxBringupPower) /
472 12.0);
milind-u9aa62a82021-10-03 20:43:19 -0700473 if (washing_machine_control_panel_victor_) {
474 washing_machine_control_panel_victor_->SetSpeed(
475 std::clamp(-output.washing_machine_spinner_voltage(),
476 -kMaxBringupPower, kMaxBringupPower) /
477 12.0);
478 }
Alex Perryc4691f52020-02-17 19:20:01 -0800479
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800480 accelerator_left_falcon_->SetSpeed(
481 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
482 kMaxBringupPower) /
483 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800484
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800485 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800486 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800487 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800488 12.0);
489
Austin Schuh0ad31d72021-03-06 17:07:04 -0800490 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
491 -kMaxBringupPower,
492 kMaxBringupPower) /
493 12.0);
494 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
495 -kMaxBringupPower,
496 kMaxBringupPower) /
497 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800498
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800499 if (climber_falcon_) {
500 climber_falcon_->Set(
501 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Tyler Chatow1039e432020-02-28 21:37:50 -0800502 std::clamp(-output.climber_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800503 kMaxBringupPower) /
504 12.0);
505 }
Alex Perryc4691f52020-02-17 19:20:01 -0800506 }
507
508 void Stop() override {
509 AOS_LOG(WARNING, "Superstructure output too old.\n");
510 hood_victor_->SetDisabled();
511 intake_joint_victor_->SetDisabled();
512 turret_victor_->SetDisabled();
milind-u9aa62a82021-10-03 20:43:19 -0700513 if (washing_machine_control_panel_victor_) {
514 washing_machine_control_panel_victor_->SetDisabled();
515 }
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800516 accelerator_left_falcon_->SetDisabled();
517 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800518 finisher_falcon0_->SetDisabled();
519 finisher_falcon1_->SetDisabled();
Ravago Jones3dda5602021-03-10 00:33:13 -0800520 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
521 intake_roller_falcon_->Set(
522 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
523 climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Alex Perryc4691f52020-02-17 19:20:01 -0800524 }
525
526 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
milind-u9aa62a82021-10-03 20:43:19 -0700527 turret_victor_, washing_machine_control_panel_victor_;
Alex Perryc4691f52020-02-17 19:20:01 -0800528
Ravago Jones3dda5602021-03-10 00:33:13 -0800529 ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800530 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800531
532 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
Ravago Jones3dda5602021-03-10 00:33:13 -0800533 intake_roller_falcon_, climber_falcon_, feeder_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800534};
535
536class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
537 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800538 ::std::unique_ptr<frc::Encoder> make_encoder(
539 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800540 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800541 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800542 }
543
544 void Run() override {
545 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
546 aos::configuration::ReadConfig("config.json");
547
548 // Thread 1.
549 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
550 ::frc971::wpilib::JoystickSender joystick_sender(
551 &joystick_sender_event_loop);
552 AddLoop(&joystick_sender_event_loop);
553
554 // Thread 2.
555 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
556 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
557 AddLoop(&pdp_fetcher_event_loop);
558
559 // Thread 3.
560 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
561 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800562 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800563 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
564 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800565 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800566 sensor_reader.set_hood_encoder(
567 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800568
Ravago Jones937587c2020-12-26 17:21:09 -0800569 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
570 sensor_reader.set_hood_single_turn_absolute_pwm(
571 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800572
Sabina Davisf7afd112020-02-23 13:42:14 -0800573 sensor_reader.set_intake_encoder(make_encoder(5));
574 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800575
Sabina Davisf7afd112020-02-23 13:42:14 -0800576 sensor_reader.set_turret_encoder(make_encoder(2));
577 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
578 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800579
Sabina Davisf7afd112020-02-23 13:42:14 -0800580 sensor_reader.set_finisher_encoder(
581 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
582 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
583 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
584
milind-u4b31c4d2021-09-18 16:08:23 -0700585 if (FLAGS_shooter_tuning) {
586 sensor_reader.set_ball_beambreak_inputs(
587 make_unique<frc::DigitalInput>(6), make_unique<frc::DigitalInput>(7));
588 }
589
James Kuszmaul022d40e2020-02-11 17:06:18 -0800590 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
591 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
592 // CS0.
Austin Schuh83873c32020-02-22 14:58:39 -0800593 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS2;
594 std::unique_ptr<frc::DigitalInput> imu_trigger;
595 std::unique_ptr<frc::DigitalOutput> imu_reset;
596 if (::aos::network::GetTeamNumber() ==
597 constants::Values::kCodingRobotTeamNumber) {
598 imu_trigger = make_unique<frc::DigitalInput>(26);
599 imu_reset = make_unique<frc::DigitalOutput>(27);
600 spi_port = frc::SPI::Port::kOnboardCS0;
601 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800602 imu_trigger = make_unique<frc::DigitalInput>(9);
603 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800604 }
605 auto spi = make_unique<frc::SPI>(spi_port);
James Kuszmaula244a912020-01-18 13:50:50 -0800606 frc971::wpilib::ADIS16470 imu(&sensor_reader_event_loop, spi.get(),
607 imu_trigger.get(), imu_reset.get());
608 sensor_reader.set_imu(&imu);
Stephan Massaltd021f972020-01-05 20:41:23 -0800609 AddLoop(&sensor_reader_event_loop);
610
611 // Thread 4.
612 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800613 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800614 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
615 drivetrain_writer.set_left_controller0(
616 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
617 drivetrain_writer.set_right_controller0(
618 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
619
620 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800621 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800622 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800623 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800624 superstructure_writer.set_intake_roller_falcon(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800625 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
626 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Ravago Jones3dda5602021-03-10 00:33:13 -0800627 superstructure_writer.set_feeder_falcon(
628 make_unique<ctre::phoenix::motorcontrol::can::TalonFX>(1));
milind-u9aa62a82021-10-03 20:43:19 -0700629 superstructure_writer.set_washing_machine_control_panel_victor(
630 make_unique<frc::VictorSP>(6));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800631 superstructure_writer.set_accelerator_left_falcon(
632 make_unique<::frc::TalonFX>(5));
633 superstructure_writer.set_accelerator_right_falcon(
634 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800635 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
636 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800637 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800638 superstructure_writer.set_climber_falcon(
Ravago Jones3dda5602021-03-10 00:33:13 -0800639 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Stephan Massaltd021f972020-01-05 20:41:23 -0800640
641 AddLoop(&output_event_loop);
642
643 RunLoops();
644 }
645};
646
647} // namespace wpilib
648} // namespace y2020
649
650AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);