blob: 397730079c1fc9781ba0a776487008febae42ac3 [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"
16#include "RobotBase.h"
17#include "dma.h"
18#include "ControllerPower.h"
19#ifndef WPILIB2015
20#include "DigitalGlitchFilter.h"
21#endif
22#undef ERROR
23
Brian Silverman17f503e2015-08-02 18:17:18 -070024#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
26#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
30#include "aos/common/stl_mutex.h"
31#include "aos/linux_code/init.h"
32#include "aos/common/messages/robot_state.q.h"
33
Brian Silverman552350b2015-08-02 18:23:34 -070034#include "frc971/shifter_hall_effect.h"
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
41#include "frc971/wpilib/hall_effect.h"
42#include "frc971/wpilib/joystick_sender.h"
43#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/buffered_solenoid.h"
45#include "frc971/wpilib/buffered_pcm.h"
46#include "frc971/wpilib/gyro_sender.h"
47#include "frc971/wpilib/dma_edge_counting.h"
48#include "frc971/wpilib/interrupt_edge_counting.h"
49#include "frc971/wpilib/encoder_and_potentiometer.h"
50#include "frc971/wpilib/logging.q.h"
51
Brian Silverman17f503e2015-08-02 18:17:18 -070052#ifndef M_PI
53#define M_PI 3.14159265358979323846
54#endif
55
Brian Silverman17f503e2015-08-02 18:17:18 -070056using ::frc971::control_loops::drivetrain_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070057using ::frc971::control_loops::claw_queue;
Brian Silverman552350b2015-08-02 18:23:34 -070058using ::frc971::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070059
60namespace frc971 {
61namespace wpilib {
62
Brian Silverman85fbb602015-08-29 19:28:20 -070063// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
64// DMA stuff and then removing the * 2.0 in *_translate.
65// The low bit is direction.
66
Brian Silverman552350b2015-08-02 18:23:34 -070067// TODO(brian): Replace this with ::std::make_unique once all our toolchains
68// have support.
69template <class T, class... U>
70std::unique_ptr<T> make_unique(U &&... u) {
71 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
72}
73
Brian Silverman17f503e2015-08-02 18:17:18 -070074double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070075 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070076 (256.0 /*cpr*/ * 4.0 /*4x*/) *
77 constants::GetValues().drivetrain_encoder_ratio *
Brian Silverman85fbb602015-08-29 19:28:20 -070078 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070079}
80
Brian Silverman552350b2015-08-02 18:23:34 -070081float hall_translate(const constants::ShifterHallEffect &k, float in_low,
82 float in_high) {
83 const float low_ratio =
84 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
85 static_cast<float>(k.low_gear_middle - k.low_gear_low);
86 const float high_ratio =
87 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
88 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070089
Brian Silverman552350b2015-08-02 18:23:34 -070090 // Return low when we are below 1/2, and high when we are above 1/2.
91 if (low_ratio + high_ratio < 1.0) {
92 return low_ratio;
93 } else {
94 return high_ratio;
95 }
Brian Silverman17f503e2015-08-02 18:17:18 -070096}
97
98double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070099 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700100 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700101 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700102}
103
Brian Silverman552350b2015-08-02 18:23:34 -0700104double shooter_translate(int32_t in) {
105 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
106 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
107 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700108}
109
110static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700111 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
112 18.0 / 32.0 /* big belt reduction */ *
113 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
114 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700115
116class SensorReader {
117 public:
118 SensorReader() {
119 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
120 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700121 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700122 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700123 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 }
125
Brian Silverman552350b2015-08-02 18:23:34 -0700126 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
127 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
129
Brian Silverman552350b2015-08-02 18:23:34 -0700130 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
131 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
135 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 }
137
Brian Silverman552350b2015-08-02 18:23:34 -0700138 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
139 high_left_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_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
143 low_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_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
147 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Brian Silverman552350b2015-08-02 18:23:34 -0700150 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
151 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700152 }
153
Brian Silverman552350b2015-08-02 18:23:34 -0700154 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
155 encoder_filter_.Add(encoder.get());
156 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700157 }
158
Austin Schuhc5e36082015-10-31 13:30:46 -0700159 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700160 hall_filter_.Add(hall.get());
161 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700162 }
163
Austin Schuhc5e36082015-10-31 13:30:46 -0700164 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700165 hall_filter_.Add(hall.get());
166 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700167 }
168
Austin Schuhc5e36082015-10-31 13:30:46 -0700169 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700170 hall_filter_.Add(hall.get());
171 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700172 }
173
Brian Silverman552350b2015-08-02 18:23:34 -0700174 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
175 encoder_filter_.Add(encoder.get());
176 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 }
178
Austin Schuhc5e36082015-10-31 13:30:46 -0700179 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700180 hall_filter_.Add(hall.get());
181 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700182 }
183
Austin Schuhc5e36082015-10-31 13:30:46 -0700184 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700185 hall_filter_.Add(hall.get());
186 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 }
188
Austin Schuhc5e36082015-10-31 13:30:46 -0700189 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700190 hall_filter_.Add(hall.get());
191 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700192 }
193
Brian Silverman552350b2015-08-02 18:23:34 -0700194 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
195 encoder_filter_.Add(encoder.get());
196 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700197 }
198
Austin Schuhc5e36082015-10-31 13:30:46 -0700199 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700200 hall_filter_.Add(hall.get());
201 shooter_proximal_ = ::std::move(hall);
202 }
203
Austin Schuhc5e36082015-10-31 13:30:46 -0700204 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700205 hall_filter_.Add(hall.get());
206 shooter_distal_ = ::std::move(hall);
207 }
208
Austin Schuhc5e36082015-10-31 13:30:46 -0700209 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700210 hall_filter_.Add(hall.get());
211 shooter_plunger_ = ::std::move(hall);
212 shooter_plunger_reader_ =
213 make_unique<DMADigitalReader>(shooter_plunger_.get());
214 }
215
Austin Schuhc5e36082015-10-31 13:30:46 -0700216 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700217 hall_filter_.Add(hall.get());
218 shooter_latch_ = ::std::move(hall);
219 shooter_latch_reader_ = make_unique<DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700220 }
221
222 // All of the DMA-related set_* calls must be made before this, and it doesn't
223 // hurt to do all of them.
224 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silverman552350b2015-08-02 18:23:34 -0700225 shooter_proximal_counter_ = make_unique<DMAEdgeCounter>(
226 shooter_encoder_.get(), shooter_proximal_.get());
227 shooter_distal_counter_ = make_unique<DMAEdgeCounter>(
228 shooter_encoder_.get(), shooter_distal_.get());
229
Brian Silverman17f503e2015-08-02 18:17:18 -0700230 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700231 dma_synchronizer_->Add(shooter_proximal_counter_.get());
232 dma_synchronizer_->Add(shooter_distal_counter_.get());
233 dma_synchronizer_->Add(shooter_plunger_reader_.get());
234 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700235 }
236
237 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700238 ::aos::SetCurrentThreadName("SensorReader");
Brian Silverman552350b2015-08-02 18:23:34 -0700239 LOG(INFO, "In sensor reader thread\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700240
241 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700242 ds_ =
243#ifdef WPILIB2015
244 DriverStation::GetInstance();
245#else
246 &DriverStation::GetInstance();
247#endif
Brian Silverman17f503e2015-08-02 18:17:18 -0700248
Brian Silverman552350b2015-08-02 18:23:34 -0700249 top_reader_.Start();
250 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700251 dma_synchronizer_->Start();
252 LOG(INFO, "Things are now started\n");
253
254 ::aos::SetCurrentThreadRealtimePriority(kPriority);
255 while (run_) {
Brian Silverman552350b2015-08-02 18:23:34 -0700256 ::aos::time::PhasedLoopXMS(10, 9000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700257 RunIteration();
258 }
259
Brian Silverman552350b2015-08-02 18:23:34 -0700260 top_reader_.Quit();
261 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700262 }
263
264 void RunIteration() {
265 {
266 auto new_state = ::aos::robot_state.MakeMessage();
267
268 new_state->reader_pid = my_pid_;
269 new_state->outputs_enabled = ds_->IsSysActive();
270 new_state->browned_out = ds_->IsSysBrownedOut();
271
272 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
273 new_state->is_5v_active = ControllerPower::GetEnabled5V();
274 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
275 new_state->voltage_5v = ControllerPower::GetVoltage5V();
276
277 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
278 new_state->voltage_battery = ds_->GetBatteryVoltage();
279
280 LOG_STRUCT(DEBUG, "robot_state", *new_state);
281
282 new_state.Send();
283 }
284
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());
293 drivetrain_message->left_shifter_position =
294 hall_translate(values.left_drive, low_left_drive_hall_->GetVoltage(),
295 high_left_drive_hall_->GetVoltage());
296 drivetrain_message->right_shifter_position = hall_translate(
297 values.right_drive, low_right_drive_hall_->GetVoltage(),
298 high_right_drive_hall_->GetVoltage());
Brian Silverman17f503e2015-08-02 18:17:18 -0700299
300 drivetrain_message.Send();
301 }
302
Brian Silverman552350b2015-08-02 18:23:34 -0700303 ::frc971::sensors::auto_mode.MakeWithBuilder()
304 .voltage(auto_selector_analog_->GetVoltage())
305 .Send();
306
Brian Silverman17f503e2015-08-02 18:17:18 -0700307 dma_synchronizer_->RunIteration();
308
Brian Silverman17f503e2015-08-02 18:17:18 -0700309 {
Brian Silverman552350b2015-08-02 18:23:34 -0700310 auto shooter_message = shooter_queue.position.MakeMessage();
311 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
312 shooter_message->plunger = shooter_plunger_reader_->value();
313 shooter_message->latch = shooter_latch_reader_->value();
314 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
315 &shooter_message->pusher_proximal);
316 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
317 &shooter_message->pusher_distal);
318
319 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700320 }
321
322 {
323 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700324 top_reader_.RunIteration(&claw_message->top);
325 bottom_reader_.RunIteration(&claw_message->bottom);
326
Brian Silverman17f503e2015-08-02 18:17:18 -0700327 claw_message.Send();
328 }
329 }
330
331 void Quit() { run_ = false; }
332
333 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700334 class HalfClawReader {
335 public:
336 HalfClawReader(bool reversed) : reversed_(reversed) {}
337
338 void set_encoder(::std::unique_ptr<Encoder> encoder) {
339 encoder_ = ::std::move(encoder);
340 }
341
Austin Schuhc5e36082015-10-31 13:30:46 -0700342 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700343 front_hall_ = ::std::move(front_hall);
344 }
345
346 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700347 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700348 calibration_hall_ = ::std::move(calibration_hall);
349 }
350
Austin Schuhc5e36082015-10-31 13:30:46 -0700351 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700352 back_hall_ = ::std::move(back_hall);
353 }
354
355 void Start() {
356 front_counter_ =
357 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
358 synchronizer_.Add(front_counter_.get());
359 calibration_counter_ =
360 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
361 synchronizer_.Add(calibration_counter_.get());
362 back_counter_ =
363 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
364 synchronizer_.Add(back_counter_.get());
365 synchronized_encoder_ =
366 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
367 synchronizer_.Add(synchronized_encoder_.get());
368
369 synchronizer_.Start();
370 }
371
372 void Quit() { synchronizer_.Quit(); }
373
374 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
375 const double multiplier = reversed_ ? -1.0 : 1.0;
376
377 synchronizer_.RunIteration();
378
379 CopyPosition(front_counter_.get(), &half_claw_position->front);
380 CopyPosition(calibration_counter_.get(),
381 &half_claw_position->calibration);
382 CopyPosition(back_counter_.get(), &half_claw_position->back);
383 half_claw_position->position =
384 multiplier * claw_translate(synchronized_encoder_->get());
385 }
386
387 private:
388 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
389 const double multiplier = reversed_ ? -1.0 : 1.0;
390
391 out->current = counter->polled_value();
392 out->posedge_count = counter->positive_interrupt_count();
393 out->negedge_count = counter->negative_interrupt_count();
394 out->posedge_value =
395 multiplier * claw_translate(counter->last_positive_encoder_value());
396 out->negedge_value =
397 multiplier * claw_translate(counter->last_negative_encoder_value());
398 }
399
400 InterruptSynchronizer synchronizer_{kInterruptPriority};
401
402 ::std::unique_ptr<EdgeCounter> front_counter_;
403 ::std::unique_ptr<EdgeCounter> calibration_counter_;
404 ::std::unique_ptr<EdgeCounter> back_counter_;
405 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
406
407 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700408 ::std::unique_ptr<DigitalInput> front_hall_;
409 ::std::unique_ptr<DigitalInput> calibration_hall_;
410 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700411
412 const bool reversed_;
413 };
414
Brian Silverman17f503e2015-08-02 18:17:18 -0700415 static const int kPriority = 30;
416 static const int kInterruptPriority = 55;
417
Brian Silverman552350b2015-08-02 18:23:34 -0700418 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
419 PosedgeOnlyCountedHallEffectStruct *output) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700420 // TODO(Brian): Remove HallEffect so current will get inverted too like
421 // everything else.
Brian Silverman552350b2015-08-02 18:23:34 -0700422 output->current = counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700423 // These are inverted because the hall effects give logical false when
424 // there's a magnet in front of them.
425 output->posedge_count = counter->negative_count();
426 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700427 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700428 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700429 }
430
Brian Silverman17f503e2015-08-02 18:17:18 -0700431 int32_t my_pid_;
432 DriverStation *ds_;
433
Brian Silverman17f503e2015-08-02 18:17:18 -0700434 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
435
Brian Silverman552350b2015-08-02 18:23:34 -0700436 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700437
Brian Silverman552350b2015-08-02 18:23:34 -0700438 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
439 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
440 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
441 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
442 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
443 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700444
Brian Silverman552350b2015-08-02 18:23:34 -0700445 HalfClawReader top_reader_{false}, bottom_reader_{true};
446
447 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700448 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
449 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silverman552350b2015-08-02 18:23:34 -0700450 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
451 shooter_distal_counter_;
452 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
453 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700454
455 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700456 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700457};
458
459class SolenoidWriter {
460 public:
461 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
462 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700463 shooter_(".frc971.control_loops.shooter_queue.output"),
464 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700465
Austin Schuhc5e36082015-10-31 13:30:46 -0700466 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 pressure_switch_ = ::std::move(pressure_switch);
468 }
469
470 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
471 compressor_relay_ = ::std::move(compressor_relay);
472 }
473
Brian Silverman552350b2015-08-02 18:23:34 -0700474 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
475 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700476 }
477
Brian Silverman552350b2015-08-02 18:23:34 -0700478 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
479 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700480 }
481
Brian Silverman552350b2015-08-02 18:23:34 -0700482 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
483 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700484 }
485
Brian Silverman552350b2015-08-02 18:23:34 -0700486 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
487 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700488 }
489
490 void operator()() {
491 ::aos::SetCurrentThreadName("Solenoids");
492 ::aos::SetCurrentThreadRealtimePriority(30);
493
494 while (run_) {
495 ::aos::time::PhasedLoopXMS(20, 1000);
496
497 {
Brian Silverman552350b2015-08-02 18:23:34 -0700498 shooter_.FetchLatest();
499 if (shooter_.get()) {
500 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
501 shooter_latch_->Set(!shooter_->latch_piston);
502 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700503 }
504 }
505
506 {
Brian Silverman552350b2015-08-02 18:23:34 -0700507 drivetrain_.FetchLatest();
508 if (drivetrain_.get()) {
509 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
510 drivetrain_left_->Set(drivetrain_->left_high);
511 drivetrain_right_->Set(drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700512 }
513 }
514
Brian Silverman17f503e2015-08-02 18:17:18 -0700515 {
516 PneumaticsToLog to_log;
517 {
518 const bool compressor_on = !pressure_switch_->Get();
519 to_log.compressor_on = compressor_on;
520 if (compressor_on) {
521 compressor_relay_->Set(Relay::kForward);
522 } else {
523 compressor_relay_->Set(Relay::kOff);
524 }
525 }
526
527 pcm_->Flush();
528 to_log.read_solenoids = pcm_->GetAll();
529 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
530 }
531 }
532 }
533
534 void Quit() { run_ = false; }
535
536 private:
537 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700538
539 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
540 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
541 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
542 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
543
Austin Schuhc5e36082015-10-31 13:30:46 -0700544 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700545 ::std::unique_ptr<Relay> compressor_relay_;
546
Brian Silverman552350b2015-08-02 18:23:34 -0700547 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
548 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700549
550 ::std::atomic<bool> run_{true};
551};
552
553class DrivetrainWriter : public LoopOutputHandler {
554 public:
555 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
556 left_drivetrain_talon_ = ::std::move(t);
557 }
558
559 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
560 right_drivetrain_talon_ = ::std::move(t);
561 }
562
563 private:
564 virtual void Read() override {
565 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
566 }
567
568 virtual void Write() override {
569 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
570 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700571 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
572 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700573 }
574
575 virtual void Stop() override {
576 LOG(WARNING, "drivetrain output too old\n");
577 left_drivetrain_talon_->Disable();
578 right_drivetrain_talon_->Disable();
579 }
580
581 ::std::unique_ptr<Talon> left_drivetrain_talon_;
582 ::std::unique_ptr<Talon> right_drivetrain_talon_;
583};
584
Brian Silverman552350b2015-08-02 18:23:34 -0700585class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700586 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700587 void set_shooter_talon(::std::unique_ptr<Talon> t) {
588 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700589 }
590
591 private:
592 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400593 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700594 }
595
596 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400597 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700598 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700599 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700600 }
601
602 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700603 LOG(WARNING, "shooter output too old\n");
604 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700605 }
606
Brian Silverman552350b2015-08-02 18:23:34 -0700607 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700608};
609
610class ClawWriter : public LoopOutputHandler {
611 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700612 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
613 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700614 }
615
Brian Silverman552350b2015-08-02 18:23:34 -0700616 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
617 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700618 }
619
Brian Silverman552350b2015-08-02 18:23:34 -0700620 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
621 left_tusk_talon_ = ::std::move(t);
622 }
623
624 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
625 right_tusk_talon_ = ::std::move(t);
626 }
627
628 void set_intake1_talon(::std::unique_ptr<Talon> t) {
629 intake1_talon_ = ::std::move(t);
630 }
631
632 void set_intake2_talon(::std::unique_ptr<Talon> t) {
633 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700634 }
635
636 private:
637 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400638 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700639 }
640
641 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400642 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700643 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700644 intake1_talon_->Set(queue->intake_voltage / 12.0);
645 intake2_talon_->Set(queue->intake_voltage / 12.0);
646 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
647 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
648 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
649 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700650 }
651
652 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700653 LOG(WARNING, "claw output too old\n");
654 intake1_talon_->Disable();
655 intake2_talon_->Disable();
656 bottom_claw_talon_->Disable();
657 top_claw_talon_->Disable();
658 left_tusk_talon_->Disable();
659 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700660 }
661
Brian Silverman552350b2015-08-02 18:23:34 -0700662 ::std::unique_ptr<Talon> top_claw_talon_;
663 ::std::unique_ptr<Talon> bottom_claw_talon_;
664 ::std::unique_ptr<Talon> left_tusk_talon_;
665 ::std::unique_ptr<Talon> right_tusk_talon_;
666 ::std::unique_ptr<Talon> intake1_talon_;
667 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700668};
669
Brian Silverman17f503e2015-08-02 18:17:18 -0700670class WPILibRobot : public RobotBase {
671 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700672 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700673 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
674 Encoder::k4X);
675 }
Brian Silverman552350b2015-08-02 18:23:34 -0700676
Brian Silverman17f503e2015-08-02 18:17:18 -0700677 virtual void StartCompetition() {
678 ::aos::InitNRT();
679 ::aos::SetCurrentThreadName("StartCompetition");
680
681 JoystickSender joystick_sender;
682 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700683
684 SensorReader reader;
685 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700686
Brian Silverman85fbb602015-08-29 19:28:20 -0700687 // Create this first to make sure it ends up in one of the lower-numbered
688 // FPGA slots so we can use it with DMA.
689 auto shooter_encoder_temp = make_encoder(2);
690
Brian Silverman552350b2015-08-02 18:23:34 -0700691 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700692
Brian Silverman552350b2015-08-02 18:23:34 -0700693 reader.set_drivetrain_left_encoder(make_encoder(0));
694 reader.set_drivetrain_right_encoder(make_encoder(1));
695 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
696 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
697 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
698 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700699
Brian Silverman552350b2015-08-02 18:23:34 -0700700 reader.set_top_claw_encoder(make_encoder(3));
701 reader.set_top_claw_front_hall(make_unique<HallEffect>(4)); // R2
702 reader.set_top_claw_calibration_hall(make_unique<HallEffect>(3)); // R3
703 reader.set_top_claw_back_hall(make_unique<HallEffect>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700704
Brian Silverman85fbb602015-08-29 19:28:20 -0700705 reader.set_bottom_claw_encoder(make_encoder(4));
Brian Silverman552350b2015-08-02 18:23:34 -0700706 reader.set_bottom_claw_front_hall(make_unique<HallEffect>(1)); // L2
707 reader.set_bottom_claw_calibration_hall(make_unique<HallEffect>(0)); // L3
708 reader.set_bottom_claw_back_hall(make_unique<HallEffect>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700709
Brian Silverman85fbb602015-08-29 19:28:20 -0700710 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Brian Silverman552350b2015-08-02 18:23:34 -0700711 reader.set_shooter_proximal(make_unique<HallEffect>(6)); // S1
712 reader.set_shooter_distal(make_unique<HallEffect>(7)); // S2
713 reader.set_shooter_plunger(make_unique<HallEffect>(8)); // S3
714 reader.set_shooter_latch(make_unique<HallEffect>(9)); // S4
715
Brian Silverman17f503e2015-08-02 18:17:18 -0700716 reader.set_dma(make_unique<DMA>());
717 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700718
Brian Silverman17f503e2015-08-02 18:17:18 -0700719 GyroSender gyro_sender;
720 ::std::thread gyro_thread(::std::ref(gyro_sender));
721
722 DrivetrainWriter drivetrain_writer;
723 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700724 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700725 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700726 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700727 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
728
Brian Silverman552350b2015-08-02 18:23:34 -0700729 ::frc971::wpilib::ClawWriter claw_writer;
730 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
731 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
732 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
733 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
734 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
735 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700736 ::std::thread claw_writer_thread(::std::ref(claw_writer));
737
Brian Silverman552350b2015-08-02 18:23:34 -0700738 ::frc971::wpilib::ShooterWriter shooter_writer;
739 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
740 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
741
Brian Silverman17f503e2015-08-02 18:17:18 -0700742 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
743 new ::frc971::wpilib::BufferedPcm());
744 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700745 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
746 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
747 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
748 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700749
Austin Schuh016a6b02015-10-08 06:41:14 +0000750 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700751 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
752 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
753
754 // Wait forever. Not much else to do...
755 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
756
757 LOG(ERROR, "Exiting WPILibRobot\n");
758
759 joystick_sender.Quit();
760 joystick_thread.join();
761 reader.Quit();
762 reader_thread.join();
763 gyro_sender.Quit();
764 gyro_thread.join();
765
766 drivetrain_writer.Quit();
767 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700768 shooter_writer.Quit();
769 shooter_writer_thread.join();
770 claw_writer.Quit();
771 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700772 solenoid_writer.Quit();
773 solenoid_thread.join();
774
775 ::aos::Cleanup();
776 }
777};
778
779} // namespace wpilib
780} // namespace frc971
781
782
783START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);