blob: b910a7f0f1d1e130bef7c3fec4ff65de7a37faab [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 Schuh8a633d52019-05-12 15:04:01 -070023#include "aos/events/event-loop.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080024#include "aos/events/shm-event-loop.h"
Sabina Davisabeae332019-02-01 21:12:57 -080025#include "aos/init.h"
26#include "aos/logging/logging.h"
27#include "aos/logging/queue_logging.h"
28#include "aos/make_unique.h"
Austin Schuhc2ee66b2019-02-19 13:37:46 -080029#include "aos/robot_state/robot_state.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080030#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080031#include "aos/util/log_interval.h"
32#include "aos/util/phased_loop.h"
33#include "aos/util/wrapping_counter.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080034#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
Sabina Davisabeae332019-02-01 21:12:57 -080035#include "frc971/autonomous/auto.q.h"
36#include "frc971/control_loops/drivetrain/drivetrain.q.h"
37#include "frc971/wpilib/ADIS16448.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080038#include "frc971/wpilib/buffered_pcm.h"
39#include "frc971/wpilib/buffered_solenoid.h"
Sabina Davisabeae332019-02-01 21:12:57 -080040#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080041#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080042#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080043#include "frc971/wpilib/joystick_sender.h"
44#include "frc971/wpilib/logging.q.h"
45#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/pdp_fetcher.h"
Sabina Davisadc58542019-02-01 22:23:00 -080047#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080048#include "frc971/wpilib/wpilib_robot_base.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080049#include "y2019/constants.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080050#include "y2019/control_loops/drivetrain/camera.q.h"
Alex Perry5fb5ff22019-02-09 21:53:17 -080051#include "y2019/control_loops/superstructure/superstructure.q.h"
Brian Silvermanf8b75252019-02-24 16:13:58 -080052#include "y2019/jevois/spi.h"
Sabina Davisc6329342019-03-01 20:44:42 -080053#include "y2019/status_light.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080054
55#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
59using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080060using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080061using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080062using ::aos::monotonic_clock;
63namespace chrono = ::std::chrono;
64using aos::make_unique;
65
66namespace y2019 {
67namespace wpilib {
68namespace {
69
70constexpr double kMaxBringupPower = 12.0;
71
72// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
73// DMA stuff and then removing the * 2.0 in *_translate.
74// The low bit is direction.
75
76// TODO(brian): Use ::std::max instead once we have C++14 so that can be
77// constexpr.
78template <typename T>
79constexpr T max(T a, T b) {
80 return (a > b) ? a : b;
81}
82
83template <typename T, typename... Rest>
84constexpr T max(T a, T b, T c, Rest... rest) {
85 return max(max(a, b), c, rest...);
86}
87
88double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080089 return ((static_cast<double>(in) /
90 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080091 (2.0 * M_PI)) *
92 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080093 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080094}
95
96double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080097 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080098 (2.0 * M_PI)) *
99 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -0800100 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -0800101}
102
Alex Perry5fb5ff22019-02-09 21:53:17 -0800103double elevator_pot_translate(double voltage) {
104 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -0800105 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800106}
107
108double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800109 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800110 (2 * M_PI /*radians*/);
111}
112
113double stilts_pot_translate(double voltage) {
114 return voltage * Values::kStiltsPotRatio() *
115 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
116}
117
Sabina Davisabeae332019-02-01 21:12:57 -0800118constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800119 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
120 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800121static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
122 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800123constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800124 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
125 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800126
Sabina Davisabeae332019-02-01 21:12:57 -0800127static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
128 "medium encoders are too fast");
129
130// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800131class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800132 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800133 SensorReader(::aos::EventLoop *event_loop)
134 : ::frc971::wpilib::SensorReader(event_loop) {
Sabina Davisabeae332019-02-01 21:12:57 -0800135 // Set to filter out anything shorter than 1/4 of the minimum pulse width
136 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800137 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
138 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800139 }
140
Alex Perry5fb5ff22019-02-09 21:53:17 -0800141 // Elevator
142
143 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
144 medium_encoder_filter_.Add(encoder.get());
145 elevator_encoder_.set_encoder(::std::move(encoder));
146 }
147
148 void set_elevator_absolute_pwm(
149 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
150 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
151 }
152
153 void set_elevator_potentiometer(
154 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
155 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
156 }
157
158 // Intake
159
160 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
161 medium_encoder_filter_.Add(encoder.get());
162 intake_encoder_.set_encoder(::std::move(encoder));
163 }
164
165 void set_intake_absolute_pwm(
166 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
167 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
168 }
169
170 // Wrist
171
172 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
173 medium_encoder_filter_.Add(encoder.get());
174 wrist_encoder_.set_encoder(::std::move(encoder));
175 }
176
177 void set_wrist_absolute_pwm(
178 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
179 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
180 }
181
182 void set_wrist_potentiometer(
183 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
184 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
185 }
186
187 // Stilts
188
189 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
190 medium_encoder_filter_.Add(encoder.get());
191 stilts_encoder_.set_encoder(::std::move(encoder));
192 }
193
194 void set_stilts_absolute_pwm(
195 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
196 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
197 }
198
199 void set_stilts_potentiometer(
200 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
201 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
202 }
203
Austin Schuhe2f22482019-04-13 23:05:43 -0700204 void set_platform_left_detect(
205 ::std::unique_ptr<frc::DigitalInput> platform_left_detect) {
206 platform_left_detect_ = ::std::move(platform_left_detect);
207 }
208
209 void set_platform_right_detect(
210 ::std::unique_ptr<frc::DigitalInput> platform_right_detect) {
211 platform_right_detect_ = ::std::move(platform_right_detect);
212 }
213
Austin Schuh461e1182019-02-17 14:56:44 -0800214 // Vacuum pressure sensor
215 void set_vacuum_sensor(int port) {
216 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
217 }
218
Austin Schuha9644062019-03-28 14:31:52 -0700219 // Auto mode switches.
220 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
221 autonomous_modes_.at(i) = ::std::move(sensor);
222 }
223
Sabina Davis399dbd82019-02-01 23:06:08 -0800224 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800225 {
226 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
227 drivetrain_message->left_encoder =
228 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
229 drivetrain_message->left_speed =
230 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
231
232 drivetrain_message->right_encoder =
233 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
234 drivetrain_message->right_speed = -drivetrain_velocity_translate(
235 drivetrain_right_encoder_->GetPeriod());
236
237 drivetrain_message.Send();
238 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800239 const auto values = constants::GetValues();
240
241 {
242 auto superstructure_message = superstructure_queue.position.MakeMessage();
243
244 // Elevator
245 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
246 Values::kElevatorEncoderCountsPerRevolution(),
247 Values::kElevatorEncoderRatio(), elevator_pot_translate,
248 false, values.elevator.potentiometer_offset);
249 // Intake
250 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
251 Values::kIntakeEncoderCountsPerRevolution(),
252 Values::kIntakeEncoderRatio(), false);
253
254 // Wrist
255 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
256 Values::kWristEncoderCountsPerRevolution(),
257 Values::kWristEncoderRatio(), wrist_pot_translate, false,
258 values.wrist.potentiometer_offset);
259
260 // Stilts
261 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
262 Values::kStiltsEncoderCountsPerRevolution(),
263 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
264 values.stilts.potentiometer_offset);
265
Austin Schuh461e1182019-02-17 14:56:44 -0800266 // Suction
267 constexpr float kMinVoltage = 0.5;
268 constexpr float kMaxVoltage = 2.1;
269 superstructure_message->suction_pressure =
270 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
271 (kMaxVoltage - kMinVoltage);
272
Austin Schuhe2f22482019-04-13 23:05:43 -0700273 superstructure_message->platform_left_detect =
274 !platform_left_detect_->Get();
275 superstructure_message->platform_right_detect =
276 !platform_right_detect_->Get();
277
Alex Perry5fb5ff22019-02-09 21:53:17 -0800278 superstructure_message.Send();
279 }
Austin Schuha9644062019-03-28 14:31:52 -0700280
281 {
282 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
283 auto_mode_message->mode = 0;
284 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
285 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
286 auto_mode_message->mode |= 1 << i;
287 }
288 }
289 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
290 auto_mode_message.Send();
291 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800292 }
293
294 private:
295 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
296 wrist_encoder_, stilts_encoder_;
297
Austin Schuhe2f22482019-04-13 23:05:43 -0700298 ::std::unique_ptr<frc::DigitalInput> platform_left_detect_;
299 ::std::unique_ptr<frc::DigitalInput> platform_right_detect_;
300
Austin Schuh461e1182019-02-17 14:56:44 -0800301 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
302
Austin Schuha9644062019-03-28 14:31:52 -0700303 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
304
Alex Perry5fb5ff22019-02-09 21:53:17 -0800305 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
306 // TODO(sabina): Add wrist and elevator hall effects.
307};
308
Brian Silvermanf8b75252019-02-24 16:13:58 -0800309class CameraReader {
310 public:
Austin Schuh8a633d52019-05-12 15:04:01 -0700311 CameraReader(::aos::EventLoop *event_loop)
312 : camera_frame_sender_(
313 event_loop
314 ->MakeSender<::y2019::control_loops::drivetrain::CameraFrame>(
315 ".y2019.control_loops.drivetrain.camera_frames")) {}
316
Brian Silvermanf8b75252019-02-24 16:13:58 -0800317 CameraReader(const CameraReader &) = delete;
318 CameraReader &operator=(const CameraReader &) = delete;
319
320 void set_spi(frc::SPI *spi) {
321 spi_ = spi;
322 spi_->SetClockRate(1e6);
323 spi_->SetChipSelectActiveHigh();
324 spi_->SetClockActiveLow();
325 spi_->SetSampleDataOnFalling();
326 // It ignores you if you try changing this...
327 spi_->SetMSBFirst();
328 }
329
Brian Silverman7ecf0672019-03-02 15:30:03 -0800330 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
331 activate_usb_ = std::move(activate_usb);
332 }
333
334 void set_activate_passthrough(
335 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
336 activate_passthrough_ = std::move(activate_passthrough);
337 }
338
Brian Silvermanf8b75252019-02-24 16:13:58 -0800339 void DoSpiTransaction() {
340 using namespace frc971::jevois;
341 RoborioToTeensy to_teensy{};
342 to_teensy.realtime_now = aos::realtime_clock::now();
Austin Schuh4e2629d2019-03-28 14:44:37 -0700343 camera_log.FetchLatest();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800344 if (activate_usb_ && !activate_usb_->Get()) {
345 to_teensy.camera_command = CameraCommand::kUsb;
346 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
347 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
Austin Schuh4e2629d2019-03-28 14:44:37 -0700348 } else if (camera_log.get() && camera_log->log) {
349 to_teensy.camera_command = CameraCommand::kLog;
Brian Silverman7ecf0672019-03-02 15:30:03 -0800350 } else {
351 to_teensy.camera_command = CameraCommand::kNormal;
352 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800353
354 std::array<char, spi_transfer_size() + 1> to_send{};
355 {
356 const auto to_send_data =
357 gsl::make_span(to_send).last<spi_transfer_size()>();
358 const auto encoded = SpiPackToTeensy(to_teensy);
359 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
360 }
361 rx_clearer_.ClearRxFifo();
362 // First, send recieve a dummy byte because the Teensy can't control what it
363 // sends for the first byte.
364 std::array<char, spi_transfer_size() + 1> to_receive;
365 DoTransaction(to_send, to_receive);
366 const auto unpacked = SpiUnpackToRoborio(
367 gsl::make_span(to_receive).last(spi_transfer_size()));
368 if (!unpacked) {
369 LOG(INFO, "Decoding SPI data failed\n");
370 return;
371 }
372
Brian Silvermanc41fb862019-03-02 21:14:46 -0800373 const auto now = aos::monotonic_clock::now();
374 for (const auto &received : unpacked->frames) {
Austin Schuh8a633d52019-05-12 15:04:01 -0700375 auto to_send = camera_frame_sender_.MakeMessage();
James Kuszmaule08f04e2019-05-01 21:46:50 -0500376 // Add an extra 10ms delay to account for unmodeled delays that Austin
377 // thinks exists.
Brian Silvermanc41fb862019-03-02 21:14:46 -0800378 to_send->timestamp =
James Kuszmaule08f04e2019-05-01 21:46:50 -0500379 std::chrono::nanoseconds(
380 (now - received.age - ::std::chrono::milliseconds(10))
381 .time_since_epoch()).count();
Brian Silvermanc41fb862019-03-02 21:14:46 -0800382 to_send->num_targets = received.targets.size();
383 for (size_t i = 0; i < received.targets.size(); ++i) {
384 to_send->targets[i].distance = received.targets[i].distance;
385 to_send->targets[i].height = received.targets[i].height;
386 to_send->targets[i].heading = received.targets[i].heading;
387 to_send->targets[i].skew = received.targets[i].skew;
388 }
389 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800390 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800391 to_send.Send();
392 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800393
394 if (dummy_spi_) {
395 uint8_t dummy_send, dummy_receive;
396 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
397 }
398 }
399
400 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
401 CHECK_EQ(to_send.size(), to_receive.size());
402 const auto result = spi_->Transaction(
403 reinterpret_cast<uint8_t *>(to_send.data()),
404 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
405 if (result == to_send.size()) {
406 return;
407 }
408 if (result == -1) {
409 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
410 return;
411 }
412 LOG(FATAL, "SPI::Transaction returned something weird\n");
413 }
414
415 void SetDummySPI(frc::SPI::Port port) {
416 dummy_spi_.reset(new frc::SPI(port));
417 // Pick the same settings here in case the roboRIO decides to try something
418 // stupid when switching.
419 if (dummy_spi_) {
420 dummy_spi_->SetClockRate(1e5);
421 dummy_spi_->SetChipSelectActiveLow();
422 dummy_spi_->SetClockActiveLow();
423 dummy_spi_->SetSampleDataOnFalling();
424 dummy_spi_->SetMSBFirst();
425 }
426 }
427
428 private:
Austin Schuh8a633d52019-05-12 15:04:01 -0700429 ::aos::Sender<::y2019::control_loops::drivetrain::CameraFrame>
430 camera_frame_sender_;
431
Brian Silvermanf8b75252019-02-24 16:13:58 -0800432 frc::SPI *spi_ = nullptr;
433 ::std::unique_ptr<frc::SPI> dummy_spi_;
434
Brian Silverman7ecf0672019-03-02 15:30:03 -0800435 std::unique_ptr<frc::DigitalInput> activate_usb_;
436 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
437
Brian Silvermanf8b75252019-02-24 16:13:58 -0800438 frc971::wpilib::SpiRxClearer rx_clearer_;
439};
440
Alex Perry5fb5ff22019-02-09 21:53:17 -0800441class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
442 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800443 SuperstructureWriter(::aos::EventLoop *event_loop)
444 : ::frc971::wpilib::LoopOutputHandler(event_loop),
445 robot_state_fetcher_(
446 event_loop->MakeFetcher<::aos::RobotState>(".aos.robot_state")) {}
447
Alex Perry5fb5ff22019-02-09 21:53:17 -0800448 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
449 elevator_victor_ = ::std::move(t);
450 }
451
Austin Schuh461e1182019-02-17 14:56:44 -0800452 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
453 suction_victor_ = ::std::move(t);
454 }
455
Alex Perry5fb5ff22019-02-09 21:53:17 -0800456 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
457 intake_victor_ = ::std::move(t);
458 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800459
460 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
461 wrist_victor_ = ::std::move(t);
462 }
463
464 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
465 stilts_victor_ = ::std::move(t);
466 }
467
468 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800469 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800470 ::y2019::control_loops::superstructure::superstructure_queue.output
471 .FetchAnother();
472 }
473
Austin Schuh461e1182019-02-17 14:56:44 -0800474 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800475 auto &queue =
476 ::y2019::control_loops::superstructure::superstructure_queue.output;
477 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800478 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800479 -kMaxBringupPower,
480 kMaxBringupPower) /
481 12.0);
482
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800483 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800484 -kMaxBringupPower, kMaxBringupPower) /
485 12.0);
486
Alex Perry5fb5ff22019-02-09 21:53:17 -0800487 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
488 -kMaxBringupPower, kMaxBringupPower) /
489 12.0);
490
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800491 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800492 -kMaxBringupPower, kMaxBringupPower) /
493 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800494
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800495 robot_state_fetcher_.Fetch();
496 const double battery_voltage = robot_state_fetcher_.get()
497 ? robot_state_fetcher_->voltage_battery
498 : 12.0;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800499
500 // Throw a fast low pass filter on the battery voltage so we don't respond
501 // too fast to noise.
502 filtered_battery_voltage_ =
503 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
504
505 suction_victor_->SetSpeed(::aos::Clip(
506 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800507 }
508
Austin Schuh461e1182019-02-17 14:56:44 -0800509 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800510 LOG(WARNING, "Superstructure output too old.\n");
511
512 elevator_victor_->SetDisabled();
513 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800514 wrist_victor_->SetDisabled();
515 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800516 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800517 }
518
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800519 ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_;
520
Alex Perry5fb5ff22019-02-09 21:53:17 -0800521 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800522 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800523
524 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800525};
526
Austin Schuhc1d6f832019-02-15 23:22:17 -0800527class SolenoidWriter {
528 public:
529 SolenoidWriter()
530 : superstructure_(
531 ".y2019.control_loops.superstructure.superstructure_queue.output") {
532 }
533
Austin Schuh461e1182019-02-17 14:56:44 -0800534 void set_big_suction_cup(int index0, int index1) {
535 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
536 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800537 }
Austin Schuh461e1182019-02-17 14:56:44 -0800538 void set_small_suction_cup(int index0, int index1) {
539 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
540 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800541 }
542
543 void set_intake_roller_talon(
544 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
545 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800546 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800547 intake_rollers_talon_->EnableCurrentLimit(true);
548 }
549
550 void operator()() {
551 ::aos::SetCurrentThreadName("Solenoids");
552 ::aos::SetCurrentThreadRealtimePriority(27);
553
554 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
555 ::std::chrono::milliseconds(1));
556
557 while (run_) {
558 {
559 const int iterations = phased_loop.SleepUntilNext();
560 if (iterations != 1) {
561 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
562 }
563 }
564
565 {
566 superstructure_.FetchLatest();
567 if (superstructure_.get()) {
568 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
569
Tyler Chatow7db827f2019-02-24 00:10:13 -0800570 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
571 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
572 small_suction_cup0_->Set(superstructure_->intake_suction_top);
573 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800574
575 intake_rollers_talon_->Set(
576 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
577 ::aos::Clip(superstructure_->intake_roller_voltage,
578 -kMaxBringupPower, kMaxBringupPower) /
579 12.0);
580 }
581 }
582
583 {
584 ::frc971::wpilib::PneumaticsToLog to_log;
585
586 pcm_.Flush();
587 to_log.read_solenoids = pcm_.GetAll();
588 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
589 }
Sabina Davisc6329342019-03-01 20:44:42 -0800590
591 status_light.FetchLatest();
592 // If we don't have a light request (or it's an old one), we are borked.
593 // Flash the red light slowly.
594 if (!status_light.get() ||
595 status_light.Age() > chrono::milliseconds(100)) {
596 StatusLight color;
597 color.red = 0.0;
598 color.green = 0.0;
599 color.blue = 0.0;
600
601 ++light_flash_;
602 if (light_flash_ > 10) {
603 color.red = 0.5;
604 }
605
606 if (light_flash_ > 20) {
607 light_flash_ = 0;
608 }
609
610 LOG_STRUCT(DEBUG, "color", color);
611 SetColor(color);
612 } else {
613 LOG_STRUCT(DEBUG, "color", *status_light);
614 SetColor(*status_light);
615 }
616 }
617 }
618
619 void SetColor(const StatusLight &status_light) {
620 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
621 // it actually changes. This is pretty low priority anyways.
622 static int time_since_last_send = 0;
623 ++time_since_last_send;
624 if (time_since_last_send > 10) {
625 time_since_last_send = 0;
626 }
627 if (status_light.green != last_green_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800628 canifier_.SetLEDOutput(status_light.green,
629 ::ctre::phoenix::CANifier::LEDChannelA);
Sabina Davisc6329342019-03-01 20:44:42 -0800630 last_green_ = status_light.green;
631 }
632
633 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800634 canifier_.SetLEDOutput(status_light.blue,
635 ::ctre::phoenix::CANifier::LEDChannelC);
Sabina Davisc6329342019-03-01 20:44:42 -0800636 last_blue_ = status_light.blue;
637 }
638
639 if (status_light.red != last_red_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800640 canifier_.SetLEDOutput(status_light.red,
641 ::ctre::phoenix::CANifier::LEDChannelB);
Sabina Davisc6329342019-03-01 20:44:42 -0800642 last_red_ = status_light.red;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800643 }
644 }
645
646 void Quit() { run_ = false; }
647
648 private:
649 ::frc971::wpilib::BufferedPcm pcm_;
650
Austin Schuh461e1182019-02-17 14:56:44 -0800651 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
652 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800653
654 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
655 intake_rollers_talon_;
656
657 ::aos::Queue<
658 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
659 superstructure_;
660
Sabina Davisc6329342019-03-01 20:44:42 -0800661 ::ctre::phoenix::CANifier canifier_{0};
662
Austin Schuhc1d6f832019-02-15 23:22:17 -0800663 ::std::atomic<bool> run_{true};
Sabina Davisc6329342019-03-01 20:44:42 -0800664
665 double last_red_ = -1.0;
666 double last_green_ = -1.0;
667 double last_blue_ = -1.0;
668
669 int light_flash_ = 0;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800670};
671
Sabina Davisabeae332019-02-01 21:12:57 -0800672class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
673 public:
674 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
675 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
676 frc::Encoder::k4X);
677 }
678
679 void Run() override {
680 ::aos::InitNRT();
681 ::aos::SetCurrentThreadName("StartCompetition");
682
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800683 ::aos::ShmEventLoop event_loop;
684
685 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Sabina Davisabeae332019-02-01 21:12:57 -0800686 ::std::thread joystick_thread(::std::ref(joystick_sender));
687
688 ::frc971::wpilib::PDPFetcher pdp_fetcher;
689 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800690 SensorReader reader(&event_loop);
Sabina Davisabeae332019-02-01 21:12:57 -0800691
Sabina Davisabeae332019-02-01 21:12:57 -0800692 reader.set_drivetrain_left_encoder(make_encoder(0));
693 reader.set_drivetrain_right_encoder(make_encoder(1));
694
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800695 reader.set_elevator_encoder(make_encoder(4));
696 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
697 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800698
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800699 reader.set_wrist_encoder(make_encoder(5));
700 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
701 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800702
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800703 reader.set_intake_encoder(make_encoder(2));
704 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800705
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800706 reader.set_stilts_encoder(make_encoder(3));
707 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800708 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
709
Austin Schuh3b010bc2019-02-24 17:25:37 -0800710 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800711 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800712
Austin Schuhe2f22482019-04-13 23:05:43 -0700713 reader.set_platform_right_detect(make_unique<frc::DigitalInput>(6));
714 reader.set_platform_left_detect(make_unique<frc::DigitalInput>(7));
715
Austin Schuha9644062019-03-28 14:31:52 -0700716 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22));
717 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23));
718
Sabina Davisabeae332019-02-01 21:12:57 -0800719 ::std::thread reader_thread(::std::ref(reader));
720
Austin Schuh8a633d52019-05-12 15:04:01 -0700721 CameraReader camera_reader(&event_loop);
Brian Silvermanf8b75252019-02-24 16:13:58 -0800722 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
723 camera_reader.set_spi(&camera_spi);
724 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800725 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
726 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
727 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800728
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800729 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800730 ::frc971::wpilib::ADIS16448 imu(&event_loop, frc::SPI::Port::kOnboardCS1,
Sabina Davisabeae332019-02-01 21:12:57 -0800731 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800732 imu.set_spi_idle_callback(
733 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800734 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800735 imu.set_reset(imu_reset.get());
736 ::std::thread imu_thread(::std::ref(imu));
737
738 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
739 // they are identical, as far as DrivetrainWriter is concerned, to the SP
740 // variety so all the Victors are written as SPs.
741
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800742 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davisd004fd62019-02-02 23:51:46 -0800743 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800744 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800745 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800746 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800747 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
748
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800749 SuperstructureWriter superstructure_writer(&event_loop);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800750 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800751 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800752 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800753 superstructure_writer.set_suction_victor(
754 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800755 superstructure_writer.set_intake_victor(
756 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800757 superstructure_writer.set_wrist_victor(
758 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
759 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800760 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800761
762 ::std::thread superstructure_writer_thread(
763 ::std::ref(superstructure_writer));
764
Austin Schuhc1d6f832019-02-15 23:22:17 -0800765 SolenoidWriter solenoid_writer;
766 solenoid_writer.set_intake_roller_talon(
767 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800768 solenoid_writer.set_big_suction_cup(0, 1);
769 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800770
771 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
772
Sabina Davisabeae332019-02-01 21:12:57 -0800773 // Wait forever. Not much else to do...
774 while (true) {
775 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
776 if (r != 0) {
777 PLOG(WARNING, "infinite select failed");
778 } else {
779 PLOG(WARNING, "infinite select succeeded??\n");
780 }
781 }
782
783 LOG(ERROR, "Exiting WPILibRobot\n");
784
Austin Schuhc1d6f832019-02-15 23:22:17 -0800785 solenoid_writer.Quit();
786 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800787 joystick_sender.Quit();
788 joystick_thread.join();
789 pdp_fetcher.Quit();
790 pdp_fetcher_thread.join();
791 reader.Quit();
792 reader_thread.join();
793 imu.Quit();
794 imu_thread.join();
795
796 drivetrain_writer.Quit();
797 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800798 superstructure_writer.Quit();
799 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800800
801 ::aos::Cleanup();
802 }
803};
804
805} // namespace
806} // namespace wpilib
807} // namespace y2019
808
809AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);