blob: 1e70fb258a4f34327fd6dccbd8a373ba67455ad6 [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
10#include "aos/common/logging/logging.h"
11#include "aos/common/logging/queue_logging.h"
12#include "aos/common/time.h"
13#include "aos/common/util/log_interval.h"
14#include "aos/common/util/phased_loop.h"
15#include "aos/common/util/wrapping_counter.h"
16#include "aos/common/stl_mutex.h"
17#include "aos/linux_code/init.h"
18#include "aos/common/messages/robot_state.q.h"
19
Brian Silverman552350b2015-08-02 18:23:34 -070020#include "frc971/shifter_hall_effect.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070021#include "y2014/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070022#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070023#include "y2014/control_loops/shooter/shooter.q.h"
24#include "y2014/constants.h"
25#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070026
27#include "frc971/wpilib/hall_effect.h"
28#include "frc971/wpilib/joystick_sender.h"
29#include "frc971/wpilib/loop_output_handler.h"
30#include "frc971/wpilib/buffered_solenoid.h"
31#include "frc971/wpilib/buffered_pcm.h"
32#include "frc971/wpilib/gyro_sender.h"
33#include "frc971/wpilib/dma_edge_counting.h"
34#include "frc971/wpilib/interrupt_edge_counting.h"
35#include "frc971/wpilib/encoder_and_potentiometer.h"
36#include "frc971/wpilib/logging.q.h"
37
38#include "Encoder.h"
39#include "Talon.h"
40#include "DriverStation.h"
41#include "AnalogInput.h"
42#include "Compressor.h"
43#include "Relay.h"
44#include "RobotBase.h"
45#include "dma.h"
46#include "ControllerPower.h"
47
48#ifndef M_PI
49#define M_PI 3.14159265358979323846
50#endif
51
Brian Silverman17f503e2015-08-02 18:17:18 -070052using ::frc971::control_loops::drivetrain_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070053using ::frc971::control_loops::claw_queue;
Brian Silverman552350b2015-08-02 18:23:34 -070054using ::frc971::control_loops::shooter_queue;
Brian Silverman17f503e2015-08-02 18:17:18 -070055
56namespace frc971 {
57namespace wpilib {
58
Brian Silverman85fbb602015-08-29 19:28:20 -070059// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
60// DMA stuff and then removing the * 2.0 in *_translate.
61// The low bit is direction.
62
Brian Silverman552350b2015-08-02 18:23:34 -070063// TODO(brian): Replace this with ::std::make_unique once all our toolchains
64// have support.
65template <class T, class... U>
66std::unique_ptr<T> make_unique(U &&... u) {
67 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
68}
69
Brian Silverman17f503e2015-08-02 18:17:18 -070070double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070071 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070072 (256.0 /*cpr*/ * 4.0 /*4x*/) *
73 constants::GetValues().drivetrain_encoder_ratio *
Brian Silverman85fbb602015-08-29 19:28:20 -070074 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070075}
76
Brian Silverman552350b2015-08-02 18:23:34 -070077float hall_translate(const constants::ShifterHallEffect &k, float in_low,
78 float in_high) {
79 const float low_ratio =
80 0.5 * (in_low - static_cast<float>(k.low_gear_low)) /
81 static_cast<float>(k.low_gear_middle - k.low_gear_low);
82 const float high_ratio =
83 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
84 static_cast<float>(k.high_gear_high - k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070085
Brian Silverman552350b2015-08-02 18:23:34 -070086 // Return low when we are below 1/2, and high when we are above 1/2.
87 if (low_ratio + high_ratio < 1.0) {
88 return low_ratio;
89 } else {
90 return high_ratio;
91 }
Brian Silverman17f503e2015-08-02 18:17:18 -070092}
93
94double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070095 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -070096 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -070097 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070098}
99
Brian Silverman552350b2015-08-02 18:23:34 -0700100double shooter_translate(int32_t in) {
101 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
102 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
103 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700104}
105
106static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700107 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
108 18.0 / 32.0 /* big belt reduction */ *
109 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
110 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700111
112class SensorReader {
113 public:
114 SensorReader() {
115 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
116 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700117 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700118 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700119 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700120 }
121
Brian Silverman552350b2015-08-02 18:23:34 -0700122 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
123 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 }
125
Brian Silverman552350b2015-08-02 18:23:34 -0700126 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
127 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
129
Brian Silverman552350b2015-08-02 18:23:34 -0700130 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
131 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
135 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 }
137
Brian Silverman552350b2015-08-02 18:23:34 -0700138 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
139 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 }
141
Brian Silverman552350b2015-08-02 18:23:34 -0700142 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
143 high_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_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
147 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Brian Silverman552350b2015-08-02 18:23:34 -0700150 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
151 encoder_filter_.Add(encoder.get());
152 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700153 }
154
Austin Schuhc5e36082015-10-31 13:30:46 -0700155 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700156 hall_filter_.Add(hall.get());
157 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700158 }
159
Austin Schuhc5e36082015-10-31 13:30:46 -0700160 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700161 hall_filter_.Add(hall.get());
162 top_reader_.set_calibration_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_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700166 hall_filter_.Add(hall.get());
167 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700168 }
169
Brian Silverman552350b2015-08-02 18:23:34 -0700170 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
171 encoder_filter_.Add(encoder.get());
172 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700173 }
174
Austin Schuhc5e36082015-10-31 13:30:46 -0700175 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700176 hall_filter_.Add(hall.get());
177 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700178 }
179
Austin Schuhc5e36082015-10-31 13:30:46 -0700180 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700181 hall_filter_.Add(hall.get());
182 bottom_reader_.set_calibration_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_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700186 hall_filter_.Add(hall.get());
187 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 }
189
Brian Silverman552350b2015-08-02 18:23:34 -0700190 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
191 encoder_filter_.Add(encoder.get());
192 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700193 }
194
Austin Schuhc5e36082015-10-31 13:30:46 -0700195 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700196 hall_filter_.Add(hall.get());
197 shooter_proximal_ = ::std::move(hall);
198 }
199
Austin Schuhc5e36082015-10-31 13:30:46 -0700200 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700201 hall_filter_.Add(hall.get());
202 shooter_distal_ = ::std::move(hall);
203 }
204
Austin Schuhc5e36082015-10-31 13:30:46 -0700205 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700206 hall_filter_.Add(hall.get());
207 shooter_plunger_ = ::std::move(hall);
208 shooter_plunger_reader_ =
209 make_unique<DMADigitalReader>(shooter_plunger_.get());
210 }
211
Austin Schuhc5e36082015-10-31 13:30:46 -0700212 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700213 hall_filter_.Add(hall.get());
214 shooter_latch_ = ::std::move(hall);
215 shooter_latch_reader_ = make_unique<DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700216 }
217
218 // All of the DMA-related set_* calls must be made before this, and it doesn't
219 // hurt to do all of them.
220 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silverman552350b2015-08-02 18:23:34 -0700221 shooter_proximal_counter_ = make_unique<DMAEdgeCounter>(
222 shooter_encoder_.get(), shooter_proximal_.get());
223 shooter_distal_counter_ = make_unique<DMAEdgeCounter>(
224 shooter_encoder_.get(), shooter_distal_.get());
225
Brian Silverman17f503e2015-08-02 18:17:18 -0700226 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700227 dma_synchronizer_->Add(shooter_proximal_counter_.get());
228 dma_synchronizer_->Add(shooter_distal_counter_.get());
229 dma_synchronizer_->Add(shooter_plunger_reader_.get());
230 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700231 }
232
233 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700234 ::aos::SetCurrentThreadName("SensorReader");
Brian Silverman552350b2015-08-02 18:23:34 -0700235 LOG(INFO, "In sensor reader thread\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700236
237 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700238 ds_ =
239#ifdef WPILIB2015
240 DriverStation::GetInstance();
241#else
242 &DriverStation::GetInstance();
243#endif
Brian Silverman17f503e2015-08-02 18:17:18 -0700244
Brian Silverman552350b2015-08-02 18:23:34 -0700245 top_reader_.Start();
246 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700247 dma_synchronizer_->Start();
248 LOG(INFO, "Things are now started\n");
249
250 ::aos::SetCurrentThreadRealtimePriority(kPriority);
251 while (run_) {
Brian Silverman552350b2015-08-02 18:23:34 -0700252 ::aos::time::PhasedLoopXMS(10, 9000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700253 RunIteration();
254 }
255
Brian Silverman552350b2015-08-02 18:23:34 -0700256 top_reader_.Quit();
257 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700258 }
259
260 void RunIteration() {
261 {
262 auto new_state = ::aos::robot_state.MakeMessage();
263
264 new_state->reader_pid = my_pid_;
265 new_state->outputs_enabled = ds_->IsSysActive();
266 new_state->browned_out = ds_->IsSysBrownedOut();
267
268 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
269 new_state->is_5v_active = ControllerPower::GetEnabled5V();
270 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
271 new_state->voltage_5v = ControllerPower::GetVoltage5V();
272
273 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
274 new_state->voltage_battery = ds_->GetBatteryVoltage();
275
276 LOG_STRUCT(DEBUG, "robot_state", *new_state);
277
278 new_state.Send();
279 }
280
Brian Silverman552350b2015-08-02 18:23:34 -0700281 const auto &values = constants::GetValues();
282
Brian Silverman17f503e2015-08-02 18:17:18 -0700283 {
284 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
285 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700286 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700287 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700288 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
289 drivetrain_message->left_shifter_position =
290 hall_translate(values.left_drive, low_left_drive_hall_->GetVoltage(),
291 high_left_drive_hall_->GetVoltage());
292 drivetrain_message->right_shifter_position = hall_translate(
293 values.right_drive, low_right_drive_hall_->GetVoltage(),
294 high_right_drive_hall_->GetVoltage());
Brian Silverman17f503e2015-08-02 18:17:18 -0700295
296 drivetrain_message.Send();
297 }
298
Brian Silverman552350b2015-08-02 18:23:34 -0700299 ::frc971::sensors::auto_mode.MakeWithBuilder()
300 .voltage(auto_selector_analog_->GetVoltage())
301 .Send();
302
Brian Silverman17f503e2015-08-02 18:17:18 -0700303 dma_synchronizer_->RunIteration();
304
Brian Silverman17f503e2015-08-02 18:17:18 -0700305 {
Brian Silverman552350b2015-08-02 18:23:34 -0700306 auto shooter_message = shooter_queue.position.MakeMessage();
307 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
308 shooter_message->plunger = shooter_plunger_reader_->value();
309 shooter_message->latch = shooter_latch_reader_->value();
310 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
311 &shooter_message->pusher_proximal);
312 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
313 &shooter_message->pusher_distal);
314
315 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700316 }
317
318 {
319 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700320 top_reader_.RunIteration(&claw_message->top);
321 bottom_reader_.RunIteration(&claw_message->bottom);
322
Brian Silverman17f503e2015-08-02 18:17:18 -0700323 claw_message.Send();
324 }
325 }
326
327 void Quit() { run_ = false; }
328
329 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700330 class HalfClawReader {
331 public:
332 HalfClawReader(bool reversed) : reversed_(reversed) {}
333
334 void set_encoder(::std::unique_ptr<Encoder> encoder) {
335 encoder_ = ::std::move(encoder);
336 }
337
Austin Schuhc5e36082015-10-31 13:30:46 -0700338 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700339 front_hall_ = ::std::move(front_hall);
340 }
341
342 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700343 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700344 calibration_hall_ = ::std::move(calibration_hall);
345 }
346
Austin Schuhc5e36082015-10-31 13:30:46 -0700347 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700348 back_hall_ = ::std::move(back_hall);
349 }
350
351 void Start() {
352 front_counter_ =
353 make_unique<EdgeCounter>(encoder_.get(), front_hall_.get());
354 synchronizer_.Add(front_counter_.get());
355 calibration_counter_ =
356 make_unique<EdgeCounter>(encoder_.get(), calibration_hall_.get());
357 synchronizer_.Add(calibration_counter_.get());
358 back_counter_ =
359 make_unique<EdgeCounter>(encoder_.get(), back_hall_.get());
360 synchronizer_.Add(back_counter_.get());
361 synchronized_encoder_ =
362 make_unique<InterruptSynchronizedEncoder>(encoder_.get());
363 synchronizer_.Add(synchronized_encoder_.get());
364
365 synchronizer_.Start();
366 }
367
368 void Quit() { synchronizer_.Quit(); }
369
370 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
371 const double multiplier = reversed_ ? -1.0 : 1.0;
372
373 synchronizer_.RunIteration();
374
375 CopyPosition(front_counter_.get(), &half_claw_position->front);
376 CopyPosition(calibration_counter_.get(),
377 &half_claw_position->calibration);
378 CopyPosition(back_counter_.get(), &half_claw_position->back);
379 half_claw_position->position =
380 multiplier * claw_translate(synchronized_encoder_->get());
381 }
382
383 private:
384 void CopyPosition(const EdgeCounter *counter, HallEffectStruct *out) {
385 const double multiplier = reversed_ ? -1.0 : 1.0;
386
387 out->current = counter->polled_value();
388 out->posedge_count = counter->positive_interrupt_count();
389 out->negedge_count = counter->negative_interrupt_count();
390 out->posedge_value =
391 multiplier * claw_translate(counter->last_positive_encoder_value());
392 out->negedge_value =
393 multiplier * claw_translate(counter->last_negative_encoder_value());
394 }
395
396 InterruptSynchronizer synchronizer_{kInterruptPriority};
397
398 ::std::unique_ptr<EdgeCounter> front_counter_;
399 ::std::unique_ptr<EdgeCounter> calibration_counter_;
400 ::std::unique_ptr<EdgeCounter> back_counter_;
401 ::std::unique_ptr<InterruptSynchronizedEncoder> synchronized_encoder_;
402
403 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700404 ::std::unique_ptr<DigitalInput> front_hall_;
405 ::std::unique_ptr<DigitalInput> calibration_hall_;
406 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700407
408 const bool reversed_;
409 };
410
Brian Silverman17f503e2015-08-02 18:17:18 -0700411 static const int kPriority = 30;
412 static const int kInterruptPriority = 55;
413
Brian Silverman552350b2015-08-02 18:23:34 -0700414 void CopyShooterPosedgeCounts(const DMAEdgeCounter *counter,
415 PosedgeOnlyCountedHallEffectStruct *output) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700416 // TODO(Brian): Remove HallEffect so current will get inverted too like
417 // everything else.
Brian Silverman552350b2015-08-02 18:23:34 -0700418 output->current = counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700419 // These are inverted because the hall effects give logical false when
420 // there's a magnet in front of them.
421 output->posedge_count = counter->negative_count();
422 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700423 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700424 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700425 }
426
Brian Silverman17f503e2015-08-02 18:17:18 -0700427 int32_t my_pid_;
428 DriverStation *ds_;
429
Brian Silverman17f503e2015-08-02 18:17:18 -0700430 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
431
Brian Silverman552350b2015-08-02 18:23:34 -0700432 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700433
Brian Silverman552350b2015-08-02 18:23:34 -0700434 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
435 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
436 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
437 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
438 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
439 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700440
Brian Silverman552350b2015-08-02 18:23:34 -0700441 HalfClawReader top_reader_{false}, bottom_reader_{true};
442
443 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700444 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
445 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silverman552350b2015-08-02 18:23:34 -0700446 ::std::unique_ptr<DMAEdgeCounter> shooter_proximal_counter_,
447 shooter_distal_counter_;
448 ::std::unique_ptr<DMADigitalReader> shooter_plunger_reader_,
449 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700450
451 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700452 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700453};
454
455class SolenoidWriter {
456 public:
457 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
458 : pcm_(pcm),
Brian Silverman552350b2015-08-02 18:23:34 -0700459 shooter_(".frc971.control_loops.shooter_queue.output"),
460 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700461
Austin Schuhc5e36082015-10-31 13:30:46 -0700462 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700463 pressure_switch_ = ::std::move(pressure_switch);
464 }
465
466 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
467 compressor_relay_ = ::std::move(compressor_relay);
468 }
469
Brian Silverman552350b2015-08-02 18:23:34 -0700470 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
471 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700472 }
473
Brian Silverman552350b2015-08-02 18:23:34 -0700474 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
475 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700476 }
477
Brian Silverman552350b2015-08-02 18:23:34 -0700478 void set_shooter_latch(::std::unique_ptr<BufferedSolenoid> s) {
479 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700480 }
481
Brian Silverman552350b2015-08-02 18:23:34 -0700482 void set_shooter_brake(::std::unique_ptr<BufferedSolenoid> s) {
483 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700484 }
485
486 void operator()() {
487 ::aos::SetCurrentThreadName("Solenoids");
488 ::aos::SetCurrentThreadRealtimePriority(30);
489
490 while (run_) {
491 ::aos::time::PhasedLoopXMS(20, 1000);
492
493 {
Brian Silverman552350b2015-08-02 18:23:34 -0700494 shooter_.FetchLatest();
495 if (shooter_.get()) {
496 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
497 shooter_latch_->Set(!shooter_->latch_piston);
498 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700499 }
500 }
501
502 {
Brian Silverman552350b2015-08-02 18:23:34 -0700503 drivetrain_.FetchLatest();
504 if (drivetrain_.get()) {
505 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
506 drivetrain_left_->Set(drivetrain_->left_high);
507 drivetrain_right_->Set(drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700508 }
509 }
510
Brian Silverman17f503e2015-08-02 18:17:18 -0700511 {
512 PneumaticsToLog to_log;
513 {
514 const bool compressor_on = !pressure_switch_->Get();
515 to_log.compressor_on = compressor_on;
516 if (compressor_on) {
517 compressor_relay_->Set(Relay::kForward);
518 } else {
519 compressor_relay_->Set(Relay::kOff);
520 }
521 }
522
523 pcm_->Flush();
524 to_log.read_solenoids = pcm_->GetAll();
525 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
526 }
527 }
528 }
529
530 void Quit() { run_ = false; }
531
532 private:
533 const ::std::unique_ptr<BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700534
535 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_;
536 ::std::unique_ptr<BufferedSolenoid> drivetrain_right_;
537 ::std::unique_ptr<BufferedSolenoid> shooter_latch_;
538 ::std::unique_ptr<BufferedSolenoid> shooter_brake_;
539
Austin Schuhc5e36082015-10-31 13:30:46 -0700540 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700541 ::std::unique_ptr<Relay> compressor_relay_;
542
Brian Silverman552350b2015-08-02 18:23:34 -0700543 ::aos::Queue<::frc971::control_loops::ShooterQueue::Output> shooter_;
544 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700545
546 ::std::atomic<bool> run_{true};
547};
548
549class DrivetrainWriter : public LoopOutputHandler {
550 public:
551 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
552 left_drivetrain_talon_ = ::std::move(t);
553 }
554
555 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
556 right_drivetrain_talon_ = ::std::move(t);
557 }
558
559 private:
560 virtual void Read() override {
561 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
562 }
563
564 virtual void Write() override {
565 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
566 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman85fbb602015-08-29 19:28:20 -0700567 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
568 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700569 }
570
571 virtual void Stop() override {
572 LOG(WARNING, "drivetrain output too old\n");
573 left_drivetrain_talon_->Disable();
574 right_drivetrain_talon_->Disable();
575 }
576
577 ::std::unique_ptr<Talon> left_drivetrain_talon_;
578 ::std::unique_ptr<Talon> right_drivetrain_talon_;
579};
580
Brian Silverman552350b2015-08-02 18:23:34 -0700581class ShooterWriter : public LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700582 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700583 void set_shooter_talon(::std::unique_ptr<Talon> t) {
584 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700585 }
586
587 private:
588 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400589 ::frc971::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 }
591
592 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400593 auto &queue = ::frc971::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700594 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700595 shooter_talon_->Set(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700596 }
597
598 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700599 LOG(WARNING, "shooter output too old\n");
600 shooter_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700601 }
602
Brian Silverman552350b2015-08-02 18:23:34 -0700603 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700604};
605
606class ClawWriter : public LoopOutputHandler {
607 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700608 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
609 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700610 }
611
Brian Silverman552350b2015-08-02 18:23:34 -0700612 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
613 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700614 }
615
Brian Silverman552350b2015-08-02 18:23:34 -0700616 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
617 left_tusk_talon_ = ::std::move(t);
618 }
619
620 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
621 right_tusk_talon_ = ::std::move(t);
622 }
623
624 void set_intake1_talon(::std::unique_ptr<Talon> t) {
625 intake1_talon_ = ::std::move(t);
626 }
627
628 void set_intake2_talon(::std::unique_ptr<Talon> t) {
629 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700630 }
631
632 private:
633 virtual void Read() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400634 ::frc971::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700635 }
636
637 virtual void Write() override {
Brian Silvermanbe5ded62015-05-14 00:23:49 -0400638 auto &queue = ::frc971::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700639 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman552350b2015-08-02 18:23:34 -0700640 intake1_talon_->Set(queue->intake_voltage / 12.0);
641 intake2_talon_->Set(queue->intake_voltage / 12.0);
642 bottom_claw_talon_->Set(-queue->bottom_claw_voltage / 12.0);
643 top_claw_talon_->Set(queue->top_claw_voltage / 12.0);
644 left_tusk_talon_->Set(queue->tusk_voltage / 12.0);
645 right_tusk_talon_->Set(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700646 }
647
648 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700649 LOG(WARNING, "claw output too old\n");
650 intake1_talon_->Disable();
651 intake2_talon_->Disable();
652 bottom_claw_talon_->Disable();
653 top_claw_talon_->Disable();
654 left_tusk_talon_->Disable();
655 right_tusk_talon_->Disable();
Brian Silverman17f503e2015-08-02 18:17:18 -0700656 }
657
Brian Silverman552350b2015-08-02 18:23:34 -0700658 ::std::unique_ptr<Talon> top_claw_talon_;
659 ::std::unique_ptr<Talon> bottom_claw_talon_;
660 ::std::unique_ptr<Talon> left_tusk_talon_;
661 ::std::unique_ptr<Talon> right_tusk_talon_;
662 ::std::unique_ptr<Talon> intake1_talon_;
663 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700664};
665
Brian Silverman17f503e2015-08-02 18:17:18 -0700666class WPILibRobot : public RobotBase {
667 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700668 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700669 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
670 Encoder::k4X);
671 }
Brian Silverman552350b2015-08-02 18:23:34 -0700672
Brian Silverman17f503e2015-08-02 18:17:18 -0700673 virtual void StartCompetition() {
674 ::aos::InitNRT();
675 ::aos::SetCurrentThreadName("StartCompetition");
676
677 JoystickSender joystick_sender;
678 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700679
680 SensorReader reader;
681 LOG(INFO, "Creating the reader\n");
Brian Silverman17f503e2015-08-02 18:17:18 -0700682
Brian Silverman85fbb602015-08-29 19:28:20 -0700683 // Create this first to make sure it ends up in one of the lower-numbered
684 // FPGA slots so we can use it with DMA.
685 auto shooter_encoder_temp = make_encoder(2);
686
Brian Silverman552350b2015-08-02 18:23:34 -0700687 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700688
Brian Silverman552350b2015-08-02 18:23:34 -0700689 reader.set_drivetrain_left_encoder(make_encoder(0));
690 reader.set_drivetrain_right_encoder(make_encoder(1));
691 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
692 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
693 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
694 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700695
Brian Silverman552350b2015-08-02 18:23:34 -0700696 reader.set_top_claw_encoder(make_encoder(3));
697 reader.set_top_claw_front_hall(make_unique<HallEffect>(4)); // R2
698 reader.set_top_claw_calibration_hall(make_unique<HallEffect>(3)); // R3
699 reader.set_top_claw_back_hall(make_unique<HallEffect>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700700
Brian Silverman85fbb602015-08-29 19:28:20 -0700701 reader.set_bottom_claw_encoder(make_encoder(4));
Brian Silverman552350b2015-08-02 18:23:34 -0700702 reader.set_bottom_claw_front_hall(make_unique<HallEffect>(1)); // L2
703 reader.set_bottom_claw_calibration_hall(make_unique<HallEffect>(0)); // L3
704 reader.set_bottom_claw_back_hall(make_unique<HallEffect>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700705
Brian Silverman85fbb602015-08-29 19:28:20 -0700706 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Brian Silverman552350b2015-08-02 18:23:34 -0700707 reader.set_shooter_proximal(make_unique<HallEffect>(6)); // S1
708 reader.set_shooter_distal(make_unique<HallEffect>(7)); // S2
709 reader.set_shooter_plunger(make_unique<HallEffect>(8)); // S3
710 reader.set_shooter_latch(make_unique<HallEffect>(9)); // S4
711
Brian Silverman17f503e2015-08-02 18:17:18 -0700712 reader.set_dma(make_unique<DMA>());
713 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700714
Brian Silverman17f503e2015-08-02 18:17:18 -0700715 GyroSender gyro_sender;
716 ::std::thread gyro_thread(::std::ref(gyro_sender));
717
718 DrivetrainWriter drivetrain_writer;
719 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700720 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700721 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700722 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700723 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
724
Brian Silverman552350b2015-08-02 18:23:34 -0700725 ::frc971::wpilib::ClawWriter claw_writer;
726 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
727 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
728 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
729 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
730 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
731 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700732 ::std::thread claw_writer_thread(::std::ref(claw_writer));
733
Brian Silverman552350b2015-08-02 18:23:34 -0700734 ::frc971::wpilib::ShooterWriter shooter_writer;
735 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
736 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
737
Brian Silverman17f503e2015-08-02 18:17:18 -0700738 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
739 new ::frc971::wpilib::BufferedPcm());
740 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700741 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
742 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
743 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
744 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700745
Austin Schuh016a6b02015-10-08 06:41:14 +0000746 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700747 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
748 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
749
750 // Wait forever. Not much else to do...
751 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
752
753 LOG(ERROR, "Exiting WPILibRobot\n");
754
755 joystick_sender.Quit();
756 joystick_thread.join();
757 reader.Quit();
758 reader_thread.join();
759 gyro_sender.Quit();
760 gyro_thread.join();
761
762 drivetrain_writer.Quit();
763 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700764 shooter_writer.Quit();
765 shooter_writer_thread.join();
766 claw_writer.Quit();
767 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700768 solenoid_writer.Quit();
769 solenoid_thread.join();
770
771 ::aos::Cleanup();
772 }
773};
774
775} // namespace wpilib
776} // namespace frc971
777
778
779START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);