blob: 2b21eb8922dfa0389ad067751a19ab60218ff5cd [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"
Brian Silvermanc41fb862019-03-02 21:14:46 -080031#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
Sabina Davisabeae332019-02-01 21:12:57 -080032#include "frc971/autonomous/auto.q.h"
33#include "frc971/control_loops/drivetrain/drivetrain.q.h"
34#include "frc971/wpilib/ADIS16448.h"
Austin Schuhc1d6f832019-02-15 23:22:17 -080035#include "frc971/wpilib/buffered_pcm.h"
36#include "frc971/wpilib/buffered_solenoid.h"
Sabina Davisabeae332019-02-01 21:12:57 -080037#include "frc971/wpilib/dma.h"
Sabina Davisd004fd62019-02-02 23:51:46 -080038#include "frc971/wpilib/drivetrain_writer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080039#include "frc971/wpilib/encoder_and_potentiometer.h"
Sabina Davisabeae332019-02-01 21:12:57 -080040#include "frc971/wpilib/joystick_sender.h"
41#include "frc971/wpilib/logging.q.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/pdp_fetcher.h"
Sabina Davisadc58542019-02-01 22:23:00 -080044#include "frc971/wpilib/sensor_reader.h"
Sabina Davisabeae332019-02-01 21:12:57 -080045#include "frc971/wpilib/wpilib_robot_base.h"
Sabina Davis7be49f32019-02-02 00:30:19 -080046#include "y2019/constants.h"
Brian Silvermanc41fb862019-03-02 21:14:46 -080047#include "y2019/control_loops/drivetrain/camera.q.h"
Alex Perry5fb5ff22019-02-09 21:53:17 -080048#include "y2019/control_loops/superstructure/superstructure.q.h"
Brian Silvermanf8b75252019-02-24 16:13:58 -080049#include "y2019/jevois/spi.h"
Sabina Davisabeae332019-02-01 21:12:57 -080050
51#ifndef M_PI
52#define M_PI 3.14159265358979323846
53#endif
54
55using ::frc971::control_loops::drivetrain_queue;
Alex Perry5fb5ff22019-02-09 21:53:17 -080056using ::y2019::control_loops::superstructure::superstructure_queue;
Sabina Davis7be49f32019-02-02 00:30:19 -080057using ::y2019::constants::Values;
Sabina Davisabeae332019-02-01 21:12:57 -080058using ::aos::monotonic_clock;
59namespace chrono = ::std::chrono;
60using aos::make_unique;
61
62namespace y2019 {
63namespace wpilib {
64namespace {
65
66constexpr double kMaxBringupPower = 12.0;
67
68// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
69// DMA stuff and then removing the * 2.0 in *_translate.
70// The low bit is direction.
71
72// TODO(brian): Use ::std::max instead once we have C++14 so that can be
73// constexpr.
74template <typename T>
75constexpr T max(T a, T b) {
76 return (a > b) ? a : b;
77}
78
79template <typename T, typename... Rest>
80constexpr T max(T a, T b, T c, Rest... rest) {
81 return max(max(a, b), c, rest...);
82}
83
84double drivetrain_translate(int32_t in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080085 return ((static_cast<double>(in) /
86 Values::kDrivetrainEncoderCountsPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080087 (2.0 * M_PI)) *
88 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080089 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080090}
91
92double drivetrain_velocity_translate(double in) {
Sabina Davis7be49f32019-02-02 00:30:19 -080093 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
Sabina Davisabeae332019-02-01 21:12:57 -080094 (2.0 * M_PI)) *
95 Values::kDrivetrainEncoderRatio() *
Sabina Davis7be49f32019-02-02 00:30:19 -080096 control_loops::drivetrain::kWheelRadius;
Sabina Davisabeae332019-02-01 21:12:57 -080097}
98
Alex Perry5fb5ff22019-02-09 21:53:17 -080099double elevator_pot_translate(double voltage) {
100 return voltage * Values::kElevatorPotRatio() *
Austin Schuhed7f8632019-02-15 23:12:20 -0800101 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Alex Perry5fb5ff22019-02-09 21:53:17 -0800102}
103
104double wrist_pot_translate(double voltage) {
Austin Schuhed7f8632019-02-15 23:12:20 -0800105 return voltage * Values::kWristPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
Alex Perry5fb5ff22019-02-09 21:53:17 -0800106 (2 * M_PI /*radians*/);
107}
108
109double stilts_pot_translate(double voltage) {
110 return voltage * Values::kStiltsPotRatio() *
111 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
112}
113
Sabina Davisabeae332019-02-01 21:12:57 -0800114constexpr double kMaxFastEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800115 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
116 Values::kMaxIntakeEncoderPulsesPerSecond());
Sabina Davisabeae332019-02-01 21:12:57 -0800117static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
118 "fast encoders are too fast");
Sabina Davisabeae332019-02-01 21:12:57 -0800119constexpr double kMaxMediumEncoderPulsesPerSecond =
Alex Perry5fb5ff22019-02-09 21:53:17 -0800120 max(Values::kMaxElevatorEncoderPulsesPerSecond(),
121 Values::kMaxWristEncoderPulsesPerSecond());
Theo Bafrali00e42272019-02-12 01:07:46 -0800122
Sabina Davisabeae332019-02-01 21:12:57 -0800123static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
124 "medium encoders are too fast");
125
126// Class to send position messages with sensor readings to our loops.
Sabina Davisadc58542019-02-01 22:23:00 -0800127class SensorReader : public ::frc971::wpilib::SensorReader {
Sabina Davisabeae332019-02-01 21:12:57 -0800128 public:
129 SensorReader() {
130 // Set to filter out anything shorter than 1/4 of the minimum pulse width
131 // we should ever see.
Austin Schuh45a549f2019-02-02 15:43:56 -0800132 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
133 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Sabina Davisabeae332019-02-01 21:12:57 -0800134 }
135
Alex Perry5fb5ff22019-02-09 21:53:17 -0800136 // Elevator
137
138 void set_elevator_encoder(::std::unique_ptr<frc::Encoder> encoder) {
139 medium_encoder_filter_.Add(encoder.get());
140 elevator_encoder_.set_encoder(::std::move(encoder));
141 }
142
143 void set_elevator_absolute_pwm(
144 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
145 elevator_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
146 }
147
148 void set_elevator_potentiometer(
149 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
150 elevator_encoder_.set_potentiometer(::std::move(potentiometer));
151 }
152
153 // Intake
154
155 void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
156 medium_encoder_filter_.Add(encoder.get());
157 intake_encoder_.set_encoder(::std::move(encoder));
158 }
159
160 void set_intake_absolute_pwm(
161 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
162 intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
163 }
164
165 // Wrist
166
167 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
168 medium_encoder_filter_.Add(encoder.get());
169 wrist_encoder_.set_encoder(::std::move(encoder));
170 }
171
172 void set_wrist_absolute_pwm(
173 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
174 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
175 }
176
177 void set_wrist_potentiometer(
178 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
179 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
180 }
181
182 // Stilts
183
184 void set_stilts_encoder(::std::unique_ptr<frc::Encoder> encoder) {
185 medium_encoder_filter_.Add(encoder.get());
186 stilts_encoder_.set_encoder(::std::move(encoder));
187 }
188
189 void set_stilts_absolute_pwm(
190 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
191 stilts_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
192 }
193
194 void set_stilts_potentiometer(
195 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
196 stilts_encoder_.set_potentiometer(::std::move(potentiometer));
197 }
198
Austin Schuh461e1182019-02-17 14:56:44 -0800199 // Vacuum pressure sensor
200 void set_vacuum_sensor(int port) {
201 vacuum_sensor_ = make_unique<frc::AnalogInput>(port);
202 }
203
Sabina Davis399dbd82019-02-01 23:06:08 -0800204 void RunIteration() override {
Sabina Davisabeae332019-02-01 21:12:57 -0800205 {
206 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
207 drivetrain_message->left_encoder =
208 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
209 drivetrain_message->left_speed =
210 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
211
212 drivetrain_message->right_encoder =
213 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
214 drivetrain_message->right_speed = -drivetrain_velocity_translate(
215 drivetrain_right_encoder_->GetPeriod());
216
217 drivetrain_message.Send();
218 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800219 const auto values = constants::GetValues();
220
221 {
222 auto superstructure_message = superstructure_queue.position.MakeMessage();
223
224 // Elevator
225 CopyPosition(elevator_encoder_, &superstructure_message->elevator,
226 Values::kElevatorEncoderCountsPerRevolution(),
227 Values::kElevatorEncoderRatio(), elevator_pot_translate,
228 false, values.elevator.potentiometer_offset);
229 // Intake
230 CopyPosition(intake_encoder_, &superstructure_message->intake_joint,
231 Values::kIntakeEncoderCountsPerRevolution(),
232 Values::kIntakeEncoderRatio(), false);
233
234 // Wrist
235 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
236 Values::kWristEncoderCountsPerRevolution(),
237 Values::kWristEncoderRatio(), wrist_pot_translate, false,
238 values.wrist.potentiometer_offset);
239
240 // Stilts
241 CopyPosition(stilts_encoder_, &superstructure_message->stilts,
242 Values::kStiltsEncoderCountsPerRevolution(),
243 Values::kStiltsEncoderRatio(), stilts_pot_translate, false,
244 values.stilts.potentiometer_offset);
245
Austin Schuh461e1182019-02-17 14:56:44 -0800246 // Suction
247 constexpr float kMinVoltage = 0.5;
248 constexpr float kMaxVoltage = 2.1;
249 superstructure_message->suction_pressure =
250 (vacuum_sensor_->GetVoltage() - kMinVoltage) /
251 (kMaxVoltage - kMinVoltage);
252
Alex Perry5fb5ff22019-02-09 21:53:17 -0800253 superstructure_message.Send();
254 }
255 }
256
257 private:
258 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer elevator_encoder_,
259 wrist_encoder_, stilts_encoder_;
260
Austin Schuh461e1182019-02-17 14:56:44 -0800261 ::std::unique_ptr<frc::AnalogInput> vacuum_sensor_;
262
Alex Perry5fb5ff22019-02-09 21:53:17 -0800263 ::frc971::wpilib::AbsoluteEncoder intake_encoder_;
264 // TODO(sabina): Add wrist and elevator hall effects.
265};
266
Brian Silvermanf8b75252019-02-24 16:13:58 -0800267class CameraReader {
268 public:
269 CameraReader() = default;
270 CameraReader(const CameraReader &) = delete;
271 CameraReader &operator=(const CameraReader &) = delete;
272
273 void set_spi(frc::SPI *spi) {
274 spi_ = spi;
275 spi_->SetClockRate(1e6);
276 spi_->SetChipSelectActiveHigh();
277 spi_->SetClockActiveLow();
278 spi_->SetSampleDataOnFalling();
279 // It ignores you if you try changing this...
280 spi_->SetMSBFirst();
281 }
282
Brian Silverman7ecf0672019-03-02 15:30:03 -0800283 void set_activate_usb(std::unique_ptr<frc::DigitalInput> activate_usb) {
284 activate_usb_ = std::move(activate_usb);
285 }
286
287 void set_activate_passthrough(
288 std::unique_ptr<frc::DigitalInput> activate_passthrough) {
289 activate_passthrough_ = std::move(activate_passthrough);
290 }
291
Brian Silvermanf8b75252019-02-24 16:13:58 -0800292 void DoSpiTransaction() {
293 using namespace frc971::jevois;
294 RoborioToTeensy to_teensy{};
295 to_teensy.realtime_now = aos::realtime_clock::now();
Brian Silverman7ecf0672019-03-02 15:30:03 -0800296 if (activate_usb_ && !activate_usb_->Get()) {
297 to_teensy.camera_command = CameraCommand::kUsb;
298 } else if (activate_passthrough_ && !activate_passthrough_->Get()) {
299 to_teensy.camera_command = CameraCommand::kCameraPassthrough;
300 } else {
301 to_teensy.camera_command = CameraCommand::kNormal;
302 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800303
304 std::array<char, spi_transfer_size() + 1> to_send{};
305 {
306 const auto to_send_data =
307 gsl::make_span(to_send).last<spi_transfer_size()>();
308 const auto encoded = SpiPackToTeensy(to_teensy);
309 std::copy(encoded.begin(), encoded.end(), to_send_data.begin());
310 }
311 rx_clearer_.ClearRxFifo();
312 // First, send recieve a dummy byte because the Teensy can't control what it
313 // sends for the first byte.
314 std::array<char, spi_transfer_size() + 1> to_receive;
315 DoTransaction(to_send, to_receive);
316 const auto unpacked = SpiUnpackToRoborio(
317 gsl::make_span(to_receive).last(spi_transfer_size()));
318 if (!unpacked) {
319 LOG(INFO, "Decoding SPI data failed\n");
320 return;
321 }
322
Brian Silvermanc41fb862019-03-02 21:14:46 -0800323 const auto now = aos::monotonic_clock::now();
324 for (const auto &received : unpacked->frames) {
325 auto to_send = control_loops::drivetrain::camera_frames.MakeMessage();
326 to_send->timestamp =
James Kuszmaul85ffeb82019-03-03 19:41:44 -0800327 std::chrono::nanoseconds((now - received.age).time_since_epoch())
Brian Silvermanc41fb862019-03-02 21:14:46 -0800328 .count();
329 to_send->num_targets = received.targets.size();
330 for (size_t i = 0; i < received.targets.size(); ++i) {
331 to_send->targets[i].distance = received.targets[i].distance;
332 to_send->targets[i].height = received.targets[i].height;
333 to_send->targets[i].heading = received.targets[i].heading;
334 to_send->targets[i].skew = received.targets[i].skew;
335 }
336 to_send->camera = received.camera_index;
Austin Schuhbb52eec2019-03-03 18:32:14 -0800337 LOG_STRUCT(DEBUG, "camera_frames", *to_send);
Brian Silvermanc41fb862019-03-02 21:14:46 -0800338 to_send.Send();
339 }
Brian Silvermanf8b75252019-02-24 16:13:58 -0800340
341 if (dummy_spi_) {
342 uint8_t dummy_send, dummy_receive;
343 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
344 }
345 }
346
347 void DoTransaction(gsl::span<char> to_send, gsl::span<char> to_receive) {
348 CHECK_EQ(to_send.size(), to_receive.size());
349 const auto result = spi_->Transaction(
350 reinterpret_cast<uint8_t *>(to_send.data()),
351 reinterpret_cast<uint8_t *>(to_receive.data()), to_send.size());
352 if (result == to_send.size()) {
353 return;
354 }
355 if (result == -1) {
356 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", to_send.size());
357 return;
358 }
359 LOG(FATAL, "SPI::Transaction returned something weird\n");
360 }
361
362 void SetDummySPI(frc::SPI::Port port) {
363 dummy_spi_.reset(new frc::SPI(port));
364 // Pick the same settings here in case the roboRIO decides to try something
365 // stupid when switching.
366 if (dummy_spi_) {
367 dummy_spi_->SetClockRate(1e5);
368 dummy_spi_->SetChipSelectActiveLow();
369 dummy_spi_->SetClockActiveLow();
370 dummy_spi_->SetSampleDataOnFalling();
371 dummy_spi_->SetMSBFirst();
372 }
373 }
374
375 private:
376 frc::SPI *spi_ = nullptr;
377 ::std::unique_ptr<frc::SPI> dummy_spi_;
378
Brian Silverman7ecf0672019-03-02 15:30:03 -0800379 std::unique_ptr<frc::DigitalInput> activate_usb_;
380 std::unique_ptr<frc::DigitalInput> activate_passthrough_;
381
Brian Silvermanf8b75252019-02-24 16:13:58 -0800382 frc971::wpilib::SpiRxClearer rx_clearer_;
383};
384
Alex Perry5fb5ff22019-02-09 21:53:17 -0800385class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
386 public:
387 void set_elevator_victor(::std::unique_ptr<::frc::VictorSP> t) {
388 elevator_victor_ = ::std::move(t);
389 }
390
Austin Schuh461e1182019-02-17 14:56:44 -0800391 void set_suction_victor(::std::unique_ptr<::frc::VictorSP> t) {
392 suction_victor_ = ::std::move(t);
393 }
394
Alex Perry5fb5ff22019-02-09 21:53:17 -0800395 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
396 intake_victor_ = ::std::move(t);
397 }
Alex Perry5fb5ff22019-02-09 21:53:17 -0800398
399 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
400 wrist_victor_ = ::std::move(t);
401 }
402
403 void set_stilts_victor(::std::unique_ptr<::frc::VictorSP> t) {
404 stilts_victor_ = ::std::move(t);
405 }
406
407 private:
Austin Schuh461e1182019-02-17 14:56:44 -0800408 void Read() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800409 ::y2019::control_loops::superstructure::superstructure_queue.output
410 .FetchAnother();
411 }
412
Austin Schuh461e1182019-02-17 14:56:44 -0800413 void Write() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800414 auto &queue =
415 ::y2019::control_loops::superstructure::superstructure_queue.output;
416 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800417 elevator_victor_->SetSpeed(::aos::Clip(queue->elevator_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800418 -kMaxBringupPower,
419 kMaxBringupPower) /
420 12.0);
421
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800422 intake_victor_->SetSpeed(::aos::Clip(queue->intake_joint_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800423 -kMaxBringupPower, kMaxBringupPower) /
424 12.0);
425
Alex Perry5fb5ff22019-02-09 21:53:17 -0800426 wrist_victor_->SetSpeed(::aos::Clip(-queue->wrist_voltage,
427 -kMaxBringupPower, kMaxBringupPower) /
428 12.0);
429
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800430 stilts_victor_->SetSpeed(::aos::Clip(queue->stilts_voltage,
Alex Perry5fb5ff22019-02-09 21:53:17 -0800431 -kMaxBringupPower, kMaxBringupPower) /
432 12.0);
Austin Schuh461e1182019-02-17 14:56:44 -0800433
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800434 ::aos::robot_state.FetchLatest();
435 const double battery_voltage =
436 ::aos::robot_state.get() ? ::aos::robot_state->voltage_battery : 12.0;
437
438 // Throw a fast low pass filter on the battery voltage so we don't respond
439 // too fast to noise.
440 filtered_battery_voltage_ =
441 0.5 * filtered_battery_voltage_ + 0.5 * battery_voltage;
442
443 suction_victor_->SetSpeed(::aos::Clip(
444 queue->pump_voltage / filtered_battery_voltage_, -1.0, 1.0));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800445 }
446
Austin Schuh461e1182019-02-17 14:56:44 -0800447 void Stop() override {
Alex Perry5fb5ff22019-02-09 21:53:17 -0800448 LOG(WARNING, "Superstructure output too old.\n");
449
450 elevator_victor_->SetDisabled();
451 intake_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800452 wrist_victor_->SetDisabled();
453 stilts_victor_->SetDisabled();
Austin Schuh461e1182019-02-17 14:56:44 -0800454 suction_victor_->SetDisabled();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800455 }
456
457 ::std::unique_ptr<::frc::VictorSP> elevator_victor_, intake_victor_,
Austin Schuh461e1182019-02-17 14:56:44 -0800458 wrist_victor_, stilts_victor_, suction_victor_;
Austin Schuhc2ee66b2019-02-19 13:37:46 -0800459
460 double filtered_battery_voltage_ = 12.0;
Sabina Davisabeae332019-02-01 21:12:57 -0800461};
462
Austin Schuhc1d6f832019-02-15 23:22:17 -0800463class SolenoidWriter {
464 public:
465 SolenoidWriter()
466 : superstructure_(
467 ".y2019.control_loops.superstructure.superstructure_queue.output") {
468 }
469
Austin Schuh461e1182019-02-17 14:56:44 -0800470 void set_big_suction_cup(int index0, int index1) {
471 big_suction_cup0_ = pcm_.MakeSolenoid(index0);
472 big_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800473 }
Austin Schuh461e1182019-02-17 14:56:44 -0800474 void set_small_suction_cup(int index0, int index1) {
475 small_suction_cup0_ = pcm_.MakeSolenoid(index0);
476 small_suction_cup1_ = pcm_.MakeSolenoid(index1);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800477 }
478
479 void set_intake_roller_talon(
480 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX> t) {
481 intake_rollers_talon_ = ::std::move(t);
Austin Schuh23a51632019-02-19 16:50:36 -0800482 intake_rollers_talon_->ConfigContinuousCurrentLimit(10.0, 0);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800483 intake_rollers_talon_->EnableCurrentLimit(true);
484 }
485
486 void operator()() {
487 ::aos::SetCurrentThreadName("Solenoids");
488 ::aos::SetCurrentThreadRealtimePriority(27);
489
490 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
491 ::std::chrono::milliseconds(1));
492
493 while (run_) {
494 {
495 const int iterations = phased_loop.SleepUntilNext();
496 if (iterations != 1) {
497 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
498 }
499 }
500
501 {
502 superstructure_.FetchLatest();
503 if (superstructure_.get()) {
504 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
505
Tyler Chatow7db827f2019-02-24 00:10:13 -0800506 big_suction_cup0_->Set(!superstructure_->intake_suction_bottom);
507 big_suction_cup1_->Set(!superstructure_->intake_suction_bottom);
508 small_suction_cup0_->Set(superstructure_->intake_suction_top);
509 small_suction_cup1_->Set(superstructure_->intake_suction_top);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800510
511 intake_rollers_talon_->Set(
512 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
513 ::aos::Clip(superstructure_->intake_roller_voltage,
514 -kMaxBringupPower, kMaxBringupPower) /
515 12.0);
516 }
517 }
518
519 {
520 ::frc971::wpilib::PneumaticsToLog to_log;
521
522 pcm_.Flush();
523 to_log.read_solenoids = pcm_.GetAll();
524 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
525 }
526 }
527 }
528
529 void Quit() { run_ = false; }
530
531 private:
532 ::frc971::wpilib::BufferedPcm pcm_;
533
Austin Schuh461e1182019-02-17 14:56:44 -0800534 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> big_suction_cup0_,
535 big_suction_cup1_, small_suction_cup0_, small_suction_cup1_;
Austin Schuhc1d6f832019-02-15 23:22:17 -0800536
537 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonSRX>
538 intake_rollers_talon_;
539
540 ::aos::Queue<
541 ::y2019::control_loops::superstructure::SuperstructureQueue::Output>
542 superstructure_;
543
544 ::std::atomic<bool> run_{true};
545};
546
Sabina Davisabeae332019-02-01 21:12:57 -0800547class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
548 public:
549 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
550 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
551 frc::Encoder::k4X);
552 }
553
554 void Run() override {
555 ::aos::InitNRT();
556 ::aos::SetCurrentThreadName("StartCompetition");
557
558 ::frc971::wpilib::JoystickSender joystick_sender;
559 ::std::thread joystick_thread(::std::ref(joystick_sender));
560
561 ::frc971::wpilib::PDPFetcher pdp_fetcher;
562 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
563 SensorReader reader;
564
Sabina Davisabeae332019-02-01 21:12:57 -0800565 reader.set_drivetrain_left_encoder(make_encoder(0));
566 reader.set_drivetrain_right_encoder(make_encoder(1));
567
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800568 reader.set_elevator_encoder(make_encoder(4));
569 reader.set_elevator_absolute_pwm(make_unique<frc::DigitalInput>(4));
570 reader.set_elevator_potentiometer(make_unique<frc::AnalogInput>(4));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800571
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800572 reader.set_wrist_encoder(make_encoder(5));
573 reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(5));
574 reader.set_wrist_potentiometer(make_unique<frc::AnalogInput>(5));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800575
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800576 reader.set_intake_encoder(make_encoder(2));
577 reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(2));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800578
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800579 reader.set_stilts_encoder(make_encoder(3));
580 reader.set_stilts_absolute_pwm(make_unique<frc::DigitalInput>(3));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800581 reader.set_stilts_potentiometer(make_unique<frc::AnalogInput>(3));
582
Austin Schuh3b010bc2019-02-24 17:25:37 -0800583 reader.set_pwm_trigger(true);
Austin Schuh461e1182019-02-17 14:56:44 -0800584 reader.set_vacuum_sensor(7);
Sabina Davisabeae332019-02-01 21:12:57 -0800585
Sabina Davisabeae332019-02-01 21:12:57 -0800586 ::std::thread reader_thread(::std::ref(reader));
587
Brian Silvermanf8b75252019-02-24 16:13:58 -0800588 CameraReader camera_reader;
589 frc::SPI camera_spi(frc::SPI::Port::kOnboardCS3);
590 camera_reader.set_spi(&camera_spi);
591 camera_reader.SetDummySPI(frc::SPI::Port::kOnboardCS2);
Brian Silverman7ecf0672019-03-02 15:30:03 -0800592 // Austin says 8, 9, 24, and 25 are good options to choose from for these.
593 camera_reader.set_activate_usb(make_unique<frc::DigitalInput>(24));
594 camera_reader.set_activate_passthrough(make_unique<frc::DigitalInput>(25));
Brian Silvermanf8b75252019-02-24 16:13:58 -0800595
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800596 auto imu_trigger = make_unique<frc::DigitalInput>(0);
Sabina Davisabeae332019-02-01 21:12:57 -0800597 ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1,
598 imu_trigger.get());
Brian Silvermanf8b75252019-02-24 16:13:58 -0800599 imu.set_spi_idle_callback(
600 [&camera_reader]() { camera_reader.DoSpiTransaction(); });
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800601 auto imu_reset = make_unique<frc::DigitalOutput>(1);
Sabina Davisabeae332019-02-01 21:12:57 -0800602 imu.set_reset(imu_reset.get());
603 ::std::thread imu_thread(::std::ref(imu));
604
605 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
606 // they are identical, as far as DrivetrainWriter is concerned, to the SP
607 // variety so all the Victors are written as SPs.
608
Sabina Davisd004fd62019-02-02 23:51:46 -0800609 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
610 drivetrain_writer.set_left_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800611 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
Sabina Davisd004fd62019-02-02 23:51:46 -0800612 drivetrain_writer.set_right_controller0(
Sabina Davis1b84afa2019-02-09 01:20:21 -0800613 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
Sabina Davisabeae332019-02-01 21:12:57 -0800614 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
615
Alex Perry5fb5ff22019-02-09 21:53:17 -0800616 SuperstructureWriter superstructure_writer;
617 superstructure_writer.set_elevator_victor(
Alex Perry5fb5ff22019-02-09 21:53:17 -0800618 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800619 // TODO(austin): Do the vacuum
Austin Schuh461e1182019-02-17 14:56:44 -0800620 superstructure_writer.set_suction_victor(
621 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800622 superstructure_writer.set_intake_victor(
623 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800624 superstructure_writer.set_wrist_victor(
625 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
626 superstructure_writer.set_stilts_victor(
Austin Schuh3e3d4ba2019-02-15 23:14:52 -0800627 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Alex Perry5fb5ff22019-02-09 21:53:17 -0800628
629 ::std::thread superstructure_writer_thread(
630 ::std::ref(superstructure_writer));
631
Austin Schuhc1d6f832019-02-15 23:22:17 -0800632 SolenoidWriter solenoid_writer;
633 solenoid_writer.set_intake_roller_talon(
634 make_unique<::ctre::phoenix::motorcontrol::can::TalonSRX>(10));
Austin Schuh461e1182019-02-17 14:56:44 -0800635 solenoid_writer.set_big_suction_cup(0, 1);
636 solenoid_writer.set_small_suction_cup(2, 3);
Austin Schuhc1d6f832019-02-15 23:22:17 -0800637
638 ::std::thread solenoid_writer_thread(::std::ref(solenoid_writer));
639
Sabina Davisabeae332019-02-01 21:12:57 -0800640 // Wait forever. Not much else to do...
641 while (true) {
642 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
643 if (r != 0) {
644 PLOG(WARNING, "infinite select failed");
645 } else {
646 PLOG(WARNING, "infinite select succeeded??\n");
647 }
648 }
649
650 LOG(ERROR, "Exiting WPILibRobot\n");
651
Austin Schuhc1d6f832019-02-15 23:22:17 -0800652 solenoid_writer.Quit();
653 solenoid_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800654 joystick_sender.Quit();
655 joystick_thread.join();
656 pdp_fetcher.Quit();
657 pdp_fetcher_thread.join();
658 reader.Quit();
659 reader_thread.join();
660 imu.Quit();
661 imu_thread.join();
662
663 drivetrain_writer.Quit();
664 drivetrain_writer_thread.join();
Alex Perry5fb5ff22019-02-09 21:53:17 -0800665 superstructure_writer.Quit();
666 superstructure_writer_thread.join();
Sabina Davisabeae332019-02-01 21:12:57 -0800667
668 ::aos::Cleanup();
669 }
670};
671
672} // namespace
673} // namespace wpilib
674} // namespace y2019
675
676AOS_ROBOT_CLASS(::y2019::wpilib::WPILibRobot);