blob: 04264447e9c88fea40d8435cd4aaf7ef63849326 [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"
Brian Silverman37281fc2018-03-11 18:42:17 -070021#include "ctre/phoenix/CANifier.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080022#undef ERROR
23
24#include "aos/common/commonmath.h"
25#include "aos/common/logging/logging.h"
26#include "aos/common/logging/queue_logging.h"
27#include "aos/common/messages/robot_state.q.h"
28#include "aos/common/stl_mutex.h"
29#include "aos/common/time.h"
30#include "aos/common/util/compiler_memory_barrier.h"
31#include "aos/common/util/log_interval.h"
32#include "aos/common/util/phased_loop.h"
33#include "aos/common/util/wrapping_counter.h"
34#include "aos/linux_code/init.h"
35
36#include "frc971/autonomous/auto.q.h"
37#include "frc971/control_loops/control_loops.q.h"
38#include "frc971/control_loops/drivetrain/drivetrain.q.h"
39#include "frc971/wpilib/ADIS16448.h"
40#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/buffered_solenoid.h"
42#include "frc971/wpilib/dma.h"
43#include "frc971/wpilib/dma_edge_counting.h"
44#include "frc971/wpilib/encoder_and_potentiometer.h"
45#include "frc971/wpilib/interrupt_edge_counting.h"
46#include "frc971/wpilib/joystick_sender.h"
47#include "frc971/wpilib/logging.q.h"
48#include "frc971/wpilib/loop_output_handler.h"
49#include "frc971/wpilib/pdp_fetcher.h"
50#include "frc971/wpilib/wpilib_interface.h"
51#include "frc971/wpilib/wpilib_robot_base.h"
52#include "y2018/constants.h"
53#include "y2018/control_loops/superstructure/superstructure.q.h"
Brian Silverman37281fc2018-03-11 18:42:17 -070054#include "y2018/status_light.q.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080055
56#ifndef M_PI
57#define M_PI 3.14159265358979323846
58#endif
59
60using ::frc971::control_loops::drivetrain_queue;
61using ::y2018::control_loops::superstructure_queue;
62using ::y2018::constants::Values;
63using ::aos::monotonic_clock;
64namespace chrono = ::std::chrono;
65
66namespace y2018 {
67namespace wpilib {
68namespace {
69
70constexpr double kMaxBringupPower = 12.0;
71
72// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
73// DMA stuff and then removing the * 2.0 in *_translate.
74// The low bit is direction.
75
76// TODO(brian): Replace this with ::std::make_unique once all our toolchains
77// have support.
78
79template <class T, class... U>
80std::unique_ptr<T> make_unique(U &&... u) {
81 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
82}
83
84// TODO(brian): Use ::std::max instead once we have C++14 so that can be
85// constexpr.
86
87template <typename T>
88constexpr T max(T a, T b) {
89 return (a > b) ? a : b;
90}
91
92template <typename T, typename... Rest>
93constexpr T max(T a, T b, T c, Rest... rest) {
94 return max(max(a, b), c, rest...);
95}
96
97double drivetrain_translate(int32_t in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080098 return ((static_cast<double>(in) /
99 Values::kDrivetrainEncoderCountsPerRevolution()) *
100 (2.0 * M_PI)) *
101 Values::kDrivetrainEncoderRatio() *
102 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800103}
104
105double drivetrain_velocity_translate(double in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -0800106 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
107 (2.0 * M_PI)) *
108 Values::kDrivetrainEncoderRatio() *
109 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800110}
111
112double proximal_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800113 return -voltage * Values::kProximalPotRatio() *
Austin Schuh2a3e0632018-02-19 16:24:49 -0800114 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
115}
116
117double distal_pot_translate(double voltage) {
118 return voltage * Values::kDistalPotRatio() *
119 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
120}
121
122double intake_pot_translate(double voltage) {
123 return voltage * Values::kIntakeMotorPotRatio() *
124 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
125}
126
127double intake_spring_translate(double voltage) {
128 return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) /
129 (5.0 /*volts*/);
130}
131
132// TODO() figure out differnce between max and min voltages on shifter pots.
133// Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be
134// passed drectly into the drivetrain position queue.
135double drivetrain_shifter_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800136 return (voltage - Values::kDrivetrainShifterPotMinVoltage()) /
137 (Values::kDrivetrainShifterPotMaxVoltage() -
138 Values::kDrivetrainShifterPotMinVoltage());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800139}
140
141constexpr double kMaxFastEncoderPulsesPerSecond =
142 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
143 Values::kMaxIntakeMotorEncoderPulsesPerSecond());
144static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
145 "fast encoders are too fast");
146
147constexpr double kMaxMediumEncoderPulsesPerSecond =
148 max(Values::kMaxProximalEncoderPulsesPerSecond(),
149 Values::kMaxDistalEncoderPulsesPerSecond());
150static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
151 "medium encoders are too fast");
152
153// Class to send position messages with sensor readings to our loops.
154class SensorReader {
155 public:
156 SensorReader() {
157 // Set to filter out anything shorter than 1/4 of the minimum pulse width
158 // we should ever see.
159 fast_encoder_filter_.SetPeriodNanoSeconds(
160 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
161 kMaxFastEncoderPulsesPerSecond * 1e9 +
162 0.5));
163 medium_encoder_filter_.SetPeriodNanoSeconds(
164 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
165 kMaxMediumEncoderPulsesPerSecond * 1e9 +
166 0.5));
167 hall_filter_.SetPeriodNanoSeconds(100000);
168 }
169
170 // Left drivetrain side.
171 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
172 fast_encoder_filter_.Add(encoder.get());
173 drivetrain_left_encoder_ = ::std::move(encoder);
174 }
175
176 void set_left_drivetrain_shifter_potentiometer(
177 ::std::unique_ptr<AnalogInput> potentiometer) {
178 left_drivetrain_shifter_ = ::std::move(potentiometer);
179 }
180
181 // Right drivetrain side.
182 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
183 fast_encoder_filter_.Add(encoder.get());
184 drivetrain_right_encoder_ = ::std::move(encoder);
185 }
186
187 void set_right_drivetrain_shifter_potentiometer(
188 ::std::unique_ptr<AnalogInput> potentiometer) {
189 right_drivetrain_shifter_ = ::std::move(potentiometer);
190 }
191
192 // Proximal joint.
193 void set_proximal_encoder(::std::unique_ptr<Encoder> encoder) {
194 medium_encoder_filter_.Add(encoder.get());
195 proximal_encoder_.set_encoder(::std::move(encoder));
196 }
197
198 void set_proximal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) {
199 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
200 }
201
202 void set_proximal_potentiometer(
203 ::std::unique_ptr<AnalogInput> potentiometer) {
204 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
205 }
206
207 // Distal joint.
208 void set_distal_encoder(::std::unique_ptr<Encoder> encoder) {
209 medium_encoder_filter_.Add(encoder.get());
210 distal_encoder_.set_encoder(::std::move(encoder));
211 }
212
213 void set_distal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) {
214 fast_encoder_filter_.Add(absolute_pwm.get());
215 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
216 }
217
218 void set_distal_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
219 distal_encoder_.set_potentiometer(::std::move(potentiometer));
220 }
221
222 // Left intake side.
223 void set_left_intake_encoder(::std::unique_ptr<Encoder> encoder) {
224 fast_encoder_filter_.Add(encoder.get());
225 left_intake_encoder_.set_encoder(::std::move(encoder));
226 }
227
228 void set_left_intake_absolute_pwm(
229 ::std::unique_ptr<DigitalInput> absolute_pwm) {
230 fast_encoder_filter_.Add(absolute_pwm.get());
231 left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
232 }
233
234 void set_left_intake_potentiometer(
235 ::std::unique_ptr<AnalogInput> potentiometer) {
236 left_intake_encoder_.set_potentiometer(::std::move(potentiometer));
237 }
238
239 void set_left_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) {
240 left_intake_spring_angle_ = ::std::move(encoder);
241 }
242
243 void set_left_intake_cube_detector(::std::unique_ptr<DigitalInput> input) {
244 left_intake_cube_detector_ = ::std::move(input);
245 }
246
247 // Right intake side.
248 void set_right_intake_encoder(::std::unique_ptr<Encoder> encoder) {
249 fast_encoder_filter_.Add(encoder.get());
250 right_intake_encoder_.set_encoder(::std::move(encoder));
251 }
252
253 void set_right_intake_absolute_pwm(
254 ::std::unique_ptr<DigitalInput> absolute_pwm) {
255 fast_encoder_filter_.Add(absolute_pwm.get());
256 right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
257 }
258
259 void set_right_intake_potentiometer(
260 ::std::unique_ptr<AnalogInput> potentiometer) {
261 right_intake_encoder_.set_potentiometer(::std::move(potentiometer));
262 }
263
264 void set_right_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) {
265 right_intake_spring_angle_ = ::std::move(encoder);
266 }
267
268 void set_right_intake_cube_detector(::std::unique_ptr<DigitalInput> input) {
269 right_intake_cube_detector_ = ::std::move(input);
270 }
271
Austin Schuh4ef51af2018-03-04 01:08:45 -0800272 void set_claw_beambreak(::std::unique_ptr<DigitalInput> input) {
273 claw_beambreak_ = ::std::move(input);
274 }
275
276 void set_box_back_beambreak(::std::unique_ptr<DigitalInput> input) {
277 box_back_beambreak_ = ::std::move(input);
278 }
279
Austin Schuh2a3e0632018-02-19 16:24:49 -0800280 // Auto mode switches.
281 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
282 autonomous_modes_.at(i) = ::std::move(sensor);
283 }
284
285 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
286 medium_encoder_filter_.Add(pwm_trigger.get());
287 pwm_trigger_ = ::std::move(pwm_trigger);
288 }
289
290 // All of the DMA-related set_* calls must be made before this, and it
291 // doesn't hurt to do all of them.
292 void set_dma(::std::unique_ptr<DMA> dma) {
293 dma_synchronizer_.reset(
294 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
295 }
296
297 void RunPWMDetecter() {
298 ::aos::SetCurrentThreadRealtimePriority(41);
299
300 pwm_trigger_->RequestInterrupts();
301 // Rising edge only.
302 pwm_trigger_->SetUpSourceEdge(true, false);
303
304 monotonic_clock::time_point last_posedge_monotonic =
305 monotonic_clock::min_time;
306
307 while (run_) {
308 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
309 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
310 // Grab all the clocks.
311 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
312
313 aos_compiler_memory_barrier();
314 const double fpga_time_before = GetFPGATime() * 1e-6;
315 aos_compiler_memory_barrier();
316 const monotonic_clock::time_point monotonic_now =
317 monotonic_clock::now();
318 aos_compiler_memory_barrier();
319 const double fpga_time_after = GetFPGATime() * 1e-6;
320 aos_compiler_memory_barrier();
321
322 const double fpga_offset =
323 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
324
325 // Compute when the edge was.
326 const monotonic_clock::time_point monotonic_edge =
327 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
328 chrono::duration<double>(fpga_offset));
329
330 LOG(DEBUG, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
331 fpga_time_after - fpga_time_before, fpga_offset,
332 monotonic_edge.time_since_epoch().count());
333
334 // Compute bounds on the timestep and sampling times.
335 const double fpga_sample_length = fpga_time_after - fpga_time_before;
336 const chrono::nanoseconds elapsed_time =
337 monotonic_edge - last_posedge_monotonic;
338
339 last_posedge_monotonic = monotonic_edge;
340
341 // Verify that the values are sane.
342 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
343 continue;
344 }
345 if (fpga_offset < 0 || fpga_offset > 0.00015) {
346 continue;
347 }
348 if (elapsed_time >
349 chrono::microseconds(5050) + chrono::microseconds(4) ||
350 elapsed_time <
351 chrono::microseconds(5050) - chrono::microseconds(4)) {
352 continue;
353 }
354 // Good edge!
355 {
356 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
357 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
358 last_period_ = elapsed_time;
359 }
360 } else {
361 LOG(INFO, "PWM triggered %d\n", ret);
362 }
363 }
364 pwm_trigger_->CancelInterrupts();
365 }
366
367 void operator()() {
368 ::aos::SetCurrentThreadName("SensorReader");
369
370 my_pid_ = getpid();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800371
372 dma_synchronizer_->Start();
373
374 ::aos::time::PhasedLoop phased_loop(last_period_,
375 ::std::chrono::milliseconds(3));
376 chrono::nanoseconds filtered_period = last_period_;
377
378 ::std::thread pwm_detecter_thread(
379 ::std::bind(&SensorReader::RunPWMDetecter, this));
380
381 ::aos::SetCurrentThreadRealtimePriority(40);
382 while (run_) {
383 {
384 const int iterations = phased_loop.SleepUntilNext();
385 if (iterations != 1) {
386 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
387 }
388 }
389 RunIteration();
390
391 monotonic_clock::time_point last_tick_timepoint;
392 chrono::nanoseconds period;
393 {
394 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
395 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
396 period = last_period_;
397 }
398
399 if (last_tick_timepoint == monotonic_clock::min_time) {
400 continue;
401 }
402 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
403 period, last_tick_timepoint + chrono::microseconds(2050));
404
405 // TODO(austin): If this is the first edge in a while, skip to it (plus
406 // an offset). Otherwise, slowly drift time to line up.
407
408 phased_loop.set_interval_and_offset(period, new_offset);
409 }
410 pwm_detecter_thread.join();
411 }
412
413 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700414 ::frc971::wpilib::SendRobotState(my_pid_);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800415
416 const auto values = constants::GetValues();
417
418 {
419 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
Austin Schuh6829f762018-03-02 21:36:01 -0800420 drivetrain_message->left_encoder =
421 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
422 drivetrain_message->left_speed =
423 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800424 drivetrain_message->left_shifter_position =
425 drivetrain_shifter_pot_translate(
426 left_drivetrain_shifter_->GetVoltage());
427
Austin Schuh6829f762018-03-02 21:36:01 -0800428 drivetrain_message->right_encoder =
429 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
430 drivetrain_message->right_speed =
431 -drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800432 drivetrain_message->right_shifter_position =
433 drivetrain_shifter_pot_translate(
434 right_drivetrain_shifter_->GetVoltage());
435
436 drivetrain_message.Send();
437 }
438
439 dma_synchronizer_->RunIteration();
440
441 {
442 auto superstructure_message = superstructure_queue.position.MakeMessage();
443
444 CopyPosition(proximal_encoder_, &superstructure_message->arm.proximal,
445 Values::kProximalEncoderCountsPerRevolution(),
446 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800447 true, values.arm_proximal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800448
449 CopyPosition(distal_encoder_, &superstructure_message->arm.distal,
450 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh6829f762018-03-02 21:36:01 -0800451 Values::kDistalEncoderRatio(), distal_pot_translate, true,
452 values.arm_distal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800453
454 CopyPosition(left_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800455 &superstructure_message->left_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800456 Values::kIntakeMotorEncoderCountsPerRevolution(),
457 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Sabina Davis8d20ca82018-02-19 13:17:45 -0800458 false, values.left_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800459
460 CopyPosition(right_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800461 &superstructure_message->right_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800462 Values::kIntakeMotorEncoderCountsPerRevolution(),
463 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800464 true, values.right_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800465
Sabina Daviscfb872f2018-02-25 16:28:20 -0800466 superstructure_message->left_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800467 intake_spring_translate(left_intake_spring_angle_->GetVoltage()) +
468 values.left_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800469 superstructure_message->left_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800470 left_intake_cube_detector_->Get();
471
Sabina Daviscfb872f2018-02-25 16:28:20 -0800472 superstructure_message->right_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800473 -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) +
474 values.right_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800475 superstructure_message->right_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800476 right_intake_cube_detector_->Get();
477
Austin Schuh96341532018-03-09 21:17:24 -0800478 superstructure_message->claw_beambreak_triggered = !claw_beambreak_->Get();
Austin Schuh4ef51af2018-03-04 01:08:45 -0800479 superstructure_message->box_back_beambreak_triggered =
480 !box_back_beambreak_->Get();
481
Austin Schuh2a3e0632018-02-19 16:24:49 -0800482 superstructure_message.Send();
483 }
484
485 {
486 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
487 auto_mode_message->mode = 0;
488 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
489 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
490 auto_mode_message->mode |= 1 << i;
491 }
492 }
493 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
494 auto_mode_message.Send();
495 }
496 }
497
498 void Quit() { run_ = false; }
499
500 private:
501 double encoder_translate(int32_t value, double counts_per_revolution,
502 double ratio) {
503 return static_cast<double>(value) / counts_per_revolution * ratio *
504 (2.0 * M_PI);
505 }
506
507 void CopyPosition(
508 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
509 ::frc971::PotAndAbsolutePosition *position,
510 double encoder_counts_per_revolution, double encoder_ratio,
511 ::std::function<double(double)> potentiometer_translate, bool reverse,
512 double pot_offset) {
513 const double multiplier = reverse ? -1.0 : 1.0;
514 position->pot = multiplier * potentiometer_translate(
515 encoder.ReadPotentiometerVoltage()) +
516 pot_offset;
517 position->encoder =
518 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
519 encoder_counts_per_revolution,
520 encoder_ratio);
521
522 position->absolute_encoder =
523 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
524 : encoder.ReadAbsoluteEncoder()) *
525 encoder_ratio * (2.0 * M_PI);
526 }
527
528 int32_t my_pid_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800529
530 // Mutex to manage access to the period and tick time variables.
531 ::aos::stl_mutex tick_time_mutex_;
532 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
533 monotonic_clock::min_time;
534 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
535
536 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
537
538 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
539 hall_filter_;
540
541 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
542 drivetrain_right_encoder_;
543
544 ::std::unique_ptr<AnalogInput> left_drivetrain_shifter_,
545 right_drivetrain_shifter_;
546
547 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
548 distal_encoder_;
549
550 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_,
551 right_intake_encoder_;
552
553 ::std::unique_ptr<AnalogInput> left_intake_spring_angle_,
554 right_intake_spring_angle_;
555 ::std::unique_ptr<DigitalInput> left_intake_cube_detector_,
556 right_intake_cube_detector_;
557
Austin Schuh4ef51af2018-03-04 01:08:45 -0800558 ::std::unique_ptr<DigitalInput> claw_beambreak_;
559 ::std::unique_ptr<DigitalInput> box_back_beambreak_;
560
Austin Schuh2a3e0632018-02-19 16:24:49 -0800561 ::std::unique_ptr<DigitalInput> pwm_trigger_;
562
563 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
564
565 ::std::atomic<bool> run_{true};
566};
567
568class SolenoidWriter {
569 public:
570 SolenoidWriter(::frc971::wpilib::BufferedPcm *pcm)
571 : pcm_(pcm),
572 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
573 superstructure_(".y2018.control_loops.superstructure_queue.output") {}
574
575 // left drive
576 // right drive
577 //
578 // claw
579 // arm brakes
580 // hook release
581 // fork release
582 void set_left_drivetrain_shifter(
583 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
584 left_drivetrain_shifter_ = ::std::move(s);
585 }
586 void set_right_drivetrain_shifter(
587 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
588 right_drivetrain_shifter_ = ::std::move(s);
589 }
590
591 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
592 claw_ = ::std::move(s);
593 }
594
595 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
596 arm_brakes_ = ::std::move(s);
597 }
598
599 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
600 hook_ = ::std::move(s);
601 }
602
603 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
604 forks_ = ::std::move(s);
605 }
606
607 void operator()() {
608 ::aos::SetCurrentThreadName("Solenoids");
609 ::aos::SetCurrentThreadRealtimePriority(27);
610
611 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
612 ::std::chrono::milliseconds(1));
613
614 while (run_) {
615 {
616 const int iterations = phased_loop.SleepUntilNext();
617 if (iterations != 1) {
618 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
619 }
620 }
621
622 {
623 drivetrain_.FetchLatest();
624 if (drivetrain_.get()) {
625 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
626 left_drivetrain_shifter_->Set(!drivetrain_->left_high);
627 right_drivetrain_shifter_->Set(!drivetrain_->right_high);
628 }
629 }
630
631 {
632 superstructure_.FetchLatest();
633 if (superstructure_.get()) {
634 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
635
Austin Schuh96341532018-03-09 21:17:24 -0800636 claw_->Set(!superstructure_->claw_grabbed);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800637 arm_brakes_->Set(superstructure_->release_arm_brake);
638 hook_->Set(superstructure_->hook_release);
639 forks_->Set(superstructure_->forks_release);
640 }
641 }
642
643 {
644 ::frc971::wpilib::PneumaticsToLog to_log;
645
646 pcm_->Flush();
647 to_log.read_solenoids = pcm_->GetAll();
648 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
649 }
Brian Silverman37281fc2018-03-11 18:42:17 -0700650
651 status_light.FetchLatest();
652 if (status_light.get()) {
653 LOG_STRUCT(DEBUG, "writing", *status_light);
654 // Not sure which of these is red vs green. We're not ready to use
655 // either,
656 // so just turn them off.
657 canifier_.SetLEDOutput(1.0, ::ctre::phoenix::CANifier::LEDChannelA);
658 canifier_.SetLEDOutput(1.0, ::ctre::phoenix::CANifier::LEDChannelB);
659 // Red
660 canifier_.SetLEDOutput(1 - status_light->red,
661 ::ctre::phoenix::CANifier::LEDChannelC);
662 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800663 }
664 }
665
666 void Quit() { run_ = false; }
667
668 private:
669 ::frc971::wpilib::BufferedPcm *pcm_;
670
671 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
672 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
673 hook_, forks_;
674
675 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
676 ::aos::Queue<::y2018::control_loops::SuperstructureQueue::Output>
677 superstructure_;
678
Brian Silverman37281fc2018-03-11 18:42:17 -0700679 ::ctre::phoenix::CANifier canifier_{0};
680
Austin Schuh2a3e0632018-02-19 16:24:49 -0800681 ::std::atomic<bool> run_{true};
682};
683
684class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
685 public:
686 void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) {
687 drivetrain_left_victor_ = ::std::move(t);
688 }
689
690 void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) {
691 drivetrain_right_victor_ = ::std::move(t);
692 }
693
694 private:
695 virtual void Read() override {
696 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
697 }
698
699 virtual void Write() override {
700 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
701 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh6829f762018-03-02 21:36:01 -0800702 drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0);
703 drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800704 }
705
706 virtual void Stop() override {
707 LOG(WARNING, "drivetrain output too old\n");
708 drivetrain_left_victor_->SetDisabled();
709 drivetrain_right_victor_->SetDisabled();
710 }
711
712 ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_,
713 drivetrain_right_victor_;
714};
715
716class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
717 public:
718 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
719 proximal_victor_ = ::std::move(t);
720 }
721 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
722 distal_victor_ = ::std::move(t);
723 }
724
725 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
726 left_intake_elastic_victor_ = ::std::move(t);
727 }
728 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
729 right_intake_elastic_victor_ = ::std::move(t);
730 }
731
732 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
733 left_intake_rollers_victor_ = ::std::move(t);
734 }
735
736 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
737 right_intake_rollers_victor_ = ::std::move(t);
738 }
739
740 private:
741 virtual void Read() override {
742 ::y2018::control_loops::superstructure_queue.output.FetchAnother();
743 }
744
745 virtual void Write() override {
746 auto &queue = ::y2018::control_loops::superstructure_queue.output;
747 LOG_STRUCT(DEBUG, "will output", *queue);
748
749 left_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800750 ::aos::Clip(-queue->left_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800751 kMaxBringupPower) /
752 12.0);
753
754 right_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800755 ::aos::Clip(queue->right_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800756 kMaxBringupPower) /
757 12.0);
758
759 left_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800760 ::aos::Clip(-queue->left_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800761 kMaxBringupPower) /
762 12.0);
763
764 right_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800765 ::aos::Clip(queue->right_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800766 kMaxBringupPower) /
767 12.0);
768
Austin Schuh6829f762018-03-02 21:36:01 -0800769 proximal_victor_->SetSpeed(::aos::Clip(-queue->voltage_proximal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800770 -kMaxBringupPower,
771 kMaxBringupPower) /
772 12.0);
773
774 distal_victor_->SetSpeed(::aos::Clip(queue->voltage_distal,
775 -kMaxBringupPower, kMaxBringupPower) /
776 12.0);
777 }
778
779 virtual void Stop() override {
780 LOG(WARNING, "Superstructure output too old.\n");
781
782 left_intake_rollers_victor_->SetDisabled();
783 right_intake_rollers_victor_->SetDisabled();
784 left_intake_elastic_victor_->SetDisabled();
785 right_intake_elastic_victor_->SetDisabled();
786
787 proximal_victor_->SetDisabled();
788 distal_victor_->SetDisabled();
789 }
790
791 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
792 right_intake_rollers_victor_, left_intake_elastic_victor_,
793 right_intake_elastic_victor_, proximal_victor_, distal_victor_;
794};
795
796class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
797 public:
798 ::std::unique_ptr<Encoder> make_encoder(int index) {
799 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
800 Encoder::k4X);
801 }
802
803 void Run() override {
804 ::aos::InitNRT();
805 ::aos::SetCurrentThreadName("StartCompetition");
806
807 ::frc971::wpilib::JoystickSender joystick_sender;
808 ::std::thread joystick_thread(::std::ref(joystick_sender));
809
810 ::frc971::wpilib::PDPFetcher pdp_fetcher;
811 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
812 SensorReader reader;
813
814 // TODO(Sabina): Update port numbers(Sensors and Victors)
815 reader.set_drivetrain_left_encoder(make_encoder(0));
Austin Schuh6829f762018-03-02 21:36:01 -0800816 reader.set_left_drivetrain_shifter_potentiometer(
817 make_unique<AnalogInput>(6));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800818 reader.set_drivetrain_right_encoder(make_encoder(1));
Austin Schuh6829f762018-03-02 21:36:01 -0800819 reader.set_right_drivetrain_shifter_potentiometer(
820 make_unique<AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800821
Austin Schuh6829f762018-03-02 21:36:01 -0800822 reader.set_proximal_encoder(make_encoder(4));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800823 reader.set_proximal_absolute_pwm(make_unique<DigitalInput>(2));
824 reader.set_proximal_potentiometer(make_unique<AnalogInput>(2));
825
Austin Schuh6829f762018-03-02 21:36:01 -0800826 reader.set_distal_encoder(make_encoder(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800827 reader.set_distal_absolute_pwm(make_unique<DigitalInput>(3));
828 reader.set_distal_potentiometer(make_unique<AnalogInput>(3));
829
Austin Schuh6829f762018-03-02 21:36:01 -0800830 reader.set_right_intake_encoder(make_encoder(5));
831 reader.set_right_intake_absolute_pwm(make_unique<DigitalInput>(7));
832 reader.set_right_intake_potentiometer(make_unique<AnalogInput>(1));
833 reader.set_right_intake_spring_angle(make_unique<AnalogInput>(5));
834 reader.set_right_intake_cube_detector(make_unique<DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800835
Austin Schuh6829f762018-03-02 21:36:01 -0800836 reader.set_left_intake_encoder(make_encoder(3));
837 reader.set_left_intake_absolute_pwm(make_unique<DigitalInput>(4));
838 reader.set_left_intake_potentiometer(make_unique<AnalogInput>(0));
839 reader.set_left_intake_spring_angle(make_unique<AnalogInput>(4));
840 reader.set_left_intake_cube_detector(make_unique<DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800841
Austin Schuh4ef51af2018-03-04 01:08:45 -0800842 reader.set_claw_beambreak(make_unique<DigitalInput>(8));
843 reader.set_box_back_beambreak(make_unique<DigitalInput>(9));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800844
Austin Schuh6829f762018-03-02 21:36:01 -0800845 reader.set_pwm_trigger(make_unique<DigitalInput>(25));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800846
847 reader.set_dma(make_unique<DMA>());
848 ::std::thread reader_thread(::std::ref(reader));
849
Austin Schuh6829f762018-03-02 21:36:01 -0800850 auto imu_trigger = make_unique<DigitalInput>(5);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800851 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
852 imu.SetDummySPI(SPI::Port::kOnboardCS2);
853 auto imu_reset = make_unique<DigitalOutput>(6);
854 imu.set_reset(imu_reset.get());
855 ::std::thread imu_thread(::std::ref(imu));
856
Austin Schuhe8a54c02018-03-05 00:25:58 -0800857 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
858 // they are identical, as far as DrivetrainWriter is concerned, to the SP
859 // variety so all the Victors are written as SPs.
Austin Schuh2a3e0632018-02-19 16:24:49 -0800860
861 DrivetrainWriter drivetrain_writer;
862 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800863 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800864 drivetrain_writer.set_drivetrain_right_victor(
865 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
866 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
867
868 SuperstructureWriter superstructure_writer;
869 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800870 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800871 superstructure_writer.set_left_intake_rollers_victor(
872 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800873 superstructure_writer.set_right_intake_elastic_victor(
874 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
875 superstructure_writer.set_right_intake_rollers_victor(
876 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800877 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800878 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800879 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800880 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
881
882 // Hanger: victor 8
Austin Schuh2a3e0632018-02-19 16:24:49 -0800883
884 ::std::thread superstructure_writer_thread(
885 ::std::ref(superstructure_writer));
886
887 ::frc971::wpilib::BufferedPcm *pcm = new ::frc971::wpilib::BufferedPcm();
888 SolenoidWriter solenoid_writer(pcm);
889 solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(0));
890 solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1));
891 solenoid_writer.set_claw(pcm->MakeSolenoid(2));
892 solenoid_writer.set_arm_brakes(pcm->MakeSolenoid(3));
893 solenoid_writer.set_hook(pcm->MakeSolenoid(4));
894 solenoid_writer.set_forks(pcm->MakeSolenoid(5));
895
896 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
897
898 // Wait forever. Not much else to do...
899 while (true) {
900 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
901 if (r != 0) {
902 PLOG(WARNING, "infinite select failed");
903 } else {
904 PLOG(WARNING, "infinite select succeeded??\n");
905 }
906 }
907
908 LOG(ERROR, "Exiting WPILibRobot\n");
909
910 joystick_sender.Quit();
911 joystick_thread.join();
912 pdp_fetcher.Quit();
913 pdp_fetcher_thread.join();
914 reader.Quit();
915 reader_thread.join();
916 imu.Quit();
917 imu_thread.join();
918
Austin Schuh2a3e0632018-02-19 16:24:49 -0800919 drivetrain_writer.Quit();
920 drivetrain_writer_thread.join();
921 superstructure_writer.Quit();
922 superstructure_writer_thread.join();
923
924 ::aos::Cleanup();
925 }
926};
927
928} // namespace
929} // namespace wpilib
930} // namespace y2018
931
932AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);