blob: b3969fb887f509d08ce1e8586b8f32319a425850 [file] [log] [blame]
Sabina Davisabeae332019-02-01 21:12:57 -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
13#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"
18#include "frc971/wpilib/ahal/VictorSP.h"
19#undef ERROR
20
21#include "aos/commonmath.h"
22#include "aos/init.h"
23#include "aos/logging/logging.h"
24#include "aos/logging/queue_logging.h"
25#include "aos/make_unique.h"
Sabina Davisabeae332019-02-01 21:12:57 -080026#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080027#include "aos/util/log_interval.h"
28#include "aos/util/phased_loop.h"
29#include "aos/util/wrapping_counter.h"
30
31#include "frc971/autonomous/auto.q.h"
32#include "frc971/control_loops/drivetrain/drivetrain.q.h"
33#include "frc971/wpilib/ADIS16448.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080034#include "frc971/wpilib/buffered_pcm.h"
35#include "frc971/wpilib/buffered_solenoid.h"
Sabina Davisabeae332019-02-01 21:12:57 -080036#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080037#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080038#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080039#include "frc971/wpilib/joystick_sender.h"
40#include "frc971/wpilib/logging.q.h"
41#include "frc971/wpilib/loop_output_handler.h"
42#include "frc971/wpilib/pdp_fetcher.h"
Sabina Davisadc58542019-02-01 22:23:00 -080043#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080044#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080045#include "third_party/Phoenix-frc-lib/cpp/include/ctre/phoenix/MotorControl/CAN/TalonSRX.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080046#include "y2019/constants.h"
Alex Perry5fb5ff22019-02-09 21:53:17 -080047#include "y2019/control_loops/superstructure/superstructure.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080048
49#ifndef M_PI
50#define M_PI 3.14159265358979323846
51#endif
52
53using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080054using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080055using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080056using ::aos::monotonic_clock;
57namespace chrono = ::std::chrono;
58using aos::make_unique;
59
60namespace y2019 {
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
70// TODO(brian): Use ::std::max instead once we have C++14 so that can be
71// constexpr.
72template <typename T>
73constexpr T max(T a, T b) {
74 return (a > b) ? a : b;
75}
76
77template <typename T, typename... Rest>
78constexpr T max(T a, T b, T c, Rest... rest) {
79 return max(max(a, b), c, rest...);
80}
81
82double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080083 return ((static_cast<double>(in) /
84 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080085 (2.0 * M_PI)) *
86 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080087 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080088}
89
90double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080091 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080092 (2.0 * M_PI)) *
93 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080094 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080095}
96
Alex Perry5fb5ff22019-02-09 21:53:17 -080097double elevator_pot_translate(double voltage) {
98 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -080099 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800100}
101
102double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800103 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800104 (2 * M_PI /*radians*/);
105}
106
107double stilts_pot_translate(double voltage) {
108 return voltage * Values::kStiltsPotRatio() *
109 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
110}
111
Sabina Davisabeae332019-02-01 21:12:57 -0800112constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800113 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
114 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800115static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
116 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800117constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800118 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
119 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800120
Sabina Davisabeae332019-02-01 21:12:57 -0800121static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
122 "medium encoders are too fast");
123
124// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800125class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800126 public:
127 SensorReader() {
128 // Set to filter out anything shorter than 1/4 of the minimum pulse width
129 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800130 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
131 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800132 }
133
Alex Perry5fb5ff22019-02-09 21:53:17 -0800134 // Elevator
135
136 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
137 medium_encoder_filter_.Add(encoder.get());
138 elevator_encoder_.set_encoder(::std::move(encoder));
139 }
140
141 void set_elevator_absolute_pwm(
142 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
143 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
144 }
145
146 void set_elevator_potentiometer(
147 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
148 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
149 }
150
151 // Intake
152
153 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
154 medium_encoder_filter_.Add(encoder.get());
155 intake_encoder_.set_encoder(::std::move(encoder));
156 }
157
158 void set_intake_absolute_pwm(
159 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
160 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
161 }
162
163 // Wrist
164
165 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
166 medium_encoder_filter_.Add(encoder.get());
167 wrist_encoder_.set_encoder(::std::move(encoder));
168 }
169
170 void set_wrist_absolute_pwm(
171 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
172 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
173 }
174
175 void set_wrist_potentiometer(
176 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
177 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
178 }
179
180 // Stilts
181
182 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
183 medium_encoder_filter_.Add(encoder.get());
184 stilts_encoder_.set_encoder(::std::move(encoder));
185 }
186
187 void set_stilts_absolute_pwm(
188 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
189 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
190 }
191
192 void set_stilts_potentiometer(
193 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
194 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
195 }
196
Austin Schuh461e1182019-02-17 14:56:44 -0800197 // Vacuum pressure sensor
198 void set_vacuum_sensor(int port) {
199 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
200 }
201
Sabina Davis399dbd82019-02-01 23:06:08 -0800202 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800203 {
204 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
205 drivetrain_message->left_encoder =
206 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
207 drivetrain_message->left_speed =
208 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
209
210 drivetrain_message->right_encoder =
211 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
212 drivetrain_message->right_speed = -drivetrain_velocity_translate(
213 drivetrain_right_encoder_->GetPeriod());
214
215 drivetrain_message.Send();
216 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800217 const auto values = constants::GetValues();
218
219 {
220 auto superstructure_message = superstructure_queue.position.MakeMessage();
221
222 // Elevator
223 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
224 Values::kElevatorEncoderCountsPerRevolution(),
225 Values::kElevatorEncoderRatio(), elevator_pot_translate,
226 false, values.elevator.potentiometer_offset);
227 // Intake
228 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
229 Values::kIntakeEncoderCountsPerRevolution(),
230 Values::kIntakeEncoderRatio(), false);
231
232 // Wrist
233 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
234 Values::kWristEncoderCountsPerRevolution(),
235 Values::kWristEncoderRatio(), wrist_pot_translate, false,
236 values.wrist.potentiometer_offset);
237
238 // Stilts
239 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
240 Values::kStiltsEncoderCountsPerRevolution(),
241 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
242 values.stilts.potentiometer_offset);
243
Austin Schuh461e1182019-02-17 14:56:44 -0800244 // Suction
245 constexpr float kMinVoltage = 0.5;
246 constexpr float kMaxVoltage = 2.1;
247 superstructure_message->suction_pressure =
248 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
249 (kMaxVoltage - kMinVoltage);
250
Alex Perry5fb5ff22019-02-09 21:53:17 -0800251 superstructure_message.Send();
252 }
253 }
254
255 private:
256 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
257 wrist_encoder_, stilts_encoder_;
258
Austin Schuh461e1182019-02-17 14:56:44 -0800259 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
260
Alex Perry5fb5ff22019-02-09 21:53:17 -0800261 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
262 // TODO(sabina): Add wrist and elevator hall effects.
263};
264
265class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
266 public:
267 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
268 elevator_victor_ = ::std::move(t);
269 }
270
Austin Schuh461e1182019-02-17 14:56:44 -0800271 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
272 suction_victor_ = ::std::move(t);
273 }
274
Alex Perry5fb5ff22019-02-09 21:53:17 -0800275 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
276 intake_victor_ = ::std::move(t);
277 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800278
279 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
280 wrist_victor_ = ::std::move(t);
281 }
282
283 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
284 stilts_victor_ = ::std::move(t);
285 }
286
287 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800288 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800289 ::y2019::control_loops::superstructure::superstructure_queue.output
290 .FetchAnother();
291 }
292
Austin Schuh461e1182019-02-17 14:56:44 -0800293 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800294 auto &queue =
295 ::y2019::control_loops::superstructure::superstructure_queue.output;
296 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800297 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800298 -kMaxBringupPower,
299 kMaxBringupPower) /
300 12.0);
301
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800302 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800303 -kMaxBringupPower, kMaxBringupPower) /
304 12.0);
305
Alex Perry5fb5ff22019-02-09 21:53:17 -0800306 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
307 -kMaxBringupPower, kMaxBringupPower) /
308 12.0);
309
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800310 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800311 -kMaxBringupPower, kMaxBringupPower) /
312 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800313
314 suction_victor_->SetSpeed(
315 ::aos::Clip(queue->pump_voltage, -kMaxBringupPower, kMaxBringupPower) /
316 12.0);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800317 }
318
Austin Schuh461e1182019-02-17 14:56:44 -0800319 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800320 LOG(WARNING, "Superstructure output too old.\n");
321
322 elevator_victor_->SetDisabled();
323 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800324 wrist_victor_->SetDisabled();
325 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800326 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800327 }
328
329 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800330 wrist_victor_, stilts_victor_, suction_victor_;
Sabina Davisabeae332019-02-01 21:12:57 -0800331};
332
Austin Schuhc1d6f832019-02-15 23:22:17 -0800333class SolenoidWriter {
334 public:
335 SolenoidWriter()
336 : superstructure_(
337 ".y2019.control_loops.superstructure.superstructure_queue.output") {
338 }
339
Austin Schuh461e1182019-02-17 14:56:44 -0800340 void set_big_suction_cup(int index0, int index1) {
341 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
342 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800343 }
Austin Schuh461e1182019-02-17 14:56:44 -0800344 void set_small_suction_cup(int index0, int index1) {
345 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
346 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800347 }
348
349 void set_intake_roller_talon(
350 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
351 intake_rollers_talon_ = ::std::move(t);
352 intake_rollers_talon_->ConfigContinuousCurrentLimit(40.0, 0);
353 intake_rollers_talon_->EnableCurrentLimit(true);
354 }
355
356 void operator()() {
357 ::aos::SetCurrentThreadName("Solenoids");
358 ::aos::SetCurrentThreadRealtimePriority(27);
359
360 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
361 ::std::chrono::milliseconds(1));
362
363 while (run_) {
364 {
365 const int iterations = phased_loop.SleepUntilNext();
366 if (iterations != 1) {
367 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
368 }
369 }
370
371 {
372 superstructure_.FetchLatest();
373 if (superstructure_.get()) {
374 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
375
Austin Schuh461e1182019-02-17 14:56:44 -0800376 big_suction_cup0_->Set(!superstructure_->intake_suction_top);
377 big_suction_cup1_->Set(!superstructure_->intake_suction_top);
378 small_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
379 small_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800380
381 intake_rollers_talon_->Set(
382 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
383 ::aos::Clip(superstructure_->intake_roller_voltage,
384 -kMaxBringupPower, kMaxBringupPower) /
385 12.0);
386 }
387 }
388
389 {
390 ::frc971::wpilib::PneumaticsToLog to_log;
391
392 pcm_.Flush();
393 to_log.read_solenoids = pcm_.GetAll();
394 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
395 }
396 }
397 }
398
399 void Quit() { run_ = false; }
400
401 private:
402 ::frc971::wpilib::BufferedPcm pcm_;
403
Austin Schuh461e1182019-02-17 14:56:44 -0800404 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
405 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800406
407 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
408 intake_rollers_talon_;
409
410 ::aos::Queue<
411 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
412 superstructure_;
413
414 ::std::atomic<bool> run_{true};
415};
416
Sabina Davisabeae332019-02-01 21:12:57 -0800417class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
418 public:
419 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
420 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
421 frc::Encoder::k4X);
422 }
423
424 void Run() override {
425 ::aos::InitNRT();
426 ::aos::SetCurrentThreadName("StartCompetition");
427
428 ::frc971::wpilib::JoystickSender joystick_sender;
429 ::std::thread joystick_thread(::std::ref(joystick_sender));
430
431 ::frc971::wpilib::PDPFetcher pdp_fetcher;
432 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
433 SensorReader reader;
434
Sabina Davisabeae332019-02-01 21:12:57 -0800435 reader.set_drivetrain_left_encoder(make_encoder(0));
436 reader.set_drivetrain_right_encoder(make_encoder(1));
437
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800438 reader.set_elevator_encoder(make_encoder(4));
439 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
440 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800441
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800442 reader.set_wrist_encoder(make_encoder(5));
443 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
444 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800445
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800446 reader.set_intake_encoder(make_encoder(2));
447 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800448
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800449 reader.set_stilts_encoder(make_encoder(3));
450 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800451 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
452
Sabina Davisabeae332019-02-01 21:12:57 -0800453 reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25));
Austin Schuh461e1182019-02-17 14:56:44 -0800454 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800455
Sabina Davisabeae332019-02-01 21:12:57 -0800456 ::std::thread reader_thread(::std::ref(reader));
457
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800458 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800459 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
460 imu_trigger.get());
461 imu.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800462 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800463 imu.set_reset(imu_reset.get());
464 ::std::thread imu_thread(::std::ref(imu));
465
466 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
467 // they are identical, as far as DrivetrainWriter is concerned, to the SP
468 // variety so all the Victors are written as SPs.
469
Sabina Davisd004fd62019-02-02 23:51:46 -0800470 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
471 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800472 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800473 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800474 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800475 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
476
Alex Perry5fb5ff22019-02-09 21:53:17 -0800477 SuperstructureWriter superstructure_writer;
478 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800479 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800480 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800481 superstructure_writer.set_suction_victor(
482 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800483 superstructure_writer.set_intake_victor(
484 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800485 superstructure_writer.set_wrist_victor(
486 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
487 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800488 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800489
490 ::std::thread superstructure_writer_thread(
491 ::std::ref(superstructure_writer));
492
Austin Schuhc1d6f832019-02-15 23:22:17 -0800493 SolenoidWriter solenoid_writer;
494 solenoid_writer.set_intake_roller_talon(
495 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800496 solenoid_writer.set_big_suction_cup(0, 1);
497 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800498
499 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
500
Sabina Davisabeae332019-02-01 21:12:57 -0800501 // Wait forever. Not much else to do...
502 while (true) {
503 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
504 if (r != 0) {
505 PLOG(WARNING, "infinite select failed");
506 } else {
507 PLOG(WARNING, "infinite select succeeded??\n");
508 }
509 }
510
511 LOG(ERROR, "Exiting WPILibRobot\n");
512
Austin Schuhc1d6f832019-02-15 23:22:17 -0800513 solenoid_writer.Quit();
514 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800515 joystick_sender.Quit();
516 joystick_thread.join();
517 pdp_fetcher.Quit();
518 pdp_fetcher_thread.join();
519 reader.Quit();
520 reader_thread.join();
521 imu.Quit();
522 imu_thread.join();
523
524 drivetrain_writer.Quit();
525 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800526 superstructure_writer.Quit();
527 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800528
529 ::aos::Cleanup();
530 }
531};
532
533} // namespace
534} // namespace wpilib
535} // namespace y2019
536
537AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);