blob: 4f39acfbbb54e45af442f3edfd0cc660b8b09b6c [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include <inttypes.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include <array>
7#include <chrono>
8#include <cmath>
9#include <functional>
10#include <mutex>
11#include <thread>
12
Stephan Massaltd021f972020-01-05 20:41:23 -080013#include "frc971/wpilib/ahal/AnalogInput.h"
14#include "frc971/wpilib/ahal/Counter.h"
15#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
16#include "frc971/wpilib/ahal/DriverStation.h"
17#include "frc971/wpilib/ahal/Encoder.h"
Alex Perryc4691f52020-02-17 19:20:01 -080018#include "frc971/wpilib/ahal/TalonFX.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080019#include "frc971/wpilib/ahal/VictorSP.h"
20#undef ERROR
21
22#include "aos/commonmath.h"
23#include "aos/events/event_loop.h"
24#include "aos/events/shm_event_loop.h"
25#include "aos/init.h"
26#include "aos/logging/logging.h"
27#include "aos/make_unique.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"
30#include "aos/robot_state/robot_state_generated.h"
31#include "aos/time/time.h"
32#include "aos/util/log_interval.h"
33#include "aos/util/phased_loop.h"
34#include "aos/util/wrapping_counter.h"
Alex Perryc4691f52020-02-17 19:20:01 -080035#include "ctre/phoenix/motorcontrol/can/TalonFX.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 Kuszmaula244a912020-01-18 13:50:50 -080038#include "frc971/wpilib/ADIS16470.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080039#include "frc971/wpilib/buffered_pcm.h"
40#include "frc971/wpilib/buffered_solenoid.h"
41#include "frc971/wpilib/dma.h"
42#include "frc971/wpilib/drivetrain_writer.h"
43#include "frc971/wpilib/encoder_and_potentiometer.h"
44#include "frc971/wpilib/joystick_sender.h"
45#include "frc971/wpilib/logging_generated.h"
46#include "frc971/wpilib/loop_output_handler.h"
47#include "frc971/wpilib/pdp_fetcher.h"
48#include "frc971/wpilib/sensor_reader.h"
49#include "frc971/wpilib/wpilib_robot_base.h"
50#include "y2020/constants.h"
51#include "y2020/control_loops/superstructure/superstructure_output_generated.h"
52#include "y2020/control_loops/superstructure/superstructure_position_generated.h"
53
54using ::aos::monotonic_clock;
55using ::y2020::constants::Values;
56namespace superstructure = ::y2020::control_loops::superstructure;
57namespace chrono = ::std::chrono;
58using aos::make_unique;
59
60namespace y2020 {
61namespace wpilib {
62namespace {
63
64constexpr double kMaxBringupPower = 12.0;
65
66// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
67// DMA stuff and then removing the * 2.0 in *_translate.
68// The low bit is direction.
69
Stephan Massaltd021f972020-01-05 20:41:23 -080070double drivetrain_translate(int32_t in) {
71 return ((static_cast<double>(in) /
72 Values::kDrivetrainEncoderCountsPerRevolution()) *
73 (2.0 * M_PI)) *
74 Values::kDrivetrainEncoderRatio() *
75 control_loops::drivetrain::kWheelRadius;
76}
77
78double drivetrain_velocity_translate(double in) {
79 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
80 (2.0 * M_PI)) *
81 Values::kDrivetrainEncoderRatio() *
82 control_loops::drivetrain::kWheelRadius;
83}
84
Alex Perryc4691f52020-02-17 19:20:01 -080085double turret_pot_translate(double voltage) {
86 return voltage * Values::kTurretPotRatio() *
87 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
88}
89
Stephan Massaltd021f972020-01-05 20:41:23 -080090constexpr double kMaxFastEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -080091 std::max({Values::kMaxControlPanelEncoderPulsesPerSecond(),
92 Values::kMaxFinisherEncoderPulsesPerSecond(),
93 Values::kMaxAcceleratorEncoderPulsesPerSecond()});
94static_assert(kMaxFastEncoderPulsesPerSecond <= 1000000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -080095 "fast encoders are too fast");
Alex Perryc4691f52020-02-17 19:20:01 -080096constexpr double kMaxMediumEncoderPulsesPerSecond =
Austin Schuh9dcd5202020-02-20 20:06:04 -080097 std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(),
98 Values::kMaxHoodEncoderPulsesPerSecond(),
99 Values::kMaxIntakeEncoderPulsesPerSecond(),
100 Values::kMaxTurretEncoderPulsesPerSecond()});
Stephan Massaltd021f972020-01-05 20:41:23 -0800101
Austin Schuh9dcd5202020-02-20 20:06:04 -0800102static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000.0,
Stephan Massaltd021f972020-01-05 20:41:23 -0800103 "medium encoders are too fast");
104
105} // namespace
106
107// Class to send position messages with sensor readings to our loops.
108class SensorReader : public ::frc971::wpilib::SensorReader {
109 public:
110 SensorReader(::aos::ShmEventLoop *event_loop)
111 : ::frc971::wpilib::SensorReader(event_loop),
112 auto_mode_sender_(
113 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
114 "/autonomous")),
115 superstructure_position_sender_(
116 event_loop->MakeSender<superstructure::Position>(
117 "/superstructure")),
118 drivetrain_position_sender_(
119 event_loop
120 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
121 "/drivetrain")) {
122 // Set to filter out anything shorter than 1/4 of the minimum pulse width
123 // we should ever see.
124 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
125 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
126 }
127
Alex Perryc4691f52020-02-17 19:20:01 -0800128 // Hood
Alex Perryc4691f52020-02-17 19:20:01 -0800129 void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) {
130 medium_encoder_filter_.Add(encoder.get());
131 hood_encoder_.set_encoder(::std::move(encoder));
132 }
133
134 void set_hood_absolute_pwm(
135 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
136 hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
137 }
138
139 // Intake
140
141 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
142 medium_encoder_filter_.Add(encoder.get());
143 intake_joint_encoder_.set_encoder(::std::move(encoder));
144 }
145
146 void set_intake_absolute_pwm(
147 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
148 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
149 }
150
151 // Turret
152
153 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
154 medium_encoder_filter_.Add(encoder.get());
155 turret_encoder_.set_encoder(::std::move(encoder));
156 }
157
158 void set_turret_absolute_pwm(
159 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
160 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
161 }
162
163 void set_turret_potentiometer(
164 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
165 turret_encoder_.set_potentiometer(::std::move(potentiometer));
166 }
167
168 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800169 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800170 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800171 finisher_encoder_ = ::std::move(encoder);
172 }
173 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
174 fast_encoder_filter_.Add(encoder.get());
175 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800176 }
177
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800178 void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800179 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800180 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800181 }
182
183 // Control Panel
184
185 void set_control_panel_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800186 fast_encoder_filter_.Add(encoder.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800187 control_panel_encoder_ = ::std::move(encoder);
188 }
189
Stephan Massaltd021f972020-01-05 20:41:23 -0800190 // Auto mode switches.
191 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800192 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800193 autonomous_modes_.at(i) = ::std::move(sensor);
194 }
195
James Kuszmaula244a912020-01-18 13:50:50 -0800196 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
197
Stephan Massaltd021f972020-01-05 20:41:23 -0800198 void RunIteration() override {
James Kuszmaula244a912020-01-18 13:50:50 -0800199 CHECK_NOTNULL(imu_)->DoReads();
200
Stephan Massaltd021f972020-01-05 20:41:23 -0800201 {
202 auto builder = drivetrain_position_sender_.MakeBuilder();
203 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
204 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
205 drivetrain_builder.add_left_encoder(
206 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
207 drivetrain_builder.add_left_speed(
208 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
209
210 drivetrain_builder.add_right_encoder(
211 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
212 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
213 drivetrain_right_encoder_->GetPeriod()));
214
215 builder.Send(drivetrain_builder.Finish());
216 }
Alex Perryc4691f52020-02-17 19:20:01 -0800217 const auto values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800218
219 {
220 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800221
Alex Perryc4691f52020-02-17 19:20:01 -0800222 // TODO(alex): check new absolute encoder api.
223 // Hood
224 frc971::AbsolutePositionT hood;
225 CopyPosition(hood_encoder_, &hood,
226 Values::kHoodEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800227 Values::kHoodEncoderRatio(), true);
Alex Perryc4691f52020-02-17 19:20:01 -0800228 flatbuffers::Offset<frc971::AbsolutePosition> hood_offset =
229 frc971::AbsolutePosition::Pack(*builder.fbb(), &hood);
230
231 // Intake
232 frc971::AbsolutePositionT intake_joint;
233 CopyPosition(intake_joint_encoder_, &intake_joint,
234 Values::kIntakeEncoderCountsPerRevolution(),
235 Values::kIntakeEncoderRatio(), false);
236 flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset =
237 frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint);
238
239 // Turret
240 frc971::PotAndAbsolutePositionT turret;
241 CopyPosition(turret_encoder_, &turret,
242 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800243 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800244 values.turret.potentiometer_offset);
245 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
246 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
247
Austin Schuh18ca45f2020-02-29 22:58:49 -0800248 // Control Panel
249 frc971::RelativePositionT control_panel;
250 CopyPosition(*control_panel_encoder_, &control_panel,
251 Values::kControlPanelEncoderCountsPerRevolution(),
252 Values::kControlPanelEncoderRatio(), false);
253 flatbuffers::Offset<frc971::RelativePosition> control_panel_offset =
254 frc971::RelativePosition::Pack(*builder.fbb(), &control_panel);
255
Alex Perryc4691f52020-02-17 19:20:01 -0800256 // Shooter
257 y2020::control_loops::superstructure::ShooterPositionT shooter;
258 shooter.theta_finisher =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800259 encoder_translate(finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800260 Values::kFinisherEncoderCountsPerRevolution(),
261 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800262
Alex Perryc4691f52020-02-17 19:20:01 -0800263 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800264 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800265 Values::kAcceleratorEncoderCountsPerRevolution(),
266 Values::kAcceleratorEncoderRatio());
267 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800268 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800269 Values::kAcceleratorEncoderCountsPerRevolution(),
270 Values::kAcceleratorEncoderRatio());
271 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
272 shooter_offset =
273 y2020::control_loops::superstructure::ShooterPosition::Pack(
274 *builder.fbb(), &shooter);
275
Austin Schuh18ca45f2020-02-29 22:58:49 -0800276 superstructure::Position::Builder position_builder =
277 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800278 position_builder.add_hood(hood_offset);
279 position_builder.add_intake_joint(intake_joint_offset);
280 position_builder.add_turret(turret_offset);
281 position_builder.add_shooter(shooter_offset);
282 position_builder.add_control_panel(control_panel_offset);
283
Stephan Massaltd021f972020-01-05 20:41:23 -0800284 builder.Send(position_builder.Finish());
285 }
286
287 {
288 auto builder = auto_mode_sender_.MakeBuilder();
289
290 uint32_t mode = 0;
291 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
292 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
293 mode |= 1 << i;
294 }
295 }
296
297 auto auto_mode_builder =
298 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
299
300 auto_mode_builder.add_mode(mode);
301
302 builder.Send(auto_mode_builder.Finish());
303 }
304 }
305
306 private:
307 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
308 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
309 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
310 drivetrain_position_sender_;
311
Alex Perryc4691f52020-02-17 19:20:01 -0800312 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
313
314 ::frc971::wpilib::AbsoluteEncoder hood_encoder_, intake_joint_encoder_;
315
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800316 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
317 left_accelerator_encoder_, right_accelerator_encoder_,
318 control_panel_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800319
Stephan Massaltd021f972020-01-05 20:41:23 -0800320 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800321
322 frc971::wpilib::ADIS16470 *imu_ = nullptr;
Stephan Massaltd021f972020-01-05 20:41:23 -0800323};
324
325class SuperstructureWriter
326 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
327 public:
328 SuperstructureWriter(::aos::EventLoop *event_loop)
329 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
330 event_loop, "/superstructure") {}
331
Alex Perryc4691f52020-02-17 19:20:01 -0800332 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
333 hood_victor_ = ::std::move(t);
334 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800335
Alex Perryc4691f52020-02-17 19:20:01 -0800336 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
337 intake_joint_victor_ = ::std::move(t);
338 }
339
340 void set_intake_roller_falcon(
341 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
342 intake_roller_falcon_ = ::std::move(t);
343 intake_roller_falcon_->ConfigSupplyCurrentLimit(
344 {true, Values::kIntakeRollerSupplyCurrentLimit(),
345 Values::kIntakeRollerSupplyCurrentLimit(), 0});
346 intake_roller_falcon_->ConfigStatorCurrentLimit(
347 {true, Values::kIntakeRollerStatorCurrentLimit(),
348 Values::kIntakeRollerStatorCurrentLimit(), 0});
349 }
350
351 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
352 turret_victor_ = ::std::move(t);
353 }
354
355 void set_feeder_falcon(::std::unique_ptr<::frc::TalonFX> t) {
356 feeder_falcon_ = ::std::move(t);
357 }
358
359 void set_washing_machine_control_panel_victor(
360 ::std::unique_ptr<::frc::VictorSP> t) {
361 washing_machine_control_panel_victor_ = ::std::move(t);
362 }
363
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800364 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
365 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800366 }
367
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800368 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
369 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800370 }
371
372 void set_flywheel_falcon(::std::unique_ptr<::frc::TalonFX> t) {
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800373 finisher_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800374 }
375
376 void set_climber_falcon(
377 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
378 climber_falcon_ = ::std::move(t);
379 climber_falcon_->ConfigSupplyCurrentLimit(
380 {true, Values::kClimberSupplyCurrentLimit(),
381 Values::kClimberSupplyCurrentLimit(), 0});
382 }
383
384 private:
385 void Write(const superstructure::Output &output) override {
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800386 hood_victor_->SetSpeed(
387 std::clamp(output.hood_voltage(), -kMaxBringupPower, kMaxBringupPower) /
388 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800389
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800390 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
391 -kMaxBringupPower,
392 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800393 12.0);
394
395 intake_roller_falcon_->Set(
396 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800397 std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower,
398 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800399 12.0);
400
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800401 turret_victor_->SetSpeed(std::clamp(-output.turret_voltage(),
402 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800403 12.0);
404
405 feeder_falcon_->SetSpeed(std::clamp(output.feeder_voltage(),
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800406 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800407 12.0);
408
409 washing_machine_control_panel_victor_->SetSpeed(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800410 std::clamp(-output.washing_machine_spinner_voltage(), -kMaxBringupPower,
411 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800412 12.0);
413
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800414 accelerator_left_falcon_->SetSpeed(
415 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
416 kMaxBringupPower) /
417 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800418
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800419 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800420 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800421 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800422 12.0);
423
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800424 finisher_falcon_->SetSpeed(std::clamp(output.finisher_voltage(),
425 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800426 12.0);
427
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800428 if (climber_falcon_) {
429 climber_falcon_->Set(
430 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Tyler Chatow1039e432020-02-28 21:37:50 -0800431 std::clamp(-output.climber_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800432 kMaxBringupPower) /
433 12.0);
434 }
Alex Perryc4691f52020-02-17 19:20:01 -0800435 }
436
437 void Stop() override {
438 AOS_LOG(WARNING, "Superstructure output too old.\n");
439 hood_victor_->SetDisabled();
440 intake_joint_victor_->SetDisabled();
441 turret_victor_->SetDisabled();
442 feeder_falcon_->SetDisabled();
443 washing_machine_control_panel_victor_->SetDisabled();
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800444 accelerator_left_falcon_->SetDisabled();
445 accelerator_right_falcon_->SetDisabled();
446 finisher_falcon_->SetDisabled();
Alex Perryc4691f52020-02-17 19:20:01 -0800447 }
448
449 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
450 turret_victor_, washing_machine_control_panel_victor_;
451
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800452 ::std::unique_ptr<::frc::TalonFX> feeder_falcon_, accelerator_left_falcon_,
453 accelerator_right_falcon_, finisher_falcon_;
Alex Perryc4691f52020-02-17 19:20:01 -0800454
455 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
456 intake_roller_falcon_, climber_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800457};
458
459class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
460 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800461 ::std::unique_ptr<frc::Encoder> make_encoder(
462 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800463 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800464 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800465 }
466
467 void Run() override {
468 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
469 aos::configuration::ReadConfig("config.json");
470
471 // Thread 1.
472 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
473 ::frc971::wpilib::JoystickSender joystick_sender(
474 &joystick_sender_event_loop);
475 AddLoop(&joystick_sender_event_loop);
476
477 // Thread 2.
478 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
479 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
480 AddLoop(&pdp_fetcher_event_loop);
481
482 // Thread 3.
483 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
484 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800485 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800486 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
487 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800488 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800489 sensor_reader.set_hood_encoder(
490 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800491
Sabina Davisf7afd112020-02-23 13:42:14 -0800492 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800493
Sabina Davisf7afd112020-02-23 13:42:14 -0800494 sensor_reader.set_intake_encoder(make_encoder(5));
495 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800496
Sabina Davisf7afd112020-02-23 13:42:14 -0800497 sensor_reader.set_turret_encoder(make_encoder(2));
498 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
499 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800500
Sabina Davisf7afd112020-02-23 13:42:14 -0800501 sensor_reader.set_finisher_encoder(
502 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
503 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
504 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
505
Austin Schuhed08a5b2020-02-23 22:06:34 -0800506 sensor_reader.set_control_panel_encoder(
507 make_unique<frc::Encoder>(5, 6, false, frc::Encoder::k1X));
Alex Perryc4691f52020-02-17 19:20:01 -0800508
James Kuszmaul022d40e2020-02-11 17:06:18 -0800509 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
510 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
511 // CS0.
Austin Schuh83873c32020-02-22 14:58:39 -0800512 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS2;
513 std::unique_ptr<frc::DigitalInput> imu_trigger;
514 std::unique_ptr<frc::DigitalOutput> imu_reset;
515 if (::aos::network::GetTeamNumber() ==
516 constants::Values::kCodingRobotTeamNumber) {
517 imu_trigger = make_unique<frc::DigitalInput>(26);
518 imu_reset = make_unique<frc::DigitalOutput>(27);
519 spi_port = frc::SPI::Port::kOnboardCS0;
520 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800521 imu_trigger = make_unique<frc::DigitalInput>(9);
522 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800523 }
524 auto spi = make_unique<frc::SPI>(spi_port);
James Kuszmaula244a912020-01-18 13:50:50 -0800525 frc971::wpilib::ADIS16470 imu(&sensor_reader_event_loop, spi.get(),
526 imu_trigger.get(), imu_reset.get());
527 sensor_reader.set_imu(&imu);
Stephan Massaltd021f972020-01-05 20:41:23 -0800528 AddLoop(&sensor_reader_event_loop);
529
530 // Thread 4.
531 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800532 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800533 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
534 drivetrain_writer.set_left_controller0(
535 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
536 drivetrain_writer.set_right_controller0(
537 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
538
539 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800540 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800541 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800542 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800543 superstructure_writer.set_intake_roller_falcon(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800544 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
545 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Alex Perryc4691f52020-02-17 19:20:01 -0800546 superstructure_writer.set_feeder_falcon(make_unique<frc::TalonFX>(6));
547 superstructure_writer.set_washing_machine_control_panel_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800548 make_unique<frc::VictorSP>(3));
549 superstructure_writer.set_accelerator_left_falcon(
550 make_unique<::frc::TalonFX>(5));
551 superstructure_writer.set_accelerator_right_falcon(
552 make_unique<::frc::TalonFX>(4));
553 superstructure_writer.set_flywheel_falcon(make_unique<::frc::TalonFX>(9));
554 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800555 superstructure_writer.set_climber_falcon(
556 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(1));
Stephan Massaltd021f972020-01-05 20:41:23 -0800557
558 AddLoop(&output_event_loop);
559
560 RunLoops();
561 }
562};
563
564} // namespace wpilib
565} // namespace y2020
566
567AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);