blob: cc634d988b7f898b4c178501d5841772a3df04b7 [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>
7#include <mutex>
8#include <functional>
9
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070010#include "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
15#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080016#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070017#include "dma.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
Brian Silverman17f503e2015-08-02 18:17:18 -070035#include "y2014/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 Silverman17f503e2015-08-02 18:17:18 -070052
Brian Silverman17f503e2015-08-02 18:17:18 -070053#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
Brian Silvermanb601d892015-12-20 18:24:38 -050057using ::y2014::control_loops::drivetrain_queue;
58using ::y2014::control_loops::claw_queue;
59using ::y2014::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070060
Brian Silvermanb601d892015-12-20 18:24:38 -050061namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070062namespace wpilib {
63
Brian Silverman85fbb602015-08-29 19:28:20 -070064// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
65// DMA stuff and then removing the * 2.0 in *_translate.
66// The low bit is direction.
67
Brian Silverman552350b2015-08-02 18:23:34 -070068// TODO(brian): Replace this with ::std::make_unique once all our toolchains
69// have support.
70template <class T, class... U>
71std::unique_ptr<T> make_unique(U &&... u) {
72 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
73}
74
Brian Silverman17f503e2015-08-02 18:17:18 -070075double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070076 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070077 (256.0 /*cpr*/ * 4.0 /*4x*/) *
78 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080079 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070080}
81
Brian Silverman51091a02015-12-26 15:56:58 -080082double drivetrain_velocity_translate(double in) {
83 return (1.0 / in) / 256.0 /*cpr*/ *
84 constants::GetValues().drivetrain_encoder_ratio *
85 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
86}
87
Brian Silverman552350b2015-08-02 18:23:34 -070088float hall_translate(const constants::ShifterHallEffect &k, float in_low,
89 float in_high) {
90 const float low_ratio =
91 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
92 static_cast<float>(k.low_gear_middle - k.low_gear_low);
93 const float high_ratio =
94 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
95 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070096
Brian Silverman552350b2015-08-02 18:23:34 -070097 // Return low when we are below 1/2, and high when we are above 1/2.
98 if (low_ratio + high_ratio < 1.0) {
99 return low_ratio;
100 } else {
101 return high_ratio;
102 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700103}
104
105double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700106 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700107 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700108 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700109}
110
Brian Silverman552350b2015-08-02 18:23:34 -0700111double shooter_translate(int32_t in) {
112 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
113 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
114 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700115}
116
117static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700118 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
119 18.0 / 32.0 /* big belt reduction */ *
120 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
121 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700122
123class SensorReader {
124 public:
Brian Silverman425492b2015-12-30 15:23:55 -0800125 SensorReader(::frc971::wpilib::PDPFetcher *pdp_fetcher)
126 : pdp_fetcher_(pdp_fetcher) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700127 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
128 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700129 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700130 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700131 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
135 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 }
137
Brian Silverman552350b2015-08-02 18:23:34 -0700138 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
139 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800140 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700141 }
142
Brian Silverman552350b2015-08-02 18:23:34 -0700143 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
144 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800145 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700146 }
147
Brian Silverman552350b2015-08-02 18:23:34 -0700148 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
149 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700150 }
151
Brian Silverman552350b2015-08-02 18:23:34 -0700152 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
153 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 }
155
Brian Silverman552350b2015-08-02 18:23:34 -0700156 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
157 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700158 }
159
Brian Silverman552350b2015-08-02 18:23:34 -0700160 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
161 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700162 }
163
Brian Silverman552350b2015-08-02 18:23:34 -0700164 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
165 encoder_filter_.Add(encoder.get());
166 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700167 }
168
Austin Schuhc5e36082015-10-31 13:30:46 -0700169 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700170 hall_filter_.Add(hall.get());
171 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700172 }
173
Austin Schuhc5e36082015-10-31 13:30:46 -0700174 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700175 hall_filter_.Add(hall.get());
176 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 }
178
Austin Schuhc5e36082015-10-31 13:30:46 -0700179 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700180 hall_filter_.Add(hall.get());
181 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700182 }
183
Brian Silverman552350b2015-08-02 18:23:34 -0700184 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
185 encoder_filter_.Add(encoder.get());
186 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 }
188
Austin Schuhc5e36082015-10-31 13:30:46 -0700189 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700190 hall_filter_.Add(hall.get());
191 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700192 }
193
Austin Schuhc5e36082015-10-31 13:30:46 -0700194 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700195 hall_filter_.Add(hall.get());
196 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700197 }
198
Austin Schuhc5e36082015-10-31 13:30:46 -0700199 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700200 hall_filter_.Add(hall.get());
201 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700202 }
203
Brian Silverman552350b2015-08-02 18:23:34 -0700204 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
205 encoder_filter_.Add(encoder.get());
206 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700207 }
208
Austin Schuhc5e36082015-10-31 13:30:46 -0700209 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700210 hall_filter_.Add(hall.get());
211 shooter_proximal_ = ::std::move(hall);
212 }
213
Austin Schuhc5e36082015-10-31 13:30:46 -0700214 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700215 hall_filter_.Add(hall.get());
216 shooter_distal_ = ::std::move(hall);
217 }
218
Austin Schuhc5e36082015-10-31 13:30:46 -0700219 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700220 hall_filter_.Add(hall.get());
221 shooter_plunger_ = ::std::move(hall);
222 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500223 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700224 }
225
Austin Schuhc5e36082015-10-31 13:30:46 -0700226 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700227 hall_filter_.Add(hall.get());
228 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500229 shooter_latch_reader_ =
230 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700231 }
232
233 // All of the DMA-related set_* calls must be made before this, and it doesn't
234 // hurt to do all of them.
235 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500236 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700237 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500238 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700239 shooter_encoder_.get(), shooter_distal_.get());
240
Brian Silvermanb601d892015-12-20 18:24:38 -0500241 dma_synchronizer_.reset(
242 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700243 dma_synchronizer_->Add(shooter_proximal_counter_.get());
244 dma_synchronizer_->Add(shooter_distal_counter_.get());
245 dma_synchronizer_->Add(shooter_plunger_reader_.get());
246 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700247 }
248
249 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700250 ::aos::SetCurrentThreadName("SensorReader");
251
252 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700253 ds_ =
254#ifdef WPILIB2015
255 DriverStation::GetInstance();
256#else
257 &DriverStation::GetInstance();
258#endif
Brian Silverman17f503e2015-08-02 18:17:18 -0700259
Brian Silverman552350b2015-08-02 18:23:34 -0700260 top_reader_.Start();
261 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700262 dma_synchronizer_->Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700263
Brian Silvermane7611a02015-12-30 16:20:33 -0800264 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
265 ::aos::time::Time::InMS(4));
266
Brian Silverman5090c432016-01-02 14:44:26 -0800267 ::aos::SetCurrentThreadRealtimePriority(40);
Brian Silverman17f503e2015-08-02 18:17:18 -0700268 while (run_) {
Brian Silvermane7611a02015-12-30 16:20:33 -0800269 {
270 const int iterations = phased_loop.SleepUntilNext();
271 if (iterations != 1) {
272 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
273 }
274 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700275 RunIteration();
276 }
277
Brian Silverman552350b2015-08-02 18:23:34 -0700278 top_reader_.Quit();
279 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700280 }
281
282 void RunIteration() {
Brian Silverman425492b2015-12-30 15:23:55 -0800283 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_fetcher_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700284
Brian Silverman552350b2015-08-02 18:23:34 -0700285 const auto &values = constants::GetValues();
286
Brian Silverman17f503e2015-08-02 18:17:18 -0700287 {
288 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
289 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700290 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700291 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700292 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800293 drivetrain_message->left_speed =
294 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
295 drivetrain_message->right_speed =
296 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800297
298 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
299 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700300 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800301 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
302 drivetrain_message->high_left_hall);
303
304 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
305 drivetrain_message->high_right_hall =
306 high_right_drive_hall_->GetVoltage();
307 drivetrain_message->right_shifter_position =
308 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
309 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700310
311 drivetrain_message.Send();
312 }
313
Brian Silvermanb601d892015-12-20 18:24:38 -0500314 ::y2014::sensors::auto_mode.MakeWithBuilder()
Brian Silverman552350b2015-08-02 18:23:34 -0700315 .voltage(auto_selector_analog_->GetVoltage())
316 .Send();
317
Brian Silverman17f503e2015-08-02 18:17:18 -0700318 dma_synchronizer_->RunIteration();
319
Brian Silverman17f503e2015-08-02 18:17:18 -0700320 {
Brian Silverman552350b2015-08-02 18:23:34 -0700321 auto shooter_message = shooter_queue.position.MakeMessage();
322 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800323 shooter_message->plunger = !shooter_plunger_reader_->value();
324 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700325 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
326 &shooter_message->pusher_proximal);
327 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
328 &shooter_message->pusher_distal);
329
330 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700331 }
332
333 {
334 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700335 top_reader_.RunIteration(&claw_message->top);
336 bottom_reader_.RunIteration(&claw_message->bottom);
337
Brian Silverman17f503e2015-08-02 18:17:18 -0700338 claw_message.Send();
339 }
340 }
341
342 void Quit() { run_ = false; }
343
344 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700345 class HalfClawReader {
346 public:
347 HalfClawReader(bool reversed) : reversed_(reversed) {}
348
349 void set_encoder(::std::unique_ptr<Encoder> encoder) {
350 encoder_ = ::std::move(encoder);
351 }
352
Austin Schuhc5e36082015-10-31 13:30:46 -0700353 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700354 front_hall_ = ::std::move(front_hall);
355 }
356
357 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700358 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700359 calibration_hall_ = ::std::move(calibration_hall);
360 }
361
Austin Schuhc5e36082015-10-31 13:30:46 -0700362 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700363 back_hall_ = ::std::move(back_hall);
364 }
365
366 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500367 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
368 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700369 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500370 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
371 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700372 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500373 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
374 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700375 synchronizer_.Add(back_counter_.get());
376 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500377 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
378 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700379 synchronizer_.Add(synchronized_encoder_.get());
380
381 synchronizer_.Start();
382 }
383
384 void Quit() { synchronizer_.Quit(); }
385
386 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
387 const double multiplier = reversed_ ? -1.0 : 1.0;
388
389 synchronizer_.RunIteration();
390
391 CopyPosition(front_counter_.get(), &half_claw_position->front);
392 CopyPosition(calibration_counter_.get(),
393 &half_claw_position->calibration);
394 CopyPosition(back_counter_.get(), &half_claw_position->back);
395 half_claw_position->position =
396 multiplier * claw_translate(synchronized_encoder_->get());
397 }
398
399 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500400 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
401 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700402 const double multiplier = reversed_ ? -1.0 : 1.0;
403
Austin Schuh5c25ab72015-11-01 13:17:11 -0800404 out->current = !counter->polled_value();
405 out->posedge_count = counter->negative_interrupt_count();
406 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700407 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800408 multiplier * claw_translate(counter->last_positive_encoder_value());
409 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700410 multiplier * claw_translate(counter->last_negative_encoder_value());
411 }
412
Brian Silverman5090c432016-01-02 14:44:26 -0800413 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700414
Brian Silvermanb601d892015-12-20 18:24:38 -0500415 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
416 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
417 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
418 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
419 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700420
421 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700422 ::std::unique_ptr<DigitalInput> front_hall_;
423 ::std::unique_ptr<DigitalInput> calibration_hall_;
424 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700425
426 const bool reversed_;
427 };
428
Brian Silvermanb601d892015-12-20 18:24:38 -0500429 void CopyShooterPosedgeCounts(
430 const ::frc971::wpilib::DMAEdgeCounter *counter,
431 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800432 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700433 // These are inverted because the hall effects give logical false when
434 // there's a magnet in front of them.
435 output->posedge_count = counter->negative_count();
436 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700437 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700438 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700439 }
440
Brian Silverman17f503e2015-08-02 18:17:18 -0700441 int32_t my_pid_;
442 DriverStation *ds_;
Brian Silverman425492b2015-12-30 15:23:55 -0800443 ::frc971::wpilib::PDPFetcher *const pdp_fetcher_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700444
Brian Silvermanb601d892015-12-20 18:24:38 -0500445 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700446
Brian Silverman552350b2015-08-02 18:23:34 -0700447 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700448
Brian Silverman552350b2015-08-02 18:23:34 -0700449 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
450 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
451 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
452 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
453 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
454 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700455
Brian Silverman552350b2015-08-02 18:23:34 -0700456 HalfClawReader top_reader_{false}, bottom_reader_{true};
457
458 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700459 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
460 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500461 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700462 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500463 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700464 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700465
466 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700467 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700468};
469
470class SolenoidWriter {
471 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500472 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700473 : pcm_(pcm),
Brian Silvermanc75c1ad2015-12-30 16:21:13 -0800474 shooter_(".y2014.control_loops.shooter_queue.output"),
475 drivetrain_(".y2014.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700476
Austin Schuhc5e36082015-10-31 13:30:46 -0700477 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700478 pressure_switch_ = ::std::move(pressure_switch);
479 }
480
481 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
482 compressor_relay_ = ::std::move(compressor_relay);
483 }
484
Brian Silvermanb601d892015-12-20 18:24:38 -0500485 void set_drivetrain_left(
486 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700487 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700488 }
489
Brian Silvermanb601d892015-12-20 18:24:38 -0500490 void set_drivetrain_right(
491 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700492 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700493 }
494
Brian Silvermanb601d892015-12-20 18:24:38 -0500495 void set_shooter_latch(
496 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700497 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700498 }
499
Brian Silvermanb601d892015-12-20 18:24:38 -0500500 void set_shooter_brake(
501 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700502 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700503 }
504
505 void operator()() {
506 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800507 ::aos::SetCurrentThreadRealtimePriority(27);
508
509 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
510 ::aos::time::Time::InMS(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700511
512 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800513 {
514 const int iterations = phased_loop.SleepUntilNext();
515 if (iterations != 1) {
516 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
517 }
518 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700519
520 {
Brian Silverman552350b2015-08-02 18:23:34 -0700521 shooter_.FetchLatest();
522 if (shooter_.get()) {
523 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
524 shooter_latch_->Set(!shooter_->latch_piston);
525 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700526 }
527 }
528
529 {
Brian Silverman552350b2015-08-02 18:23:34 -0700530 drivetrain_.FetchLatest();
531 if (drivetrain_.get()) {
532 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800533 drivetrain_left_->Set(!drivetrain_->left_high);
534 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700535 }
536 }
537
Brian Silverman17f503e2015-08-02 18:17:18 -0700538 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500539 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700540 {
541 const bool compressor_on = !pressure_switch_->Get();
542 to_log.compressor_on = compressor_on;
543 if (compressor_on) {
544 compressor_relay_->Set(Relay::kForward);
545 } else {
546 compressor_relay_->Set(Relay::kOff);
547 }
548 }
549
550 pcm_->Flush();
551 to_log.read_solenoids = pcm_->GetAll();
552 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
553 }
554 }
555 }
556
557 void Quit() { run_ = false; }
558
559 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500560 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700561
Brian Silvermanb601d892015-12-20 18:24:38 -0500562 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
563 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
564 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
565 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700566
Austin Schuhc5e36082015-10-31 13:30:46 -0700567 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700568 ::std::unique_ptr<Relay> compressor_relay_;
569
Brian Silvermanb601d892015-12-20 18:24:38 -0500570 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
571 ::aos::Queue<::y2014::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700572
573 ::std::atomic<bool> run_{true};
574};
575
Brian Silvermanb601d892015-12-20 18:24:38 -0500576class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700577 public:
578 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
579 left_drivetrain_talon_ = ::std::move(t);
580 }
581
582 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
583 right_drivetrain_talon_ = ::std::move(t);
584 }
585
586 private:
587 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500588 ::y2014::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700589 }
590
591 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500592 auto &queue = ::y2014::control_loops::drivetrain_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700593 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700594 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
595 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700596 }
597
598 virtual void Stop() override {
599 LOG(WARNING, "drivetrain output too old\n");
600 left_drivetrain_talon_->Disable();
601 right_drivetrain_talon_->Disable();
602 }
603
604 ::std::unique_ptr<Talon> left_drivetrain_talon_;
605 ::std::unique_ptr<Talon> right_drivetrain_talon_;
606};
607
Brian Silvermanb601d892015-12-20 18:24:38 -0500608class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700609 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700610 void set_shooter_talon(::std::unique_ptr<Talon> t) {
611 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700612 }
613
614 private:
615 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500616 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700617 }
618
619 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500620 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700621 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700622 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700623 }
624
625 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700626 LOG(WARNING, "shooter output too old\n");
627 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700628 }
629
Brian Silverman552350b2015-08-02 18:23:34 -0700630 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700631};
632
Brian Silvermanb601d892015-12-20 18:24:38 -0500633class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700634 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700635 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
636 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700637 }
638
Brian Silverman552350b2015-08-02 18:23:34 -0700639 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
640 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700641 }
642
Brian Silverman552350b2015-08-02 18:23:34 -0700643 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
644 left_tusk_talon_ = ::std::move(t);
645 }
646
647 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
648 right_tusk_talon_ = ::std::move(t);
649 }
650
651 void set_intake1_talon(::std::unique_ptr<Talon> t) {
652 intake1_talon_ = ::std::move(t);
653 }
654
655 void set_intake2_talon(::std::unique_ptr<Talon> t) {
656 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700657 }
658
659 private:
660 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500661 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700662 }
663
664 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500665 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700666 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700667 intake1_talon_->Set(queue->intake_voltage / 12.0);
668 intake2_talon_->Set(queue->intake_voltage / 12.0);
669 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
670 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
671 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
672 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700673 }
674
675 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700676 LOG(WARNING, "claw output too old\n");
677 intake1_talon_->Disable();
678 intake2_talon_->Disable();
679 bottom_claw_talon_->Disable();
680 top_claw_talon_->Disable();
681 left_tusk_talon_->Disable();
682 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700683 }
684
Brian Silverman552350b2015-08-02 18:23:34 -0700685 ::std::unique_ptr<Talon> top_claw_talon_;
686 ::std::unique_ptr<Talon> bottom_claw_talon_;
687 ::std::unique_ptr<Talon> left_tusk_talon_;
688 ::std::unique_ptr<Talon> right_tusk_talon_;
689 ::std::unique_ptr<Talon> intake1_talon_;
690 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700691};
692
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800693class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700694 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700695 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700696 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
697 Encoder::k4X);
698 }
Brian Silverman552350b2015-08-02 18:23:34 -0700699
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800700 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700701 ::aos::InitNRT();
702 ::aos::SetCurrentThreadName("StartCompetition");
703
Brian Silvermanb601d892015-12-20 18:24:38 -0500704 ::frc971::wpilib::JoystickSender joystick_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700705 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700706
Brian Silverman425492b2015-12-30 15:23:55 -0800707 ::frc971::wpilib::PDPFetcher pdp_fetcher;
708 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
709 SensorReader reader(&pdp_fetcher);
Brian Silverman17f503e2015-08-02 18:17:18 -0700710
Brian Silverman85fbb602015-08-29 19:28:20 -0700711 // Create this first to make sure it ends up in one of the lower-numbered
712 // FPGA slots so we can use it with DMA.
713 auto shooter_encoder_temp = make_encoder(2);
714
Brian Silverman552350b2015-08-02 18:23:34 -0700715 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700716
Brian Silverman552350b2015-08-02 18:23:34 -0700717 reader.set_drivetrain_left_encoder(make_encoder(0));
718 reader.set_drivetrain_right_encoder(make_encoder(1));
719 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
720 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
721 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
722 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700723
Brian Silverman552350b2015-08-02 18:23:34 -0700724 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800725 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
726 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
727 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700728
Brian Silverman85fbb602015-08-29 19:28:20 -0700729 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800730 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
731 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
732 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700733
Brian Silverman85fbb602015-08-29 19:28:20 -0700734 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800735 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
736 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
737 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
738 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700739
Brian Silverman17f503e2015-08-02 18:17:18 -0700740 reader.set_dma(make_unique<DMA>());
741 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700742
Brian Silvermanb601d892015-12-20 18:24:38 -0500743 ::frc971::wpilib::GyroSender gyro_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700744 ::std::thread gyro_thread(::std::ref(gyro_sender));
745
746 DrivetrainWriter drivetrain_writer;
747 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700748 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700749 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700750 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700751 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
752
Brian Silvermanb601d892015-12-20 18:24:38 -0500753 ::y2014::wpilib::ClawWriter claw_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700754 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
755 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
756 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
757 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
758 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
759 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700760 ::std::thread claw_writer_thread(::std::ref(claw_writer));
761
Brian Silvermanb601d892015-12-20 18:24:38 -0500762 ::y2014::wpilib::ShooterWriter shooter_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700763 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
764 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
765
Brian Silverman17f503e2015-08-02 18:17:18 -0700766 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
767 new ::frc971::wpilib::BufferedPcm());
768 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700769 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
770 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
771 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
772 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700773
Austin Schuh016a6b02015-10-08 06:41:14 +0000774 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700775 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
776 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
777
778 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800779 while (true) {
780 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
781 if (r != 0) {
782 PLOG(WARNING, "infinite select failed");
783 } else {
784 PLOG(WARNING, "infinite select succeeded??\n");
785 }
786 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700787
788 LOG(ERROR, "Exiting WPILibRobot\n");
789
790 joystick_sender.Quit();
791 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800792 pdp_fetcher.Quit();
793 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700794 reader.Quit();
795 reader_thread.join();
796 gyro_sender.Quit();
797 gyro_thread.join();
798
799 drivetrain_writer.Quit();
800 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700801 shooter_writer.Quit();
802 shooter_writer_thread.join();
803 claw_writer.Quit();
804 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700805 solenoid_writer.Quit();
806 solenoid_thread.join();
807
808 ::aos::Cleanup();
809 }
810};
811
812} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500813} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700814
815
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800816AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);