blob: b43f42168c82c801cc41d55c0d4073ef3ff1daba [file] [log] [blame]
Austin Schuh2a3e0632018-02-19 16:24:49 -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 "AnalogInput.h"
14#include "Counter.h"
15#include "DigitalGlitchFilter.h"
16#include "DriverStation.h"
17#include "Encoder.h"
18#include "Relay.h"
19#include "Servo.h"
20#include "VictorSP.h"
21#undef ERROR
22
23#include "aos/common/commonmath.h"
24#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
26#include "aos/common/messages/robot_state.q.h"
27#include "aos/common/stl_mutex.h"
28#include "aos/common/time.h"
29#include "aos/common/util/compiler_memory_barrier.h"
30#include "aos/common/util/log_interval.h"
31#include "aos/common/util/phased_loop.h"
32#include "aos/common/util/wrapping_counter.h"
33#include "aos/linux_code/init.h"
34
35#include "frc971/autonomous/auto.q.h"
36#include "frc971/control_loops/control_loops.q.h"
37#include "frc971/control_loops/drivetrain/drivetrain.q.h"
38#include "frc971/wpilib/ADIS16448.h"
39#include "frc971/wpilib/buffered_pcm.h"
40#include "frc971/wpilib/buffered_solenoid.h"
41#include "frc971/wpilib/dma.h"
42#include "frc971/wpilib/dma_edge_counting.h"
43#include "frc971/wpilib/encoder_and_potentiometer.h"
44#include "frc971/wpilib/interrupt_edge_counting.h"
45#include "frc971/wpilib/joystick_sender.h"
46#include "frc971/wpilib/logging.q.h"
47#include "frc971/wpilib/loop_output_handler.h"
48#include "frc971/wpilib/pdp_fetcher.h"
49#include "frc971/wpilib/wpilib_interface.h"
50#include "frc971/wpilib/wpilib_robot_base.h"
51#include "y2018/constants.h"
52#include "y2018/control_loops/superstructure/superstructure.q.h"
53
54#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
58using ::frc971::control_loops::drivetrain_queue;
59using ::y2018::control_loops::superstructure_queue;
60using ::y2018::constants::Values;
61using ::aos::monotonic_clock;
62namespace chrono = ::std::chrono;
63
64namespace y2018 {
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): Replace this with ::std::make_unique once all our toolchains
75// have support.
76
77template <class T, class... U>
78std::unique_ptr<T> make_unique(U &&... u) {
79 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
80}
81
82// TODO(brian): Use ::std::max instead once we have C++14 so that can be
83// constexpr.
84
85template <typename T>
86constexpr T max(T a, T b) {
87 return (a > b) ? a : b;
88}
89
90template <typename T, typename... Rest>
91constexpr T max(T a, T b, T c, Rest... rest) {
92 return max(max(a, b), c, rest...);
93}
94
95double drivetrain_translate(int32_t in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080096 return ((static_cast<double>(in) /
97 Values::kDrivetrainEncoderCountsPerRevolution()) *
98 (2.0 * M_PI)) *
99 Values::kDrivetrainEncoderRatio() *
100 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800101}
102
103double drivetrain_velocity_translate(double in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -0800104 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
105 (2.0 * M_PI)) *
106 Values::kDrivetrainEncoderRatio() *
107 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800108}
109
110double proximal_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800111 return -voltage * Values::kProximalPotRatio() *
Austin Schuh2a3e0632018-02-19 16:24:49 -0800112 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
113}
114
115double distal_pot_translate(double voltage) {
116 return voltage * Values::kDistalPotRatio() *
117 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
118}
119
120double intake_pot_translate(double voltage) {
121 return voltage * Values::kIntakeMotorPotRatio() *
122 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
123}
124
125double intake_spring_translate(double voltage) {
126 return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) /
127 (5.0 /*volts*/);
128}
129
130// TODO() figure out differnce between max and min voltages on shifter pots.
131// Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be
132// passed drectly into the drivetrain position queue.
133double drivetrain_shifter_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800134 return (voltage - Values::kDrivetrainShifterPotMinVoltage()) /
135 (Values::kDrivetrainShifterPotMaxVoltage() -
136 Values::kDrivetrainShifterPotMinVoltage());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800137}
138
139constexpr double kMaxFastEncoderPulsesPerSecond =
140 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
141 Values::kMaxIntakeMotorEncoderPulsesPerSecond());
142static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
143 "fast encoders are too fast");
144
145constexpr double kMaxMediumEncoderPulsesPerSecond =
146 max(Values::kMaxProximalEncoderPulsesPerSecond(),
147 Values::kMaxDistalEncoderPulsesPerSecond());
148static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
149 "medium encoders are too fast");
150
151// Class to send position messages with sensor readings to our loops.
152class SensorReader {
153 public:
154 SensorReader() {
155 // Set to filter out anything shorter than 1/4 of the minimum pulse width
156 // we should ever see.
157 fast_encoder_filter_.SetPeriodNanoSeconds(
158 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
159 kMaxFastEncoderPulsesPerSecond * 1e9 +
160 0.5));
161 medium_encoder_filter_.SetPeriodNanoSeconds(
162 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
163 kMaxMediumEncoderPulsesPerSecond * 1e9 +
164 0.5));
165 hall_filter_.SetPeriodNanoSeconds(100000);
166 }
167
168 // Left drivetrain side.
169 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
170 fast_encoder_filter_.Add(encoder.get());
171 drivetrain_left_encoder_ = ::std::move(encoder);
172 }
173
174 void set_left_drivetrain_shifter_potentiometer(
175 ::std::unique_ptr<AnalogInput> potentiometer) {
176 left_drivetrain_shifter_ = ::std::move(potentiometer);
177 }
178
179 // Right drivetrain side.
180 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
181 fast_encoder_filter_.Add(encoder.get());
182 drivetrain_right_encoder_ = ::std::move(encoder);
183 }
184
185 void set_right_drivetrain_shifter_potentiometer(
186 ::std::unique_ptr<AnalogInput> potentiometer) {
187 right_drivetrain_shifter_ = ::std::move(potentiometer);
188 }
189
190 // Proximal joint.
191 void set_proximal_encoder(::std::unique_ptr<Encoder> encoder) {
192 medium_encoder_filter_.Add(encoder.get());
193 proximal_encoder_.set_encoder(::std::move(encoder));
194 }
195
196 void set_proximal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) {
197 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
198 }
199
200 void set_proximal_potentiometer(
201 ::std::unique_ptr<AnalogInput> potentiometer) {
202 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
203 }
204
205 // Distal joint.
206 void set_distal_encoder(::std::unique_ptr<Encoder> encoder) {
207 medium_encoder_filter_.Add(encoder.get());
208 distal_encoder_.set_encoder(::std::move(encoder));
209 }
210
211 void set_distal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) {
212 fast_encoder_filter_.Add(absolute_pwm.get());
213 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
214 }
215
216 void set_distal_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
217 distal_encoder_.set_potentiometer(::std::move(potentiometer));
218 }
219
220 // Left intake side.
221 void set_left_intake_encoder(::std::unique_ptr<Encoder> encoder) {
222 fast_encoder_filter_.Add(encoder.get());
223 left_intake_encoder_.set_encoder(::std::move(encoder));
224 }
225
226 void set_left_intake_absolute_pwm(
227 ::std::unique_ptr<DigitalInput> absolute_pwm) {
228 fast_encoder_filter_.Add(absolute_pwm.get());
229 left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
230 }
231
232 void set_left_intake_potentiometer(
233 ::std::unique_ptr<AnalogInput> potentiometer) {
234 left_intake_encoder_.set_potentiometer(::std::move(potentiometer));
235 }
236
237 void set_left_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) {
238 left_intake_spring_angle_ = ::std::move(encoder);
239 }
240
241 void set_left_intake_cube_detector(::std::unique_ptr<DigitalInput> input) {
242 left_intake_cube_detector_ = ::std::move(input);
243 }
244
245 // Right intake side.
246 void set_right_intake_encoder(::std::unique_ptr<Encoder> encoder) {
247 fast_encoder_filter_.Add(encoder.get());
248 right_intake_encoder_.set_encoder(::std::move(encoder));
249 }
250
251 void set_right_intake_absolute_pwm(
252 ::std::unique_ptr<DigitalInput> absolute_pwm) {
253 fast_encoder_filter_.Add(absolute_pwm.get());
254 right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
255 }
256
257 void set_right_intake_potentiometer(
258 ::std::unique_ptr<AnalogInput> potentiometer) {
259 right_intake_encoder_.set_potentiometer(::std::move(potentiometer));
260 }
261
262 void set_right_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) {
263 right_intake_spring_angle_ = ::std::move(encoder);
264 }
265
266 void set_right_intake_cube_detector(::std::unique_ptr<DigitalInput> input) {
267 right_intake_cube_detector_ = ::std::move(input);
268 }
269
Austin Schuh4ef51af2018-03-04 01:08:45 -0800270 void set_claw_beambreak(::std::unique_ptr<DigitalInput> input) {
271 claw_beambreak_ = ::std::move(input);
272 }
273
274 void set_box_back_beambreak(::std::unique_ptr<DigitalInput> input) {
275 box_back_beambreak_ = ::std::move(input);
276 }
277
Austin Schuh2a3e0632018-02-19 16:24:49 -0800278 // Auto mode switches.
279 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
280 autonomous_modes_.at(i) = ::std::move(sensor);
281 }
282
283 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
284 medium_encoder_filter_.Add(pwm_trigger.get());
285 pwm_trigger_ = ::std::move(pwm_trigger);
286 }
287
288 // All of the DMA-related set_* calls must be made before this, and it
289 // doesn't hurt to do all of them.
290 void set_dma(::std::unique_ptr<DMA> dma) {
291 dma_synchronizer_.reset(
292 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
293 }
294
295 void RunPWMDetecter() {
296 ::aos::SetCurrentThreadRealtimePriority(41);
297
298 pwm_trigger_->RequestInterrupts();
299 // Rising edge only.
300 pwm_trigger_->SetUpSourceEdge(true, false);
301
302 monotonic_clock::time_point last_posedge_monotonic =
303 monotonic_clock::min_time;
304
305 while (run_) {
306 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
307 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
308 // Grab all the clocks.
309 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
310
311 aos_compiler_memory_barrier();
312 const double fpga_time_before = GetFPGATime() * 1e-6;
313 aos_compiler_memory_barrier();
314 const monotonic_clock::time_point monotonic_now =
315 monotonic_clock::now();
316 aos_compiler_memory_barrier();
317 const double fpga_time_after = GetFPGATime() * 1e-6;
318 aos_compiler_memory_barrier();
319
320 const double fpga_offset =
321 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
322
323 // Compute when the edge was.
324 const monotonic_clock::time_point monotonic_edge =
325 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
326 chrono::duration<double>(fpga_offset));
327
328 LOG(DEBUG, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
329 fpga_time_after - fpga_time_before, fpga_offset,
330 monotonic_edge.time_since_epoch().count());
331
332 // Compute bounds on the timestep and sampling times.
333 const double fpga_sample_length = fpga_time_after - fpga_time_before;
334 const chrono::nanoseconds elapsed_time =
335 monotonic_edge - last_posedge_monotonic;
336
337 last_posedge_monotonic = monotonic_edge;
338
339 // Verify that the values are sane.
340 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
341 continue;
342 }
343 if (fpga_offset < 0 || fpga_offset > 0.00015) {
344 continue;
345 }
346 if (elapsed_time >
347 chrono::microseconds(5050) + chrono::microseconds(4) ||
348 elapsed_time <
349 chrono::microseconds(5050) - chrono::microseconds(4)) {
350 continue;
351 }
352 // Good edge!
353 {
354 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
355 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
356 last_period_ = elapsed_time;
357 }
358 } else {
359 LOG(INFO, "PWM triggered %d\n", ret);
360 }
361 }
362 pwm_trigger_->CancelInterrupts();
363 }
364
365 void operator()() {
366 ::aos::SetCurrentThreadName("SensorReader");
367
368 my_pid_ = getpid();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800369
370 dma_synchronizer_->Start();
371
372 ::aos::time::PhasedLoop phased_loop(last_period_,
373 ::std::chrono::milliseconds(3));
374 chrono::nanoseconds filtered_period = last_period_;
375
376 ::std::thread pwm_detecter_thread(
377 ::std::bind(&SensorReader::RunPWMDetecter, this));
378
379 ::aos::SetCurrentThreadRealtimePriority(40);
380 while (run_) {
381 {
382 const int iterations = phased_loop.SleepUntilNext();
383 if (iterations != 1) {
384 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
385 }
386 }
387 RunIteration();
388
389 monotonic_clock::time_point last_tick_timepoint;
390 chrono::nanoseconds period;
391 {
392 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
393 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
394 period = last_period_;
395 }
396
397 if (last_tick_timepoint == monotonic_clock::min_time) {
398 continue;
399 }
400 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
401 period, last_tick_timepoint + chrono::microseconds(2050));
402
403 // TODO(austin): If this is the first edge in a while, skip to it (plus
404 // an offset). Otherwise, slowly drift time to line up.
405
406 phased_loop.set_interval_and_offset(period, new_offset);
407 }
408 pwm_detecter_thread.join();
409 }
410
411 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700412 ::frc971::wpilib::SendRobotState(my_pid_);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800413
414 const auto values = constants::GetValues();
415
416 {
417 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
Austin Schuh6829f762018-03-02 21:36:01 -0800418 drivetrain_message->left_encoder =
419 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
420 drivetrain_message->left_speed =
421 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800422 drivetrain_message->left_shifter_position =
423 drivetrain_shifter_pot_translate(
424 left_drivetrain_shifter_->GetVoltage());
425
Austin Schuh6829f762018-03-02 21:36:01 -0800426 drivetrain_message->right_encoder =
427 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
428 drivetrain_message->right_speed =
429 -drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800430 drivetrain_message->right_shifter_position =
431 drivetrain_shifter_pot_translate(
432 right_drivetrain_shifter_->GetVoltage());
433
434 drivetrain_message.Send();
435 }
436
437 dma_synchronizer_->RunIteration();
438
439 {
440 auto superstructure_message = superstructure_queue.position.MakeMessage();
441
442 CopyPosition(proximal_encoder_, &superstructure_message->arm.proximal,
443 Values::kProximalEncoderCountsPerRevolution(),
444 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800445 true, values.arm_proximal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800446
447 CopyPosition(distal_encoder_, &superstructure_message->arm.distal,
448 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh6829f762018-03-02 21:36:01 -0800449 Values::kDistalEncoderRatio(), distal_pot_translate, true,
450 values.arm_distal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800451
452 CopyPosition(left_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800453 &superstructure_message->left_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800454 Values::kIntakeMotorEncoderCountsPerRevolution(),
455 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Sabina Davis8d20ca82018-02-19 13:17:45 -0800456 false, values.left_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800457
458 CopyPosition(right_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800459 &superstructure_message->right_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800460 Values::kIntakeMotorEncoderCountsPerRevolution(),
461 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800462 true, values.right_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800463
Sabina Daviscfb872f2018-02-25 16:28:20 -0800464 superstructure_message->left_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800465 intake_spring_translate(left_intake_spring_angle_->GetVoltage()) +
466 values.left_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800467 superstructure_message->left_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800468 left_intake_cube_detector_->Get();
469
Sabina Daviscfb872f2018-02-25 16:28:20 -0800470 superstructure_message->right_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800471 -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) +
472 values.right_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800473 superstructure_message->right_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800474 right_intake_cube_detector_->Get();
475
Austin Schuh96341532018-03-09 21:17:24 -0800476 superstructure_message->claw_beambreak_triggered = !claw_beambreak_->Get();
Austin Schuh4ef51af2018-03-04 01:08:45 -0800477 superstructure_message->box_back_beambreak_triggered =
478 !box_back_beambreak_->Get();
479
Austin Schuh2a3e0632018-02-19 16:24:49 -0800480 superstructure_message.Send();
481 }
482
483 {
484 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
485 auto_mode_message->mode = 0;
486 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
487 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
488 auto_mode_message->mode |= 1 << i;
489 }
490 }
491 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
492 auto_mode_message.Send();
493 }
494 }
495
496 void Quit() { run_ = false; }
497
498 private:
499 double encoder_translate(int32_t value, double counts_per_revolution,
500 double ratio) {
501 return static_cast<double>(value) / counts_per_revolution * ratio *
502 (2.0 * M_PI);
503 }
504
505 void CopyPosition(
506 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
507 ::frc971::PotAndAbsolutePosition *position,
508 double encoder_counts_per_revolution, double encoder_ratio,
509 ::std::function<double(double)> potentiometer_translate, bool reverse,
510 double pot_offset) {
511 const double multiplier = reverse ? -1.0 : 1.0;
512 position->pot = multiplier * potentiometer_translate(
513 encoder.ReadPotentiometerVoltage()) +
514 pot_offset;
515 position->encoder =
516 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
517 encoder_counts_per_revolution,
518 encoder_ratio);
519
520 position->absolute_encoder =
521 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
522 : encoder.ReadAbsoluteEncoder()) *
523 encoder_ratio * (2.0 * M_PI);
524 }
525
526 int32_t my_pid_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800527
528 // Mutex to manage access to the period and tick time variables.
529 ::aos::stl_mutex tick_time_mutex_;
530 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
531 monotonic_clock::min_time;
532 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
533
534 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
535
536 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
537 hall_filter_;
538
539 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
540 drivetrain_right_encoder_;
541
542 ::std::unique_ptr<AnalogInput> left_drivetrain_shifter_,
543 right_drivetrain_shifter_;
544
545 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
546 distal_encoder_;
547
548 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_,
549 right_intake_encoder_;
550
551 ::std::unique_ptr<AnalogInput> left_intake_spring_angle_,
552 right_intake_spring_angle_;
553 ::std::unique_ptr<DigitalInput> left_intake_cube_detector_,
554 right_intake_cube_detector_;
555
Austin Schuh4ef51af2018-03-04 01:08:45 -0800556 ::std::unique_ptr<DigitalInput> claw_beambreak_;
557 ::std::unique_ptr<DigitalInput> box_back_beambreak_;
558
Austin Schuh2a3e0632018-02-19 16:24:49 -0800559 ::std::unique_ptr<DigitalInput> pwm_trigger_;
560
561 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
562
563 ::std::atomic<bool> run_{true};
564};
565
566class SolenoidWriter {
567 public:
568 SolenoidWriter(::frc971::wpilib::BufferedPcm *pcm)
569 : pcm_(pcm),
570 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
571 superstructure_(".y2018.control_loops.superstructure_queue.output") {}
572
573 // left drive
574 // right drive
575 //
576 // claw
577 // arm brakes
578 // hook release
579 // fork release
580 void set_left_drivetrain_shifter(
581 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
582 left_drivetrain_shifter_ = ::std::move(s);
583 }
584 void set_right_drivetrain_shifter(
585 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
586 right_drivetrain_shifter_ = ::std::move(s);
587 }
588
589 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
590 claw_ = ::std::move(s);
591 }
592
593 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
594 arm_brakes_ = ::std::move(s);
595 }
596
597 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
598 hook_ = ::std::move(s);
599 }
600
601 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
602 forks_ = ::std::move(s);
603 }
604
605 void operator()() {
606 ::aos::SetCurrentThreadName("Solenoids");
607 ::aos::SetCurrentThreadRealtimePriority(27);
608
609 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
610 ::std::chrono::milliseconds(1));
611
612 while (run_) {
613 {
614 const int iterations = phased_loop.SleepUntilNext();
615 if (iterations != 1) {
616 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
617 }
618 }
619
620 {
621 drivetrain_.FetchLatest();
622 if (drivetrain_.get()) {
623 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
624 left_drivetrain_shifter_->Set(!drivetrain_->left_high);
625 right_drivetrain_shifter_->Set(!drivetrain_->right_high);
626 }
627 }
628
629 {
630 superstructure_.FetchLatest();
631 if (superstructure_.get()) {
632 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
633
Austin Schuh96341532018-03-09 21:17:24 -0800634 claw_->Set(!superstructure_->claw_grabbed);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800635 arm_brakes_->Set(superstructure_->release_arm_brake);
636 hook_->Set(superstructure_->hook_release);
637 forks_->Set(superstructure_->forks_release);
638 }
639 }
640
641 {
642 ::frc971::wpilib::PneumaticsToLog to_log;
643
644 pcm_->Flush();
645 to_log.read_solenoids = pcm_->GetAll();
646 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
647 }
648 }
649 }
650
651 void Quit() { run_ = false; }
652
653 private:
654 ::frc971::wpilib::BufferedPcm *pcm_;
655
656 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
657 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
658 hook_, forks_;
659
660 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
661 ::aos::Queue<::y2018::control_loops::SuperstructureQueue::Output>
662 superstructure_;
663
664 ::std::atomic<bool> run_{true};
665};
666
667class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
668 public:
669 void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) {
670 drivetrain_left_victor_ = ::std::move(t);
671 }
672
673 void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) {
674 drivetrain_right_victor_ = ::std::move(t);
675 }
676
677 private:
678 virtual void Read() override {
679 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
680 }
681
682 virtual void Write() override {
683 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
684 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh6829f762018-03-02 21:36:01 -0800685 drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0);
686 drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800687 }
688
689 virtual void Stop() override {
690 LOG(WARNING, "drivetrain output too old\n");
691 drivetrain_left_victor_->SetDisabled();
692 drivetrain_right_victor_->SetDisabled();
693 }
694
695 ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_,
696 drivetrain_right_victor_;
697};
698
699class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
700 public:
701 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
702 proximal_victor_ = ::std::move(t);
703 }
704 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
705 distal_victor_ = ::std::move(t);
706 }
707
708 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
709 left_intake_elastic_victor_ = ::std::move(t);
710 }
711 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
712 right_intake_elastic_victor_ = ::std::move(t);
713 }
714
715 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
716 left_intake_rollers_victor_ = ::std::move(t);
717 }
718
719 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
720 right_intake_rollers_victor_ = ::std::move(t);
721 }
722
723 private:
724 virtual void Read() override {
725 ::y2018::control_loops::superstructure_queue.output.FetchAnother();
726 }
727
728 virtual void Write() override {
729 auto &queue = ::y2018::control_loops::superstructure_queue.output;
730 LOG_STRUCT(DEBUG, "will output", *queue);
731
732 left_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800733 ::aos::Clip(-queue->left_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800734 kMaxBringupPower) /
735 12.0);
736
737 right_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800738 ::aos::Clip(queue->right_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800739 kMaxBringupPower) /
740 12.0);
741
742 left_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800743 ::aos::Clip(-queue->left_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800744 kMaxBringupPower) /
745 12.0);
746
747 right_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800748 ::aos::Clip(queue->right_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800749 kMaxBringupPower) /
750 12.0);
751
Austin Schuh6829f762018-03-02 21:36:01 -0800752 proximal_victor_->SetSpeed(::aos::Clip(-queue->voltage_proximal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800753 -kMaxBringupPower,
754 kMaxBringupPower) /
755 12.0);
756
757 distal_victor_->SetSpeed(::aos::Clip(queue->voltage_distal,
758 -kMaxBringupPower, kMaxBringupPower) /
759 12.0);
760 }
761
762 virtual void Stop() override {
763 LOG(WARNING, "Superstructure output too old.\n");
764
765 left_intake_rollers_victor_->SetDisabled();
766 right_intake_rollers_victor_->SetDisabled();
767 left_intake_elastic_victor_->SetDisabled();
768 right_intake_elastic_victor_->SetDisabled();
769
770 proximal_victor_->SetDisabled();
771 distal_victor_->SetDisabled();
772 }
773
774 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
775 right_intake_rollers_victor_, left_intake_elastic_victor_,
776 right_intake_elastic_victor_, proximal_victor_, distal_victor_;
777};
778
779class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
780 public:
781 ::std::unique_ptr<Encoder> make_encoder(int index) {
782 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
783 Encoder::k4X);
784 }
785
786 void Run() override {
787 ::aos::InitNRT();
788 ::aos::SetCurrentThreadName("StartCompetition");
789
790 ::frc971::wpilib::JoystickSender joystick_sender;
791 ::std::thread joystick_thread(::std::ref(joystick_sender));
792
793 ::frc971::wpilib::PDPFetcher pdp_fetcher;
794 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
795 SensorReader reader;
796
797 // TODO(Sabina): Update port numbers(Sensors and Victors)
798 reader.set_drivetrain_left_encoder(make_encoder(0));
Austin Schuh6829f762018-03-02 21:36:01 -0800799 reader.set_left_drivetrain_shifter_potentiometer(
800 make_unique<AnalogInput>(6));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800801 reader.set_drivetrain_right_encoder(make_encoder(1));
Austin Schuh6829f762018-03-02 21:36:01 -0800802 reader.set_right_drivetrain_shifter_potentiometer(
803 make_unique<AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800804
Austin Schuh6829f762018-03-02 21:36:01 -0800805 reader.set_proximal_encoder(make_encoder(4));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800806 reader.set_proximal_absolute_pwm(make_unique<DigitalInput>(2));
807 reader.set_proximal_potentiometer(make_unique<AnalogInput>(2));
808
Austin Schuh6829f762018-03-02 21:36:01 -0800809 reader.set_distal_encoder(make_encoder(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800810 reader.set_distal_absolute_pwm(make_unique<DigitalInput>(3));
811 reader.set_distal_potentiometer(make_unique<AnalogInput>(3));
812
Austin Schuh6829f762018-03-02 21:36:01 -0800813 reader.set_right_intake_encoder(make_encoder(5));
814 reader.set_right_intake_absolute_pwm(make_unique<DigitalInput>(7));
815 reader.set_right_intake_potentiometer(make_unique<AnalogInput>(1));
816 reader.set_right_intake_spring_angle(make_unique<AnalogInput>(5));
817 reader.set_right_intake_cube_detector(make_unique<DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800818
Austin Schuh6829f762018-03-02 21:36:01 -0800819 reader.set_left_intake_encoder(make_encoder(3));
820 reader.set_left_intake_absolute_pwm(make_unique<DigitalInput>(4));
821 reader.set_left_intake_potentiometer(make_unique<AnalogInput>(0));
822 reader.set_left_intake_spring_angle(make_unique<AnalogInput>(4));
823 reader.set_left_intake_cube_detector(make_unique<DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800824
Austin Schuh4ef51af2018-03-04 01:08:45 -0800825 reader.set_claw_beambreak(make_unique<DigitalInput>(8));
826 reader.set_box_back_beambreak(make_unique<DigitalInput>(9));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800827
Austin Schuh6829f762018-03-02 21:36:01 -0800828 reader.set_pwm_trigger(make_unique<DigitalInput>(25));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800829
830 reader.set_dma(make_unique<DMA>());
831 ::std::thread reader_thread(::std::ref(reader));
832
Austin Schuh6829f762018-03-02 21:36:01 -0800833 auto imu_trigger = make_unique<DigitalInput>(5);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800834 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
835 imu.SetDummySPI(SPI::Port::kOnboardCS2);
836 auto imu_reset = make_unique<DigitalOutput>(6);
837 imu.set_reset(imu_reset.get());
838 ::std::thread imu_thread(::std::ref(imu));
839
Austin Schuhe8a54c02018-03-05 00:25:58 -0800840 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
841 // they are identical, as far as DrivetrainWriter is concerned, to the SP
842 // variety so all the Victors are written as SPs.
Austin Schuh2a3e0632018-02-19 16:24:49 -0800843
844 DrivetrainWriter drivetrain_writer;
845 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800846 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800847 drivetrain_writer.set_drivetrain_right_victor(
848 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
849 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
850
851 SuperstructureWriter superstructure_writer;
852 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800853 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800854 superstructure_writer.set_left_intake_rollers_victor(
855 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800856 superstructure_writer.set_right_intake_elastic_victor(
857 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
858 superstructure_writer.set_right_intake_rollers_victor(
859 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800860 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800861 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800862 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800863 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
864
865 // Hanger: victor 8
Austin Schuh2a3e0632018-02-19 16:24:49 -0800866
867 ::std::thread superstructure_writer_thread(
868 ::std::ref(superstructure_writer));
869
870 ::frc971::wpilib::BufferedPcm *pcm = new ::frc971::wpilib::BufferedPcm();
871 SolenoidWriter solenoid_writer(pcm);
872 solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(0));
873 solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1));
874 solenoid_writer.set_claw(pcm->MakeSolenoid(2));
875 solenoid_writer.set_arm_brakes(pcm->MakeSolenoid(3));
876 solenoid_writer.set_hook(pcm->MakeSolenoid(4));
877 solenoid_writer.set_forks(pcm->MakeSolenoid(5));
878
879 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
880
881 // Wait forever. Not much else to do...
882 while (true) {
883 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
884 if (r != 0) {
885 PLOG(WARNING, "infinite select failed");
886 } else {
887 PLOG(WARNING, "infinite select succeeded??\n");
888 }
889 }
890
891 LOG(ERROR, "Exiting WPILibRobot\n");
892
893 joystick_sender.Quit();
894 joystick_thread.join();
895 pdp_fetcher.Quit();
896 pdp_fetcher_thread.join();
897 reader.Quit();
898 reader_thread.join();
899 imu.Quit();
900 imu_thread.join();
901
Austin Schuh2a3e0632018-02-19 16:24:49 -0800902 drivetrain_writer.Quit();
903 drivetrain_writer_thread.join();
904 superstructure_writer.Quit();
905 superstructure_writer_thread.join();
906
907 ::aos::Cleanup();
908 }
909};
910
911} // namespace
912} // namespace wpilib
913} // namespace y2018
914
915AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);