blob: 29f110a3b19329350669824be4e538e0538e98fb [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"
Sabina Davisc6329342019-03-01 20:44:42 -080019#include "ctre/phoenix/CANifier.h"
Sabina Davisabeae332019-02-01 21:12:57 -080020#undef ERROR
21
22#include "aos/commonmath.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080023#include "aos/events/shm-event-loop.h"
Sabina Davisabeae332019-02-01 21:12:57 -080024#include "aos/init.h"
25#include "aos/logging/logging.h"
26#include "aos/logging/queue_logging.h"
27#include "aos/make_unique.h"
Austin Schuhc2ee66b2019-02-19 13:37:46 -080028#include "aos/robot_state/robot_state.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080029#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080030#include "aos/util/log_interval.h"
31#include "aos/util/phased_loop.h"
32#include "aos/util/wrapping_counter.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080033#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
Sabina Davisabeae332019-02-01 21:12:57 -080034#include "frc971/autonomous/auto.q.h"
35#include "frc971/control_loops/drivetrain/drivetrain.q.h"
36#include "frc971/wpilib/ADIS16448.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080037#include "frc971/wpilib/buffered_pcm.h"
38#include "frc971/wpilib/buffered_solenoid.h"
Sabina Davisabeae332019-02-01 21:12:57 -080039#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080040#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080041#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080042#include "frc971/wpilib/joystick_sender.h"
43#include "frc971/wpilib/logging.q.h"
44#include "frc971/wpilib/loop_output_handler.h"
45#include "frc971/wpilib/pdp_fetcher.h"
Sabina Davisadc58542019-02-01 22:23:00 -080046#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080047#include "frc971/wpilib/wpilib_robot_base.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080048#include "y2019/constants.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080049#include "y2019/control_loops/drivetrain/camera.q.h"
Alex Perry5fb5ff22019-02-09 21:53:17 -080050#include "y2019/control_loops/superstructure/superstructure.q.h"
Brian Silvermanf8b75252019-02-24 16:13:58 -080051#include "y2019/jevois/spi.h"
Sabina Davisc6329342019-03-01 20:44:42 -080052#include "y2019/status_light.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080053
54#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
58using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080059using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080060using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080061using ::aos::monotonic_clock;
62namespace chrono = ::std::chrono;
63using aos::make_unique;
64
65namespace y2019 {
66namespace wpilib {
67namespace {
68
69constexpr double kMaxBringupPower = 12.0;
70
71// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
72// DMA stuff and then removing the * 2.0 in *_translate.
73// The low bit is direction.
74
75// TODO(brian): Use ::std::max instead once we have C++14 so that can be
76// constexpr.
77template <typename T>
78constexpr T max(T a, T b) {
79 return (a > b) ? a : b;
80}
81
82template <typename T, typename... Rest>
83constexpr T max(T a, T b, T c, Rest... rest) {
84 return max(max(a, b), c, rest...);
85}
86
87double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080088 return ((static_cast<double>(in) /
89 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080090 (2.0 * M_PI)) *
91 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080092 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080093}
94
95double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080096 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080097 (2.0 * M_PI)) *
98 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080099 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -0800100}
101
Alex Perry5fb5ff22019-02-09 21:53:17 -0800102double elevator_pot_translate(double voltage) {
103 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -0800104 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800105}
106
107double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800108 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800109 (2 * M_PI /*radians*/);
110}
111
112double stilts_pot_translate(double voltage) {
113 return voltage * Values::kStiltsPotRatio() *
114 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
115}
116
Sabina Davisabeae332019-02-01 21:12:57 -0800117constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800118 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
119 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800120static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
121 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800122constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800123 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
124 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800125
Sabina Davisabeae332019-02-01 21:12:57 -0800126static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
127 "medium encoders are too fast");
128
129// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800130class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800131 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800132 SensorReader(::aos::EventLoop *event_loop)
133 : ::frc971::wpilib::SensorReader(event_loop) {
Sabina Davisabeae332019-02-01 21:12:57 -0800134 // Set to filter out anything shorter than 1/4 of the minimum pulse width
135 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800136 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
137 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800138 }
139
Alex Perry5fb5ff22019-02-09 21:53:17 -0800140 // Elevator
141
142 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
143 medium_encoder_filter_.Add(encoder.get());
144 elevator_encoder_.set_encoder(::std::move(encoder));
145 }
146
147 void set_elevator_absolute_pwm(
148 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
149 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
150 }
151
152 void set_elevator_potentiometer(
153 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
154 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
155 }
156
157 // Intake
158
159 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
160 medium_encoder_filter_.Add(encoder.get());
161 intake_encoder_.set_encoder(::std::move(encoder));
162 }
163
164 void set_intake_absolute_pwm(
165 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
166 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
167 }
168
169 // Wrist
170
171 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
172 medium_encoder_filter_.Add(encoder.get());
173 wrist_encoder_.set_encoder(::std::move(encoder));
174 }
175
176 void set_wrist_absolute_pwm(
177 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
178 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
179 }
180
181 void set_wrist_potentiometer(
182 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
183 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
184 }
185
186 // Stilts
187
188 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
189 medium_encoder_filter_.Add(encoder.get());
190 stilts_encoder_.set_encoder(::std::move(encoder));
191 }
192
193 void set_stilts_absolute_pwm(
194 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
195 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
196 }
197
198 void set_stilts_potentiometer(
199 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
200 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
201 }
202
Austin Schuhe2f22482019-04-13 23:05:43 -0700203 void set_platform_left_detect(
204 ::std::unique_ptr<frc::DigitalInput> platform_left_detect) {
205 platform_left_detect_ = ::std::move(platform_left_detect);
206 }
207
208 void set_platform_right_detect(
209 ::std::unique_ptr<frc::DigitalInput> platform_right_detect) {
210 platform_right_detect_ = ::std::move(platform_right_detect);
211 }
212
Austin Schuh461e1182019-02-17 14:56:44 -0800213 // Vacuum pressure sensor
214 void set_vacuum_sensor(int port) {
215 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
216 }
217
Austin Schuha9644062019-03-28 14:31:52 -0700218 // Auto mode switches.
219 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
220 autonomous_modes_.at(i) = ::std::move(sensor);
221 }
222
Sabina Davis399dbd82019-02-01 23:06:08 -0800223 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800224 {
225 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
226 drivetrain_message->left_encoder =
227 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
228 drivetrain_message->left_speed =
229 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
230
231 drivetrain_message->right_encoder =
232 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
233 drivetrain_message->right_speed = -drivetrain_velocity_translate(
234 drivetrain_right_encoder_->GetPeriod());
235
236 drivetrain_message.Send();
237 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800238 const auto values = constants::GetValues();
239
240 {
241 auto superstructure_message = superstructure_queue.position.MakeMessage();
242
243 // Elevator
244 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
245 Values::kElevatorEncoderCountsPerRevolution(),
246 Values::kElevatorEncoderRatio(), elevator_pot_translate,
247 false, values.elevator.potentiometer_offset);
248 // Intake
249 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
250 Values::kIntakeEncoderCountsPerRevolution(),
251 Values::kIntakeEncoderRatio(), false);
252
253 // Wrist
254 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
255 Values::kWristEncoderCountsPerRevolution(),
256 Values::kWristEncoderRatio(), wrist_pot_translate, false,
257 values.wrist.potentiometer_offset);
258
259 // Stilts
260 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
261 Values::kStiltsEncoderCountsPerRevolution(),
262 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
263 values.stilts.potentiometer_offset);
264
Austin Schuh461e1182019-02-17 14:56:44 -0800265 // Suction
266 constexpr float kMinVoltage = 0.5;
267 constexpr float kMaxVoltage = 2.1;
268 superstructure_message->suction_pressure =
269 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
270 (kMaxVoltage - kMinVoltage);
271
Austin Schuhe2f22482019-04-13 23:05:43 -0700272 superstructure_message->platform_left_detect =
273 !platform_left_detect_->Get();
274 superstructure_message->platform_right_detect =
275 !platform_right_detect_->Get();
276
Alex Perry5fb5ff22019-02-09 21:53:17 -0800277 superstructure_message.Send();
278 }
Austin Schuha9644062019-03-28 14:31:52 -0700279
280 {
281 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
282 auto_mode_message->mode = 0;
283 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
284 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
285 auto_mode_message->mode |= 1 << i;
286 }
287 }
288 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
289 auto_mode_message.Send();
290 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800291 }
292
293 private:
294 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
295 wrist_encoder_, stilts_encoder_;
296
Austin Schuhe2f22482019-04-13 23:05:43 -0700297 ::std::unique_ptr<frc::DigitalInput> platform_left_detect_;
298 ::std::unique_ptr<frc::DigitalInput> platform_right_detect_;
299
Austin Schuh461e1182019-02-17 14:56:44 -0800300 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
301
Austin Schuha9644062019-03-28 14:31:52 -0700302 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
303
Alex Perry5fb5ff22019-02-09 21:53:17 -0800304 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
305 // TODO(sabina): Add wrist and elevator hall effects.
306};
307
Brian Silvermanf8b75252019-02-24 16:13:58 -0800308class CameraReader {
309 public:
310 CameraReader() = default;
311 CameraReader(const CameraReader &) = delete;
312 CameraReader &operator=(const CameraReader &) = delete;
313
314 void set_spi(frc::SPI *spi) {
315 spi_ = spi;
316 spi_->SetClockRate(1e6);
317 spi_->SetChipSelectActiveHigh();
318 spi_->SetClockActiveLow();
319 spi_->SetSampleDataOnFalling();
320 // It ignores you if you try changing this...
321 spi_->SetMSBFirst();
322 }
323
Brian Silverman7ecf0672019-03-02 15:30:03 -0800324 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
325 activate_usb_ = std::move(activate_usb);
326 }
327
328 void set_activate_passthrough(
329 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
330 activate_passthrough_ = std::move(activate_passthrough);
331 }
332
Brian Silvermanf8b75252019-02-24 16:13:58 -0800333 void DoSpiTransaction() {
334 using namespace frc971::jevois;
335 RoborioToTeensy to_teensy{};
336 to_teensy.realtime_now = aos::realtime_clock::now();
Austin Schuh4e2629d2019-03-28 14:44:37 -0700337 camera_log.FetchLatest();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800338 if (activate_usb_ && !activate_usb_->Get()) {
339 to_teensy.camera_command = CameraCommand::kUsb;
340 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
341 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
Austin Schuh4e2629d2019-03-28 14:44:37 -0700342 } else if (camera_log.get() && camera_log->log) {
343 to_teensy.camera_command = CameraCommand::kLog;
Brian Silverman7ecf0672019-03-02 15:30:03 -0800344 } else {
345 to_teensy.camera_command = CameraCommand::kNormal;
346 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800347
348 std::array<char, spi_transfer_size() + 1> to_send{};
349 {
350 const auto to_send_data =
351 gsl::make_span(to_send).last<spi_transfer_size()>();
352 const auto encoded = SpiPackToTeensy(to_teensy);
353 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
354 }
355 rx_clearer_.ClearRxFifo();
356 // First, send recieve a dummy byte because the Teensy can't control what it
357 // sends for the first byte.
358 std::array<char, spi_transfer_size() + 1> to_receive;
359 DoTransaction(to_send, to_receive);
360 const auto unpacked = SpiUnpackToRoborio(
361 gsl::make_span(to_receive).last(spi_transfer_size()));
362 if (!unpacked) {
363 LOG(INFO, "Decoding SPI data failed\n");
364 return;
365 }
366
Brian Silvermanc41fb862019-03-02 21:14:46 -0800367 const auto now = aos::monotonic_clock::now();
368 for (const auto &received : unpacked->frames) {
369 auto to_send = control_loops::drivetrain::camera_frames.MakeMessage();
James Kuszmaule08f04e2019-05-01 21:46:50 -0500370 // Add an extra 10ms delay to account for unmodeled delays that Austin
371 // thinks exists.
Brian Silvermanc41fb862019-03-02 21:14:46 -0800372 to_send->timestamp =
James Kuszmaule08f04e2019-05-01 21:46:50 -0500373 std::chrono::nanoseconds(
374 (now - received.age - ::std::chrono::milliseconds(10))
375 .time_since_epoch()).count();
Brian Silvermanc41fb862019-03-02 21:14:46 -0800376 to_send->num_targets = received.targets.size();
377 for (size_t i = 0; i < received.targets.size(); ++i) {
378 to_send->targets[i].distance = received.targets[i].distance;
379 to_send->targets[i].height = received.targets[i].height;
380 to_send->targets[i].heading = received.targets[i].heading;
381 to_send->targets[i].skew = received.targets[i].skew;
382 }
383 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800384 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800385 to_send.Send();
386 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800387
388 if (dummy_spi_) {
389 uint8_t dummy_send, dummy_receive;
390 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
391 }
392 }
393
394 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
395 CHECK_EQ(to_send.size(), to_receive.size());
396 const auto result = spi_->Transaction(
397 reinterpret_cast<uint8_t *>(to_send.data()),
398 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
399 if (result == to_send.size()) {
400 return;
401 }
402 if (result == -1) {
403 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
404 return;
405 }
406 LOG(FATAL, "SPI::Transaction returned something weird\n");
407 }
408
409 void SetDummySPI(frc::SPI::Port port) {
410 dummy_spi_.reset(new frc::SPI(port));
411 // Pick the same settings here in case the roboRIO decides to try something
412 // stupid when switching.
413 if (dummy_spi_) {
414 dummy_spi_->SetClockRate(1e5);
415 dummy_spi_->SetChipSelectActiveLow();
416 dummy_spi_->SetClockActiveLow();
417 dummy_spi_->SetSampleDataOnFalling();
418 dummy_spi_->SetMSBFirst();
419 }
420 }
421
422 private:
423 frc::SPI *spi_ = nullptr;
424 ::std::unique_ptr<frc::SPI> dummy_spi_;
425
Brian Silverman7ecf0672019-03-02 15:30:03 -0800426 std::unique_ptr<frc::DigitalInput> activate_usb_;
427 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
428
Brian Silvermanf8b75252019-02-24 16:13:58 -0800429 frc971::wpilib::SpiRxClearer rx_clearer_;
430};
431
Alex Perry5fb5ff22019-02-09 21:53:17 -0800432class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
433 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800434 SuperstructureWriter(::aos::EventLoop *event_loop)
435 : ::frc971::wpilib::LoopOutputHandler(event_loop),
436 robot_state_fetcher_(
437 event_loop->MakeFetcher<::aos::RobotState>(".aos.robot_state")) {}
438
Alex Perry5fb5ff22019-02-09 21:53:17 -0800439 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
440 elevator_victor_ = ::std::move(t);
441 }
442
Austin Schuh461e1182019-02-17 14:56:44 -0800443 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
444 suction_victor_ = ::std::move(t);
445 }
446
Alex Perry5fb5ff22019-02-09 21:53:17 -0800447 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
448 intake_victor_ = ::std::move(t);
449 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800450
451 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
452 wrist_victor_ = ::std::move(t);
453 }
454
455 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
456 stilts_victor_ = ::std::move(t);
457 }
458
459 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800460 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800461 ::y2019::control_loops::superstructure::superstructure_queue.output
462 .FetchAnother();
463 }
464
Austin Schuh461e1182019-02-17 14:56:44 -0800465 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800466 auto &queue =
467 ::y2019::control_loops::superstructure::superstructure_queue.output;
468 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800469 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800470 -kMaxBringupPower,
471 kMaxBringupPower) /
472 12.0);
473
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800474 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800475 -kMaxBringupPower, kMaxBringupPower) /
476 12.0);
477
Alex Perry5fb5ff22019-02-09 21:53:17 -0800478 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
479 -kMaxBringupPower, kMaxBringupPower) /
480 12.0);
481
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800482 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800483 -kMaxBringupPower, kMaxBringupPower) /
484 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800485
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800486 robot_state_fetcher_.Fetch();
487 const double battery_voltage = robot_state_fetcher_.get()
488 ? robot_state_fetcher_->voltage_battery
489 : 12.0;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800490
491 // Throw a fast low pass filter on the battery voltage so we don't respond
492 // too fast to noise.
493 filtered_battery_voltage_ =
494 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
495
496 suction_victor_->SetSpeed(::aos::Clip(
497 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800498 }
499
Austin Schuh461e1182019-02-17 14:56:44 -0800500 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800501 LOG(WARNING, "Superstructure output too old.\n");
502
503 elevator_victor_->SetDisabled();
504 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800505 wrist_victor_->SetDisabled();
506 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800507 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800508 }
509
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800510 ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_;
511
Alex Perry5fb5ff22019-02-09 21:53:17 -0800512 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800513 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800514
515 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800516};
517
Austin Schuhc1d6f832019-02-15 23:22:17 -0800518class SolenoidWriter {
519 public:
520 SolenoidWriter()
521 : superstructure_(
522 ".y2019.control_loops.superstructure.superstructure_queue.output") {
523 }
524
Austin Schuh461e1182019-02-17 14:56:44 -0800525 void set_big_suction_cup(int index0, int index1) {
526 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
527 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800528 }
Austin Schuh461e1182019-02-17 14:56:44 -0800529 void set_small_suction_cup(int index0, int index1) {
530 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
531 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800532 }
533
534 void set_intake_roller_talon(
535 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
536 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800537 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800538 intake_rollers_talon_->EnableCurrentLimit(true);
539 }
540
541 void operator()() {
542 ::aos::SetCurrentThreadName("Solenoids");
543 ::aos::SetCurrentThreadRealtimePriority(27);
544
545 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
546 ::std::chrono::milliseconds(1));
547
548 while (run_) {
549 {
550 const int iterations = phased_loop.SleepUntilNext();
551 if (iterations != 1) {
552 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
553 }
554 }
555
556 {
557 superstructure_.FetchLatest();
558 if (superstructure_.get()) {
559 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
560
Tyler Chatow7db827f2019-02-24 00:10:13 -0800561 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
562 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
563 small_suction_cup0_->Set(superstructure_->intake_suction_top);
564 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800565
566 intake_rollers_talon_->Set(
567 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
568 ::aos::Clip(superstructure_->intake_roller_voltage,
569 -kMaxBringupPower, kMaxBringupPower) /
570 12.0);
571 }
572 }
573
574 {
575 ::frc971::wpilib::PneumaticsToLog to_log;
576
577 pcm_.Flush();
578 to_log.read_solenoids = pcm_.GetAll();
579 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
580 }
Sabina Davisc6329342019-03-01 20:44:42 -0800581
582 status_light.FetchLatest();
583 // If we don't have a light request (or it's an old one), we are borked.
584 // Flash the red light slowly.
585 if (!status_light.get() ||
586 status_light.Age() > chrono::milliseconds(100)) {
587 StatusLight color;
588 color.red = 0.0;
589 color.green = 0.0;
590 color.blue = 0.0;
591
592 ++light_flash_;
593 if (light_flash_ > 10) {
594 color.red = 0.5;
595 }
596
597 if (light_flash_ > 20) {
598 light_flash_ = 0;
599 }
600
601 LOG_STRUCT(DEBUG, "color", color);
602 SetColor(color);
603 } else {
604 LOG_STRUCT(DEBUG, "color", *status_light);
605 SetColor(*status_light);
606 }
607 }
608 }
609
610 void SetColor(const StatusLight &status_light) {
611 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
612 // it actually changes. This is pretty low priority anyways.
613 static int time_since_last_send = 0;
614 ++time_since_last_send;
615 if (time_since_last_send > 10) {
616 time_since_last_send = 0;
617 }
618 if (status_light.green != last_green_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800619 canifier_.SetLEDOutput(status_light.green,
620 ::ctre::phoenix::CANifier::LEDChannelA);
Sabina Davisc6329342019-03-01 20:44:42 -0800621 last_green_ = status_light.green;
622 }
623
624 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800625 canifier_.SetLEDOutput(status_light.blue,
626 ::ctre::phoenix::CANifier::LEDChannelC);
Sabina Davisc6329342019-03-01 20:44:42 -0800627 last_blue_ = status_light.blue;
628 }
629
630 if (status_light.red != last_red_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800631 canifier_.SetLEDOutput(status_light.red,
632 ::ctre::phoenix::CANifier::LEDChannelB);
Sabina Davisc6329342019-03-01 20:44:42 -0800633 last_red_ = status_light.red;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800634 }
635 }
636
637 void Quit() { run_ = false; }
638
639 private:
640 ::frc971::wpilib::BufferedPcm pcm_;
641
Austin Schuh461e1182019-02-17 14:56:44 -0800642 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
643 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800644
645 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
646 intake_rollers_talon_;
647
648 ::aos::Queue<
649 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
650 superstructure_;
651
Sabina Davisc6329342019-03-01 20:44:42 -0800652 ::ctre::phoenix::CANifier canifier_{0};
653
Austin Schuhc1d6f832019-02-15 23:22:17 -0800654 ::std::atomic<bool> run_{true};
Sabina Davisc6329342019-03-01 20:44:42 -0800655
656 double last_red_ = -1.0;
657 double last_green_ = -1.0;
658 double last_blue_ = -1.0;
659
660 int light_flash_ = 0;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800661};
662
Sabina Davisabeae332019-02-01 21:12:57 -0800663class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
664 public:
665 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
666 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
667 frc::Encoder::k4X);
668 }
669
670 void Run() override {
671 ::aos::InitNRT();
672 ::aos::SetCurrentThreadName("StartCompetition");
673
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800674 ::aos::ShmEventLoop event_loop;
675
676 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Sabina Davisabeae332019-02-01 21:12:57 -0800677 ::std::thread joystick_thread(::std::ref(joystick_sender));
678
679 ::frc971::wpilib::PDPFetcher pdp_fetcher;
680 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800681 SensorReader reader(&event_loop);
Sabina Davisabeae332019-02-01 21:12:57 -0800682
Sabina Davisabeae332019-02-01 21:12:57 -0800683 reader.set_drivetrain_left_encoder(make_encoder(0));
684 reader.set_drivetrain_right_encoder(make_encoder(1));
685
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800686 reader.set_elevator_encoder(make_encoder(4));
687 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
688 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800689
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800690 reader.set_wrist_encoder(make_encoder(5));
691 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
692 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800693
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800694 reader.set_intake_encoder(make_encoder(2));
695 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800696
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800697 reader.set_stilts_encoder(make_encoder(3));
698 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800699 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
700
Austin Schuh3b010bc2019-02-24 17:25:37 -0800701 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800702 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800703
Austin Schuhe2f22482019-04-13 23:05:43 -0700704 reader.set_platform_right_detect(make_unique<frc::DigitalInput>(6));
705 reader.set_platform_left_detect(make_unique<frc::DigitalInput>(7));
706
Austin Schuha9644062019-03-28 14:31:52 -0700707 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22));
708 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23));
709
Sabina Davisabeae332019-02-01 21:12:57 -0800710 ::std::thread reader_thread(::std::ref(reader));
711
Brian Silvermanf8b75252019-02-24 16:13:58 -0800712 CameraReader camera_reader;
713 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
714 camera_reader.set_spi(&camera_spi);
715 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800716 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
717 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
718 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800719
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800720 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800721 ::frc971::wpilib::ADIS16448 imu(&event_loop, frc::SPI::Port::kOnboardCS1,
Sabina Davisabeae332019-02-01 21:12:57 -0800722 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800723 imu.set_spi_idle_callback(
724 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800725 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800726 imu.set_reset(imu_reset.get());
727 ::std::thread imu_thread(::std::ref(imu));
728
729 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
730 // they are identical, as far as DrivetrainWriter is concerned, to the SP
731 // variety so all the Victors are written as SPs.
732
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800733 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davisd004fd62019-02-02 23:51:46 -0800734 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800735 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800736 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800737 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800738 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
739
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800740 SuperstructureWriter superstructure_writer(&event_loop);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800741 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800742 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800743 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800744 superstructure_writer.set_suction_victor(
745 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800746 superstructure_writer.set_intake_victor(
747 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800748 superstructure_writer.set_wrist_victor(
749 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
750 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800751 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800752
753 ::std::thread superstructure_writer_thread(
754 ::std::ref(superstructure_writer));
755
Austin Schuhc1d6f832019-02-15 23:22:17 -0800756 SolenoidWriter solenoid_writer;
757 solenoid_writer.set_intake_roller_talon(
758 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800759 solenoid_writer.set_big_suction_cup(0, 1);
760 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800761
762 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
763
Sabina Davisabeae332019-02-01 21:12:57 -0800764 // Wait forever. Not much else to do...
765 while (true) {
766 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
767 if (r != 0) {
768 PLOG(WARNING, "infinite select failed");
769 } else {
770 PLOG(WARNING, "infinite select succeeded??\n");
771 }
772 }
773
774 LOG(ERROR, "Exiting WPILibRobot\n");
775
Austin Schuhc1d6f832019-02-15 23:22:17 -0800776 solenoid_writer.Quit();
777 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800778 joystick_sender.Quit();
779 joystick_thread.join();
780 pdp_fetcher.Quit();
781 pdp_fetcher_thread.join();
782 reader.Quit();
783 reader_thread.join();
784 imu.Quit();
785 imu_thread.join();
786
787 drivetrain_writer.Quit();
788 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800789 superstructure_writer.Quit();
790 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800791
792 ::aos::Cleanup();
793 }
794};
795
796} // namespace
797} // namespace wpilib
798} // namespace y2019
799
800AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);