blob: 49060091f84c1a4359344c729e6efb00d1c8fa28 [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
Parker Schuhd3b7a8872018-02-19 16:42:27 -080013#include "frc971/wpilib/ahal/AnalogInput.h"
14#include "frc971/wpilib/ahal/Compressor.h"
15#include "frc971/wpilib/ahal/Counter.h"
16#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
17#include "frc971/wpilib/ahal/DriverStation.h"
18#include "frc971/wpilib/ahal/Encoder.h"
19#include "frc971/wpilib/ahal/Relay.h"
20#include "frc971/wpilib/ahal/Servo.h"
21#include "frc971/wpilib/ahal/VictorSP.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080022#undef ERROR
23
John Park33858a32018-09-28 23:05:48 -070024#include "aos/commonmath.h"
25#include "aos/logging/logging.h"
26#include "aos/logging/queue_logging.h"
27#include "aos/robot_state/robot_state.q.h"
28#include "aos/stl_mutex/stl_mutex.h"
29#include "aos/time/time.h"
30#include "aos/util/compiler_memory_barrier.h"
31#include "aos/util/log_interval.h"
32#include "aos/util/phased_loop.h"
33#include "aos/util/wrapping_counter.h"
John Park398c74a2018-10-20 21:17:39 -070034#include "aos/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;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080064using namespace frc;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080065
66namespace y2017 {
67namespace wpilib {
68namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080069
Campbell Crowleyae6e8422017-02-05 12:38:50 -080070constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080071
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.
78template <class T, class... U>
79std::unique_ptr<T> make_unique(U &&... u) {
80 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
81}
82
Brian Silverman052e69d2017-02-12 16:19:55 -080083// TODO(brian): Use ::std::max instead once we have C++14 so that can be
84// constexpr.
85template <typename T>
86constexpr T max(T a, T b) {
87 return (a > b) ? a : b;
88}
89template <typename T, typename... Rest>
90constexpr T max(T a, T b, T c, Rest... rest) {
91 return max(max(a, b), c, rest...);
92}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080093
Campbell Crowleyae6e8422017-02-05 12:38:50 -080094double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080095 return static_cast<double>(in) /
96 Values::kDrivetrainEncoderCountsPerRevolution *
97 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080098}
99
100double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800101 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
102 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800103}
104
Brian Silverman50826c02017-02-18 14:40:25 -0800105// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800106double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800107 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800108 (2 * M_PI /*radians*/);
109}
110
Brian Silverman052e69d2017-02-12 16:19:55 -0800111constexpr double kMaxFastEncoderPulsesPerSecond =
112 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
113 Values::kMaxShooterEncoderPulsesPerSecond);
114static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
115 "fast encoders are too fast");
116constexpr double kMaxMediumEncoderPulsesPerSecond =
117 max(Values::kMaxIntakeEncoderPulsesPerSecond,
118 Values::kMaxTurretEncoderPulsesPerSecond,
119 Values::kMaxIndexerEncoderPulsesPerSecond);
120static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
121 "medium encoders are too fast");
122constexpr double kMaxSlowEncoderPulsesPerSecond =
123 Values::kMaxHoodEncoderPulsesPerSecond;
124static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
125 "slow encoders are too fast");
Brianef030df2017-03-05 15:06:04 -0800126static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond,
127 "slow encoders are faster than medium?");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800128
129// Class to send position messages with sensor readings to our loops.
130class SensorReader {
131 public:
132 SensorReader() {
Brian Silverman052e69d2017-02-12 16:19:55 -0800133 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800134 // we should ever see.
Brian Silverman052e69d2017-02-12 16:19:55 -0800135 fast_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800136 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800137 kMaxFastEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800138 0.5));
Brian Silverman052e69d2017-02-12 16:19:55 -0800139 medium_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800140 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800141 kMaxMediumEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800142 0.5));
Brianef030df2017-03-05 15:06:04 -0800143 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800144 }
145
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800146 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800147 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800148 drivetrain_left_encoder_ = ::std::move(encoder);
149 }
150
151 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800152 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800153 drivetrain_right_encoder_ = ::std::move(encoder);
154 }
155
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800156 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800157 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800158 shooter_encoder_ = ::std::move(encoder);
159 }
160
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800161 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800162 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800163 intake_encoder_.set_encoder(::std::move(encoder));
164 }
165
166 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
167 intake_encoder_.set_potentiometer(::std::move(potentiometer));
168 }
169
Brian Silverman50826c02017-02-18 14:40:25 -0800170 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
171 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800172 }
173
Brian Silverman052e69d2017-02-12 16:19:55 -0800174 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
175 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800176 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800177 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800178 }
179
Brianef030df2017-03-05 15:06:04 -0800180 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
181 hall_filter_.Add(input.get());
182 indexer_counter_.set_input(input.get());
183 indexer_hall_ = ::std::move(input);
184 }
185
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800186 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800187 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800188 turret_counter_.set_encoder(encoder.get());
189 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800190 }
191
Brianef030df2017-03-05 15:06:04 -0800192 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
193 hall_filter_.Add(input.get());
194 turret_counter_.set_input(input.get());
195 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800196 }
197
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800198 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800199 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800200 hood_encoder_.set_encoder(::std::move(encoder));
201 }
202
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800203 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800204 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800205 hood_encoder_.set_index(::std::move(index));
206 }
207
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800208 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
209 autonomous_modes_.at(i) = ::std::move(sensor);
210 }
211
Austin Schuh8347cb62017-04-08 14:37:34 -0700212 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
213 medium_encoder_filter_.Add(pwm_trigger.get());
214 pwm_trigger_ = ::std::move(pwm_trigger);
215 }
216
217 // All of the DMA-related set_* calls must be made before this, and it
218 // doesn't
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800219 // hurt to do all of them.
220 void set_dma(::std::unique_ptr<DMA> dma) {
221 dma_synchronizer_.reset(
222 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brianef030df2017-03-05 15:06:04 -0800223 dma_synchronizer_->Add(&indexer_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800224 dma_synchronizer_->Add(&hood_encoder_);
Brianef030df2017-03-05 15:06:04 -0800225 dma_synchronizer_->Add(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800226 }
227
Austin Schuh8347cb62017-04-08 14:37:34 -0700228 void RunPWMDetecter() {
229 ::aos::SetCurrentThreadRealtimePriority(41);
230
231 pwm_trigger_->RequestInterrupts();
232 // Rising edge only.
233 pwm_trigger_->SetUpSourceEdge(true, false);
234
235 monotonic_clock::time_point last_posedge_monotonic =
236 monotonic_clock::min_time;
237
238 while (run_) {
239 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
240 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
241 // Grab all the clocks.
242 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
243
244 aos_compiler_memory_barrier();
245 const double fpga_time_before = GetFPGATime() * 1e-6;
246 aos_compiler_memory_barrier();
247 const monotonic_clock::time_point monotonic_now =
248 monotonic_clock::now();
249 aos_compiler_memory_barrier();
250 const double fpga_time_after = GetFPGATime() * 1e-6;
251 aos_compiler_memory_barrier();
252
253 const double fpga_offset =
254 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
255
256 // Compute when the edge was.
257 const monotonic_clock::time_point monotonic_edge =
258 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
259 chrono::duration<double>(fpga_offset));
260
261 LOG(INFO, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
262 fpga_time_after - fpga_time_before, fpga_offset,
263 monotonic_edge.time_since_epoch().count());
264
265 // Compute bounds on the timestep and sampling times.
266 const double fpga_sample_length = fpga_time_after - fpga_time_before;
267 const chrono::nanoseconds elapsed_time =
268 monotonic_edge - last_posedge_monotonic;
269
270 last_posedge_monotonic = monotonic_edge;
271
272 // Verify that the values are sane.
273 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
274 continue;
275 }
276 if (fpga_offset < 0 || fpga_offset > 0.00015) {
277 continue;
278 }
279 if (elapsed_time >
280 chrono::microseconds(5050) + chrono::microseconds(4) ||
281 elapsed_time <
282 chrono::microseconds(5050) - chrono::microseconds(4)) {
283 continue;
284 }
285 // Good edge!
286 {
287 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
288 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
289 last_period_ = elapsed_time;
290 }
291 } else {
292 LOG(INFO, "PWM triggered %d\n", ret);
293 }
294 }
295 pwm_trigger_->CancelInterrupts();
296 }
297
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800298 void operator()() {
299 ::aos::SetCurrentThreadName("SensorReader");
300
301 my_pid_ = getpid();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800302
303 dma_synchronizer_->Start();
304
Austin Schuh8347cb62017-04-08 14:37:34 -0700305 ::aos::time::PhasedLoop phased_loop(last_period_,
306 ::std::chrono::milliseconds(3));
307 chrono::nanoseconds filtered_period = last_period_;
308
309 ::std::thread pwm_detecter_thread(
310 ::std::bind(&SensorReader::RunPWMDetecter, this));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800311
312 ::aos::SetCurrentThreadRealtimePriority(40);
313 while (run_) {
314 {
315 const int iterations = phased_loop.SleepUntilNext();
316 if (iterations != 1) {
317 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
318 }
319 }
320 RunIteration();
Austin Schuh8347cb62017-04-08 14:37:34 -0700321
322 monotonic_clock::time_point last_tick_timepoint;
323 chrono::nanoseconds period;
324 {
325 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
326 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
327 period = last_period_;
328 }
329
330 if (last_tick_timepoint == monotonic_clock::min_time) {
331 continue;
332 }
333 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
334 period, last_tick_timepoint + chrono::microseconds(2050));
335
336 // TODO(austin): If this is the first edge in a while, skip to it (plus
337 // an offset). Otherwise, slowly drift time to line up.
338
339 phased_loop.set_interval_and_offset(period, new_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800340 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700341 pwm_detecter_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800342 }
343
344 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700345 ::frc971::wpilib::SendRobotState(my_pid_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800346
347 const auto values = constants::GetValues();
348
349 {
350 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
351 drivetrain_message->right_encoder =
352 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800353 drivetrain_message->right_speed =
354 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
355
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800356 drivetrain_message->left_encoder =
357 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
358 drivetrain_message->left_speed =
359 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800360
361 drivetrain_message.Send();
362 }
363
364 dma_synchronizer_->RunIteration();
365
366 {
367 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800368 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800369 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800370 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800371 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800372
Brianef030df2017-03-05 15:06:04 -0800373 CopyPosition(indexer_counter_, &superstructure_message->column.indexer,
374 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuh546a0382017-04-16 19:10:18 -0700375 Values::kIndexerEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800376
Brian Silverman50826c02017-02-18 14:40:25 -0800377 superstructure_message->theta_shooter =
378 encoder_translate(shooter_encoder_->GetRaw(),
379 Values::kShooterEncoderCountsPerRevolution,
380 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800381
Brian Silverman50826c02017-02-18 14:40:25 -0800382 CopyPosition(hood_encoder_, &superstructure_message->hood,
383 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800384 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800385
Brianef030df2017-03-05 15:06:04 -0800386 CopyPosition(turret_counter_, &superstructure_message->column.turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800387 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800388 Values::kTurretEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800389
390 superstructure_message.Send();
391 }
392
393 {
Philipp Schrader996a2a22017-02-22 05:02:48 +0000394 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800395 auto_mode_message->mode = 0;
396 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700397 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800398 auto_mode_message->mode |= 1 << i;
399 }
400 }
401 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
402 auto_mode_message.Send();
403 }
404 }
405
406 void Quit() { run_ = false; }
407
408 private:
Brian Silverman50826c02017-02-18 14:40:25 -0800409 double encoder_translate(int32_t value, double counts_per_revolution,
410 double ratio) {
411 return static_cast<double>(value) / counts_per_revolution * ratio *
412 (2.0 * M_PI);
413 }
414
Brian Silverman7cce2d32017-02-19 21:48:48 -0800415 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
416 ::frc971::IndexPosition *position,
Brian Silverman50826c02017-02-18 14:40:25 -0800417 double encoder_counts_per_revolution, double encoder_ratio,
Brian Silverman7cce2d32017-02-19 21:48:48 -0800418 bool reverse) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800419 const double multiplier = reverse ? -1.0 : 1.0;
420 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800421 multiplier * encoder_translate(encoder.polled_encoder_value(),
422 encoder_counts_per_revolution,
423 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800424 position->latched_encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800425 multiplier * encoder_translate(encoder.last_encoder_value(),
426 encoder_counts_per_revolution,
427 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800428 position->index_pulses = encoder.index_posedge_count();
429 }
430
Austin Schuh2a3e0632018-02-19 16:24:49 -0800431 void CopyPosition(
432 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
433 ::frc971::PotAndAbsolutePosition *position,
434 double encoder_counts_per_revolution, double encoder_ratio,
435 ::std::function<double(double)> potentiometer_translate, bool reverse,
436 double pot_offset) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800437 const double multiplier = reverse ? -1.0 : 1.0;
438 position->pot = multiplier * potentiometer_translate(
Brian Silverman50826c02017-02-18 14:40:25 -0800439 encoder.ReadPotentiometerVoltage()) +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800440 pot_offset;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800441 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800442 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
443 encoder_counts_per_revolution,
444 encoder_ratio);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800445
446 position->absolute_encoder =
447 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
448 : encoder.ReadAbsoluteEncoder()) *
449 encoder_ratio * (2.0 * M_PI);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800450 }
451
Brianef030df2017-03-05 15:06:04 -0800452 void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter,
453 ::frc971::HallEffectAndPosition *position,
454 double encoder_counts_per_revolution, double encoder_ratio,
455 bool reverse) {
456 const double multiplier = reverse ? -1.0 : 1.0;
457 position->position =
458 multiplier * encoder_translate(counter.polled_encoder(),
459 encoder_counts_per_revolution,
460 encoder_ratio);
461 position->current = !counter.polled_value();
462 position->posedge_count = counter.negative_count();
463 position->negedge_count = counter.positive_count();
464 position->posedge_value =
465 multiplier * encoder_translate(counter.last_negative_encoder_value(),
466 encoder_counts_per_revolution,
467 encoder_ratio);
468 position->negedge_value =
469 multiplier * encoder_translate(counter.last_positive_encoder_value(),
470 encoder_counts_per_revolution,
471 encoder_ratio);
472 }
473
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800474 int32_t my_pid_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800475
Austin Schuh8347cb62017-04-08 14:37:34 -0700476 // Mutex to manage access to the period and tick time variables.
477 ::aos::stl_mutex tick_time_mutex_;
478 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
479 monotonic_clock::min_time;
480 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
481
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800482 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
483
Brian Silverman052e69d2017-02-12 16:19:55 -0800484 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
Brianef030df2017-03-05 15:06:04 -0800485 hall_filter_;
Brian Silverman052e69d2017-02-12 16:19:55 -0800486
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800487 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
488 drivetrain_right_encoder_;
489
Austin Schuh2a3e0632018-02-19 16:24:49 -0800490 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800491
Brian Silverman052e69d2017-02-12 16:19:55 -0800492 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800493 ::std::unique_ptr<DigitalInput> indexer_hall_;
494 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800495
Brianef030df2017-03-05 15:06:04 -0800496 ::std::unique_ptr<Encoder> turret_encoder_;
497 ::std::unique_ptr<DigitalInput> turret_hall_;
498 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
499
Brian Silverman7cce2d32017-02-19 21:48:48 -0800500 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800501 ::std::unique_ptr<Encoder> shooter_encoder_;
502
Austin Schuh8347cb62017-04-08 14:37:34 -0700503 ::std::unique_ptr<DigitalInput> pwm_trigger_;
504
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800505 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
506
507 ::std::atomic<bool> run_{true};
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800508};
509
Adam Snaidere0554ef2017-03-11 23:02:45 -0800510class SolenoidWriter {
511 public:
512 SolenoidWriter()
513 : superstructure_(".y2017.control_loops.superstructure_queue.output") {}
514
515 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
516
Austin Schuh8347cb62017-04-08 14:37:34 -0700517 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800518 lights_ = ::std::move(s);
519 }
520
Austin Schuh8347cb62017-04-08 14:37:34 -0700521 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700522 rgb_lights_ = ::std::move(s);
523 }
524
Adam Snaidere0554ef2017-03-11 23:02:45 -0800525 void operator()() {
526 ::aos::SetCurrentThreadName("Solenoids");
527 ::aos::SetCurrentThreadRealtimePriority(27);
528
529 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
530 ::std::chrono::milliseconds(1));
531
532 while (run_) {
533 {
534 const int iterations = phased_loop.SleepUntilNext();
535 if (iterations != 1) {
536 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
537 }
538 }
539
540 {
541 superstructure_.FetchLatest();
542 if (superstructure_.get()) {
543 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
544 lights_->Set(superstructure_->lights_on);
Austin Schuhc587c882017-03-29 21:33:10 -0700545 rgb_lights_->Set(superstructure_->red_light_on |
546 superstructure_->green_light_on |
547 superstructure_->blue_light_on);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800548 }
549 }
550
551 pcm_.Flush();
552 }
553 }
554
555 void Quit() { run_ = false; }
556
557 private:
558 ::frc971::wpilib::BufferedPcm pcm_;
559
Austin Schuhc587c882017-03-29 21:33:10 -0700560 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800561
Austin Schuh8347cb62017-04-08 14:37:34 -0700562 ::aos::Queue<::y2017::control_loops::SuperstructureQueue::Output>
Adam Snaidere0554ef2017-03-11 23:02:45 -0800563 superstructure_;
564
565 ::std::atomic<bool> run_{true};
566};
567
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800568class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
569 public:
Austin Schuh8347cb62017-04-08 14:37:34 -0700570 void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800571 drivetrain_left_victor_ = ::std::move(t);
572 }
573
Austin Schuh8347cb62017-04-08 14:37:34 -0700574 void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800575 drivetrain_right_victor_ = ::std::move(t);
576 }
577
578 private:
579 virtual void Read() override {
580 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
581 }
582
583 virtual void Write() override {
584 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
585 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh410e3812017-02-21 16:44:03 -0800586 drivetrain_left_victor_->SetSpeed(-queue->left_voltage / 12.0);
587 drivetrain_right_victor_->SetSpeed(queue->right_voltage / 12.0);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800588 }
589
590 virtual void Stop() override {
591 LOG(WARNING, "drivetrain output too old\n");
592 drivetrain_left_victor_->SetDisabled();
593 drivetrain_right_victor_->SetDisabled();
594 }
595
Austin Schuh8347cb62017-04-08 14:37:34 -0700596 ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_,
597 drivetrain_right_victor_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800598};
599
600class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
601 public:
Austin Schuh8347cb62017-04-08 14:37:34 -0700602 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800603 intake_victor_ = ::std::move(t);
604 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700605 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800606 intake_rollers_victor_ = ::std::move(t);
607 }
608
Austin Schuh8347cb62017-04-08 14:37:34 -0700609 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800610 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800611 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700612 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800613 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800614 }
615
Austin Schuh6a8131b2017-04-08 15:39:22 -0700616 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
617 gear_servo_ = ::std::move(t);
618 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700619 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800620 shooter_victor_ = ::std::move(t);
621 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700622 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800623 turret_victor_ = ::std::move(t);
624 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700625 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800626 hood_victor_ = ::std::move(t);
627 }
628
Austin Schuhc587c882017-03-29 21:33:10 -0700629 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
630 red_light_ = ::std::move(t);
631 }
632 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
633 green_light_ = ::std::move(t);
634 }
635 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
636 blue_light_ = ::std::move(t);
637 }
638
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800639 private:
640 virtual void Read() override {
641 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
642 }
643
644 virtual void Write() override {
645 auto &queue = ::y2017::control_loops::superstructure_queue.output;
646 LOG_STRUCT(DEBUG, "will output", *queue);
647 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
648 -kMaxBringupPower, kMaxBringupPower) /
649 12.0);
650 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800651 indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0);
652 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0);
Austin Schuh410e3812017-02-21 16:44:03 -0800653 turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800654 -kMaxBringupPower, kMaxBringupPower) /
655 12.0);
656 hood_victor_->SetSpeed(
657 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
658 12.0);
659 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700660
661 red_light_->Set(queue->red_light_on);
662 green_light_->Set(queue->green_light_on);
663 blue_light_->Set(queue->blue_light_on);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700664
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800665 gear_servo_->SetPosition(queue->gear_servo);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800666 }
667
668 virtual void Stop() override {
669 LOG(WARNING, "Superstructure output too old.\n");
670 intake_victor_->SetDisabled();
671 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800672 indexer_victor_->SetDisabled();
673 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800674 turret_victor_->SetDisabled();
675 hood_victor_->SetDisabled();
676 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700677
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800678 gear_servo_->SetRaw(0);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700679
Austin Schuhc587c882017-03-29 21:33:10 -0700680 red_light_->Set(true);
681 green_light_->Set(true);
682 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800683 }
684
Austin Schuh8347cb62017-04-08 14:37:34 -0700685 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
686 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
687 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700688
Austin Schuh6a8131b2017-04-08 15:39:22 -0700689 ::std::unique_ptr<::frc::Servo> gear_servo_;
690
Austin Schuhc587c882017-03-29 21:33:10 -0700691 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800692};
693
694class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
695 public:
696 ::std::unique_ptr<Encoder> make_encoder(int index) {
697 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
698 Encoder::k4X);
699 }
700
701 void Run() override {
702 ::aos::InitNRT();
703 ::aos::SetCurrentThreadName("StartCompetition");
704
705 ::frc971::wpilib::JoystickSender joystick_sender;
706 ::std::thread joystick_thread(::std::ref(joystick_sender));
707
708 ::frc971::wpilib::PDPFetcher pdp_fetcher;
709 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
710 SensorReader reader;
711
712 // TODO(campbell): Update port numbers
713 reader.set_drivetrain_left_encoder(make_encoder(0));
714 reader.set_drivetrain_right_encoder(make_encoder(1));
715
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800716 reader.set_intake_encoder(make_encoder(3));
Brian Silverman50826c02017-02-18 14:40:25 -0800717 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800718 reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800719
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800720 reader.set_indexer_encoder(make_encoder(5));
Brianef030df2017-03-05 15:06:04 -0800721 reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800722
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800723 reader.set_turret_encoder(make_encoder(6));
Brianef030df2017-03-05 15:06:04 -0800724 reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800725
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800726 reader.set_hood_encoder(make_encoder(4));
727 reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800728
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800729 reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800730
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800731 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
732 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Austin Schuh8347cb62017-04-08 14:37:34 -0700733
734 reader.set_pwm_trigger(make_unique<DigitalInput>(7));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800735
736 reader.set_dma(make_unique<DMA>());
737 ::std::thread reader_thread(::std::ref(reader));
738
Brian Silvermanb4439852017-02-24 19:49:09 -0800739 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800740 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700741 imu.SetDummySPI(SPI::Port::kOnboardCS2);
742 auto imu_reset = make_unique<DigitalOutput>(6);
743 imu.set_reset(imu_reset.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800744 ::std::thread imu_thread(::std::ref(imu));
745
746 DrivetrainWriter drivetrain_writer;
747 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700748 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800749 drivetrain_writer.set_drivetrain_right_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700750 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800751 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
752
753 SuperstructureWriter superstructure_writer;
754 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700755 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800756 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700757 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800758 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700759 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800760 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700761 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800762 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700763 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800764 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700765 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800766 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700767 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700768
Austin Schuh6a8131b2017-04-08 15:39:22 -0700769 superstructure_writer.set_gear_servo(
770 ::std::unique_ptr<Servo>(new Servo(0)));
771
Austin Schuhc587c882017-03-29 21:33:10 -0700772 superstructure_writer.set_red_light(
773 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
774 superstructure_writer.set_green_light(
775 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
776 superstructure_writer.set_blue_light(
777 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
778
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800779 ::std::thread superstructure_writer_thread(
780 ::std::ref(superstructure_writer));
781
Adam Snaidere0554ef2017-03-11 23:02:45 -0800782 SolenoidWriter solenoid_writer;
783 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700784 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800785
786 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
787
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800788 // Wait forever. Not much else to do...
789 while (true) {
790 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
791 if (r != 0) {
792 PLOG(WARNING, "infinite select failed");
793 } else {
794 PLOG(WARNING, "infinite select succeeded??\n");
795 }
796 }
797
798 LOG(ERROR, "Exiting WPILibRobot\n");
799
800 joystick_sender.Quit();
801 joystick_thread.join();
802 pdp_fetcher.Quit();
803 pdp_fetcher_thread.join();
804 reader.Quit();
805 reader_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800806 imu.Quit();
807 imu_thread.join();
808
809 drivetrain_writer.Quit();
810 drivetrain_writer_thread.join();
811 superstructure_writer.Quit();
812 superstructure_writer_thread.join();
813
814 ::aos::Cleanup();
815 }
816};
817
Brian Silverman052e69d2017-02-12 16:19:55 -0800818} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800819} // namespace wpilib
820} // namespace y2017
821
822AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);