blob: 54c2ebcc2b3359d251147db11ac5a4631cd123c1 [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 Schuhe2f22482019-04-13 23:05:43 -0700201 void set_platform_left_detect(
202 ::std::unique_ptr<frc::DigitalInput> platform_left_detect) {
203 platform_left_detect_ = ::std::move(platform_left_detect);
204 }
205
206 void set_platform_right_detect(
207 ::std::unique_ptr<frc::DigitalInput> platform_right_detect) {
208 platform_right_detect_ = ::std::move(platform_right_detect);
209 }
210
Austin Schuh461e1182019-02-17 14:56:44 -0800211 // Vacuum pressure sensor
212 void set_vacuum_sensor(int port) {
213 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
214 }
215
Austin Schuha9644062019-03-28 14:31:52 -0700216 // Auto mode switches.
217 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
218 autonomous_modes_.at(i) = ::std::move(sensor);
219 }
220
Sabina Davis399dbd82019-02-01 23:06:08 -0800221 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800222 {
223 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
224 drivetrain_message->left_encoder =
225 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
226 drivetrain_message->left_speed =
227 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
228
229 drivetrain_message->right_encoder =
230 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
231 drivetrain_message->right_speed = -drivetrain_velocity_translate(
232 drivetrain_right_encoder_->GetPeriod());
233
234 drivetrain_message.Send();
235 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800236 const auto values = constants::GetValues();
237
238 {
239 auto superstructure_message = superstructure_queue.position.MakeMessage();
240
241 // Elevator
242 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
243 Values::kElevatorEncoderCountsPerRevolution(),
244 Values::kElevatorEncoderRatio(), elevator_pot_translate,
245 false, values.elevator.potentiometer_offset);
246 // Intake
247 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
248 Values::kIntakeEncoderCountsPerRevolution(),
249 Values::kIntakeEncoderRatio(), false);
250
251 // Wrist
252 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
253 Values::kWristEncoderCountsPerRevolution(),
254 Values::kWristEncoderRatio(), wrist_pot_translate, false,
255 values.wrist.potentiometer_offset);
256
257 // Stilts
258 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
259 Values::kStiltsEncoderCountsPerRevolution(),
260 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
261 values.stilts.potentiometer_offset);
262
Austin Schuh461e1182019-02-17 14:56:44 -0800263 // Suction
264 constexpr float kMinVoltage = 0.5;
265 constexpr float kMaxVoltage = 2.1;
266 superstructure_message->suction_pressure =
267 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
268 (kMaxVoltage - kMinVoltage);
269
Austin Schuhe2f22482019-04-13 23:05:43 -0700270 superstructure_message->platform_left_detect =
271 !platform_left_detect_->Get();
272 superstructure_message->platform_right_detect =
273 !platform_right_detect_->Get();
274
Alex Perry5fb5ff22019-02-09 21:53:17 -0800275 superstructure_message.Send();
276 }
Austin Schuha9644062019-03-28 14:31:52 -0700277
278 {
279 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
280 auto_mode_message->mode = 0;
281 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
282 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
283 auto_mode_message->mode |= 1 << i;
284 }
285 }
286 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
287 auto_mode_message.Send();
288 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800289 }
290
291 private:
292 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
293 wrist_encoder_, stilts_encoder_;
294
Austin Schuhe2f22482019-04-13 23:05:43 -0700295 ::std::unique_ptr<frc::DigitalInput> platform_left_detect_;
296 ::std::unique_ptr<frc::DigitalInput> platform_right_detect_;
297
Austin Schuh461e1182019-02-17 14:56:44 -0800298 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
299
Austin Schuha9644062019-03-28 14:31:52 -0700300 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
301
Alex Perry5fb5ff22019-02-09 21:53:17 -0800302 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
303 // TODO(sabina): Add wrist and elevator hall effects.
304};
305
Brian Silvermanf8b75252019-02-24 16:13:58 -0800306class CameraReader {
307 public:
308 CameraReader() = default;
309 CameraReader(const CameraReader &) = delete;
310 CameraReader &operator=(const CameraReader &) = delete;
311
312 void set_spi(frc::SPI *spi) {
313 spi_ = spi;
314 spi_->SetClockRate(1e6);
315 spi_->SetChipSelectActiveHigh();
316 spi_->SetClockActiveLow();
317 spi_->SetSampleDataOnFalling();
318 // It ignores you if you try changing this...
319 spi_->SetMSBFirst();
320 }
321
Brian Silverman7ecf0672019-03-02 15:30:03 -0800322 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
323 activate_usb_ = std::move(activate_usb);
324 }
325
326 void set_activate_passthrough(
327 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
328 activate_passthrough_ = std::move(activate_passthrough);
329 }
330
Brian Silvermanf8b75252019-02-24 16:13:58 -0800331 void DoSpiTransaction() {
332 using namespace frc971::jevois;
333 RoborioToTeensy to_teensy{};
334 to_teensy.realtime_now = aos::realtime_clock::now();
Austin Schuh4e2629d2019-03-28 14:44:37 -0700335 camera_log.FetchLatest();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800336 if (activate_usb_ && !activate_usb_->Get()) {
337 to_teensy.camera_command = CameraCommand::kUsb;
338 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
339 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
Austin Schuh4e2629d2019-03-28 14:44:37 -0700340 } else if (camera_log.get() && camera_log->log) {
341 to_teensy.camera_command = CameraCommand::kLog;
Brian Silverman7ecf0672019-03-02 15:30:03 -0800342 } else {
343 to_teensy.camera_command = CameraCommand::kNormal;
344 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800345
346 std::array<char, spi_transfer_size() + 1> to_send{};
347 {
348 const auto to_send_data =
349 gsl::make_span(to_send).last<spi_transfer_size()>();
350 const auto encoded = SpiPackToTeensy(to_teensy);
351 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
352 }
353 rx_clearer_.ClearRxFifo();
354 // First, send recieve a dummy byte because the Teensy can't control what it
355 // sends for the first byte.
356 std::array<char, spi_transfer_size() + 1> to_receive;
357 DoTransaction(to_send, to_receive);
358 const auto unpacked = SpiUnpackToRoborio(
359 gsl::make_span(to_receive).last(spi_transfer_size()));
360 if (!unpacked) {
361 LOG(INFO, "Decoding SPI data failed\n");
362 return;
363 }
364
Brian Silvermanc41fb862019-03-02 21:14:46 -0800365 const auto now = aos::monotonic_clock::now();
366 for (const auto &received : unpacked->frames) {
367 auto to_send = control_loops::drivetrain::camera_frames.MakeMessage();
James Kuszmaule08f04e2019-05-01 21:46:50 -0500368 // Add an extra 10ms delay to account for unmodeled delays that Austin
369 // thinks exists.
Brian Silvermanc41fb862019-03-02 21:14:46 -0800370 to_send->timestamp =
James Kuszmaule08f04e2019-05-01 21:46:50 -0500371 std::chrono::nanoseconds(
372 (now - received.age - ::std::chrono::milliseconds(10))
373 .time_since_epoch()).count();
Brian Silvermanc41fb862019-03-02 21:14:46 -0800374 to_send->num_targets = received.targets.size();
375 for (size_t i = 0; i < received.targets.size(); ++i) {
376 to_send->targets[i].distance = received.targets[i].distance;
377 to_send->targets[i].height = received.targets[i].height;
378 to_send->targets[i].heading = received.targets[i].heading;
379 to_send->targets[i].skew = received.targets[i].skew;
380 }
381 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800382 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800383 to_send.Send();
384 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800385
386 if (dummy_spi_) {
387 uint8_t dummy_send, dummy_receive;
388 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
389 }
390 }
391
392 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
393 CHECK_EQ(to_send.size(), to_receive.size());
394 const auto result = spi_->Transaction(
395 reinterpret_cast<uint8_t *>(to_send.data()),
396 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
397 if (result == to_send.size()) {
398 return;
399 }
400 if (result == -1) {
401 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
402 return;
403 }
404 LOG(FATAL, "SPI::Transaction returned something weird\n");
405 }
406
407 void SetDummySPI(frc::SPI::Port port) {
408 dummy_spi_.reset(new frc::SPI(port));
409 // Pick the same settings here in case the roboRIO decides to try something
410 // stupid when switching.
411 if (dummy_spi_) {
412 dummy_spi_->SetClockRate(1e5);
413 dummy_spi_->SetChipSelectActiveLow();
414 dummy_spi_->SetClockActiveLow();
415 dummy_spi_->SetSampleDataOnFalling();
416 dummy_spi_->SetMSBFirst();
417 }
418 }
419
420 private:
421 frc::SPI *spi_ = nullptr;
422 ::std::unique_ptr<frc::SPI> dummy_spi_;
423
Brian Silverman7ecf0672019-03-02 15:30:03 -0800424 std::unique_ptr<frc::DigitalInput> activate_usb_;
425 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
426
Brian Silvermanf8b75252019-02-24 16:13:58 -0800427 frc971::wpilib::SpiRxClearer rx_clearer_;
428};
429
Alex Perry5fb5ff22019-02-09 21:53:17 -0800430class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
431 public:
432 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
433 elevator_victor_ = ::std::move(t);
434 }
435
Austin Schuh461e1182019-02-17 14:56:44 -0800436 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
437 suction_victor_ = ::std::move(t);
438 }
439
Alex Perry5fb5ff22019-02-09 21:53:17 -0800440 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
441 intake_victor_ = ::std::move(t);
442 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800443
444 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
445 wrist_victor_ = ::std::move(t);
446 }
447
448 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
449 stilts_victor_ = ::std::move(t);
450 }
451
452 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800453 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800454 ::y2019::control_loops::superstructure::superstructure_queue.output
455 .FetchAnother();
456 }
457
Austin Schuh461e1182019-02-17 14:56:44 -0800458 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800459 auto &queue =
460 ::y2019::control_loops::superstructure::superstructure_queue.output;
461 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800462 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800463 -kMaxBringupPower,
464 kMaxBringupPower) /
465 12.0);
466
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800467 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800468 -kMaxBringupPower, kMaxBringupPower) /
469 12.0);
470
Alex Perry5fb5ff22019-02-09 21:53:17 -0800471 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
472 -kMaxBringupPower, kMaxBringupPower) /
473 12.0);
474
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800475 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800476 -kMaxBringupPower, kMaxBringupPower) /
477 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800478
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800479 ::aos::robot_state.FetchLatest();
480 const double battery_voltage =
481 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
482
483 // Throw a fast low pass filter on the battery voltage so we don't respond
484 // too fast to noise.
485 filtered_battery_voltage_ =
486 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
487
488 suction_victor_->SetSpeed(::aos::Clip(
489 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800490 }
491
Austin Schuh461e1182019-02-17 14:56:44 -0800492 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800493 LOG(WARNING, "Superstructure output too old.\n");
494
495 elevator_victor_->SetDisabled();
496 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800497 wrist_victor_->SetDisabled();
498 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800499 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800500 }
501
502 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800503 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800504
505 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800506};
507
Austin Schuhc1d6f832019-02-15 23:22:17 -0800508class SolenoidWriter {
509 public:
510 SolenoidWriter()
511 : superstructure_(
512 ".y2019.control_loops.superstructure.superstructure_queue.output") {
513 }
514
Austin Schuh461e1182019-02-17 14:56:44 -0800515 void set_big_suction_cup(int index0, int index1) {
516 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
517 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800518 }
Austin Schuh461e1182019-02-17 14:56:44 -0800519 void set_small_suction_cup(int index0, int index1) {
520 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
521 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800522 }
523
524 void set_intake_roller_talon(
525 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
526 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800527 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800528 intake_rollers_talon_->EnableCurrentLimit(true);
529 }
530
531 void operator()() {
532 ::aos::SetCurrentThreadName("Solenoids");
533 ::aos::SetCurrentThreadRealtimePriority(27);
534
535 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
536 ::std::chrono::milliseconds(1));
537
538 while (run_) {
539 {
540 const int iterations = phased_loop.SleepUntilNext();
541 if (iterations != 1) {
542 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
543 }
544 }
545
546 {
547 superstructure_.FetchLatest();
548 if (superstructure_.get()) {
549 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
550
Tyler Chatow7db827f2019-02-24 00:10:13 -0800551 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
552 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
553 small_suction_cup0_->Set(superstructure_->intake_suction_top);
554 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800555
556 intake_rollers_talon_->Set(
557 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
558 ::aos::Clip(superstructure_->intake_roller_voltage,
559 -kMaxBringupPower, kMaxBringupPower) /
560 12.0);
561 }
562 }
563
564 {
565 ::frc971::wpilib::PneumaticsToLog to_log;
566
567 pcm_.Flush();
568 to_log.read_solenoids = pcm_.GetAll();
569 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
570 }
Sabina Davisc6329342019-03-01 20:44:42 -0800571
572 status_light.FetchLatest();
573 // If we don't have a light request (or it's an old one), we are borked.
574 // Flash the red light slowly.
575 if (!status_light.get() ||
576 status_light.Age() > chrono::milliseconds(100)) {
577 StatusLight color;
578 color.red = 0.0;
579 color.green = 0.0;
580 color.blue = 0.0;
581
582 ++light_flash_;
583 if (light_flash_ > 10) {
584 color.red = 0.5;
585 }
586
587 if (light_flash_ > 20) {
588 light_flash_ = 0;
589 }
590
591 LOG_STRUCT(DEBUG, "color", color);
592 SetColor(color);
593 } else {
594 LOG_STRUCT(DEBUG, "color", *status_light);
595 SetColor(*status_light);
596 }
597 }
598 }
599
600 void SetColor(const StatusLight &status_light) {
601 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
602 // it actually changes. This is pretty low priority anyways.
603 static int time_since_last_send = 0;
604 ++time_since_last_send;
605 if (time_since_last_send > 10) {
606 time_since_last_send = 0;
607 }
608 if (status_light.green != last_green_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800609 canifier_.SetLEDOutput(status_light.green,
610 ::ctre::phoenix::CANifier::LEDChannelA);
Sabina Davisc6329342019-03-01 20:44:42 -0800611 last_green_ = status_light.green;
612 }
613
614 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800615 canifier_.SetLEDOutput(status_light.blue,
616 ::ctre::phoenix::CANifier::LEDChannelC);
Sabina Davisc6329342019-03-01 20:44:42 -0800617 last_blue_ = status_light.blue;
618 }
619
620 if (status_light.red != last_red_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800621 canifier_.SetLEDOutput(status_light.red,
622 ::ctre::phoenix::CANifier::LEDChannelB);
Sabina Davisc6329342019-03-01 20:44:42 -0800623 last_red_ = status_light.red;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800624 }
625 }
626
627 void Quit() { run_ = false; }
628
629 private:
630 ::frc971::wpilib::BufferedPcm pcm_;
631
Austin Schuh461e1182019-02-17 14:56:44 -0800632 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
633 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800634
635 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
636 intake_rollers_talon_;
637
638 ::aos::Queue<
639 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
640 superstructure_;
641
Sabina Davisc6329342019-03-01 20:44:42 -0800642 ::ctre::phoenix::CANifier canifier_{0};
643
Austin Schuhc1d6f832019-02-15 23:22:17 -0800644 ::std::atomic<bool> run_{true};
Sabina Davisc6329342019-03-01 20:44:42 -0800645
646 double last_red_ = -1.0;
647 double last_green_ = -1.0;
648 double last_blue_ = -1.0;
649
650 int light_flash_ = 0;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800651};
652
Sabina Davisabeae332019-02-01 21:12:57 -0800653class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
654 public:
655 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
656 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
657 frc::Encoder::k4X);
658 }
659
660 void Run() override {
661 ::aos::InitNRT();
662 ::aos::SetCurrentThreadName("StartCompetition");
663
664 ::frc971::wpilib::JoystickSender joystick_sender;
665 ::std::thread joystick_thread(::std::ref(joystick_sender));
666
667 ::frc971::wpilib::PDPFetcher pdp_fetcher;
668 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
669 SensorReader reader;
670
Sabina Davisabeae332019-02-01 21:12:57 -0800671 reader.set_drivetrain_left_encoder(make_encoder(0));
672 reader.set_drivetrain_right_encoder(make_encoder(1));
673
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800674 reader.set_elevator_encoder(make_encoder(4));
675 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
676 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800677
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800678 reader.set_wrist_encoder(make_encoder(5));
679 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
680 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800681
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800682 reader.set_intake_encoder(make_encoder(2));
683 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800684
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800685 reader.set_stilts_encoder(make_encoder(3));
686 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800687 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
688
Austin Schuh3b010bc2019-02-24 17:25:37 -0800689 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800690 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800691
Austin Schuhe2f22482019-04-13 23:05:43 -0700692 reader.set_platform_right_detect(make_unique<frc::DigitalInput>(6));
693 reader.set_platform_left_detect(make_unique<frc::DigitalInput>(7));
694
Austin Schuha9644062019-03-28 14:31:52 -0700695 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22));
696 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23));
697
Sabina Davisabeae332019-02-01 21:12:57 -0800698 ::std::thread reader_thread(::std::ref(reader));
699
Brian Silvermanf8b75252019-02-24 16:13:58 -0800700 CameraReader camera_reader;
701 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
702 camera_reader.set_spi(&camera_spi);
703 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800704 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
705 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
706 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800707
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800708 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800709 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
710 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800711 imu.set_spi_idle_callback(
712 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800713 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800714 imu.set_reset(imu_reset.get());
715 ::std::thread imu_thread(::std::ref(imu));
716
717 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
718 // they are identical, as far as DrivetrainWriter is concerned, to the SP
719 // variety so all the Victors are written as SPs.
720
Sabina Davisd004fd62019-02-02 23:51:46 -0800721 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
722 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800723 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800724 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800725 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800726 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
727
Alex Perry5fb5ff22019-02-09 21:53:17 -0800728 SuperstructureWriter superstructure_writer;
729 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800730 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800731 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800732 superstructure_writer.set_suction_victor(
733 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800734 superstructure_writer.set_intake_victor(
735 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800736 superstructure_writer.set_wrist_victor(
737 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
738 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800739 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800740
741 ::std::thread superstructure_writer_thread(
742 ::std::ref(superstructure_writer));
743
Austin Schuhc1d6f832019-02-15 23:22:17 -0800744 SolenoidWriter solenoid_writer;
745 solenoid_writer.set_intake_roller_talon(
746 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800747 solenoid_writer.set_big_suction_cup(0, 1);
748 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800749
750 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
751
Sabina Davisabeae332019-02-01 21:12:57 -0800752 // Wait forever. Not much else to do...
753 while (true) {
754 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
755 if (r != 0) {
756 PLOG(WARNING, "infinite select failed");
757 } else {
758 PLOG(WARNING, "infinite select succeeded??\n");
759 }
760 }
761
762 LOG(ERROR, "Exiting WPILibRobot\n");
763
Austin Schuhc1d6f832019-02-15 23:22:17 -0800764 solenoid_writer.Quit();
765 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800766 joystick_sender.Quit();
767 joystick_thread.join();
768 pdp_fetcher.Quit();
769 pdp_fetcher_thread.join();
770 reader.Quit();
771 reader_thread.join();
772 imu.Quit();
773 imu_thread.join();
774
775 drivetrain_writer.Quit();
776 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800777 superstructure_writer.Quit();
778 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800779
780 ::aos::Cleanup();
781 }
782};
783
784} // namespace
785} // namespace wpilib
786} // namespace y2019
787
788AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);