blob: 1663ecd0793b02668865957386bc1f857729a806 [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"
Brian Silvermanf819b442019-01-20 16:51:04 -080025#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070026#include "aos/logging/logging.h"
27#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080028#include "aos/make_unique.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/robot_state/robot_state.q.h"
30#include "aos/stl_mutex/stl_mutex.h"
31#include "aos/time/time.h"
32#include "aos/util/compiler_memory_barrier.h"
33#include "aos/util/log_interval.h"
34#include "aos/util/phased_loop.h"
35#include "aos/util/wrapping_counter.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080036
Philipp Schrader996a2a22017-02-22 05:02:48 +000037#include "frc971/autonomous/auto.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080038#include "frc971/control_loops/control_loops.q.h"
39#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070040#include "frc971/wpilib/ADIS16448.h"
41#include "frc971/wpilib/buffered_pcm.h"
42#include "frc971/wpilib/buffered_solenoid.h"
43#include "frc971/wpilib/dma.h"
44#include "frc971/wpilib/dma_edge_counting.h"
45#include "frc971/wpilib/encoder_and_potentiometer.h"
46#include "frc971/wpilib/interrupt_edge_counting.h"
47#include "frc971/wpilib/joystick_sender.h"
48#include "frc971/wpilib/logging.q.h"
49#include "frc971/wpilib/loop_output_handler.h"
50#include "frc971/wpilib/pdp_fetcher.h"
51#include "frc971/wpilib/wpilib_interface.h"
52#include "frc971/wpilib/wpilib_robot_base.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080053#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080054#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080055
Campbell Crowleyae6e8422017-02-05 12:38:50 -080056#ifndef M_PI
57#define M_PI 3.14159265358979323846
58#endif
59
60using ::frc971::control_loops::drivetrain_queue;
61using ::y2017::control_loops::superstructure_queue;
Brian Silverman052e69d2017-02-12 16:19:55 -080062using ::y2017::constants::Values;
Austin Schuh8347cb62017-04-08 14:37:34 -070063using ::aos::monotonic_clock;
64namespace chrono = ::std::chrono;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080065using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080066using aos::make_unique;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080067
68namespace y2017 {
69namespace wpilib {
70namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080071
Campbell Crowleyae6e8422017-02-05 12:38:50 -080072constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080073
74// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
75// DMA stuff and then removing the * 2.0 in *_translate.
76// The low bit is direction.
77
Brian Silverman052e69d2017-02-12 16:19:55 -080078// TODO(brian): Use ::std::max instead once we have C++14 so that can be
79// constexpr.
80template <typename T>
81constexpr T max(T a, T b) {
82 return (a > b) ? a : b;
83}
84template <typename T, typename... Rest>
85constexpr T max(T a, T b, T c, Rest... rest) {
86 return max(max(a, b), c, rest...);
87}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080088
Campbell Crowleyae6e8422017-02-05 12:38:50 -080089double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080090 return static_cast<double>(in) /
91 Values::kDrivetrainEncoderCountsPerRevolution *
92 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080093}
94
95double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080096 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
97 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080098}
99
Brian Silverman50826c02017-02-18 14:40:25 -0800100// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800101double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800102 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800103 (2 * M_PI /*radians*/);
104}
105
Brian Silverman052e69d2017-02-12 16:19:55 -0800106constexpr double kMaxFastEncoderPulsesPerSecond =
107 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
108 Values::kMaxShooterEncoderPulsesPerSecond);
109static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
110 "fast encoders are too fast");
111constexpr double kMaxMediumEncoderPulsesPerSecond =
112 max(Values::kMaxIntakeEncoderPulsesPerSecond,
113 Values::kMaxTurretEncoderPulsesPerSecond,
114 Values::kMaxIndexerEncoderPulsesPerSecond);
115static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
116 "medium encoders are too fast");
117constexpr double kMaxSlowEncoderPulsesPerSecond =
118 Values::kMaxHoodEncoderPulsesPerSecond;
119static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
120 "slow encoders are too fast");
Brianef030df2017-03-05 15:06:04 -0800121static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond,
122 "slow encoders are faster than medium?");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800123
124// Class to send position messages with sensor readings to our loops.
125class SensorReader {
126 public:
127 SensorReader() {
Brian Silverman052e69d2017-02-12 16:19:55 -0800128 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800129 // we should ever see.
Brian Silverman052e69d2017-02-12 16:19:55 -0800130 fast_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800131 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800132 kMaxFastEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800133 0.5));
Brian Silverman052e69d2017-02-12 16:19:55 -0800134 medium_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800135 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800136 kMaxMediumEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800137 0.5));
Brianef030df2017-03-05 15:06:04 -0800138 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800139 }
140
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800141 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800142 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800143 drivetrain_left_encoder_ = ::std::move(encoder);
144 }
145
146 void set_drivetrain_right_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_right_encoder_ = ::std::move(encoder);
149 }
150
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800151 void set_shooter_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 shooter_encoder_ = ::std::move(encoder);
154 }
155
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800156 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800157 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800158 intake_encoder_.set_encoder(::std::move(encoder));
159 }
160
161 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
162 intake_encoder_.set_potentiometer(::std::move(potentiometer));
163 }
164
Brian Silverman50826c02017-02-18 14:40:25 -0800165 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
166 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800167 }
168
Brian Silverman052e69d2017-02-12 16:19:55 -0800169 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
170 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800171 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800172 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800173 }
174
Brianef030df2017-03-05 15:06:04 -0800175 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
176 hall_filter_.Add(input.get());
177 indexer_counter_.set_input(input.get());
178 indexer_hall_ = ::std::move(input);
179 }
180
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800181 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800182 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800183 turret_counter_.set_encoder(encoder.get());
184 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800185 }
186
Brianef030df2017-03-05 15:06:04 -0800187 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
188 hall_filter_.Add(input.get());
189 turret_counter_.set_input(input.get());
190 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800191 }
192
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800193 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800194 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800195 hood_encoder_.set_encoder(::std::move(encoder));
196 }
197
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800198 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800199 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800200 hood_encoder_.set_index(::std::move(index));
201 }
202
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800203 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
204 autonomous_modes_.at(i) = ::std::move(sensor);
205 }
206
Austin Schuh8347cb62017-04-08 14:37:34 -0700207 void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) {
208 medium_encoder_filter_.Add(pwm_trigger.get());
209 pwm_trigger_ = ::std::move(pwm_trigger);
210 }
211
212 // All of the DMA-related set_* calls must be made before this, and it
213 // doesn't
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800214 // hurt to do all of them.
215 void set_dma(::std::unique_ptr<DMA> dma) {
216 dma_synchronizer_.reset(
217 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brianef030df2017-03-05 15:06:04 -0800218 dma_synchronizer_->Add(&indexer_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800219 dma_synchronizer_->Add(&hood_encoder_);
Brianef030df2017-03-05 15:06:04 -0800220 dma_synchronizer_->Add(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800221 }
222
Austin Schuh8347cb62017-04-08 14:37:34 -0700223 void RunPWMDetecter() {
224 ::aos::SetCurrentThreadRealtimePriority(41);
225
226 pwm_trigger_->RequestInterrupts();
227 // Rising edge only.
228 pwm_trigger_->SetUpSourceEdge(true, false);
229
230 monotonic_clock::time_point last_posedge_monotonic =
231 monotonic_clock::min_time;
232
233 while (run_) {
234 auto ret = pwm_trigger_->WaitForInterrupt(1.0, true);
235 if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) {
236 // Grab all the clocks.
237 const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp();
238
239 aos_compiler_memory_barrier();
240 const double fpga_time_before = GetFPGATime() * 1e-6;
241 aos_compiler_memory_barrier();
242 const monotonic_clock::time_point monotonic_now =
243 monotonic_clock::now();
244 aos_compiler_memory_barrier();
245 const double fpga_time_after = GetFPGATime() * 1e-6;
246 aos_compiler_memory_barrier();
247
248 const double fpga_offset =
249 (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time;
250
251 // Compute when the edge was.
252 const monotonic_clock::time_point monotonic_edge =
253 monotonic_now - chrono::duration_cast<chrono::nanoseconds>(
254 chrono::duration<double>(fpga_offset));
255
256 LOG(INFO, "Got PWM pulse %f spread, %f offset, %lld trigger\n",
257 fpga_time_after - fpga_time_before, fpga_offset,
258 monotonic_edge.time_since_epoch().count());
259
260 // Compute bounds on the timestep and sampling times.
261 const double fpga_sample_length = fpga_time_after - fpga_time_before;
262 const chrono::nanoseconds elapsed_time =
263 monotonic_edge - last_posedge_monotonic;
264
265 last_posedge_monotonic = monotonic_edge;
266
267 // Verify that the values are sane.
268 if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) {
269 continue;
270 }
271 if (fpga_offset < 0 || fpga_offset > 0.00015) {
272 continue;
273 }
274 if (elapsed_time >
275 chrono::microseconds(5050) + chrono::microseconds(4) ||
276 elapsed_time <
277 chrono::microseconds(5050) - chrono::microseconds(4)) {
278 continue;
279 }
280 // Good edge!
281 {
282 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
283 last_tick_time_monotonic_timepoint_ = last_posedge_monotonic;
284 last_period_ = elapsed_time;
285 }
286 } else {
287 LOG(INFO, "PWM triggered %d\n", ret);
288 }
289 }
290 pwm_trigger_->CancelInterrupts();
291 }
292
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800293 void operator()() {
294 ::aos::SetCurrentThreadName("SensorReader");
295
296 my_pid_ = getpid();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800297
298 dma_synchronizer_->Start();
299
Austin Schuh8347cb62017-04-08 14:37:34 -0700300 ::aos::time::PhasedLoop phased_loop(last_period_,
301 ::std::chrono::milliseconds(3));
302 chrono::nanoseconds filtered_period = last_period_;
303
304 ::std::thread pwm_detecter_thread(
305 ::std::bind(&SensorReader::RunPWMDetecter, this));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800306
307 ::aos::SetCurrentThreadRealtimePriority(40);
308 while (run_) {
309 {
310 const int iterations = phased_loop.SleepUntilNext();
311 if (iterations != 1) {
312 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
313 }
314 }
315 RunIteration();
Austin Schuh8347cb62017-04-08 14:37:34 -0700316
317 monotonic_clock::time_point last_tick_timepoint;
318 chrono::nanoseconds period;
319 {
320 ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_);
321 last_tick_timepoint = last_tick_time_monotonic_timepoint_;
322 period = last_period_;
323 }
324
325 if (last_tick_timepoint == monotonic_clock::min_time) {
326 continue;
327 }
328 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
329 period, last_tick_timepoint + chrono::microseconds(2050));
330
331 // TODO(austin): If this is the first edge in a while, skip to it (plus
332 // an offset). Otherwise, slowly drift time to line up.
333
334 phased_loop.set_interval_and_offset(period, new_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800335 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700336 pwm_detecter_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800337 }
338
339 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700340 ::frc971::wpilib::SendRobotState(my_pid_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800341
342 const auto values = constants::GetValues();
343
344 {
345 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
346 drivetrain_message->right_encoder =
347 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800348 drivetrain_message->right_speed =
349 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
350
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800351 drivetrain_message->left_encoder =
352 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
353 drivetrain_message->left_speed =
354 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800355
356 drivetrain_message.Send();
357 }
358
359 dma_synchronizer_->RunIteration();
360
361 {
362 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800363 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800364 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800365 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800366 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800367
Brianef030df2017-03-05 15:06:04 -0800368 CopyPosition(indexer_counter_, &superstructure_message->column.indexer,
369 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuh546a0382017-04-16 19:10:18 -0700370 Values::kIndexerEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800371
Brian Silverman50826c02017-02-18 14:40:25 -0800372 superstructure_message->theta_shooter =
373 encoder_translate(shooter_encoder_->GetRaw(),
374 Values::kShooterEncoderCountsPerRevolution,
375 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800376
Brian Silverman50826c02017-02-18 14:40:25 -0800377 CopyPosition(hood_encoder_, &superstructure_message->hood,
378 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800379 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800380
Brianef030df2017-03-05 15:06:04 -0800381 CopyPosition(turret_counter_, &superstructure_message->column.turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800382 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800383 Values::kTurretEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800384
385 superstructure_message.Send();
386 }
387
388 {
Philipp Schrader996a2a22017-02-22 05:02:48 +0000389 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800390 auto_mode_message->mode = 0;
391 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700392 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800393 auto_mode_message->mode |= 1 << i;
394 }
395 }
396 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
397 auto_mode_message.Send();
398 }
399 }
400
401 void Quit() { run_ = false; }
402
403 private:
Brian Silverman50826c02017-02-18 14:40:25 -0800404 double encoder_translate(int32_t value, double counts_per_revolution,
405 double ratio) {
406 return static_cast<double>(value) / counts_per_revolution * ratio *
407 (2.0 * M_PI);
408 }
409
Brian Silverman7cce2d32017-02-19 21:48:48 -0800410 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
411 ::frc971::IndexPosition *position,
Brian Silverman50826c02017-02-18 14:40:25 -0800412 double encoder_counts_per_revolution, double encoder_ratio,
Brian Silverman7cce2d32017-02-19 21:48:48 -0800413 bool reverse) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800414 const double multiplier = reverse ? -1.0 : 1.0;
415 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800416 multiplier * encoder_translate(encoder.polled_encoder_value(),
417 encoder_counts_per_revolution,
418 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800419 position->latched_encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800420 multiplier * encoder_translate(encoder.last_encoder_value(),
421 encoder_counts_per_revolution,
422 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800423 position->index_pulses = encoder.index_posedge_count();
424 }
425
Austin Schuh2a3e0632018-02-19 16:24:49 -0800426 void CopyPosition(
427 const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder,
428 ::frc971::PotAndAbsolutePosition *position,
429 double encoder_counts_per_revolution, double encoder_ratio,
430 ::std::function<double(double)> potentiometer_translate, bool reverse,
431 double pot_offset) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800432 const double multiplier = reverse ? -1.0 : 1.0;
433 position->pot = multiplier * potentiometer_translate(
Brian Silverman50826c02017-02-18 14:40:25 -0800434 encoder.ReadPotentiometerVoltage()) +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800435 pot_offset;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800436 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800437 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
438 encoder_counts_per_revolution,
439 encoder_ratio);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800440
441 position->absolute_encoder =
442 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
443 : encoder.ReadAbsoluteEncoder()) *
444 encoder_ratio * (2.0 * M_PI);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800445 }
446
Brianef030df2017-03-05 15:06:04 -0800447 void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter,
448 ::frc971::HallEffectAndPosition *position,
449 double encoder_counts_per_revolution, double encoder_ratio,
450 bool reverse) {
451 const double multiplier = reverse ? -1.0 : 1.0;
452 position->position =
453 multiplier * encoder_translate(counter.polled_encoder(),
454 encoder_counts_per_revolution,
455 encoder_ratio);
456 position->current = !counter.polled_value();
457 position->posedge_count = counter.negative_count();
458 position->negedge_count = counter.positive_count();
459 position->posedge_value =
460 multiplier * encoder_translate(counter.last_negative_encoder_value(),
461 encoder_counts_per_revolution,
462 encoder_ratio);
463 position->negedge_value =
464 multiplier * encoder_translate(counter.last_positive_encoder_value(),
465 encoder_counts_per_revolution,
466 encoder_ratio);
467 }
468
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800469 int32_t my_pid_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800470
Austin Schuh8347cb62017-04-08 14:37:34 -0700471 // Mutex to manage access to the period and tick time variables.
472 ::aos::stl_mutex tick_time_mutex_;
473 monotonic_clock::time_point last_tick_time_monotonic_timepoint_ =
474 monotonic_clock::min_time;
475 chrono::nanoseconds last_period_ = chrono::microseconds(5050);
476
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800477 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
478
Brian Silverman052e69d2017-02-12 16:19:55 -0800479 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
Brianef030df2017-03-05 15:06:04 -0800480 hall_filter_;
Brian Silverman052e69d2017-02-12 16:19:55 -0800481
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800482 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
483 drivetrain_right_encoder_;
484
Austin Schuh2a3e0632018-02-19 16:24:49 -0800485 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800486
Brian Silverman052e69d2017-02-12 16:19:55 -0800487 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800488 ::std::unique_ptr<DigitalInput> indexer_hall_;
489 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800490
Brianef030df2017-03-05 15:06:04 -0800491 ::std::unique_ptr<Encoder> turret_encoder_;
492 ::std::unique_ptr<DigitalInput> turret_hall_;
493 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
494
Brian Silverman7cce2d32017-02-19 21:48:48 -0800495 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800496 ::std::unique_ptr<Encoder> shooter_encoder_;
497
Austin Schuh8347cb62017-04-08 14:37:34 -0700498 ::std::unique_ptr<DigitalInput> pwm_trigger_;
499
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800500 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
501
502 ::std::atomic<bool> run_{true};
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800503};
504
Adam Snaidere0554ef2017-03-11 23:02:45 -0800505class SolenoidWriter {
506 public:
507 SolenoidWriter()
508 : superstructure_(".y2017.control_loops.superstructure_queue.output") {}
509
510 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
511
Austin Schuh8347cb62017-04-08 14:37:34 -0700512 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800513 lights_ = ::std::move(s);
514 }
515
Austin Schuh8347cb62017-04-08 14:37:34 -0700516 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700517 rgb_lights_ = ::std::move(s);
518 }
519
Adam Snaidere0554ef2017-03-11 23:02:45 -0800520 void operator()() {
521 ::aos::SetCurrentThreadName("Solenoids");
522 ::aos::SetCurrentThreadRealtimePriority(27);
523
524 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
525 ::std::chrono::milliseconds(1));
526
527 while (run_) {
528 {
529 const int iterations = phased_loop.SleepUntilNext();
530 if (iterations != 1) {
531 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
532 }
533 }
534
535 {
536 superstructure_.FetchLatest();
537 if (superstructure_.get()) {
538 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
539 lights_->Set(superstructure_->lights_on);
Austin Schuhc587c882017-03-29 21:33:10 -0700540 rgb_lights_->Set(superstructure_->red_light_on |
541 superstructure_->green_light_on |
542 superstructure_->blue_light_on);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800543 }
544 }
545
546 pcm_.Flush();
547 }
548 }
549
550 void Quit() { run_ = false; }
551
552 private:
553 ::frc971::wpilib::BufferedPcm pcm_;
554
Austin Schuhc587c882017-03-29 21:33:10 -0700555 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800556
Austin Schuh8347cb62017-04-08 14:37:34 -0700557 ::aos::Queue<::y2017::control_loops::SuperstructureQueue::Output>
Adam Snaidere0554ef2017-03-11 23:02:45 -0800558 superstructure_;
559
560 ::std::atomic<bool> run_{true};
561};
562
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800563class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
564 public:
Austin Schuh8347cb62017-04-08 14:37:34 -0700565 void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800566 drivetrain_left_victor_ = ::std::move(t);
567 }
568
Austin Schuh8347cb62017-04-08 14:37:34 -0700569 void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800570 drivetrain_right_victor_ = ::std::move(t);
571 }
572
573 private:
574 virtual void Read() override {
575 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
576 }
577
578 virtual void Write() override {
579 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
580 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh410e3812017-02-21 16:44:03 -0800581 drivetrain_left_victor_->SetSpeed(-queue->left_voltage / 12.0);
582 drivetrain_right_victor_->SetSpeed(queue->right_voltage / 12.0);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800583 }
584
585 virtual void Stop() override {
586 LOG(WARNING, "drivetrain output too old\n");
587 drivetrain_left_victor_->SetDisabled();
588 drivetrain_right_victor_->SetDisabled();
589 }
590
Austin Schuh8347cb62017-04-08 14:37:34 -0700591 ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_,
592 drivetrain_right_victor_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800593};
594
595class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
596 public:
Austin Schuh8347cb62017-04-08 14:37:34 -0700597 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800598 intake_victor_ = ::std::move(t);
599 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700600 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800601 intake_rollers_victor_ = ::std::move(t);
602 }
603
Austin Schuh8347cb62017-04-08 14:37:34 -0700604 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800605 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800606 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700607 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800608 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800609 }
610
Austin Schuh6a8131b2017-04-08 15:39:22 -0700611 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
612 gear_servo_ = ::std::move(t);
613 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700614 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800615 shooter_victor_ = ::std::move(t);
616 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700617 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800618 turret_victor_ = ::std::move(t);
619 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700620 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800621 hood_victor_ = ::std::move(t);
622 }
623
Austin Schuhc587c882017-03-29 21:33:10 -0700624 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
625 red_light_ = ::std::move(t);
626 }
627 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
628 green_light_ = ::std::move(t);
629 }
630 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
631 blue_light_ = ::std::move(t);
632 }
633
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800634 private:
635 virtual void Read() override {
636 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
637 }
638
639 virtual void Write() override {
640 auto &queue = ::y2017::control_loops::superstructure_queue.output;
641 LOG_STRUCT(DEBUG, "will output", *queue);
642 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
643 -kMaxBringupPower, kMaxBringupPower) /
644 12.0);
645 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800646 indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0);
647 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0);
Austin Schuh410e3812017-02-21 16:44:03 -0800648 turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800649 -kMaxBringupPower, kMaxBringupPower) /
650 12.0);
651 hood_victor_->SetSpeed(
652 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
653 12.0);
654 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700655
656 red_light_->Set(queue->red_light_on);
657 green_light_->Set(queue->green_light_on);
658 blue_light_->Set(queue->blue_light_on);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700659
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800660 gear_servo_->SetPosition(queue->gear_servo);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800661 }
662
663 virtual void Stop() override {
664 LOG(WARNING, "Superstructure output too old.\n");
665 intake_victor_->SetDisabled();
666 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800667 indexer_victor_->SetDisabled();
668 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800669 turret_victor_->SetDisabled();
670 hood_victor_->SetDisabled();
671 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700672
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800673 gear_servo_->SetRaw(0);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700674
Austin Schuhc587c882017-03-29 21:33:10 -0700675 red_light_->Set(true);
676 green_light_->Set(true);
677 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800678 }
679
Austin Schuh8347cb62017-04-08 14:37:34 -0700680 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
681 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
682 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700683
Austin Schuh6a8131b2017-04-08 15:39:22 -0700684 ::std::unique_ptr<::frc::Servo> gear_servo_;
685
Austin Schuhc587c882017-03-29 21:33:10 -0700686 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800687};
688
689class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
690 public:
691 ::std::unique_ptr<Encoder> make_encoder(int index) {
692 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
693 Encoder::k4X);
694 }
695
696 void Run() override {
697 ::aos::InitNRT();
698 ::aos::SetCurrentThreadName("StartCompetition");
699
700 ::frc971::wpilib::JoystickSender joystick_sender;
701 ::std::thread joystick_thread(::std::ref(joystick_sender));
702
703 ::frc971::wpilib::PDPFetcher pdp_fetcher;
704 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
705 SensorReader reader;
706
707 // TODO(campbell): Update port numbers
708 reader.set_drivetrain_left_encoder(make_encoder(0));
709 reader.set_drivetrain_right_encoder(make_encoder(1));
710
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800711 reader.set_intake_encoder(make_encoder(3));
Brian Silverman50826c02017-02-18 14:40:25 -0800712 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800713 reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800714
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800715 reader.set_indexer_encoder(make_encoder(5));
Brianef030df2017-03-05 15:06:04 -0800716 reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800717
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800718 reader.set_turret_encoder(make_encoder(6));
Brianef030df2017-03-05 15:06:04 -0800719 reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800720
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800721 reader.set_hood_encoder(make_encoder(4));
722 reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800723
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800724 reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800725
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800726 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
727 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Austin Schuh8347cb62017-04-08 14:37:34 -0700728
729 reader.set_pwm_trigger(make_unique<DigitalInput>(7));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800730
731 reader.set_dma(make_unique<DMA>());
732 ::std::thread reader_thread(::std::ref(reader));
733
Brian Silvermanb4439852017-02-24 19:49:09 -0800734 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800735 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700736 imu.SetDummySPI(SPI::Port::kOnboardCS2);
737 auto imu_reset = make_unique<DigitalOutput>(6);
738 imu.set_reset(imu_reset.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800739 ::std::thread imu_thread(::std::ref(imu));
740
741 DrivetrainWriter drivetrain_writer;
742 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700743 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800744 drivetrain_writer.set_drivetrain_right_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700745 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800746 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
747
748 SuperstructureWriter superstructure_writer;
749 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700750 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800751 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700752 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800753 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700754 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800755 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700756 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800757 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700758 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800759 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700760 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800761 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700762 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700763
Austin Schuh6a8131b2017-04-08 15:39:22 -0700764 superstructure_writer.set_gear_servo(
765 ::std::unique_ptr<Servo>(new Servo(0)));
766
Austin Schuhc587c882017-03-29 21:33:10 -0700767 superstructure_writer.set_red_light(
768 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
769 superstructure_writer.set_green_light(
770 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
771 superstructure_writer.set_blue_light(
772 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
773
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800774 ::std::thread superstructure_writer_thread(
775 ::std::ref(superstructure_writer));
776
Adam Snaidere0554ef2017-03-11 23:02:45 -0800777 SolenoidWriter solenoid_writer;
778 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700779 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800780
781 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
782
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800783 // Wait forever. Not much else to do...
784 while (true) {
785 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
786 if (r != 0) {
787 PLOG(WARNING, "infinite select failed");
788 } else {
789 PLOG(WARNING, "infinite select succeeded??\n");
790 }
791 }
792
793 LOG(ERROR, "Exiting WPILibRobot\n");
794
795 joystick_sender.Quit();
796 joystick_thread.join();
797 pdp_fetcher.Quit();
798 pdp_fetcher_thread.join();
799 reader.Quit();
800 reader_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800801 imu.Quit();
802 imu_thread.join();
803
804 drivetrain_writer.Quit();
805 drivetrain_writer_thread.join();
806 superstructure_writer.Quit();
807 superstructure_writer_thread.join();
808
809 ::aos::Cleanup();
810 }
811};
812
Brian Silverman052e69d2017-02-12 16:19:55 -0800813} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800814} // namespace wpilib
815} // namespace y2017
816
817AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);