blob: 20bef26a827bd24b0886dbbcd2250f2116df90b9 [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
Brian Silverman17f503e2015-08-02 18:17:18 -070041#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
45#include "frc971/wpilib/gyro_sender.h"
46#include "frc971/wpilib/dma_edge_counting.h"
47#include "frc971/wpilib/interrupt_edge_counting.h"
48#include "frc971/wpilib/encoder_and_potentiometer.h"
49#include "frc971/wpilib/logging.q.h"
50
Brian Silverman17f503e2015-08-02 18:17:18 -070051#ifndef M_PI
52#define M_PI 3.14159265358979323846
53#endif
54
Brian Silverman17f503e2015-08-02 18:17:18 -070055using ::frc971::control_loops::drivetrain_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070056using ::frc971::control_loops::claw_queue;
Brian Silverman552350b2015-08-02 18:23:34 -070057using ::frc971::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070058
59namespace frc971 {
60namespace wpilib {
61
Brian Silverman85fbb602015-08-29 19:28:20 -070062// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
63// DMA stuff and then removing the * 2.0 in *_translate.
64// The low bit is direction.
65
Brian Silverman552350b2015-08-02 18:23:34 -070066// TODO(brian): Replace this with ::std::make_unique once all our toolchains
67// have support.
68template <class T, class... U>
69std::unique_ptr<T> make_unique(U &&... u) {
70 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
71}
72
Brian Silverman17f503e2015-08-02 18:17:18 -070073double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070074 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070075 (256.0 /*cpr*/ * 4.0 /*4x*/) *
76 constants::GetValues().drivetrain_encoder_ratio *
Brian Silverman85fbb602015-08-29 19:28:20 -070077 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070078}
79
Brian Silverman552350b2015-08-02 18:23:34 -070080float hall_translate(const constants::ShifterHallEffect &k, float in_low,
81 float in_high) {
82 const float low_ratio =
83 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
84 static_cast<float>(k.low_gear_middle - k.low_gear_low);
85 const float high_ratio =
86 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
87 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070088
Brian Silverman552350b2015-08-02 18:23:34 -070089 // Return low when we are below 1/2, and high when we are above 1/2.
90 if (low_ratio + high_ratio < 1.0) {
91 return low_ratio;
92 } else {
93 return high_ratio;
94 }
Brian Silverman17f503e2015-08-02 18:17:18 -070095}
96
97double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070098 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -070099 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700100 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700101}
102
Brian Silverman552350b2015-08-02 18:23:34 -0700103double shooter_translate(int32_t in) {
104 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
105 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
106 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700107}
108
109static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700110 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
111 18.0 / 32.0 /* big belt reduction */ *
112 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
113 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700114
115class SensorReader {
116 public:
117 SensorReader() {
118 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
119 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700120 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700121 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700122 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700123 }
124
Brian Silverman552350b2015-08-02 18:23:34 -0700125 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
126 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700127 }
128
Brian Silverman552350b2015-08-02 18:23:34 -0700129 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
130 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700131 }
132
Brian Silverman552350b2015-08-02 18:23:34 -0700133 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
134 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700135 }
136
Brian Silverman552350b2015-08-02 18:23:34 -0700137 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
138 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700139 }
140
Brian Silverman552350b2015-08-02 18:23:34 -0700141 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
142 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700143 }
144
Brian Silverman552350b2015-08-02 18:23:34 -0700145 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
146 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700147 }
148
Brian Silverman552350b2015-08-02 18:23:34 -0700149 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
150 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700151 }
152
Brian Silverman552350b2015-08-02 18:23:34 -0700153 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
154 encoder_filter_.Add(encoder.get());
155 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700156 }
157
Austin Schuhc5e36082015-10-31 13:30:46 -0700158 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700159 hall_filter_.Add(hall.get());
160 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700161 }
162
Austin Schuhc5e36082015-10-31 13:30:46 -0700163 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700164 hall_filter_.Add(hall.get());
165 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700166 }
167
Austin Schuhc5e36082015-10-31 13:30:46 -0700168 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700169 hall_filter_.Add(hall.get());
170 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700171 }
172
Brian Silverman552350b2015-08-02 18:23:34 -0700173 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
174 encoder_filter_.Add(encoder.get());
175 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700176 }
177
Austin Schuhc5e36082015-10-31 13:30:46 -0700178 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700179 hall_filter_.Add(hall.get());
180 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700181 }
182
Austin Schuhc5e36082015-10-31 13:30:46 -0700183 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700184 hall_filter_.Add(hall.get());
185 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700186 }
187
Austin Schuhc5e36082015-10-31 13:30:46 -0700188 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700189 hall_filter_.Add(hall.get());
190 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700191 }
192
Brian Silverman552350b2015-08-02 18:23:34 -0700193 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
194 encoder_filter_.Add(encoder.get());
195 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700196 }
197
Austin Schuhc5e36082015-10-31 13:30:46 -0700198 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700199 hall_filter_.Add(hall.get());
200 shooter_proximal_ = ::std::move(hall);
201 }
202
Austin Schuhc5e36082015-10-31 13:30:46 -0700203 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700204 hall_filter_.Add(hall.get());
205 shooter_distal_ = ::std::move(hall);
206 }
207
Austin Schuhc5e36082015-10-31 13:30:46 -0700208 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700209 hall_filter_.Add(hall.get());
210 shooter_plunger_ = ::std::move(hall);
211 shooter_plunger_reader_ =
212 make_unique<DMADigitalReader>(shooter_plunger_.get());
213 }
214
Austin Schuhc5e36082015-10-31 13:30:46 -0700215 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700216 hall_filter_.Add(hall.get());
217 shooter_latch_ = ::std::move(hall);
218 shooter_latch_reader_ = make_unique<DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700219 }
220
221 // All of the DMA-related set_* calls must be made before this, and it doesn't
222 // hurt to do all of them.
223 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silverman552350b2015-08-02 18:23:34 -0700224 shooter_proximal_counter_ = make_unique<DMAEdgeCounter>(
225 shooter_encoder_.get(), shooter_proximal_.get());
226 shooter_distal_counter_ = make_unique<DMAEdgeCounter>(
227 shooter_encoder_.get(), shooter_distal_.get());
228
Brian Silverman17f503e2015-08-02 18:17:18 -0700229 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700230 dma_synchronizer_->Add(shooter_proximal_counter_.get());
231 dma_synchronizer_->Add(shooter_distal_counter_.get());
232 dma_synchronizer_->Add(shooter_plunger_reader_.get());
233 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700234 }
235
236 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700237 ::aos::SetCurrentThreadName("SensorReader");
Brian Silverman552350b2015-08-02 18:23:34 -0700238 LOG(INFO, "In sensor reader thread\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700239
240 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700241 ds_ =
242#ifdef WPILIB2015
243 DriverStation::GetInstance();
244#else
245 &DriverStation::GetInstance();
246#endif
Brian Silverman17f503e2015-08-02 18:17:18 -0700247
Brian Silverman552350b2015-08-02 18:23:34 -0700248 top_reader_.Start();
249 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700250 dma_synchronizer_->Start();
251 LOG(INFO, "Things are now started\n");
252
253 ::aos::SetCurrentThreadRealtimePriority(kPriority);
254 while (run_) {
Brian Silverman552350b2015-08-02 18:23:34 -0700255 ::aos::time::PhasedLoopXMS(10, 9000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700256 RunIteration();
257 }
258
Brian Silverman552350b2015-08-02 18:23:34 -0700259 top_reader_.Quit();
260 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700261 }
262
263 void RunIteration() {
264 {
265 auto new_state = ::aos::robot_state.MakeMessage();
266
267 new_state->reader_pid = my_pid_;
268 new_state->outputs_enabled = ds_->IsSysActive();
269 new_state->browned_out = ds_->IsSysBrownedOut();
270
271 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
272 new_state->is_5v_active = ControllerPower::GetEnabled5V();
273 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
274 new_state->voltage_5v = ControllerPower::GetVoltage5V();
275
276 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
277 new_state->voltage_battery = ds_->GetBatteryVoltage();
278
279 LOG_STRUCT(DEBUG, "robot_state", *new_state);
280
281 new_state.Send();
282 }
283
Brian Silverman552350b2015-08-02 18:23:34 -0700284 const auto &values = constants::GetValues();
285
Brian Silverman17f503e2015-08-02 18:17:18 -0700286 {
287 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
288 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700289 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700290 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700291 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Austin Schuha0c1e152015-11-08 14:10:13 -0800292
293 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
294 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700295 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800296 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
297 drivetrain_message->high_left_hall);
298
299 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
300 drivetrain_message->high_right_hall =
301 high_right_drive_hall_->GetVoltage();
302 drivetrain_message->right_shifter_position =
303 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
304 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700305
306 drivetrain_message.Send();
307 }
308
Brian Silverman552350b2015-08-02 18:23:34 -0700309 ::frc971::sensors::auto_mode.MakeWithBuilder()
310 .voltage(auto_selector_analog_->GetVoltage())
311 .Send();
312
Brian Silverman17f503e2015-08-02 18:17:18 -0700313 dma_synchronizer_->RunIteration();
314
Brian Silverman17f503e2015-08-02 18:17:18 -0700315 {
Brian Silverman552350b2015-08-02 18:23:34 -0700316 auto shooter_message = shooter_queue.position.MakeMessage();
317 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800318 shooter_message->plunger = !shooter_plunger_reader_->value();
319 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700320 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
321 &shooter_message->pusher_proximal);
322 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
323 &shooter_message->pusher_distal);
324
325 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700326 }
327
328 {
329 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700330 top_reader_.RunIteration(&claw_message->top);
331 bottom_reader_.RunIteration(&claw_message->bottom);
332
Brian Silverman17f503e2015-08-02 18:17:18 -0700333 claw_message.Send();
334 }
335 }
336
337 void Quit() { run_ = false; }
338
339 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700340 class HalfClawReader {
341 public:
342 HalfClawReader(bool reversed) : reversed_(reversed) {}
343
344 void set_encoder(::std::unique_ptr<Encoder> encoder) {
345 encoder_ = ::std::move(encoder);
346 }
347
Austin Schuhc5e36082015-10-31 13:30:46 -0700348 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700349 front_hall_ = ::std::move(front_hall);
350 }
351
352 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700353 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700354 calibration_hall_ = ::std::move(calibration_hall);
355 }
356
Austin Schuhc5e36082015-10-31 13:30:46 -0700357 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700358 back_hall_ = ::std::move(back_hall);
359 }
360
361 void Start() {
362 front_counter_ =
363 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
364 synchronizer_.Add(front_counter_.get());
365 calibration_counter_ =
366 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
367 synchronizer_.Add(calibration_counter_.get());
368 back_counter_ =
369 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
370 synchronizer_.Add(back_counter_.get());
371 synchronized_encoder_ =
372 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
373 synchronizer_.Add(synchronized_encoder_.get());
374
375 synchronizer_.Start();
376 }
377
378 void Quit() { synchronizer_.Quit(); }
379
380 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
381 const double multiplier = reversed_ ? -1.0 : 1.0;
382
383 synchronizer_.RunIteration();
384
385 CopyPosition(front_counter_.get(), &half_claw_position->front);
386 CopyPosition(calibration_counter_.get(),
387 &half_claw_position->calibration);
388 CopyPosition(back_counter_.get(), &half_claw_position->back);
389 half_claw_position->position =
390 multiplier * claw_translate(synchronized_encoder_->get());
391 }
392
393 private:
394 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
395 const double multiplier = reversed_ ? -1.0 : 1.0;
396
Austin Schuh5c25ab72015-11-01 13:17:11 -0800397 out->current = !counter->polled_value();
398 out->posedge_count = counter->negative_interrupt_count();
399 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700400 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800401 multiplier * claw_translate(counter->last_positive_encoder_value());
402 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700403 multiplier * claw_translate(counter->last_negative_encoder_value());
404 }
405
406 InterruptSynchronizer synchronizer_{kInterruptPriority};
407
408 ::std::unique_ptr<EdgeCounter> front_counter_;
409 ::std::unique_ptr<EdgeCounter> calibration_counter_;
410 ::std::unique_ptr<EdgeCounter> back_counter_;
411 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
412
413 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700414 ::std::unique_ptr<DigitalInput> front_hall_;
415 ::std::unique_ptr<DigitalInput> calibration_hall_;
416 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700417
418 const bool reversed_;
419 };
420
Brian Silverman17f503e2015-08-02 18:17:18 -0700421 static const int kPriority = 30;
422 static const int kInterruptPriority = 55;
423
Brian Silverman552350b2015-08-02 18:23:34 -0700424 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
425 PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800426 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700427 // These are inverted because the hall effects give logical false when
428 // there's a magnet in front of them.
429 output->posedge_count = counter->negative_count();
430 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700431 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700432 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700433 }
434
Brian Silverman17f503e2015-08-02 18:17:18 -0700435 int32_t my_pid_;
436 DriverStation *ds_;
437
Brian Silverman17f503e2015-08-02 18:17:18 -0700438 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
439
Brian Silverman552350b2015-08-02 18:23:34 -0700440 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700441
Brian Silverman552350b2015-08-02 18:23:34 -0700442 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
443 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
444 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
445 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
446 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
447 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700448
Brian Silverman552350b2015-08-02 18:23:34 -0700449 HalfClawReader top_reader_{false}, bottom_reader_{true};
450
451 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700452 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
453 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silverman552350b2015-08-02 18:23:34 -0700454 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
455 shooter_distal_counter_;
456 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
457 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700458
459 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700460 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700461};
462
463class SolenoidWriter {
464 public:
465 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
466 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700467 shooter_(".frc971.control_loops.shooter_queue.output"),
468 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700469
Austin Schuhc5e36082015-10-31 13:30:46 -0700470 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700471 pressure_switch_ = ::std::move(pressure_switch);
472 }
473
474 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
475 compressor_relay_ = ::std::move(compressor_relay);
476 }
477
Brian Silverman552350b2015-08-02 18:23:34 -0700478 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
479 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700480 }
481
Brian Silverman552350b2015-08-02 18:23:34 -0700482 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
483 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700484 }
485
Brian Silverman552350b2015-08-02 18:23:34 -0700486 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
487 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700488 }
489
Brian Silverman552350b2015-08-02 18:23:34 -0700490 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
491 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700492 }
493
494 void operator()() {
495 ::aos::SetCurrentThreadName("Solenoids");
496 ::aos::SetCurrentThreadRealtimePriority(30);
497
498 while (run_) {
499 ::aos::time::PhasedLoopXMS(20, 1000);
500
501 {
Brian Silverman552350b2015-08-02 18:23:34 -0700502 shooter_.FetchLatest();
503 if (shooter_.get()) {
504 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
505 shooter_latch_->Set(!shooter_->latch_piston);
506 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700507 }
508 }
509
510 {
Brian Silverman552350b2015-08-02 18:23:34 -0700511 drivetrain_.FetchLatest();
512 if (drivetrain_.get()) {
513 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
514 drivetrain_left_->Set(drivetrain_->left_high);
515 drivetrain_right_->Set(drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700516 }
517 }
518
Brian Silverman17f503e2015-08-02 18:17:18 -0700519 {
520 PneumaticsToLog to_log;
521 {
522 const bool compressor_on = !pressure_switch_->Get();
523 to_log.compressor_on = compressor_on;
524 if (compressor_on) {
525 compressor_relay_->Set(Relay::kForward);
526 } else {
527 compressor_relay_->Set(Relay::kOff);
528 }
529 }
530
531 pcm_->Flush();
532 to_log.read_solenoids = pcm_->GetAll();
533 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
534 }
535 }
536 }
537
538 void Quit() { run_ = false; }
539
540 private:
541 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700542
543 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
544 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
545 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
546 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
547
Austin Schuhc5e36082015-10-31 13:30:46 -0700548 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700549 ::std::unique_ptr<Relay> compressor_relay_;
550
Brian Silverman552350b2015-08-02 18:23:34 -0700551 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
552 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700553
554 ::std::atomic<bool> run_{true};
555};
556
557class DrivetrainWriter : public LoopOutputHandler {
558 public:
559 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
560 left_drivetrain_talon_ = ::std::move(t);
561 }
562
563 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
564 right_drivetrain_talon_ = ::std::move(t);
565 }
566
567 private:
568 virtual void Read() override {
569 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
570 }
571
572 virtual void Write() override {
573 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
574 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700575 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
576 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700577 }
578
579 virtual void Stop() override {
580 LOG(WARNING, "drivetrain output too old\n");
581 left_drivetrain_talon_->Disable();
582 right_drivetrain_talon_->Disable();
583 }
584
585 ::std::unique_ptr<Talon> left_drivetrain_talon_;
586 ::std::unique_ptr<Talon> right_drivetrain_talon_;
587};
588
Brian Silverman552350b2015-08-02 18:23:34 -0700589class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700591 void set_shooter_talon(::std::unique_ptr<Talon> t) {
592 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700593 }
594
595 private:
596 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400597 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700598 }
599
600 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400601 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700602 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700603 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700604 }
605
606 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700607 LOG(WARNING, "shooter output too old\n");
608 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700609 }
610
Brian Silverman552350b2015-08-02 18:23:34 -0700611 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700612};
613
614class ClawWriter : public LoopOutputHandler {
615 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700616 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
617 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700618 }
619
Brian Silverman552350b2015-08-02 18:23:34 -0700620 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
621 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700622 }
623
Brian Silverman552350b2015-08-02 18:23:34 -0700624 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
625 left_tusk_talon_ = ::std::move(t);
626 }
627
628 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
629 right_tusk_talon_ = ::std::move(t);
630 }
631
632 void set_intake1_talon(::std::unique_ptr<Talon> t) {
633 intake1_talon_ = ::std::move(t);
634 }
635
636 void set_intake2_talon(::std::unique_ptr<Talon> t) {
637 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700638 }
639
640 private:
641 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400642 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700643 }
644
645 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400646 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700647 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700648 intake1_talon_->Set(queue->intake_voltage / 12.0);
649 intake2_talon_->Set(queue->intake_voltage / 12.0);
650 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
651 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
652 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
653 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700654 }
655
656 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700657 LOG(WARNING, "claw output too old\n");
658 intake1_talon_->Disable();
659 intake2_talon_->Disable();
660 bottom_claw_talon_->Disable();
661 top_claw_talon_->Disable();
662 left_tusk_talon_->Disable();
663 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700664 }
665
Brian Silverman552350b2015-08-02 18:23:34 -0700666 ::std::unique_ptr<Talon> top_claw_talon_;
667 ::std::unique_ptr<Talon> bottom_claw_talon_;
668 ::std::unique_ptr<Talon> left_tusk_talon_;
669 ::std::unique_ptr<Talon> right_tusk_talon_;
670 ::std::unique_ptr<Talon> intake1_talon_;
671 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700672};
673
Brian Silverman17f503e2015-08-02 18:17:18 -0700674class WPILibRobot : public RobotBase {
675 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700676 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700677 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
678 Encoder::k4X);
679 }
Brian Silverman552350b2015-08-02 18:23:34 -0700680
Brian Silverman17f503e2015-08-02 18:17:18 -0700681 virtual void StartCompetition() {
682 ::aos::InitNRT();
683 ::aos::SetCurrentThreadName("StartCompetition");
684
685 JoystickSender joystick_sender;
686 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700687
688 SensorReader reader;
689 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700690
Brian Silverman85fbb602015-08-29 19:28:20 -0700691 // Create this first to make sure it ends up in one of the lower-numbered
692 // FPGA slots so we can use it with DMA.
693 auto shooter_encoder_temp = make_encoder(2);
694
Brian Silverman552350b2015-08-02 18:23:34 -0700695 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700696
Brian Silverman552350b2015-08-02 18:23:34 -0700697 reader.set_drivetrain_left_encoder(make_encoder(0));
698 reader.set_drivetrain_right_encoder(make_encoder(1));
699 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
700 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
701 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
702 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700703
Brian Silverman552350b2015-08-02 18:23:34 -0700704 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800705 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
706 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
707 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700708
Brian Silverman85fbb602015-08-29 19:28:20 -0700709 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800710 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
711 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
712 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700713
Brian Silverman85fbb602015-08-29 19:28:20 -0700714 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800715 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
716 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
717 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
718 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700719
Brian Silverman17f503e2015-08-02 18:17:18 -0700720 reader.set_dma(make_unique<DMA>());
721 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700722
Brian Silverman17f503e2015-08-02 18:17:18 -0700723 GyroSender gyro_sender;
724 ::std::thread gyro_thread(::std::ref(gyro_sender));
725
726 DrivetrainWriter drivetrain_writer;
727 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700728 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700729 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700730 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700731 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
732
Brian Silverman552350b2015-08-02 18:23:34 -0700733 ::frc971::wpilib::ClawWriter claw_writer;
734 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
735 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
736 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
737 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
738 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
739 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700740 ::std::thread claw_writer_thread(::std::ref(claw_writer));
741
Brian Silverman552350b2015-08-02 18:23:34 -0700742 ::frc971::wpilib::ShooterWriter shooter_writer;
743 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
744 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
745
Brian Silverman17f503e2015-08-02 18:17:18 -0700746 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
747 new ::frc971::wpilib::BufferedPcm());
748 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700749 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
750 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
751 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
752 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700753
Austin Schuh016a6b02015-10-08 06:41:14 +0000754 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700755 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
756 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
757
758 // Wait forever. Not much else to do...
759 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
760
761 LOG(ERROR, "Exiting WPILibRobot\n");
762
763 joystick_sender.Quit();
764 joystick_thread.join();
765 reader.Quit();
766 reader_thread.join();
767 gyro_sender.Quit();
768 gyro_thread.join();
769
770 drivetrain_writer.Quit();
771 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700772 shooter_writer.Quit();
773 shooter_writer_thread.join();
774 claw_writer.Quit();
775 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700776 solenoid_writer.Quit();
777 solenoid_thread.join();
778
779 ::aos::Cleanup();
780 }
781};
782
783} // namespace wpilib
784} // namespace frc971
785
786
787START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);