blob: 221bb5143d1c932a9d6269ce2c0d81f6c3a4e74f [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"
Austin Schuhc2ee66b2019-02-19 13:37:46 -080026#include "aos/robot_state/robot_state.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080027#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080028#include "aos/util/log_interval.h"
29#include "aos/util/phased_loop.h"
30#include "aos/util/wrapping_counter.h"
Sabina Davisabeae332019-02-01 21:12:57 -080031#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
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800314 ::aos::robot_state.FetchLatest();
315 const double battery_voltage =
316 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
317
318 // Throw a fast low pass filter on the battery voltage so we don't respond
319 // too fast to noise.
320 filtered_battery_voltage_ =
321 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
322
323 suction_victor_->SetSpeed(::aos::Clip(
324 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800325 }
326
Austin Schuh461e1182019-02-17 14:56:44 -0800327 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800328 LOG(WARNING, "Superstructure output too old.\n");
329
330 elevator_victor_->SetDisabled();
331 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800332 wrist_victor_->SetDisabled();
333 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800334 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800335 }
336
337 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800338 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800339
340 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800341};
342
Austin Schuhc1d6f832019-02-15 23:22:17 -0800343class SolenoidWriter {
344 public:
345 SolenoidWriter()
346 : superstructure_(
347 ".y2019.control_loops.superstructure.superstructure_queue.output") {
348 }
349
Austin Schuh461e1182019-02-17 14:56:44 -0800350 void set_big_suction_cup(int index0, int index1) {
351 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
352 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800353 }
Austin Schuh461e1182019-02-17 14:56:44 -0800354 void set_small_suction_cup(int index0, int index1) {
355 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
356 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800357 }
358
359 void set_intake_roller_talon(
360 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
361 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800362 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800363 intake_rollers_talon_->EnableCurrentLimit(true);
364 }
365
366 void operator()() {
367 ::aos::SetCurrentThreadName("Solenoids");
368 ::aos::SetCurrentThreadRealtimePriority(27);
369
370 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
371 ::std::chrono::milliseconds(1));
372
373 while (run_) {
374 {
375 const int iterations = phased_loop.SleepUntilNext();
376 if (iterations != 1) {
377 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
378 }
379 }
380
381 {
382 superstructure_.FetchLatest();
383 if (superstructure_.get()) {
384 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
385
Sabina Davisa569ac82019-02-23 10:47:42 -0800386 big_suction_cup0_->Set(superstructure_->intake_suction_top);
387 big_suction_cup1_->Set(superstructure_->intake_suction_top);
388 small_suction_cup0_->Set(superstructure_->intake_suction_bottom);
389 small_suction_cup1_->Set(superstructure_->intake_suction_bottom);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800390
391 intake_rollers_talon_->Set(
392 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
393 ::aos::Clip(superstructure_->intake_roller_voltage,
394 -kMaxBringupPower, kMaxBringupPower) /
395 12.0);
396 }
397 }
398
399 {
400 ::frc971::wpilib::PneumaticsToLog to_log;
401
402 pcm_.Flush();
403 to_log.read_solenoids = pcm_.GetAll();
404 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
405 }
406 }
407 }
408
409 void Quit() { run_ = false; }
410
411 private:
412 ::frc971::wpilib::BufferedPcm pcm_;
413
Austin Schuh461e1182019-02-17 14:56:44 -0800414 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
415 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800416
417 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
418 intake_rollers_talon_;
419
420 ::aos::Queue<
421 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
422 superstructure_;
423
424 ::std::atomic<bool> run_{true};
425};
426
Sabina Davisabeae332019-02-01 21:12:57 -0800427class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
428 public:
429 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
430 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
431 frc::Encoder::k4X);
432 }
433
434 void Run() override {
435 ::aos::InitNRT();
436 ::aos::SetCurrentThreadName("StartCompetition");
437
438 ::frc971::wpilib::JoystickSender joystick_sender;
439 ::std::thread joystick_thread(::std::ref(joystick_sender));
440
441 ::frc971::wpilib::PDPFetcher pdp_fetcher;
442 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
443 SensorReader reader;
444
Sabina Davisabeae332019-02-01 21:12:57 -0800445 reader.set_drivetrain_left_encoder(make_encoder(0));
446 reader.set_drivetrain_right_encoder(make_encoder(1));
447
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800448 reader.set_elevator_encoder(make_encoder(4));
449 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
450 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800451
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800452 reader.set_wrist_encoder(make_encoder(5));
453 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
454 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800455
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800456 reader.set_intake_encoder(make_encoder(2));
457 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800458
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800459 reader.set_stilts_encoder(make_encoder(3));
460 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800461 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
462
Sabina Davisabeae332019-02-01 21:12:57 -0800463 reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25));
Austin Schuh461e1182019-02-17 14:56:44 -0800464 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800465
Sabina Davisabeae332019-02-01 21:12:57 -0800466 ::std::thread reader_thread(::std::ref(reader));
467
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800468 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800469 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
470 imu_trigger.get());
471 imu.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800472 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800473 imu.set_reset(imu_reset.get());
474 ::std::thread imu_thread(::std::ref(imu));
475
476 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
477 // they are identical, as far as DrivetrainWriter is concerned, to the SP
478 // variety so all the Victors are written as SPs.
479
Sabina Davisd004fd62019-02-02 23:51:46 -0800480 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
481 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800482 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800483 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800484 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800485 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
486
Alex Perry5fb5ff22019-02-09 21:53:17 -0800487 SuperstructureWriter superstructure_writer;
488 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800489 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800490 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800491 superstructure_writer.set_suction_victor(
492 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800493 superstructure_writer.set_intake_victor(
494 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800495 superstructure_writer.set_wrist_victor(
496 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
497 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800498 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800499
500 ::std::thread superstructure_writer_thread(
501 ::std::ref(superstructure_writer));
502
Austin Schuhc1d6f832019-02-15 23:22:17 -0800503 SolenoidWriter solenoid_writer;
504 solenoid_writer.set_intake_roller_talon(
505 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800506 solenoid_writer.set_big_suction_cup(0, 1);
507 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800508
509 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
510
Sabina Davisabeae332019-02-01 21:12:57 -0800511 // Wait forever. Not much else to do...
512 while (true) {
513 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
514 if (r != 0) {
515 PLOG(WARNING, "infinite select failed");
516 } else {
517 PLOG(WARNING, "infinite select succeeded??\n");
518 }
519 }
520
521 LOG(ERROR, "Exiting WPILibRobot\n");
522
Austin Schuhc1d6f832019-02-15 23:22:17 -0800523 solenoid_writer.Quit();
524 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800525 joystick_sender.Quit();
526 joystick_thread.join();
527 pdp_fetcher.Quit();
528 pdp_fetcher_thread.join();
529 reader.Quit();
530 reader_thread.join();
531 imu.Quit();
532 imu_thread.join();
533
534 drivetrain_writer.Quit();
535 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800536 superstructure_writer.Quit();
537 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800538
539 ::aos::Cleanup();
540 }
541};
542
543} // namespace
544} // namespace wpilib
545} // namespace y2019
546
547AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);