blob: 93f2ef4e56f8fa71d38956527a3404e198f15d83 [file] [log] [blame]
Campbell Crowleyae6e8422017-02-05 12:38:50 -08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
6#include <chrono>
7#include <thread>
8#include <mutex>
9#include <functional>
10#include <array>
Brian Silverman50826c02017-02-18 14:40:25 -080011#include <cmath>
Campbell Crowleyae6e8422017-02-05 12:38:50 -080012
Brian Silverman50826c02017-02-18 14:40:25 -080013#include "Counter.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080014#include "Encoder.h"
15#include "VictorSP.h"
16#include "Relay.h"
17#include "DriverStation.h"
18#include "AnalogInput.h"
19#include "Compressor.h"
20#include "DigitalGlitchFilter.h"
21#undef ERROR
22
23#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
25#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
29#include "aos/common/stl_mutex.h"
30#include "aos/linux_code/init.h"
31#include "aos/common/messages/robot_state.q.h"
32#include "aos/common/commonmath.h"
33
34#include "frc971/control_loops/control_loops.q.h"
35#include "frc971/control_loops/drivetrain/drivetrain.q.h"
36#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080037#include "y2017/control_loops/superstructure/superstructure.q.h"
38#include "y2017/actors/autonomous_action.q.h"
39
40#include "frc971/wpilib/wpilib_robot_base.h"
41#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
45#include "frc971/wpilib/gyro_sender.h"
46#include "frc971/wpilib/dma_edge_counting.h"
47#include "frc971/wpilib/interrupt_edge_counting.h"
48#include "frc971/wpilib/encoder_and_potentiometer.h"
49#include "frc971/wpilib/logging.q.h"
50#include "frc971/wpilib/wpilib_interface.h"
51#include "frc971/wpilib/pdp_fetcher.h"
52#include "frc971/wpilib/ADIS16448.h"
53#include "frc971/wpilib/dma.h"
54
55#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;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080062
63namespace y2017 {
64namespace wpilib {
65namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080066
Campbell Crowleyae6e8422017-02-05 12:38:50 -080067constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080068
69// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
70// DMA stuff and then removing the * 2.0 in *_translate.
71// The low bit is direction.
72
73// TODO(brian): Replace this with ::std::make_unique once all our toolchains
74// have support.
75template <class T, class... U>
76std::unique_ptr<T> make_unique(U &&... u) {
77 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
78}
79
Brian Silverman052e69d2017-02-12 16:19:55 -080080// TODO(brian): Use ::std::max instead once we have C++14 so that can be
81// constexpr.
82template <typename T>
83constexpr T max(T a, T b) {
84 return (a > b) ? a : b;
85}
86template <typename T, typename... Rest>
87constexpr T max(T a, T b, T c, Rest... rest) {
88 return max(max(a, b), c, rest...);
89}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080090
Campbell Crowleyae6e8422017-02-05 12:38:50 -080091double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080092 return static_cast<double>(in) /
93 Values::kDrivetrainEncoderCountsPerRevolution *
94 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080095}
96
97double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080098 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
99 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800100}
101
Brian Silverman50826c02017-02-18 14:40:25 -0800102// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800103double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800104 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800105 (2 * M_PI /*radians*/);
106}
107
Brian Silverman052e69d2017-02-12 16:19:55 -0800108double turret_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800109 return voltage * Values::kTurretPotRatio * (10.0 /*turns*/ / 5.0 /*volts*/) *
Brian Silverman052e69d2017-02-12 16:19:55 -0800110 (2 * M_PI /*radians*/);
111}
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800112
Brian Silverman052e69d2017-02-12 16:19:55 -0800113constexpr double kMaxFastEncoderPulsesPerSecond =
114 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
115 Values::kMaxShooterEncoderPulsesPerSecond);
116static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
117 "fast encoders are too fast");
118constexpr double kMaxMediumEncoderPulsesPerSecond =
119 max(Values::kMaxIntakeEncoderPulsesPerSecond,
120 Values::kMaxTurretEncoderPulsesPerSecond,
121 Values::kMaxIndexerEncoderPulsesPerSecond);
122static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
123 "medium encoders are too fast");
124constexpr double kMaxSlowEncoderPulsesPerSecond =
125 Values::kMaxHoodEncoderPulsesPerSecond;
126static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
127 "slow encoders are too fast");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800128
Brian Silverman50826c02017-02-18 14:40:25 -0800129// Handles reading the duty cycle on a DigitalInput.
130class DutyCycleReader {
131 public:
132 void set_input(::std::unique_ptr<DigitalInput> input) {
133 high_counter_.reset(new Counter(input.get()));
134 high_counter_->SetMaxPeriod(kMaxPeriod);
135 high_counter_->SetSemiPeriodMode(true);
136
137 period_length_counter_.reset(new Counter(input.get()));
138 period_length_counter_->SetMaxPeriod(kMaxPeriod);
139 period_length_counter_->SetUpSourceEdge(true, false);
140
141 input_ = ::std::move(input);
142 }
143
144 double Read() const {
145 const double high_time = high_counter_->GetPeriod();
146 const double period_length = period_length_counter_->GetPeriod();
147 if (!::std::isfinite(high_time) || !::std::isfinite(period_length)) {
148 return ::std::numeric_limits<double>::quiet_NaN();
149 }
150 return high_time / period_length;
151 }
152
153 private:
154 static constexpr ::std::chrono::nanoseconds kNominalPeriod =
155 ::std::chrono::microseconds(4096);
156 static constexpr double kMaxPeriod =
157 (::std::chrono::duration_cast<::std::chrono::seconds>(kNominalPeriod) * 2)
158 .count();
159
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));
Brian Silverman052e69d2017-02-12 16:19:55 -0800206 slow_encoder_filter_.SetPeriodNanoSeconds(
207 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
208 kMaxSlowEncoderPulsesPerSecond * 1e9 +
209 0.5));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800210 }
211
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800212 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800213 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800214 drivetrain_left_encoder_ = ::std::move(encoder);
215 }
216
217 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800218 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800219 drivetrain_right_encoder_ = ::std::move(encoder);
220 }
221
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800222 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800223 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800224 shooter_encoder_ = ::std::move(encoder);
225 }
226
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800227 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800228 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800229 intake_encoder_.set_encoder(::std::move(encoder));
230 }
231
232 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
233 intake_encoder_.set_potentiometer(::std::move(potentiometer));
234 }
235
Brian Silverman50826c02017-02-18 14:40:25 -0800236 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
237 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800238 }
239
Brian Silverman052e69d2017-02-12 16:19:55 -0800240 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
241 medium_encoder_filter_.Add(encoder.get());
242 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800243 }
244
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800245 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800246 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800247 turret_encoder_.set_encoder(::std::move(encoder));
248 }
249
250 void set_turret_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
251 turret_encoder_.set_potentiometer(::std::move(potentiometer));
252 }
253
Brian Silverman50826c02017-02-18 14:40:25 -0800254 void set_turret_absolute(::std::unique_ptr<DigitalInput> input) {
255 turret_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800256 }
257
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800258 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800259 slow_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800260 hood_encoder_.set_encoder(::std::move(encoder));
261 }
262
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800263 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800264 slow_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800265 hood_encoder_.set_index(::std::move(index));
266 }
267
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800268 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
269 autonomous_modes_.at(i) = ::std::move(sensor);
270 }
271
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800272 // All of the DMA-related set_* calls must be made before this, and it doesn't
273 // hurt to do all of them.
274 void set_dma(::std::unique_ptr<DMA> dma) {
275 dma_synchronizer_.reset(
276 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800277 dma_synchronizer_->Add(&hood_encoder_);
278 }
279
280 void operator()() {
281 ::aos::SetCurrentThreadName("SensorReader");
282
283 my_pid_ = getpid();
284 ds_ =
285 &DriverStation::GetInstance();
286
287 dma_synchronizer_->Start();
288
289 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
290 ::std::chrono::milliseconds(4));
291
292 ::aos::SetCurrentThreadRealtimePriority(40);
293 while (run_) {
294 {
295 const int iterations = phased_loop.SleepUntilNext();
296 if (iterations != 1) {
297 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
298 }
299 }
300 RunIteration();
301 }
302 }
303
304 void RunIteration() {
305 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
306
307 const auto values = constants::GetValues();
308
309 {
310 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
311 drivetrain_message->right_encoder =
312 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
313 drivetrain_message->left_encoder =
314 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
315 drivetrain_message->left_speed =
316 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
317 drivetrain_message->right_speed =
318 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
319
320 drivetrain_message.Send();
321 }
322
323 dma_synchronizer_->RunIteration();
324
325 {
326 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800327 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800328 Values::kIntakeEncoderCountsPerRevolution,
329 Values::kIntakeEncoderRatio, intake_pot_translate, false,
Brian Silverman052e69d2017-02-12 16:19:55 -0800330 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800331
Brian Silverman052e69d2017-02-12 16:19:55 -0800332 superstructure_message->theta_indexer =
Brian Silverman50826c02017-02-18 14:40:25 -0800333 encoder_translate(indexer_encoder_->GetRaw(),
334 Values::kMaxIndexerEncoderCountsPerRevolution,
335 Values::kIndexerEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800336
Brian Silverman50826c02017-02-18 14:40:25 -0800337 superstructure_message->theta_shooter =
338 encoder_translate(shooter_encoder_->GetRaw(),
339 Values::kShooterEncoderCountsPerRevolution,
340 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800341
Brian Silverman50826c02017-02-18 14:40:25 -0800342 CopyPosition(hood_encoder_, &superstructure_message->hood,
343 Values::kHoodEncoderCountsPerRevolution,
Brian Silverman7cce2d32017-02-19 21:48:48 -0800344 Values::kHoodEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800345
Brian Silverman052e69d2017-02-12 16:19:55 -0800346 CopyPosition(turret_encoder_, &superstructure_message->turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800347 Values::kTurretEncoderCountsPerRevolution,
348 Values::kTurretEncoderRatio, turret_pot_translate, false,
Brian Silverman052e69d2017-02-12 16:19:55 -0800349 values.turret.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800350
351 superstructure_message.Send();
352 }
353
354 {
355 auto auto_mode_message = ::y2017::actors::auto_mode.MakeMessage();
356 auto_mode_message->mode = 0;
357 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
358 if (autonomous_modes_[i]->Get()) {
359 auto_mode_message->mode |= 1 << i;
360 }
361 }
362 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
363 auto_mode_message.Send();
364 }
365 }
366
367 void Quit() { run_ = false; }
368
369 private:
Brian Silverman50826c02017-02-18 14:40:25 -0800370 double encoder_translate(int32_t value, double counts_per_revolution,
371 double ratio) {
372 return static_cast<double>(value) / counts_per_revolution * ratio *
373 (2.0 * M_PI);
374 }
375
Brian Silverman7cce2d32017-02-19 21:48:48 -0800376 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
377 ::frc971::IndexPosition *position,
Brian Silverman50826c02017-02-18 14:40:25 -0800378 double encoder_counts_per_revolution, double encoder_ratio,
Brian Silverman7cce2d32017-02-19 21:48:48 -0800379 bool reverse) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800380 const double multiplier = reverse ? -1.0 : 1.0;
381 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800382 multiplier * encoder_translate(encoder.polled_encoder_value(),
383 encoder_counts_per_revolution,
384 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800385 position->latched_encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800386 multiplier * encoder_translate(encoder.last_encoder_value(),
387 encoder_counts_per_revolution,
388 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800389 position->index_pulses = encoder.index_posedge_count();
390 }
391
Brian Silverman50826c02017-02-18 14:40:25 -0800392 void CopyPosition(const AbsoluteEncoderAndPotentiometer &encoder,
393 ::frc971::PotAndAbsolutePosition *position,
394 double encoder_counts_per_revolution, double encoder_ratio,
395 ::std::function<double(double)> potentiometer_translate,
396 bool reverse, double pot_offset) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800397 const double multiplier = reverse ? -1.0 : 1.0;
398 position->pot = multiplier * potentiometer_translate(
Brian Silverman50826c02017-02-18 14:40:25 -0800399 encoder.ReadPotentiometerVoltage()) +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800400 pot_offset;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800401 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800402 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
403 encoder_counts_per_revolution,
404 encoder_ratio);
405 position->absolute_encoder = multiplier * encoder.ReadAbsoluteEncoder() *
406 encoder_ratio * (2.0 * M_PI);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800407 }
408
409 int32_t my_pid_;
410 DriverStation *ds_;
411
412 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
413
Brian Silverman052e69d2017-02-12 16:19:55 -0800414 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
415 slow_encoder_filter_;
416
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800417 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
418 drivetrain_right_encoder_;
419
Brian Silverman50826c02017-02-18 14:40:25 -0800420 AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800421
Brian Silverman052e69d2017-02-12 16:19:55 -0800422 ::std::unique_ptr<Encoder> indexer_encoder_;
423 ::std::unique_ptr<AnalogInput> indexer_hall_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800424
Brian Silverman50826c02017-02-18 14:40:25 -0800425 AbsoluteEncoderAndPotentiometer turret_encoder_;
Brian Silverman7cce2d32017-02-19 21:48:48 -0800426 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800427 ::std::unique_ptr<Encoder> shooter_encoder_;
428
429 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
430
431 ::std::atomic<bool> run_{true};
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800432};
433
434class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
435 public:
436 void set_drivetrain_left_victor(::std::unique_ptr<VictorSP> t) {
437 drivetrain_left_victor_ = ::std::move(t);
438 }
439
440 void set_drivetrain_right_victor(::std::unique_ptr<VictorSP> t) {
441 drivetrain_right_victor_ = ::std::move(t);
442 }
443
444 private:
445 virtual void Read() override {
446 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
447 }
448
449 virtual void Write() override {
450 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
451 LOG_STRUCT(DEBUG, "will output", *queue);
452 drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0);
453 drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0);
454 }
455
456 virtual void Stop() override {
457 LOG(WARNING, "drivetrain output too old\n");
458 drivetrain_left_victor_->SetDisabled();
459 drivetrain_right_victor_->SetDisabled();
460 }
461
462 ::std::unique_ptr<VictorSP> drivetrain_left_victor_, drivetrain_right_victor_;
463};
464
465class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
466 public:
467 void set_intake_victor(::std::unique_ptr<VictorSP> t) {
468 intake_victor_ = ::std::move(t);
469 }
470 void set_intake_rollers_victor(::std::unique_ptr<VictorSP> t) {
471 intake_rollers_victor_ = ::std::move(t);
472 }
473
Brian Silverman052e69d2017-02-12 16:19:55 -0800474 void set_indexer_victor(::std::unique_ptr<VictorSP> t) {
475 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800476 }
Brian Silverman052e69d2017-02-12 16:19:55 -0800477 void set_indexer_roller_victor(::std::unique_ptr<VictorSP> t) {
478 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800479 }
480
481 void set_shooter_victor(::std::unique_ptr<VictorSP> t) {
482 shooter_victor_ = ::std::move(t);
483 }
484 void set_turret_victor(::std::unique_ptr<VictorSP> t) {
485 turret_victor_ = ::std::move(t);
486 }
487 void set_hood_victor(::std::unique_ptr<VictorSP> t) {
488 hood_victor_ = ::std::move(t);
489 }
490
491 private:
492 virtual void Read() override {
493 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
494 }
495
496 virtual void Write() override {
497 auto &queue = ::y2017::control_loops::superstructure_queue.output;
498 LOG_STRUCT(DEBUG, "will output", *queue);
499 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
500 -kMaxBringupPower, kMaxBringupPower) /
501 12.0);
502 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Brian Silverman052e69d2017-02-12 16:19:55 -0800503 indexer_victor_->SetSpeed(queue->voltage_indexer / 12.0);
504 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers /
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800505 12.0);
506 turret_victor_->SetSpeed(::aos::Clip(queue->voltage_turret,
507 -kMaxBringupPower, kMaxBringupPower) /
508 12.0);
509 hood_victor_->SetSpeed(
510 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
511 12.0);
512 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
513 }
514
515 virtual void Stop() override {
516 LOG(WARNING, "Superstructure output too old.\n");
517 intake_victor_->SetDisabled();
518 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800519 indexer_victor_->SetDisabled();
520 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800521 turret_victor_->SetDisabled();
522 hood_victor_->SetDisabled();
523 shooter_victor_->SetDisabled();
524 }
525
526 ::std::unique_ptr<VictorSP> intake_victor_, intake_rollers_victor_,
Brian Silverman052e69d2017-02-12 16:19:55 -0800527 indexer_victor_, indexer_roller_victor_, shooter_victor_,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800528 turret_victor_, hood_victor_;
529};
530
531class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
532 public:
533 ::std::unique_ptr<Encoder> make_encoder(int index) {
534 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
535 Encoder::k4X);
536 }
537
538 void Run() override {
539 ::aos::InitNRT();
540 ::aos::SetCurrentThreadName("StartCompetition");
541
542 ::frc971::wpilib::JoystickSender joystick_sender;
543 ::std::thread joystick_thread(::std::ref(joystick_sender));
544
545 ::frc971::wpilib::PDPFetcher pdp_fetcher;
546 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
547 SensorReader reader;
548
549 // TODO(campbell): Update port numbers
550 reader.set_drivetrain_left_encoder(make_encoder(0));
551 reader.set_drivetrain_right_encoder(make_encoder(1));
552
553 reader.set_intake_encoder(make_encoder(2));
Brian Silverman50826c02017-02-18 14:40:25 -0800554 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800555 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
556
Brian Silverman052e69d2017-02-12 16:19:55 -0800557 reader.set_indexer_encoder(make_encoder(3));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800558
559 reader.set_turret_encoder(make_encoder(5));
Brian Silverman50826c02017-02-18 14:40:25 -0800560 reader.set_turret_absolute(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800561 reader.set_turret_potentiometer(make_unique<AnalogInput>(3));
562
563 reader.set_hood_encoder(make_encoder(6));
564 reader.set_hood_index(make_unique<DigitalInput>(2));
565
566 reader.set_shooter_encoder(make_encoder(7));
567
568 reader.set_autonomous_mode(0, make_unique<DigitalInput>(6));
569 reader.set_autonomous_mode(1, make_unique<DigitalInput>(5));
570 reader.set_autonomous_mode(2, make_unique<DigitalInput>(4));
571 reader.set_autonomous_mode(3, make_unique<DigitalInput>(3));
572
573 reader.set_dma(make_unique<DMA>());
574 ::std::thread reader_thread(::std::ref(reader));
575
576 ::frc971::wpilib::GyroSender gyro_sender;
577 ::std::thread gyro_thread(::std::ref(gyro_sender));
578
579 auto imu_trigger = make_unique<DigitalInput>(5);
580 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get());
581 ::std::thread imu_thread(::std::ref(imu));
582
583 DrivetrainWriter drivetrain_writer;
584 drivetrain_writer.set_drivetrain_left_victor(
585 ::std::unique_ptr<VictorSP>(new VictorSP(0)));
586 drivetrain_writer.set_drivetrain_right_victor(
587 ::std::unique_ptr<VictorSP>(new VictorSP(1)));
588 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
589
590 SuperstructureWriter superstructure_writer;
591 superstructure_writer.set_intake_victor(
592 ::std::unique_ptr<VictorSP>(new VictorSP(2)));
593 superstructure_writer.set_intake_rollers_victor(
594 ::std::unique_ptr<VictorSP>(new VictorSP(3)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800595 superstructure_writer.set_indexer_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800596 ::std::unique_ptr<VictorSP>(new VictorSP(4)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800597 superstructure_writer.set_indexer_roller_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800598 ::std::unique_ptr<VictorSP>(new VictorSP(5)));
599 superstructure_writer.set_turret_victor(
600 ::std::unique_ptr<VictorSP>(new VictorSP(6)));
601 superstructure_writer.set_hood_victor(
602 ::std::unique_ptr<VictorSP>(new VictorSP(7)));
603 superstructure_writer.set_shooter_victor(
604 ::std::unique_ptr<VictorSP>(new VictorSP(8)));
605 ::std::thread superstructure_writer_thread(
606 ::std::ref(superstructure_writer));
607
608 // Wait forever. Not much else to do...
609 while (true) {
610 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
611 if (r != 0) {
612 PLOG(WARNING, "infinite select failed");
613 } else {
614 PLOG(WARNING, "infinite select succeeded??\n");
615 }
616 }
617
618 LOG(ERROR, "Exiting WPILibRobot\n");
619
620 joystick_sender.Quit();
621 joystick_thread.join();
622 pdp_fetcher.Quit();
623 pdp_fetcher_thread.join();
624 reader.Quit();
625 reader_thread.join();
626 gyro_sender.Quit();
627 gyro_thread.join();
628 imu.Quit();
629 imu_thread.join();
630
631 drivetrain_writer.Quit();
632 drivetrain_writer_thread.join();
633 superstructure_writer.Quit();
634 superstructure_writer_thread.join();
635
636 ::aos::Cleanup();
637 }
638};
639
Brian Silverman052e69d2017-02-12 16:19:55 -0800640} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800641} // namespace wpilib
642} // namespace y2017
643
644AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);