blob: a4d3620d0bc1edf8a8941f092fbb420b55622567 [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"
Brian Silvermanf8b75252019-02-24 16:13:58 -080048#include "y2019/jevois/spi.h"
Sabina Davisabeae332019-02-01 21:12:57 -080049
50#ifndef M_PI
51#define M_PI 3.14159265358979323846
52#endif
53
54using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080055using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080056using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080057using ::aos::monotonic_clock;
58namespace chrono = ::std::chrono;
59using aos::make_unique;
60
61namespace y2019 {
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
71// TODO(brian): Use ::std::max instead once we have C++14 so that can be
72// constexpr.
73template <typename T>
74constexpr T max(T a, T b) {
75 return (a > b) ? a : b;
76}
77
78template <typename T, typename... Rest>
79constexpr T max(T a, T b, T c, Rest... rest) {
80 return max(max(a, b), c, rest...);
81}
82
83double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080084 return ((static_cast<double>(in) /
85 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080086 (2.0 * M_PI)) *
87 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080088 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080089}
90
91double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080092 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080093 (2.0 * M_PI)) *
94 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080095 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080096}
97
Alex Perry5fb5ff22019-02-09 21:53:17 -080098double elevator_pot_translate(double voltage) {
99 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -0800100 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800101}
102
103double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800104 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800105 (2 * M_PI /*radians*/);
106}
107
108double stilts_pot_translate(double voltage) {
109 return voltage * Values::kStiltsPotRatio() *
110 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
111}
112
Sabina Davisabeae332019-02-01 21:12:57 -0800113constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800114 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
115 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800116static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
117 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800118constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800119 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
120 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800121
Sabina Davisabeae332019-02-01 21:12:57 -0800122static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
123 "medium encoders are too fast");
124
125// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800126class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800127 public:
128 SensorReader() {
129 // Set to filter out anything shorter than 1/4 of the minimum pulse width
130 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800131 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
132 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800133 }
134
Alex Perry5fb5ff22019-02-09 21:53:17 -0800135 // Elevator
136
137 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
138 medium_encoder_filter_.Add(encoder.get());
139 elevator_encoder_.set_encoder(::std::move(encoder));
140 }
141
142 void set_elevator_absolute_pwm(
143 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
144 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
145 }
146
147 void set_elevator_potentiometer(
148 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
149 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
150 }
151
152 // Intake
153
154 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
155 medium_encoder_filter_.Add(encoder.get());
156 intake_encoder_.set_encoder(::std::move(encoder));
157 }
158
159 void set_intake_absolute_pwm(
160 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
161 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
162 }
163
164 // Wrist
165
166 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
167 medium_encoder_filter_.Add(encoder.get());
168 wrist_encoder_.set_encoder(::std::move(encoder));
169 }
170
171 void set_wrist_absolute_pwm(
172 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
173 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
174 }
175
176 void set_wrist_potentiometer(
177 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
178 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
179 }
180
181 // Stilts
182
183 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
184 medium_encoder_filter_.Add(encoder.get());
185 stilts_encoder_.set_encoder(::std::move(encoder));
186 }
187
188 void set_stilts_absolute_pwm(
189 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
190 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
191 }
192
193 void set_stilts_potentiometer(
194 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
195 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
196 }
197
Austin Schuh461e1182019-02-17 14:56:44 -0800198 // Vacuum pressure sensor
199 void set_vacuum_sensor(int port) {
200 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
201 }
202
Sabina Davis399dbd82019-02-01 23:06:08 -0800203 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800204 {
205 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
206 drivetrain_message->left_encoder =
207 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
208 drivetrain_message->left_speed =
209 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
210
211 drivetrain_message->right_encoder =
212 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
213 drivetrain_message->right_speed = -drivetrain_velocity_translate(
214 drivetrain_right_encoder_->GetPeriod());
215
216 drivetrain_message.Send();
217 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800218 const auto values = constants::GetValues();
219
220 {
221 auto superstructure_message = superstructure_queue.position.MakeMessage();
222
223 // Elevator
224 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
225 Values::kElevatorEncoderCountsPerRevolution(),
226 Values::kElevatorEncoderRatio(), elevator_pot_translate,
227 false, values.elevator.potentiometer_offset);
228 // Intake
229 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
230 Values::kIntakeEncoderCountsPerRevolution(),
231 Values::kIntakeEncoderRatio(), false);
232
233 // Wrist
234 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
235 Values::kWristEncoderCountsPerRevolution(),
236 Values::kWristEncoderRatio(), wrist_pot_translate, false,
237 values.wrist.potentiometer_offset);
238
239 // Stilts
240 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
241 Values::kStiltsEncoderCountsPerRevolution(),
242 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
243 values.stilts.potentiometer_offset);
244
Austin Schuh461e1182019-02-17 14:56:44 -0800245 // Suction
246 constexpr float kMinVoltage = 0.5;
247 constexpr float kMaxVoltage = 2.1;
248 superstructure_message->suction_pressure =
249 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
250 (kMaxVoltage - kMinVoltage);
251
Alex Perry5fb5ff22019-02-09 21:53:17 -0800252 superstructure_message.Send();
253 }
254 }
255
256 private:
257 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
258 wrist_encoder_, stilts_encoder_;
259
Austin Schuh461e1182019-02-17 14:56:44 -0800260 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
261
Alex Perry5fb5ff22019-02-09 21:53:17 -0800262 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
263 // TODO(sabina): Add wrist and elevator hall effects.
264};
265
Brian Silvermanf8b75252019-02-24 16:13:58 -0800266class CameraReader {
267 public:
268 CameraReader() = default;
269 CameraReader(const CameraReader &) = delete;
270 CameraReader &operator=(const CameraReader &) = delete;
271
272 void set_spi(frc::SPI *spi) {
273 spi_ = spi;
274 spi_->SetClockRate(1e6);
275 spi_->SetChipSelectActiveHigh();
276 spi_->SetClockActiveLow();
277 spi_->SetSampleDataOnFalling();
278 // It ignores you if you try changing this...
279 spi_->SetMSBFirst();
280 }
281
282 void DoSpiTransaction() {
283 using namespace frc971::jevois;
284 RoborioToTeensy to_teensy{};
285 to_teensy.realtime_now = aos::realtime_clock::now();
286
287 std::array<char, spi_transfer_size() + 1> to_send{};
288 {
289 const auto to_send_data =
290 gsl::make_span(to_send).last<spi_transfer_size()>();
291 const auto encoded = SpiPackToTeensy(to_teensy);
292 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
293 }
294 rx_clearer_.ClearRxFifo();
295 // First, send recieve a dummy byte because the Teensy can't control what it
296 // sends for the first byte.
297 std::array<char, spi_transfer_size() + 1> to_receive;
298 DoTransaction(to_send, to_receive);
299 const auto unpacked = SpiUnpackToRoborio(
300 gsl::make_span(to_receive).last(spi_transfer_size()));
301 if (!unpacked) {
302 LOG(INFO, "Decoding SPI data failed\n");
303 return;
304 }
305
306 // TODO(Brian): Do something useful with the targets.
307
308 if (dummy_spi_) {
309 uint8_t dummy_send, dummy_receive;
310 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
311 }
312 }
313
314 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
315 CHECK_EQ(to_send.size(), to_receive.size());
316 const auto result = spi_->Transaction(
317 reinterpret_cast<uint8_t *>(to_send.data()),
318 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
319 if (result == to_send.size()) {
320 return;
321 }
322 if (result == -1) {
323 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
324 return;
325 }
326 LOG(FATAL, "SPI::Transaction returned something weird\n");
327 }
328
329 void SetDummySPI(frc::SPI::Port port) {
330 dummy_spi_.reset(new frc::SPI(port));
331 // Pick the same settings here in case the roboRIO decides to try something
332 // stupid when switching.
333 if (dummy_spi_) {
334 dummy_spi_->SetClockRate(1e5);
335 dummy_spi_->SetChipSelectActiveLow();
336 dummy_spi_->SetClockActiveLow();
337 dummy_spi_->SetSampleDataOnFalling();
338 dummy_spi_->SetMSBFirst();
339 }
340 }
341
342 private:
343 frc::SPI *spi_ = nullptr;
344 ::std::unique_ptr<frc::SPI> dummy_spi_;
345
346 frc971::wpilib::SpiRxClearer rx_clearer_;
347};
348
Alex Perry5fb5ff22019-02-09 21:53:17 -0800349class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
350 public:
351 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
352 elevator_victor_ = ::std::move(t);
353 }
354
Austin Schuh461e1182019-02-17 14:56:44 -0800355 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
356 suction_victor_ = ::std::move(t);
357 }
358
Alex Perry5fb5ff22019-02-09 21:53:17 -0800359 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
360 intake_victor_ = ::std::move(t);
361 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800362
363 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
364 wrist_victor_ = ::std::move(t);
365 }
366
367 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
368 stilts_victor_ = ::std::move(t);
369 }
370
371 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800372 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800373 ::y2019::control_loops::superstructure::superstructure_queue.output
374 .FetchAnother();
375 }
376
Austin Schuh461e1182019-02-17 14:56:44 -0800377 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800378 auto &queue =
379 ::y2019::control_loops::superstructure::superstructure_queue.output;
380 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800381 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800382 -kMaxBringupPower,
383 kMaxBringupPower) /
384 12.0);
385
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800386 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800387 -kMaxBringupPower, kMaxBringupPower) /
388 12.0);
389
Alex Perry5fb5ff22019-02-09 21:53:17 -0800390 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
391 -kMaxBringupPower, kMaxBringupPower) /
392 12.0);
393
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800394 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800395 -kMaxBringupPower, kMaxBringupPower) /
396 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800397
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800398 ::aos::robot_state.FetchLatest();
399 const double battery_voltage =
400 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
401
402 // Throw a fast low pass filter on the battery voltage so we don't respond
403 // too fast to noise.
404 filtered_battery_voltage_ =
405 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
406
407 suction_victor_->SetSpeed(::aos::Clip(
408 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800409 }
410
Austin Schuh461e1182019-02-17 14:56:44 -0800411 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800412 LOG(WARNING, "Superstructure output too old.\n");
413
414 elevator_victor_->SetDisabled();
415 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800416 wrist_victor_->SetDisabled();
417 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800418 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800419 }
420
421 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800422 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800423
424 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800425};
426
Austin Schuhc1d6f832019-02-15 23:22:17 -0800427class SolenoidWriter {
428 public:
429 SolenoidWriter()
430 : superstructure_(
431 ".y2019.control_loops.superstructure.superstructure_queue.output") {
432 }
433
Austin Schuh461e1182019-02-17 14:56:44 -0800434 void set_big_suction_cup(int index0, int index1) {
435 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
436 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800437 }
Austin Schuh461e1182019-02-17 14:56:44 -0800438 void set_small_suction_cup(int index0, int index1) {
439 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
440 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800441 }
442
443 void set_intake_roller_talon(
444 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
445 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800446 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800447 intake_rollers_talon_->EnableCurrentLimit(true);
448 }
449
450 void operator()() {
451 ::aos::SetCurrentThreadName("Solenoids");
452 ::aos::SetCurrentThreadRealtimePriority(27);
453
454 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
455 ::std::chrono::milliseconds(1));
456
457 while (run_) {
458 {
459 const int iterations = phased_loop.SleepUntilNext();
460 if (iterations != 1) {
461 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
462 }
463 }
464
465 {
466 superstructure_.FetchLatest();
467 if (superstructure_.get()) {
468 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
469
Sabina Davisa569ac82019-02-23 10:47:42 -0800470 big_suction_cup0_->Set(superstructure_->intake_suction_top);
471 big_suction_cup1_->Set(superstructure_->intake_suction_top);
472 small_suction_cup0_->Set(superstructure_->intake_suction_bottom);
473 small_suction_cup1_->Set(superstructure_->intake_suction_bottom);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800474
475 intake_rollers_talon_->Set(
476 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
477 ::aos::Clip(superstructure_->intake_roller_voltage,
478 -kMaxBringupPower, kMaxBringupPower) /
479 12.0);
480 }
481 }
482
483 {
484 ::frc971::wpilib::PneumaticsToLog to_log;
485
486 pcm_.Flush();
487 to_log.read_solenoids = pcm_.GetAll();
488 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
489 }
490 }
491 }
492
493 void Quit() { run_ = false; }
494
495 private:
496 ::frc971::wpilib::BufferedPcm pcm_;
497
Austin Schuh461e1182019-02-17 14:56:44 -0800498 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
499 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800500
501 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
502 intake_rollers_talon_;
503
504 ::aos::Queue<
505 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
506 superstructure_;
507
508 ::std::atomic<bool> run_{true};
509};
510
Sabina Davisabeae332019-02-01 21:12:57 -0800511class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
512 public:
513 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
514 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
515 frc::Encoder::k4X);
516 }
517
518 void Run() override {
519 ::aos::InitNRT();
520 ::aos::SetCurrentThreadName("StartCompetition");
521
522 ::frc971::wpilib::JoystickSender joystick_sender;
523 ::std::thread joystick_thread(::std::ref(joystick_sender));
524
525 ::frc971::wpilib::PDPFetcher pdp_fetcher;
526 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
527 SensorReader reader;
528
Sabina Davisabeae332019-02-01 21:12:57 -0800529 reader.set_drivetrain_left_encoder(make_encoder(0));
530 reader.set_drivetrain_right_encoder(make_encoder(1));
531
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800532 reader.set_elevator_encoder(make_encoder(4));
533 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
534 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800535
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800536 reader.set_wrist_encoder(make_encoder(5));
537 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
538 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800539
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800540 reader.set_intake_encoder(make_encoder(2));
541 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800542
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800543 reader.set_stilts_encoder(make_encoder(3));
544 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800545 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
546
Sabina Davisabeae332019-02-01 21:12:57 -0800547 reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25));
Austin Schuh461e1182019-02-17 14:56:44 -0800548 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800549
Sabina Davisabeae332019-02-01 21:12:57 -0800550 ::std::thread reader_thread(::std::ref(reader));
551
Brian Silvermanf8b75252019-02-24 16:13:58 -0800552 CameraReader camera_reader;
553 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
554 camera_reader.set_spi(&camera_spi);
555 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
556
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800557 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800558 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
559 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800560 imu.set_spi_idle_callback(
561 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800562 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800563 imu.set_reset(imu_reset.get());
564 ::std::thread imu_thread(::std::ref(imu));
565
566 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
567 // they are identical, as far as DrivetrainWriter is concerned, to the SP
568 // variety so all the Victors are written as SPs.
569
Sabina Davisd004fd62019-02-02 23:51:46 -0800570 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
571 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800572 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800573 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800574 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800575 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
576
Alex Perry5fb5ff22019-02-09 21:53:17 -0800577 SuperstructureWriter superstructure_writer;
578 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800579 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800580 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800581 superstructure_writer.set_suction_victor(
582 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800583 superstructure_writer.set_intake_victor(
584 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800585 superstructure_writer.set_wrist_victor(
586 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
587 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800588 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800589
590 ::std::thread superstructure_writer_thread(
591 ::std::ref(superstructure_writer));
592
Austin Schuhc1d6f832019-02-15 23:22:17 -0800593 SolenoidWriter solenoid_writer;
594 solenoid_writer.set_intake_roller_talon(
595 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800596 solenoid_writer.set_big_suction_cup(0, 1);
597 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800598
599 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
600
Sabina Davisabeae332019-02-01 21:12:57 -0800601 // Wait forever. Not much else to do...
602 while (true) {
603 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
604 if (r != 0) {
605 PLOG(WARNING, "infinite select failed");
606 } else {
607 PLOG(WARNING, "infinite select succeeded??\n");
608 }
609 }
610
611 LOG(ERROR, "Exiting WPILibRobot\n");
612
Austin Schuhc1d6f832019-02-15 23:22:17 -0800613 solenoid_writer.Quit();
614 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800615 joystick_sender.Quit();
616 joystick_thread.join();
617 pdp_fetcher.Quit();
618 pdp_fetcher_thread.join();
619 reader.Quit();
620 reader_thread.join();
621 imu.Quit();
622 imu_thread.join();
623
624 drivetrain_writer.Quit();
625 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800626 superstructure_writer.Quit();
627 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800628
629 ::aos::Cleanup();
630 }
631};
632
633} // namespace
634} // namespace wpilib
635} // namespace y2019
636
637AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);