blob: a130544552d3474fdebc7b075676d1891ebfb7d8 [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
6#include <thread>
Austin Schuh8aec1ed2016-05-01 13:29:20 -07007#include <chrono>
Brian Silverman17f503e2015-08-02 18:17:18 -07008#include <mutex>
9#include <functional>
10
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070011#include "Encoder.h"
12#include "Talon.h"
13#include "DriverStation.h"
14#include "AnalogInput.h"
15#include "Compressor.h"
16#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080017#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#undef ERROR
22
Brian Silverman17f503e2015-08-02 18:17:18 -070023#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
Brian Silverman552350b2015-08-02 18:23:34 -070033#include "frc971/shifter_hall_effect.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050034
Comran Morshed5323ecb2015-12-26 20:50:55 +000035#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070036#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070037#include "y2014/control_loops/shooter/shooter.q.h"
38#include "y2014/constants.h"
39#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070040
Brian Silverman17f503e2015-08-02 18:17:18 -070041#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"
Brian Silverman811f8ec2015-12-06 01:29:42 -050050#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman425492b2015-12-30 15:23:55 -080051#include "frc971/wpilib/pdp_fetcher.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050052#include "frc971/wpilib/dma.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070053
Brian Silverman17f503e2015-08-02 18:17:18 -070054#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
Comran Morshed5323ecb2015-12-26 20:50:55 +000058using ::frc971::control_loops::drivetrain_queue;
Brian Silvermanb601d892015-12-20 18:24:38 -050059using ::y2014::control_loops::claw_queue;
60using ::y2014::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070061
Brian Silvermanb601d892015-12-20 18:24:38 -050062namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070063namespace wpilib {
64
Brian Silverman85fbb602015-08-29 19:28:20 -070065// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
66// DMA stuff and then removing the * 2.0 in *_translate.
67// The low bit is direction.
68
Brian Silverman552350b2015-08-02 18:23:34 -070069// TODO(brian): Replace this with ::std::make_unique once all our toolchains
70// have support.
71template <class T, class... U>
72std::unique_ptr<T> make_unique(U &&... u) {
73 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
74}
75
Brian Silverman17f503e2015-08-02 18:17:18 -070076double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070077 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070078 (256.0 /*cpr*/ * 4.0 /*4x*/) *
79 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080080 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070081}
82
Brian Silverman51091a02015-12-26 15:56:58 -080083double drivetrain_velocity_translate(double in) {
84 return (1.0 / in) / 256.0 /*cpr*/ *
85 constants::GetValues().drivetrain_encoder_ratio *
86 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
87}
88
Sabina Davis415bb6c2017-10-16 23:30:52 -070089float hall_translate(const constants::DualHallShifterHallEffect &k, float in_low,
Brian Silverman552350b2015-08-02 18:23:34 -070090 float in_high) {
91 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070092 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
93 static_cast<float>(k.low_gear_middle - k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070094 const float high_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070095 0.5 +
96 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
97 static_cast<float>(k.shifter_hall_effect.high_gear_high -
98 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070099
Brian Silverman552350b2015-08-02 18:23:34 -0700100 // Return low when we are below 1/2, and high when we are above 1/2.
101 if (low_ratio + high_ratio < 1.0) {
102 return low_ratio;
103 } else {
104 return high_ratio;
105 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700106}
107
108double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700109 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700110 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700111 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700112}
113
Brian Silverman552350b2015-08-02 18:23:34 -0700114double shooter_translate(int32_t in) {
115 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
116 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
117 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700118}
119
120static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700121 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
122 18.0 / 32.0 /* big belt reduction */ *
123 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
124 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700125
126class SensorReader {
127 public:
Brian Silverman39b339e2016-01-03 13:24:22 -0800128 SensorReader() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700129 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
130 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700131 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700133 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700134 }
135
Brian Silverman552350b2015-08-02 18:23:34 -0700136 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
137 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700138 }
139
Brian Silverman552350b2015-08-02 18:23:34 -0700140 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
141 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800142 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700143 }
144
Brian Silverman552350b2015-08-02 18:23:34 -0700145 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
146 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800147 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Brian Silverman552350b2015-08-02 18:23:34 -0700150 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
151 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700152 }
153
Brian Silverman552350b2015-08-02 18:23:34 -0700154 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
155 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700156 }
157
Brian Silverman552350b2015-08-02 18:23:34 -0700158 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
159 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700160 }
161
Brian Silverman552350b2015-08-02 18:23:34 -0700162 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
163 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700164 }
165
Brian Silverman552350b2015-08-02 18:23:34 -0700166 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
167 encoder_filter_.Add(encoder.get());
168 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700169 }
170
Austin Schuhc5e36082015-10-31 13:30:46 -0700171 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700172 hall_filter_.Add(hall.get());
173 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700174 }
175
Austin Schuhc5e36082015-10-31 13:30:46 -0700176 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700177 hall_filter_.Add(hall.get());
178 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700179 }
180
Austin Schuhc5e36082015-10-31 13:30:46 -0700181 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700182 hall_filter_.Add(hall.get());
183 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700184 }
185
Brian Silverman552350b2015-08-02 18:23:34 -0700186 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
187 encoder_filter_.Add(encoder.get());
188 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700189 }
190
Austin Schuhc5e36082015-10-31 13:30:46 -0700191 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700192 hall_filter_.Add(hall.get());
193 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700194 }
195
Austin Schuhc5e36082015-10-31 13:30:46 -0700196 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700197 hall_filter_.Add(hall.get());
198 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700199 }
200
Austin Schuhc5e36082015-10-31 13:30:46 -0700201 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700202 hall_filter_.Add(hall.get());
203 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700204 }
205
Brian Silverman552350b2015-08-02 18:23:34 -0700206 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
207 encoder_filter_.Add(encoder.get());
208 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700209 }
210
Austin Schuhc5e36082015-10-31 13:30:46 -0700211 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700212 hall_filter_.Add(hall.get());
213 shooter_proximal_ = ::std::move(hall);
214 }
215
Austin Schuhc5e36082015-10-31 13:30:46 -0700216 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700217 hall_filter_.Add(hall.get());
218 shooter_distal_ = ::std::move(hall);
219 }
220
Austin Schuhc5e36082015-10-31 13:30:46 -0700221 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700222 hall_filter_.Add(hall.get());
223 shooter_plunger_ = ::std::move(hall);
224 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500225 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700226 }
227
Austin Schuhc5e36082015-10-31 13:30:46 -0700228 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700229 hall_filter_.Add(hall.get());
230 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500231 shooter_latch_reader_ =
232 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700233 }
234
235 // All of the DMA-related set_* calls must be made before this, and it doesn't
236 // hurt to do all of them.
237 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500238 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700239 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500240 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700241 shooter_encoder_.get(), shooter_distal_.get());
242
Brian Silvermanb601d892015-12-20 18:24:38 -0500243 dma_synchronizer_.reset(
244 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700245 dma_synchronizer_->Add(shooter_proximal_counter_.get());
246 dma_synchronizer_->Add(shooter_distal_counter_.get());
247 dma_synchronizer_->Add(shooter_plunger_reader_.get());
248 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700249 }
250
251 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700252 ::aos::SetCurrentThreadName("SensorReader");
253
254 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700255 ds_ =
256#ifdef WPILIB2015
257 DriverStation::GetInstance();
258#else
259 &DriverStation::GetInstance();
260#endif
Brian Silverman17f503e2015-08-02 18:17:18 -0700261
Brian Silverman552350b2015-08-02 18:23:34 -0700262 top_reader_.Start();
263 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700264 dma_synchronizer_->Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700265
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700266 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
267 ::std::chrono::milliseconds(4));
Brian Silvermane7611a02015-12-30 16:20:33 -0800268
Brian Silverman5090c432016-01-02 14:44:26 -0800269 ::aos::SetCurrentThreadRealtimePriority(40);
Brian Silverman17f503e2015-08-02 18:17:18 -0700270 while (run_) {
Brian Silvermane7611a02015-12-30 16:20:33 -0800271 {
272 const int iterations = phased_loop.SleepUntilNext();
273 if (iterations != 1) {
274 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
275 }
276 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700277 RunIteration();
278 }
279
Brian Silverman552350b2015-08-02 18:23:34 -0700280 top_reader_.Quit();
281 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700282 }
283
284 void RunIteration() {
Brian Silverman39b339e2016-01-03 13:24:22 -0800285 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700286
Brian Silverman552350b2015-08-02 18:23:34 -0700287 const auto &values = constants::GetValues();
288
Brian Silverman17f503e2015-08-02 18:17:18 -0700289 {
290 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
291 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700292 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700293 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700294 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800295 drivetrain_message->left_speed =
296 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
297 drivetrain_message->right_speed =
298 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800299
300 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
301 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700302 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800303 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
304 drivetrain_message->high_left_hall);
305
306 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
307 drivetrain_message->high_right_hall =
308 high_right_drive_hall_->GetVoltage();
309 drivetrain_message->right_shifter_position =
310 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
311 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700312
313 drivetrain_message.Send();
314 }
315
Brian Silvermanb601d892015-12-20 18:24:38 -0500316 ::y2014::sensors::auto_mode.MakeWithBuilder()
Brian Silverman552350b2015-08-02 18:23:34 -0700317 .voltage(auto_selector_analog_->GetVoltage())
318 .Send();
319
Brian Silverman17f503e2015-08-02 18:17:18 -0700320 dma_synchronizer_->RunIteration();
321
Brian Silverman17f503e2015-08-02 18:17:18 -0700322 {
Brian Silverman552350b2015-08-02 18:23:34 -0700323 auto shooter_message = shooter_queue.position.MakeMessage();
324 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800325 shooter_message->plunger = !shooter_plunger_reader_->value();
326 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700327 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
328 &shooter_message->pusher_proximal);
329 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
330 &shooter_message->pusher_distal);
331
332 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700333 }
334
335 {
336 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700337 top_reader_.RunIteration(&claw_message->top);
338 bottom_reader_.RunIteration(&claw_message->bottom);
339
Brian Silverman17f503e2015-08-02 18:17:18 -0700340 claw_message.Send();
341 }
342 }
343
344 void Quit() { run_ = false; }
345
346 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700347 class HalfClawReader {
348 public:
349 HalfClawReader(bool reversed) : reversed_(reversed) {}
350
351 void set_encoder(::std::unique_ptr<Encoder> encoder) {
352 encoder_ = ::std::move(encoder);
353 }
354
Austin Schuhc5e36082015-10-31 13:30:46 -0700355 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700356 front_hall_ = ::std::move(front_hall);
357 }
358
359 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700360 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700361 calibration_hall_ = ::std::move(calibration_hall);
362 }
363
Austin Schuhc5e36082015-10-31 13:30:46 -0700364 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700365 back_hall_ = ::std::move(back_hall);
366 }
367
368 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500369 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
370 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700371 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500372 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
373 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700374 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500375 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
376 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700377 synchronizer_.Add(back_counter_.get());
378 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500379 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
380 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700381 synchronizer_.Add(synchronized_encoder_.get());
382
383 synchronizer_.Start();
384 }
385
386 void Quit() { synchronizer_.Quit(); }
387
388 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
389 const double multiplier = reversed_ ? -1.0 : 1.0;
390
391 synchronizer_.RunIteration();
392
393 CopyPosition(front_counter_.get(), &half_claw_position->front);
394 CopyPosition(calibration_counter_.get(),
395 &half_claw_position->calibration);
396 CopyPosition(back_counter_.get(), &half_claw_position->back);
397 half_claw_position->position =
398 multiplier * claw_translate(synchronized_encoder_->get());
399 }
400
401 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500402 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
403 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700404 const double multiplier = reversed_ ? -1.0 : 1.0;
405
Austin Schuh5c25ab72015-11-01 13:17:11 -0800406 out->current = !counter->polled_value();
407 out->posedge_count = counter->negative_interrupt_count();
408 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700409 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800410 multiplier * claw_translate(counter->last_positive_encoder_value());
411 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700412 multiplier * claw_translate(counter->last_negative_encoder_value());
413 }
414
Brian Silverman5090c432016-01-02 14:44:26 -0800415 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700416
Brian Silvermanb601d892015-12-20 18:24:38 -0500417 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
418 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
419 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
420 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
421 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700422
423 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700424 ::std::unique_ptr<DigitalInput> front_hall_;
425 ::std::unique_ptr<DigitalInput> calibration_hall_;
426 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700427
428 const bool reversed_;
429 };
430
Brian Silvermanb601d892015-12-20 18:24:38 -0500431 void CopyShooterPosedgeCounts(
432 const ::frc971::wpilib::DMAEdgeCounter *counter,
433 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800434 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700435 // These are inverted because the hall effects give logical false when
436 // there's a magnet in front of them.
437 output->posedge_count = counter->negative_count();
438 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700439 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700440 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700441 }
442
Brian Silverman17f503e2015-08-02 18:17:18 -0700443 int32_t my_pid_;
444 DriverStation *ds_;
445
Brian Silvermanb601d892015-12-20 18:24:38 -0500446 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700447
Brian Silverman552350b2015-08-02 18:23:34 -0700448 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700449
Brian Silverman552350b2015-08-02 18:23:34 -0700450 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
451 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
452 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
453 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
454 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
455 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700456
Brian Silverman552350b2015-08-02 18:23:34 -0700457 HalfClawReader top_reader_{false}, bottom_reader_{true};
458
459 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700460 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
461 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500462 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700463 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500464 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700465 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700466
467 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700468 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700469};
470
471class SolenoidWriter {
472 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500473 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700474 : pcm_(pcm),
Brian Silvermanc75c1ad2015-12-30 16:21:13 -0800475 shooter_(".y2014.control_loops.shooter_queue.output"),
Comran Morshed5323ecb2015-12-26 20:50:55 +0000476 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700477
Austin Schuhc5e36082015-10-31 13:30:46 -0700478 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700479 pressure_switch_ = ::std::move(pressure_switch);
480 }
481
482 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
483 compressor_relay_ = ::std::move(compressor_relay);
484 }
485
Brian Silvermanb601d892015-12-20 18:24:38 -0500486 void set_drivetrain_left(
487 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700488 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700489 }
490
Brian Silvermanb601d892015-12-20 18:24:38 -0500491 void set_drivetrain_right(
492 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700493 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700494 }
495
Brian Silvermanb601d892015-12-20 18:24:38 -0500496 void set_shooter_latch(
497 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700498 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700499 }
500
Brian Silvermanb601d892015-12-20 18:24:38 -0500501 void set_shooter_brake(
502 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700503 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700504 }
505
506 void operator()() {
507 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800508 ::aos::SetCurrentThreadRealtimePriority(27);
509
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700510 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
511 ::std::chrono::milliseconds(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700512
513 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800514 {
515 const int iterations = phased_loop.SleepUntilNext();
516 if (iterations != 1) {
517 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
518 }
519 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700520
521 {
Brian Silverman552350b2015-08-02 18:23:34 -0700522 shooter_.FetchLatest();
523 if (shooter_.get()) {
524 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
525 shooter_latch_->Set(!shooter_->latch_piston);
526 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700527 }
528 }
529
530 {
Brian Silverman552350b2015-08-02 18:23:34 -0700531 drivetrain_.FetchLatest();
532 if (drivetrain_.get()) {
533 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800534 drivetrain_left_->Set(!drivetrain_->left_high);
535 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700536 }
537 }
538
Brian Silverman17f503e2015-08-02 18:17:18 -0700539 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500540 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700541 {
542 const bool compressor_on = !pressure_switch_->Get();
543 to_log.compressor_on = compressor_on;
544 if (compressor_on) {
545 compressor_relay_->Set(Relay::kForward);
546 } else {
547 compressor_relay_->Set(Relay::kOff);
548 }
549 }
550
551 pcm_->Flush();
552 to_log.read_solenoids = pcm_->GetAll();
553 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
554 }
555 }
556 }
557
558 void Quit() { run_ = false; }
559
560 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500561 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700562
Brian Silvermanb601d892015-12-20 18:24:38 -0500563 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
564 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
565 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
566 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700567
Austin Schuhc5e36082015-10-31 13:30:46 -0700568 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700569 ::std::unique_ptr<Relay> compressor_relay_;
570
Brian Silvermanb601d892015-12-20 18:24:38 -0500571 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000572 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700573
574 ::std::atomic<bool> run_{true};
575};
576
Brian Silvermanb601d892015-12-20 18:24:38 -0500577class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700578 public:
579 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
580 left_drivetrain_talon_ = ::std::move(t);
581 }
582
583 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
584 right_drivetrain_talon_ = ::std::move(t);
585 }
586
587 private:
588 virtual void Read() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000589 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 }
591
592 virtual void Write() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000593 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700594 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800595 left_drivetrain_talon_->SetSpeed(-queue->left_voltage / 12.0);
596 right_drivetrain_talon_->SetSpeed(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700597 }
598
599 virtual void Stop() override {
600 LOG(WARNING, "drivetrain output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800601 left_drivetrain_talon_->SetDisabled();
602 right_drivetrain_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700603 }
604
605 ::std::unique_ptr<Talon> left_drivetrain_talon_;
606 ::std::unique_ptr<Talon> right_drivetrain_talon_;
607};
608
Brian Silvermanb601d892015-12-20 18:24:38 -0500609class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700610 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700611 void set_shooter_talon(::std::unique_ptr<Talon> t) {
612 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700613 }
614
615 private:
616 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500617 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700618 }
619
620 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500621 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700622 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800623 shooter_talon_->SetSpeed(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700624 }
625
626 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700627 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800628 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700629 }
630
Brian Silverman552350b2015-08-02 18:23:34 -0700631 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700632};
633
Brian Silvermanb601d892015-12-20 18:24:38 -0500634class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700635 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700636 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
637 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700638 }
639
Brian Silverman552350b2015-08-02 18:23:34 -0700640 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
641 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700642 }
643
Brian Silverman552350b2015-08-02 18:23:34 -0700644 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
645 left_tusk_talon_ = ::std::move(t);
646 }
647
648 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
649 right_tusk_talon_ = ::std::move(t);
650 }
651
652 void set_intake1_talon(::std::unique_ptr<Talon> t) {
653 intake1_talon_ = ::std::move(t);
654 }
655
656 void set_intake2_talon(::std::unique_ptr<Talon> t) {
657 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700658 }
659
660 private:
661 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500662 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700663 }
664
665 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500666 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700667 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800668 intake1_talon_->SetSpeed(queue->intake_voltage / 12.0);
669 intake2_talon_->SetSpeed(queue->intake_voltage / 12.0);
670 bottom_claw_talon_->SetSpeed(-queue->bottom_claw_voltage / 12.0);
671 top_claw_talon_->SetSpeed(queue->top_claw_voltage / 12.0);
672 left_tusk_talon_->SetSpeed(queue->tusk_voltage / 12.0);
673 right_tusk_talon_->SetSpeed(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700674 }
675
676 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700677 LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800678 intake1_talon_->SetDisabled();
679 intake2_talon_->SetDisabled();
680 bottom_claw_talon_->SetDisabled();
681 top_claw_talon_->SetDisabled();
682 left_tusk_talon_->SetDisabled();
683 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700684 }
685
Brian Silverman552350b2015-08-02 18:23:34 -0700686 ::std::unique_ptr<Talon> top_claw_talon_;
687 ::std::unique_ptr<Talon> bottom_claw_talon_;
688 ::std::unique_ptr<Talon> left_tusk_talon_;
689 ::std::unique_ptr<Talon> right_tusk_talon_;
690 ::std::unique_ptr<Talon> intake1_talon_;
691 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700692};
693
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800694class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700695 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700696 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700697 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
698 Encoder::k4X);
699 }
Brian Silverman552350b2015-08-02 18:23:34 -0700700
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800701 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700702 ::aos::InitNRT();
703 ::aos::SetCurrentThreadName("StartCompetition");
704
Brian Silvermanb601d892015-12-20 18:24:38 -0500705 ::frc971::wpilib::JoystickSender joystick_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700706 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700707
Brian Silverman425492b2015-12-30 15:23:55 -0800708 ::frc971::wpilib::PDPFetcher pdp_fetcher;
709 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Brian Silverman39b339e2016-01-03 13:24:22 -0800710 SensorReader reader;
Brian Silverman17f503e2015-08-02 18:17:18 -0700711
Brian Silverman85fbb602015-08-29 19:28:20 -0700712 // Create this first to make sure it ends up in one of the lower-numbered
713 // FPGA slots so we can use it with DMA.
714 auto shooter_encoder_temp = make_encoder(2);
715
Brian Silverman552350b2015-08-02 18:23:34 -0700716 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700717
Brian Silverman552350b2015-08-02 18:23:34 -0700718 reader.set_drivetrain_left_encoder(make_encoder(0));
719 reader.set_drivetrain_right_encoder(make_encoder(1));
720 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
721 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
722 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
723 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700724
Brian Silverman552350b2015-08-02 18:23:34 -0700725 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800726 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
727 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
728 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700729
Brian Silverman85fbb602015-08-29 19:28:20 -0700730 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800731 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
732 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
733 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700734
Brian Silverman85fbb602015-08-29 19:28:20 -0700735 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800736 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
737 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
738 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
739 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700740
Brian Silverman17f503e2015-08-02 18:17:18 -0700741 reader.set_dma(make_unique<DMA>());
742 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700743
Brian Silvermanb601d892015-12-20 18:24:38 -0500744 ::frc971::wpilib::GyroSender gyro_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700745 ::std::thread gyro_thread(::std::ref(gyro_sender));
746
747 DrivetrainWriter drivetrain_writer;
748 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700749 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700750 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700751 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700752 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
753
Brian Silvermanb601d892015-12-20 18:24:38 -0500754 ::y2014::wpilib::ClawWriter claw_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700755 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
756 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
757 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
758 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
759 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
760 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700761 ::std::thread claw_writer_thread(::std::ref(claw_writer));
762
Brian Silvermanb601d892015-12-20 18:24:38 -0500763 ::y2014::wpilib::ShooterWriter shooter_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700764 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
765 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
766
Brian Silverman17f503e2015-08-02 18:17:18 -0700767 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
768 new ::frc971::wpilib::BufferedPcm());
769 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700770 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
771 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
772 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
773 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700774
Austin Schuh016a6b02015-10-08 06:41:14 +0000775 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700776 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
777 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
778
779 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800780 while (true) {
781 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
782 if (r != 0) {
783 PLOG(WARNING, "infinite select failed");
784 } else {
785 PLOG(WARNING, "infinite select succeeded??\n");
786 }
787 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700788
789 LOG(ERROR, "Exiting WPILibRobot\n");
790
791 joystick_sender.Quit();
792 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800793 pdp_fetcher.Quit();
794 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700795 reader.Quit();
796 reader_thread.join();
797 gyro_sender.Quit();
798 gyro_thread.join();
799
800 drivetrain_writer.Quit();
801 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700802 shooter_writer.Quit();
803 shooter_writer_thread.join();
804 claw_writer.Quit();
805 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700806 solenoid_writer.Quit();
807 solenoid_thread.join();
808
809 ::aos::Cleanup();
810 }
811};
812
813} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500814} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700815
816
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800817AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);