blob: 4a7fe1dce77ef50a1521353337e33f2ff7290280 [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
Austin Schuh8e5950d2018-03-21 20:29:40 -0700290 void set_lidar_lite_input(::std::unique_ptr<DigitalInput> lidar_lite_input) {
291 lidar_lite_input_ = ::std::move(lidar_lite_input);
292 lidar_lite_.set_input(lidar_lite_input_.get());
293 }
294
Austin Schuh2a3e0632018-02-19 16:24:49 -0800295 // All of the DMA-related set_* calls must be made before this, and it
296 // doesn't hurt to do all of them.
297 void set_dma(::std::unique_ptr<DMA> dma) {
298 dma_synchronizer_.reset(
299 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Austin Schuh8e5950d2018-03-21 20:29:40 -0700300 dma_synchronizer_->Add(&lidar_lite_);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800301 }
302
303 void RunPWMDetecter() {
304 ::aos::SetCurrentThreadRealtimePriority(41);
305
306 pwm_trigger_->RequestInterrupts();
307 // Rising edge only.
308 pwm_trigger_->SetUpSourceEdge(true, false);
309
310 monotonic_clock::time_point last_posedge_monotonic =
311 monotonic_clock::min_time;
312
313 while (run_) {
314 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
315 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
316 // Grab all the clocks.
317 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
318
319 aos_compiler_memory_barrier();
320 const double fpga_time_before = GetFPGATime() * 1e-6;
321 aos_compiler_memory_barrier();
322 const monotonic_clock::time_point monotonic_now =
323 monotonic_clock::now();
324 aos_compiler_memory_barrier();
325 const double fpga_time_after = GetFPGATime() * 1e-6;
326 aos_compiler_memory_barrier();
327
328 const double fpga_offset =
329 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
330
331 // Compute when the edge was.
332 const monotonic_clock::time_point monotonic_edge =
333 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
334 chrono::duration<double>(fpga_offset));
335
336 LOG(DEBUG, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
337 fpga_time_after - fpga_time_before, fpga_offset,
338 monotonic_edge.time_since_epoch().count());
339
340 // Compute bounds on the timestep and sampling times.
341 const double fpga_sample_length = fpga_time_after - fpga_time_before;
342 const chrono::nanoseconds elapsed_time =
343 monotonic_edge - last_posedge_monotonic;
344
345 last_posedge_monotonic = monotonic_edge;
346
347 // Verify that the values are sane.
348 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
349 continue;
350 }
351 if (fpga_offset < 0 || fpga_offset > 0.00015) {
352 continue;
353 }
354 if (elapsed_time >
355 chrono::microseconds(5050) + chrono::microseconds(4) ||
356 elapsed_time <
357 chrono::microseconds(5050) - chrono::microseconds(4)) {
358 continue;
359 }
360 // Good edge!
361 {
362 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
363 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
364 last_period_ = elapsed_time;
365 }
366 } else {
367 LOG(INFO, "PWM triggered %d\n", ret);
368 }
369 }
370 pwm_trigger_->CancelInterrupts();
371 }
372
373 void operator()() {
374 ::aos::SetCurrentThreadName("SensorReader");
375
376 my_pid_ = getpid();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800377
378 dma_synchronizer_->Start();
379
380 ::aos::time::PhasedLoop phased_loop(last_period_,
381 ::std::chrono::milliseconds(3));
382 chrono::nanoseconds filtered_period = last_period_;
383
384 ::std::thread pwm_detecter_thread(
385 ::std::bind(&SensorReader::RunPWMDetecter, this));
386
387 ::aos::SetCurrentThreadRealtimePriority(40);
388 while (run_) {
389 {
390 const int iterations = phased_loop.SleepUntilNext();
391 if (iterations != 1) {
392 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
393 }
394 }
395 RunIteration();
396
397 monotonic_clock::time_point last_tick_timepoint;
398 chrono::nanoseconds period;
399 {
400 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
401 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
402 period = last_period_;
403 }
404
405 if (last_tick_timepoint == monotonic_clock::min_time) {
406 continue;
407 }
408 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
409 period, last_tick_timepoint + chrono::microseconds(2050));
410
411 // TODO(austin): If this is the first edge in a while, skip to it (plus
412 // an offset). Otherwise, slowly drift time to line up.
413
414 phased_loop.set_interval_and_offset(period, new_offset);
415 }
416 pwm_detecter_thread.join();
417 }
418
419 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700420 ::frc971::wpilib::SendRobotState(my_pid_);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800421
422 const auto values = constants::GetValues();
423
424 {
425 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
Austin Schuh6829f762018-03-02 21:36:01 -0800426 drivetrain_message->left_encoder =
427 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
428 drivetrain_message->left_speed =
429 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800430 drivetrain_message->left_shifter_position =
431 drivetrain_shifter_pot_translate(
432 left_drivetrain_shifter_->GetVoltage());
433
Austin Schuh6829f762018-03-02 21:36:01 -0800434 drivetrain_message->right_encoder =
435 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
436 drivetrain_message->right_speed =
437 -drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800438 drivetrain_message->right_shifter_position =
439 drivetrain_shifter_pot_translate(
440 right_drivetrain_shifter_->GetVoltage());
441
442 drivetrain_message.Send();
443 }
444
445 dma_synchronizer_->RunIteration();
446
447 {
448 auto superstructure_message = superstructure_queue.position.MakeMessage();
449
450 CopyPosition(proximal_encoder_, &superstructure_message->arm.proximal,
451 Values::kProximalEncoderCountsPerRevolution(),
452 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800453 true, values.arm_proximal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800454
455 CopyPosition(distal_encoder_, &superstructure_message->arm.distal,
456 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh6829f762018-03-02 21:36:01 -0800457 Values::kDistalEncoderRatio(), distal_pot_translate, true,
458 values.arm_distal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800459
460 CopyPosition(left_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800461 &superstructure_message->left_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800462 Values::kIntakeMotorEncoderCountsPerRevolution(),
463 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Sabina Davis8d20ca82018-02-19 13:17:45 -0800464 false, values.left_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800465
466 CopyPosition(right_intake_encoder_,
Sabina Daviscfb872f2018-02-25 16:28:20 -0800467 &superstructure_message->right_intake.motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800468 Values::kIntakeMotorEncoderCountsPerRevolution(),
469 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800470 true, values.right_intake.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800471
Sabina Daviscfb872f2018-02-25 16:28:20 -0800472 superstructure_message->left_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800473 intake_spring_translate(left_intake_spring_angle_->GetVoltage()) +
474 values.left_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800475 superstructure_message->left_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800476 left_intake_cube_detector_->Get();
477
Sabina Daviscfb872f2018-02-25 16:28:20 -0800478 superstructure_message->right_intake.spring_angle =
Austin Schuh6829f762018-03-02 21:36:01 -0800479 -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) +
480 values.right_intake.spring_offset;
Sabina Daviscfb872f2018-02-25 16:28:20 -0800481 superstructure_message->right_intake.beam_break =
Austin Schuh2a3e0632018-02-19 16:24:49 -0800482 right_intake_cube_detector_->Get();
483
Austin Schuh96341532018-03-09 21:17:24 -0800484 superstructure_message->claw_beambreak_triggered = !claw_beambreak_->Get();
Austin Schuh4ef51af2018-03-04 01:08:45 -0800485 superstructure_message->box_back_beambreak_triggered =
486 !box_back_beambreak_->Get();
487
Austin Schuh8e5950d2018-03-21 20:29:40 -0700488 superstructure_message->box_distance =
489 lidar_lite_.last_width() / 0.00001 / 100.0 / 2;
490
Austin Schuh2a3e0632018-02-19 16:24:49 -0800491 superstructure_message.Send();
492 }
493
494 {
495 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
496 auto_mode_message->mode = 0;
497 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
498 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
499 auto_mode_message->mode |= 1 << i;
500 }
501 }
502 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
503 auto_mode_message.Send();
504 }
505 }
506
507 void Quit() { run_ = false; }
508
509 private:
510 double encoder_translate(int32_t value, double counts_per_revolution,
511 double ratio) {
512 return static_cast<double>(value) / counts_per_revolution * ratio *
513 (2.0 * M_PI);
514 }
515
516 void CopyPosition(
517 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
518 ::frc971::PotAndAbsolutePosition *position,
519 double encoder_counts_per_revolution, double encoder_ratio,
520 ::std::function<double(double)> potentiometer_translate, bool reverse,
521 double pot_offset) {
522 const double multiplier = reverse ? -1.0 : 1.0;
523 position->pot = multiplier * potentiometer_translate(
524 encoder.ReadPotentiometerVoltage()) +
525 pot_offset;
526 position->encoder =
527 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
528 encoder_counts_per_revolution,
529 encoder_ratio);
530
531 position->absolute_encoder =
532 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
533 : encoder.ReadAbsoluteEncoder()) *
534 encoder_ratio * (2.0 * M_PI);
535 }
536
537 int32_t my_pid_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800538
539 // Mutex to manage access to the period and tick time variables.
540 ::aos::stl_mutex tick_time_mutex_;
541 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
542 monotonic_clock::min_time;
543 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
544
545 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
546
547 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
548 hall_filter_;
549
550 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
551 drivetrain_right_encoder_;
552
553 ::std::unique_ptr<AnalogInput> left_drivetrain_shifter_,
554 right_drivetrain_shifter_;
555
556 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
557 distal_encoder_;
558
559 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_,
560 right_intake_encoder_;
561
562 ::std::unique_ptr<AnalogInput> left_intake_spring_angle_,
563 right_intake_spring_angle_;
564 ::std::unique_ptr<DigitalInput> left_intake_cube_detector_,
565 right_intake_cube_detector_;
566
Austin Schuh4ef51af2018-03-04 01:08:45 -0800567 ::std::unique_ptr<DigitalInput> claw_beambreak_;
568 ::std::unique_ptr<DigitalInput> box_back_beambreak_;
569
Austin Schuh2a3e0632018-02-19 16:24:49 -0800570 ::std::unique_ptr<DigitalInput> pwm_trigger_;
571
572 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
573
Austin Schuh8e5950d2018-03-21 20:29:40 -0700574 ::std::unique_ptr<DigitalInput> lidar_lite_input_;
575 ::frc971::wpilib::DMAPulseWidthReader lidar_lite_;
576
Austin Schuh2a3e0632018-02-19 16:24:49 -0800577 ::std::atomic<bool> run_{true};
578};
579
580class SolenoidWriter {
581 public:
582 SolenoidWriter(::frc971::wpilib::BufferedPcm *pcm)
583 : pcm_(pcm),
584 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
585 superstructure_(".y2018.control_loops.superstructure_queue.output") {}
586
587 // left drive
588 // right drive
589 //
590 // claw
591 // arm brakes
592 // hook release
593 // fork release
594 void set_left_drivetrain_shifter(
595 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
596 left_drivetrain_shifter_ = ::std::move(s);
597 }
598 void set_right_drivetrain_shifter(
599 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
600 right_drivetrain_shifter_ = ::std::move(s);
601 }
602
603 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
604 claw_ = ::std::move(s);
605 }
606
607 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
608 arm_brakes_ = ::std::move(s);
609 }
610
611 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
612 hook_ = ::std::move(s);
613 }
614
615 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
616 forks_ = ::std::move(s);
617 }
618
619 void operator()() {
620 ::aos::SetCurrentThreadName("Solenoids");
621 ::aos::SetCurrentThreadRealtimePriority(27);
622
623 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
624 ::std::chrono::milliseconds(1));
625
626 while (run_) {
627 {
628 const int iterations = phased_loop.SleepUntilNext();
629 if (iterations != 1) {
630 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
631 }
632 }
633
634 {
635 drivetrain_.FetchLatest();
636 if (drivetrain_.get()) {
637 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
638 left_drivetrain_shifter_->Set(!drivetrain_->left_high);
639 right_drivetrain_shifter_->Set(!drivetrain_->right_high);
640 }
641 }
642
643 {
644 superstructure_.FetchLatest();
645 if (superstructure_.get()) {
646 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
647
Austin Schuh96341532018-03-09 21:17:24 -0800648 claw_->Set(!superstructure_->claw_grabbed);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800649 arm_brakes_->Set(superstructure_->release_arm_brake);
650 hook_->Set(superstructure_->hook_release);
651 forks_->Set(superstructure_->forks_release);
652 }
653 }
654
655 {
656 ::frc971::wpilib::PneumaticsToLog to_log;
657
658 pcm_->Flush();
659 to_log.read_solenoids = pcm_->GetAll();
660 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
661 }
Brian Silverman37281fc2018-03-11 18:42:17 -0700662
Austin Schuh1e2d4982018-03-21 20:34:33 -0700663 static double last_red = -1.0;
664 static double last_green = -1.0;
665 static double last_blue = -1.0;
Brian Silverman37281fc2018-03-11 18:42:17 -0700666 status_light.FetchLatest();
667 if (status_light.get()) {
668 LOG_STRUCT(DEBUG, "writing", *status_light);
669 // Not sure which of these is red vs green. We're not ready to use
Austin Schuh1e2d4982018-03-21 20:34:33 -0700670 // either, so just turn them off.
671 if (status_light->green != last_green) {
672 canifier_.SetLEDOutput(1.0 - status_light->green,
673 ::ctre::phoenix::CANifier::LEDChannelA);
674 last_green = status_light->green;
675 }
676
677 if (status_light->blue != last_blue) {
678 canifier_.SetLEDOutput(1.0 - status_light->blue,
679 ::ctre::phoenix::CANifier::LEDChannelB);
680 last_blue = status_light->blue;
681 }
682
683 if (status_light->red != last_red) {
684 canifier_.SetLEDOutput(1.0 - status_light->red,
685 ::ctre::phoenix::CANifier::LEDChannelC);
686 last_red = status_light->red;
687 }
Brian Silverman37281fc2018-03-11 18:42:17 -0700688 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800689 }
690 }
691
692 void Quit() { run_ = false; }
693
694 private:
695 ::frc971::wpilib::BufferedPcm *pcm_;
696
697 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
698 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
699 hook_, forks_;
700
701 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
702 ::aos::Queue<::y2018::control_loops::SuperstructureQueue::Output>
703 superstructure_;
704
Brian Silverman37281fc2018-03-11 18:42:17 -0700705 ::ctre::phoenix::CANifier canifier_{0};
706
Austin Schuh2a3e0632018-02-19 16:24:49 -0800707 ::std::atomic<bool> run_{true};
708};
709
710class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
711 public:
712 void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) {
713 drivetrain_left_victor_ = ::std::move(t);
714 }
715
716 void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) {
717 drivetrain_right_victor_ = ::std::move(t);
718 }
719
720 private:
721 virtual void Read() override {
722 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
723 }
724
725 virtual void Write() override {
726 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
727 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh6829f762018-03-02 21:36:01 -0800728 drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0);
729 drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800730 }
731
732 virtual void Stop() override {
733 LOG(WARNING, "drivetrain output too old\n");
734 drivetrain_left_victor_->SetDisabled();
735 drivetrain_right_victor_->SetDisabled();
736 }
737
738 ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_,
739 drivetrain_right_victor_;
740};
741
742class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
743 public:
744 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
745 proximal_victor_ = ::std::move(t);
746 }
747 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
748 distal_victor_ = ::std::move(t);
749 }
750
Austin Schuh17e484e2018-03-11 01:11:36 -0800751 void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) {
752 hanger_victor_ = ::std::move(t);
753 }
754
Austin Schuh2a3e0632018-02-19 16:24:49 -0800755 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
756 left_intake_elastic_victor_ = ::std::move(t);
757 }
758 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
759 right_intake_elastic_victor_ = ::std::move(t);
760 }
761
762 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
763 left_intake_rollers_victor_ = ::std::move(t);
764 }
765
766 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
767 right_intake_rollers_victor_ = ::std::move(t);
768 }
769
770 private:
771 virtual void Read() override {
772 ::y2018::control_loops::superstructure_queue.output.FetchAnother();
773 }
774
775 virtual void Write() override {
776 auto &queue = ::y2018::control_loops::superstructure_queue.output;
777 LOG_STRUCT(DEBUG, "will output", *queue);
778
779 left_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800780 ::aos::Clip(-queue->left_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800781 kMaxBringupPower) /
782 12.0);
783
784 right_intake_elastic_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800785 ::aos::Clip(queue->right_intake.voltage_elastic, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800786 kMaxBringupPower) /
787 12.0);
788
789 left_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800790 ::aos::Clip(-queue->left_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800791 kMaxBringupPower) /
792 12.0);
793
794 right_intake_rollers_victor_->SetSpeed(
Sabina Daviscfb872f2018-02-25 16:28:20 -0800795 ::aos::Clip(queue->right_intake.voltage_rollers, -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800796 kMaxBringupPower) /
797 12.0);
798
Austin Schuh6829f762018-03-02 21:36:01 -0800799 proximal_victor_->SetSpeed(::aos::Clip(-queue->voltage_proximal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800800 -kMaxBringupPower,
801 kMaxBringupPower) /
802 12.0);
803
804 distal_victor_->SetSpeed(::aos::Clip(queue->voltage_distal,
805 -kMaxBringupPower, kMaxBringupPower) /
806 12.0);
Austin Schuh17e484e2018-03-11 01:11:36 -0800807 hanger_victor_->SetSpeed(
808 ::aos::Clip(-queue->voltage_winch, -kMaxBringupPower, kMaxBringupPower) /
809 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800810 }
811
812 virtual void Stop() override {
813 LOG(WARNING, "Superstructure output too old.\n");
814
815 left_intake_rollers_victor_->SetDisabled();
816 right_intake_rollers_victor_->SetDisabled();
817 left_intake_elastic_victor_->SetDisabled();
818 right_intake_elastic_victor_->SetDisabled();
819
820 proximal_victor_->SetDisabled();
821 distal_victor_->SetDisabled();
Austin Schuh17e484e2018-03-11 01:11:36 -0800822 hanger_victor_->SetDisabled();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800823 }
824
825 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
826 right_intake_rollers_victor_, left_intake_elastic_victor_,
Austin Schuh17e484e2018-03-11 01:11:36 -0800827 right_intake_elastic_victor_, proximal_victor_, distal_victor_,
828 hanger_victor_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800829};
830
831class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
832 public:
833 ::std::unique_ptr<Encoder> make_encoder(int index) {
834 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
835 Encoder::k4X);
836 }
837
838 void Run() override {
839 ::aos::InitNRT();
840 ::aos::SetCurrentThreadName("StartCompetition");
841
842 ::frc971::wpilib::JoystickSender joystick_sender;
843 ::std::thread joystick_thread(::std::ref(joystick_sender));
844
845 ::frc971::wpilib::PDPFetcher pdp_fetcher;
846 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
847 SensorReader reader;
848
849 // TODO(Sabina): Update port numbers(Sensors and Victors)
850 reader.set_drivetrain_left_encoder(make_encoder(0));
Austin Schuh6829f762018-03-02 21:36:01 -0800851 reader.set_left_drivetrain_shifter_potentiometer(
852 make_unique<AnalogInput>(6));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800853 reader.set_drivetrain_right_encoder(make_encoder(1));
Austin Schuh6829f762018-03-02 21:36:01 -0800854 reader.set_right_drivetrain_shifter_potentiometer(
855 make_unique<AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800856
Austin Schuh6829f762018-03-02 21:36:01 -0800857 reader.set_proximal_encoder(make_encoder(4));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800858 reader.set_proximal_absolute_pwm(make_unique<DigitalInput>(2));
859 reader.set_proximal_potentiometer(make_unique<AnalogInput>(2));
860
Austin Schuh6829f762018-03-02 21:36:01 -0800861 reader.set_distal_encoder(make_encoder(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800862 reader.set_distal_absolute_pwm(make_unique<DigitalInput>(3));
863 reader.set_distal_potentiometer(make_unique<AnalogInput>(3));
864
Austin Schuh6829f762018-03-02 21:36:01 -0800865 reader.set_right_intake_encoder(make_encoder(5));
866 reader.set_right_intake_absolute_pwm(make_unique<DigitalInput>(7));
867 reader.set_right_intake_potentiometer(make_unique<AnalogInput>(1));
868 reader.set_right_intake_spring_angle(make_unique<AnalogInput>(5));
869 reader.set_right_intake_cube_detector(make_unique<DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800870
Austin Schuh6829f762018-03-02 21:36:01 -0800871 reader.set_left_intake_encoder(make_encoder(3));
872 reader.set_left_intake_absolute_pwm(make_unique<DigitalInput>(4));
873 reader.set_left_intake_potentiometer(make_unique<AnalogInput>(0));
874 reader.set_left_intake_spring_angle(make_unique<AnalogInput>(4));
875 reader.set_left_intake_cube_detector(make_unique<DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800876
Austin Schuh4ef51af2018-03-04 01:08:45 -0800877 reader.set_claw_beambreak(make_unique<DigitalInput>(8));
878 reader.set_box_back_beambreak(make_unique<DigitalInput>(9));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800879
Austin Schuh6829f762018-03-02 21:36:01 -0800880 reader.set_pwm_trigger(make_unique<DigitalInput>(25));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800881
Austin Schuh8e5950d2018-03-21 20:29:40 -0700882 reader.set_lidar_lite_input(make_unique<DigitalInput>(22));
883
Austin Schuh2a3e0632018-02-19 16:24:49 -0800884 reader.set_dma(make_unique<DMA>());
885 ::std::thread reader_thread(::std::ref(reader));
886
Austin Schuh6829f762018-03-02 21:36:01 -0800887 auto imu_trigger = make_unique<DigitalInput>(5);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800888 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
889 imu.SetDummySPI(SPI::Port::kOnboardCS2);
890 auto imu_reset = make_unique<DigitalOutput>(6);
891 imu.set_reset(imu_reset.get());
892 ::std::thread imu_thread(::std::ref(imu));
893
Austin Schuhe8a54c02018-03-05 00:25:58 -0800894 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
895 // they are identical, as far as DrivetrainWriter is concerned, to the SP
896 // variety so all the Victors are written as SPs.
Austin Schuh2a3e0632018-02-19 16:24:49 -0800897
898 DrivetrainWriter drivetrain_writer;
899 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800900 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800901 drivetrain_writer.set_drivetrain_right_victor(
902 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
903 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
904
905 SuperstructureWriter superstructure_writer;
906 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800907 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800908 superstructure_writer.set_left_intake_rollers_victor(
909 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800910 superstructure_writer.set_right_intake_elastic_victor(
911 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
912 superstructure_writer.set_right_intake_rollers_victor(
913 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800914 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800915 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800916 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800917 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
918
Austin Schuh17e484e2018-03-11 01:11:36 -0800919 superstructure_writer.set_hanger_victor(
920 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800921
922 ::std::thread superstructure_writer_thread(
923 ::std::ref(superstructure_writer));
924
925 ::frc971::wpilib::BufferedPcm *pcm = new ::frc971::wpilib::BufferedPcm();
926 SolenoidWriter solenoid_writer(pcm);
927 solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(0));
928 solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1));
929 solenoid_writer.set_claw(pcm->MakeSolenoid(2));
930 solenoid_writer.set_arm_brakes(pcm->MakeSolenoid(3));
931 solenoid_writer.set_hook(pcm->MakeSolenoid(4));
932 solenoid_writer.set_forks(pcm->MakeSolenoid(5));
933
934 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
935
936 // Wait forever. Not much else to do...
937 while (true) {
938 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
939 if (r != 0) {
940 PLOG(WARNING, "infinite select failed");
941 } else {
942 PLOG(WARNING, "infinite select succeeded??\n");
943 }
944 }
945
946 LOG(ERROR, "Exiting WPILibRobot\n");
947
948 joystick_sender.Quit();
949 joystick_thread.join();
950 pdp_fetcher.Quit();
951 pdp_fetcher_thread.join();
952 reader.Quit();
953 reader_thread.join();
954 imu.Quit();
955 imu_thread.join();
956
Austin Schuh2a3e0632018-02-19 16:24:49 -0800957 drivetrain_writer.Quit();
958 drivetrain_writer_thread.join();
959 superstructure_writer.Quit();
960 superstructure_writer_thread.join();
961
962 ::aos::Cleanup();
963 }
964};
965
966} // namespace
967} // namespace wpilib
968} // namespace y2018
969
970AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);