blob: dec9b9c32669ae298e75fdcdabaa6f41e270ea83 [file] [log] [blame]
Sabina Davisabeae332019-02-01 21:12:57 -08001#include <inttypes.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include <array>
7#include <chrono>
8#include <cmath>
9#include <functional>
10#include <mutex>
11#include <thread>
12
13#include "frc971/wpilib/ahal/AnalogInput.h"
14#include "frc971/wpilib/ahal/Counter.h"
15#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
16#include "frc971/wpilib/ahal/DriverStation.h"
17#include "frc971/wpilib/ahal/Encoder.h"
18#include "frc971/wpilib/ahal/VictorSP.h"
Sabina Davisc6329342019-03-01 20:44:42 -080019#include "ctre/phoenix/CANifier.h"
Sabina Davisabeae332019-02-01 21:12:57 -080020#undef ERROR
21
22#include "aos/commonmath.h"
Austin Schuh8a633d52019-05-12 15:04:01 -070023#include "aos/events/event-loop.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080024#include "aos/events/shm-event-loop.h"
Sabina Davisabeae332019-02-01 21:12:57 -080025#include "aos/init.h"
26#include "aos/logging/logging.h"
27#include "aos/logging/queue_logging.h"
28#include "aos/make_unique.h"
Austin Schuhc2ee66b2019-02-19 13:37:46 -080029#include "aos/robot_state/robot_state.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080030#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080031#include "aos/util/log_interval.h"
32#include "aos/util/phased_loop.h"
33#include "aos/util/wrapping_counter.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080034#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
Sabina Davisabeae332019-02-01 21:12:57 -080035#include "frc971/autonomous/auto.q.h"
36#include "frc971/control_loops/drivetrain/drivetrain.q.h"
37#include "frc971/wpilib/ADIS16448.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080038#include "frc971/wpilib/buffered_pcm.h"
39#include "frc971/wpilib/buffered_solenoid.h"
Sabina Davisabeae332019-02-01 21:12:57 -080040#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080041#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080042#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080043#include "frc971/wpilib/joystick_sender.h"
44#include "frc971/wpilib/logging.q.h"
45#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/pdp_fetcher.h"
Sabina Davisadc58542019-02-01 22:23:00 -080047#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080048#include "frc971/wpilib/wpilib_robot_base.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080049#include "y2019/constants.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080050#include "y2019/control_loops/drivetrain/camera.q.h"
Alex Perry5fb5ff22019-02-09 21:53:17 -080051#include "y2019/control_loops/superstructure/superstructure.q.h"
Brian Silvermanf8b75252019-02-24 16:13:58 -080052#include "y2019/jevois/spi.h"
Sabina Davisc6329342019-03-01 20:44:42 -080053#include "y2019/status_light.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080054
55#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
59using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080060using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080061using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080062using ::aos::monotonic_clock;
63namespace chrono = ::std::chrono;
64using aos::make_unique;
65
66namespace y2019 {
67namespace wpilib {
68namespace {
69
70constexpr double kMaxBringupPower = 12.0;
71
72// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
73// DMA stuff and then removing the * 2.0 in *_translate.
74// The low bit is direction.
75
76// TODO(brian): Use ::std::max instead once we have C++14 so that can be
77// constexpr.
78template <typename T>
79constexpr T max(T a, T b) {
80 return (a > b) ? a : b;
81}
82
83template <typename T, typename... Rest>
84constexpr T max(T a, T b, T c, Rest... rest) {
85 return max(max(a, b), c, rest...);
86}
87
88double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080089 return ((static_cast<double>(in) /
90 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080091 (2.0 * M_PI)) *
92 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080093 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080094}
95
96double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080097 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080098 (2.0 * M_PI)) *
99 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -0800100 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -0800101}
102
Alex Perry5fb5ff22019-02-09 21:53:17 -0800103double elevator_pot_translate(double voltage) {
104 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -0800105 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800106}
107
108double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800109 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800110 (2 * M_PI /*radians*/);
111}
112
113double stilts_pot_translate(double voltage) {
114 return voltage * Values::kStiltsPotRatio() *
115 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
116}
117
Sabina Davisabeae332019-02-01 21:12:57 -0800118constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800119 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
120 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800121static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
122 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800123constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800124 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
125 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800126
Sabina Davisabeae332019-02-01 21:12:57 -0800127static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
128 "medium encoders are too fast");
129
130// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800131class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800132 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800133 SensorReader(::aos::EventLoop *event_loop)
134 : ::frc971::wpilib::SensorReader(event_loop) {
Sabina Davisabeae332019-02-01 21:12:57 -0800135 // Set to filter out anything shorter than 1/4 of the minimum pulse width
136 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800137 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
138 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800139 }
140
Alex Perry5fb5ff22019-02-09 21:53:17 -0800141 // Elevator
142
143 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
144 medium_encoder_filter_.Add(encoder.get());
145 elevator_encoder_.set_encoder(::std::move(encoder));
146 }
147
148 void set_elevator_absolute_pwm(
149 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
150 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
151 }
152
153 void set_elevator_potentiometer(
154 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
155 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
156 }
157
158 // Intake
159
160 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
161 medium_encoder_filter_.Add(encoder.get());
162 intake_encoder_.set_encoder(::std::move(encoder));
163 }
164
165 void set_intake_absolute_pwm(
166 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
167 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
168 }
169
170 // Wrist
171
172 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
173 medium_encoder_filter_.Add(encoder.get());
174 wrist_encoder_.set_encoder(::std::move(encoder));
175 }
176
177 void set_wrist_absolute_pwm(
178 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
179 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
180 }
181
182 void set_wrist_potentiometer(
183 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
184 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
185 }
186
187 // Stilts
188
189 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
190 medium_encoder_filter_.Add(encoder.get());
191 stilts_encoder_.set_encoder(::std::move(encoder));
192 }
193
194 void set_stilts_absolute_pwm(
195 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
196 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
197 }
198
199 void set_stilts_potentiometer(
200 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
201 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
202 }
203
Austin Schuhe2f22482019-04-13 23:05:43 -0700204 void set_platform_left_detect(
205 ::std::unique_ptr<frc::DigitalInput> platform_left_detect) {
206 platform_left_detect_ = ::std::move(platform_left_detect);
207 }
208
209 void set_platform_right_detect(
210 ::std::unique_ptr<frc::DigitalInput> platform_right_detect) {
211 platform_right_detect_ = ::std::move(platform_right_detect);
212 }
213
Austin Schuh461e1182019-02-17 14:56:44 -0800214 // Vacuum pressure sensor
215 void set_vacuum_sensor(int port) {
216 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
217 }
218
Austin Schuha9644062019-03-28 14:31:52 -0700219 // Auto mode switches.
220 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
221 autonomous_modes_.at(i) = ::std::move(sensor);
222 }
223
Sabina Davis399dbd82019-02-01 23:06:08 -0800224 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800225 {
226 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
227 drivetrain_message->left_encoder =
228 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
229 drivetrain_message->left_speed =
230 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
231
232 drivetrain_message->right_encoder =
233 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
234 drivetrain_message->right_speed = -drivetrain_velocity_translate(
235 drivetrain_right_encoder_->GetPeriod());
236
237 drivetrain_message.Send();
238 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800239 const auto values = constants::GetValues();
240
241 {
242 auto superstructure_message = superstructure_queue.position.MakeMessage();
243
244 // Elevator
245 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
246 Values::kElevatorEncoderCountsPerRevolution(),
247 Values::kElevatorEncoderRatio(), elevator_pot_translate,
248 false, values.elevator.potentiometer_offset);
249 // Intake
250 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
251 Values::kIntakeEncoderCountsPerRevolution(),
252 Values::kIntakeEncoderRatio(), false);
253
254 // Wrist
255 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
256 Values::kWristEncoderCountsPerRevolution(),
257 Values::kWristEncoderRatio(), wrist_pot_translate, false,
258 values.wrist.potentiometer_offset);
259
260 // Stilts
261 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
262 Values::kStiltsEncoderCountsPerRevolution(),
263 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
264 values.stilts.potentiometer_offset);
265
Austin Schuh461e1182019-02-17 14:56:44 -0800266 // Suction
267 constexpr float kMinVoltage = 0.5;
268 constexpr float kMaxVoltage = 2.1;
269 superstructure_message->suction_pressure =
270 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
271 (kMaxVoltage - kMinVoltage);
272
Austin Schuhe2f22482019-04-13 23:05:43 -0700273 superstructure_message->platform_left_detect =
274 !platform_left_detect_->Get();
275 superstructure_message->platform_right_detect =
276 !platform_right_detect_->Get();
277
Alex Perry5fb5ff22019-02-09 21:53:17 -0800278 superstructure_message.Send();
279 }
Austin Schuha9644062019-03-28 14:31:52 -0700280
281 {
282 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
283 auto_mode_message->mode = 0;
284 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
285 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
286 auto_mode_message->mode |= 1 << i;
287 }
288 }
289 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
290 auto_mode_message.Send();
291 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800292 }
293
294 private:
295 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
296 wrist_encoder_, stilts_encoder_;
297
Austin Schuhe2f22482019-04-13 23:05:43 -0700298 ::std::unique_ptr<frc::DigitalInput> platform_left_detect_;
299 ::std::unique_ptr<frc::DigitalInput> platform_right_detect_;
300
Austin Schuh461e1182019-02-17 14:56:44 -0800301 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
302
Austin Schuha9644062019-03-28 14:31:52 -0700303 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
304
Alex Perry5fb5ff22019-02-09 21:53:17 -0800305 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
306 // TODO(sabina): Add wrist and elevator hall effects.
307};
308
Brian Silvermanf8b75252019-02-24 16:13:58 -0800309class CameraReader {
310 public:
Austin Schuh8a633d52019-05-12 15:04:01 -0700311 CameraReader(::aos::EventLoop *event_loop)
312 : camera_frame_sender_(
313 event_loop
314 ->MakeSender<::y2019::control_loops::drivetrain::CameraFrame>(
Austin Schuh5671a8c2019-05-19 17:01:04 -0700315 ".y2019.control_loops.drivetrain.camera_frames")),
316 camera_log_fetcher_(
317 event_loop->MakeFetcher<::y2019::CameraLog>(".y2019.camera_log")) {}
Austin Schuh8a633d52019-05-12 15:04:01 -0700318
Brian Silvermanf8b75252019-02-24 16:13:58 -0800319 CameraReader(const CameraReader &) = delete;
320 CameraReader &operator=(const CameraReader &) = delete;
321
322 void set_spi(frc::SPI *spi) {
323 spi_ = spi;
324 spi_->SetClockRate(1e6);
325 spi_->SetChipSelectActiveHigh();
326 spi_->SetClockActiveLow();
327 spi_->SetSampleDataOnFalling();
328 // It ignores you if you try changing this...
329 spi_->SetMSBFirst();
330 }
331
Brian Silverman7ecf0672019-03-02 15:30:03 -0800332 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
333 activate_usb_ = std::move(activate_usb);
334 }
335
336 void set_activate_passthrough(
337 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
338 activate_passthrough_ = std::move(activate_passthrough);
339 }
340
Brian Silvermanf8b75252019-02-24 16:13:58 -0800341 void DoSpiTransaction() {
342 using namespace frc971::jevois;
343 RoborioToTeensy to_teensy{};
344 to_teensy.realtime_now = aos::realtime_clock::now();
Austin Schuh5671a8c2019-05-19 17:01:04 -0700345 camera_log_fetcher_.Fetch();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800346 if (activate_usb_ && !activate_usb_->Get()) {
347 to_teensy.camera_command = CameraCommand::kUsb;
348 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
349 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
Austin Schuh5671a8c2019-05-19 17:01:04 -0700350 } else if (camera_log_fetcher_.get() && camera_log_fetcher_->log) {
Austin Schuh4e2629d2019-03-28 14:44:37 -0700351 to_teensy.camera_command = CameraCommand::kLog;
Brian Silverman7ecf0672019-03-02 15:30:03 -0800352 } else {
353 to_teensy.camera_command = CameraCommand::kNormal;
354 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800355
356 std::array<char, spi_transfer_size() + 1> to_send{};
357 {
358 const auto to_send_data =
359 gsl::make_span(to_send).last<spi_transfer_size()>();
360 const auto encoded = SpiPackToTeensy(to_teensy);
361 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
362 }
363 rx_clearer_.ClearRxFifo();
364 // First, send recieve a dummy byte because the Teensy can't control what it
365 // sends for the first byte.
366 std::array<char, spi_transfer_size() + 1> to_receive;
367 DoTransaction(to_send, to_receive);
368 const auto unpacked = SpiUnpackToRoborio(
369 gsl::make_span(to_receive).last(spi_transfer_size()));
370 if (!unpacked) {
371 LOG(INFO, "Decoding SPI data failed\n");
372 return;
373 }
374
Brian Silvermanc41fb862019-03-02 21:14:46 -0800375 const auto now = aos::monotonic_clock::now();
376 for (const auto &received : unpacked->frames) {
Austin Schuh8a633d52019-05-12 15:04:01 -0700377 auto to_send = camera_frame_sender_.MakeMessage();
James Kuszmaule08f04e2019-05-01 21:46:50 -0500378 // Add an extra 10ms delay to account for unmodeled delays that Austin
379 // thinks exists.
Brian Silvermanc41fb862019-03-02 21:14:46 -0800380 to_send->timestamp =
James Kuszmaule08f04e2019-05-01 21:46:50 -0500381 std::chrono::nanoseconds(
382 (now - received.age - ::std::chrono::milliseconds(10))
383 .time_since_epoch()).count();
Brian Silvermanc41fb862019-03-02 21:14:46 -0800384 to_send->num_targets = received.targets.size();
385 for (size_t i = 0; i < received.targets.size(); ++i) {
386 to_send->targets[i].distance = received.targets[i].distance;
387 to_send->targets[i].height = received.targets[i].height;
388 to_send->targets[i].heading = received.targets[i].heading;
389 to_send->targets[i].skew = received.targets[i].skew;
390 }
391 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800392 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800393 to_send.Send();
394 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800395
396 if (dummy_spi_) {
397 uint8_t dummy_send, dummy_receive;
398 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
399 }
400 }
401
402 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
403 CHECK_EQ(to_send.size(), to_receive.size());
404 const auto result = spi_->Transaction(
405 reinterpret_cast<uint8_t *>(to_send.data()),
406 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
407 if (result == to_send.size()) {
408 return;
409 }
410 if (result == -1) {
411 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
412 return;
413 }
414 LOG(FATAL, "SPI::Transaction returned something weird\n");
415 }
416
417 void SetDummySPI(frc::SPI::Port port) {
418 dummy_spi_.reset(new frc::SPI(port));
419 // Pick the same settings here in case the roboRIO decides to try something
420 // stupid when switching.
421 if (dummy_spi_) {
422 dummy_spi_->SetClockRate(1e5);
423 dummy_spi_->SetChipSelectActiveLow();
424 dummy_spi_->SetClockActiveLow();
425 dummy_spi_->SetSampleDataOnFalling();
426 dummy_spi_->SetMSBFirst();
427 }
428 }
429
430 private:
Austin Schuh8a633d52019-05-12 15:04:01 -0700431 ::aos::Sender<::y2019::control_loops::drivetrain::CameraFrame>
432 camera_frame_sender_;
Austin Schuh5671a8c2019-05-19 17:01:04 -0700433 ::aos::Fetcher<::y2019::CameraLog> camera_log_fetcher_;
Austin Schuh8a633d52019-05-12 15:04:01 -0700434
Brian Silvermanf8b75252019-02-24 16:13:58 -0800435 frc::SPI *spi_ = nullptr;
436 ::std::unique_ptr<frc::SPI> dummy_spi_;
437
Brian Silverman7ecf0672019-03-02 15:30:03 -0800438 std::unique_ptr<frc::DigitalInput> activate_usb_;
439 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
440
Brian Silvermanf8b75252019-02-24 16:13:58 -0800441 frc971::wpilib::SpiRxClearer rx_clearer_;
442};
443
Alex Perry5fb5ff22019-02-09 21:53:17 -0800444class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
445 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800446 SuperstructureWriter(::aos::EventLoop *event_loop)
447 : ::frc971::wpilib::LoopOutputHandler(event_loop),
448 robot_state_fetcher_(
449 event_loop->MakeFetcher<::aos::RobotState>(".aos.robot_state")) {}
450
Alex Perry5fb5ff22019-02-09 21:53:17 -0800451 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
452 elevator_victor_ = ::std::move(t);
453 }
454
Austin Schuh461e1182019-02-17 14:56:44 -0800455 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
456 suction_victor_ = ::std::move(t);
457 }
458
Alex Perry5fb5ff22019-02-09 21:53:17 -0800459 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
460 intake_victor_ = ::std::move(t);
461 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800462
463 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
464 wrist_victor_ = ::std::move(t);
465 }
466
467 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
468 stilts_victor_ = ::std::move(t);
469 }
470
471 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800472 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800473 ::y2019::control_loops::superstructure::superstructure_queue.output
474 .FetchAnother();
475 }
476
Austin Schuh461e1182019-02-17 14:56:44 -0800477 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800478 auto &queue =
479 ::y2019::control_loops::superstructure::superstructure_queue.output;
480 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800481 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800482 -kMaxBringupPower,
483 kMaxBringupPower) /
484 12.0);
485
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800486 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800487 -kMaxBringupPower, kMaxBringupPower) /
488 12.0);
489
Alex Perry5fb5ff22019-02-09 21:53:17 -0800490 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
491 -kMaxBringupPower, kMaxBringupPower) /
492 12.0);
493
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800494 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800495 -kMaxBringupPower, kMaxBringupPower) /
496 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800497
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800498 robot_state_fetcher_.Fetch();
499 const double battery_voltage = robot_state_fetcher_.get()
500 ? robot_state_fetcher_->voltage_battery
501 : 12.0;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800502
503 // Throw a fast low pass filter on the battery voltage so we don't respond
504 // too fast to noise.
505 filtered_battery_voltage_ =
506 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
507
508 suction_victor_->SetSpeed(::aos::Clip(
509 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800510 }
511
Austin Schuh461e1182019-02-17 14:56:44 -0800512 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800513 LOG(WARNING, "Superstructure output too old.\n");
514
515 elevator_victor_->SetDisabled();
516 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800517 wrist_victor_->SetDisabled();
518 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800519 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800520 }
521
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800522 ::aos::Fetcher<::aos::RobotState> robot_state_fetcher_;
523
Alex Perry5fb5ff22019-02-09 21:53:17 -0800524 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800525 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800526
527 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800528};
529
Austin Schuhc1d6f832019-02-15 23:22:17 -0800530class SolenoidWriter {
531 public:
Austin Schuhff973552019-05-19 16:49:28 -0700532 SolenoidWriter(::aos::EventLoop *event_loop)
533 : event_loop_(event_loop),
534 superstructure_fetcher_(event_loop->MakeFetcher<
535 ::y2019::control_loops::superstructure::
536 SuperstructureQueue::Output>(
537 ".y2019.control_loops.superstructure.superstructure_queue.output")),
538 status_light_fetcher_(event_loop->MakeFetcher<::y2019::StatusLight>(
539 ".y2019.status_light")) {}
Austin Schuhc1d6f832019-02-15 23:22:17 -0800540
Austin Schuh461e1182019-02-17 14:56:44 -0800541 void set_big_suction_cup(int index0, int index1) {
542 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
543 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800544 }
Austin Schuh461e1182019-02-17 14:56:44 -0800545 void set_small_suction_cup(int index0, int index1) {
546 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
547 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800548 }
549
550 void set_intake_roller_talon(
551 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
552 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800553 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800554 intake_rollers_talon_->EnableCurrentLimit(true);
555 }
556
557 void operator()() {
558 ::aos::SetCurrentThreadName("Solenoids");
559 ::aos::SetCurrentThreadRealtimePriority(27);
560
561 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
562 ::std::chrono::milliseconds(1));
563
564 while (run_) {
565 {
566 const int iterations = phased_loop.SleepUntilNext();
567 if (iterations != 1) {
568 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
569 }
570 }
571
572 {
Austin Schuhff973552019-05-19 16:49:28 -0700573 superstructure_fetcher_.Fetch();
574 if (superstructure_fetcher_.get()) {
575 LOG_STRUCT(DEBUG, "solenoids", *superstructure_fetcher_);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800576
Austin Schuhff973552019-05-19 16:49:28 -0700577 big_suction_cup0_->Set(
578 !superstructure_fetcher_->intake_suction_bottom);
579 big_suction_cup1_->Set(
580 !superstructure_fetcher_->intake_suction_bottom);
581 small_suction_cup0_->Set(superstructure_fetcher_->intake_suction_top);
582 small_suction_cup1_->Set(superstructure_fetcher_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800583
584 intake_rollers_talon_->Set(
585 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
Austin Schuhff973552019-05-19 16:49:28 -0700586 ::aos::Clip(superstructure_fetcher_->intake_roller_voltage,
Austin Schuhc1d6f832019-02-15 23:22:17 -0800587 -kMaxBringupPower, kMaxBringupPower) /
588 12.0);
589 }
590 }
591
592 {
593 ::frc971::wpilib::PneumaticsToLog to_log;
594
595 pcm_.Flush();
596 to_log.read_solenoids = pcm_.GetAll();
597 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
598 }
Sabina Davisc6329342019-03-01 20:44:42 -0800599
Austin Schuhff973552019-05-19 16:49:28 -0700600 status_light_fetcher_.Fetch();
Sabina Davisc6329342019-03-01 20:44:42 -0800601 // If we don't have a light request (or it's an old one), we are borked.
602 // Flash the red light slowly.
Austin Schuhff973552019-05-19 16:49:28 -0700603 if (!status_light_fetcher_.get() ||
604 status_light_fetcher_.get()->sent_time + chrono::milliseconds(100) <
605 event_loop_->monotonic_now()) {
Sabina Davisc6329342019-03-01 20:44:42 -0800606 StatusLight color;
607 color.red = 0.0;
608 color.green = 0.0;
609 color.blue = 0.0;
610
611 ++light_flash_;
612 if (light_flash_ > 10) {
613 color.red = 0.5;
614 }
615
616 if (light_flash_ > 20) {
617 light_flash_ = 0;
618 }
619
620 LOG_STRUCT(DEBUG, "color", color);
621 SetColor(color);
622 } else {
Austin Schuhff973552019-05-19 16:49:28 -0700623 LOG_STRUCT(DEBUG, "color", *status_light_fetcher_.get());
624 SetColor(*status_light_fetcher_.get());
Sabina Davisc6329342019-03-01 20:44:42 -0800625 }
626 }
627 }
628
629 void SetColor(const StatusLight &status_light) {
630 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
631 // it actually changes. This is pretty low priority anyways.
632 static int time_since_last_send = 0;
633 ++time_since_last_send;
634 if (time_since_last_send > 10) {
635 time_since_last_send = 0;
636 }
637 if (status_light.green != last_green_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800638 canifier_.SetLEDOutput(status_light.green,
639 ::ctre::phoenix::CANifier::LEDChannelA);
Sabina Davisc6329342019-03-01 20:44:42 -0800640 last_green_ = status_light.green;
641 }
642
643 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800644 canifier_.SetLEDOutput(status_light.blue,
645 ::ctre::phoenix::CANifier::LEDChannelC);
Sabina Davisc6329342019-03-01 20:44:42 -0800646 last_blue_ = status_light.blue;
647 }
648
649 if (status_light.red != last_red_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800650 canifier_.SetLEDOutput(status_light.red,
651 ::ctre::phoenix::CANifier::LEDChannelB);
Sabina Davisc6329342019-03-01 20:44:42 -0800652 last_red_ = status_light.red;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800653 }
654 }
655
656 void Quit() { run_ = false; }
657
658 private:
Austin Schuhff973552019-05-19 16:49:28 -0700659 ::aos::EventLoop *event_loop_;
660
Austin Schuhc1d6f832019-02-15 23:22:17 -0800661 ::frc971::wpilib::BufferedPcm pcm_;
662
Austin Schuh461e1182019-02-17 14:56:44 -0800663 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
664 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800665
666 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
667 intake_rollers_talon_;
668
Austin Schuhff973552019-05-19 16:49:28 -0700669 ::aos::Fetcher<
Austin Schuhc1d6f832019-02-15 23:22:17 -0800670 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
Austin Schuhff973552019-05-19 16:49:28 -0700671 superstructure_fetcher_;
672 ::aos::Fetcher<::y2019::StatusLight> status_light_fetcher_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800673
Sabina Davisc6329342019-03-01 20:44:42 -0800674 ::ctre::phoenix::CANifier canifier_{0};
675
Austin Schuhc1d6f832019-02-15 23:22:17 -0800676 ::std::atomic<bool> run_{true};
Sabina Davisc6329342019-03-01 20:44:42 -0800677
678 double last_red_ = -1.0;
679 double last_green_ = -1.0;
680 double last_blue_ = -1.0;
681
682 int light_flash_ = 0;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800683};
684
Sabina Davisabeae332019-02-01 21:12:57 -0800685class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
686 public:
687 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
688 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
689 frc::Encoder::k4X);
690 }
691
692 void Run() override {
693 ::aos::InitNRT();
694 ::aos::SetCurrentThreadName("StartCompetition");
695
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800696 ::aos::ShmEventLoop event_loop;
Austin Schuhff973552019-05-19 16:49:28 -0700697 ::aos::ShmEventLoop solenoid_event_loop;
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800698
699 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Sabina Davisabeae332019-02-01 21:12:57 -0800700 ::std::thread joystick_thread(::std::ref(joystick_sender));
701
Austin Schuh0b545432019-05-12 15:46:12 -0700702 ::frc971::wpilib::PDPFetcher pdp_fetcher(&event_loop);
Sabina Davisabeae332019-02-01 21:12:57 -0800703 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800704 SensorReader reader(&event_loop);
Sabina Davisabeae332019-02-01 21:12:57 -0800705
Sabina Davisabeae332019-02-01 21:12:57 -0800706 reader.set_drivetrain_left_encoder(make_encoder(0));
707 reader.set_drivetrain_right_encoder(make_encoder(1));
708
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800709 reader.set_elevator_encoder(make_encoder(4));
710 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
711 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800712
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800713 reader.set_wrist_encoder(make_encoder(5));
714 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
715 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800716
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800717 reader.set_intake_encoder(make_encoder(2));
718 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800719
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800720 reader.set_stilts_encoder(make_encoder(3));
721 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800722 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
723
Austin Schuh3b010bc2019-02-24 17:25:37 -0800724 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800725 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800726
Austin Schuhe2f22482019-04-13 23:05:43 -0700727 reader.set_platform_right_detect(make_unique<frc::DigitalInput>(6));
728 reader.set_platform_left_detect(make_unique<frc::DigitalInput>(7));
729
Austin Schuha9644062019-03-28 14:31:52 -0700730 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(22));
731 reader.set_autonomous_mode(0, make_unique<frc::DigitalInput>(23));
732
Sabina Davisabeae332019-02-01 21:12:57 -0800733 ::std::thread reader_thread(::std::ref(reader));
734
Austin Schuh8a633d52019-05-12 15:04:01 -0700735 CameraReader camera_reader(&event_loop);
Brian Silvermanf8b75252019-02-24 16:13:58 -0800736 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
737 camera_reader.set_spi(&camera_spi);
738 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800739 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
740 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
741 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800742
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800743 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800744 ::frc971::wpilib::ADIS16448 imu(&event_loop, frc::SPI::Port::kOnboardCS1,
Sabina Davisabeae332019-02-01 21:12:57 -0800745 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800746 imu.set_spi_idle_callback(
747 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800748 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800749 imu.set_reset(imu_reset.get());
750 ::std::thread imu_thread(::std::ref(imu));
751
752 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
753 // they are identical, as far as DrivetrainWriter is concerned, to the SP
754 // variety so all the Victors are written as SPs.
755
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800756 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davisd004fd62019-02-02 23:51:46 -0800757 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800758 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800759 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800760 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800761 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
762
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800763 SuperstructureWriter superstructure_writer(&event_loop);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800764 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800765 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800766 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800767 superstructure_writer.set_suction_victor(
768 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800769 superstructure_writer.set_intake_victor(
770 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800771 superstructure_writer.set_wrist_victor(
772 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
773 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800774 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800775
776 ::std::thread superstructure_writer_thread(
777 ::std::ref(superstructure_writer));
778
Austin Schuhff973552019-05-19 16:49:28 -0700779 SolenoidWriter solenoid_writer(&solenoid_event_loop);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800780 solenoid_writer.set_intake_roller_talon(
781 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800782 solenoid_writer.set_big_suction_cup(0, 1);
783 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800784
785 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
786
Sabina Davisabeae332019-02-01 21:12:57 -0800787 // Wait forever. Not much else to do...
788 while (true) {
789 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
790 if (r != 0) {
791 PLOG(WARNING, "infinite select failed");
792 } else {
793 PLOG(WARNING, "infinite select succeeded??\n");
794 }
795 }
796
797 LOG(ERROR, "Exiting WPILibRobot\n");
798
Austin Schuhc1d6f832019-02-15 23:22:17 -0800799 solenoid_writer.Quit();
800 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800801 joystick_sender.Quit();
802 joystick_thread.join();
803 pdp_fetcher.Quit();
804 pdp_fetcher_thread.join();
805 reader.Quit();
806 reader_thread.join();
807 imu.Quit();
808 imu_thread.join();
809
810 drivetrain_writer.Quit();
811 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800812 superstructure_writer.Quit();
813 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800814
815 ::aos::Cleanup();
816 }
817};
818
819} // namespace
820} // namespace wpilib
821} // namespace y2019
822
823AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);