blob: 95ed28e46e6e6e32e4ec04f7c7ffdbc4229c0714 [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"
23#include "aos/init.h"
24#include "aos/logging/logging.h"
25#include "aos/logging/queue_logging.h"
26#include "aos/make_unique.h"
Austin Schuhc2ee66b2019-02-19 13:37:46 -080027#include "aos/robot_state/robot_state.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080028#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080029#include "aos/util/log_interval.h"
30#include "aos/util/phased_loop.h"
31#include "aos/util/wrapping_counter.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080032#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
Sabina Davisabeae332019-02-01 21:12:57 -080033#include "frc971/autonomous/auto.q.h"
34#include "frc971/control_loops/drivetrain/drivetrain.q.h"
35#include "frc971/wpilib/ADIS16448.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080036#include "frc971/wpilib/buffered_pcm.h"
37#include "frc971/wpilib/buffered_solenoid.h"
Sabina Davisabeae332019-02-01 21:12:57 -080038#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080039#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080040#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080041#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/logging.q.h"
43#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/pdp_fetcher.h"
Sabina Davisadc58542019-02-01 22:23:00 -080045#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080046#include "frc971/wpilib/wpilib_robot_base.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080047#include "y2019/constants.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080048#include "y2019/control_loops/drivetrain/camera.q.h"
Alex Perry5fb5ff22019-02-09 21:53:17 -080049#include "y2019/control_loops/superstructure/superstructure.q.h"
Brian Silvermanf8b75252019-02-24 16:13:58 -080050#include "y2019/jevois/spi.h"
Sabina Davisc6329342019-03-01 20:44:42 -080051#include "y2019/status_light.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080052
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080058using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080059using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080060using ::aos::monotonic_clock;
61namespace chrono = ::std::chrono;
62using aos::make_unique;
63
64namespace y2019 {
65namespace wpilib {
66namespace {
67
68constexpr double kMaxBringupPower = 12.0;
69
70// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
71// DMA stuff and then removing the * 2.0 in *_translate.
72// The low bit is direction.
73
74// TODO(brian): Use ::std::max instead once we have C++14 so that can be
75// constexpr.
76template <typename T>
77constexpr T max(T a, T b) {
78 return (a > b) ? a : b;
79}
80
81template <typename T, typename... Rest>
82constexpr T max(T a, T b, T c, Rest... rest) {
83 return max(max(a, b), c, rest...);
84}
85
86double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080087 return ((static_cast<double>(in) /
88 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080089 (2.0 * M_PI)) *
90 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080091 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080092}
93
94double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080095 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080096 (2.0 * M_PI)) *
97 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080098 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080099}
100
Alex Perry5fb5ff22019-02-09 21:53:17 -0800101double elevator_pot_translate(double voltage) {
102 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -0800103 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800104}
105
106double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800107 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800108 (2 * M_PI /*radians*/);
109}
110
111double stilts_pot_translate(double voltage) {
112 return voltage * Values::kStiltsPotRatio() *
113 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
114}
115
Sabina Davisabeae332019-02-01 21:12:57 -0800116constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800117 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
118 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800119static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
120 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800121constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800122 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
123 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800124
Sabina Davisabeae332019-02-01 21:12:57 -0800125static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
126 "medium encoders are too fast");
127
128// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800129class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800130 public:
131 SensorReader() {
132 // Set to filter out anything shorter than 1/4 of the minimum pulse width
133 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800134 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
135 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800136 }
137
Alex Perry5fb5ff22019-02-09 21:53:17 -0800138 // Elevator
139
140 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
141 medium_encoder_filter_.Add(encoder.get());
142 elevator_encoder_.set_encoder(::std::move(encoder));
143 }
144
145 void set_elevator_absolute_pwm(
146 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
147 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
148 }
149
150 void set_elevator_potentiometer(
151 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
152 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
153 }
154
155 // Intake
156
157 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
158 medium_encoder_filter_.Add(encoder.get());
159 intake_encoder_.set_encoder(::std::move(encoder));
160 }
161
162 void set_intake_absolute_pwm(
163 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
164 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
165 }
166
167 // Wrist
168
169 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
170 medium_encoder_filter_.Add(encoder.get());
171 wrist_encoder_.set_encoder(::std::move(encoder));
172 }
173
174 void set_wrist_absolute_pwm(
175 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
176 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
177 }
178
179 void set_wrist_potentiometer(
180 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
181 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
182 }
183
184 // Stilts
185
186 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
187 medium_encoder_filter_.Add(encoder.get());
188 stilts_encoder_.set_encoder(::std::move(encoder));
189 }
190
191 void set_stilts_absolute_pwm(
192 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
193 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
194 }
195
196 void set_stilts_potentiometer(
197 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
198 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
199 }
200
Austin Schuh461e1182019-02-17 14:56:44 -0800201 // Vacuum pressure sensor
202 void set_vacuum_sensor(int port) {
203 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
204 }
205
Austin Schuha9644062019-03-28 14:31:52 -0700206 // Auto mode switches.
207 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
208 autonomous_modes_.at(i) = ::std::move(sensor);
209 }
210
Sabina Davis399dbd82019-02-01 23:06:08 -0800211 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800212 {
213 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
214 drivetrain_message->left_encoder =
215 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
216 drivetrain_message->left_speed =
217 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
218
219 drivetrain_message->right_encoder =
220 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
221 drivetrain_message->right_speed = -drivetrain_velocity_translate(
222 drivetrain_right_encoder_->GetPeriod());
223
224 drivetrain_message.Send();
225 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800226 const auto values = constants::GetValues();
227
228 {
229 auto superstructure_message = superstructure_queue.position.MakeMessage();
230
231 // Elevator
232 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
233 Values::kElevatorEncoderCountsPerRevolution(),
234 Values::kElevatorEncoderRatio(), elevator_pot_translate,
235 false, values.elevator.potentiometer_offset);
236 // Intake
237 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
238 Values::kIntakeEncoderCountsPerRevolution(),
239 Values::kIntakeEncoderRatio(), false);
240
241 // Wrist
242 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
243 Values::kWristEncoderCountsPerRevolution(),
244 Values::kWristEncoderRatio(), wrist_pot_translate, false,
245 values.wrist.potentiometer_offset);
246
247 // Stilts
248 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
249 Values::kStiltsEncoderCountsPerRevolution(),
250 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
251 values.stilts.potentiometer_offset);
252
Austin Schuh461e1182019-02-17 14:56:44 -0800253 // Suction
254 constexpr float kMinVoltage = 0.5;
255 constexpr float kMaxVoltage = 2.1;
256 superstructure_message->suction_pressure =
257 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
258 (kMaxVoltage - kMinVoltage);
259
Alex Perry5fb5ff22019-02-09 21:53:17 -0800260 superstructure_message.Send();
261 }
Austin Schuha9644062019-03-28 14:31:52 -0700262
263 {
264 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
265 auto_mode_message->mode = 0;
266 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
267 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
268 auto_mode_message->mode |= 1 << i;
269 }
270 }
271 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
272 auto_mode_message.Send();
273 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800274 }
275
276 private:
277 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
278 wrist_encoder_, stilts_encoder_;
279
Austin Schuh461e1182019-02-17 14:56:44 -0800280 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
281
Austin Schuha9644062019-03-28 14:31:52 -0700282 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
283
Alex Perry5fb5ff22019-02-09 21:53:17 -0800284 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
285 // TODO(sabina): Add wrist and elevator hall effects.
286};
287
Brian Silvermanf8b75252019-02-24 16:13:58 -0800288class CameraReader {
289 public:
290 CameraReader() = default;
291 CameraReader(const CameraReader &) = delete;
292 CameraReader &operator=(const CameraReader &) = delete;
293
294 void set_spi(frc::SPI *spi) {
295 spi_ = spi;
296 spi_->SetClockRate(1e6);
297 spi_->SetChipSelectActiveHigh();
298 spi_->SetClockActiveLow();
299 spi_->SetSampleDataOnFalling();
300 // It ignores you if you try changing this...
301 spi_->SetMSBFirst();
302 }
303
Brian Silverman7ecf0672019-03-02 15:30:03 -0800304 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
305 activate_usb_ = std::move(activate_usb);
306 }
307
308 void set_activate_passthrough(
309 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
310 activate_passthrough_ = std::move(activate_passthrough);
311 }
312
Brian Silvermanf8b75252019-02-24 16:13:58 -0800313 void DoSpiTransaction() {
314 using namespace frc971::jevois;
315 RoborioToTeensy to_teensy{};
316 to_teensy.realtime_now = aos::realtime_clock::now();
Austin Schuh4e2629d2019-03-28 14:44:37 -0700317 camera_log.FetchLatest();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800318 if (activate_usb_ && !activate_usb_->Get()) {
319 to_teensy.camera_command = CameraCommand::kUsb;
320 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
321 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
Austin Schuh4e2629d2019-03-28 14:44:37 -0700322 } else if (camera_log.get() && camera_log->log) {
323 to_teensy.camera_command = CameraCommand::kLog;
Brian Silverman7ecf0672019-03-02 15:30:03 -0800324 } else {
325 to_teensy.camera_command = CameraCommand::kNormal;
326 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800327
328 std::array<char, spi_transfer_size() + 1> to_send{};
329 {
330 const auto to_send_data =
331 gsl::make_span(to_send).last<spi_transfer_size()>();
332 const auto encoded = SpiPackToTeensy(to_teensy);
333 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
334 }
335 rx_clearer_.ClearRxFifo();
336 // First, send recieve a dummy byte because the Teensy can't control what it
337 // sends for the first byte.
338 std::array<char, spi_transfer_size() + 1> to_receive;
339 DoTransaction(to_send, to_receive);
340 const auto unpacked = SpiUnpackToRoborio(
341 gsl::make_span(to_receive).last(spi_transfer_size()));
342 if (!unpacked) {
343 LOG(INFO, "Decoding SPI data failed\n");
344 return;
345 }
346
Brian Silvermanc41fb862019-03-02 21:14:46 -0800347 const auto now = aos::monotonic_clock::now();
348 for (const auto &received : unpacked->frames) {
349 auto to_send = control_loops::drivetrain::camera_frames.MakeMessage();
350 to_send->timestamp =
James Kuszmaul85ffeb82019-03-03 19:41:44 -0800351 std::chrono::nanoseconds((now - received.age).time_since_epoch())
Brian Silvermanc41fb862019-03-02 21:14:46 -0800352 .count();
353 to_send->num_targets = received.targets.size();
354 for (size_t i = 0; i < received.targets.size(); ++i) {
355 to_send->targets[i].distance = received.targets[i].distance;
356 to_send->targets[i].height = received.targets[i].height;
357 to_send->targets[i].heading = received.targets[i].heading;
358 to_send->targets[i].skew = received.targets[i].skew;
359 }
360 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800361 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800362 to_send.Send();
363 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800364
365 if (dummy_spi_) {
366 uint8_t dummy_send, dummy_receive;
367 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
368 }
369 }
370
371 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
372 CHECK_EQ(to_send.size(), to_receive.size());
373 const auto result = spi_->Transaction(
374 reinterpret_cast<uint8_t *>(to_send.data()),
375 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
376 if (result == to_send.size()) {
377 return;
378 }
379 if (result == -1) {
380 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
381 return;
382 }
383 LOG(FATAL, "SPI::Transaction returned something weird\n");
384 }
385
386 void SetDummySPI(frc::SPI::Port port) {
387 dummy_spi_.reset(new frc::SPI(port));
388 // Pick the same settings here in case the roboRIO decides to try something
389 // stupid when switching.
390 if (dummy_spi_) {
391 dummy_spi_->SetClockRate(1e5);
392 dummy_spi_->SetChipSelectActiveLow();
393 dummy_spi_->SetClockActiveLow();
394 dummy_spi_->SetSampleDataOnFalling();
395 dummy_spi_->SetMSBFirst();
396 }
397 }
398
399 private:
400 frc::SPI *spi_ = nullptr;
401 ::std::unique_ptr<frc::SPI> dummy_spi_;
402
Brian Silverman7ecf0672019-03-02 15:30:03 -0800403 std::unique_ptr<frc::DigitalInput> activate_usb_;
404 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
405
Brian Silvermanf8b75252019-02-24 16:13:58 -0800406 frc971::wpilib::SpiRxClearer rx_clearer_;
407};
408
Alex Perry5fb5ff22019-02-09 21:53:17 -0800409class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
410 public:
411 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
412 elevator_victor_ = ::std::move(t);
413 }
414
Austin Schuh461e1182019-02-17 14:56:44 -0800415 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
416 suction_victor_ = ::std::move(t);
417 }
418
Alex Perry5fb5ff22019-02-09 21:53:17 -0800419 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
420 intake_victor_ = ::std::move(t);
421 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800422
423 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
424 wrist_victor_ = ::std::move(t);
425 }
426
427 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
428 stilts_victor_ = ::std::move(t);
429 }
430
431 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800432 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800433 ::y2019::control_loops::superstructure::superstructure_queue.output
434 .FetchAnother();
435 }
436
Austin Schuh461e1182019-02-17 14:56:44 -0800437 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800438 auto &queue =
439 ::y2019::control_loops::superstructure::superstructure_queue.output;
440 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800441 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800442 -kMaxBringupPower,
443 kMaxBringupPower) /
444 12.0);
445
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800446 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800447 -kMaxBringupPower, kMaxBringupPower) /
448 12.0);
449
Alex Perry5fb5ff22019-02-09 21:53:17 -0800450 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
451 -kMaxBringupPower, kMaxBringupPower) /
452 12.0);
453
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800454 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800455 -kMaxBringupPower, kMaxBringupPower) /
456 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800457
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800458 ::aos::robot_state.FetchLatest();
459 const double battery_voltage =
460 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
461
462 // Throw a fast low pass filter on the battery voltage so we don't respond
463 // too fast to noise.
464 filtered_battery_voltage_ =
465 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
466
467 suction_victor_->SetSpeed(::aos::Clip(
468 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800469 }
470
Austin Schuh461e1182019-02-17 14:56:44 -0800471 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800472 LOG(WARNING, "Superstructure output too old.\n");
473
474 elevator_victor_->SetDisabled();
475 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800476 wrist_victor_->SetDisabled();
477 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800478 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800479 }
480
481 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800482 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800483
484 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800485};
486
Austin Schuhc1d6f832019-02-15 23:22:17 -0800487class SolenoidWriter {
488 public:
489 SolenoidWriter()
490 : superstructure_(
491 ".y2019.control_loops.superstructure.superstructure_queue.output") {
492 }
493
Austin Schuh461e1182019-02-17 14:56:44 -0800494 void set_big_suction_cup(int index0, int index1) {
495 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
496 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800497 }
Austin Schuh461e1182019-02-17 14:56:44 -0800498 void set_small_suction_cup(int index0, int index1) {
499 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
500 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800501 }
502
503 void set_intake_roller_talon(
504 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
505 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800506 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800507 intake_rollers_talon_->EnableCurrentLimit(true);
508 }
509
510 void operator()() {
511 ::aos::SetCurrentThreadName("Solenoids");
512 ::aos::SetCurrentThreadRealtimePriority(27);
513
514 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
515 ::std::chrono::milliseconds(1));
516
517 while (run_) {
518 {
519 const int iterations = phased_loop.SleepUntilNext();
520 if (iterations != 1) {
521 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
522 }
523 }
524
525 {
526 superstructure_.FetchLatest();
527 if (superstructure_.get()) {
528 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
529
Tyler Chatow7db827f2019-02-24 00:10:13 -0800530 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
531 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
532 small_suction_cup0_->Set(superstructure_->intake_suction_top);
533 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800534
535 intake_rollers_talon_->Set(
536 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
537 ::aos::Clip(superstructure_->intake_roller_voltage,
538 -kMaxBringupPower, kMaxBringupPower) /
539 12.0);
540 }
541 }
542
543 {
544 ::frc971::wpilib::PneumaticsToLog to_log;
545
546 pcm_.Flush();
547 to_log.read_solenoids = pcm_.GetAll();
548 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
549 }
Sabina Davisc6329342019-03-01 20:44:42 -0800550
551 status_light.FetchLatest();
552 // If we don't have a light request (or it's an old one), we are borked.
553 // Flash the red light slowly.
554 if (!status_light.get() ||
555 status_light.Age() > chrono::milliseconds(100)) {
556 StatusLight color;
557 color.red = 0.0;
558 color.green = 0.0;
559 color.blue = 0.0;
560
561 ++light_flash_;
562 if (light_flash_ > 10) {
563 color.red = 0.5;
564 }
565
566 if (light_flash_ > 20) {
567 light_flash_ = 0;
568 }
569
570 LOG_STRUCT(DEBUG, "color", color);
571 SetColor(color);
572 } else {
573 LOG_STRUCT(DEBUG, "color", *status_light);
574 SetColor(*status_light);
575 }
576 }
577 }
578
579 void SetColor(const StatusLight &status_light) {
580 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
581 // it actually changes. This is pretty low priority anyways.
582 static int time_since_last_send = 0;
583 ++time_since_last_send;
584 if (time_since_last_send > 10) {
585 time_since_last_send = 0;
586 }
587 if (status_light.green != last_green_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800588 canifier_.SetLEDOutput(status_light.green,
589 ::ctre::phoenix::CANifier::LEDChannelA);
Sabina Davisc6329342019-03-01 20:44:42 -0800590 last_green_ = status_light.green;
591 }
592
593 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800594 canifier_.SetLEDOutput(status_light.blue,
595 ::ctre::phoenix::CANifier::LEDChannelC);
Sabina Davisc6329342019-03-01 20:44:42 -0800596 last_blue_ = status_light.blue;
597 }
598
599 if (status_light.red != last_red_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800600 canifier_.SetLEDOutput(status_light.red,
601 ::ctre::phoenix::CANifier::LEDChannelB);
Sabina Davisc6329342019-03-01 20:44:42 -0800602 last_red_ = status_light.red;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800603 }
604 }
605
606 void Quit() { run_ = false; }
607
608 private:
609 ::frc971::wpilib::BufferedPcm pcm_;
610
Austin Schuh461e1182019-02-17 14:56:44 -0800611 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
612 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800613
614 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
615 intake_rollers_talon_;
616
617 ::aos::Queue<
618 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
619 superstructure_;
620
Sabina Davisc6329342019-03-01 20:44:42 -0800621 ::ctre::phoenix::CANifier canifier_{0};
622
Austin Schuhc1d6f832019-02-15 23:22:17 -0800623 ::std::atomic<bool> run_{true};
Sabina Davisc6329342019-03-01 20:44:42 -0800624
625 double last_red_ = -1.0;
626 double last_green_ = -1.0;
627 double last_blue_ = -1.0;
628
629 int light_flash_ = 0;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800630};
631
Sabina Davisabeae332019-02-01 21:12:57 -0800632class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
633 public:
634 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
635 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
636 frc::Encoder::k4X);
637 }
638
639 void Run() override {
640 ::aos::InitNRT();
641 ::aos::SetCurrentThreadName("StartCompetition");
642
643 ::frc971::wpilib::JoystickSender joystick_sender;
644 ::std::thread joystick_thread(::std::ref(joystick_sender));
645
646 ::frc971::wpilib::PDPFetcher pdp_fetcher;
647 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
648 SensorReader reader;
649
Sabina Davisabeae332019-02-01 21:12:57 -0800650 reader.set_drivetrain_left_encoder(make_encoder(0));
651 reader.set_drivetrain_right_encoder(make_encoder(1));
652
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800653 reader.set_elevator_encoder(make_encoder(4));
654 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
655 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800656
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800657 reader.set_wrist_encoder(make_encoder(5));
658 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
659 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800660
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800661 reader.set_intake_encoder(make_encoder(2));
662 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800663
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800664 reader.set_stilts_encoder(make_encoder(3));
665 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800666 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
667
Austin Schuh3b010bc2019-02-24 17:25:37 -0800668 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800669 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800670
Austin Schuha9644062019-03-28 14:31:52 -0700671 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22));
672 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23));
673
Sabina Davisabeae332019-02-01 21:12:57 -0800674 ::std::thread reader_thread(::std::ref(reader));
675
Brian Silvermanf8b75252019-02-24 16:13:58 -0800676 CameraReader camera_reader;
677 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
678 camera_reader.set_spi(&camera_spi);
679 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800680 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
681 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
682 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800683
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800684 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800685 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
686 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800687 imu.set_spi_idle_callback(
688 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800689 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800690 imu.set_reset(imu_reset.get());
691 ::std::thread imu_thread(::std::ref(imu));
692
693 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
694 // they are identical, as far as DrivetrainWriter is concerned, to the SP
695 // variety so all the Victors are written as SPs.
696
Sabina Davisd004fd62019-02-02 23:51:46 -0800697 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
698 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800699 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800700 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800701 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800702 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
703
Alex Perry5fb5ff22019-02-09 21:53:17 -0800704 SuperstructureWriter superstructure_writer;
705 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800706 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800707 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800708 superstructure_writer.set_suction_victor(
709 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800710 superstructure_writer.set_intake_victor(
711 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800712 superstructure_writer.set_wrist_victor(
713 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
714 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800715 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800716
717 ::std::thread superstructure_writer_thread(
718 ::std::ref(superstructure_writer));
719
Austin Schuhc1d6f832019-02-15 23:22:17 -0800720 SolenoidWriter solenoid_writer;
721 solenoid_writer.set_intake_roller_talon(
722 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800723 solenoid_writer.set_big_suction_cup(0, 1);
724 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800725
726 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
727
Sabina Davisabeae332019-02-01 21:12:57 -0800728 // Wait forever. Not much else to do...
729 while (true) {
730 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
731 if (r != 0) {
732 PLOG(WARNING, "infinite select failed");
733 } else {
734 PLOG(WARNING, "infinite select succeeded??\n");
735 }
736 }
737
738 LOG(ERROR, "Exiting WPILibRobot\n");
739
Austin Schuhc1d6f832019-02-15 23:22:17 -0800740 solenoid_writer.Quit();
741 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800742 joystick_sender.Quit();
743 joystick_thread.join();
744 pdp_fetcher.Quit();
745 pdp_fetcher_thread.join();
746 reader.Quit();
747 reader_thread.join();
748 imu.Quit();
749 imu_thread.join();
750
751 drivetrain_writer.Quit();
752 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800753 superstructure_writer.Quit();
754 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800755
756 ::aos::Cleanup();
757 }
758};
759
760} // namespace
761} // namespace wpilib
762} // namespace y2019
763
764AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);