blob: 43528145c11b19fa31f0d40eda0229cba892340c [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
21#undef ERROR
22
Brian Silverman17f503e2015-08-02 18:17:18 -070023#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
25#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
29#include "aos/common/stl_mutex.h"
30#include "aos/linux_code/init.h"
31#include "aos/common/messages/robot_state.q.h"
32
Brian Silverman552350b2015-08-02 18:23:34 -070033#include "frc971/shifter_hall_effect.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050034
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"
Brian Silverman811f8ec2015-12-06 01:29:42 -050050#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070051
Brian Silverman17f503e2015-08-02 18:17:18 -070052#ifndef M_PI
53#define M_PI 3.14159265358979323846
54#endif
55
Brian Silverman17f503e2015-08-02 18:17:18 -070056using ::frc971::control_loops::drivetrain_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070057using ::frc971::control_loops::claw_queue;
Brian Silverman552350b2015-08-02 18:23:34 -070058using ::frc971::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070059
60namespace frc971 {
61namespace wpilib {
62
Brian Silverman85fbb602015-08-29 19:28:20 -070063// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
64// DMA stuff and then removing the * 2.0 in *_translate.
65// The low bit is direction.
66
Brian Silverman552350b2015-08-02 18:23:34 -070067// TODO(brian): Replace this with ::std::make_unique once all our toolchains
68// have support.
69template <class T, class... U>
70std::unique_ptr<T> make_unique(U &&... u) {
71 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
72}
73
Brian Silverman17f503e2015-08-02 18:17:18 -070074double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070075 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070076 (256.0 /*cpr*/ * 4.0 /*4x*/) *
77 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080078 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070079}
80
Brian Silverman552350b2015-08-02 18:23:34 -070081float hall_translate(const constants::ShifterHallEffect &k, float in_low,
82 float in_high) {
83 const float low_ratio =
84 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
85 static_cast<float>(k.low_gear_middle - k.low_gear_low);
86 const float high_ratio =
87 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
88 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070089
Brian Silverman552350b2015-08-02 18:23:34 -070090 // Return low when we are below 1/2, and high when we are above 1/2.
91 if (low_ratio + high_ratio < 1.0) {
92 return low_ratio;
93 } else {
94 return high_ratio;
95 }
Brian Silverman17f503e2015-08-02 18:17:18 -070096}
97
98double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070099 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700100 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700101 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700102}
103
Brian Silverman552350b2015-08-02 18:23:34 -0700104double shooter_translate(int32_t in) {
105 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
106 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
107 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700108}
109
110static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700111 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
112 18.0 / 32.0 /* big belt reduction */ *
113 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
114 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700115
116class SensorReader {
117 public:
118 SensorReader() {
119 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
120 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700121 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700122 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700123 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 }
125
Brian Silverman552350b2015-08-02 18:23:34 -0700126 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
127 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
129
Brian Silverman552350b2015-08-02 18:23:34 -0700130 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
131 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
135 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 }
137
Brian Silverman552350b2015-08-02 18:23:34 -0700138 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
139 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 }
141
Brian Silverman552350b2015-08-02 18:23:34 -0700142 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
143 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 }
145
Brian Silverman552350b2015-08-02 18:23:34 -0700146 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
147 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Brian Silverman552350b2015-08-02 18:23:34 -0700150 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
151 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700152 }
153
Brian Silverman552350b2015-08-02 18:23:34 -0700154 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
155 encoder_filter_.Add(encoder.get());
156 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700157 }
158
Austin Schuhc5e36082015-10-31 13:30:46 -0700159 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700160 hall_filter_.Add(hall.get());
161 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700162 }
163
Austin Schuhc5e36082015-10-31 13:30:46 -0700164 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700165 hall_filter_.Add(hall.get());
166 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700167 }
168
Austin Schuhc5e36082015-10-31 13:30:46 -0700169 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700170 hall_filter_.Add(hall.get());
171 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700172 }
173
Brian Silverman552350b2015-08-02 18:23:34 -0700174 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
175 encoder_filter_.Add(encoder.get());
176 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 }
178
Austin Schuhc5e36082015-10-31 13:30:46 -0700179 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700180 hall_filter_.Add(hall.get());
181 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700182 }
183
Austin Schuhc5e36082015-10-31 13:30:46 -0700184 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700185 hall_filter_.Add(hall.get());
186 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 }
188
Austin Schuhc5e36082015-10-31 13:30:46 -0700189 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700190 hall_filter_.Add(hall.get());
191 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700192 }
193
Brian Silverman552350b2015-08-02 18:23:34 -0700194 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
195 encoder_filter_.Add(encoder.get());
196 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700197 }
198
Austin Schuhc5e36082015-10-31 13:30:46 -0700199 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700200 hall_filter_.Add(hall.get());
201 shooter_proximal_ = ::std::move(hall);
202 }
203
Austin Schuhc5e36082015-10-31 13:30:46 -0700204 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700205 hall_filter_.Add(hall.get());
206 shooter_distal_ = ::std::move(hall);
207 }
208
Austin Schuhc5e36082015-10-31 13:30:46 -0700209 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700210 hall_filter_.Add(hall.get());
211 shooter_plunger_ = ::std::move(hall);
212 shooter_plunger_reader_ =
213 make_unique<DMADigitalReader>(shooter_plunger_.get());
214 }
215
Austin Schuhc5e36082015-10-31 13:30:46 -0700216 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700217 hall_filter_.Add(hall.get());
218 shooter_latch_ = ::std::move(hall);
219 shooter_latch_reader_ = make_unique<DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700220 }
221
222 // All of the DMA-related set_* calls must be made before this, and it doesn't
223 // hurt to do all of them.
224 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silverman552350b2015-08-02 18:23:34 -0700225 shooter_proximal_counter_ = make_unique<DMAEdgeCounter>(
226 shooter_encoder_.get(), shooter_proximal_.get());
227 shooter_distal_counter_ = make_unique<DMAEdgeCounter>(
228 shooter_encoder_.get(), shooter_distal_.get());
229
Brian Silverman17f503e2015-08-02 18:17:18 -0700230 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700231 dma_synchronizer_->Add(shooter_proximal_counter_.get());
232 dma_synchronizer_->Add(shooter_distal_counter_.get());
233 dma_synchronizer_->Add(shooter_plunger_reader_.get());
234 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700235 }
236
237 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700238 ::aos::SetCurrentThreadName("SensorReader");
Brian Silverman552350b2015-08-02 18:23:34 -0700239 LOG(INFO, "In sensor reader thread\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700240
241 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700242 ds_ =
243#ifdef WPILIB2015
244 DriverStation::GetInstance();
245#else
246 &DriverStation::GetInstance();
247#endif
Brian Silverman17f503e2015-08-02 18:17:18 -0700248
Brian Silverman552350b2015-08-02 18:23:34 -0700249 top_reader_.Start();
250 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700251 dma_synchronizer_->Start();
252 LOG(INFO, "Things are now started\n");
253
254 ::aos::SetCurrentThreadRealtimePriority(kPriority);
255 while (run_) {
Austin Schuhadf2cde2015-11-08 20:35:16 -0800256 ::aos::time::PhasedLoopXMS(5, 4000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700257 RunIteration();
258 }
259
Brian Silverman552350b2015-08-02 18:23:34 -0700260 top_reader_.Quit();
261 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700262 }
263
264 void RunIteration() {
Brian Silverman811f8ec2015-12-06 01:29:42 -0500265 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700266
Brian Silverman552350b2015-08-02 18:23:34 -0700267 const auto &values = constants::GetValues();
268
Brian Silverman17f503e2015-08-02 18:17:18 -0700269 {
270 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
271 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700272 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700273 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700274 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Austin Schuha0c1e152015-11-08 14:10:13 -0800275
276 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
277 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700278 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800279 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
280 drivetrain_message->high_left_hall);
281
282 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
283 drivetrain_message->high_right_hall =
284 high_right_drive_hall_->GetVoltage();
285 drivetrain_message->right_shifter_position =
286 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
287 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700288
289 drivetrain_message.Send();
290 }
291
Brian Silverman552350b2015-08-02 18:23:34 -0700292 ::frc971::sensors::auto_mode.MakeWithBuilder()
293 .voltage(auto_selector_analog_->GetVoltage())
294 .Send();
295
Brian Silverman17f503e2015-08-02 18:17:18 -0700296 dma_synchronizer_->RunIteration();
297
Brian Silverman17f503e2015-08-02 18:17:18 -0700298 {
Brian Silverman552350b2015-08-02 18:23:34 -0700299 auto shooter_message = shooter_queue.position.MakeMessage();
300 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800301 shooter_message->plunger = !shooter_plunger_reader_->value();
302 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700303 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
304 &shooter_message->pusher_proximal);
305 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
306 &shooter_message->pusher_distal);
307
308 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700309 }
310
311 {
312 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700313 top_reader_.RunIteration(&claw_message->top);
314 bottom_reader_.RunIteration(&claw_message->bottom);
315
Brian Silverman17f503e2015-08-02 18:17:18 -0700316 claw_message.Send();
317 }
318 }
319
320 void Quit() { run_ = false; }
321
322 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700323 class HalfClawReader {
324 public:
325 HalfClawReader(bool reversed) : reversed_(reversed) {}
326
327 void set_encoder(::std::unique_ptr<Encoder> encoder) {
328 encoder_ = ::std::move(encoder);
329 }
330
Austin Schuhc5e36082015-10-31 13:30:46 -0700331 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700332 front_hall_ = ::std::move(front_hall);
333 }
334
335 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700336 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700337 calibration_hall_ = ::std::move(calibration_hall);
338 }
339
Austin Schuhc5e36082015-10-31 13:30:46 -0700340 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700341 back_hall_ = ::std::move(back_hall);
342 }
343
344 void Start() {
345 front_counter_ =
346 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
347 synchronizer_.Add(front_counter_.get());
348 calibration_counter_ =
349 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
350 synchronizer_.Add(calibration_counter_.get());
351 back_counter_ =
352 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
353 synchronizer_.Add(back_counter_.get());
354 synchronized_encoder_ =
355 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
356 synchronizer_.Add(synchronized_encoder_.get());
357
358 synchronizer_.Start();
359 }
360
361 void Quit() { synchronizer_.Quit(); }
362
363 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
364 const double multiplier = reversed_ ? -1.0 : 1.0;
365
366 synchronizer_.RunIteration();
367
368 CopyPosition(front_counter_.get(), &half_claw_position->front);
369 CopyPosition(calibration_counter_.get(),
370 &half_claw_position->calibration);
371 CopyPosition(back_counter_.get(), &half_claw_position->back);
372 half_claw_position->position =
373 multiplier * claw_translate(synchronized_encoder_->get());
374 }
375
376 private:
377 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
378 const double multiplier = reversed_ ? -1.0 : 1.0;
379
Austin Schuh5c25ab72015-11-01 13:17:11 -0800380 out->current = !counter->polled_value();
381 out->posedge_count = counter->negative_interrupt_count();
382 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700383 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800384 multiplier * claw_translate(counter->last_positive_encoder_value());
385 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700386 multiplier * claw_translate(counter->last_negative_encoder_value());
387 }
388
389 InterruptSynchronizer synchronizer_{kInterruptPriority};
390
391 ::std::unique_ptr<EdgeCounter> front_counter_;
392 ::std::unique_ptr<EdgeCounter> calibration_counter_;
393 ::std::unique_ptr<EdgeCounter> back_counter_;
394 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
395
396 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700397 ::std::unique_ptr<DigitalInput> front_hall_;
398 ::std::unique_ptr<DigitalInput> calibration_hall_;
399 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700400
401 const bool reversed_;
402 };
403
Brian Silverman17f503e2015-08-02 18:17:18 -0700404 static const int kPriority = 30;
405 static const int kInterruptPriority = 55;
406
Brian Silverman552350b2015-08-02 18:23:34 -0700407 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
408 PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800409 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700410 // These are inverted because the hall effects give logical false when
411 // there's a magnet in front of them.
412 output->posedge_count = counter->negative_count();
413 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700414 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700415 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700416 }
417
Brian Silverman17f503e2015-08-02 18:17:18 -0700418 int32_t my_pid_;
419 DriverStation *ds_;
420
Brian Silverman17f503e2015-08-02 18:17:18 -0700421 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
422
Brian Silverman552350b2015-08-02 18:23:34 -0700423 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700424
Brian Silverman552350b2015-08-02 18:23:34 -0700425 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
426 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
427 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
428 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
429 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
430 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700431
Brian Silverman552350b2015-08-02 18:23:34 -0700432 HalfClawReader top_reader_{false}, bottom_reader_{true};
433
434 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700435 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
436 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silverman552350b2015-08-02 18:23:34 -0700437 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
438 shooter_distal_counter_;
439 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
440 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700441
442 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700443 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700444};
445
446class SolenoidWriter {
447 public:
448 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
449 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700450 shooter_(".frc971.control_loops.shooter_queue.output"),
451 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700452
Austin Schuhc5e36082015-10-31 13:30:46 -0700453 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700454 pressure_switch_ = ::std::move(pressure_switch);
455 }
456
457 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
458 compressor_relay_ = ::std::move(compressor_relay);
459 }
460
Brian Silverman552350b2015-08-02 18:23:34 -0700461 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
462 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700463 }
464
Brian Silverman552350b2015-08-02 18:23:34 -0700465 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
466 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 }
468
Brian Silverman552350b2015-08-02 18:23:34 -0700469 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
470 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700471 }
472
Brian Silverman552350b2015-08-02 18:23:34 -0700473 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
474 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700475 }
476
477 void operator()() {
478 ::aos::SetCurrentThreadName("Solenoids");
479 ::aos::SetCurrentThreadRealtimePriority(30);
480
481 while (run_) {
482 ::aos::time::PhasedLoopXMS(20, 1000);
483
484 {
Brian Silverman552350b2015-08-02 18:23:34 -0700485 shooter_.FetchLatest();
486 if (shooter_.get()) {
487 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
488 shooter_latch_->Set(!shooter_->latch_piston);
489 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700490 }
491 }
492
493 {
Brian Silverman552350b2015-08-02 18:23:34 -0700494 drivetrain_.FetchLatest();
495 if (drivetrain_.get()) {
496 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800497 drivetrain_left_->Set(!drivetrain_->left_high);
498 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700499 }
500 }
501
Brian Silverman17f503e2015-08-02 18:17:18 -0700502 {
503 PneumaticsToLog to_log;
504 {
505 const bool compressor_on = !pressure_switch_->Get();
506 to_log.compressor_on = compressor_on;
507 if (compressor_on) {
508 compressor_relay_->Set(Relay::kForward);
509 } else {
510 compressor_relay_->Set(Relay::kOff);
511 }
512 }
513
514 pcm_->Flush();
515 to_log.read_solenoids = pcm_->GetAll();
516 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
517 }
518 }
519 }
520
521 void Quit() { run_ = false; }
522
523 private:
524 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700525
526 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
527 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
528 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
529 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
530
Austin Schuhc5e36082015-10-31 13:30:46 -0700531 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700532 ::std::unique_ptr<Relay> compressor_relay_;
533
Brian Silverman552350b2015-08-02 18:23:34 -0700534 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
535 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700536
537 ::std::atomic<bool> run_{true};
538};
539
540class DrivetrainWriter : public LoopOutputHandler {
541 public:
542 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
543 left_drivetrain_talon_ = ::std::move(t);
544 }
545
546 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
547 right_drivetrain_talon_ = ::std::move(t);
548 }
549
550 private:
551 virtual void Read() override {
552 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
553 }
554
555 virtual void Write() override {
556 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
557 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700558 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
559 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700560 }
561
562 virtual void Stop() override {
563 LOG(WARNING, "drivetrain output too old\n");
564 left_drivetrain_talon_->Disable();
565 right_drivetrain_talon_->Disable();
566 }
567
568 ::std::unique_ptr<Talon> left_drivetrain_talon_;
569 ::std::unique_ptr<Talon> right_drivetrain_talon_;
570};
571
Brian Silverman552350b2015-08-02 18:23:34 -0700572class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700573 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700574 void set_shooter_talon(::std::unique_ptr<Talon> t) {
575 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700576 }
577
578 private:
579 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400580 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700581 }
582
583 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400584 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700585 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700586 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700587 }
588
589 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700590 LOG(WARNING, "shooter output too old\n");
591 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700592 }
593
Brian Silverman552350b2015-08-02 18:23:34 -0700594 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700595};
596
597class ClawWriter : public LoopOutputHandler {
598 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700599 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
600 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700601 }
602
Brian Silverman552350b2015-08-02 18:23:34 -0700603 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
604 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700605 }
606
Brian Silverman552350b2015-08-02 18:23:34 -0700607 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
608 left_tusk_talon_ = ::std::move(t);
609 }
610
611 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
612 right_tusk_talon_ = ::std::move(t);
613 }
614
615 void set_intake1_talon(::std::unique_ptr<Talon> t) {
616 intake1_talon_ = ::std::move(t);
617 }
618
619 void set_intake2_talon(::std::unique_ptr<Talon> t) {
620 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700621 }
622
623 private:
624 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400625 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700626 }
627
628 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400629 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700630 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700631 intake1_talon_->Set(queue->intake_voltage / 12.0);
632 intake2_talon_->Set(queue->intake_voltage / 12.0);
633 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
634 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
635 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
636 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700637 }
638
639 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700640 LOG(WARNING, "claw output too old\n");
641 intake1_talon_->Disable();
642 intake2_talon_->Disable();
643 bottom_claw_talon_->Disable();
644 top_claw_talon_->Disable();
645 left_tusk_talon_->Disable();
646 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700647 }
648
Brian Silverman552350b2015-08-02 18:23:34 -0700649 ::std::unique_ptr<Talon> top_claw_talon_;
650 ::std::unique_ptr<Talon> bottom_claw_talon_;
651 ::std::unique_ptr<Talon> left_tusk_talon_;
652 ::std::unique_ptr<Talon> right_tusk_talon_;
653 ::std::unique_ptr<Talon> intake1_talon_;
654 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700655};
656
Brian Silverman17f503e2015-08-02 18:17:18 -0700657class WPILibRobot : public RobotBase {
658 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700659 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700660 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
661 Encoder::k4X);
662 }
Brian Silverman552350b2015-08-02 18:23:34 -0700663
Brian Silverman17f503e2015-08-02 18:17:18 -0700664 virtual void StartCompetition() {
665 ::aos::InitNRT();
666 ::aos::SetCurrentThreadName("StartCompetition");
667
668 JoystickSender joystick_sender;
669 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700670
671 SensorReader reader;
672 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700673
Brian Silverman85fbb602015-08-29 19:28:20 -0700674 // Create this first to make sure it ends up in one of the lower-numbered
675 // FPGA slots so we can use it with DMA.
676 auto shooter_encoder_temp = make_encoder(2);
677
Brian Silverman552350b2015-08-02 18:23:34 -0700678 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700679
Brian Silverman552350b2015-08-02 18:23:34 -0700680 reader.set_drivetrain_left_encoder(make_encoder(0));
681 reader.set_drivetrain_right_encoder(make_encoder(1));
682 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
683 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
684 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
685 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700686
Brian Silverman552350b2015-08-02 18:23:34 -0700687 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800688 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
689 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
690 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700691
Brian Silverman85fbb602015-08-29 19:28:20 -0700692 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800693 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
694 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
695 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700696
Brian Silverman85fbb602015-08-29 19:28:20 -0700697 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800698 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
699 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
700 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
701 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700702
Brian Silverman17f503e2015-08-02 18:17:18 -0700703 reader.set_dma(make_unique<DMA>());
704 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700705
Brian Silverman17f503e2015-08-02 18:17:18 -0700706 GyroSender gyro_sender;
707 ::std::thread gyro_thread(::std::ref(gyro_sender));
708
709 DrivetrainWriter drivetrain_writer;
710 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700711 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700712 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700713 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700714 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
715
Brian Silverman552350b2015-08-02 18:23:34 -0700716 ::frc971::wpilib::ClawWriter claw_writer;
717 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
718 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
719 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
720 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
721 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
722 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700723 ::std::thread claw_writer_thread(::std::ref(claw_writer));
724
Brian Silverman552350b2015-08-02 18:23:34 -0700725 ::frc971::wpilib::ShooterWriter shooter_writer;
726 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
727 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
728
Brian Silverman17f503e2015-08-02 18:17:18 -0700729 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
730 new ::frc971::wpilib::BufferedPcm());
731 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700732 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
733 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
734 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
735 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700736
Austin Schuh016a6b02015-10-08 06:41:14 +0000737 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700738 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
739 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
740
741 // Wait forever. Not much else to do...
742 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
743
744 LOG(ERROR, "Exiting WPILibRobot\n");
745
746 joystick_sender.Quit();
747 joystick_thread.join();
748 reader.Quit();
749 reader_thread.join();
750 gyro_sender.Quit();
751 gyro_thread.join();
752
753 drivetrain_writer.Quit();
754 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700755 shooter_writer.Quit();
756 shooter_writer_thread.join();
757 claw_writer.Quit();
758 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700759 solenoid_writer.Quit();
760 solenoid_thread.join();
761
762 ::aos::Cleanup();
763 }
764};
765
766} // namespace wpilib
767} // namespace frc971
768
769
770START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);