blob: cc1e1b8fea8a0204392e37c32f895d090900a411 [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
Ravago Jones937587c2020-12-26 17:21:09 -0800139 void set_hood_single_turn_absolute_pwm(
140 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
141 hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm));
142 }
143
Alex Perryc4691f52020-02-17 19:20:01 -0800144 // Intake
145
146 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
147 medium_encoder_filter_.Add(encoder.get());
148 intake_joint_encoder_.set_encoder(::std::move(encoder));
149 }
150
151 void set_intake_absolute_pwm(
152 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
153 intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
154 }
155
156 // Turret
157
158 void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) {
159 medium_encoder_filter_.Add(encoder.get());
160 turret_encoder_.set_encoder(::std::move(encoder));
161 }
162
163 void set_turret_absolute_pwm(
164 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
165 turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
166 }
167
168 void set_turret_potentiometer(
169 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
170 turret_encoder_.set_potentiometer(::std::move(potentiometer));
171 }
172
173 // Shooter
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800174 void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800175 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800176 finisher_encoder_ = ::std::move(encoder);
177 }
178 void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
179 fast_encoder_filter_.Add(encoder.get());
180 left_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800181 }
182
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800183 void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Alex Perryc4691f52020-02-17 19:20:01 -0800184 fast_encoder_filter_.Add(encoder.get());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800185 right_accelerator_encoder_ = ::std::move(encoder);
Alex Perryc4691f52020-02-17 19:20:01 -0800186 }
187
188 // Control Panel
189
190 void set_control_panel_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800191 fast_encoder_filter_.Add(encoder.get());
Alex Perryc4691f52020-02-17 19:20:01 -0800192 control_panel_encoder_ = ::std::move(encoder);
193 }
194
Stephan Massaltd021f972020-01-05 20:41:23 -0800195 // Auto mode switches.
196 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800197 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800198 autonomous_modes_.at(i) = ::std::move(sensor);
199 }
200
James Kuszmaula244a912020-01-18 13:50:50 -0800201 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
202
Stephan Massaltd021f972020-01-05 20:41:23 -0800203 void RunIteration() override {
James Kuszmaula244a912020-01-18 13:50:50 -0800204 CHECK_NOTNULL(imu_)->DoReads();
205
Stephan Massaltd021f972020-01-05 20:41:23 -0800206 {
207 auto builder = drivetrain_position_sender_.MakeBuilder();
208 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
209 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
210 drivetrain_builder.add_left_encoder(
211 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
212 drivetrain_builder.add_left_speed(
213 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
214
215 drivetrain_builder.add_right_encoder(
216 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
217 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
218 drivetrain_right_encoder_->GetPeriod()));
219
220 builder.Send(drivetrain_builder.Finish());
221 }
Alex Perryc4691f52020-02-17 19:20:01 -0800222 const auto values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800223
224 {
225 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800226
Alex Perryc4691f52020-02-17 19:20:01 -0800227 // TODO(alex): check new absolute encoder api.
228 // Hood
Ravago Jones937587c2020-12-26 17:21:09 -0800229 frc971::AbsoluteAndAbsolutePositionT hood;
Alex Perryc4691f52020-02-17 19:20:01 -0800230 CopyPosition(hood_encoder_, &hood,
231 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800232 Values::kHoodEncoderRatio(),
233 Values::kHoodSingleTurnEncoderRatio(), false);
234 flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset =
235 frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood);
Alex Perryc4691f52020-02-17 19:20:01 -0800236
237 // Intake
238 frc971::AbsolutePositionT intake_joint;
239 CopyPosition(intake_joint_encoder_, &intake_joint,
240 Values::kIntakeEncoderCountsPerRevolution(),
241 Values::kIntakeEncoderRatio(), false);
242 flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset =
243 frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint);
244
245 // Turret
246 frc971::PotAndAbsolutePositionT turret;
247 CopyPosition(turret_encoder_, &turret,
248 Values::kTurretEncoderCountsPerRevolution(),
Sabina Davisf7afd112020-02-23 13:42:14 -0800249 Values::kTurretEncoderRatio(), turret_pot_translate, true,
Alex Perryc4691f52020-02-17 19:20:01 -0800250 values.turret.potentiometer_offset);
251 flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset =
252 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret);
253
Austin Schuh18ca45f2020-02-29 22:58:49 -0800254 // Control Panel
255 frc971::RelativePositionT control_panel;
256 CopyPosition(*control_panel_encoder_, &control_panel,
257 Values::kControlPanelEncoderCountsPerRevolution(),
258 Values::kControlPanelEncoderRatio(), false);
259 flatbuffers::Offset<frc971::RelativePosition> control_panel_offset =
260 frc971::RelativePosition::Pack(*builder.fbb(), &control_panel);
261
Alex Perryc4691f52020-02-17 19:20:01 -0800262 // Shooter
263 y2020::control_loops::superstructure::ShooterPositionT shooter;
264 shooter.theta_finisher =
Austin Schuh0ad31d72021-03-06 17:07:04 -0800265 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800266 Values::kFinisherEncoderCountsPerRevolution(),
267 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800268
Alex Perryc4691f52020-02-17 19:20:01 -0800269 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800270 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800271 Values::kAcceleratorEncoderCountsPerRevolution(),
272 Values::kAcceleratorEncoderRatio());
273 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800274 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800275 Values::kAcceleratorEncoderCountsPerRevolution(),
276 Values::kAcceleratorEncoderRatio());
277 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
278 shooter_offset =
279 y2020::control_loops::superstructure::ShooterPosition::Pack(
280 *builder.fbb(), &shooter);
281
Austin Schuh18ca45f2020-02-29 22:58:49 -0800282 superstructure::Position::Builder position_builder =
283 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800284 position_builder.add_hood(hood_offset);
285 position_builder.add_intake_joint(intake_joint_offset);
286 position_builder.add_turret(turret_offset);
287 position_builder.add_shooter(shooter_offset);
288 position_builder.add_control_panel(control_panel_offset);
289
Stephan Massaltd021f972020-01-05 20:41:23 -0800290 builder.Send(position_builder.Finish());
291 }
292
293 {
294 auto builder = auto_mode_sender_.MakeBuilder();
295
296 uint32_t mode = 0;
297 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
298 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
299 mode |= 1 << i;
300 }
301 }
302
303 auto auto_mode_builder =
304 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
305
306 auto_mode_builder.add_mode(mode);
307
308 builder.Send(auto_mode_builder.Finish());
309 }
310 }
311
312 private:
313 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
314 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
315 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
316 drivetrain_position_sender_;
317
Alex Perryc4691f52020-02-17 19:20:01 -0800318 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
319
Ravago Jones937587c2020-12-26 17:21:09 -0800320 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
321
322 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800323
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800324 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
325 left_accelerator_encoder_, right_accelerator_encoder_,
326 control_panel_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800327
Stephan Massaltd021f972020-01-05 20:41:23 -0800328 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800329
330 frc971::wpilib::ADIS16470 *imu_ = nullptr;
Stephan Massaltd021f972020-01-05 20:41:23 -0800331};
332
333class SuperstructureWriter
334 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
335 public:
336 SuperstructureWriter(::aos::EventLoop *event_loop)
337 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
338 event_loop, "/superstructure") {}
339
Alex Perryc4691f52020-02-17 19:20:01 -0800340 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
341 hood_victor_ = ::std::move(t);
342 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800343
Alex Perryc4691f52020-02-17 19:20:01 -0800344 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
345 intake_joint_victor_ = ::std::move(t);
346 }
347
348 void set_intake_roller_falcon(
349 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
350 intake_roller_falcon_ = ::std::move(t);
351 intake_roller_falcon_->ConfigSupplyCurrentLimit(
352 {true, Values::kIntakeRollerSupplyCurrentLimit(),
353 Values::kIntakeRollerSupplyCurrentLimit(), 0});
354 intake_roller_falcon_->ConfigStatorCurrentLimit(
355 {true, Values::kIntakeRollerStatorCurrentLimit(),
356 Values::kIntakeRollerStatorCurrentLimit(), 0});
357 }
358
359 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
360 turret_victor_ = ::std::move(t);
361 }
362
363 void set_feeder_falcon(::std::unique_ptr<::frc::TalonFX> t) {
364 feeder_falcon_ = ::std::move(t);
365 }
366
367 void set_washing_machine_control_panel_victor(
368 ::std::unique_ptr<::frc::VictorSP> t) {
369 washing_machine_control_panel_victor_ = ::std::move(t);
370 }
371
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800372 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
373 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800374 }
375
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800376 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
377 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800378 }
379
Austin Schuh0ad31d72021-03-06 17:07:04 -0800380 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
381 finisher_falcon0_ = ::std::move(t);
382 }
383
384 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
385 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800386 }
387
388 void set_climber_falcon(
389 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
390 climber_falcon_ = ::std::move(t);
391 climber_falcon_->ConfigSupplyCurrentLimit(
392 {true, Values::kClimberSupplyCurrentLimit(),
393 Values::kClimberSupplyCurrentLimit(), 0});
394 }
395
396 private:
397 void Write(const superstructure::Output &output) override {
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800398 hood_victor_->SetSpeed(
399 std::clamp(output.hood_voltage(), -kMaxBringupPower, kMaxBringupPower) /
400 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800401
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800402 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
403 -kMaxBringupPower,
404 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800405 12.0);
406
407 intake_roller_falcon_->Set(
408 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800409 std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower,
410 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800411 12.0);
412
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800413 turret_victor_->SetSpeed(std::clamp(-output.turret_voltage(),
414 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800415 12.0);
416
417 feeder_falcon_->SetSpeed(std::clamp(output.feeder_voltage(),
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800418 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800419 12.0);
420
Austin Schuh0ad31d72021-03-06 17:07:04 -0800421 if (washing_machine_control_panel_victor_) {
422 washing_machine_control_panel_victor_->SetSpeed(
423 std::clamp(-output.washing_machine_spinner_voltage(),
424 -kMaxBringupPower, kMaxBringupPower) /
425 12.0);
426 }
Alex Perryc4691f52020-02-17 19:20:01 -0800427
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800428 accelerator_left_falcon_->SetSpeed(
429 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
430 kMaxBringupPower) /
431 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800432
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800433 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800434 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800435 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800436 12.0);
437
Austin Schuh0ad31d72021-03-06 17:07:04 -0800438 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
439 -kMaxBringupPower,
440 kMaxBringupPower) /
441 12.0);
442 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
443 -kMaxBringupPower,
444 kMaxBringupPower) /
445 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800446
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800447 if (climber_falcon_) {
448 climber_falcon_->Set(
449 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Tyler Chatow1039e432020-02-28 21:37:50 -0800450 std::clamp(-output.climber_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800451 kMaxBringupPower) /
452 12.0);
453 }
Alex Perryc4691f52020-02-17 19:20:01 -0800454 }
455
456 void Stop() override {
457 AOS_LOG(WARNING, "Superstructure output too old.\n");
458 hood_victor_->SetDisabled();
459 intake_joint_victor_->SetDisabled();
460 turret_victor_->SetDisabled();
461 feeder_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800462 if (washing_machine_control_panel_victor_) {
463 washing_machine_control_panel_victor_->SetDisabled();
464 }
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800465 accelerator_left_falcon_->SetDisabled();
466 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800467 finisher_falcon0_->SetDisabled();
468 finisher_falcon1_->SetDisabled();
Alex Perryc4691f52020-02-17 19:20:01 -0800469 }
470
471 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
472 turret_victor_, washing_machine_control_panel_victor_;
473
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800474 ::std::unique_ptr<::frc::TalonFX> feeder_falcon_, accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800475 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800476
477 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
478 intake_roller_falcon_, climber_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800479};
480
481class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
482 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800483 ::std::unique_ptr<frc::Encoder> make_encoder(
484 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800485 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800486 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800487 }
488
489 void Run() override {
490 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
491 aos::configuration::ReadConfig("config.json");
492
493 // Thread 1.
494 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
495 ::frc971::wpilib::JoystickSender joystick_sender(
496 &joystick_sender_event_loop);
497 AddLoop(&joystick_sender_event_loop);
498
499 // Thread 2.
500 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
501 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
502 AddLoop(&pdp_fetcher_event_loop);
503
504 // Thread 3.
505 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
506 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800507 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800508 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
509 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800510 // TODO: pin numbers
Ravago Jones937587c2020-12-26 17:21:09 -0800511 // TODO(Ravago): Hood pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800512 sensor_reader.set_hood_encoder(
513 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800514
Ravago Jones937587c2020-12-26 17:21:09 -0800515 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
516 sensor_reader.set_hood_single_turn_absolute_pwm(
517 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800518
Sabina Davisf7afd112020-02-23 13:42:14 -0800519 sensor_reader.set_intake_encoder(make_encoder(5));
520 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800521
Sabina Davisf7afd112020-02-23 13:42:14 -0800522 sensor_reader.set_turret_encoder(make_encoder(2));
523 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
524 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800525
Sabina Davisf7afd112020-02-23 13:42:14 -0800526 sensor_reader.set_finisher_encoder(
527 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
528 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
529 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
530
Austin Schuhed08a5b2020-02-23 22:06:34 -0800531 sensor_reader.set_control_panel_encoder(
532 make_unique<frc::Encoder>(5, 6, false, frc::Encoder::k1X));
Alex Perryc4691f52020-02-17 19:20:01 -0800533
James Kuszmaul022d40e2020-02-11 17:06:18 -0800534 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
535 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
536 // CS0.
Austin Schuh83873c32020-02-22 14:58:39 -0800537 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS2;
538 std::unique_ptr<frc::DigitalInput> imu_trigger;
539 std::unique_ptr<frc::DigitalOutput> imu_reset;
540 if (::aos::network::GetTeamNumber() ==
541 constants::Values::kCodingRobotTeamNumber) {
542 imu_trigger = make_unique<frc::DigitalInput>(26);
543 imu_reset = make_unique<frc::DigitalOutput>(27);
544 spi_port = frc::SPI::Port::kOnboardCS0;
545 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800546 imu_trigger = make_unique<frc::DigitalInput>(9);
547 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800548 }
549 auto spi = make_unique<frc::SPI>(spi_port);
James Kuszmaula244a912020-01-18 13:50:50 -0800550 frc971::wpilib::ADIS16470 imu(&sensor_reader_event_loop, spi.get(),
551 imu_trigger.get(), imu_reset.get());
552 sensor_reader.set_imu(&imu);
Stephan Massaltd021f972020-01-05 20:41:23 -0800553 AddLoop(&sensor_reader_event_loop);
554
555 // Thread 4.
556 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800557 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800558 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
559 drivetrain_writer.set_left_controller0(
560 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
561 drivetrain_writer.set_right_controller0(
562 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
563
564 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800565 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800566 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800567 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800568 superstructure_writer.set_intake_roller_falcon(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800569 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
570 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Alex Perryc4691f52020-02-17 19:20:01 -0800571 superstructure_writer.set_feeder_falcon(make_unique<frc::TalonFX>(6));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800572 // TODO(austin): When this goes over to CAN, update it and make it work.
573 //superstructure_writer.set_washing_machine_control_panel_victor(
574 //make_unique<frc::VictorSP>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800575 superstructure_writer.set_accelerator_left_falcon(
576 make_unique<::frc::TalonFX>(5));
577 superstructure_writer.set_accelerator_right_falcon(
578 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800579 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
580 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800581 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800582 superstructure_writer.set_climber_falcon(
583 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(1));
Stephan Massaltd021f972020-01-05 20:41:23 -0800584
585 AddLoop(&output_event_loop);
586
587 RunLoops();
588 }
589};
590
591} // namespace wpilib
592} // namespace y2020
593
594AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);