blob: b7cf7e15e847b6ecd08b550b54431aae660e9070 [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
Stephan Massaltd021f972020-01-05 20:41:23 -0800189 // Auto mode switches.
190 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
Austin Schuh9dcd5202020-02-20 20:06:04 -0800191 medium_encoder_filter_.Add(sensor.get());
Stephan Massaltd021f972020-01-05 20:41:23 -0800192 autonomous_modes_.at(i) = ::std::move(sensor);
193 }
194
James Kuszmaula244a912020-01-18 13:50:50 -0800195 void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; }
196
Stephan Massaltd021f972020-01-05 20:41:23 -0800197 void RunIteration() override {
James Kuszmaula244a912020-01-18 13:50:50 -0800198 CHECK_NOTNULL(imu_)->DoReads();
199
Stephan Massaltd021f972020-01-05 20:41:23 -0800200 {
201 auto builder = drivetrain_position_sender_.MakeBuilder();
202 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
203 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
204 drivetrain_builder.add_left_encoder(
205 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
206 drivetrain_builder.add_left_speed(
207 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
208
209 drivetrain_builder.add_right_encoder(
210 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
211 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
212 drivetrain_right_encoder_->GetPeriod()));
213
214 builder.Send(drivetrain_builder.Finish());
215 }
Alex Perryc4691f52020-02-17 19:20:01 -0800216 const auto values = constants::GetValues();
Stephan Massaltd021f972020-01-05 20:41:23 -0800217
218 {
219 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh18ca45f2020-02-29 22:58:49 -0800220
Alex Perryc4691f52020-02-17 19:20:01 -0800221 // TODO(alex): check new absolute encoder api.
222 // Hood
Ravago Jones937587c2020-12-26 17:21:09 -0800223 frc971::AbsoluteAndAbsolutePositionT hood;
Alex Perryc4691f52020-02-17 19:20:01 -0800224 CopyPosition(hood_encoder_, &hood,
225 Values::kHoodEncoderCountsPerRevolution(),
Ravago Jones937587c2020-12-26 17:21:09 -0800226 Values::kHoodEncoderRatio(),
227 Values::kHoodSingleTurnEncoderRatio(), false);
228 flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset =
229 frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood);
Alex Perryc4691f52020-02-17 19:20:01 -0800230
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
248 // Shooter
249 y2020::control_loops::superstructure::ShooterPositionT shooter;
250 shooter.theta_finisher =
Austin Schuh0ad31d72021-03-06 17:07:04 -0800251 encoder_translate(-finisher_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800252 Values::kFinisherEncoderCountsPerRevolution(),
253 Values::kFinisherEncoderRatio());
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800254
Alex Perryc4691f52020-02-17 19:20:01 -0800255 shooter.theta_accelerator_left =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800256 encoder_translate(-left_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800257 Values::kAcceleratorEncoderCountsPerRevolution(),
258 Values::kAcceleratorEncoderRatio());
259 shooter.theta_accelerator_right =
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800260 encoder_translate(right_accelerator_encoder_->GetRaw(),
Alex Perryc4691f52020-02-17 19:20:01 -0800261 Values::kAcceleratorEncoderCountsPerRevolution(),
262 Values::kAcceleratorEncoderRatio());
263 flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition>
264 shooter_offset =
265 y2020::control_loops::superstructure::ShooterPosition::Pack(
266 *builder.fbb(), &shooter);
267
Austin Schuh18ca45f2020-02-29 22:58:49 -0800268 superstructure::Position::Builder position_builder =
269 builder.MakeBuilder<superstructure::Position>();
Alex Perryc4691f52020-02-17 19:20:01 -0800270 position_builder.add_hood(hood_offset);
271 position_builder.add_intake_joint(intake_joint_offset);
272 position_builder.add_turret(turret_offset);
273 position_builder.add_shooter(shooter_offset);
Alex Perryc4691f52020-02-17 19:20:01 -0800274
Stephan Massaltd021f972020-01-05 20:41:23 -0800275 builder.Send(position_builder.Finish());
276 }
277
278 {
279 auto builder = auto_mode_sender_.MakeBuilder();
280
281 uint32_t mode = 0;
282 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
283 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
284 mode |= 1 << i;
285 }
286 }
287
288 auto auto_mode_builder =
289 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
290
291 auto_mode_builder.add_mode(mode);
292
293 builder.Send(auto_mode_builder.Finish());
294 }
295 }
296
297 private:
298 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
299 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
300 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
301 drivetrain_position_sender_;
302
Alex Perryc4691f52020-02-17 19:20:01 -0800303 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_;
304
Ravago Jones937587c2020-12-26 17:21:09 -0800305 ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_;
306
307 ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_;
Alex Perryc4691f52020-02-17 19:20:01 -0800308
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800309 ::std::unique_ptr<::frc::Encoder> finisher_encoder_,
milind-u3d68aa72021-09-26 12:19:03 -0700310 left_accelerator_encoder_, right_accelerator_encoder_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800311 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
James Kuszmaula244a912020-01-18 13:50:50 -0800312
313 frc971::wpilib::ADIS16470 *imu_ = nullptr;
Stephan Massaltd021f972020-01-05 20:41:23 -0800314};
315
316class SuperstructureWriter
317 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
318 public:
319 SuperstructureWriter(::aos::EventLoop *event_loop)
320 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
321 event_loop, "/superstructure") {}
322
Alex Perryc4691f52020-02-17 19:20:01 -0800323 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
324 hood_victor_ = ::std::move(t);
325 }
Stephan Massaltd021f972020-01-05 20:41:23 -0800326
Alex Perryc4691f52020-02-17 19:20:01 -0800327 void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
328 intake_joint_victor_ = ::std::move(t);
329 }
330
331 void set_intake_roller_falcon(
332 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
333 intake_roller_falcon_ = ::std::move(t);
334 intake_roller_falcon_->ConfigSupplyCurrentLimit(
335 {true, Values::kIntakeRollerSupplyCurrentLimit(),
336 Values::kIntakeRollerSupplyCurrentLimit(), 0});
337 intake_roller_falcon_->ConfigStatorCurrentLimit(
338 {true, Values::kIntakeRollerStatorCurrentLimit(),
339 Values::kIntakeRollerStatorCurrentLimit(), 0});
340 }
341
342 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
343 turret_victor_ = ::std::move(t);
344 }
345
Ravago Jones3dda5602021-03-10 00:33:13 -0800346 void set_feeder_falcon(
347 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
Alex Perryc4691f52020-02-17 19:20:01 -0800348 feeder_falcon_ = ::std::move(t);
Austin Schuh3399d122021-03-20 14:29:20 -0700349 {
350 auto result = feeder_falcon_->ConfigSupplyCurrentLimit(
351 {true, Values::kFeederSupplyCurrentLimit(),
352 Values::kFeederSupplyCurrentLimit(), 0});
353 if (result != ctre::phoenix::OKAY) {
354 LOG(WARNING) << "Failed to configure feeder supply current limit: "
355 << result;
356 }
357 }
358 {
359 auto result = feeder_falcon_->ConfigStatorCurrentLimit(
360 {true, Values::kFeederStatorCurrentLimit(),
361 Values::kFeederStatorCurrentLimit(), 0});
362 if (result != ctre::phoenix::OKAY) {
363 LOG(WARNING) << "Failed to configure feeder stator current limit: "
364 << result;
365 }
366 }
Alex Perryc4691f52020-02-17 19:20:01 -0800367 }
368
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800369 void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) {
370 accelerator_left_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800371 }
372
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800373 void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) {
374 accelerator_right_falcon_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800375 }
376
Austin Schuh0ad31d72021-03-06 17:07:04 -0800377 void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) {
378 finisher_falcon0_ = ::std::move(t);
379 }
380
381 void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) {
382 finisher_falcon1_ = ::std::move(t);
Alex Perryc4691f52020-02-17 19:20:01 -0800383 }
384
385 void set_climber_falcon(
386 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
387 climber_falcon_ = ::std::move(t);
388 climber_falcon_->ConfigSupplyCurrentLimit(
389 {true, Values::kClimberSupplyCurrentLimit(),
390 Values::kClimberSupplyCurrentLimit(), 0});
391 }
392
393 private:
394 void Write(const superstructure::Output &output) override {
Austin Schuh2efe1682021-03-06 22:47:15 -0800395 hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower,
396 kMaxBringupPower) /
397 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800398
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800399 intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(),
400 -kMaxBringupPower,
401 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800402 12.0);
403
404 intake_roller_falcon_->Set(
405 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800406 std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower,
407 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800408 12.0);
409
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800410 turret_victor_->SetSpeed(std::clamp(-output.turret_voltage(),
411 -kMaxBringupPower, kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800412 12.0);
413
Ravago Jones3dda5602021-03-10 00:33:13 -0800414 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
415 std::clamp(output.feeder_voltage(), -kMaxBringupPower,
416 kMaxBringupPower) /
417 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800418
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800419 accelerator_left_falcon_->SetSpeed(
420 std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower,
421 kMaxBringupPower) /
422 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800423
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800424 accelerator_right_falcon_->SetSpeed(
Alex Perryc4691f52020-02-17 19:20:01 -0800425 std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800426 kMaxBringupPower) /
Alex Perryc4691f52020-02-17 19:20:01 -0800427 12.0);
428
Austin Schuh0ad31d72021-03-06 17:07:04 -0800429 finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(),
430 -kMaxBringupPower,
431 kMaxBringupPower) /
432 12.0);
433 finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(),
434 -kMaxBringupPower,
435 kMaxBringupPower) /
436 12.0);
Alex Perryc4691f52020-02-17 19:20:01 -0800437
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800438 if (climber_falcon_) {
439 climber_falcon_->Set(
440 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Tyler Chatow1039e432020-02-28 21:37:50 -0800441 std::clamp(-output.climber_voltage(), -kMaxBringupPower,
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800442 kMaxBringupPower) /
443 12.0);
444 }
Alex Perryc4691f52020-02-17 19:20:01 -0800445 }
446
447 void Stop() override {
448 AOS_LOG(WARNING, "Superstructure output too old.\n");
449 hood_victor_->SetDisabled();
450 intake_joint_victor_->SetDisabled();
451 turret_victor_->SetDisabled();
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800452 accelerator_left_falcon_->SetDisabled();
453 accelerator_right_falcon_->SetDisabled();
Austin Schuh0ad31d72021-03-06 17:07:04 -0800454 finisher_falcon0_->SetDisabled();
455 finisher_falcon1_->SetDisabled();
Ravago Jones3dda5602021-03-10 00:33:13 -0800456 feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
457 intake_roller_falcon_->Set(
458 ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
459 climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
Alex Perryc4691f52020-02-17 19:20:01 -0800460 }
461
462 ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_,
milind-u3d68aa72021-09-26 12:19:03 -0700463 turret_victor_;
Alex Perryc4691f52020-02-17 19:20:01 -0800464
Ravago Jones3dda5602021-03-10 00:33:13 -0800465 ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_,
Austin Schuh0ad31d72021-03-06 17:07:04 -0800466 accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_;
Alex Perryc4691f52020-02-17 19:20:01 -0800467
468 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
Ravago Jones3dda5602021-03-10 00:33:13 -0800469 intake_roller_falcon_, climber_falcon_, feeder_falcon_;
Stephan Massaltd021f972020-01-05 20:41:23 -0800470};
471
472class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
473 public:
Alex Perryc4691f52020-02-17 19:20:01 -0800474 ::std::unique_ptr<frc::Encoder> make_encoder(
475 int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) {
Stephan Massaltd021f972020-01-05 20:41:23 -0800476 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
Alex Perryc4691f52020-02-17 19:20:01 -0800477 encodingType);
Stephan Massaltd021f972020-01-05 20:41:23 -0800478 }
479
480 void Run() override {
481 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
482 aos::configuration::ReadConfig("config.json");
483
484 // Thread 1.
485 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
486 ::frc971::wpilib::JoystickSender joystick_sender(
487 &joystick_sender_event_loop);
488 AddLoop(&joystick_sender_event_loop);
489
490 // Thread 2.
491 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
492 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
493 AddLoop(&pdp_fetcher_event_loop);
494
495 // Thread 3.
496 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
497 SensorReader sensor_reader(&sensor_reader_event_loop);
Austin Schuhf7db58c2020-02-29 22:57:43 -0800498 sensor_reader.set_pwm_trigger(true);
Stephan Massaltd021f972020-01-05 20:41:23 -0800499 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
500 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800501 // TODO: pin numbers
Sabina Davisf7afd112020-02-23 13:42:14 -0800502 sensor_reader.set_hood_encoder(
503 make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X));
Alex Perryc4691f52020-02-17 19:20:01 -0800504
Ravago Jones937587c2020-12-26 17:21:09 -0800505 sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25));
506 sensor_reader.set_hood_single_turn_absolute_pwm(
507 make_unique<frc::DigitalInput>(24));
Alex Perryc4691f52020-02-17 19:20:01 -0800508
Sabina Davisf7afd112020-02-23 13:42:14 -0800509 sensor_reader.set_intake_encoder(make_encoder(5));
510 sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1));
Alex Perryc4691f52020-02-17 19:20:01 -0800511
Sabina Davisf7afd112020-02-23 13:42:14 -0800512 sensor_reader.set_turret_encoder(make_encoder(2));
513 sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0));
514 sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0));
Alex Perryc4691f52020-02-17 19:20:01 -0800515
Sabina Davisf7afd112020-02-23 13:42:14 -0800516 sensor_reader.set_finisher_encoder(
517 make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X));
518 sensor_reader.set_left_accelerator_encoder(make_encoder(4));
519 sensor_reader.set_right_accelerator_encoder(make_encoder(3));
520
James Kuszmaul022d40e2020-02-11 17:06:18 -0800521 // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without
522 // the Spartan Board, then trigger is on 26, reset 27, and chip select is
523 // CS0.
Austin Schuh83873c32020-02-22 14:58:39 -0800524 frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS2;
525 std::unique_ptr<frc::DigitalInput> imu_trigger;
526 std::unique_ptr<frc::DigitalOutput> imu_reset;
527 if (::aos::network::GetTeamNumber() ==
528 constants::Values::kCodingRobotTeamNumber) {
529 imu_trigger = make_unique<frc::DigitalInput>(26);
530 imu_reset = make_unique<frc::DigitalOutput>(27);
531 spi_port = frc::SPI::Port::kOnboardCS0;
532 } else {
Sabina Davisf7afd112020-02-23 13:42:14 -0800533 imu_trigger = make_unique<frc::DigitalInput>(9);
534 imu_reset = make_unique<frc::DigitalOutput>(8);
Austin Schuh83873c32020-02-22 14:58:39 -0800535 }
536 auto spi = make_unique<frc::SPI>(spi_port);
James Kuszmaula244a912020-01-18 13:50:50 -0800537 frc971::wpilib::ADIS16470 imu(&sensor_reader_event_loop, spi.get(),
538 imu_trigger.get(), imu_reset.get());
539 sensor_reader.set_imu(&imu);
Stephan Massaltd021f972020-01-05 20:41:23 -0800540 AddLoop(&sensor_reader_event_loop);
541
542 // Thread 4.
543 ::aos::ShmEventLoop output_event_loop(&config.message());
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800544 output_event_loop.set_name("output_writer");
Stephan Massaltd021f972020-01-05 20:41:23 -0800545 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
546 drivetrain_writer.set_left_controller0(
547 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
548 drivetrain_writer.set_right_controller0(
549 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
550
551 SuperstructureWriter superstructure_writer(&output_event_loop);
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800552 superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8));
Alex Perryc4691f52020-02-17 19:20:01 -0800553 superstructure_writer.set_intake_joint_victor(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800554 make_unique<frc::VictorSP>(2));
Alex Perryc4691f52020-02-17 19:20:01 -0800555 superstructure_writer.set_intake_roller_falcon(
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800556 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
557 superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7));
Ravago Jones3dda5602021-03-10 00:33:13 -0800558 superstructure_writer.set_feeder_falcon(
559 make_unique<ctre::phoenix::motorcontrol::can::TalonFX>(1));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800560 superstructure_writer.set_accelerator_left_falcon(
561 make_unique<::frc::TalonFX>(5));
562 superstructure_writer.set_accelerator_right_falcon(
563 make_unique<::frc::TalonFX>(4));
Austin Schuh0ad31d72021-03-06 17:07:04 -0800564 superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9));
565 superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3));
Sabina Davisc2e4bdb2020-02-23 13:44:58 -0800566 // TODO: check port
Tyler Chatow1039e432020-02-28 21:37:50 -0800567 superstructure_writer.set_climber_falcon(
Ravago Jones3dda5602021-03-10 00:33:13 -0800568 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Stephan Massaltd021f972020-01-05 20:41:23 -0800569
570 AddLoop(&output_event_loop);
571
572 RunLoops();
573 }
574};
575
576} // namespace wpilib
577} // namespace y2020
578
579AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot);