blob: 659f11cbf7111335a0b71b3a99200eefa7393821 [file] [log] [blame]
Austin Schuh8347cb62017-04-08 14:37:34 -07001#include <inttypes.h>
Campbell Crowleyae6e8422017-02-05 12:38:50 -08002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Campbell Crowleyae6e8422017-02-05 12:38:50 -08005
Campbell Crowleyae6e8422017-02-05 12:38:50 -08006#include <array>
Austin Schuh8347cb62017-04-08 14:37:34 -07007#include <chrono>
Brian Silverman50826c02017-02-18 14:40:25 -08008#include <cmath>
Austin Schuh8347cb62017-04-08 14:37:34 -07009#include <functional>
10#include <mutex>
11#include <thread>
Campbell Crowleyae6e8422017-02-05 12:38:50 -080012
Campbell Crowleyae6e8422017-02-05 12:38:50 -080013#include "AnalogInput.h"
14#include "Compressor.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070015#include "Counter.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080016#include "DigitalGlitchFilter.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070017#include "DriverStation.h"
18#include "Encoder.h"
19#include "Relay.h"
20#include "Servo.h"
21#include "VictorSP.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080022#undef ERROR
23
Austin Schuh8347cb62017-04-08 14:37:34 -070024#include "aos/common/commonmath.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080025#include "aos/common/logging/logging.h"
26#include "aos/common/logging/queue_logging.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070027#include "aos/common/messages/robot_state.q.h"
28#include "aos/common/stl_mutex.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080029#include "aos/common/time.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070030#include "aos/common/util/compiler_memory_barrier.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080031#include "aos/common/util/log_interval.h"
32#include "aos/common/util/phased_loop.h"
33#include "aos/common/util/wrapping_counter.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080034#include "aos/linux_code/init.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080035
Philipp Schrader996a2a22017-02-22 05:02:48 +000036#include "frc971/autonomous/auto.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080037#include "frc971/control_loops/control_loops.q.h"
38#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070039#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"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080052#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080053#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080054
Campbell Crowleyae6e8422017-02-05 12:38:50 -080055#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
59using ::frc971::control_loops::drivetrain_queue;
60using ::y2017::control_loops::superstructure_queue;
Brian Silverman052e69d2017-02-12 16:19:55 -080061using ::y2017::constants::Values;
Austin Schuh8347cb62017-04-08 14:37:34 -070062using ::aos::monotonic_clock;
63namespace chrono = ::std::chrono;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080064
65namespace y2017 {
66namespace wpilib {
67namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080068
Campbell Crowleyae6e8422017-02-05 12:38:50 -080069constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080070
71// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
72// DMA stuff and then removing the * 2.0 in *_translate.
73// The low bit is direction.
74
75// TODO(brian): Replace this with ::std::make_unique once all our toolchains
76// have support.
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
Brian Silverman052e69d2017-02-12 16:19:55 -080082// TODO(brian): Use ::std::max instead once we have C++14 so that can be
83// constexpr.
84template <typename T>
85constexpr T max(T a, T b) {
86 return (a > b) ? a : b;
87}
88template <typename T, typename... Rest>
89constexpr T max(T a, T b, T c, Rest... rest) {
90 return max(max(a, b), c, rest...);
91}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080092
Campbell Crowleyae6e8422017-02-05 12:38:50 -080093double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080094 return static_cast<double>(in) /
95 Values::kDrivetrainEncoderCountsPerRevolution *
96 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080097}
98
99double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800100 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
101 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800102}
103
Brian Silverman50826c02017-02-18 14:40:25 -0800104// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800105double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800106 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800107 (2 * M_PI /*radians*/);
108}
109
Brian Silverman052e69d2017-02-12 16:19:55 -0800110constexpr double kMaxFastEncoderPulsesPerSecond =
111 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
112 Values::kMaxShooterEncoderPulsesPerSecond);
113static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
114 "fast encoders are too fast");
115constexpr double kMaxMediumEncoderPulsesPerSecond =
116 max(Values::kMaxIntakeEncoderPulsesPerSecond,
117 Values::kMaxTurretEncoderPulsesPerSecond,
118 Values::kMaxIndexerEncoderPulsesPerSecond);
119static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
120 "medium encoders are too fast");
121constexpr double kMaxSlowEncoderPulsesPerSecond =
122 Values::kMaxHoodEncoderPulsesPerSecond;
123static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
124 "slow encoders are too fast");
Brianef030df2017-03-05 15:06:04 -0800125static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond,
126 "slow encoders are faster than medium?");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800127
Brian Silverman50826c02017-02-18 14:40:25 -0800128// Handles reading the duty cycle on a DigitalInput.
129class DutyCycleReader {
130 public:
131 void set_input(::std::unique_ptr<DigitalInput> input) {
132 high_counter_.reset(new Counter(input.get()));
133 high_counter_->SetMaxPeriod(kMaxPeriod);
134 high_counter_->SetSemiPeriodMode(true);
135
136 period_length_counter_.reset(new Counter(input.get()));
137 period_length_counter_->SetMaxPeriod(kMaxPeriod);
138 period_length_counter_->SetUpSourceEdge(true, false);
139
140 input_ = ::std::move(input);
141 }
142
143 double Read() const {
144 const double high_time = high_counter_->GetPeriod();
145 const double period_length = period_length_counter_->GetPeriod();
146 if (!::std::isfinite(high_time) || !::std::isfinite(period_length)) {
147 return ::std::numeric_limits<double>::quiet_NaN();
148 }
149 return high_time / period_length;
150 }
151
152 private:
153 static constexpr ::std::chrono::nanoseconds kNominalPeriod =
154 ::std::chrono::microseconds(4096);
155 static constexpr double kMaxPeriod =
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800156 (::std::chrono::duration_cast<::std::chrono::duration<double>>(
157 kNominalPeriod) *
158 2).count();
Brian Silverman50826c02017-02-18 14:40:25 -0800159
160 ::std::unique_ptr<Counter> high_counter_, period_length_counter_;
161 ::std::unique_ptr<DigitalInput> input_;
162};
163
164class AbsoluteEncoderAndPotentiometer {
165 public:
166 void set_absolute_pwm(::std::unique_ptr<DigitalInput> input) {
167 duty_cycle_.set_input(::std::move(input));
168 }
169
170 void set_encoder(::std::unique_ptr<Encoder> encoder) {
171 encoder_ = ::std::move(encoder);
172 }
173
174 void set_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
175 potentiometer_ = ::std::move(potentiometer);
176 }
177
178 double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); }
179
180 int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); }
181
182 double ReadPotentiometerVoltage() const {
183 return potentiometer_->GetVoltage();
184 }
185
186 private:
187 DutyCycleReader duty_cycle_;
188 ::std::unique_ptr<Encoder> encoder_;
189 ::std::unique_ptr<AnalogInput> potentiometer_;
190};
191
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800192// Class to send position messages with sensor readings to our loops.
193class SensorReader {
194 public:
195 SensorReader() {
Brian Silverman052e69d2017-02-12 16:19:55 -0800196 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800197 // we should ever see.
Brian Silverman052e69d2017-02-12 16:19:55 -0800198 fast_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800199 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800200 kMaxFastEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800201 0.5));
Brian Silverman052e69d2017-02-12 16:19:55 -0800202 medium_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800203 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800204 kMaxMediumEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800205 0.5));
Brianef030df2017-03-05 15:06:04 -0800206 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800207 }
208
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800209 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800210 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800211 drivetrain_left_encoder_ = ::std::move(encoder);
212 }
213
214 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800215 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800216 drivetrain_right_encoder_ = ::std::move(encoder);
217 }
218
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800219 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800220 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800221 shooter_encoder_ = ::std::move(encoder);
222 }
223
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800224 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800225 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800226 intake_encoder_.set_encoder(::std::move(encoder));
227 }
228
229 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
230 intake_encoder_.set_potentiometer(::std::move(potentiometer));
231 }
232
Brian Silverman50826c02017-02-18 14:40:25 -0800233 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
234 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800235 }
236
Brian Silverman052e69d2017-02-12 16:19:55 -0800237 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
238 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800239 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800240 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800241 }
242
Brianef030df2017-03-05 15:06:04 -0800243 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
244 hall_filter_.Add(input.get());
245 indexer_counter_.set_input(input.get());
246 indexer_hall_ = ::std::move(input);
247 }
248
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800249 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800250 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800251 turret_counter_.set_encoder(encoder.get());
252 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800253 }
254
Brianef030df2017-03-05 15:06:04 -0800255 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
256 hall_filter_.Add(input.get());
257 turret_counter_.set_input(input.get());
258 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800259 }
260
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800261 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800262 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800263 hood_encoder_.set_encoder(::std::move(encoder));
264 }
265
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800266 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800267 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800268 hood_encoder_.set_index(::std::move(index));
269 }
270
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800271 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
272 autonomous_modes_.at(i) = ::std::move(sensor);
273 }
274
Austin Schuh8347cb62017-04-08 14:37:34 -0700275 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
276 medium_encoder_filter_.Add(pwm_trigger.get());
277 pwm_trigger_ = ::std::move(pwm_trigger);
278 }
279
280 // All of the DMA-related set_* calls must be made before this, and it
281 // doesn't
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800282 // hurt to do all of them.
283 void set_dma(::std::unique_ptr<DMA> dma) {
284 dma_synchronizer_.reset(
285 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brianef030df2017-03-05 15:06:04 -0800286 dma_synchronizer_->Add(&indexer_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800287 dma_synchronizer_->Add(&hood_encoder_);
Brianef030df2017-03-05 15:06:04 -0800288 dma_synchronizer_->Add(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800289 }
290
Austin Schuh8347cb62017-04-08 14:37:34 -0700291 void RunPWMDetecter() {
292 ::aos::SetCurrentThreadRealtimePriority(41);
293
294 pwm_trigger_->RequestInterrupts();
295 // Rising edge only.
296 pwm_trigger_->SetUpSourceEdge(true, false);
297
298 monotonic_clock::time_point last_posedge_monotonic =
299 monotonic_clock::min_time;
300
301 while (run_) {
302 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
303 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
304 // Grab all the clocks.
305 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
306
307 aos_compiler_memory_barrier();
308 const double fpga_time_before = GetFPGATime() * 1e-6;
309 aos_compiler_memory_barrier();
310 const monotonic_clock::time_point monotonic_now =
311 monotonic_clock::now();
312 aos_compiler_memory_barrier();
313 const double fpga_time_after = GetFPGATime() * 1e-6;
314 aos_compiler_memory_barrier();
315
316 const double fpga_offset =
317 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
318
319 // Compute when the edge was.
320 const monotonic_clock::time_point monotonic_edge =
321 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
322 chrono::duration<double>(fpga_offset));
323
324 LOG(INFO, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
325 fpga_time_after - fpga_time_before, fpga_offset,
326 monotonic_edge.time_since_epoch().count());
327
328 // Compute bounds on the timestep and sampling times.
329 const double fpga_sample_length = fpga_time_after - fpga_time_before;
330 const chrono::nanoseconds elapsed_time =
331 monotonic_edge - last_posedge_monotonic;
332
333 last_posedge_monotonic = monotonic_edge;
334
335 // Verify that the values are sane.
336 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
337 continue;
338 }
339 if (fpga_offset < 0 || fpga_offset > 0.00015) {
340 continue;
341 }
342 if (elapsed_time >
343 chrono::microseconds(5050) + chrono::microseconds(4) ||
344 elapsed_time <
345 chrono::microseconds(5050) - chrono::microseconds(4)) {
346 continue;
347 }
348 // Good edge!
349 {
350 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
351 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
352 last_period_ = elapsed_time;
353 }
354 } else {
355 LOG(INFO, "PWM triggered %d\n", ret);
356 }
357 }
358 pwm_trigger_->CancelInterrupts();
359 }
360
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800361 void operator()() {
362 ::aos::SetCurrentThreadName("SensorReader");
363
364 my_pid_ = getpid();
Austin Schuh8347cb62017-04-08 14:37:34 -0700365 ds_ = &DriverStation::GetInstance();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800366
367 dma_synchronizer_->Start();
368
Austin Schuh8347cb62017-04-08 14:37:34 -0700369 ::aos::time::PhasedLoop phased_loop(last_period_,
370 ::std::chrono::milliseconds(3));
371 chrono::nanoseconds filtered_period = last_period_;
372
373 ::std::thread pwm_detecter_thread(
374 ::std::bind(&SensorReader::RunPWMDetecter, this));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800375
376 ::aos::SetCurrentThreadRealtimePriority(40);
377 while (run_) {
378 {
379 const int iterations = phased_loop.SleepUntilNext();
380 if (iterations != 1) {
381 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
382 }
383 }
384 RunIteration();
Austin Schuh8347cb62017-04-08 14:37:34 -0700385
386 monotonic_clock::time_point last_tick_timepoint;
387 chrono::nanoseconds period;
388 {
389 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
390 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
391 period = last_period_;
392 }
393
394 if (last_tick_timepoint == monotonic_clock::min_time) {
395 continue;
396 }
397 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
398 period, last_tick_timepoint + chrono::microseconds(2050));
399
400 // TODO(austin): If this is the first edge in a while, skip to it (plus
401 // an offset). Otherwise, slowly drift time to line up.
402
403 phased_loop.set_interval_and_offset(period, new_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800404 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700405 pwm_detecter_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800406 }
407
408 void RunIteration() {
409 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
410
411 const auto values = constants::GetValues();
412
413 {
414 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
415 drivetrain_message->right_encoder =
416 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800417 drivetrain_message->right_speed =
418 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
419
Campbell Crowleyae6e8422017-02-05 12:38:50 -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());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800424
425 drivetrain_message.Send();
426 }
427
428 dma_synchronizer_->RunIteration();
429
430 {
431 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800432 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800433 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800434 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800435 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800436
Brianef030df2017-03-05 15:06:04 -0800437 CopyPosition(indexer_counter_, &superstructure_message->column.indexer,
438 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuh546a0382017-04-16 19:10:18 -0700439 Values::kIndexerEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800440
Brian Silverman50826c02017-02-18 14:40:25 -0800441 superstructure_message->theta_shooter =
442 encoder_translate(shooter_encoder_->GetRaw(),
443 Values::kShooterEncoderCountsPerRevolution,
444 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800445
Brian Silverman50826c02017-02-18 14:40:25 -0800446 CopyPosition(hood_encoder_, &superstructure_message->hood,
447 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800448 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800449
Brianef030df2017-03-05 15:06:04 -0800450 CopyPosition(turret_counter_, &superstructure_message->column.turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800451 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800452 Values::kTurretEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800453
454 superstructure_message.Send();
455 }
456
457 {
Philipp Schrader996a2a22017-02-22 05:02:48 +0000458 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800459 auto_mode_message->mode = 0;
460 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700461 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800462 auto_mode_message->mode |= 1 << i;
463 }
464 }
465 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
466 auto_mode_message.Send();
467 }
468 }
469
470 void Quit() { run_ = false; }
471
472 private:
Brian Silverman50826c02017-02-18 14:40:25 -0800473 double encoder_translate(int32_t value, double counts_per_revolution,
474 double ratio) {
475 return static_cast<double>(value) / counts_per_revolution * ratio *
476 (2.0 * M_PI);
477 }
478
Brian Silverman7cce2d32017-02-19 21:48:48 -0800479 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
480 ::frc971::IndexPosition *position,
Brian Silverman50826c02017-02-18 14:40:25 -0800481 double encoder_counts_per_revolution, double encoder_ratio,
Brian Silverman7cce2d32017-02-19 21:48:48 -0800482 bool reverse) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800483 const double multiplier = reverse ? -1.0 : 1.0;
484 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800485 multiplier * encoder_translate(encoder.polled_encoder_value(),
486 encoder_counts_per_revolution,
487 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800488 position->latched_encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800489 multiplier * encoder_translate(encoder.last_encoder_value(),
490 encoder_counts_per_revolution,
491 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800492 position->index_pulses = encoder.index_posedge_count();
493 }
494
Brian Silverman50826c02017-02-18 14:40:25 -0800495 void CopyPosition(const AbsoluteEncoderAndPotentiometer &encoder,
496 ::frc971::PotAndAbsolutePosition *position,
497 double encoder_counts_per_revolution, double encoder_ratio,
498 ::std::function<double(double)> potentiometer_translate,
499 bool reverse, double pot_offset) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800500 const double multiplier = reverse ? -1.0 : 1.0;
501 position->pot = multiplier * potentiometer_translate(
Brian Silverman50826c02017-02-18 14:40:25 -0800502 encoder.ReadPotentiometerVoltage()) +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800503 pot_offset;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800504 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800505 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
506 encoder_counts_per_revolution,
507 encoder_ratio);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800508
509 position->absolute_encoder =
510 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
511 : encoder.ReadAbsoluteEncoder()) *
512 encoder_ratio * (2.0 * M_PI);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800513 }
514
Brianef030df2017-03-05 15:06:04 -0800515 void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter,
516 ::frc971::HallEffectAndPosition *position,
517 double encoder_counts_per_revolution, double encoder_ratio,
518 bool reverse) {
519 const double multiplier = reverse ? -1.0 : 1.0;
520 position->position =
521 multiplier * encoder_translate(counter.polled_encoder(),
522 encoder_counts_per_revolution,
523 encoder_ratio);
524 position->current = !counter.polled_value();
525 position->posedge_count = counter.negative_count();
526 position->negedge_count = counter.positive_count();
527 position->posedge_value =
528 multiplier * encoder_translate(counter.last_negative_encoder_value(),
529 encoder_counts_per_revolution,
530 encoder_ratio);
531 position->negedge_value =
532 multiplier * encoder_translate(counter.last_positive_encoder_value(),
533 encoder_counts_per_revolution,
534 encoder_ratio);
535 }
536
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800537 int32_t my_pid_;
538 DriverStation *ds_;
539
Austin Schuh8347cb62017-04-08 14:37:34 -0700540 // Mutex to manage access to the period and tick time variables.
541 ::aos::stl_mutex tick_time_mutex_;
542 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
543 monotonic_clock::min_time;
544 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
545
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800546 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
547
Brian Silverman052e69d2017-02-12 16:19:55 -0800548 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
Brianef030df2017-03-05 15:06:04 -0800549 hall_filter_;
Brian Silverman052e69d2017-02-12 16:19:55 -0800550
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800551 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
552 drivetrain_right_encoder_;
553
Brian Silverman50826c02017-02-18 14:40:25 -0800554 AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800555
Brian Silverman052e69d2017-02-12 16:19:55 -0800556 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800557 ::std::unique_ptr<DigitalInput> indexer_hall_;
558 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800559
Brianef030df2017-03-05 15:06:04 -0800560 ::std::unique_ptr<Encoder> turret_encoder_;
561 ::std::unique_ptr<DigitalInput> turret_hall_;
562 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
563
Brian Silverman7cce2d32017-02-19 21:48:48 -0800564 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800565 ::std::unique_ptr<Encoder> shooter_encoder_;
566
Austin Schuh8347cb62017-04-08 14:37:34 -0700567 ::std::unique_ptr<DigitalInput> pwm_trigger_;
568
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800569 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
570
571 ::std::atomic<bool> run_{true};
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800572};
573
Adam Snaidere0554ef2017-03-11 23:02:45 -0800574class SolenoidWriter {
575 public:
576 SolenoidWriter()
577 : superstructure_(".y2017.control_loops.superstructure_queue.output") {}
578
579 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
580
Austin Schuh8347cb62017-04-08 14:37:34 -0700581 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800582 lights_ = ::std::move(s);
583 }
584
Austin Schuh8347cb62017-04-08 14:37:34 -0700585 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700586 rgb_lights_ = ::std::move(s);
587 }
588
Adam Snaidere0554ef2017-03-11 23:02:45 -0800589 void operator()() {
590 ::aos::SetCurrentThreadName("Solenoids");
591 ::aos::SetCurrentThreadRealtimePriority(27);
592
593 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
594 ::std::chrono::milliseconds(1));
595
596 while (run_) {
597 {
598 const int iterations = phased_loop.SleepUntilNext();
599 if (iterations != 1) {
600 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
601 }
602 }
603
604 {
605 superstructure_.FetchLatest();
606 if (superstructure_.get()) {
607 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
608 lights_->Set(superstructure_->lights_on);
Austin Schuhc587c882017-03-29 21:33:10 -0700609 rgb_lights_->Set(superstructure_->red_light_on |
610 superstructure_->green_light_on |
611 superstructure_->blue_light_on);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800612 }
613 }
614
615 pcm_.Flush();
616 }
617 }
618
619 void Quit() { run_ = false; }
620
621 private:
622 ::frc971::wpilib::BufferedPcm pcm_;
623
Austin Schuhc587c882017-03-29 21:33:10 -0700624 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800625
Austin Schuh8347cb62017-04-08 14:37:34 -0700626 ::aos::Queue<::y2017::control_loops::SuperstructureQueue::Output>
Adam Snaidere0554ef2017-03-11 23:02:45 -0800627 superstructure_;
628
629 ::std::atomic<bool> run_{true};
630};
631
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800632class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
633 public:
Austin Schuh8347cb62017-04-08 14:37:34 -0700634 void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800635 drivetrain_left_victor_ = ::std::move(t);
636 }
637
Austin Schuh8347cb62017-04-08 14:37:34 -0700638 void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800639 drivetrain_right_victor_ = ::std::move(t);
640 }
641
642 private:
643 virtual void Read() override {
644 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
645 }
646
647 virtual void Write() override {
648 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
649 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh410e3812017-02-21 16:44:03 -0800650 drivetrain_left_victor_->SetSpeed(-queue->left_voltage / 12.0);
651 drivetrain_right_victor_->SetSpeed(queue->right_voltage / 12.0);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800652 }
653
654 virtual void Stop() override {
655 LOG(WARNING, "drivetrain output too old\n");
656 drivetrain_left_victor_->SetDisabled();
657 drivetrain_right_victor_->SetDisabled();
658 }
659
Austin Schuh8347cb62017-04-08 14:37:34 -0700660 ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_,
661 drivetrain_right_victor_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800662};
663
664class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
665 public:
Austin Schuh8347cb62017-04-08 14:37:34 -0700666 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800667 intake_victor_ = ::std::move(t);
668 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700669 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800670 intake_rollers_victor_ = ::std::move(t);
671 }
672
Austin Schuh8347cb62017-04-08 14:37:34 -0700673 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800674 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800675 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700676 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800677 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800678 }
679
Austin Schuh6a8131b2017-04-08 15:39:22 -0700680 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
681 gear_servo_ = ::std::move(t);
682 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700683 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800684 shooter_victor_ = ::std::move(t);
685 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700686 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800687 turret_victor_ = ::std::move(t);
688 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700689 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800690 hood_victor_ = ::std::move(t);
691 }
692
Austin Schuhc587c882017-03-29 21:33:10 -0700693 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
694 red_light_ = ::std::move(t);
695 }
696 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
697 green_light_ = ::std::move(t);
698 }
699 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
700 blue_light_ = ::std::move(t);
701 }
702
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800703 private:
704 virtual void Read() override {
705 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
706 }
707
708 virtual void Write() override {
709 auto &queue = ::y2017::control_loops::superstructure_queue.output;
710 LOG_STRUCT(DEBUG, "will output", *queue);
711 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
712 -kMaxBringupPower, kMaxBringupPower) /
713 12.0);
714 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800715 indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0);
716 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0);
Austin Schuh410e3812017-02-21 16:44:03 -0800717 turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800718 -kMaxBringupPower, kMaxBringupPower) /
719 12.0);
720 hood_victor_->SetSpeed(
721 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
722 12.0);
723 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700724
725 red_light_->Set(queue->red_light_on);
726 green_light_->Set(queue->green_light_on);
727 blue_light_->Set(queue->blue_light_on);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700728
729 gear_servo_->Set(queue->gear_servo);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800730 }
731
732 virtual void Stop() override {
733 LOG(WARNING, "Superstructure output too old.\n");
734 intake_victor_->SetDisabled();
735 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800736 indexer_victor_->SetDisabled();
737 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800738 turret_victor_->SetDisabled();
739 hood_victor_->SetDisabled();
740 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700741
Austin Schuh6a8131b2017-04-08 15:39:22 -0700742 gear_servo_->SetOffline();
743
Austin Schuhc587c882017-03-29 21:33:10 -0700744 red_light_->Set(true);
745 green_light_->Set(true);
746 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800747 }
748
Austin Schuh8347cb62017-04-08 14:37:34 -0700749 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
750 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
751 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700752
Austin Schuh6a8131b2017-04-08 15:39:22 -0700753 ::std::unique_ptr<::frc::Servo> gear_servo_;
754
Austin Schuhc587c882017-03-29 21:33:10 -0700755 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800756};
757
758class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
759 public:
760 ::std::unique_ptr<Encoder> make_encoder(int index) {
761 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
762 Encoder::k4X);
763 }
764
765 void Run() override {
766 ::aos::InitNRT();
767 ::aos::SetCurrentThreadName("StartCompetition");
768
769 ::frc971::wpilib::JoystickSender joystick_sender;
770 ::std::thread joystick_thread(::std::ref(joystick_sender));
771
772 ::frc971::wpilib::PDPFetcher pdp_fetcher;
773 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
774 SensorReader reader;
775
776 // TODO(campbell): Update port numbers
777 reader.set_drivetrain_left_encoder(make_encoder(0));
778 reader.set_drivetrain_right_encoder(make_encoder(1));
779
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800780 reader.set_intake_encoder(make_encoder(3));
Brian Silverman50826c02017-02-18 14:40:25 -0800781 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800782 reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800783
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800784 reader.set_indexer_encoder(make_encoder(5));
Brianef030df2017-03-05 15:06:04 -0800785 reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800786
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800787 reader.set_turret_encoder(make_encoder(6));
Brianef030df2017-03-05 15:06:04 -0800788 reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800789
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800790 reader.set_hood_encoder(make_encoder(4));
791 reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800792
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800793 reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800794
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800795 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
796 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Austin Schuh8347cb62017-04-08 14:37:34 -0700797
798 reader.set_pwm_trigger(make_unique<DigitalInput>(7));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800799
800 reader.set_dma(make_unique<DMA>());
801 ::std::thread reader_thread(::std::ref(reader));
802
Brian Silvermanb4439852017-02-24 19:49:09 -0800803 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800804 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700805 imu.SetDummySPI(SPI::Port::kOnboardCS2);
806 auto imu_reset = make_unique<DigitalOutput>(6);
807 imu.set_reset(imu_reset.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800808 ::std::thread imu_thread(::std::ref(imu));
809
810 DrivetrainWriter drivetrain_writer;
811 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700812 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800813 drivetrain_writer.set_drivetrain_right_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700814 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800815 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
816
817 SuperstructureWriter superstructure_writer;
818 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700819 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800820 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700821 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800822 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700823 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800824 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700825 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800826 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700827 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800828 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700829 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800830 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700831 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700832
Austin Schuh6a8131b2017-04-08 15:39:22 -0700833 superstructure_writer.set_gear_servo(
834 ::std::unique_ptr<Servo>(new Servo(0)));
835
Austin Schuhc587c882017-03-29 21:33:10 -0700836 superstructure_writer.set_red_light(
837 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
838 superstructure_writer.set_green_light(
839 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
840 superstructure_writer.set_blue_light(
841 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
842
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800843 ::std::thread superstructure_writer_thread(
844 ::std::ref(superstructure_writer));
845
Adam Snaidere0554ef2017-03-11 23:02:45 -0800846 SolenoidWriter solenoid_writer;
847 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700848 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800849
850 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
851
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800852 // Wait forever. Not much else to do...
853 while (true) {
854 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
855 if (r != 0) {
856 PLOG(WARNING, "infinite select failed");
857 } else {
858 PLOG(WARNING, "infinite select succeeded??\n");
859 }
860 }
861
862 LOG(ERROR, "Exiting WPILibRobot\n");
863
864 joystick_sender.Quit();
865 joystick_thread.join();
866 pdp_fetcher.Quit();
867 pdp_fetcher_thread.join();
868 reader.Quit();
869 reader_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800870 imu.Quit();
871 imu_thread.join();
872
873 drivetrain_writer.Quit();
874 drivetrain_writer_thread.join();
875 superstructure_writer.Quit();
876 superstructure_writer_thread.join();
877
878 ::aos::Cleanup();
879 }
880};
881
Brian Silverman052e69d2017-02-12 16:19:55 -0800882} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800883} // namespace wpilib
884} // namespace y2017
885
886AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);