blob: c8792d000a2a7fac4983fd25ea2cf3b678644d7c [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());
292 drivetrain_message->left_shifter_position =
293 hall_translate(values.left_drive, low_left_drive_hall_->GetVoltage(),
294 high_left_drive_hall_->GetVoltage());
295 drivetrain_message->right_shifter_position = hall_translate(
296 values.right_drive, low_right_drive_hall_->GetVoltage(),
297 high_right_drive_hall_->GetVoltage());
Brian Silverman17f503e2015-08-02 18:17:18 -0700298
299 drivetrain_message.Send();
300 }
301
Brian Silverman552350b2015-08-02 18:23:34 -0700302 ::frc971::sensors::auto_mode.MakeWithBuilder()
303 .voltage(auto_selector_analog_->GetVoltage())
304 .Send();
305
Brian Silverman17f503e2015-08-02 18:17:18 -0700306 dma_synchronizer_->RunIteration();
307
Brian Silverman17f503e2015-08-02 18:17:18 -0700308 {
Brian Silverman552350b2015-08-02 18:23:34 -0700309 auto shooter_message = shooter_queue.position.MakeMessage();
310 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800311 shooter_message->plunger = !shooter_plunger_reader_->value();
312 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700313 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
314 &shooter_message->pusher_proximal);
315 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
316 &shooter_message->pusher_distal);
317
318 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700319 }
320
321 {
322 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700323 top_reader_.RunIteration(&claw_message->top);
324 bottom_reader_.RunIteration(&claw_message->bottom);
325
Brian Silverman17f503e2015-08-02 18:17:18 -0700326 claw_message.Send();
327 }
328 }
329
330 void Quit() { run_ = false; }
331
332 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700333 class HalfClawReader {
334 public:
335 HalfClawReader(bool reversed) : reversed_(reversed) {}
336
337 void set_encoder(::std::unique_ptr<Encoder> encoder) {
338 encoder_ = ::std::move(encoder);
339 }
340
Austin Schuhc5e36082015-10-31 13:30:46 -0700341 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700342 front_hall_ = ::std::move(front_hall);
343 }
344
345 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700346 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700347 calibration_hall_ = ::std::move(calibration_hall);
348 }
349
Austin Schuhc5e36082015-10-31 13:30:46 -0700350 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700351 back_hall_ = ::std::move(back_hall);
352 }
353
354 void Start() {
355 front_counter_ =
356 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
357 synchronizer_.Add(front_counter_.get());
358 calibration_counter_ =
359 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
360 synchronizer_.Add(calibration_counter_.get());
361 back_counter_ =
362 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
363 synchronizer_.Add(back_counter_.get());
364 synchronized_encoder_ =
365 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
366 synchronizer_.Add(synchronized_encoder_.get());
367
368 synchronizer_.Start();
369 }
370
371 void Quit() { synchronizer_.Quit(); }
372
373 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
374 const double multiplier = reversed_ ? -1.0 : 1.0;
375
376 synchronizer_.RunIteration();
377
378 CopyPosition(front_counter_.get(), &half_claw_position->front);
379 CopyPosition(calibration_counter_.get(),
380 &half_claw_position->calibration);
381 CopyPosition(back_counter_.get(), &half_claw_position->back);
382 half_claw_position->position =
383 multiplier * claw_translate(synchronized_encoder_->get());
384 }
385
386 private:
387 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
388 const double multiplier = reversed_ ? -1.0 : 1.0;
389
Austin Schuh5c25ab72015-11-01 13:17:11 -0800390 out->current = !counter->polled_value();
391 out->posedge_count = counter->negative_interrupt_count();
392 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700393 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800394 multiplier * claw_translate(counter->last_positive_encoder_value());
395 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700396 multiplier * claw_translate(counter->last_negative_encoder_value());
397 }
398
399 InterruptSynchronizer synchronizer_{kInterruptPriority};
400
401 ::std::unique_ptr<EdgeCounter> front_counter_;
402 ::std::unique_ptr<EdgeCounter> calibration_counter_;
403 ::std::unique_ptr<EdgeCounter> back_counter_;
404 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
405
406 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700407 ::std::unique_ptr<DigitalInput> front_hall_;
408 ::std::unique_ptr<DigitalInput> calibration_hall_;
409 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700410
411 const bool reversed_;
412 };
413
Brian Silverman17f503e2015-08-02 18:17:18 -0700414 static const int kPriority = 30;
415 static const int kInterruptPriority = 55;
416
Brian Silverman552350b2015-08-02 18:23:34 -0700417 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
418 PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800419 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700420 // These are inverted because the hall effects give logical false when
421 // there's a magnet in front of them.
422 output->posedge_count = counter->negative_count();
423 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700424 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700425 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700426 }
427
Brian Silverman17f503e2015-08-02 18:17:18 -0700428 int32_t my_pid_;
429 DriverStation *ds_;
430
Brian Silverman17f503e2015-08-02 18:17:18 -0700431 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
432
Brian Silverman552350b2015-08-02 18:23:34 -0700433 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700434
Brian Silverman552350b2015-08-02 18:23:34 -0700435 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
436 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
437 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
438 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
439 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
440 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700441
Brian Silverman552350b2015-08-02 18:23:34 -0700442 HalfClawReader top_reader_{false}, bottom_reader_{true};
443
444 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700445 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
446 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silverman552350b2015-08-02 18:23:34 -0700447 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
448 shooter_distal_counter_;
449 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
450 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700451
452 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700453 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700454};
455
456class SolenoidWriter {
457 public:
458 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
459 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700460 shooter_(".frc971.control_loops.shooter_queue.output"),
461 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700462
Austin Schuhc5e36082015-10-31 13:30:46 -0700463 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700464 pressure_switch_ = ::std::move(pressure_switch);
465 }
466
467 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
468 compressor_relay_ = ::std::move(compressor_relay);
469 }
470
Brian Silverman552350b2015-08-02 18:23:34 -0700471 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
472 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700473 }
474
Brian Silverman552350b2015-08-02 18:23:34 -0700475 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
476 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700477 }
478
Brian Silverman552350b2015-08-02 18:23:34 -0700479 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
480 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700481 }
482
Brian Silverman552350b2015-08-02 18:23:34 -0700483 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
484 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700485 }
486
487 void operator()() {
488 ::aos::SetCurrentThreadName("Solenoids");
489 ::aos::SetCurrentThreadRealtimePriority(30);
490
491 while (run_) {
492 ::aos::time::PhasedLoopXMS(20, 1000);
493
494 {
Brian Silverman552350b2015-08-02 18:23:34 -0700495 shooter_.FetchLatest();
496 if (shooter_.get()) {
497 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
498 shooter_latch_->Set(!shooter_->latch_piston);
499 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700500 }
501 }
502
503 {
Brian Silverman552350b2015-08-02 18:23:34 -0700504 drivetrain_.FetchLatest();
505 if (drivetrain_.get()) {
506 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
507 drivetrain_left_->Set(drivetrain_->left_high);
508 drivetrain_right_->Set(drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700509 }
510 }
511
Brian Silverman17f503e2015-08-02 18:17:18 -0700512 {
513 PneumaticsToLog to_log;
514 {
515 const bool compressor_on = !pressure_switch_->Get();
516 to_log.compressor_on = compressor_on;
517 if (compressor_on) {
518 compressor_relay_->Set(Relay::kForward);
519 } else {
520 compressor_relay_->Set(Relay::kOff);
521 }
522 }
523
524 pcm_->Flush();
525 to_log.read_solenoids = pcm_->GetAll();
526 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
527 }
528 }
529 }
530
531 void Quit() { run_ = false; }
532
533 private:
534 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700535
536 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
537 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
538 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
539 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
540
Austin Schuhc5e36082015-10-31 13:30:46 -0700541 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700542 ::std::unique_ptr<Relay> compressor_relay_;
543
Brian Silverman552350b2015-08-02 18:23:34 -0700544 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
545 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700546
547 ::std::atomic<bool> run_{true};
548};
549
550class DrivetrainWriter : public LoopOutputHandler {
551 public:
552 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
553 left_drivetrain_talon_ = ::std::move(t);
554 }
555
556 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
557 right_drivetrain_talon_ = ::std::move(t);
558 }
559
560 private:
561 virtual void Read() override {
562 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
563 }
564
565 virtual void Write() override {
566 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
567 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700568 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
569 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700570 }
571
572 virtual void Stop() override {
573 LOG(WARNING, "drivetrain output too old\n");
574 left_drivetrain_talon_->Disable();
575 right_drivetrain_talon_->Disable();
576 }
577
578 ::std::unique_ptr<Talon> left_drivetrain_talon_;
579 ::std::unique_ptr<Talon> right_drivetrain_talon_;
580};
581
Brian Silverman552350b2015-08-02 18:23:34 -0700582class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700583 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700584 void set_shooter_talon(::std::unique_ptr<Talon> t) {
585 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700586 }
587
588 private:
589 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400590 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700591 }
592
593 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400594 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700595 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700596 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700597 }
598
599 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700600 LOG(WARNING, "shooter output too old\n");
601 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700602 }
603
Brian Silverman552350b2015-08-02 18:23:34 -0700604 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700605};
606
607class ClawWriter : public LoopOutputHandler {
608 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700609 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
610 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700611 }
612
Brian Silverman552350b2015-08-02 18:23:34 -0700613 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
614 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700615 }
616
Brian Silverman552350b2015-08-02 18:23:34 -0700617 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
618 left_tusk_talon_ = ::std::move(t);
619 }
620
621 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
622 right_tusk_talon_ = ::std::move(t);
623 }
624
625 void set_intake1_talon(::std::unique_ptr<Talon> t) {
626 intake1_talon_ = ::std::move(t);
627 }
628
629 void set_intake2_talon(::std::unique_ptr<Talon> t) {
630 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700631 }
632
633 private:
634 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400635 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700636 }
637
638 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400639 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700640 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700641 intake1_talon_->Set(queue->intake_voltage / 12.0);
642 intake2_talon_->Set(queue->intake_voltage / 12.0);
643 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
644 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
645 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
646 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700647 }
648
649 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700650 LOG(WARNING, "claw output too old\n");
651 intake1_talon_->Disable();
652 intake2_talon_->Disable();
653 bottom_claw_talon_->Disable();
654 top_claw_talon_->Disable();
655 left_tusk_talon_->Disable();
656 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700657 }
658
Brian Silverman552350b2015-08-02 18:23:34 -0700659 ::std::unique_ptr<Talon> top_claw_talon_;
660 ::std::unique_ptr<Talon> bottom_claw_talon_;
661 ::std::unique_ptr<Talon> left_tusk_talon_;
662 ::std::unique_ptr<Talon> right_tusk_talon_;
663 ::std::unique_ptr<Talon> intake1_talon_;
664 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700665};
666
Brian Silverman17f503e2015-08-02 18:17:18 -0700667class WPILibRobot : public RobotBase {
668 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700669 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700670 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
671 Encoder::k4X);
672 }
Brian Silverman552350b2015-08-02 18:23:34 -0700673
Brian Silverman17f503e2015-08-02 18:17:18 -0700674 virtual void StartCompetition() {
675 ::aos::InitNRT();
676 ::aos::SetCurrentThreadName("StartCompetition");
677
678 JoystickSender joystick_sender;
679 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700680
681 SensorReader reader;
682 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700683
Brian Silverman85fbb602015-08-29 19:28:20 -0700684 // Create this first to make sure it ends up in one of the lower-numbered
685 // FPGA slots so we can use it with DMA.
686 auto shooter_encoder_temp = make_encoder(2);
687
Brian Silverman552350b2015-08-02 18:23:34 -0700688 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700689
Brian Silverman552350b2015-08-02 18:23:34 -0700690 reader.set_drivetrain_left_encoder(make_encoder(0));
691 reader.set_drivetrain_right_encoder(make_encoder(1));
692 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
693 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
694 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
695 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700696
Brian Silverman552350b2015-08-02 18:23:34 -0700697 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800698 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
699 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
700 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700701
Brian Silverman85fbb602015-08-29 19:28:20 -0700702 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800703 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
704 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
705 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700706
Brian Silverman85fbb602015-08-29 19:28:20 -0700707 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800708 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
709 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
710 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
711 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700712
Brian Silverman17f503e2015-08-02 18:17:18 -0700713 reader.set_dma(make_unique<DMA>());
714 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700715
Brian Silverman17f503e2015-08-02 18:17:18 -0700716 GyroSender gyro_sender;
717 ::std::thread gyro_thread(::std::ref(gyro_sender));
718
719 DrivetrainWriter drivetrain_writer;
720 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700721 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700722 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700723 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700724 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
725
Brian Silverman552350b2015-08-02 18:23:34 -0700726 ::frc971::wpilib::ClawWriter claw_writer;
727 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
728 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
729 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
730 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
731 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
732 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700733 ::std::thread claw_writer_thread(::std::ref(claw_writer));
734
Brian Silverman552350b2015-08-02 18:23:34 -0700735 ::frc971::wpilib::ShooterWriter shooter_writer;
736 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
737 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
738
Brian Silverman17f503e2015-08-02 18:17:18 -0700739 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
740 new ::frc971::wpilib::BufferedPcm());
741 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700742 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
743 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
744 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
745 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700746
Austin Schuh016a6b02015-10-08 06:41:14 +0000747 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700748 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
749 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
750
751 // Wait forever. Not much else to do...
752 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
753
754 LOG(ERROR, "Exiting WPILibRobot\n");
755
756 joystick_sender.Quit();
757 joystick_thread.join();
758 reader.Quit();
759 reader_thread.join();
760 gyro_sender.Quit();
761 gyro_thread.join();
762
763 drivetrain_writer.Quit();
764 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700765 shooter_writer.Quit();
766 shooter_writer_thread.join();
767 claw_writer.Quit();
768 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700769 solenoid_writer.Quit();
770 solenoid_thread.join();
771
772 ::aos::Cleanup();
773 }
774};
775
776} // namespace wpilib
777} // namespace frc971
778
779
780START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);