blob: 6ccc6f98f70257c17242680b0bf00d6738e68ac4 [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) {
96 return static_cast<double>(in) /
97 Values::kDrivetrainEncoderCountsPerRevolution() *
98 Values::kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius;
99}
100
101double drivetrain_velocity_translate(double in) {
102 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution() *
103 Values::kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius;
104}
105
106double proximal_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800107 return -voltage * Values::kProximalPotRatio() *
Austin Schuh2a3e0632018-02-19 16:24:49 -0800108 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
109}
110
111double distal_pot_translate(double voltage) {
112 return voltage * Values::kDistalPotRatio() *
113 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
114}
115
116double intake_pot_translate(double voltage) {
117 return voltage * Values::kIntakeMotorPotRatio() *
118 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
119}
120
121double intake_spring_translate(double voltage) {
122 return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) /
123 (5.0 /*volts*/);
124}
125
126// TODO() figure out differnce between max and min voltages on shifter pots.
127// Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be
128// passed drectly into the drivetrain position queue.
129double drivetrain_shifter_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800130 return (voltage - Values::kDrivetrainShifterPotMinVoltage()) /
131 (Values::kDrivetrainShifterPotMaxVoltage() -
132 Values::kDrivetrainShifterPotMinVoltage());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800133}
134
135constexpr double kMaxFastEncoderPulsesPerSecond =
136 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
137 Values::kMaxIntakeMotorEncoderPulsesPerSecond());
138static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
139 "fast encoders are too fast");
140
141constexpr double kMaxMediumEncoderPulsesPerSecond =
142 max(Values::kMaxProximalEncoderPulsesPerSecond(),
143 Values::kMaxDistalEncoderPulsesPerSecond());
144static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
145 "medium encoders are too fast");
146
147// Class to send position messages with sensor readings to our loops.
148class SensorReader {
149 public:
150 SensorReader() {
151 // Set to filter out anything shorter than 1/4 of the minimum pulse width
152 // we should ever see.
153 fast_encoder_filter_.SetPeriodNanoSeconds(
154 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
155 kMaxFastEncoderPulsesPerSecond * 1e9 +
156 0.5));
157 medium_encoder_filter_.SetPeriodNanoSeconds(
158 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
159 kMaxMediumEncoderPulsesPerSecond * 1e9 +
160 0.5));
161 hall_filter_.SetPeriodNanoSeconds(100000);
162 }
163
164 // Left drivetrain side.
165 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
166 fast_encoder_filter_.Add(encoder.get());
167 drivetrain_left_encoder_ = ::std::move(encoder);
168 }
169
170 void set_left_drivetrain_shifter_potentiometer(
171 ::std::unique_ptr<AnalogInput> potentiometer) {
172 left_drivetrain_shifter_ = ::std::move(potentiometer);
173 }
174
175 // Right drivetrain side.
176 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
177 fast_encoder_filter_.Add(encoder.get());
178 drivetrain_right_encoder_ = ::std::move(encoder);
179 }
180
181 void set_right_drivetrain_shifter_potentiometer(
182 ::std::unique_ptr<AnalogInput> potentiometer) {
183 right_drivetrain_shifter_ = ::std::move(potentiometer);
184 }
185
186 // Proximal joint.
187 void set_proximal_encoder(::std::unique_ptr<Encoder> encoder) {
188 medium_encoder_filter_.Add(encoder.get());
189 proximal_encoder_.set_encoder(::std::move(encoder));
190 }
191
192 void set_proximal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) {
193 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
194 }
195
196 void set_proximal_potentiometer(
197 ::std::unique_ptr<AnalogInput> potentiometer) {
198 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
199 }
200
201 // Distal joint.
202 void set_distal_encoder(::std::unique_ptr<Encoder> encoder) {
203 medium_encoder_filter_.Add(encoder.get());
204 distal_encoder_.set_encoder(::std::move(encoder));
205 }
206
207 void set_distal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) {
208 fast_encoder_filter_.Add(absolute_pwm.get());
209 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
210 }
211
212 void set_distal_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
213 distal_encoder_.set_potentiometer(::std::move(potentiometer));
214 }
215
216 // Left intake side.
217 void set_left_intake_encoder(::std::unique_ptr<Encoder> encoder) {
218 fast_encoder_filter_.Add(encoder.get());
219 left_intake_encoder_.set_encoder(::std::move(encoder));
220 }
221
222 void set_left_intake_absolute_pwm(
223 ::std::unique_ptr<DigitalInput> absolute_pwm) {
224 fast_encoder_filter_.Add(absolute_pwm.get());
225 left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
226 }
227
228 void set_left_intake_potentiometer(
229 ::std::unique_ptr<AnalogInput> potentiometer) {
230 left_intake_encoder_.set_potentiometer(::std::move(potentiometer));
231 }
232
233 void set_left_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) {
234 left_intake_spring_angle_ = ::std::move(encoder);
235 }
236
237 void set_left_intake_cube_detector(::std::unique_ptr<DigitalInput> input) {
238 left_intake_cube_detector_ = ::std::move(input);
239 }
240
241 // Right intake side.
242 void set_right_intake_encoder(::std::unique_ptr<Encoder> encoder) {
243 fast_encoder_filter_.Add(encoder.get());
244 right_intake_encoder_.set_encoder(::std::move(encoder));
245 }
246
247 void set_right_intake_absolute_pwm(
248 ::std::unique_ptr<DigitalInput> absolute_pwm) {
249 fast_encoder_filter_.Add(absolute_pwm.get());
250 right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
251 }
252
253 void set_right_intake_potentiometer(
254 ::std::unique_ptr<AnalogInput> potentiometer) {
255 right_intake_encoder_.set_potentiometer(::std::move(potentiometer));
256 }
257
258 void set_right_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) {
259 right_intake_spring_angle_ = ::std::move(encoder);
260 }
261
262 void set_right_intake_cube_detector(::std::unique_ptr<DigitalInput> input) {
263 right_intake_cube_detector_ = ::std::move(input);
264 }
265
266 // Auto mode switches.
267 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
268 autonomous_modes_.at(i) = ::std::move(sensor);
269 }
270
271 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
272 medium_encoder_filter_.Add(pwm_trigger.get());
273 pwm_trigger_ = ::std::move(pwm_trigger);
274 }
275
276 // All of the DMA-related set_* calls must be made before this, and it
277 // doesn't hurt to do all of them.
278 void set_dma(::std::unique_ptr<DMA> dma) {
279 dma_synchronizer_.reset(
280 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
281 }
282
283 void RunPWMDetecter() {
284 ::aos::SetCurrentThreadRealtimePriority(41);
285
286 pwm_trigger_->RequestInterrupts();
287 // Rising edge only.
288 pwm_trigger_->SetUpSourceEdge(true, false);
289
290 monotonic_clock::time_point last_posedge_monotonic =
291 monotonic_clock::min_time;
292
293 while (run_) {
294 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
295 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
296 // Grab all the clocks.
297 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
298
299 aos_compiler_memory_barrier();
300 const double fpga_time_before = GetFPGATime() * 1e-6;
301 aos_compiler_memory_barrier();
302 const monotonic_clock::time_point monotonic_now =
303 monotonic_clock::now();
304 aos_compiler_memory_barrier();
305 const double fpga_time_after = GetFPGATime() * 1e-6;
306 aos_compiler_memory_barrier();
307
308 const double fpga_offset =
309 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
310
311 // Compute when the edge was.
312 const monotonic_clock::time_point monotonic_edge =
313 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
314 chrono::duration<double>(fpga_offset));
315
316 LOG(DEBUG, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
317 fpga_time_after - fpga_time_before, fpga_offset,
318 monotonic_edge.time_since_epoch().count());
319
320 // Compute bounds on the timestep and sampling times.
321 const double fpga_sample_length = fpga_time_after - fpga_time_before;
322 const chrono::nanoseconds elapsed_time =
323 monotonic_edge - last_posedge_monotonic;
324
325 last_posedge_monotonic = monotonic_edge;
326
327 // Verify that the values are sane.
328 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
329 continue;
330 }
331 if (fpga_offset < 0 || fpga_offset > 0.00015) {
332 continue;
333 }
334 if (elapsed_time >
335 chrono::microseconds(5050) + chrono::microseconds(4) ||
336 elapsed_time <
337 chrono::microseconds(5050) - chrono::microseconds(4)) {
338 continue;
339 }
340 // Good edge!
341 {
342 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
343 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
344 last_period_ = elapsed_time;
345 }
346 } else {
347 LOG(INFO, "PWM triggered %d\n", ret);
348 }
349 }
350 pwm_trigger_->CancelInterrupts();
351 }
352
353 void operator()() {
354 ::aos::SetCurrentThreadName("SensorReader");
355
356 my_pid_ = getpid();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800357
358 dma_synchronizer_->Start();
359
360 ::aos::time::PhasedLoop phased_loop(last_period_,
361 ::std::chrono::milliseconds(3));
362 chrono::nanoseconds filtered_period = last_period_;
363
364 ::std::thread pwm_detecter_thread(
365 ::std::bind(&SensorReader::RunPWMDetecter, this));
366
367 ::aos::SetCurrentThreadRealtimePriority(40);
368 while (run_) {
369 {
370 const int iterations = phased_loop.SleepUntilNext();
371 if (iterations != 1) {
372 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
373 }
374 }
375 RunIteration();
376
377 monotonic_clock::time_point last_tick_timepoint;
378 chrono::nanoseconds period;
379 {
380 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
381 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
382 period = last_period_;
383 }
384
385 if (last_tick_timepoint == monotonic_clock::min_time) {
386 continue;
387 }
388 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
389 period, last_tick_timepoint + chrono::microseconds(2050));
390
391 // TODO(austin): If this is the first edge in a while, skip to it (plus
392 // an offset). Otherwise, slowly drift time to line up.
393
394 phased_loop.set_interval_and_offset(period, new_offset);
395 }
396 pwm_detecter_thread.join();
397 }
398
399 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700400 ::frc971::wpilib::SendRobotState(my_pid_);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800401
402 const auto values = constants::GetValues();
403
404 {
405 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
Austin Schuh6829f762018-03-02 21:36:01 -0800406 drivetrain_message->left_encoder =
407 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
408 drivetrain_message->left_speed =
409 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800410 drivetrain_message->left_shifter_position =
411 drivetrain_shifter_pot_translate(
412 left_drivetrain_shifter_->GetVoltage());
413
Austin Schuh6829f762018-03-02 21:36:01 -0800414 drivetrain_message->right_encoder =
415 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
416 drivetrain_message->right_speed =
417 -drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800418 drivetrain_message->right_shifter_position =
419 drivetrain_shifter_pot_translate(
420 right_drivetrain_shifter_->GetVoltage());
421
422 drivetrain_message.Send();
423 }
424
425 dma_synchronizer_->RunIteration();
426
427 {
428 auto superstructure_message = superstructure_queue.position.MakeMessage();
429
430 CopyPosition(proximal_encoder_, &superstructure_message->arm.proximal,
431 Values::kProximalEncoderCountsPerRevolution(),
432 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800433 true, values.arm_proximal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800434
435 CopyPosition(distal_encoder_, &superstructure_message->arm.distal,
436 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh6829f762018-03-02 21:36:01 -0800437 Values::kDistalEncoderRatio(), distal_pot_translate, true,
438 values.arm_distal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800439
440 CopyPosition(left_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800441 &superstructure_message->left_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800442 Values::kIntakeMotorEncoderCountsPerRevolution(),
443 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Sabina Davis8d20ca82018-02-19 13:17:45 -0800444 false, values.left_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800445
446 CopyPosition(right_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800447 &superstructure_message->right_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800448 Values::kIntakeMotorEncoderCountsPerRevolution(),
449 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800450 true, values.right_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800451
Sabina Daviscfb872f2018-02-25 16:28:20 -0800452 superstructure_message->left_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800453 intake_spring_translate(left_intake_spring_angle_->GetVoltage()) +
454 values.left_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800455 superstructure_message->left_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800456 left_intake_cube_detector_->Get();
457
Sabina Daviscfb872f2018-02-25 16:28:20 -0800458 superstructure_message->right_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800459 -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) +
460 values.right_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800461 superstructure_message->right_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800462 right_intake_cube_detector_->Get();
463
464 superstructure_message.Send();
465 }
466
467 {
468 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
469 auto_mode_message->mode = 0;
470 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
471 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
472 auto_mode_message->mode |= 1 << i;
473 }
474 }
475 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
476 auto_mode_message.Send();
477 }
478 }
479
480 void Quit() { run_ = false; }
481
482 private:
483 double encoder_translate(int32_t value, double counts_per_revolution,
484 double ratio) {
485 return static_cast<double>(value) / counts_per_revolution * ratio *
486 (2.0 * M_PI);
487 }
488
489 void CopyPosition(
490 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
491 ::frc971::PotAndAbsolutePosition *position,
492 double encoder_counts_per_revolution, double encoder_ratio,
493 ::std::function<double(double)> potentiometer_translate, bool reverse,
494 double pot_offset) {
495 const double multiplier = reverse ? -1.0 : 1.0;
496 position->pot = multiplier * potentiometer_translate(
497 encoder.ReadPotentiometerVoltage()) +
498 pot_offset;
499 position->encoder =
500 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
501 encoder_counts_per_revolution,
502 encoder_ratio);
503
504 position->absolute_encoder =
505 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
506 : encoder.ReadAbsoluteEncoder()) *
507 encoder_ratio * (2.0 * M_PI);
508 }
509
510 int32_t my_pid_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800511
512 // Mutex to manage access to the period and tick time variables.
513 ::aos::stl_mutex tick_time_mutex_;
514 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
515 monotonic_clock::min_time;
516 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
517
518 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
519
520 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
521 hall_filter_;
522
523 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
524 drivetrain_right_encoder_;
525
526 ::std::unique_ptr<AnalogInput> left_drivetrain_shifter_,
527 right_drivetrain_shifter_;
528
529 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
530 distal_encoder_;
531
532 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_,
533 right_intake_encoder_;
534
535 ::std::unique_ptr<AnalogInput> left_intake_spring_angle_,
536 right_intake_spring_angle_;
537 ::std::unique_ptr<DigitalInput> left_intake_cube_detector_,
538 right_intake_cube_detector_;
539
540 ::std::unique_ptr<DigitalInput> pwm_trigger_;
541
542 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
543
544 ::std::atomic<bool> run_{true};
545};
546
547class SolenoidWriter {
548 public:
549 SolenoidWriter(::frc971::wpilib::BufferedPcm *pcm)
550 : pcm_(pcm),
551 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
552 superstructure_(".y2018.control_loops.superstructure_queue.output") {}
553
554 // left drive
555 // right drive
556 //
557 // claw
558 // arm brakes
559 // hook release
560 // fork release
561 void set_left_drivetrain_shifter(
562 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
563 left_drivetrain_shifter_ = ::std::move(s);
564 }
565 void set_right_drivetrain_shifter(
566 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
567 right_drivetrain_shifter_ = ::std::move(s);
568 }
569
570 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
571 claw_ = ::std::move(s);
572 }
573
574 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
575 arm_brakes_ = ::std::move(s);
576 }
577
578 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
579 hook_ = ::std::move(s);
580 }
581
582 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
583 forks_ = ::std::move(s);
584 }
585
586 void operator()() {
587 ::aos::SetCurrentThreadName("Solenoids");
588 ::aos::SetCurrentThreadRealtimePriority(27);
589
590 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
591 ::std::chrono::milliseconds(1));
592
593 while (run_) {
594 {
595 const int iterations = phased_loop.SleepUntilNext();
596 if (iterations != 1) {
597 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
598 }
599 }
600
601 {
602 drivetrain_.FetchLatest();
603 if (drivetrain_.get()) {
604 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
605 left_drivetrain_shifter_->Set(!drivetrain_->left_high);
606 right_drivetrain_shifter_->Set(!drivetrain_->right_high);
607 }
608 }
609
610 {
611 superstructure_.FetchLatest();
612 if (superstructure_.get()) {
613 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
614
615 claw_->Set(superstructure_->claw_grabbed);
616 arm_brakes_->Set(superstructure_->release_arm_brake);
617 hook_->Set(superstructure_->hook_release);
618 forks_->Set(superstructure_->forks_release);
619 }
620 }
621
622 {
623 ::frc971::wpilib::PneumaticsToLog to_log;
624
625 pcm_->Flush();
626 to_log.read_solenoids = pcm_->GetAll();
627 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
628 }
629 }
630 }
631
632 void Quit() { run_ = false; }
633
634 private:
635 ::frc971::wpilib::BufferedPcm *pcm_;
636
637 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
638 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
639 hook_, forks_;
640
641 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
642 ::aos::Queue<::y2018::control_loops::SuperstructureQueue::Output>
643 superstructure_;
644
645 ::std::atomic<bool> run_{true};
646};
647
648class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
649 public:
650 void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) {
651 drivetrain_left_victor_ = ::std::move(t);
652 }
653
654 void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) {
655 drivetrain_right_victor_ = ::std::move(t);
656 }
657
658 private:
659 virtual void Read() override {
660 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
661 }
662
663 virtual void Write() override {
664 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
665 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh6829f762018-03-02 21:36:01 -0800666 drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0);
667 drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800668 }
669
670 virtual void Stop() override {
671 LOG(WARNING, "drivetrain output too old\n");
672 drivetrain_left_victor_->SetDisabled();
673 drivetrain_right_victor_->SetDisabled();
674 }
675
676 ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_,
677 drivetrain_right_victor_;
678};
679
680class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
681 public:
682 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
683 proximal_victor_ = ::std::move(t);
684 }
685 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
686 distal_victor_ = ::std::move(t);
687 }
688
689 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
690 left_intake_elastic_victor_ = ::std::move(t);
691 }
692 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
693 right_intake_elastic_victor_ = ::std::move(t);
694 }
695
696 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
697 left_intake_rollers_victor_ = ::std::move(t);
698 }
699
700 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
701 right_intake_rollers_victor_ = ::std::move(t);
702 }
703
704 private:
705 virtual void Read() override {
706 ::y2018::control_loops::superstructure_queue.output.FetchAnother();
707 }
708
709 virtual void Write() override {
710 auto &queue = ::y2018::control_loops::superstructure_queue.output;
711 LOG_STRUCT(DEBUG, "will output", *queue);
712
713 left_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800714 ::aos::Clip(-queue->left_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800715 kMaxBringupPower) /
716 12.0);
717
718 right_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800719 ::aos::Clip(queue->right_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800720 kMaxBringupPower) /
721 12.0);
722
723 left_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800724 ::aos::Clip(-queue->left_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800725 kMaxBringupPower) /
726 12.0);
727
728 right_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800729 ::aos::Clip(queue->right_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800730 kMaxBringupPower) /
731 12.0);
732
Austin Schuh6829f762018-03-02 21:36:01 -0800733 proximal_victor_->SetSpeed(::aos::Clip(-queue->voltage_proximal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800734 -kMaxBringupPower,
735 kMaxBringupPower) /
736 12.0);
737
738 distal_victor_->SetSpeed(::aos::Clip(queue->voltage_distal,
739 -kMaxBringupPower, kMaxBringupPower) /
740 12.0);
741 }
742
743 virtual void Stop() override {
744 LOG(WARNING, "Superstructure output too old.\n");
745
746 left_intake_rollers_victor_->SetDisabled();
747 right_intake_rollers_victor_->SetDisabled();
748 left_intake_elastic_victor_->SetDisabled();
749 right_intake_elastic_victor_->SetDisabled();
750
751 proximal_victor_->SetDisabled();
752 distal_victor_->SetDisabled();
753 }
754
755 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
756 right_intake_rollers_victor_, left_intake_elastic_victor_,
757 right_intake_elastic_victor_, proximal_victor_, distal_victor_;
758};
759
760class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
761 public:
762 ::std::unique_ptr<Encoder> make_encoder(int index) {
763 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
764 Encoder::k4X);
765 }
766
767 void Run() override {
768 ::aos::InitNRT();
769 ::aos::SetCurrentThreadName("StartCompetition");
770
771 ::frc971::wpilib::JoystickSender joystick_sender;
772 ::std::thread joystick_thread(::std::ref(joystick_sender));
773
774 ::frc971::wpilib::PDPFetcher pdp_fetcher;
775 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
776 SensorReader reader;
777
778 // TODO(Sabina): Update port numbers(Sensors and Victors)
779 reader.set_drivetrain_left_encoder(make_encoder(0));
Austin Schuh6829f762018-03-02 21:36:01 -0800780 reader.set_left_drivetrain_shifter_potentiometer(
781 make_unique<AnalogInput>(6));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800782 reader.set_drivetrain_right_encoder(make_encoder(1));
Austin Schuh6829f762018-03-02 21:36:01 -0800783 reader.set_right_drivetrain_shifter_potentiometer(
784 make_unique<AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800785
Austin Schuh6829f762018-03-02 21:36:01 -0800786 reader.set_proximal_encoder(make_encoder(4));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800787 reader.set_proximal_absolute_pwm(make_unique<DigitalInput>(2));
788 reader.set_proximal_potentiometer(make_unique<AnalogInput>(2));
789
Austin Schuh6829f762018-03-02 21:36:01 -0800790 reader.set_distal_encoder(make_encoder(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800791 reader.set_distal_absolute_pwm(make_unique<DigitalInput>(3));
792 reader.set_distal_potentiometer(make_unique<AnalogInput>(3));
793
Austin Schuh6829f762018-03-02 21:36:01 -0800794 reader.set_right_intake_encoder(make_encoder(5));
795 reader.set_right_intake_absolute_pwm(make_unique<DigitalInput>(7));
796 reader.set_right_intake_potentiometer(make_unique<AnalogInput>(1));
797 reader.set_right_intake_spring_angle(make_unique<AnalogInput>(5));
798 reader.set_right_intake_cube_detector(make_unique<DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800799
Austin Schuh6829f762018-03-02 21:36:01 -0800800 reader.set_left_intake_encoder(make_encoder(3));
801 reader.set_left_intake_absolute_pwm(make_unique<DigitalInput>(4));
802 reader.set_left_intake_potentiometer(make_unique<AnalogInput>(0));
803 reader.set_left_intake_spring_angle(make_unique<AnalogInput>(4));
804 reader.set_left_intake_cube_detector(make_unique<DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800805
806 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
807 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
808
Austin Schuh6829f762018-03-02 21:36:01 -0800809 reader.set_pwm_trigger(make_unique<DigitalInput>(25));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800810
811 reader.set_dma(make_unique<DMA>());
812 ::std::thread reader_thread(::std::ref(reader));
813
Austin Schuh6829f762018-03-02 21:36:01 -0800814 auto imu_trigger = make_unique<DigitalInput>(5);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800815 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
816 imu.SetDummySPI(SPI::Port::kOnboardCS2);
817 auto imu_reset = make_unique<DigitalOutput>(6);
818 imu.set_reset(imu_reset.get());
819 ::std::thread imu_thread(::std::ref(imu));
820
821// While as of 2/9/18 the drivetrain Victors are SPX, it appears as though they
822// are identical, as far as DrivetrainWriter is concerned, to the SP variety
823// so all the Victors are written as SPs.
824
825 DrivetrainWriter drivetrain_writer;
826 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800827 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800828 drivetrain_writer.set_drivetrain_right_victor(
829 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
830 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
831
832 SuperstructureWriter superstructure_writer;
833 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800834 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800835 superstructure_writer.set_left_intake_rollers_victor(
836 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800837 superstructure_writer.set_right_intake_elastic_victor(
838 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
839 superstructure_writer.set_right_intake_rollers_victor(
840 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800841 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800842 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800843 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800844 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
845
846 // Hanger: victor 8
Austin Schuh2a3e0632018-02-19 16:24:49 -0800847
848 ::std::thread superstructure_writer_thread(
849 ::std::ref(superstructure_writer));
850
851 ::frc971::wpilib::BufferedPcm *pcm = new ::frc971::wpilib::BufferedPcm();
852 SolenoidWriter solenoid_writer(pcm);
853 solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(0));
854 solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1));
855 solenoid_writer.set_claw(pcm->MakeSolenoid(2));
856 solenoid_writer.set_arm_brakes(pcm->MakeSolenoid(3));
857 solenoid_writer.set_hook(pcm->MakeSolenoid(4));
858 solenoid_writer.set_forks(pcm->MakeSolenoid(5));
859
860 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
861
862 // Wait forever. Not much else to do...
863 while (true) {
864 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
865 if (r != 0) {
866 PLOG(WARNING, "infinite select failed");
867 } else {
868 PLOG(WARNING, "infinite select succeeded??\n");
869 }
870 }
871
872 LOG(ERROR, "Exiting WPILibRobot\n");
873
874 joystick_sender.Quit();
875 joystick_thread.join();
876 pdp_fetcher.Quit();
877 pdp_fetcher_thread.join();
878 reader.Quit();
879 reader_thread.join();
880 imu.Quit();
881 imu_thread.join();
882
883 drivetrain_writer.Quit();
884 drivetrain_writer_thread.join();
885 superstructure_writer.Quit();
886 superstructure_writer_thread.join();
887
888 ::aos::Cleanup();
889 }
890};
891
892} // namespace
893} // namespace wpilib
894} // namespace y2018
895
896AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);