blob: 914c9c6502d41f83c429abfcb980154f8adb30fc [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"
51#include "y2020/constants.h"
52#include "y2020/control_loops/superstructure/superstructure_output_generated.h"
53#include "y2020/control_loops/superstructure/superstructure_position_generated.h"
54
55using ::aos::monotonic_clock;
56using ::y2020::constants::Values;
57namespace superstructure = ::y2020::control_loops::superstructure;
58namespace chrono = ::std::chrono;
Vinay Sivae52a6b32021-07-10 15:19:26 -070059using std::make_unique;
Stephan Massaltd021f972020-01-05 20:41:23 -080060
61namespace y2020 {
62namespace wpilib {
63namespace {
64
65constexpr double kMaxBringupPower = 12.0;
66
67// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
68// DMA stuff and then removing the * 2.0 in *_translate.
69// The low bit is direction.
70
Stephan Massaltd021f972020-01-05 20:41:23 -080071double drivetrain_translate(int32_t in) {
72 return ((static_cast<double>(in) /
73 Values::kDrivetrainEncoderCountsPerRevolution()) *
74 (2.0 * M_PI)) *
75 Values::kDrivetrainEncoderRatio() *
76 control_loops::drivetrain::kWheelRadius;
77}
78
79double drivetrain_velocity_translate(double in) {
80 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
81 (2.0 * M_PI)) *
82 Values::kDrivetrainEncoderRatio() *
83 control_loops::drivetrain::kWheelRadius;
84}
85
Alex Perryc4691f52020-02-17 19:20:01 -080086double turret_pot_translate(double voltage) {
87 return voltage * Values::kTurretPotRatio() *
88 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
89}
90
Stephan Massaltd021f972020-01-05 20:41:23 -080091constexpr double kMaxFastEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -080092 std::max({Values::kMaxControlPanelEncoderPulsesPerSecond(),
93 Values::kMaxFinisherEncoderPulsesPerSecond(),
94 Values::kMaxAcceleratorEncoderPulsesPerSecond()});
95static_assert(kMaxFastEncoderPulsesPerSecond <= 1000000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -080096 "fast encoders are too fast");
Alex Perryc4691f52020-02-17 19:20:01 -080097constexpr double kMaxMediumEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -080098 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
99 Values::kMaxHoodEncoderPulsesPerSecond(),
100 Values::kMaxIntakeEncoderPulsesPerSecond(),
101 Values::kMaxTurretEncoderPulsesPerSecond()});
Stephan Massaltd021f972020-01-05 20:41:23 -0800102
Austin Schuh9dcd5202020-02-20 20:06:04 -0800103static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -0800104 "medium encoders are too fast");
105
106} // namespace
107
108// Class to send position messages with sensor readings to our loops.
109class SensorReader : public ::frc971::wpilib::SensorReader {
110 public:
111 SensorReader(::aos::ShmEventLoop *event_loop)
112 : ::frc971::wpilib::SensorReader(event_loop),
113 auto_mode_sender_(
114 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
115 "/autonomous")),
116 superstructure_position_sender_(
117 event_loop->MakeSender<superstructure::Position>(
118 "/superstructure")),
119 drivetrain_position_sender_(
120 event_loop
121 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
122 "/drivetrain")) {
123 // Set to filter out anything shorter than 1/4 of the minimum pulse width
124 // we should ever see.
125 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
126 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
127 }
128
Alex Perryc4691f52020-02-17 19:20:01 -0800129 // Hood
Alex Perryc4691f52020-02-17 19:20:01 -0800130 void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) {
131 medium_encoder_filter_.Add(encoder.get());
132 hood_encoder_.set_encoder(::std::move(encoder));
133 }
134
135 void set_hood_absolute_pwm(
136 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
137 hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
138 }
139
Ravago Jones937587c2020-12-26 17:21:09 -0800140 void set_hood_single_turn_absolute_pwm(
141 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
142 hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm));
143 }
144
Alex Perryc4691f52020-02-17 19:20:01 -0800145 // Intake
146
147 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
148 medium_encoder_filter_.Add(encoder.get());
149 intake_joint_encoder_.set_encoder(::std::move(encoder));
150 }
151
152 void set_intake_absolute_pwm(
153 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
154 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
155 }
156
157 // Turret
158
159 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
160 medium_encoder_filter_.Add(encoder.get());
161 turret_encoder_.set_encoder(::std::move(encoder));
162 }
163
164 void set_turret_absolute_pwm(
165 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
166 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
167 }
168
169 void set_turret_potentiometer(
170 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
171 turret_encoder_.set_potentiometer(::std::move(potentiometer));
172 }
173
174 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800175 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800176 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800177 finisher_encoder_ = ::std::move(encoder);
178 }
179 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
180 fast_encoder_filter_.Add(encoder.get());
181 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800182 }
183
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800184 void set_right_accelerator_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 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800187 }
188
189 // Control Panel
190
191 void set_control_panel_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800192 fast_encoder_filter_.Add(encoder.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800193 control_panel_encoder_ = ::std::move(encoder);
194 }
195
Stephan Massaltd021f972020-01-05 20:41:23 -0800196 // Auto mode switches.
197 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800198 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800199 autonomous_modes_.at(i) = ::std::move(sensor);
200 }
201
James Kuszmaula244a912020-01-18 13:50:50 -0800202 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
203
Stephan Massaltd021f972020-01-05 20:41:23 -0800204 void RunIteration() override {
James Kuszmaula244a912020-01-18 13:50:50 -0800205 CHECK_NOTNULL(imu_)->DoReads();
206
Stephan Massaltd021f972020-01-05 20:41:23 -0800207 {
208 auto builder = drivetrain_position_sender_.MakeBuilder();
209 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
210 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
211 drivetrain_builder.add_left_encoder(
212 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
213 drivetrain_builder.add_left_speed(
214 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
215
216 drivetrain_builder.add_right_encoder(
217 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
218 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
219 drivetrain_right_encoder_->GetPeriod()));
220
221 builder.Send(drivetrain_builder.Finish());
222 }
Alex Perryc4691f52020-02-17 19:20:01 -0800223 const auto values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800224
225 {
226 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800227
Alex Perryc4691f52020-02-17 19:20:01 -0800228 // TODO(alex): check new absolute encoder api.
229 // Hood
Ravago Jones937587c2020-12-26 17:21:09 -0800230 frc971::AbsoluteAndAbsolutePositionT hood;
Alex Perryc4691f52020-02-17 19:20:01 -0800231 CopyPosition(hood_encoder_, &hood,
232 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800233 Values::kHoodEncoderRatio(),
234 Values::kHoodSingleTurnEncoderRatio(), false);
235 flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset =
236 frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood);
Alex Perryc4691f52020-02-17 19:20:01 -0800237
238 // Intake
239 frc971::AbsolutePositionT intake_joint;
240 CopyPosition(intake_joint_encoder_, &intake_joint,
241 Values::kIntakeEncoderCountsPerRevolution(),
242 Values::kIntakeEncoderRatio(), false);
243 flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset =
244 frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint);
245
246 // Turret
247 frc971::PotAndAbsolutePositionT turret;
248 CopyPosition(turret_encoder_, &turret,
249 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800250 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800251 values.turret.potentiometer_offset);
252 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
253 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
254
Austin Schuh18ca45f2020-02-29 22:58:49 -0800255 // Control Panel
256 frc971::RelativePositionT control_panel;
257 CopyPosition(*control_panel_encoder_, &control_panel,
258 Values::kControlPanelEncoderCountsPerRevolution(),
259 Values::kControlPanelEncoderRatio(), false);
260 flatbuffers::Offset<frc971::RelativePosition> control_panel_offset =
261 frc971::RelativePosition::Pack(*builder.fbb(), &control_panel);
262
Alex Perryc4691f52020-02-17 19:20:01 -0800263 // Shooter
264 y2020::control_loops::superstructure::ShooterPositionT shooter;
265 shooter.theta_finisher =
Austin Schuh0ad31d72021-03-06 17:07:04 -0800266 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800267 Values::kFinisherEncoderCountsPerRevolution(),
268 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800269
Alex Perryc4691f52020-02-17 19:20:01 -0800270 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800271 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800272 Values::kAcceleratorEncoderCountsPerRevolution(),
273 Values::kAcceleratorEncoderRatio());
274 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800275 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800276 Values::kAcceleratorEncoderCountsPerRevolution(),
277 Values::kAcceleratorEncoderRatio());
278 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
279 shooter_offset =
280 y2020::control_loops::superstructure::ShooterPosition::Pack(
281 *builder.fbb(), &shooter);
282
Austin Schuh18ca45f2020-02-29 22:58:49 -0800283 superstructure::Position::Builder position_builder =
284 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800285 position_builder.add_hood(hood_offset);
286 position_builder.add_intake_joint(intake_joint_offset);
287 position_builder.add_turret(turret_offset);
288 position_builder.add_shooter(shooter_offset);
289 position_builder.add_control_panel(control_panel_offset);
290
Stephan Massaltd021f972020-01-05 20:41:23 -0800291 builder.Send(position_builder.Finish());
292 }
293
294 {
295 auto builder = auto_mode_sender_.MakeBuilder();
296
297 uint32_t mode = 0;
298 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
299 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
300 mode |= 1 << i;
301 }
302 }
303
304 auto auto_mode_builder =
305 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
306
307 auto_mode_builder.add_mode(mode);
308
309 builder.Send(auto_mode_builder.Finish());
310 }
311 }
312
313 private:
314 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
315 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
316 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
317 drivetrain_position_sender_;
318
Alex Perryc4691f52020-02-17 19:20:01 -0800319 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
320
Ravago Jones937587c2020-12-26 17:21:09 -0800321 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
322
323 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800324
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800325 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
326 left_accelerator_encoder_, right_accelerator_encoder_,
327 control_panel_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800328
Stephan Massaltd021f972020-01-05 20:41:23 -0800329 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800330
331 frc971::wpilib::ADIS16470 *imu_ = nullptr;
Stephan Massaltd021f972020-01-05 20:41:23 -0800332};
333
334class SuperstructureWriter
335 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
336 public:
337 SuperstructureWriter(::aos::EventLoop *event_loop)
338 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
339 event_loop, "/superstructure") {}
340
Alex Perryc4691f52020-02-17 19:20:01 -0800341 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
342 hood_victor_ = ::std::move(t);
343 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800344
Alex Perryc4691f52020-02-17 19:20:01 -0800345 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
346 intake_joint_victor_ = ::std::move(t);
347 }
348
349 void set_intake_roller_falcon(
350 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
351 intake_roller_falcon_ = ::std::move(t);
352 intake_roller_falcon_->ConfigSupplyCurrentLimit(
353 {true, Values::kIntakeRollerSupplyCurrentLimit(),
354 Values::kIntakeRollerSupplyCurrentLimit(), 0});
355 intake_roller_falcon_->ConfigStatorCurrentLimit(
356 {true, Values::kIntakeRollerStatorCurrentLimit(),
357 Values::kIntakeRollerStatorCurrentLimit(), 0});
358 }
359
360 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
361 turret_victor_ = ::std::move(t);
362 }
363
Ravago Jones3dda5602021-03-10 00:33:13 -0800364 void set_feeder_falcon(
365 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800366 feeder_falcon_ = ::std::move(t);
Austin Schuh3399d122021-03-20 14:29:20 -0700367 {
368 auto result = feeder_falcon_->ConfigSupplyCurrentLimit(
369 {true, Values::kFeederSupplyCurrentLimit(),
370 Values::kFeederSupplyCurrentLimit(), 0});
371 if (result != ctre::phoenix::OKAY) {
372 LOG(WARNING) << "Failed to configure feeder supply current limit: "
373 << result;
374 }
375 }
376 {
377 auto result = feeder_falcon_->ConfigStatorCurrentLimit(
378 {true, Values::kFeederStatorCurrentLimit(),
379 Values::kFeederStatorCurrentLimit(), 0});
380 if (result != ctre::phoenix::OKAY) {
381 LOG(WARNING) << "Failed to configure feeder stator current limit: "
382 << result;
383 }
384 }
Alex Perryc4691f52020-02-17 19:20:01 -0800385 }
386
387 void set_washing_machine_control_panel_victor(
388 ::std::unique_ptr<::frc::VictorSP> t) {
389 washing_machine_control_panel_victor_ = ::std::move(t);
390 }
391
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800392 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
393 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800394 }
395
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800396 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
397 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800398 }
399
Austin Schuh0ad31d72021-03-06 17:07:04 -0800400 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
401 finisher_falcon0_ = ::std::move(t);
402 }
403
404 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
405 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800406 }
407
408 void set_climber_falcon(
409 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
410 climber_falcon_ = ::std::move(t);
411 climber_falcon_->ConfigSupplyCurrentLimit(
412 {true, Values::kClimberSupplyCurrentLimit(),
413 Values::kClimberSupplyCurrentLimit(), 0});
414 }
415
416 private:
417 void Write(const superstructure::Output &output) override {
Austin Schuh2efe1682021-03-06 22:47:15 -0800418 hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower,
419 kMaxBringupPower) /
420 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800421
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800422 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
423 -kMaxBringupPower,
424 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800425 12.0);
426
427 intake_roller_falcon_->Set(
428 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800429 std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower,
430 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800431 12.0);
432
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800433 turret_victor_->SetSpeed(std::clamp(-output.turret_voltage(),
434 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800435 12.0);
436
Ravago Jones3dda5602021-03-10 00:33:13 -0800437 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
438 std::clamp(output.feeder_voltage(), -kMaxBringupPower,
439 kMaxBringupPower) /
440 12.0);
Austin Schuh0ad31d72021-03-06 17:07:04 -0800441 if (washing_machine_control_panel_victor_) {
442 washing_machine_control_panel_victor_->SetSpeed(
443 std::clamp(-output.washing_machine_spinner_voltage(),
444 -kMaxBringupPower, kMaxBringupPower) /
445 12.0);
446 }
Alex Perryc4691f52020-02-17 19:20:01 -0800447
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800448 accelerator_left_falcon_->SetSpeed(
449 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
450 kMaxBringupPower) /
451 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800452
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800453 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800454 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800455 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800456 12.0);
457
Austin Schuh0ad31d72021-03-06 17:07:04 -0800458 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
459 -kMaxBringupPower,
460 kMaxBringupPower) /
461 12.0);
462 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
463 -kMaxBringupPower,
464 kMaxBringupPower) /
465 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800466
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800467 if (climber_falcon_) {
468 climber_falcon_->Set(
469 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Tyler Chatow1039e432020-02-28 21:37:50 -0800470 std::clamp(-output.climber_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800471 kMaxBringupPower) /
472 12.0);
473 }
Alex Perryc4691f52020-02-17 19:20:01 -0800474 }
475
476 void Stop() override {
477 AOS_LOG(WARNING, "Superstructure output too old.\n");
478 hood_victor_->SetDisabled();
479 intake_joint_victor_->SetDisabled();
480 turret_victor_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800481 if (washing_machine_control_panel_victor_) {
482 washing_machine_control_panel_victor_->SetDisabled();
483 }
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800484 accelerator_left_falcon_->SetDisabled();
485 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800486 finisher_falcon0_->SetDisabled();
487 finisher_falcon1_->SetDisabled();
Ravago Jones3dda5602021-03-10 00:33:13 -0800488 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
489 intake_roller_falcon_->Set(
490 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
491 climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Alex Perryc4691f52020-02-17 19:20:01 -0800492 }
493
494 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
495 turret_victor_, washing_machine_control_panel_victor_;
496
Ravago Jones3dda5602021-03-10 00:33:13 -0800497 ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800498 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800499
500 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
Ravago Jones3dda5602021-03-10 00:33:13 -0800501 intake_roller_falcon_, climber_falcon_, feeder_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800502};
503
504class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
505 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800506 ::std::unique_ptr<frc::Encoder> make_encoder(
507 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800508 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800509 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800510 }
511
512 void Run() override {
513 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
514 aos::configuration::ReadConfig("config.json");
515
516 // Thread 1.
517 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
518 ::frc971::wpilib::JoystickSender joystick_sender(
519 &joystick_sender_event_loop);
520 AddLoop(&joystick_sender_event_loop);
521
522 // Thread 2.
523 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
524 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
525 AddLoop(&pdp_fetcher_event_loop);
526
527 // Thread 3.
528 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
529 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800530 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800531 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
532 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800533 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800534 sensor_reader.set_hood_encoder(
535 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800536
Ravago Jones937587c2020-12-26 17:21:09 -0800537 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
538 sensor_reader.set_hood_single_turn_absolute_pwm(
539 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800540
Sabina Davisf7afd112020-02-23 13:42:14 -0800541 sensor_reader.set_intake_encoder(make_encoder(5));
542 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800543
Sabina Davisf7afd112020-02-23 13:42:14 -0800544 sensor_reader.set_turret_encoder(make_encoder(2));
545 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
546 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800547
Sabina Davisf7afd112020-02-23 13:42:14 -0800548 sensor_reader.set_finisher_encoder(
549 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
550 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
551 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
552
Austin Schuhed08a5b2020-02-23 22:06:34 -0800553 sensor_reader.set_control_panel_encoder(
554 make_unique<frc::Encoder>(5, 6, false, frc::Encoder::k1X));
Alex Perryc4691f52020-02-17 19:20:01 -0800555
James Kuszmaul022d40e2020-02-11 17:06:18 -0800556 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
557 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
558 // CS0.
Austin Schuh83873c32020-02-22 14:58:39 -0800559 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS2;
560 std::unique_ptr<frc::DigitalInput> imu_trigger;
561 std::unique_ptr<frc::DigitalOutput> imu_reset;
562 if (::aos::network::GetTeamNumber() ==
563 constants::Values::kCodingRobotTeamNumber) {
564 imu_trigger = make_unique<frc::DigitalInput>(26);
565 imu_reset = make_unique<frc::DigitalOutput>(27);
566 spi_port = frc::SPI::Port::kOnboardCS0;
567 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800568 imu_trigger = make_unique<frc::DigitalInput>(9);
569 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800570 }
571 auto spi = make_unique<frc::SPI>(spi_port);
James Kuszmaula244a912020-01-18 13:50:50 -0800572 frc971::wpilib::ADIS16470 imu(&sensor_reader_event_loop, spi.get(),
573 imu_trigger.get(), imu_reset.get());
574 sensor_reader.set_imu(&imu);
Stephan Massaltd021f972020-01-05 20:41:23 -0800575 AddLoop(&sensor_reader_event_loop);
576
577 // Thread 4.
578 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800579 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800580 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
581 drivetrain_writer.set_left_controller0(
582 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
583 drivetrain_writer.set_right_controller0(
584 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
585
586 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800587 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800588 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800589 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800590 superstructure_writer.set_intake_roller_falcon(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800591 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
592 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Ravago Jones3dda5602021-03-10 00:33:13 -0800593 superstructure_writer.set_feeder_falcon(
594 make_unique<ctre::phoenix::motorcontrol::can::TalonFX>(1));
595 superstructure_writer.set_washing_machine_control_panel_victor(
596 make_unique<frc::VictorSP>(6));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800597 superstructure_writer.set_accelerator_left_falcon(
598 make_unique<::frc::TalonFX>(5));
599 superstructure_writer.set_accelerator_right_falcon(
600 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800601 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
602 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800603 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800604 superstructure_writer.set_climber_falcon(
Ravago Jones3dda5602021-03-10 00:33:13 -0800605 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Stephan Massaltd021f972020-01-05 20:41:23 -0800606
607 AddLoop(&output_event_loop);
608
609 RunLoops();
610 }
611};
612
613} // namespace wpilib
614} // namespace y2020
615
616AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);