blob: 9d6b8b3091cd615ae6777a0c40eabe066ce9a0a2 [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"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -050021#include "PowerDistributionPanel.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070022#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 Silverman811f8ec2015-12-06 01:29:42 -050035
Brian Silverman17f503e2015-08-02 18:17:18 -070036#include "y2014/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070037#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070038#include "y2014/control_loops/shooter/shooter.q.h"
39#include "y2014/constants.h"
40#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070041
Brian Silverman17f503e2015-08-02 18:17:18 -070042#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"
Brian Silverman811f8ec2015-12-06 01:29:42 -050051#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070052
Brian Silverman17f503e2015-08-02 18:17:18 -070053#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
Brian Silverman17f503e2015-08-02 18:17:18 -070057using ::frc971::control_loops::drivetrain_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070058using ::frc971::control_loops::claw_queue;
Brian Silverman552350b2015-08-02 18:23:34 -070059using ::frc971::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070060
61namespace frc971 {
62namespace wpilib {
63
Brian Silverman85fbb602015-08-29 19:28:20 -070064// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
65// DMA stuff and then removing the * 2.0 in *_translate.
66// The low bit is direction.
67
Brian Silverman552350b2015-08-02 18:23:34 -070068// TODO(brian): Replace this with ::std::make_unique once all our toolchains
69// have support.
70template <class T, class... U>
71std::unique_ptr<T> make_unique(U &&... u) {
72 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
73}
74
Brian Silverman17f503e2015-08-02 18:17:18 -070075double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070076 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070077 (256.0 /*cpr*/ * 4.0 /*4x*/) *
78 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080079 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070080}
81
Brian Silverman552350b2015-08-02 18:23:34 -070082float hall_translate(const constants::ShifterHallEffect &k, float in_low,
83 float in_high) {
84 const float low_ratio =
85 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
86 static_cast<float>(k.low_gear_middle - k.low_gear_low);
87 const float high_ratio =
88 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
89 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070090
Brian Silverman552350b2015-08-02 18:23:34 -070091 // Return low when we are below 1/2, and high when we are above 1/2.
92 if (low_ratio + high_ratio < 1.0) {
93 return low_ratio;
94 } else {
95 return high_ratio;
96 }
Brian Silverman17f503e2015-08-02 18:17:18 -070097}
98
99double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700100 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700101 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700102 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700103}
104
Brian Silverman552350b2015-08-02 18:23:34 -0700105double shooter_translate(int32_t in) {
106 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
107 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
108 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700109}
110
111static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700112 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
113 18.0 / 32.0 /* big belt reduction */ *
114 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
115 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700116
117class SensorReader {
118 public:
119 SensorReader() {
120 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
121 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700122 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700123 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700124 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700125 }
126
Brian Silverman552350b2015-08-02 18:23:34 -0700127 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
128 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700129 }
130
Brian Silverman552350b2015-08-02 18:23:34 -0700131 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
132 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700133 }
134
Brian Silverman552350b2015-08-02 18:23:34 -0700135 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
136 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 }
138
Brian Silverman552350b2015-08-02 18:23:34 -0700139 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
140 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700141 }
142
Brian Silverman552350b2015-08-02 18:23:34 -0700143 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
144 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700145 }
146
Brian Silverman552350b2015-08-02 18:23:34 -0700147 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
148 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700149 }
150
Brian Silverman552350b2015-08-02 18:23:34 -0700151 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
152 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700153 }
154
Brian Silverman552350b2015-08-02 18:23:34 -0700155 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
156 encoder_filter_.Add(encoder.get());
157 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700158 }
159
Austin Schuhc5e36082015-10-31 13:30:46 -0700160 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700161 hall_filter_.Add(hall.get());
162 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700163 }
164
Austin Schuhc5e36082015-10-31 13:30:46 -0700165 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700166 hall_filter_.Add(hall.get());
167 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700168 }
169
Austin Schuhc5e36082015-10-31 13:30:46 -0700170 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700171 hall_filter_.Add(hall.get());
172 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700173 }
174
Brian Silverman552350b2015-08-02 18:23:34 -0700175 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
176 encoder_filter_.Add(encoder.get());
177 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700178 }
179
Austin Schuhc5e36082015-10-31 13:30:46 -0700180 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700181 hall_filter_.Add(hall.get());
182 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700183 }
184
Austin Schuhc5e36082015-10-31 13:30:46 -0700185 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700186 hall_filter_.Add(hall.get());
187 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 }
189
Austin Schuhc5e36082015-10-31 13:30:46 -0700190 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700191 hall_filter_.Add(hall.get());
192 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700193 }
194
Brian Silverman552350b2015-08-02 18:23:34 -0700195 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
196 encoder_filter_.Add(encoder.get());
197 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700198 }
199
Austin Schuhc5e36082015-10-31 13:30:46 -0700200 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700201 hall_filter_.Add(hall.get());
202 shooter_proximal_ = ::std::move(hall);
203 }
204
Austin Schuhc5e36082015-10-31 13:30:46 -0700205 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700206 hall_filter_.Add(hall.get());
207 shooter_distal_ = ::std::move(hall);
208 }
209
Austin Schuhc5e36082015-10-31 13:30:46 -0700210 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700211 hall_filter_.Add(hall.get());
212 shooter_plunger_ = ::std::move(hall);
213 shooter_plunger_reader_ =
214 make_unique<DMADigitalReader>(shooter_plunger_.get());
215 }
216
Austin Schuhc5e36082015-10-31 13:30:46 -0700217 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700218 hall_filter_.Add(hall.get());
219 shooter_latch_ = ::std::move(hall);
220 shooter_latch_reader_ = make_unique<DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700221 }
222
223 // All of the DMA-related set_* calls must be made before this, and it doesn't
224 // hurt to do all of them.
225 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silverman552350b2015-08-02 18:23:34 -0700226 shooter_proximal_counter_ = make_unique<DMAEdgeCounter>(
227 shooter_encoder_.get(), shooter_proximal_.get());
228 shooter_distal_counter_ = make_unique<DMAEdgeCounter>(
229 shooter_encoder_.get(), shooter_distal_.get());
230
Brian Silverman17f503e2015-08-02 18:17:18 -0700231 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700232 dma_synchronizer_->Add(shooter_proximal_counter_.get());
233 dma_synchronizer_->Add(shooter_distal_counter_.get());
234 dma_synchronizer_->Add(shooter_plunger_reader_.get());
235 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700236 }
237
238 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700239 ::aos::SetCurrentThreadName("SensorReader");
Brian Silverman552350b2015-08-02 18:23:34 -0700240 LOG(INFO, "In sensor reader thread\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700241
242 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700243 ds_ =
244#ifdef WPILIB2015
245 DriverStation::GetInstance();
246#else
247 &DriverStation::GetInstance();
248#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500249 pdp_.reset(new PowerDistributionPanel());
Brian Silverman17f503e2015-08-02 18:17:18 -0700250
Brian Silverman552350b2015-08-02 18:23:34 -0700251 top_reader_.Start();
252 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700253 dma_synchronizer_->Start();
254 LOG(INFO, "Things are now started\n");
255
256 ::aos::SetCurrentThreadRealtimePriority(kPriority);
257 while (run_) {
Austin Schuhadf2cde2015-11-08 20:35:16 -0800258 ::aos::time::PhasedLoopXMS(5, 4000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700259 RunIteration();
260 }
261
Brian Silverman552350b2015-08-02 18:23:34 -0700262 top_reader_.Quit();
263 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700264 }
265
266 void RunIteration() {
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500267 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700268
Brian Silverman552350b2015-08-02 18:23:34 -0700269 const auto &values = constants::GetValues();
270
Brian Silverman17f503e2015-08-02 18:17:18 -0700271 {
272 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
273 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700274 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700275 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700276 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Austin Schuha0c1e152015-11-08 14:10:13 -0800277
278 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
279 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700280 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800281 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
282 drivetrain_message->high_left_hall);
283
284 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
285 drivetrain_message->high_right_hall =
286 high_right_drive_hall_->GetVoltage();
287 drivetrain_message->right_shifter_position =
288 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
289 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700290
291 drivetrain_message.Send();
292 }
293
Brian Silverman552350b2015-08-02 18:23:34 -0700294 ::frc971::sensors::auto_mode.MakeWithBuilder()
295 .voltage(auto_selector_analog_->GetVoltage())
296 .Send();
297
Brian Silverman17f503e2015-08-02 18:17:18 -0700298 dma_synchronizer_->RunIteration();
299
Brian Silverman17f503e2015-08-02 18:17:18 -0700300 {
Brian Silverman552350b2015-08-02 18:23:34 -0700301 auto shooter_message = shooter_queue.position.MakeMessage();
302 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800303 shooter_message->plunger = !shooter_plunger_reader_->value();
304 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700305 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
306 &shooter_message->pusher_proximal);
307 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
308 &shooter_message->pusher_distal);
309
310 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700311 }
312
313 {
314 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700315 top_reader_.RunIteration(&claw_message->top);
316 bottom_reader_.RunIteration(&claw_message->bottom);
317
Brian Silverman17f503e2015-08-02 18:17:18 -0700318 claw_message.Send();
319 }
320 }
321
322 void Quit() { run_ = false; }
323
324 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700325 class HalfClawReader {
326 public:
327 HalfClawReader(bool reversed) : reversed_(reversed) {}
328
329 void set_encoder(::std::unique_ptr<Encoder> encoder) {
330 encoder_ = ::std::move(encoder);
331 }
332
Austin Schuhc5e36082015-10-31 13:30:46 -0700333 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700334 front_hall_ = ::std::move(front_hall);
335 }
336
337 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700338 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700339 calibration_hall_ = ::std::move(calibration_hall);
340 }
341
Austin Schuhc5e36082015-10-31 13:30:46 -0700342 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700343 back_hall_ = ::std::move(back_hall);
344 }
345
346 void Start() {
347 front_counter_ =
348 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
349 synchronizer_.Add(front_counter_.get());
350 calibration_counter_ =
351 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
352 synchronizer_.Add(calibration_counter_.get());
353 back_counter_ =
354 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
355 synchronizer_.Add(back_counter_.get());
356 synchronized_encoder_ =
357 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
358 synchronizer_.Add(synchronized_encoder_.get());
359
360 synchronizer_.Start();
361 }
362
363 void Quit() { synchronizer_.Quit(); }
364
365 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
366 const double multiplier = reversed_ ? -1.0 : 1.0;
367
368 synchronizer_.RunIteration();
369
370 CopyPosition(front_counter_.get(), &half_claw_position->front);
371 CopyPosition(calibration_counter_.get(),
372 &half_claw_position->calibration);
373 CopyPosition(back_counter_.get(), &half_claw_position->back);
374 half_claw_position->position =
375 multiplier * claw_translate(synchronized_encoder_->get());
376 }
377
378 private:
379 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
380 const double multiplier = reversed_ ? -1.0 : 1.0;
381
Austin Schuh5c25ab72015-11-01 13:17:11 -0800382 out->current = !counter->polled_value();
383 out->posedge_count = counter->negative_interrupt_count();
384 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700385 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800386 multiplier * claw_translate(counter->last_positive_encoder_value());
387 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700388 multiplier * claw_translate(counter->last_negative_encoder_value());
389 }
390
391 InterruptSynchronizer synchronizer_{kInterruptPriority};
392
393 ::std::unique_ptr<EdgeCounter> front_counter_;
394 ::std::unique_ptr<EdgeCounter> calibration_counter_;
395 ::std::unique_ptr<EdgeCounter> back_counter_;
396 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
397
398 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700399 ::std::unique_ptr<DigitalInput> front_hall_;
400 ::std::unique_ptr<DigitalInput> calibration_hall_;
401 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700402
403 const bool reversed_;
404 };
405
Brian Silverman17f503e2015-08-02 18:17:18 -0700406 static const int kPriority = 30;
407 static const int kInterruptPriority = 55;
408
Brian Silverman552350b2015-08-02 18:23:34 -0700409 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
410 PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800411 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700412 // These are inverted because the hall effects give logical false when
413 // there's a magnet in front of them.
414 output->posedge_count = counter->negative_count();
415 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700416 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700417 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700418 }
419
Brian Silverman17f503e2015-08-02 18:17:18 -0700420 int32_t my_pid_;
421 DriverStation *ds_;
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500422 ::std::unique_ptr<PowerDistributionPanel> pdp_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700423
Brian Silverman17f503e2015-08-02 18:17:18 -0700424 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
425
Brian Silverman552350b2015-08-02 18:23:34 -0700426 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700427
Brian Silverman552350b2015-08-02 18:23:34 -0700428 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
429 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
430 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
431 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
432 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
433 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700434
Brian Silverman552350b2015-08-02 18:23:34 -0700435 HalfClawReader top_reader_{false}, bottom_reader_{true};
436
437 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700438 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
439 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silverman552350b2015-08-02 18:23:34 -0700440 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
441 shooter_distal_counter_;
442 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
443 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700444
445 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700446 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700447};
448
449class SolenoidWriter {
450 public:
451 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
452 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700453 shooter_(".frc971.control_loops.shooter_queue.output"),
454 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700455
Austin Schuhc5e36082015-10-31 13:30:46 -0700456 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700457 pressure_switch_ = ::std::move(pressure_switch);
458 }
459
460 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
461 compressor_relay_ = ::std::move(compressor_relay);
462 }
463
Brian Silverman552350b2015-08-02 18:23:34 -0700464 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
465 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700466 }
467
Brian Silverman552350b2015-08-02 18:23:34 -0700468 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
469 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700470 }
471
Brian Silverman552350b2015-08-02 18:23:34 -0700472 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
473 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700474 }
475
Brian Silverman552350b2015-08-02 18:23:34 -0700476 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
477 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700478 }
479
480 void operator()() {
481 ::aos::SetCurrentThreadName("Solenoids");
482 ::aos::SetCurrentThreadRealtimePriority(30);
483
484 while (run_) {
485 ::aos::time::PhasedLoopXMS(20, 1000);
486
487 {
Brian Silverman552350b2015-08-02 18:23:34 -0700488 shooter_.FetchLatest();
489 if (shooter_.get()) {
490 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
491 shooter_latch_->Set(!shooter_->latch_piston);
492 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700493 }
494 }
495
496 {
Brian Silverman552350b2015-08-02 18:23:34 -0700497 drivetrain_.FetchLatest();
498 if (drivetrain_.get()) {
499 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800500 drivetrain_left_->Set(!drivetrain_->left_high);
501 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700502 }
503 }
504
Brian Silverman17f503e2015-08-02 18:17:18 -0700505 {
506 PneumaticsToLog to_log;
507 {
508 const bool compressor_on = !pressure_switch_->Get();
509 to_log.compressor_on = compressor_on;
510 if (compressor_on) {
511 compressor_relay_->Set(Relay::kForward);
512 } else {
513 compressor_relay_->Set(Relay::kOff);
514 }
515 }
516
517 pcm_->Flush();
518 to_log.read_solenoids = pcm_->GetAll();
519 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
520 }
521 }
522 }
523
524 void Quit() { run_ = false; }
525
526 private:
527 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700528
529 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
530 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
531 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
532 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
533
Austin Schuhc5e36082015-10-31 13:30:46 -0700534 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700535 ::std::unique_ptr<Relay> compressor_relay_;
536
Brian Silverman552350b2015-08-02 18:23:34 -0700537 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
538 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700539
540 ::std::atomic<bool> run_{true};
541};
542
543class DrivetrainWriter : public LoopOutputHandler {
544 public:
545 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
546 left_drivetrain_talon_ = ::std::move(t);
547 }
548
549 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
550 right_drivetrain_talon_ = ::std::move(t);
551 }
552
553 private:
554 virtual void Read() override {
555 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
556 }
557
558 virtual void Write() override {
559 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
560 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700561 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
562 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700563 }
564
565 virtual void Stop() override {
566 LOG(WARNING, "drivetrain output too old\n");
567 left_drivetrain_talon_->Disable();
568 right_drivetrain_talon_->Disable();
569 }
570
571 ::std::unique_ptr<Talon> left_drivetrain_talon_;
572 ::std::unique_ptr<Talon> right_drivetrain_talon_;
573};
574
Brian Silverman552350b2015-08-02 18:23:34 -0700575class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700576 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700577 void set_shooter_talon(::std::unique_ptr<Talon> t) {
578 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700579 }
580
581 private:
582 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400583 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700584 }
585
586 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400587 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700588 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700589 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 }
591
592 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700593 LOG(WARNING, "shooter output too old\n");
594 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700595 }
596
Brian Silverman552350b2015-08-02 18:23:34 -0700597 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700598};
599
600class ClawWriter : public LoopOutputHandler {
601 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700602 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
603 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700604 }
605
Brian Silverman552350b2015-08-02 18:23:34 -0700606 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
607 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700608 }
609
Brian Silverman552350b2015-08-02 18:23:34 -0700610 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
611 left_tusk_talon_ = ::std::move(t);
612 }
613
614 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
615 right_tusk_talon_ = ::std::move(t);
616 }
617
618 void set_intake1_talon(::std::unique_ptr<Talon> t) {
619 intake1_talon_ = ::std::move(t);
620 }
621
622 void set_intake2_talon(::std::unique_ptr<Talon> t) {
623 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700624 }
625
626 private:
627 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400628 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700629 }
630
631 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400632 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700633 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700634 intake1_talon_->Set(queue->intake_voltage / 12.0);
635 intake2_talon_->Set(queue->intake_voltage / 12.0);
636 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
637 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
638 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
639 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700640 }
641
642 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700643 LOG(WARNING, "claw output too old\n");
644 intake1_talon_->Disable();
645 intake2_talon_->Disable();
646 bottom_claw_talon_->Disable();
647 top_claw_talon_->Disable();
648 left_tusk_talon_->Disable();
649 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700650 }
651
Brian Silverman552350b2015-08-02 18:23:34 -0700652 ::std::unique_ptr<Talon> top_claw_talon_;
653 ::std::unique_ptr<Talon> bottom_claw_talon_;
654 ::std::unique_ptr<Talon> left_tusk_talon_;
655 ::std::unique_ptr<Talon> right_tusk_talon_;
656 ::std::unique_ptr<Talon> intake1_talon_;
657 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700658};
659
Brian Silverman17f503e2015-08-02 18:17:18 -0700660class WPILibRobot : public RobotBase {
661 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700662 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700663 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
664 Encoder::k4X);
665 }
Brian Silverman552350b2015-08-02 18:23:34 -0700666
Brian Silverman17f503e2015-08-02 18:17:18 -0700667 virtual void StartCompetition() {
668 ::aos::InitNRT();
669 ::aos::SetCurrentThreadName("StartCompetition");
670
671 JoystickSender joystick_sender;
672 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700673
674 SensorReader reader;
675 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700676
Brian Silverman85fbb602015-08-29 19:28:20 -0700677 // Create this first to make sure it ends up in one of the lower-numbered
678 // FPGA slots so we can use it with DMA.
679 auto shooter_encoder_temp = make_encoder(2);
680
Brian Silverman552350b2015-08-02 18:23:34 -0700681 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700682
Brian Silverman552350b2015-08-02 18:23:34 -0700683 reader.set_drivetrain_left_encoder(make_encoder(0));
684 reader.set_drivetrain_right_encoder(make_encoder(1));
685 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
686 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
687 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
688 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700689
Brian Silverman552350b2015-08-02 18:23:34 -0700690 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800691 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
692 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
693 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700694
Brian Silverman85fbb602015-08-29 19:28:20 -0700695 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800696 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
697 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
698 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700699
Brian Silverman85fbb602015-08-29 19:28:20 -0700700 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800701 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
702 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
703 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
704 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700705
Brian Silverman17f503e2015-08-02 18:17:18 -0700706 reader.set_dma(make_unique<DMA>());
707 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700708
Brian Silverman17f503e2015-08-02 18:17:18 -0700709 GyroSender gyro_sender;
710 ::std::thread gyro_thread(::std::ref(gyro_sender));
711
712 DrivetrainWriter drivetrain_writer;
713 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700714 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700715 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700716 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700717 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
718
Brian Silverman552350b2015-08-02 18:23:34 -0700719 ::frc971::wpilib::ClawWriter claw_writer;
720 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
721 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
722 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
723 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
724 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
725 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700726 ::std::thread claw_writer_thread(::std::ref(claw_writer));
727
Brian Silverman552350b2015-08-02 18:23:34 -0700728 ::frc971::wpilib::ShooterWriter shooter_writer;
729 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
730 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
731
Brian Silverman17f503e2015-08-02 18:17:18 -0700732 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
733 new ::frc971::wpilib::BufferedPcm());
734 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700735 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
736 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
737 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
738 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700739
Austin Schuh016a6b02015-10-08 06:41:14 +0000740 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700741 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
742 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
743
744 // Wait forever. Not much else to do...
745 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
746
747 LOG(ERROR, "Exiting WPILibRobot\n");
748
749 joystick_sender.Quit();
750 joystick_thread.join();
751 reader.Quit();
752 reader_thread.join();
753 gyro_sender.Quit();
754 gyro_thread.join();
755
756 drivetrain_writer.Quit();
757 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700758 shooter_writer.Quit();
759 shooter_writer_thread.join();
760 claw_writer.Quit();
761 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700762 solenoid_writer.Quit();
763 solenoid_thread.join();
764
765 ::aos::Cleanup();
766 }
767};
768
769} // namespace wpilib
770} // namespace frc971
771
772
773START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);