blob: 02269ebb8c4509050d98841a05b7248651149a3f [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"
19#undef ERROR
20
21#include "aos/commonmath.h"
22#include "aos/init.h"
23#include "aos/logging/logging.h"
24#include "aos/logging/queue_logging.h"
25#include "aos/make_unique.h"
Austin Schuhc2ee66b2019-02-19 13:37:46 -080026#include "aos/robot_state/robot_state.q.h"
Sabina Davisabeae332019-02-01 21:12:57 -080027#include "aos/time/time.h"
Sabina Davisabeae332019-02-01 21:12:57 -080028#include "aos/util/log_interval.h"
29#include "aos/util/phased_loop.h"
30#include "aos/util/wrapping_counter.h"
Sabina Davisabeae332019-02-01 21:12:57 -080031#include "frc971/autonomous/auto.q.h"
32#include "frc971/control_loops/drivetrain/drivetrain.q.h"
33#include "frc971/wpilib/ADIS16448.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080034#include "frc971/wpilib/buffered_pcm.h"
35#include "frc971/wpilib/buffered_solenoid.h"
Sabina Davisabeae332019-02-01 21:12:57 -080036#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080037#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080038#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080039#include "frc971/wpilib/joystick_sender.h"
40#include "frc971/wpilib/logging.q.h"
41#include "frc971/wpilib/loop_output_handler.h"
42#include "frc971/wpilib/pdp_fetcher.h"
Sabina Davisadc58542019-02-01 22:23:00 -080043#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080044#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuhf6b94632019-02-02 22:11:27 -080045#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080046#include "y2019/constants.h"
Alex Perry5fb5ff22019-02-09 21:53:17 -080047#include "y2019/control_loops/superstructure/superstructure.q.h"
Brian Silvermanf8b75252019-02-24 16:13:58 -080048#include "y2019/jevois/spi.h"
Sabina Davisabeae332019-02-01 21:12:57 -080049
50#ifndef M_PI
51#define M_PI 3.14159265358979323846
52#endif
53
54using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080055using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080056using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080057using ::aos::monotonic_clock;
58namespace chrono = ::std::chrono;
59using aos::make_unique;
60
61namespace y2019 {
62namespace wpilib {
63namespace {
64
65constexpr double kMaxBringupPower = 12.0;
66
67// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
68// DMA stuff and then removing the * 2.0 in *_translate.
69// The low bit is direction.
70
71// TODO(brian): Use ::std::max instead once we have C++14 so that can be
72// constexpr.
73template <typename T>
74constexpr T max(T a, T b) {
75 return (a > b) ? a : b;
76}
77
78template <typename T, typename... Rest>
79constexpr T max(T a, T b, T c, Rest... rest) {
80 return max(max(a, b), c, rest...);
81}
82
83double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080084 return ((static_cast<double>(in) /
85 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080086 (2.0 * M_PI)) *
87 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080088 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080089}
90
91double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080092 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080093 (2.0 * M_PI)) *
94 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080095 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080096}
97
Alex Perry5fb5ff22019-02-09 21:53:17 -080098double elevator_pot_translate(double voltage) {
99 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -0800100 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800101}
102
103double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800104 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800105 (2 * M_PI /*radians*/);
106}
107
108double stilts_pot_translate(double voltage) {
109 return voltage * Values::kStiltsPotRatio() *
110 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
111}
112
Sabina Davisabeae332019-02-01 21:12:57 -0800113constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800114 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
115 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800116static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
117 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800118constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800119 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
120 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800121
Sabina Davisabeae332019-02-01 21:12:57 -0800122static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
123 "medium encoders are too fast");
124
125// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800126class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800127 public:
128 SensorReader() {
129 // Set to filter out anything shorter than 1/4 of the minimum pulse width
130 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800131 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
132 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800133 }
134
Alex Perry5fb5ff22019-02-09 21:53:17 -0800135 // Elevator
136
137 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
138 medium_encoder_filter_.Add(encoder.get());
139 elevator_encoder_.set_encoder(::std::move(encoder));
140 }
141
142 void set_elevator_absolute_pwm(
143 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
144 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
145 }
146
147 void set_elevator_potentiometer(
148 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
149 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
150 }
151
152 // Intake
153
154 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
155 medium_encoder_filter_.Add(encoder.get());
156 intake_encoder_.set_encoder(::std::move(encoder));
157 }
158
159 void set_intake_absolute_pwm(
160 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
161 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
162 }
163
164 // Wrist
165
166 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
167 medium_encoder_filter_.Add(encoder.get());
168 wrist_encoder_.set_encoder(::std::move(encoder));
169 }
170
171 void set_wrist_absolute_pwm(
172 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
173 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
174 }
175
176 void set_wrist_potentiometer(
177 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
178 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
179 }
180
181 // Stilts
182
183 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
184 medium_encoder_filter_.Add(encoder.get());
185 stilts_encoder_.set_encoder(::std::move(encoder));
186 }
187
188 void set_stilts_absolute_pwm(
189 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
190 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
191 }
192
193 void set_stilts_potentiometer(
194 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
195 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
196 }
197
Austin Schuh461e1182019-02-17 14:56:44 -0800198 // Vacuum pressure sensor
199 void set_vacuum_sensor(int port) {
200 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
201 }
202
Sabina Davis399dbd82019-02-01 23:06:08 -0800203 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800204 {
205 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
206 drivetrain_message->left_encoder =
207 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
208 drivetrain_message->left_speed =
209 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
210
211 drivetrain_message->right_encoder =
212 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
213 drivetrain_message->right_speed = -drivetrain_velocity_translate(
214 drivetrain_right_encoder_->GetPeriod());
215
216 drivetrain_message.Send();
217 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800218 const auto values = constants::GetValues();
219
220 {
221 auto superstructure_message = superstructure_queue.position.MakeMessage();
222
223 // Elevator
224 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
225 Values::kElevatorEncoderCountsPerRevolution(),
226 Values::kElevatorEncoderRatio(), elevator_pot_translate,
227 false, values.elevator.potentiometer_offset);
228 // Intake
229 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
230 Values::kIntakeEncoderCountsPerRevolution(),
231 Values::kIntakeEncoderRatio(), false);
232
233 // Wrist
234 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
235 Values::kWristEncoderCountsPerRevolution(),
236 Values::kWristEncoderRatio(), wrist_pot_translate, false,
237 values.wrist.potentiometer_offset);
238
239 // Stilts
240 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
241 Values::kStiltsEncoderCountsPerRevolution(),
242 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
243 values.stilts.potentiometer_offset);
244
Austin Schuh461e1182019-02-17 14:56:44 -0800245 // Suction
246 constexpr float kMinVoltage = 0.5;
247 constexpr float kMaxVoltage = 2.1;
248 superstructure_message->suction_pressure =
249 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
250 (kMaxVoltage - kMinVoltage);
251
Alex Perry5fb5ff22019-02-09 21:53:17 -0800252 superstructure_message.Send();
253 }
254 }
255
256 private:
257 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
258 wrist_encoder_, stilts_encoder_;
259
Austin Schuh461e1182019-02-17 14:56:44 -0800260 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
261
Alex Perry5fb5ff22019-02-09 21:53:17 -0800262 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
263 // TODO(sabina): Add wrist and elevator hall effects.
264};
265
Brian Silvermanf8b75252019-02-24 16:13:58 -0800266class CameraReader {
267 public:
268 CameraReader() = default;
269 CameraReader(const CameraReader &) = delete;
270 CameraReader &operator=(const CameraReader &) = delete;
271
272 void set_spi(frc::SPI *spi) {
273 spi_ = spi;
274 spi_->SetClockRate(1e6);
275 spi_->SetChipSelectActiveHigh();
276 spi_->SetClockActiveLow();
277 spi_->SetSampleDataOnFalling();
278 // It ignores you if you try changing this...
279 spi_->SetMSBFirst();
280 }
281
Brian Silverman7ecf0672019-03-02 15:30:03 -0800282 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
283 activate_usb_ = std::move(activate_usb);
284 }
285
286 void set_activate_passthrough(
287 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
288 activate_passthrough_ = std::move(activate_passthrough);
289 }
290
Brian Silvermanf8b75252019-02-24 16:13:58 -0800291 void DoSpiTransaction() {
292 using namespace frc971::jevois;
293 RoborioToTeensy to_teensy{};
294 to_teensy.realtime_now = aos::realtime_clock::now();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800295 if (activate_usb_ && !activate_usb_->Get()) {
296 to_teensy.camera_command = CameraCommand::kUsb;
297 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
298 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
299 } else {
300 to_teensy.camera_command = CameraCommand::kNormal;
301 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800302
303 std::array<char, spi_transfer_size() + 1> to_send{};
304 {
305 const auto to_send_data =
306 gsl::make_span(to_send).last<spi_transfer_size()>();
307 const auto encoded = SpiPackToTeensy(to_teensy);
308 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
309 }
310 rx_clearer_.ClearRxFifo();
311 // First, send recieve a dummy byte because the Teensy can't control what it
312 // sends for the first byte.
313 std::array<char, spi_transfer_size() + 1> to_receive;
314 DoTransaction(to_send, to_receive);
315 const auto unpacked = SpiUnpackToRoborio(
316 gsl::make_span(to_receive).last(spi_transfer_size()));
317 if (!unpacked) {
318 LOG(INFO, "Decoding SPI data failed\n");
319 return;
320 }
321
322 // TODO(Brian): Do something useful with the targets.
323
324 if (dummy_spi_) {
325 uint8_t dummy_send, dummy_receive;
326 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
327 }
328 }
329
330 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
331 CHECK_EQ(to_send.size(), to_receive.size());
332 const auto result = spi_->Transaction(
333 reinterpret_cast<uint8_t *>(to_send.data()),
334 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
335 if (result == to_send.size()) {
336 return;
337 }
338 if (result == -1) {
339 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
340 return;
341 }
342 LOG(FATAL, "SPI::Transaction returned something weird\n");
343 }
344
345 void SetDummySPI(frc::SPI::Port port) {
346 dummy_spi_.reset(new frc::SPI(port));
347 // Pick the same settings here in case the roboRIO decides to try something
348 // stupid when switching.
349 if (dummy_spi_) {
350 dummy_spi_->SetClockRate(1e5);
351 dummy_spi_->SetChipSelectActiveLow();
352 dummy_spi_->SetClockActiveLow();
353 dummy_spi_->SetSampleDataOnFalling();
354 dummy_spi_->SetMSBFirst();
355 }
356 }
357
358 private:
359 frc::SPI *spi_ = nullptr;
360 ::std::unique_ptr<frc::SPI> dummy_spi_;
361
Brian Silverman7ecf0672019-03-02 15:30:03 -0800362 std::unique_ptr<frc::DigitalInput> activate_usb_;
363 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
364
Brian Silvermanf8b75252019-02-24 16:13:58 -0800365 frc971::wpilib::SpiRxClearer rx_clearer_;
366};
367
Alex Perry5fb5ff22019-02-09 21:53:17 -0800368class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
369 public:
370 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
371 elevator_victor_ = ::std::move(t);
372 }
373
Austin Schuh461e1182019-02-17 14:56:44 -0800374 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
375 suction_victor_ = ::std::move(t);
376 }
377
Alex Perry5fb5ff22019-02-09 21:53:17 -0800378 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
379 intake_victor_ = ::std::move(t);
380 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800381
382 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
383 wrist_victor_ = ::std::move(t);
384 }
385
386 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
387 stilts_victor_ = ::std::move(t);
388 }
389
390 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800391 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800392 ::y2019::control_loops::superstructure::superstructure_queue.output
393 .FetchAnother();
394 }
395
Austin Schuh461e1182019-02-17 14:56:44 -0800396 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800397 auto &queue =
398 ::y2019::control_loops::superstructure::superstructure_queue.output;
399 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800400 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800401 -kMaxBringupPower,
402 kMaxBringupPower) /
403 12.0);
404
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800405 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800406 -kMaxBringupPower, kMaxBringupPower) /
407 12.0);
408
Alex Perry5fb5ff22019-02-09 21:53:17 -0800409 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
410 -kMaxBringupPower, kMaxBringupPower) /
411 12.0);
412
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800413 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800414 -kMaxBringupPower, kMaxBringupPower) /
415 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800416
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800417 ::aos::robot_state.FetchLatest();
418 const double battery_voltage =
419 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
420
421 // Throw a fast low pass filter on the battery voltage so we don't respond
422 // too fast to noise.
423 filtered_battery_voltage_ =
424 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
425
426 suction_victor_->SetSpeed(::aos::Clip(
427 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800428 }
429
Austin Schuh461e1182019-02-17 14:56:44 -0800430 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800431 LOG(WARNING, "Superstructure output too old.\n");
432
433 elevator_victor_->SetDisabled();
434 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800435 wrist_victor_->SetDisabled();
436 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800437 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800438 }
439
440 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800441 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800442
443 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800444};
445
Austin Schuhc1d6f832019-02-15 23:22:17 -0800446class SolenoidWriter {
447 public:
448 SolenoidWriter()
449 : superstructure_(
450 ".y2019.control_loops.superstructure.superstructure_queue.output") {
451 }
452
Austin Schuh461e1182019-02-17 14:56:44 -0800453 void set_big_suction_cup(int index0, int index1) {
454 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
455 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800456 }
Austin Schuh461e1182019-02-17 14:56:44 -0800457 void set_small_suction_cup(int index0, int index1) {
458 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
459 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800460 }
461
462 void set_intake_roller_talon(
463 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
464 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800465 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800466 intake_rollers_talon_->EnableCurrentLimit(true);
467 }
468
469 void operator()() {
470 ::aos::SetCurrentThreadName("Solenoids");
471 ::aos::SetCurrentThreadRealtimePriority(27);
472
473 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
474 ::std::chrono::milliseconds(1));
475
476 while (run_) {
477 {
478 const int iterations = phased_loop.SleepUntilNext();
479 if (iterations != 1) {
480 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
481 }
482 }
483
484 {
485 superstructure_.FetchLatest();
486 if (superstructure_.get()) {
487 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
488
Tyler Chatow7db827f2019-02-24 00:10:13 -0800489 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
490 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
491 small_suction_cup0_->Set(superstructure_->intake_suction_top);
492 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800493
494 intake_rollers_talon_->Set(
495 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
496 ::aos::Clip(superstructure_->intake_roller_voltage,
497 -kMaxBringupPower, kMaxBringupPower) /
498 12.0);
499 }
500 }
501
502 {
503 ::frc971::wpilib::PneumaticsToLog to_log;
504
505 pcm_.Flush();
506 to_log.read_solenoids = pcm_.GetAll();
507 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
508 }
509 }
510 }
511
512 void Quit() { run_ = false; }
513
514 private:
515 ::frc971::wpilib::BufferedPcm pcm_;
516
Austin Schuh461e1182019-02-17 14:56:44 -0800517 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
518 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800519
520 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
521 intake_rollers_talon_;
522
523 ::aos::Queue<
524 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
525 superstructure_;
526
527 ::std::atomic<bool> run_{true};
528};
529
Sabina Davisabeae332019-02-01 21:12:57 -0800530class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
531 public:
532 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
533 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
534 frc::Encoder::k4X);
535 }
536
537 void Run() override {
538 ::aos::InitNRT();
539 ::aos::SetCurrentThreadName("StartCompetition");
540
541 ::frc971::wpilib::JoystickSender joystick_sender;
542 ::std::thread joystick_thread(::std::ref(joystick_sender));
543
544 ::frc971::wpilib::PDPFetcher pdp_fetcher;
545 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
546 SensorReader reader;
547
Sabina Davisabeae332019-02-01 21:12:57 -0800548 reader.set_drivetrain_left_encoder(make_encoder(0));
549 reader.set_drivetrain_right_encoder(make_encoder(1));
550
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800551 reader.set_elevator_encoder(make_encoder(4));
552 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
553 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800554
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800555 reader.set_wrist_encoder(make_encoder(5));
556 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
557 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800558
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800559 reader.set_intake_encoder(make_encoder(2));
560 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800561
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800562 reader.set_stilts_encoder(make_encoder(3));
563 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800564 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
565
Austin Schuh3b010bc2019-02-24 17:25:37 -0800566 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800567 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800568
Sabina Davisabeae332019-02-01 21:12:57 -0800569 ::std::thread reader_thread(::std::ref(reader));
570
Brian Silvermanf8b75252019-02-24 16:13:58 -0800571 CameraReader camera_reader;
572 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
573 camera_reader.set_spi(&camera_spi);
574 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800575 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
576 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
577 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800578
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800579 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800580 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
581 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800582 imu.set_spi_idle_callback(
583 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800584 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800585 imu.set_reset(imu_reset.get());
586 ::std::thread imu_thread(::std::ref(imu));
587
588 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
589 // they are identical, as far as DrivetrainWriter is concerned, to the SP
590 // variety so all the Victors are written as SPs.
591
Sabina Davisd004fd62019-02-02 23:51:46 -0800592 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
593 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800594 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800595 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800596 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800597 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
598
Alex Perry5fb5ff22019-02-09 21:53:17 -0800599 SuperstructureWriter superstructure_writer;
600 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800601 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800602 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800603 superstructure_writer.set_suction_victor(
604 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800605 superstructure_writer.set_intake_victor(
606 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800607 superstructure_writer.set_wrist_victor(
608 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
609 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800610 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800611
612 ::std::thread superstructure_writer_thread(
613 ::std::ref(superstructure_writer));
614
Austin Schuhc1d6f832019-02-15 23:22:17 -0800615 SolenoidWriter solenoid_writer;
616 solenoid_writer.set_intake_roller_talon(
617 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800618 solenoid_writer.set_big_suction_cup(0, 1);
619 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800620
621 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
622
Sabina Davisabeae332019-02-01 21:12:57 -0800623 // Wait forever. Not much else to do...
624 while (true) {
625 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
626 if (r != 0) {
627 PLOG(WARNING, "infinite select failed");
628 } else {
629 PLOG(WARNING, "infinite select succeeded??\n");
630 }
631 }
632
633 LOG(ERROR, "Exiting WPILibRobot\n");
634
Austin Schuhc1d6f832019-02-15 23:22:17 -0800635 solenoid_writer.Quit();
636 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800637 joystick_sender.Quit();
638 joystick_thread.join();
639 pdp_fetcher.Quit();
640 pdp_fetcher_thread.join();
641 reader.Quit();
642 reader_thread.join();
643 imu.Quit();
644 imu_thread.join();
645
646 drivetrain_writer.Quit();
647 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800648 superstructure_writer.Quit();
649 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800650
651 ::aos::Cleanup();
652 }
653};
654
655} // namespace
656} // namespace wpilib
657} // namespace y2019
658
659AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);