blob: fd9df73d229122d03b248dfe12a8c49c110fe73a [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
Sabina Davis399dbd82019-02-01 23:06:08 -0800206 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800207 {
208 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
209 drivetrain_message->left_encoder =
210 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
211 drivetrain_message->left_speed =
212 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
213
214 drivetrain_message->right_encoder =
215 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
216 drivetrain_message->right_speed = -drivetrain_velocity_translate(
217 drivetrain_right_encoder_->GetPeriod());
218
219 drivetrain_message.Send();
220 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800221 const auto values = constants::GetValues();
222
223 {
224 auto superstructure_message = superstructure_queue.position.MakeMessage();
225
226 // Elevator
227 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
228 Values::kElevatorEncoderCountsPerRevolution(),
229 Values::kElevatorEncoderRatio(), elevator_pot_translate,
230 false, values.elevator.potentiometer_offset);
231 // Intake
232 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
233 Values::kIntakeEncoderCountsPerRevolution(),
234 Values::kIntakeEncoderRatio(), false);
235
236 // Wrist
237 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
238 Values::kWristEncoderCountsPerRevolution(),
239 Values::kWristEncoderRatio(), wrist_pot_translate, false,
240 values.wrist.potentiometer_offset);
241
242 // Stilts
243 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
244 Values::kStiltsEncoderCountsPerRevolution(),
245 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
246 values.stilts.potentiometer_offset);
247
Austin Schuh461e1182019-02-17 14:56:44 -0800248 // Suction
249 constexpr float kMinVoltage = 0.5;
250 constexpr float kMaxVoltage = 2.1;
251 superstructure_message->suction_pressure =
252 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
253 (kMaxVoltage - kMinVoltage);
254
Alex Perry5fb5ff22019-02-09 21:53:17 -0800255 superstructure_message.Send();
256 }
257 }
258
259 private:
260 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
261 wrist_encoder_, stilts_encoder_;
262
Austin Schuh461e1182019-02-17 14:56:44 -0800263 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
264
Alex Perry5fb5ff22019-02-09 21:53:17 -0800265 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
266 // TODO(sabina): Add wrist and elevator hall effects.
267};
268
Brian Silvermanf8b75252019-02-24 16:13:58 -0800269class CameraReader {
270 public:
271 CameraReader() = default;
272 CameraReader(const CameraReader &) = delete;
273 CameraReader &operator=(const CameraReader &) = delete;
274
275 void set_spi(frc::SPI *spi) {
276 spi_ = spi;
277 spi_->SetClockRate(1e6);
278 spi_->SetChipSelectActiveHigh();
279 spi_->SetClockActiveLow();
280 spi_->SetSampleDataOnFalling();
281 // It ignores you if you try changing this...
282 spi_->SetMSBFirst();
283 }
284
Brian Silverman7ecf0672019-03-02 15:30:03 -0800285 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
286 activate_usb_ = std::move(activate_usb);
287 }
288
289 void set_activate_passthrough(
290 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
291 activate_passthrough_ = std::move(activate_passthrough);
292 }
293
Brian Silvermanf8b75252019-02-24 16:13:58 -0800294 void DoSpiTransaction() {
295 using namespace frc971::jevois;
296 RoborioToTeensy to_teensy{};
297 to_teensy.realtime_now = aos::realtime_clock::now();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800298 if (activate_usb_ && !activate_usb_->Get()) {
299 to_teensy.camera_command = CameraCommand::kUsb;
300 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
301 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
302 } else {
303 to_teensy.camera_command = CameraCommand::kNormal;
304 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800305
306 std::array<char, spi_transfer_size() + 1> to_send{};
307 {
308 const auto to_send_data =
309 gsl::make_span(to_send).last<spi_transfer_size()>();
310 const auto encoded = SpiPackToTeensy(to_teensy);
311 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
312 }
313 rx_clearer_.ClearRxFifo();
314 // First, send recieve a dummy byte because the Teensy can't control what it
315 // sends for the first byte.
316 std::array<char, spi_transfer_size() + 1> to_receive;
317 DoTransaction(to_send, to_receive);
318 const auto unpacked = SpiUnpackToRoborio(
319 gsl::make_span(to_receive).last(spi_transfer_size()));
320 if (!unpacked) {
321 LOG(INFO, "Decoding SPI data failed\n");
322 return;
323 }
324
Brian Silvermanc41fb862019-03-02 21:14:46 -0800325 const auto now = aos::monotonic_clock::now();
326 for (const auto &received : unpacked->frames) {
327 auto to_send = control_loops::drivetrain::camera_frames.MakeMessage();
328 to_send->timestamp =
James Kuszmaul85ffeb82019-03-03 19:41:44 -0800329 std::chrono::nanoseconds((now - received.age).time_since_epoch())
Brian Silvermanc41fb862019-03-02 21:14:46 -0800330 .count();
331 to_send->num_targets = received.targets.size();
332 for (size_t i = 0; i < received.targets.size(); ++i) {
333 to_send->targets[i].distance = received.targets[i].distance;
334 to_send->targets[i].height = received.targets[i].height;
335 to_send->targets[i].heading = received.targets[i].heading;
336 to_send->targets[i].skew = received.targets[i].skew;
337 }
338 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800339 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800340 to_send.Send();
341 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800342
343 if (dummy_spi_) {
344 uint8_t dummy_send, dummy_receive;
345 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
346 }
347 }
348
349 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
350 CHECK_EQ(to_send.size(), to_receive.size());
351 const auto result = spi_->Transaction(
352 reinterpret_cast<uint8_t *>(to_send.data()),
353 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
354 if (result == to_send.size()) {
355 return;
356 }
357 if (result == -1) {
358 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
359 return;
360 }
361 LOG(FATAL, "SPI::Transaction returned something weird\n");
362 }
363
364 void SetDummySPI(frc::SPI::Port port) {
365 dummy_spi_.reset(new frc::SPI(port));
366 // Pick the same settings here in case the roboRIO decides to try something
367 // stupid when switching.
368 if (dummy_spi_) {
369 dummy_spi_->SetClockRate(1e5);
370 dummy_spi_->SetChipSelectActiveLow();
371 dummy_spi_->SetClockActiveLow();
372 dummy_spi_->SetSampleDataOnFalling();
373 dummy_spi_->SetMSBFirst();
374 }
375 }
376
377 private:
378 frc::SPI *spi_ = nullptr;
379 ::std::unique_ptr<frc::SPI> dummy_spi_;
380
Brian Silverman7ecf0672019-03-02 15:30:03 -0800381 std::unique_ptr<frc::DigitalInput> activate_usb_;
382 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
383
Brian Silvermanf8b75252019-02-24 16:13:58 -0800384 frc971::wpilib::SpiRxClearer rx_clearer_;
385};
386
Alex Perry5fb5ff22019-02-09 21:53:17 -0800387class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
388 public:
389 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
390 elevator_victor_ = ::std::move(t);
391 }
392
Austin Schuh461e1182019-02-17 14:56:44 -0800393 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
394 suction_victor_ = ::std::move(t);
395 }
396
Alex Perry5fb5ff22019-02-09 21:53:17 -0800397 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
398 intake_victor_ = ::std::move(t);
399 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800400
401 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
402 wrist_victor_ = ::std::move(t);
403 }
404
405 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
406 stilts_victor_ = ::std::move(t);
407 }
408
409 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800410 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800411 ::y2019::control_loops::superstructure::superstructure_queue.output
412 .FetchAnother();
413 }
414
Austin Schuh461e1182019-02-17 14:56:44 -0800415 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800416 auto &queue =
417 ::y2019::control_loops::superstructure::superstructure_queue.output;
418 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800419 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800420 -kMaxBringupPower,
421 kMaxBringupPower) /
422 12.0);
423
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800424 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800425 -kMaxBringupPower, kMaxBringupPower) /
426 12.0);
427
Alex Perry5fb5ff22019-02-09 21:53:17 -0800428 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
429 -kMaxBringupPower, kMaxBringupPower) /
430 12.0);
431
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800432 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800433 -kMaxBringupPower, kMaxBringupPower) /
434 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800435
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800436 ::aos::robot_state.FetchLatest();
437 const double battery_voltage =
438 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
439
440 // Throw a fast low pass filter on the battery voltage so we don't respond
441 // too fast to noise.
442 filtered_battery_voltage_ =
443 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
444
445 suction_victor_->SetSpeed(::aos::Clip(
446 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800447 }
448
Austin Schuh461e1182019-02-17 14:56:44 -0800449 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800450 LOG(WARNING, "Superstructure output too old.\n");
451
452 elevator_victor_->SetDisabled();
453 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800454 wrist_victor_->SetDisabled();
455 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800456 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800457 }
458
459 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800460 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800461
462 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800463};
464
Austin Schuhc1d6f832019-02-15 23:22:17 -0800465class SolenoidWriter {
466 public:
467 SolenoidWriter()
468 : superstructure_(
469 ".y2019.control_loops.superstructure.superstructure_queue.output") {
470 }
471
Austin Schuh461e1182019-02-17 14:56:44 -0800472 void set_big_suction_cup(int index0, int index1) {
473 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
474 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800475 }
Austin Schuh461e1182019-02-17 14:56:44 -0800476 void set_small_suction_cup(int index0, int index1) {
477 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
478 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800479 }
480
481 void set_intake_roller_talon(
482 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
483 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800484 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800485 intake_rollers_talon_->EnableCurrentLimit(true);
486 }
487
488 void operator()() {
489 ::aos::SetCurrentThreadName("Solenoids");
490 ::aos::SetCurrentThreadRealtimePriority(27);
491
492 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
493 ::std::chrono::milliseconds(1));
494
495 while (run_) {
496 {
497 const int iterations = phased_loop.SleepUntilNext();
498 if (iterations != 1) {
499 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
500 }
501 }
502
503 {
504 superstructure_.FetchLatest();
505 if (superstructure_.get()) {
506 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
507
Tyler Chatow7db827f2019-02-24 00:10:13 -0800508 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
509 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
510 small_suction_cup0_->Set(superstructure_->intake_suction_top);
511 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800512
513 intake_rollers_talon_->Set(
514 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
515 ::aos::Clip(superstructure_->intake_roller_voltage,
516 -kMaxBringupPower, kMaxBringupPower) /
517 12.0);
518 }
519 }
520
521 {
522 ::frc971::wpilib::PneumaticsToLog to_log;
523
524 pcm_.Flush();
525 to_log.read_solenoids = pcm_.GetAll();
526 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
527 }
Sabina Davisc6329342019-03-01 20:44:42 -0800528
529 status_light.FetchLatest();
530 // If we don't have a light request (or it's an old one), we are borked.
531 // Flash the red light slowly.
532 if (!status_light.get() ||
533 status_light.Age() > chrono::milliseconds(100)) {
534 StatusLight color;
535 color.red = 0.0;
536 color.green = 0.0;
537 color.blue = 0.0;
538
539 ++light_flash_;
540 if (light_flash_ > 10) {
541 color.red = 0.5;
542 }
543
544 if (light_flash_ > 20) {
545 light_flash_ = 0;
546 }
547
548 LOG_STRUCT(DEBUG, "color", color);
549 SetColor(color);
550 } else {
551 LOG_STRUCT(DEBUG, "color", *status_light);
552 SetColor(*status_light);
553 }
554 }
555 }
556
557 void SetColor(const StatusLight &status_light) {
558 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
559 // it actually changes. This is pretty low priority anyways.
560 static int time_since_last_send = 0;
561 ++time_since_last_send;
562 if (time_since_last_send > 10) {
563 time_since_last_send = 0;
564 }
565 if (status_light.green != last_green_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800566 canifier_.SetLEDOutput(status_light.green,
567 ::ctre::phoenix::CANifier::LEDChannelA);
Sabina Davisc6329342019-03-01 20:44:42 -0800568 last_green_ = status_light.green;
569 }
570
571 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800572 canifier_.SetLEDOutput(status_light.blue,
573 ::ctre::phoenix::CANifier::LEDChannelC);
Sabina Davisc6329342019-03-01 20:44:42 -0800574 last_blue_ = status_light.blue;
575 }
576
577 if (status_light.red != last_red_ || time_since_last_send == 0) {
Sabina Davis77a11cf2019-03-09 18:20:26 -0800578 canifier_.SetLEDOutput(status_light.red,
579 ::ctre::phoenix::CANifier::LEDChannelB);
Sabina Davisc6329342019-03-01 20:44:42 -0800580 last_red_ = status_light.red;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800581 }
582 }
583
584 void Quit() { run_ = false; }
585
586 private:
587 ::frc971::wpilib::BufferedPcm pcm_;
588
Austin Schuh461e1182019-02-17 14:56:44 -0800589 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
590 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800591
592 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
593 intake_rollers_talon_;
594
595 ::aos::Queue<
596 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
597 superstructure_;
598
Sabina Davisc6329342019-03-01 20:44:42 -0800599 ::ctre::phoenix::CANifier canifier_{0};
600
Austin Schuhc1d6f832019-02-15 23:22:17 -0800601 ::std::atomic<bool> run_{true};
Sabina Davisc6329342019-03-01 20:44:42 -0800602
603 double last_red_ = -1.0;
604 double last_green_ = -1.0;
605 double last_blue_ = -1.0;
606
607 int light_flash_ = 0;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800608};
609
Sabina Davisabeae332019-02-01 21:12:57 -0800610class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
611 public:
612 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
613 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
614 frc::Encoder::k4X);
615 }
616
617 void Run() override {
618 ::aos::InitNRT();
619 ::aos::SetCurrentThreadName("StartCompetition");
620
621 ::frc971::wpilib::JoystickSender joystick_sender;
622 ::std::thread joystick_thread(::std::ref(joystick_sender));
623
624 ::frc971::wpilib::PDPFetcher pdp_fetcher;
625 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
626 SensorReader reader;
627
Sabina Davisabeae332019-02-01 21:12:57 -0800628 reader.set_drivetrain_left_encoder(make_encoder(0));
629 reader.set_drivetrain_right_encoder(make_encoder(1));
630
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800631 reader.set_elevator_encoder(make_encoder(4));
632 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
633 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800634
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800635 reader.set_wrist_encoder(make_encoder(5));
636 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
637 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800638
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800639 reader.set_intake_encoder(make_encoder(2));
640 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800641
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800642 reader.set_stilts_encoder(make_encoder(3));
643 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800644 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
645
Austin Schuh3b010bc2019-02-24 17:25:37 -0800646 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800647 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800648
Sabina Davisabeae332019-02-01 21:12:57 -0800649 ::std::thread reader_thread(::std::ref(reader));
650
Brian Silvermanf8b75252019-02-24 16:13:58 -0800651 CameraReader camera_reader;
652 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
653 camera_reader.set_spi(&camera_spi);
654 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800655 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
656 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
657 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800658
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800659 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800660 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
661 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800662 imu.set_spi_idle_callback(
663 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800664 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800665 imu.set_reset(imu_reset.get());
666 ::std::thread imu_thread(::std::ref(imu));
667
668 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
669 // they are identical, as far as DrivetrainWriter is concerned, to the SP
670 // variety so all the Victors are written as SPs.
671
Sabina Davisd004fd62019-02-02 23:51:46 -0800672 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
673 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800674 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800675 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800676 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800677 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
678
Alex Perry5fb5ff22019-02-09 21:53:17 -0800679 SuperstructureWriter superstructure_writer;
680 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800681 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800682 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800683 superstructure_writer.set_suction_victor(
684 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800685 superstructure_writer.set_intake_victor(
686 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800687 superstructure_writer.set_wrist_victor(
688 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
689 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800690 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800691
692 ::std::thread superstructure_writer_thread(
693 ::std::ref(superstructure_writer));
694
Austin Schuhc1d6f832019-02-15 23:22:17 -0800695 SolenoidWriter solenoid_writer;
696 solenoid_writer.set_intake_roller_talon(
697 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800698 solenoid_writer.set_big_suction_cup(0, 1);
699 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800700
701 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
702
Sabina Davisabeae332019-02-01 21:12:57 -0800703 // Wait forever. Not much else to do...
704 while (true) {
705 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
706 if (r != 0) {
707 PLOG(WARNING, "infinite select failed");
708 } else {
709 PLOG(WARNING, "infinite select succeeded??\n");
710 }
711 }
712
713 LOG(ERROR, "Exiting WPILibRobot\n");
714
Austin Schuhc1d6f832019-02-15 23:22:17 -0800715 solenoid_writer.Quit();
716 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800717 joystick_sender.Quit();
718 joystick_thread.join();
719 pdp_fetcher.Quit();
720 pdp_fetcher_thread.join();
721 reader.Quit();
722 reader_thread.join();
723 imu.Quit();
724 imu_thread.join();
725
726 drivetrain_writer.Quit();
727 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800728 superstructure_writer.Quit();
729 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800730
731 ::aos::Cleanup();
732 }
733};
734
735} // namespace
736} // namespace wpilib
737} // namespace y2019
738
739AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);