blob: 776b68187aca9a5752b8912ad272ea6ddaf877b0 [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
10#include "aos/common/logging/logging.h"
11#include "aos/common/logging/queue_logging.h"
12#include "aos/common/time.h"
13#include "aos/common/util/log_interval.h"
14#include "aos/common/util/phased_loop.h"
15#include "aos/common/util/wrapping_counter.h"
16#include "aos/common/stl_mutex.h"
17#include "aos/linux_code/init.h"
18#include "aos/common/messages/robot_state.q.h"
19
Brian Silverman552350b2015-08-02 18:23:34 -070020#include "frc971/shifter_hall_effect.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070021#include "y2014/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070022#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070023#include "y2014/control_loops/shooter/shooter.q.h"
24#include "y2014/constants.h"
25#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070026
27#include "frc971/wpilib/hall_effect.h"
28#include "frc971/wpilib/joystick_sender.h"
29#include "frc971/wpilib/loop_output_handler.h"
30#include "frc971/wpilib/buffered_solenoid.h"
31#include "frc971/wpilib/buffered_pcm.h"
32#include "frc971/wpilib/gyro_sender.h"
33#include "frc971/wpilib/dma_edge_counting.h"
34#include "frc971/wpilib/interrupt_edge_counting.h"
35#include "frc971/wpilib/encoder_and_potentiometer.h"
36#include "frc971/wpilib/logging.q.h"
37
38#include "Encoder.h"
39#include "Talon.h"
40#include "DriverStation.h"
41#include "AnalogInput.h"
42#include "Compressor.h"
43#include "Relay.h"
44#include "RobotBase.h"
45#include "dma.h"
46#include "ControllerPower.h"
47
48#ifndef M_PI
49#define M_PI 3.14159265358979323846
50#endif
51
Brian Silverman17f503e2015-08-02 18:17:18 -070052using ::frc971::control_loops::drivetrain_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070053using ::frc971::control_loops::claw_queue;
Brian Silverman552350b2015-08-02 18:23:34 -070054using ::frc971::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070055
56namespace frc971 {
57namespace wpilib {
58
Brian Silverman552350b2015-08-02 18:23:34 -070059// TODO(brian): Replace this with ::std::make_unique once all our toolchains
60// have support.
61template <class T, class... U>
62std::unique_ptr<T> make_unique(U &&... u) {
63 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
64}
65
Brian Silverman17f503e2015-08-02 18:17:18 -070066double drivetrain_translate(int32_t in) {
67 return static_cast<double>(in) /
68 (256.0 /*cpr*/ * 4.0 /*4x*/) *
69 constants::GetValues().drivetrain_encoder_ratio *
Brian Silverman552350b2015-08-02 18:23:34 -070070 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
Brian Silverman17f503e2015-08-02 18:17:18 -070071}
72
Brian Silverman552350b2015-08-02 18:23:34 -070073float hall_translate(const constants::ShifterHallEffect &k, float in_low,
74 float in_high) {
75 const float low_ratio =
76 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
77 static_cast<float>(k.low_gear_middle - k.low_gear_low);
78 const float high_ratio =
79 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
80 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070081
Brian Silverman552350b2015-08-02 18:23:34 -070082 // Return low when we are below 1/2, and high when we are above 1/2.
83 if (low_ratio + high_ratio < 1.0) {
84 return low_ratio;
85 } else {
86 return high_ratio;
87 }
Brian Silverman17f503e2015-08-02 18:17:18 -070088}
89
90double claw_translate(int32_t in) {
Brian Silverman552350b2015-08-02 18:23:34 -070091 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
92 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
93 (M_PI / 180.0);
Brian Silverman17f503e2015-08-02 18:17:18 -070094}
95
Brian Silverman552350b2015-08-02 18:23:34 -070096double shooter_translate(int32_t in) {
97 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
98 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
99 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700100}
101
102static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700103 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
104 18.0 / 32.0 /* big belt reduction */ *
105 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
106 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700107
108class SensorReader {
109 public:
110 SensorReader() {
111 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
112 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700113 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700114 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700115 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700116 }
117
Brian Silverman552350b2015-08-02 18:23:34 -0700118 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
119 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700120 }
121
Brian Silverman552350b2015-08-02 18:23:34 -0700122 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
123 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 }
125
Brian Silverman552350b2015-08-02 18:23:34 -0700126 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
127 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
129
Brian Silverman552350b2015-08-02 18:23:34 -0700130 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
131 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
135 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 }
137
Brian Silverman552350b2015-08-02 18:23:34 -0700138 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
139 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 }
141
Brian Silverman552350b2015-08-02 18:23:34 -0700142 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
143 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 }
145
Brian Silverman552350b2015-08-02 18:23:34 -0700146 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
147 encoder_filter_.Add(encoder.get());
148 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700149 }
150
Brian Silverman552350b2015-08-02 18:23:34 -0700151 void set_top_claw_front_hall(::std::unique_ptr<DigitalSource> hall) {
152 hall_filter_.Add(hall.get());
153 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 }
155
Brian Silverman552350b2015-08-02 18:23:34 -0700156 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalSource> hall) {
157 hall_filter_.Add(hall.get());
158 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700159 }
160
Brian Silverman552350b2015-08-02 18:23:34 -0700161 void set_top_claw_back_hall(::std::unique_ptr<DigitalSource> hall) {
162 hall_filter_.Add(hall.get());
163 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700164 }
165
Brian Silverman552350b2015-08-02 18:23:34 -0700166 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
167 encoder_filter_.Add(encoder.get());
168 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700169 }
170
Brian Silverman552350b2015-08-02 18:23:34 -0700171 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalSource> hall) {
172 hall_filter_.Add(hall.get());
173 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700174 }
175
Brian Silverman552350b2015-08-02 18:23:34 -0700176 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalSource> hall) {
177 hall_filter_.Add(hall.get());
178 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700179 }
180
Brian Silverman552350b2015-08-02 18:23:34 -0700181 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalSource> hall) {
182 hall_filter_.Add(hall.get());
183 bottom_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_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
187 encoder_filter_.Add(encoder.get());
188 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700189 }
190
Brian Silverman552350b2015-08-02 18:23:34 -0700191 void set_shooter_proximal(::std::unique_ptr<DigitalSource> hall) {
192 hall_filter_.Add(hall.get());
193 shooter_proximal_ = ::std::move(hall);
194 }
195
196 void set_shooter_distal(::std::unique_ptr<DigitalSource> hall) {
197 hall_filter_.Add(hall.get());
198 shooter_distal_ = ::std::move(hall);
199 }
200
201 void set_shooter_plunger(::std::unique_ptr<DigitalSource> hall) {
202 hall_filter_.Add(hall.get());
203 shooter_plunger_ = ::std::move(hall);
204 shooter_plunger_reader_ =
205 make_unique<DMADigitalReader>(shooter_plunger_.get());
206 }
207
208 void set_shooter_latch(::std::unique_ptr<DigitalSource> hall) {
209 hall_filter_.Add(hall.get());
210 shooter_latch_ = ::std::move(hall);
211 shooter_latch_reader_ = make_unique<DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700212 }
213
214 // All of the DMA-related set_* calls must be made before this, and it doesn't
215 // hurt to do all of them.
216 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silverman552350b2015-08-02 18:23:34 -0700217 shooter_proximal_counter_ = make_unique<DMAEdgeCounter>(
218 shooter_encoder_.get(), shooter_proximal_.get());
219 shooter_distal_counter_ = make_unique<DMAEdgeCounter>(
220 shooter_encoder_.get(), shooter_distal_.get());
221
Brian Silverman17f503e2015-08-02 18:17:18 -0700222 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700223 dma_synchronizer_->Add(shooter_proximal_counter_.get());
224 dma_synchronizer_->Add(shooter_distal_counter_.get());
225 dma_synchronizer_->Add(shooter_plunger_reader_.get());
226 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700227 }
228
229 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700230 ::aos::SetCurrentThreadName("SensorReader");
Brian Silverman552350b2015-08-02 18:23:34 -0700231 LOG(INFO, "In sensor reader thread\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700232
233 my_pid_ = getpid();
234 ds_ = DriverStation::GetInstance();
235
Brian Silverman552350b2015-08-02 18:23:34 -0700236 top_reader_.Start();
237 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700238 dma_synchronizer_->Start();
239 LOG(INFO, "Things are now started\n");
240
241 ::aos::SetCurrentThreadRealtimePriority(kPriority);
242 while (run_) {
Brian Silverman552350b2015-08-02 18:23:34 -0700243 ::aos::time::PhasedLoopXMS(10, 9000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700244 RunIteration();
245 }
246
Brian Silverman552350b2015-08-02 18:23:34 -0700247 top_reader_.Quit();
248 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700249 }
250
251 void RunIteration() {
252 {
253 auto new_state = ::aos::robot_state.MakeMessage();
254
255 new_state->reader_pid = my_pid_;
256 new_state->outputs_enabled = ds_->IsSysActive();
257 new_state->browned_out = ds_->IsSysBrownedOut();
258
259 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
260 new_state->is_5v_active = ControllerPower::GetEnabled5V();
261 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
262 new_state->voltage_5v = ControllerPower::GetVoltage5V();
263
264 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
265 new_state->voltage_battery = ds_->GetBatteryVoltage();
266
267 LOG_STRUCT(DEBUG, "robot_state", *new_state);
268
269 new_state.Send();
270 }
271
Brian Silverman552350b2015-08-02 18:23:34 -0700272 const auto &values = constants::GetValues();
273
Brian Silverman17f503e2015-08-02 18:17:18 -0700274 {
275 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
276 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700277 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700278 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700279 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
280 drivetrain_message->left_shifter_position =
281 hall_translate(values.left_drive, low_left_drive_hall_->GetVoltage(),
282 high_left_drive_hall_->GetVoltage());
283 drivetrain_message->right_shifter_position = hall_translate(
284 values.right_drive, low_right_drive_hall_->GetVoltage(),
285 high_right_drive_hall_->GetVoltage());
Brian Silverman17f503e2015-08-02 18:17:18 -0700286
287 drivetrain_message.Send();
288 }
289
Brian Silverman552350b2015-08-02 18:23:34 -0700290 ::frc971::sensors::auto_mode.MakeWithBuilder()
291 .voltage(auto_selector_analog_->GetVoltage())
292 .Send();
293
Brian Silverman17f503e2015-08-02 18:17:18 -0700294 dma_synchronizer_->RunIteration();
295
Brian Silverman17f503e2015-08-02 18:17:18 -0700296 {
Brian Silverman552350b2015-08-02 18:23:34 -0700297 auto shooter_message = shooter_queue.position.MakeMessage();
298 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
299 shooter_message->plunger = shooter_plunger_reader_->value();
300 shooter_message->latch = shooter_latch_reader_->value();
301 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
302 &shooter_message->pusher_proximal);
303 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
304 &shooter_message->pusher_distal);
305
306 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700307 }
308
309 {
310 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700311 top_reader_.RunIteration(&claw_message->top);
312 bottom_reader_.RunIteration(&claw_message->bottom);
313
Brian Silverman17f503e2015-08-02 18:17:18 -0700314 claw_message.Send();
315 }
316 }
317
318 void Quit() { run_ = false; }
319
320 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700321 class HalfClawReader {
322 public:
323 HalfClawReader(bool reversed) : reversed_(reversed) {}
324
325 void set_encoder(::std::unique_ptr<Encoder> encoder) {
326 encoder_ = ::std::move(encoder);
327 }
328
329 void set_front_hall(::std::unique_ptr<DigitalSource> front_hall) {
330 front_hall_ = ::std::move(front_hall);
331 }
332
333 void set_calibration_hall(
334 ::std::unique_ptr<DigitalSource> calibration_hall) {
335 calibration_hall_ = ::std::move(calibration_hall);
336 }
337
338 void set_back_hall(::std::unique_ptr<DigitalSource> back_hall) {
339 back_hall_ = ::std::move(back_hall);
340 }
341
342 void Start() {
343 front_counter_ =
344 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
345 synchronizer_.Add(front_counter_.get());
346 calibration_counter_ =
347 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
348 synchronizer_.Add(calibration_counter_.get());
349 back_counter_ =
350 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
351 synchronizer_.Add(back_counter_.get());
352 synchronized_encoder_ =
353 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
354 synchronizer_.Add(synchronized_encoder_.get());
355
356 synchronizer_.Start();
357 }
358
359 void Quit() { synchronizer_.Quit(); }
360
361 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
362 const double multiplier = reversed_ ? -1.0 : 1.0;
363
364 synchronizer_.RunIteration();
365
366 CopyPosition(front_counter_.get(), &half_claw_position->front);
367 CopyPosition(calibration_counter_.get(),
368 &half_claw_position->calibration);
369 CopyPosition(back_counter_.get(), &half_claw_position->back);
370 half_claw_position->position =
371 multiplier * claw_translate(synchronized_encoder_->get());
372 }
373
374 private:
375 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
376 const double multiplier = reversed_ ? -1.0 : 1.0;
377
378 out->current = counter->polled_value();
379 out->posedge_count = counter->positive_interrupt_count();
380 out->negedge_count = counter->negative_interrupt_count();
381 out->posedge_value =
382 multiplier * claw_translate(counter->last_positive_encoder_value());
383 out->negedge_value =
384 multiplier * claw_translate(counter->last_negative_encoder_value());
385 }
386
387 InterruptSynchronizer synchronizer_{kInterruptPriority};
388
389 ::std::unique_ptr<EdgeCounter> front_counter_;
390 ::std::unique_ptr<EdgeCounter> calibration_counter_;
391 ::std::unique_ptr<EdgeCounter> back_counter_;
392 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
393
394 ::std::unique_ptr<Encoder> encoder_;
395 ::std::unique_ptr<DigitalSource> front_hall_;
396 ::std::unique_ptr<DigitalSource> calibration_hall_;
397 ::std::unique_ptr<DigitalSource> back_hall_;
398
399 const bool reversed_;
400 };
401
Brian Silverman17f503e2015-08-02 18:17:18 -0700402 static const int kPriority = 30;
403 static const int kInterruptPriority = 55;
404
Brian Silverman552350b2015-08-02 18:23:34 -0700405 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
406 PosedgeOnlyCountedHallEffectStruct *output) {
407 output->current = counter->polled_value();
408 output->posedge_count = counter->positive_count();
409 output->negedge_count = counter->negative_count();
410 output->posedge_value =
411 shooter_translate(counter->last_positive_encoder_value());
412 }
413
Brian Silverman17f503e2015-08-02 18:17:18 -0700414 int32_t my_pid_;
415 DriverStation *ds_;
416
Brian Silverman17f503e2015-08-02 18:17:18 -0700417 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
418
Brian Silverman552350b2015-08-02 18:23:34 -0700419 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700420
Brian Silverman552350b2015-08-02 18:23:34 -0700421 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
422 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
423 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
424 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
425 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
426 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700427
Brian Silverman552350b2015-08-02 18:23:34 -0700428 HalfClawReader top_reader_{false}, bottom_reader_{true};
429
430 ::std::unique_ptr<Encoder> shooter_encoder_;
431 ::std::unique_ptr<DigitalSource> shooter_proximal_, shooter_distal_;
432 ::std::unique_ptr<DigitalSource> shooter_plunger_, shooter_latch_;
433 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
434 shooter_distal_counter_;
435 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
436 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700437
438 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700439 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700440};
441
442class SolenoidWriter {
443 public:
444 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
445 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700446 shooter_(".frc971.control_loops.shooter_queue.output"),
447 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700448
449 void set_pressure_switch(::std::unique_ptr<DigitalSource> pressure_switch) {
450 pressure_switch_ = ::std::move(pressure_switch);
451 }
452
453 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
454 compressor_relay_ = ::std::move(compressor_relay);
455 }
456
Brian Silverman552350b2015-08-02 18:23:34 -0700457 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
458 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700459 }
460
Brian Silverman552350b2015-08-02 18:23:34 -0700461 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
462 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700463 }
464
Brian Silverman552350b2015-08-02 18:23:34 -0700465 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
466 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 }
468
Brian Silverman552350b2015-08-02 18:23:34 -0700469 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
470 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700471 }
472
473 void operator()() {
474 ::aos::SetCurrentThreadName("Solenoids");
475 ::aos::SetCurrentThreadRealtimePriority(30);
476
477 while (run_) {
478 ::aos::time::PhasedLoopXMS(20, 1000);
479
480 {
Brian Silverman552350b2015-08-02 18:23:34 -0700481 shooter_.FetchLatest();
482 if (shooter_.get()) {
483 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
484 shooter_latch_->Set(!shooter_->latch_piston);
485 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700486 }
487 }
488
489 {
Brian Silverman552350b2015-08-02 18:23:34 -0700490 drivetrain_.FetchLatest();
491 if (drivetrain_.get()) {
492 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
493 drivetrain_left_->Set(drivetrain_->left_high);
494 drivetrain_right_->Set(drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700495 }
496 }
497
Brian Silverman17f503e2015-08-02 18:17:18 -0700498 {
499 PneumaticsToLog to_log;
500 {
501 const bool compressor_on = !pressure_switch_->Get();
502 to_log.compressor_on = compressor_on;
503 if (compressor_on) {
504 compressor_relay_->Set(Relay::kForward);
505 } else {
506 compressor_relay_->Set(Relay::kOff);
507 }
508 }
509
510 pcm_->Flush();
511 to_log.read_solenoids = pcm_->GetAll();
512 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
513 }
514 }
515 }
516
517 void Quit() { run_ = false; }
518
519 private:
520 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700521
522 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
523 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
524 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
525 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
526
Brian Silverman17f503e2015-08-02 18:17:18 -0700527 ::std::unique_ptr<DigitalSource> pressure_switch_;
528 ::std::unique_ptr<Relay> compressor_relay_;
529
Brian Silverman552350b2015-08-02 18:23:34 -0700530 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
531 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700532
533 ::std::atomic<bool> run_{true};
534};
535
536class DrivetrainWriter : public LoopOutputHandler {
537 public:
538 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
539 left_drivetrain_talon_ = ::std::move(t);
540 }
541
542 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
543 right_drivetrain_talon_ = ::std::move(t);
544 }
545
546 private:
547 virtual void Read() override {
548 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
549 }
550
551 virtual void Write() override {
552 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
553 LOG_STRUCT(DEBUG, "will output", *queue);
554 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
555 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
556 }
557
558 virtual void Stop() override {
559 LOG(WARNING, "drivetrain output too old\n");
560 left_drivetrain_talon_->Disable();
561 right_drivetrain_talon_->Disable();
562 }
563
564 ::std::unique_ptr<Talon> left_drivetrain_talon_;
565 ::std::unique_ptr<Talon> right_drivetrain_talon_;
566};
567
Brian Silverman552350b2015-08-02 18:23:34 -0700568class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700569 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700570 void set_shooter_talon(::std::unique_ptr<Talon> t) {
571 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700572 }
573
574 private:
575 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400576 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700577 }
578
579 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400580 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700581 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700582 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700583 }
584
585 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700586 LOG(WARNING, "shooter output too old\n");
587 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700588 }
589
Brian Silverman552350b2015-08-02 18:23:34 -0700590 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700591};
592
593class ClawWriter : public LoopOutputHandler {
594 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700595 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
596 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700597 }
598
Brian Silverman552350b2015-08-02 18:23:34 -0700599 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
600 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700601 }
602
Brian Silverman552350b2015-08-02 18:23:34 -0700603 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
604 left_tusk_talon_ = ::std::move(t);
605 }
606
607 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
608 right_tusk_talon_ = ::std::move(t);
609 }
610
611 void set_intake1_talon(::std::unique_ptr<Talon> t) {
612 intake1_talon_ = ::std::move(t);
613 }
614
615 void set_intake2_talon(::std::unique_ptr<Talon> t) {
616 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700617 }
618
619 private:
620 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400621 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700622 }
623
624 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400625 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700626 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700627 intake1_talon_->Set(queue->intake_voltage / 12.0);
628 intake2_talon_->Set(queue->intake_voltage / 12.0);
629 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
630 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
631 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
632 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700633 }
634
635 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700636 LOG(WARNING, "claw output too old\n");
637 intake1_talon_->Disable();
638 intake2_talon_->Disable();
639 bottom_claw_talon_->Disable();
640 top_claw_talon_->Disable();
641 left_tusk_talon_->Disable();
642 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700643 }
644
Brian Silverman552350b2015-08-02 18:23:34 -0700645 ::std::unique_ptr<Talon> top_claw_talon_;
646 ::std::unique_ptr<Talon> bottom_claw_talon_;
647 ::std::unique_ptr<Talon> left_tusk_talon_;
648 ::std::unique_ptr<Talon> right_tusk_talon_;
649 ::std::unique_ptr<Talon> intake1_talon_;
650 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700651};
652
Brian Silverman17f503e2015-08-02 18:17:18 -0700653class WPILibRobot : public RobotBase {
654 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700655 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700656 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
657 Encoder::k4X);
658 }
Brian Silverman552350b2015-08-02 18:23:34 -0700659
Brian Silverman17f503e2015-08-02 18:17:18 -0700660 virtual void StartCompetition() {
661 ::aos::InitNRT();
662 ::aos::SetCurrentThreadName("StartCompetition");
663
664 JoystickSender joystick_sender;
665 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700666
667 SensorReader reader;
668 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700669
Brian Silverman552350b2015-08-02 18:23:34 -0700670 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700671
Brian Silverman552350b2015-08-02 18:23:34 -0700672 reader.set_drivetrain_left_encoder(make_encoder(0));
673 reader.set_drivetrain_right_encoder(make_encoder(1));
674 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
675 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
676 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
677 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700678
Brian Silverman552350b2015-08-02 18:23:34 -0700679 reader.set_top_claw_encoder(make_encoder(3));
680 reader.set_top_claw_front_hall(make_unique<HallEffect>(4)); // R2
681 reader.set_top_claw_calibration_hall(make_unique<HallEffect>(3)); // R3
682 reader.set_top_claw_back_hall(make_unique<HallEffect>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700683
Brian Silverman552350b2015-08-02 18:23:34 -0700684 reader.set_bottom_claw_encoder(make_encoder(5));
685 reader.set_bottom_claw_front_hall(make_unique<HallEffect>(1)); // L2
686 reader.set_bottom_claw_calibration_hall(make_unique<HallEffect>(0)); // L3
687 reader.set_bottom_claw_back_hall(make_unique<HallEffect>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700688
Brian Silverman552350b2015-08-02 18:23:34 -0700689 reader.set_shooter_encoder(make_encoder(2));
690 reader.set_shooter_proximal(make_unique<HallEffect>(6)); // S1
691 reader.set_shooter_distal(make_unique<HallEffect>(7)); // S2
692 reader.set_shooter_plunger(make_unique<HallEffect>(8)); // S3
693 reader.set_shooter_latch(make_unique<HallEffect>(9)); // S4
694
Brian Silverman17f503e2015-08-02 18:17:18 -0700695 reader.set_dma(make_unique<DMA>());
696 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700697
Brian Silverman17f503e2015-08-02 18:17:18 -0700698 GyroSender gyro_sender;
699 ::std::thread gyro_thread(::std::ref(gyro_sender));
700
701 DrivetrainWriter drivetrain_writer;
702 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700703 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700704 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700705 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700706 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
707
Brian Silverman552350b2015-08-02 18:23:34 -0700708 ::frc971::wpilib::ClawWriter claw_writer;
709 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
710 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
711 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
712 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
713 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
714 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700715 ::std::thread claw_writer_thread(::std::ref(claw_writer));
716
Brian Silverman552350b2015-08-02 18:23:34 -0700717 ::frc971::wpilib::ShooterWriter shooter_writer;
718 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
719 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
720
Brian Silverman17f503e2015-08-02 18:17:18 -0700721 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
722 new ::frc971::wpilib::BufferedPcm());
723 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700724 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
725 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
726 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
727 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700728
729 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
730 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
731 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
732
733 // Wait forever. Not much else to do...
734 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
735
736 LOG(ERROR, "Exiting WPILibRobot\n");
737
738 joystick_sender.Quit();
739 joystick_thread.join();
740 reader.Quit();
741 reader_thread.join();
742 gyro_sender.Quit();
743 gyro_thread.join();
744
745 drivetrain_writer.Quit();
746 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700747 shooter_writer.Quit();
748 shooter_writer_thread.join();
749 claw_writer.Quit();
750 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700751 solenoid_writer.Quit();
752 solenoid_thread.join();
753
754 ::aos::Cleanup();
755 }
756};
757
758} // namespace wpilib
759} // namespace frc971
760
761
762START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);