blob: e8bd214520e257c9cc1d7ea80a109a96b8f5e7ac [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>
11
12#include "Encoder.h"
13#include "VictorSP.h"
14#include "Relay.h"
15#include "DriverStation.h"
16#include "AnalogInput.h"
17#include "Compressor.h"
18#include "DigitalGlitchFilter.h"
19#undef ERROR
20
21#include "aos/common/logging/logging.h"
22#include "aos/common/logging/queue_logging.h"
23#include "aos/common/time.h"
24#include "aos/common/util/log_interval.h"
25#include "aos/common/util/phased_loop.h"
26#include "aos/common/util/wrapping_counter.h"
27#include "aos/common/stl_mutex.h"
28#include "aos/linux_code/init.h"
29#include "aos/common/messages/robot_state.q.h"
30#include "aos/common/commonmath.h"
31
32#include "frc971/control_loops/control_loops.q.h"
33#include "frc971/control_loops/drivetrain/drivetrain.q.h"
34#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080035#include "y2017/control_loops/superstructure/superstructure.q.h"
36#include "y2017/actors/autonomous_action.q.h"
37
38#include "frc971/wpilib/wpilib_robot_base.h"
39#include "frc971/wpilib/joystick_sender.h"
40#include "frc971/wpilib/loop_output_handler.h"
41#include "frc971/wpilib/buffered_solenoid.h"
42#include "frc971/wpilib/buffered_pcm.h"
43#include "frc971/wpilib/gyro_sender.h"
44#include "frc971/wpilib/dma_edge_counting.h"
45#include "frc971/wpilib/interrupt_edge_counting.h"
46#include "frc971/wpilib/encoder_and_potentiometer.h"
47#include "frc971/wpilib/logging.q.h"
48#include "frc971/wpilib/wpilib_interface.h"
49#include "frc971/wpilib/pdp_fetcher.h"
50#include "frc971/wpilib/ADIS16448.h"
51#include "frc971/wpilib/dma.h"
52
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::frc971::control_loops::drivetrain_queue;
58using ::y2017::control_loops::superstructure_queue;
Brian Silverman052e69d2017-02-12 16:19:55 -080059using ::y2017::constants::Values;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080060
61namespace y2017 {
62namespace wpilib {
63namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080064
Campbell Crowleyae6e8422017-02-05 12:38:50 -080065constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080066
67// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
68// DMA stuff and then removing the * 2.0 in *_translate.
69// The low bit is direction.
70
71// TODO(brian): Replace this with ::std::make_unique once all our toolchains
72// have support.
73template <class T, class... U>
74std::unique_ptr<T> make_unique(U &&... u) {
75 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
76}
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
100double shooter_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800101 return static_cast<double>(in) / Values::kShooterEncoderCountsPerRevolution *
102 Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800103}
104
105double intake_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800106 return static_cast<double>(in) / Values::kIntakeEncoderCountsPerRevolution *
107 Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800108}
109
Brian Silverman052e69d2017-02-12 16:19:55 -0800110// TODO(constants): Update the number of turns.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800111double intake_pot_translate(double voltage) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800112 return voltage * Values::kIntakePotRatio * (10.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800113 (2 * M_PI /*radians*/);
114}
115
116double hood_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800117 return static_cast<double>(in) / Values::kHoodEncoderCountsPerRevolution *
118 Values::kHoodEncoderRatio * (2 * M_PI /*radians*/);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800119}
120
Brian Silverman052e69d2017-02-12 16:19:55 -0800121// TODO(constants): Update the number of turns.
122double hood_pot_translate(double voltage) {
123 return voltage * Values::kHoodPotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
124 (2 * M_PI /*radians*/);
125}
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800126
Brian Silverman052e69d2017-02-12 16:19:55 -0800127double turret_translate(int32_t in) {
128 return static_cast<double>(in) / Values::kTurretEncoderCountsPerRevolution *
129 Values::kTurretEncoderRatio * (2 * M_PI /*radians*/);
130}
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800131
Brian Silverman052e69d2017-02-12 16:19:55 -0800132// TODO(constants): Update the number of turns.
133double turret_pot_translate(double voltage) {
134 return voltage * Values::kTurretPotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
135 (2 * M_PI /*radians*/);
136}
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800137
Brian Silverman052e69d2017-02-12 16:19:55 -0800138double indexer_translate(int32_t in) {
139 return static_cast<double>(in) /
140 Values::kMaxIndexerEncoderCountsPerRevolution *
141 Values::kIndexerEncoderRatio * (2 * M_PI /*radians*/);
142}
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800143
Brian Silverman052e69d2017-02-12 16:19:55 -0800144constexpr double kMaxFastEncoderPulsesPerSecond =
145 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
146 Values::kMaxShooterEncoderPulsesPerSecond);
147static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
148 "fast encoders are too fast");
149constexpr double kMaxMediumEncoderPulsesPerSecond =
150 max(Values::kMaxIntakeEncoderPulsesPerSecond,
151 Values::kMaxTurretEncoderPulsesPerSecond,
152 Values::kMaxIndexerEncoderPulsesPerSecond);
153static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
154 "medium encoders are too fast");
155constexpr double kMaxSlowEncoderPulsesPerSecond =
156 Values::kMaxHoodEncoderPulsesPerSecond;
157static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
158 "slow encoders are too fast");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800159
160// Class to send position messages with sensor readings to our loops.
161class SensorReader {
162 public:
163 SensorReader() {
Brian Silverman052e69d2017-02-12 16:19:55 -0800164 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800165 // we should ever see.
Brian Silverman052e69d2017-02-12 16:19:55 -0800166 fast_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800167 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800168 kMaxFastEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800169 0.5));
Brian Silverman052e69d2017-02-12 16:19:55 -0800170 medium_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800171 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800172 kMaxMediumEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800173 0.5));
Brian Silverman052e69d2017-02-12 16:19:55 -0800174 slow_encoder_filter_.SetPeriodNanoSeconds(
175 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
176 kMaxSlowEncoderPulsesPerSecond * 1e9 +
177 0.5));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800178 }
179
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800180 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800181 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800182 drivetrain_left_encoder_ = ::std::move(encoder);
183 }
184
185 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800186 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800187 drivetrain_right_encoder_ = ::std::move(encoder);
188 }
189
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800190 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800191 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800192 shooter_encoder_ = ::std::move(encoder);
193 }
194
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800195 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800196 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800197 intake_encoder_.set_encoder(::std::move(encoder));
198 }
199
200 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
201 intake_encoder_.set_potentiometer(::std::move(potentiometer));
202 }
203
Brian Silverman052e69d2017-02-12 16:19:55 -0800204 // TODO(brian): This is wrong.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800205 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800206 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800207 intake_encoder_.set_index(::std::move(index));
208 }
209
Brian Silverman052e69d2017-02-12 16:19:55 -0800210 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
211 medium_encoder_filter_.Add(encoder.get());
212 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800213 }
214
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800215 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800216 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800217 turret_encoder_.set_encoder(::std::move(encoder));
218 }
219
220 void set_turret_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
221 turret_encoder_.set_potentiometer(::std::move(potentiometer));
222 }
223
Brian Silverman052e69d2017-02-12 16:19:55 -0800224 // TODO(brian): This is wrong.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800225 void set_turret_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800226 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800227 turret_encoder_.set_index(::std::move(index));
228 }
229
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800230 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800231 slow_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800232 hood_encoder_.set_encoder(::std::move(encoder));
233 }
234
235 void set_hood_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
236 hood_encoder_.set_potentiometer(::std::move(potentiometer));
237 }
238
239 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800240 slow_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800241 hood_encoder_.set_index(::std::move(index));
242 }
243
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800244 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
245 autonomous_modes_.at(i) = ::std::move(sensor);
246 }
247
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800248 // All of the DMA-related set_* calls must be made before this, and it doesn't
249 // hurt to do all of them.
250 void set_dma(::std::unique_ptr<DMA> dma) {
251 dma_synchronizer_.reset(
252 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800253 // TODO(constants): Update this.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800254 dma_synchronizer_->Add(&intake_encoder_);
255 dma_synchronizer_->Add(&turret_encoder_);
256 dma_synchronizer_->Add(&hood_encoder_);
257 }
258
259 void operator()() {
260 ::aos::SetCurrentThreadName("SensorReader");
261
262 my_pid_ = getpid();
263 ds_ =
264 &DriverStation::GetInstance();
265
266 dma_synchronizer_->Start();
267
268 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
269 ::std::chrono::milliseconds(4));
270
271 ::aos::SetCurrentThreadRealtimePriority(40);
272 while (run_) {
273 {
274 const int iterations = phased_loop.SleepUntilNext();
275 if (iterations != 1) {
276 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
277 }
278 }
279 RunIteration();
280 }
281 }
282
283 void RunIteration() {
284 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
285
286 const auto values = constants::GetValues();
287
288 {
289 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
290 drivetrain_message->right_encoder =
291 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
292 drivetrain_message->left_encoder =
293 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
294 drivetrain_message->left_speed =
295 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
296 drivetrain_message->right_speed =
297 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
298
299 drivetrain_message.Send();
300 }
301
302 dma_synchronizer_->RunIteration();
303
304 {
305 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800306 CopyPosition(intake_encoder_, &superstructure_message->intake,
307 intake_translate, intake_pot_translate, false,
308 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800309
Brian Silverman052e69d2017-02-12 16:19:55 -0800310 superstructure_message->theta_indexer =
311 indexer_translate(indexer_encoder_->GetRaw());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800312
313 superstructure_message->theta_shooter=
314 shooter_translate(shooter_encoder_->GetRaw());
315
Brian Silverman052e69d2017-02-12 16:19:55 -0800316 CopyPosition(hood_encoder_, &superstructure_message->hood, hood_translate,
317 hood_pot_translate, false, values.hood.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800318
Brian Silverman052e69d2017-02-12 16:19:55 -0800319 CopyPosition(turret_encoder_, &superstructure_message->turret,
320 turret_translate, turret_pot_translate, false,
321 values.turret.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800322
323 superstructure_message.Send();
324 }
325
326 {
327 auto auto_mode_message = ::y2017::actors::auto_mode.MakeMessage();
328 auto_mode_message->mode = 0;
329 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
330 if (autonomous_modes_[i]->Get()) {
331 auto_mode_message->mode |= 1 << i;
332 }
333 }
334 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
335 auto_mode_message.Send();
336 }
337 }
338
339 void Quit() { run_ = false; }
340
341 private:
Brian Silverman052e69d2017-02-12 16:19:55 -0800342 void CopyPosition(const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
343 ::frc971::PotAndIndexPosition *position,
344 ::std::function<double(int32_t)> encoder_translate,
345 ::std::function<double(double)> potentiometer_translate,
346 bool reverse, double pot_offset) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800347 const double multiplier = reverse ? -1.0 : 1.0;
348 position->encoder =
349 multiplier * encoder_translate(encoder.polled_encoder_value());
350 position->pot = multiplier * potentiometer_translate(
351 encoder.polled_potentiometer_voltage()) +
352 pot_offset;
353 position->latched_encoder =
354 multiplier * encoder_translate(encoder.last_encoder_value());
355 position->latched_pot =
356 multiplier *
357 potentiometer_translate(encoder.last_potentiometer_voltage()) +
358 pot_offset;
359 position->index_pulses = encoder.index_posedge_count();
360 }
361
Brian Silverman052e69d2017-02-12 16:19:55 -0800362#if 0
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800363 // TODO(campbell): Fix this stuff. It is all wrong.
Brian Silverman052e69d2017-02-12 16:19:55 -0800364 void CopyPosition(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800365 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
366 ::frc971::PotAndAbsolutePosition *position,
367 ::std::function<double(int32_t)> encoder_translate,
368 ::std::function<double(double)> potentiometer_translate, bool reverse,
369 double pot_offset) {
370 const double multiplier = reverse ? -1.0 : 1.0;
371 position->pot = multiplier * potentiometer_translate(
372 encoder.polled_potentiometer_voltage()) +
373 pot_offset;
374 position->relative_encoder =
375 multiplier * encoder_translate(encoder.last_encoder_value());
376 position->absolute_encoder =
377 multiplier * encoder_translate(encoder.polled_encoder_value());
378 }
379
380 // TODO(campbell): Fix this stuff. It is all wrong.
Brian Silverman052e69d2017-02-12 16:19:55 -0800381 void CopyPosition(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800382 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
383 ::frc971::EncoderAndIndexPosition *position,
384 ::std::function<double(int32_t)> encoder_translate, bool reverse) {
385 const double multiplier = reverse ? -1.0 : 1.0;
386 position->encoder =
387 multiplier * encoder_translate(encoder.polled_encoder_value());
388 position->latched_encoder =
389 multiplier * encoder_translate(encoder.last_encoder_value());
390 position->index_pulses = encoder.index_posedge_count();
391 }
Brian Silverman052e69d2017-02-12 16:19:55 -0800392#endif
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800393
394 int32_t my_pid_;
395 DriverStation *ds_;
396
397 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
398
Brian Silverman052e69d2017-02-12 16:19:55 -0800399 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
400 slow_encoder_filter_;
401
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800402 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
403 drivetrain_right_encoder_;
404
405 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_;
406
Brian Silverman052e69d2017-02-12 16:19:55 -0800407 ::std::unique_ptr<Encoder> indexer_encoder_;
408 ::std::unique_ptr<AnalogInput> indexer_hall_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800409
410 ::frc971::wpilib::DMAEncoderAndPotentiometer turret_encoder_;
411 ::frc971::wpilib::DMAEncoderAndPotentiometer hood_encoder_;
412 ::std::unique_ptr<Encoder> shooter_encoder_;
413
414 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
415
416 ::std::atomic<bool> run_{true};
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800417};
418
419class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
420 public:
421 void set_drivetrain_left_victor(::std::unique_ptr<VictorSP> t) {
422 drivetrain_left_victor_ = ::std::move(t);
423 }
424
425 void set_drivetrain_right_victor(::std::unique_ptr<VictorSP> t) {
426 drivetrain_right_victor_ = ::std::move(t);
427 }
428
429 private:
430 virtual void Read() override {
431 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
432 }
433
434 virtual void Write() override {
435 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
436 LOG_STRUCT(DEBUG, "will output", *queue);
437 drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0);
438 drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0);
439 }
440
441 virtual void Stop() override {
442 LOG(WARNING, "drivetrain output too old\n");
443 drivetrain_left_victor_->SetDisabled();
444 drivetrain_right_victor_->SetDisabled();
445 }
446
447 ::std::unique_ptr<VictorSP> drivetrain_left_victor_, drivetrain_right_victor_;
448};
449
450class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
451 public:
452 void set_intake_victor(::std::unique_ptr<VictorSP> t) {
453 intake_victor_ = ::std::move(t);
454 }
455 void set_intake_rollers_victor(::std::unique_ptr<VictorSP> t) {
456 intake_rollers_victor_ = ::std::move(t);
457 }
458
Brian Silverman052e69d2017-02-12 16:19:55 -0800459 void set_indexer_victor(::std::unique_ptr<VictorSP> t) {
460 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800461 }
Brian Silverman052e69d2017-02-12 16:19:55 -0800462 void set_indexer_roller_victor(::std::unique_ptr<VictorSP> t) {
463 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800464 }
465
466 void set_shooter_victor(::std::unique_ptr<VictorSP> t) {
467 shooter_victor_ = ::std::move(t);
468 }
469 void set_turret_victor(::std::unique_ptr<VictorSP> t) {
470 turret_victor_ = ::std::move(t);
471 }
472 void set_hood_victor(::std::unique_ptr<VictorSP> t) {
473 hood_victor_ = ::std::move(t);
474 }
475
476 private:
477 virtual void Read() override {
478 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
479 }
480
481 virtual void Write() override {
482 auto &queue = ::y2017::control_loops::superstructure_queue.output;
483 LOG_STRUCT(DEBUG, "will output", *queue);
484 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
485 -kMaxBringupPower, kMaxBringupPower) /
486 12.0);
487 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Brian Silverman052e69d2017-02-12 16:19:55 -0800488 indexer_victor_->SetSpeed(queue->voltage_indexer / 12.0);
489 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers /
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800490 12.0);
491 turret_victor_->SetSpeed(::aos::Clip(queue->voltage_turret,
492 -kMaxBringupPower, kMaxBringupPower) /
493 12.0);
494 hood_victor_->SetSpeed(
495 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
496 12.0);
497 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
498 }
499
500 virtual void Stop() override {
501 LOG(WARNING, "Superstructure output too old.\n");
502 intake_victor_->SetDisabled();
503 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800504 indexer_victor_->SetDisabled();
505 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800506 turret_victor_->SetDisabled();
507 hood_victor_->SetDisabled();
508 shooter_victor_->SetDisabled();
509 }
510
511 ::std::unique_ptr<VictorSP> intake_victor_, intake_rollers_victor_,
Brian Silverman052e69d2017-02-12 16:19:55 -0800512 indexer_victor_, indexer_roller_victor_, shooter_victor_,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800513 turret_victor_, hood_victor_;
514};
515
516class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
517 public:
518 ::std::unique_ptr<Encoder> make_encoder(int index) {
519 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
520 Encoder::k4X);
521 }
522
523 void Run() override {
524 ::aos::InitNRT();
525 ::aos::SetCurrentThreadName("StartCompetition");
526
527 ::frc971::wpilib::JoystickSender joystick_sender;
528 ::std::thread joystick_thread(::std::ref(joystick_sender));
529
530 ::frc971::wpilib::PDPFetcher pdp_fetcher;
531 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
532 SensorReader reader;
533
534 // TODO(campbell): Update port numbers
535 reader.set_drivetrain_left_encoder(make_encoder(0));
536 reader.set_drivetrain_right_encoder(make_encoder(1));
537
538 reader.set_intake_encoder(make_encoder(2));
539 reader.set_intake_index(make_unique<DigitalInput>(0));
540 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
541
Brian Silverman052e69d2017-02-12 16:19:55 -0800542 reader.set_indexer_encoder(make_encoder(3));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800543
544 reader.set_turret_encoder(make_encoder(5));
545 reader.set_turret_index(make_unique<DigitalInput>(1));
546 reader.set_turret_potentiometer(make_unique<AnalogInput>(3));
547
548 reader.set_hood_encoder(make_encoder(6));
549 reader.set_hood_index(make_unique<DigitalInput>(2));
550
551 reader.set_shooter_encoder(make_encoder(7));
552
553 reader.set_autonomous_mode(0, make_unique<DigitalInput>(6));
554 reader.set_autonomous_mode(1, make_unique<DigitalInput>(5));
555 reader.set_autonomous_mode(2, make_unique<DigitalInput>(4));
556 reader.set_autonomous_mode(3, make_unique<DigitalInput>(3));
557
558 reader.set_dma(make_unique<DMA>());
559 ::std::thread reader_thread(::std::ref(reader));
560
561 ::frc971::wpilib::GyroSender gyro_sender;
562 ::std::thread gyro_thread(::std::ref(gyro_sender));
563
564 auto imu_trigger = make_unique<DigitalInput>(5);
565 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get());
566 ::std::thread imu_thread(::std::ref(imu));
567
568 DrivetrainWriter drivetrain_writer;
569 drivetrain_writer.set_drivetrain_left_victor(
570 ::std::unique_ptr<VictorSP>(new VictorSP(0)));
571 drivetrain_writer.set_drivetrain_right_victor(
572 ::std::unique_ptr<VictorSP>(new VictorSP(1)));
573 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
574
575 SuperstructureWriter superstructure_writer;
576 superstructure_writer.set_intake_victor(
577 ::std::unique_ptr<VictorSP>(new VictorSP(2)));
578 superstructure_writer.set_intake_rollers_victor(
579 ::std::unique_ptr<VictorSP>(new VictorSP(3)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800580 superstructure_writer.set_indexer_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800581 ::std::unique_ptr<VictorSP>(new VictorSP(4)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800582 superstructure_writer.set_indexer_roller_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800583 ::std::unique_ptr<VictorSP>(new VictorSP(5)));
584 superstructure_writer.set_turret_victor(
585 ::std::unique_ptr<VictorSP>(new VictorSP(6)));
586 superstructure_writer.set_hood_victor(
587 ::std::unique_ptr<VictorSP>(new VictorSP(7)));
588 superstructure_writer.set_shooter_victor(
589 ::std::unique_ptr<VictorSP>(new VictorSP(8)));
590 ::std::thread superstructure_writer_thread(
591 ::std::ref(superstructure_writer));
592
593 // Wait forever. Not much else to do...
594 while (true) {
595 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
596 if (r != 0) {
597 PLOG(WARNING, "infinite select failed");
598 } else {
599 PLOG(WARNING, "infinite select succeeded??\n");
600 }
601 }
602
603 LOG(ERROR, "Exiting WPILibRobot\n");
604
605 joystick_sender.Quit();
606 joystick_thread.join();
607 pdp_fetcher.Quit();
608 pdp_fetcher_thread.join();
609 reader.Quit();
610 reader_thread.join();
611 gyro_sender.Quit();
612 gyro_thread.join();
613 imu.Quit();
614 imu_thread.join();
615
616 drivetrain_writer.Quit();
617 drivetrain_writer_thread.join();
618 superstructure_writer.Quit();
619 superstructure_writer_thread.join();
620
621 ::aos::Cleanup();
622 }
623};
624
Brian Silverman052e69d2017-02-12 16:19:55 -0800625} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800626} // namespace wpilib
627} // namespace y2017
628
629AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);