blob: be6ee33d0814a85dc8958de82d46cfe895232b69 [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 Schuhf6b94632019-02-02 22:11:27 -080045#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();
Brian Silvermane9924fd2019-03-02 15:20:42 -0800286 to_teensy.camera_command = CameraCommand::kNormal;
Brian Silvermanf8b75252019-02-24 16:13:58 -0800287
288 std::array<char, spi_transfer_size() + 1> to_send{};
289 {
290 const auto to_send_data =
291 gsl::make_span(to_send).last<spi_transfer_size()>();
292 const auto encoded = SpiPackToTeensy(to_teensy);
293 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
294 }
295 rx_clearer_.ClearRxFifo();
296 // First, send recieve a dummy byte because the Teensy can't control what it
297 // sends for the first byte.
298 std::array<char, spi_transfer_size() + 1> to_receive;
299 DoTransaction(to_send, to_receive);
300 const auto unpacked = SpiUnpackToRoborio(
301 gsl::make_span(to_receive).last(spi_transfer_size()));
302 if (!unpacked) {
303 LOG(INFO, "Decoding SPI data failed\n");
304 return;
305 }
306
307 // TODO(Brian): Do something useful with the targets.
308
309 if (dummy_spi_) {
310 uint8_t dummy_send, dummy_receive;
311 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
312 }
313 }
314
315 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
316 CHECK_EQ(to_send.size(), to_receive.size());
317 const auto result = spi_->Transaction(
318 reinterpret_cast<uint8_t *>(to_send.data()),
319 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
320 if (result == to_send.size()) {
321 return;
322 }
323 if (result == -1) {
324 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
325 return;
326 }
327 LOG(FATAL, "SPI::Transaction returned something weird\n");
328 }
329
330 void SetDummySPI(frc::SPI::Port port) {
331 dummy_spi_.reset(new frc::SPI(port));
332 // Pick the same settings here in case the roboRIO decides to try something
333 // stupid when switching.
334 if (dummy_spi_) {
335 dummy_spi_->SetClockRate(1e5);
336 dummy_spi_->SetChipSelectActiveLow();
337 dummy_spi_->SetClockActiveLow();
338 dummy_spi_->SetSampleDataOnFalling();
339 dummy_spi_->SetMSBFirst();
340 }
341 }
342
343 private:
344 frc::SPI *spi_ = nullptr;
345 ::std::unique_ptr<frc::SPI> dummy_spi_;
346
347 frc971::wpilib::SpiRxClearer rx_clearer_;
348};
349
Alex Perry5fb5ff22019-02-09 21:53:17 -0800350class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
351 public:
352 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
353 elevator_victor_ = ::std::move(t);
354 }
355
Austin Schuh461e1182019-02-17 14:56:44 -0800356 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
357 suction_victor_ = ::std::move(t);
358 }
359
Alex Perry5fb5ff22019-02-09 21:53:17 -0800360 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
361 intake_victor_ = ::std::move(t);
362 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800363
364 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
365 wrist_victor_ = ::std::move(t);
366 }
367
368 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
369 stilts_victor_ = ::std::move(t);
370 }
371
372 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800373 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800374 ::y2019::control_loops::superstructure::superstructure_queue.output
375 .FetchAnother();
376 }
377
Austin Schuh461e1182019-02-17 14:56:44 -0800378 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800379 auto &queue =
380 ::y2019::control_loops::superstructure::superstructure_queue.output;
381 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800382 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800383 -kMaxBringupPower,
384 kMaxBringupPower) /
385 12.0);
386
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800387 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800388 -kMaxBringupPower, kMaxBringupPower) /
389 12.0);
390
Alex Perry5fb5ff22019-02-09 21:53:17 -0800391 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
392 -kMaxBringupPower, kMaxBringupPower) /
393 12.0);
394
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800395 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800396 -kMaxBringupPower, kMaxBringupPower) /
397 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800398
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800399 ::aos::robot_state.FetchLatest();
400 const double battery_voltage =
401 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
402
403 // Throw a fast low pass filter on the battery voltage so we don't respond
404 // too fast to noise.
405 filtered_battery_voltage_ =
406 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
407
408 suction_victor_->SetSpeed(::aos::Clip(
409 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800410 }
411
Austin Schuh461e1182019-02-17 14:56:44 -0800412 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800413 LOG(WARNING, "Superstructure output too old.\n");
414
415 elevator_victor_->SetDisabled();
416 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800417 wrist_victor_->SetDisabled();
418 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800419 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800420 }
421
422 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800423 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800424
425 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800426};
427
Austin Schuhc1d6f832019-02-15 23:22:17 -0800428class SolenoidWriter {
429 public:
430 SolenoidWriter()
431 : superstructure_(
432 ".y2019.control_loops.superstructure.superstructure_queue.output") {
433 }
434
Austin Schuh461e1182019-02-17 14:56:44 -0800435 void set_big_suction_cup(int index0, int index1) {
436 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
437 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800438 }
Austin Schuh461e1182019-02-17 14:56:44 -0800439 void set_small_suction_cup(int index0, int index1) {
440 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
441 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800442 }
443
444 void set_intake_roller_talon(
445 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
446 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800447 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800448 intake_rollers_talon_->EnableCurrentLimit(true);
449 }
450
451 void operator()() {
452 ::aos::SetCurrentThreadName("Solenoids");
453 ::aos::SetCurrentThreadRealtimePriority(27);
454
455 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
456 ::std::chrono::milliseconds(1));
457
458 while (run_) {
459 {
460 const int iterations = phased_loop.SleepUntilNext();
461 if (iterations != 1) {
462 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
463 }
464 }
465
466 {
467 superstructure_.FetchLatest();
468 if (superstructure_.get()) {
469 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
470
Tyler Chatow7db827f2019-02-24 00:10:13 -0800471 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
472 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
473 small_suction_cup0_->Set(superstructure_->intake_suction_top);
474 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800475
476 intake_rollers_talon_->Set(
477 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
478 ::aos::Clip(superstructure_->intake_roller_voltage,
479 -kMaxBringupPower, kMaxBringupPower) /
480 12.0);
481 }
482 }
483
484 {
485 ::frc971::wpilib::PneumaticsToLog to_log;
486
487 pcm_.Flush();
488 to_log.read_solenoids = pcm_.GetAll();
489 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
490 }
491 }
492 }
493
494 void Quit() { run_ = false; }
495
496 private:
497 ::frc971::wpilib::BufferedPcm pcm_;
498
Austin Schuh461e1182019-02-17 14:56:44 -0800499 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
500 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800501
502 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
503 intake_rollers_talon_;
504
505 ::aos::Queue<
506 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
507 superstructure_;
508
509 ::std::atomic<bool> run_{true};
510};
511
Sabina Davisabeae332019-02-01 21:12:57 -0800512class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
513 public:
514 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
515 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
516 frc::Encoder::k4X);
517 }
518
519 void Run() override {
520 ::aos::InitNRT();
521 ::aos::SetCurrentThreadName("StartCompetition");
522
523 ::frc971::wpilib::JoystickSender joystick_sender;
524 ::std::thread joystick_thread(::std::ref(joystick_sender));
525
526 ::frc971::wpilib::PDPFetcher pdp_fetcher;
527 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
528 SensorReader reader;
529
Sabina Davisabeae332019-02-01 21:12:57 -0800530 reader.set_drivetrain_left_encoder(make_encoder(0));
531 reader.set_drivetrain_right_encoder(make_encoder(1));
532
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800533 reader.set_elevator_encoder(make_encoder(4));
534 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
535 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800536
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800537 reader.set_wrist_encoder(make_encoder(5));
538 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
539 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800540
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800541 reader.set_intake_encoder(make_encoder(2));
542 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800543
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800544 reader.set_stilts_encoder(make_encoder(3));
545 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800546 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
547
Austin Schuh3b010bc2019-02-24 17:25:37 -0800548 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800549 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800550
Sabina Davisabeae332019-02-01 21:12:57 -0800551 ::std::thread reader_thread(::std::ref(reader));
552
Brian Silvermanf8b75252019-02-24 16:13:58 -0800553 CameraReader camera_reader;
554 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
555 camera_reader.set_spi(&camera_spi);
556 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
557
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800558 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800559 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
560 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800561 imu.set_spi_idle_callback(
562 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800563 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800564 imu.set_reset(imu_reset.get());
565 ::std::thread imu_thread(::std::ref(imu));
566
567 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
568 // they are identical, as far as DrivetrainWriter is concerned, to the SP
569 // variety so all the Victors are written as SPs.
570
Sabina Davisd004fd62019-02-02 23:51:46 -0800571 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
572 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800573 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800574 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800575 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800576 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
577
Alex Perry5fb5ff22019-02-09 21:53:17 -0800578 SuperstructureWriter superstructure_writer;
579 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800580 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800581 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800582 superstructure_writer.set_suction_victor(
583 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800584 superstructure_writer.set_intake_victor(
585 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800586 superstructure_writer.set_wrist_victor(
587 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
588 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800589 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800590
591 ::std::thread superstructure_writer_thread(
592 ::std::ref(superstructure_writer));
593
Austin Schuhc1d6f832019-02-15 23:22:17 -0800594 SolenoidWriter solenoid_writer;
595 solenoid_writer.set_intake_roller_talon(
596 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800597 solenoid_writer.set_big_suction_cup(0, 1);
598 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800599
600 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
601
Sabina Davisabeae332019-02-01 21:12:57 -0800602 // Wait forever. Not much else to do...
603 while (true) {
604 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
605 if (r != 0) {
606 PLOG(WARNING, "infinite select failed");
607 } else {
608 PLOG(WARNING, "infinite select succeeded??\n");
609 }
610 }
611
612 LOG(ERROR, "Exiting WPILibRobot\n");
613
Austin Schuhc1d6f832019-02-15 23:22:17 -0800614 solenoid_writer.Quit();
615 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800616 joystick_sender.Quit();
617 joystick_thread.join();
618 pdp_fetcher.Quit();
619 pdp_fetcher_thread.join();
620 reader.Quit();
621 reader_thread.join();
622 imu.Quit();
623 imu_thread.join();
624
625 drivetrain_writer.Quit();
626 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800627 superstructure_writer.Quit();
628 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800629
630 ::aos::Cleanup();
631 }
632};
633
634} // namespace
635} // namespace wpilib
636} // namespace y2019
637
638AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);