blob: e75b8253aef64c1b876220b05045589cac3e7c2b [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();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800317 if (activate_usb_ && !activate_usb_->Get()) {
318 to_teensy.camera_command = CameraCommand::kUsb;
319 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
320 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
321 } else {
322 to_teensy.camera_command = CameraCommand::kNormal;
323 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800324
325 std::array<char, spi_transfer_size() + 1> to_send{};
326 {
327 const auto to_send_data =
328 gsl::make_span(to_send).last<spi_transfer_size()>();
329 const auto encoded = SpiPackToTeensy(to_teensy);
330 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
331 }
332 rx_clearer_.ClearRxFifo();
333 // First, send recieve a dummy byte because the Teensy can't control what it
334 // sends for the first byte.
335 std::array<char, spi_transfer_size() + 1> to_receive;
336 DoTransaction(to_send, to_receive);
337 const auto unpacked = SpiUnpackToRoborio(
338 gsl::make_span(to_receive).last(spi_transfer_size()));
339 if (!unpacked) {
340 LOG(INFO, "Decoding SPI data failed\n");
341 return;
342 }
343
Brian Silvermanc41fb862019-03-02 21:14:46 -0800344 const auto now = aos::monotonic_clock::now();
345 for (const auto &received : unpacked->frames) {
346 auto to_send = control_loops::drivetrain::camera_frames.MakeMessage();
347 to_send->timestamp =
James Kuszmaul85ffeb82019-03-03 19:41:44 -0800348 std::chrono::nanoseconds((now - received.age).time_since_epoch())
Brian Silvermanc41fb862019-03-02 21:14:46 -0800349 .count();
350 to_send->num_targets = received.targets.size();
351 for (size_t i = 0; i < received.targets.size(); ++i) {
352 to_send->targets[i].distance = received.targets[i].distance;
353 to_send->targets[i].height = received.targets[i].height;
354 to_send->targets[i].heading = received.targets[i].heading;
355 to_send->targets[i].skew = received.targets[i].skew;
356 }
357 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800358 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800359 to_send.Send();
360 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800361
362 if (dummy_spi_) {
363 uint8_t dummy_send, dummy_receive;
364 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
365 }
366 }
367
368 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
369 CHECK_EQ(to_send.size(), to_receive.size());
370 const auto result = spi_->Transaction(
371 reinterpret_cast<uint8_t *>(to_send.data()),
372 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
373 if (result == to_send.size()) {
374 return;
375 }
376 if (result == -1) {
377 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
378 return;
379 }
380 LOG(FATAL, "SPI::Transaction returned something weird\n");
381 }
382
383 void SetDummySPI(frc::SPI::Port port) {
384 dummy_spi_.reset(new frc::SPI(port));
385 // Pick the same settings here in case the roboRIO decides to try something
386 // stupid when switching.
387 if (dummy_spi_) {
388 dummy_spi_->SetClockRate(1e5);
389 dummy_spi_->SetChipSelectActiveLow();
390 dummy_spi_->SetClockActiveLow();
391 dummy_spi_->SetSampleDataOnFalling();
392 dummy_spi_->SetMSBFirst();
393 }
394 }
395
396 private:
397 frc::SPI *spi_ = nullptr;
398 ::std::unique_ptr<frc::SPI> dummy_spi_;
399
Brian Silverman7ecf0672019-03-02 15:30:03 -0800400 std::unique_ptr<frc::DigitalInput> activate_usb_;
401 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
402
Brian Silvermanf8b75252019-02-24 16:13:58 -0800403 frc971::wpilib::SpiRxClearer rx_clearer_;
404};
405
Alex Perry5fb5ff22019-02-09 21:53:17 -0800406class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
407 public:
408 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
409 elevator_victor_ = ::std::move(t);
410 }
411
Austin Schuh461e1182019-02-17 14:56:44 -0800412 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
413 suction_victor_ = ::std::move(t);
414 }
415
Alex Perry5fb5ff22019-02-09 21:53:17 -0800416 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
417 intake_victor_ = ::std::move(t);
418 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800419
420 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
421 wrist_victor_ = ::std::move(t);
422 }
423
424 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
425 stilts_victor_ = ::std::move(t);
426 }
427
428 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800429 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800430 ::y2019::control_loops::superstructure::superstructure_queue.output
431 .FetchAnother();
432 }
433
Austin Schuh461e1182019-02-17 14:56:44 -0800434 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800435 auto &queue =
436 ::y2019::control_loops::superstructure::superstructure_queue.output;
437 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800438 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800439 -kMaxBringupPower,
440 kMaxBringupPower) /
441 12.0);
442
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800443 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800444 -kMaxBringupPower, kMaxBringupPower) /
445 12.0);
446
Alex Perry5fb5ff22019-02-09 21:53:17 -0800447 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
448 -kMaxBringupPower, kMaxBringupPower) /
449 12.0);
450
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800451 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800452 -kMaxBringupPower, kMaxBringupPower) /
453 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800454
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800455 ::aos::robot_state.FetchLatest();
456 const double battery_voltage =
457 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
458
459 // Throw a fast low pass filter on the battery voltage so we don't respond
460 // too fast to noise.
461 filtered_battery_voltage_ =
462 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
463
464 suction_victor_->SetSpeed(::aos::Clip(
465 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800466 }
467
Austin Schuh461e1182019-02-17 14:56:44 -0800468 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800469 LOG(WARNING, "Superstructure output too old.\n");
470
471 elevator_victor_->SetDisabled();
472 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800473 wrist_victor_->SetDisabled();
474 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800475 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800476 }
477
478 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800479 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800480
481 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800482};
483
Austin Schuhc1d6f832019-02-15 23:22:17 -0800484class SolenoidWriter {
485 public:
486 SolenoidWriter()
487 : superstructure_(
488 ".y2019.control_loops.superstructure.superstructure_queue.output") {
489 }
490
Austin Schuh461e1182019-02-17 14:56:44 -0800491 void set_big_suction_cup(int index0, int index1) {
492 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
493 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800494 }
Austin Schuh461e1182019-02-17 14:56:44 -0800495 void set_small_suction_cup(int index0, int index1) {
496 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
497 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800498 }
499
500 void set_intake_roller_talon(
501 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
502 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800503 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800504 intake_rollers_talon_->EnableCurrentLimit(true);
505 }
506
507 void operator()() {
508 ::aos::SetCurrentThreadName("Solenoids");
509 ::aos::SetCurrentThreadRealtimePriority(27);
510
511 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
512 ::std::chrono::milliseconds(1));
513
514 while (run_) {
515 {
516 const int iterations = phased_loop.SleepUntilNext();
517 if (iterations != 1) {
518 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
519 }
520 }
521
522 {
523 superstructure_.FetchLatest();
524 if (superstructure_.get()) {
525 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
526
Tyler Chatow7db827f2019-02-24 00:10:13 -0800527 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
528 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
529 small_suction_cup0_->Set(superstructure_->intake_suction_top);
530 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800531
532 intake_rollers_talon_->Set(
533 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
534 ::aos::Clip(superstructure_->intake_roller_voltage,
535 -kMaxBringupPower, kMaxBringupPower) /
536 12.0);
537 }
538 }
539
540 {
541 ::frc971::wpilib::PneumaticsToLog to_log;
542
543 pcm_.Flush();
544 to_log.read_solenoids = pcm_.GetAll();
545 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
546 }
Sabina Davisc6329342019-03-01 20:44:42 -0800547
548 status_light.FetchLatest();
549 // If we don't have a light request (or it's an old one), we are borked.
550 // Flash the red light slowly.
551 if (!status_light.get() ||
552 status_light.Age() > chrono::milliseconds(100)) {
553 StatusLight color;
554 color.red = 0.0;
555 color.green = 0.0;
556 color.blue = 0.0;
557
558 ++light_flash_;
559 if (light_flash_ > 10) {
560 color.red = 0.5;
561 }
562
563 if (light_flash_ > 20) {
564 light_flash_ = 0;
565 }
566
567 LOG_STRUCT(DEBUG, "color", color);
568 SetColor(color);
569 } else {
570 LOG_STRUCT(DEBUG, "color", *status_light);
571 SetColor(*status_light);
572 }
573 }
574 }
575
576 void SetColor(const StatusLight &status_light) {
577 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
578 // it actually changes. This is pretty low priority anyways.
579 static int time_since_last_send = 0;
580 ++time_since_last_send;
581 if (time_since_last_send > 10) {
582 time_since_last_send = 0;
583 }
584 if (status_light.green != last_green_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800585 canifier_.SetLEDOutput(status_light.green,
586 ::ctre::phoenix::CANifier::LEDChannelA);
Sabina Davisc6329342019-03-01 20:44:42 -0800587 last_green_ = status_light.green;
588 }
589
590 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800591 canifier_.SetLEDOutput(status_light.blue,
592 ::ctre::phoenix::CANifier::LEDChannelC);
Sabina Davisc6329342019-03-01 20:44:42 -0800593 last_blue_ = status_light.blue;
594 }
595
596 if (status_light.red != last_red_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800597 canifier_.SetLEDOutput(status_light.red,
598 ::ctre::phoenix::CANifier::LEDChannelB);
Sabina Davisc6329342019-03-01 20:44:42 -0800599 last_red_ = status_light.red;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800600 }
601 }
602
603 void Quit() { run_ = false; }
604
605 private:
606 ::frc971::wpilib::BufferedPcm pcm_;
607
Austin Schuh461e1182019-02-17 14:56:44 -0800608 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
609 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800610
611 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
612 intake_rollers_talon_;
613
614 ::aos::Queue<
615 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
616 superstructure_;
617
Sabina Davisc6329342019-03-01 20:44:42 -0800618 ::ctre::phoenix::CANifier canifier_{0};
619
Austin Schuhc1d6f832019-02-15 23:22:17 -0800620 ::std::atomic<bool> run_{true};
Sabina Davisc6329342019-03-01 20:44:42 -0800621
622 double last_red_ = -1.0;
623 double last_green_ = -1.0;
624 double last_blue_ = -1.0;
625
626 int light_flash_ = 0;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800627};
628
Sabina Davisabeae332019-02-01 21:12:57 -0800629class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
630 public:
631 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
632 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
633 frc::Encoder::k4X);
634 }
635
636 void Run() override {
637 ::aos::InitNRT();
638 ::aos::SetCurrentThreadName("StartCompetition");
639
640 ::frc971::wpilib::JoystickSender joystick_sender;
641 ::std::thread joystick_thread(::std::ref(joystick_sender));
642
643 ::frc971::wpilib::PDPFetcher pdp_fetcher;
644 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
645 SensorReader reader;
646
Sabina Davisabeae332019-02-01 21:12:57 -0800647 reader.set_drivetrain_left_encoder(make_encoder(0));
648 reader.set_drivetrain_right_encoder(make_encoder(1));
649
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800650 reader.set_elevator_encoder(make_encoder(4));
651 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
652 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800653
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800654 reader.set_wrist_encoder(make_encoder(5));
655 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
656 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800657
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800658 reader.set_intake_encoder(make_encoder(2));
659 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800660
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800661 reader.set_stilts_encoder(make_encoder(3));
662 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800663 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
664
Austin Schuh3b010bc2019-02-24 17:25:37 -0800665 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800666 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800667
Austin Schuha9644062019-03-28 14:31:52 -0700668 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22));
669 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23));
670
Sabina Davisabeae332019-02-01 21:12:57 -0800671 ::std::thread reader_thread(::std::ref(reader));
672
Brian Silvermanf8b75252019-02-24 16:13:58 -0800673 CameraReader camera_reader;
674 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
675 camera_reader.set_spi(&camera_spi);
676 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800677 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
678 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
679 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800680
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800681 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800682 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
683 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800684 imu.set_spi_idle_callback(
685 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800686 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800687 imu.set_reset(imu_reset.get());
688 ::std::thread imu_thread(::std::ref(imu));
689
690 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
691 // they are identical, as far as DrivetrainWriter is concerned, to the SP
692 // variety so all the Victors are written as SPs.
693
Sabina Davisd004fd62019-02-02 23:51:46 -0800694 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
695 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800696 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800697 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800698 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800699 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
700
Alex Perry5fb5ff22019-02-09 21:53:17 -0800701 SuperstructureWriter superstructure_writer;
702 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800703 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800704 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800705 superstructure_writer.set_suction_victor(
706 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800707 superstructure_writer.set_intake_victor(
708 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800709 superstructure_writer.set_wrist_victor(
710 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
711 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800712 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800713
714 ::std::thread superstructure_writer_thread(
715 ::std::ref(superstructure_writer));
716
Austin Schuhc1d6f832019-02-15 23:22:17 -0800717 SolenoidWriter solenoid_writer;
718 solenoid_writer.set_intake_roller_talon(
719 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800720 solenoid_writer.set_big_suction_cup(0, 1);
721 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800722
723 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
724
Sabina Davisabeae332019-02-01 21:12:57 -0800725 // Wait forever. Not much else to do...
726 while (true) {
727 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
728 if (r != 0) {
729 PLOG(WARNING, "infinite select failed");
730 } else {
731 PLOG(WARNING, "infinite select succeeded??\n");
732 }
733 }
734
735 LOG(ERROR, "Exiting WPILibRobot\n");
736
Austin Schuhc1d6f832019-02-15 23:22:17 -0800737 solenoid_writer.Quit();
738 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800739 joystick_sender.Quit();
740 joystick_thread.join();
741 pdp_fetcher.Quit();
742 pdp_fetcher_thread.join();
743 reader.Quit();
744 reader_thread.join();
745 imu.Quit();
746 imu_thread.join();
747
748 drivetrain_writer.Quit();
749 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800750 superstructure_writer.Quit();
751 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800752
753 ::aos::Cleanup();
754 }
755};
756
757} // namespace
758} // namespace wpilib
759} // namespace y2019
760
761AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);