blob: 107a18ca227a78da4b604a059853c4575e83014b [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 Silverman85fbb602015-08-29 19:28:20 -070059// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
60// DMA stuff and then removing the * 2.0 in *_translate.
61// The low bit is direction.
62
Brian Silverman552350b2015-08-02 18:23:34 -070063// TODO(brian): Replace this with ::std::make_unique once all our toolchains
64// have support.
65template <class T, class... U>
66std::unique_ptr<T> make_unique(U &&... u) {
67 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
68}
69
Brian Silverman17f503e2015-08-02 18:17:18 -070070double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070071 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070072 (256.0 /*cpr*/ * 4.0 /*4x*/) *
73 constants::GetValues().drivetrain_encoder_ratio *
Brian Silverman85fbb602015-08-29 19:28:20 -070074 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070075}
76
Brian Silverman552350b2015-08-02 18:23:34 -070077float hall_translate(const constants::ShifterHallEffect &k, float in_low,
78 float in_high) {
79 const float low_ratio =
80 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
81 static_cast<float>(k.low_gear_middle - k.low_gear_low);
82 const float high_ratio =
83 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
84 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070085
Brian Silverman552350b2015-08-02 18:23:34 -070086 // Return low when we are below 1/2, and high when we are above 1/2.
87 if (low_ratio + high_ratio < 1.0) {
88 return low_ratio;
89 } else {
90 return high_ratio;
91 }
Brian Silverman17f503e2015-08-02 18:17:18 -070092}
93
94double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070095 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -070096 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -070097 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070098}
99
Brian Silverman552350b2015-08-02 18:23:34 -0700100double shooter_translate(int32_t in) {
101 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
102 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
103 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700104}
105
106static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700107 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
108 18.0 / 32.0 /* big belt reduction */ *
109 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
110 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700111
112class SensorReader {
113 public:
114 SensorReader() {
115 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
116 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700117 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700118 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700119 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700120 }
121
Brian Silverman552350b2015-08-02 18:23:34 -0700122 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
123 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 }
125
Brian Silverman552350b2015-08-02 18:23:34 -0700126 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
127 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
129
Brian Silverman552350b2015-08-02 18:23:34 -0700130 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
131 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
135 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 }
137
Brian Silverman552350b2015-08-02 18:23:34 -0700138 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
139 low_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_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
143 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 }
145
Brian Silverman552350b2015-08-02 18:23:34 -0700146 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
147 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Brian Silverman552350b2015-08-02 18:23:34 -0700150 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
151 encoder_filter_.Add(encoder.get());
152 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700153 }
154
Brian Silverman552350b2015-08-02 18:23:34 -0700155 void set_top_claw_front_hall(::std::unique_ptr<DigitalSource> hall) {
156 hall_filter_.Add(hall.get());
157 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700158 }
159
Brian Silverman552350b2015-08-02 18:23:34 -0700160 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalSource> hall) {
161 hall_filter_.Add(hall.get());
162 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700163 }
164
Brian Silverman552350b2015-08-02 18:23:34 -0700165 void set_top_claw_back_hall(::std::unique_ptr<DigitalSource> hall) {
166 hall_filter_.Add(hall.get());
167 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700168 }
169
Brian Silverman552350b2015-08-02 18:23:34 -0700170 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
171 encoder_filter_.Add(encoder.get());
172 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700173 }
174
Brian Silverman552350b2015-08-02 18:23:34 -0700175 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalSource> hall) {
176 hall_filter_.Add(hall.get());
177 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700178 }
179
Brian Silverman552350b2015-08-02 18:23:34 -0700180 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalSource> hall) {
181 hall_filter_.Add(hall.get());
182 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700183 }
184
Brian Silverman552350b2015-08-02 18:23:34 -0700185 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalSource> hall) {
186 hall_filter_.Add(hall.get());
187 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 }
189
Brian Silverman552350b2015-08-02 18:23:34 -0700190 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
191 encoder_filter_.Add(encoder.get());
192 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700193 }
194
Brian Silverman552350b2015-08-02 18:23:34 -0700195 void set_shooter_proximal(::std::unique_ptr<DigitalSource> hall) {
196 hall_filter_.Add(hall.get());
197 shooter_proximal_ = ::std::move(hall);
198 }
199
200 void set_shooter_distal(::std::unique_ptr<DigitalSource> hall) {
201 hall_filter_.Add(hall.get());
202 shooter_distal_ = ::std::move(hall);
203 }
204
205 void set_shooter_plunger(::std::unique_ptr<DigitalSource> hall) {
206 hall_filter_.Add(hall.get());
207 shooter_plunger_ = ::std::move(hall);
208 shooter_plunger_reader_ =
209 make_unique<DMADigitalReader>(shooter_plunger_.get());
210 }
211
212 void set_shooter_latch(::std::unique_ptr<DigitalSource> hall) {
213 hall_filter_.Add(hall.get());
214 shooter_latch_ = ::std::move(hall);
215 shooter_latch_reader_ = make_unique<DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700216 }
217
218 // All of the DMA-related set_* calls must be made before this, and it doesn't
219 // hurt to do all of them.
220 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silverman552350b2015-08-02 18:23:34 -0700221 shooter_proximal_counter_ = make_unique<DMAEdgeCounter>(
222 shooter_encoder_.get(), shooter_proximal_.get());
223 shooter_distal_counter_ = make_unique<DMAEdgeCounter>(
224 shooter_encoder_.get(), shooter_distal_.get());
225
Brian Silverman17f503e2015-08-02 18:17:18 -0700226 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700227 dma_synchronizer_->Add(shooter_proximal_counter_.get());
228 dma_synchronizer_->Add(shooter_distal_counter_.get());
229 dma_synchronizer_->Add(shooter_plunger_reader_.get());
230 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700231 }
232
233 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700234 ::aos::SetCurrentThreadName("SensorReader");
Brian Silverman552350b2015-08-02 18:23:34 -0700235 LOG(INFO, "In sensor reader thread\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700236
237 my_pid_ = getpid();
238 ds_ = DriverStation::GetInstance();
239
Brian Silverman552350b2015-08-02 18:23:34 -0700240 top_reader_.Start();
241 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700242 dma_synchronizer_->Start();
243 LOG(INFO, "Things are now started\n");
244
245 ::aos::SetCurrentThreadRealtimePriority(kPriority);
246 while (run_) {
Brian Silverman552350b2015-08-02 18:23:34 -0700247 ::aos::time::PhasedLoopXMS(10, 9000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700248 RunIteration();
249 }
250
Brian Silverman552350b2015-08-02 18:23:34 -0700251 top_reader_.Quit();
252 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700253 }
254
255 void RunIteration() {
256 {
257 auto new_state = ::aos::robot_state.MakeMessage();
258
259 new_state->reader_pid = my_pid_;
260 new_state->outputs_enabled = ds_->IsSysActive();
261 new_state->browned_out = ds_->IsSysBrownedOut();
262
263 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
264 new_state->is_5v_active = ControllerPower::GetEnabled5V();
265 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
266 new_state->voltage_5v = ControllerPower::GetVoltage5V();
267
268 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
269 new_state->voltage_battery = ds_->GetBatteryVoltage();
270
271 LOG_STRUCT(DEBUG, "robot_state", *new_state);
272
273 new_state.Send();
274 }
275
Brian Silverman552350b2015-08-02 18:23:34 -0700276 const auto &values = constants::GetValues();
277
Brian Silverman17f503e2015-08-02 18:17:18 -0700278 {
279 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
280 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700281 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700282 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700283 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
284 drivetrain_message->left_shifter_position =
285 hall_translate(values.left_drive, low_left_drive_hall_->GetVoltage(),
286 high_left_drive_hall_->GetVoltage());
287 drivetrain_message->right_shifter_position = hall_translate(
288 values.right_drive, low_right_drive_hall_->GetVoltage(),
289 high_right_drive_hall_->GetVoltage());
Brian Silverman17f503e2015-08-02 18:17:18 -0700290
291 drivetrain_message.Send();
292 }
293
Brian Silverman552350b2015-08-02 18:23:34 -0700294 ::frc971::sensors::auto_mode.MakeWithBuilder()
295 .voltage(auto_selector_analog_->GetVoltage())
296 .Send();
297
Brian Silverman17f503e2015-08-02 18:17:18 -0700298 dma_synchronizer_->RunIteration();
299
Brian Silverman17f503e2015-08-02 18:17:18 -0700300 {
Brian Silverman552350b2015-08-02 18:23:34 -0700301 auto shooter_message = shooter_queue.position.MakeMessage();
302 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
303 shooter_message->plunger = shooter_plunger_reader_->value();
304 shooter_message->latch = shooter_latch_reader_->value();
305 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
306 &shooter_message->pusher_proximal);
307 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
308 &shooter_message->pusher_distal);
309
310 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700311 }
312
313 {
314 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700315 top_reader_.RunIteration(&claw_message->top);
316 bottom_reader_.RunIteration(&claw_message->bottom);
317
Brian Silverman17f503e2015-08-02 18:17:18 -0700318 claw_message.Send();
319 }
320 }
321
322 void Quit() { run_ = false; }
323
324 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700325 class HalfClawReader {
326 public:
327 HalfClawReader(bool reversed) : reversed_(reversed) {}
328
329 void set_encoder(::std::unique_ptr<Encoder> encoder) {
330 encoder_ = ::std::move(encoder);
331 }
332
333 void set_front_hall(::std::unique_ptr<DigitalSource> front_hall) {
334 front_hall_ = ::std::move(front_hall);
335 }
336
337 void set_calibration_hall(
338 ::std::unique_ptr<DigitalSource> calibration_hall) {
339 calibration_hall_ = ::std::move(calibration_hall);
340 }
341
342 void set_back_hall(::std::unique_ptr<DigitalSource> back_hall) {
343 back_hall_ = ::std::move(back_hall);
344 }
345
346 void Start() {
347 front_counter_ =
348 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
349 synchronizer_.Add(front_counter_.get());
350 calibration_counter_ =
351 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
352 synchronizer_.Add(calibration_counter_.get());
353 back_counter_ =
354 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
355 synchronizer_.Add(back_counter_.get());
356 synchronized_encoder_ =
357 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
358 synchronizer_.Add(synchronized_encoder_.get());
359
360 synchronizer_.Start();
361 }
362
363 void Quit() { synchronizer_.Quit(); }
364
365 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
366 const double multiplier = reversed_ ? -1.0 : 1.0;
367
368 synchronizer_.RunIteration();
369
370 CopyPosition(front_counter_.get(), &half_claw_position->front);
371 CopyPosition(calibration_counter_.get(),
372 &half_claw_position->calibration);
373 CopyPosition(back_counter_.get(), &half_claw_position->back);
374 half_claw_position->position =
375 multiplier * claw_translate(synchronized_encoder_->get());
376 }
377
378 private:
379 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
380 const double multiplier = reversed_ ? -1.0 : 1.0;
381
382 out->current = counter->polled_value();
383 out->posedge_count = counter->positive_interrupt_count();
384 out->negedge_count = counter->negative_interrupt_count();
385 out->posedge_value =
386 multiplier * claw_translate(counter->last_positive_encoder_value());
387 out->negedge_value =
388 multiplier * claw_translate(counter->last_negative_encoder_value());
389 }
390
391 InterruptSynchronizer synchronizer_{kInterruptPriority};
392
393 ::std::unique_ptr<EdgeCounter> front_counter_;
394 ::std::unique_ptr<EdgeCounter> calibration_counter_;
395 ::std::unique_ptr<EdgeCounter> back_counter_;
396 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
397
398 ::std::unique_ptr<Encoder> encoder_;
399 ::std::unique_ptr<DigitalSource> front_hall_;
400 ::std::unique_ptr<DigitalSource> calibration_hall_;
401 ::std::unique_ptr<DigitalSource> back_hall_;
402
403 const bool reversed_;
404 };
405
Brian Silverman17f503e2015-08-02 18:17:18 -0700406 static const int kPriority = 30;
407 static const int kInterruptPriority = 55;
408
Brian Silverman552350b2015-08-02 18:23:34 -0700409 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
410 PosedgeOnlyCountedHallEffectStruct *output) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700411 // TODO(Brian): Remove HallEffect so current will get inverted too like
412 // everything else.
Brian Silverman552350b2015-08-02 18:23:34 -0700413 output->current = counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700414 // These are inverted because the hall effects give logical false when
415 // there's a magnet in front of them.
416 output->posedge_count = counter->negative_count();
417 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700418 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700419 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700420 }
421
Brian Silverman17f503e2015-08-02 18:17:18 -0700422 int32_t my_pid_;
423 DriverStation *ds_;
424
Brian Silverman17f503e2015-08-02 18:17:18 -0700425 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
426
Brian Silverman552350b2015-08-02 18:23:34 -0700427 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700428
Brian Silverman552350b2015-08-02 18:23:34 -0700429 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
430 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
431 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
432 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
433 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
434 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700435
Brian Silverman552350b2015-08-02 18:23:34 -0700436 HalfClawReader top_reader_{false}, bottom_reader_{true};
437
438 ::std::unique_ptr<Encoder> shooter_encoder_;
439 ::std::unique_ptr<DigitalSource> shooter_proximal_, shooter_distal_;
440 ::std::unique_ptr<DigitalSource> shooter_plunger_, shooter_latch_;
441 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
442 shooter_distal_counter_;
443 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
444 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700445
446 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700447 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700448};
449
450class SolenoidWriter {
451 public:
452 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
453 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700454 shooter_(".frc971.control_loops.shooter_queue.output"),
455 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700456
457 void set_pressure_switch(::std::unique_ptr<DigitalSource> pressure_switch) {
458 pressure_switch_ = ::std::move(pressure_switch);
459 }
460
461 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
462 compressor_relay_ = ::std::move(compressor_relay);
463 }
464
Brian Silverman552350b2015-08-02 18:23:34 -0700465 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
466 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 }
468
Brian Silverman552350b2015-08-02 18:23:34 -0700469 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
470 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700471 }
472
Brian Silverman552350b2015-08-02 18:23:34 -0700473 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
474 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700475 }
476
Brian Silverman552350b2015-08-02 18:23:34 -0700477 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
478 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700479 }
480
481 void operator()() {
482 ::aos::SetCurrentThreadName("Solenoids");
483 ::aos::SetCurrentThreadRealtimePriority(30);
484
485 while (run_) {
486 ::aos::time::PhasedLoopXMS(20, 1000);
487
488 {
Brian Silverman552350b2015-08-02 18:23:34 -0700489 shooter_.FetchLatest();
490 if (shooter_.get()) {
491 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
492 shooter_latch_->Set(!shooter_->latch_piston);
493 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700494 }
495 }
496
497 {
Brian Silverman552350b2015-08-02 18:23:34 -0700498 drivetrain_.FetchLatest();
499 if (drivetrain_.get()) {
500 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
501 drivetrain_left_->Set(drivetrain_->left_high);
502 drivetrain_right_->Set(drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700503 }
504 }
505
Brian Silverman17f503e2015-08-02 18:17:18 -0700506 {
507 PneumaticsToLog to_log;
508 {
509 const bool compressor_on = !pressure_switch_->Get();
510 to_log.compressor_on = compressor_on;
511 if (compressor_on) {
512 compressor_relay_->Set(Relay::kForward);
513 } else {
514 compressor_relay_->Set(Relay::kOff);
515 }
516 }
517
518 pcm_->Flush();
519 to_log.read_solenoids = pcm_->GetAll();
520 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
521 }
522 }
523 }
524
525 void Quit() { run_ = false; }
526
527 private:
528 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700529
530 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
531 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
532 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
533 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
534
Brian Silverman17f503e2015-08-02 18:17:18 -0700535 ::std::unique_ptr<DigitalSource> pressure_switch_;
536 ::std::unique_ptr<Relay> compressor_relay_;
537
Brian Silverman552350b2015-08-02 18:23:34 -0700538 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
539 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700540
541 ::std::atomic<bool> run_{true};
542};
543
544class DrivetrainWriter : public LoopOutputHandler {
545 public:
546 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
547 left_drivetrain_talon_ = ::std::move(t);
548 }
549
550 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
551 right_drivetrain_talon_ = ::std::move(t);
552 }
553
554 private:
555 virtual void Read() override {
556 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
557 }
558
559 virtual void Write() override {
560 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
561 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700562 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
563 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700564 }
565
566 virtual void Stop() override {
567 LOG(WARNING, "drivetrain output too old\n");
568 left_drivetrain_talon_->Disable();
569 right_drivetrain_talon_->Disable();
570 }
571
572 ::std::unique_ptr<Talon> left_drivetrain_talon_;
573 ::std::unique_ptr<Talon> right_drivetrain_talon_;
574};
575
Brian Silverman552350b2015-08-02 18:23:34 -0700576class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700577 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700578 void set_shooter_talon(::std::unique_ptr<Talon> t) {
579 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700580 }
581
582 private:
583 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400584 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700585 }
586
587 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400588 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700589 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700590 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700591 }
592
593 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700594 LOG(WARNING, "shooter output too old\n");
595 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700596 }
597
Brian Silverman552350b2015-08-02 18:23:34 -0700598 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700599};
600
601class ClawWriter : public LoopOutputHandler {
602 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700603 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
604 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700605 }
606
Brian Silverman552350b2015-08-02 18:23:34 -0700607 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
608 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700609 }
610
Brian Silverman552350b2015-08-02 18:23:34 -0700611 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
612 left_tusk_talon_ = ::std::move(t);
613 }
614
615 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
616 right_tusk_talon_ = ::std::move(t);
617 }
618
619 void set_intake1_talon(::std::unique_ptr<Talon> t) {
620 intake1_talon_ = ::std::move(t);
621 }
622
623 void set_intake2_talon(::std::unique_ptr<Talon> t) {
624 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700625 }
626
627 private:
628 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400629 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700630 }
631
632 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400633 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700634 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700635 intake1_talon_->Set(queue->intake_voltage / 12.0);
636 intake2_talon_->Set(queue->intake_voltage / 12.0);
637 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
638 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
639 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
640 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700641 }
642
643 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700644 LOG(WARNING, "claw output too old\n");
645 intake1_talon_->Disable();
646 intake2_talon_->Disable();
647 bottom_claw_talon_->Disable();
648 top_claw_talon_->Disable();
649 left_tusk_talon_->Disable();
650 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700651 }
652
Brian Silverman552350b2015-08-02 18:23:34 -0700653 ::std::unique_ptr<Talon> top_claw_talon_;
654 ::std::unique_ptr<Talon> bottom_claw_talon_;
655 ::std::unique_ptr<Talon> left_tusk_talon_;
656 ::std::unique_ptr<Talon> right_tusk_talon_;
657 ::std::unique_ptr<Talon> intake1_talon_;
658 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700659};
660
Brian Silverman17f503e2015-08-02 18:17:18 -0700661class WPILibRobot : public RobotBase {
662 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700663 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700664 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
665 Encoder::k4X);
666 }
Brian Silverman552350b2015-08-02 18:23:34 -0700667
Brian Silverman17f503e2015-08-02 18:17:18 -0700668 virtual void StartCompetition() {
669 ::aos::InitNRT();
670 ::aos::SetCurrentThreadName("StartCompetition");
671
672 JoystickSender joystick_sender;
673 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700674
675 SensorReader reader;
676 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700677
Brian Silverman85fbb602015-08-29 19:28:20 -0700678 // Create this first to make sure it ends up in one of the lower-numbered
679 // FPGA slots so we can use it with DMA.
680 auto shooter_encoder_temp = make_encoder(2);
681
Brian Silverman552350b2015-08-02 18:23:34 -0700682 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700683
Brian Silverman552350b2015-08-02 18:23:34 -0700684 reader.set_drivetrain_left_encoder(make_encoder(0));
685 reader.set_drivetrain_right_encoder(make_encoder(1));
686 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
687 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
688 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
689 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700690
Brian Silverman552350b2015-08-02 18:23:34 -0700691 reader.set_top_claw_encoder(make_encoder(3));
692 reader.set_top_claw_front_hall(make_unique<HallEffect>(4)); // R2
693 reader.set_top_claw_calibration_hall(make_unique<HallEffect>(3)); // R3
694 reader.set_top_claw_back_hall(make_unique<HallEffect>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700695
Brian Silverman85fbb602015-08-29 19:28:20 -0700696 reader.set_bottom_claw_encoder(make_encoder(4));
Brian Silverman552350b2015-08-02 18:23:34 -0700697 reader.set_bottom_claw_front_hall(make_unique<HallEffect>(1)); // L2
698 reader.set_bottom_claw_calibration_hall(make_unique<HallEffect>(0)); // L3
699 reader.set_bottom_claw_back_hall(make_unique<HallEffect>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700700
Brian Silverman85fbb602015-08-29 19:28:20 -0700701 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Brian Silverman552350b2015-08-02 18:23:34 -0700702 reader.set_shooter_proximal(make_unique<HallEffect>(6)); // S1
703 reader.set_shooter_distal(make_unique<HallEffect>(7)); // S2
704 reader.set_shooter_plunger(make_unique<HallEffect>(8)); // S3
705 reader.set_shooter_latch(make_unique<HallEffect>(9)); // S4
706
Brian Silverman17f503e2015-08-02 18:17:18 -0700707 reader.set_dma(make_unique<DMA>());
708 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700709
Brian Silverman17f503e2015-08-02 18:17:18 -0700710 GyroSender gyro_sender;
711 ::std::thread gyro_thread(::std::ref(gyro_sender));
712
713 DrivetrainWriter drivetrain_writer;
714 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700715 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700716 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700717 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700718 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
719
Brian Silverman552350b2015-08-02 18:23:34 -0700720 ::frc971::wpilib::ClawWriter claw_writer;
721 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
722 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
723 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
724 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
725 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
726 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700727 ::std::thread claw_writer_thread(::std::ref(claw_writer));
728
Brian Silverman552350b2015-08-02 18:23:34 -0700729 ::frc971::wpilib::ShooterWriter shooter_writer;
730 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
731 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
732
Brian Silverman17f503e2015-08-02 18:17:18 -0700733 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
734 new ::frc971::wpilib::BufferedPcm());
735 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700736 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
737 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
738 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
739 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700740
Austin Schuh016a6b02015-10-08 06:41:14 +0000741 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700742 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
743 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
744
745 // Wait forever. Not much else to do...
746 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
747
748 LOG(ERROR, "Exiting WPILibRobot\n");
749
750 joystick_sender.Quit();
751 joystick_thread.join();
752 reader.Quit();
753 reader_thread.join();
754 gyro_sender.Quit();
755 gyro_thread.join();
756
757 drivetrain_writer.Quit();
758 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700759 shooter_writer.Quit();
760 shooter_writer_thread.join();
761 claw_writer.Quit();
762 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700763 solenoid_writer.Quit();
764 solenoid_thread.join();
765
766 ::aos::Cleanup();
767 }
768};
769
770} // namespace wpilib
771} // namespace frc971
772
773
774START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);