blob: 2cd9b50752c844eac7505133a7ec16146eee9fd7 [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>
Austin Schuh8aec1ed2016-05-01 13:29:20 -07007#include <chrono>
Brian Silverman17f503e2015-08-02 18:17:18 -07008#include <mutex>
9#include <functional>
10
Parker Schuhd3b7a8872018-02-19 16:42:27 -080011#include "frc971/wpilib/ahal/AnalogInput.h"
12#include "frc971/wpilib/ahal/Compressor.h"
13#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
14#include "frc971/wpilib/ahal/DriverStation.h"
15#include "frc971/wpilib/ahal/Encoder.h"
16#include "frc971/wpilib/ahal/Relay.h"
17#include "frc971/wpilib/ahal/Talon.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080018#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070019#undef ERROR
20
John Park33858a32018-09-28 23:05:48 -070021#include "aos/logging/logging.h"
22#include "aos/logging/queue_logging.h"
23#include "aos/time/time.h"
24#include "aos/util/log_interval.h"
25#include "aos/util/phased_loop.h"
26#include "aos/util/wrapping_counter.h"
27#include "aos/stl_mutex/stl_mutex.h"
John Park398c74a2018-10-20 21:17:39 -070028#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/robot_state/robot_state.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070030
Brian Silverman552350b2015-08-02 18:23:34 -070031#include "frc971/shifter_hall_effect.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050032
Comran Morshed5323ecb2015-12-26 20:50:55 +000033#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070034#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070035#include "y2014/control_loops/shooter/shooter.q.h"
36#include "y2014/constants.h"
37#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070038
Brian Silverman17f503e2015-08-02 18:17:18 -070039#include "frc971/wpilib/joystick_sender.h"
40#include "frc971/wpilib/loop_output_handler.h"
41#include "frc971/wpilib/buffered_solenoid.h"
42#include "frc971/wpilib/buffered_pcm.h"
43#include "frc971/wpilib/gyro_sender.h"
44#include "frc971/wpilib/dma_edge_counting.h"
45#include "frc971/wpilib/interrupt_edge_counting.h"
46#include "frc971/wpilib/encoder_and_potentiometer.h"
47#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050048#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman425492b2015-12-30 15:23:55 -080049#include "frc971/wpilib/pdp_fetcher.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050050#include "frc971/wpilib/dma.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
Comran Morshed5323ecb2015-12-26 20:50:55 +000056using ::frc971::control_loops::drivetrain_queue;
Brian Silvermanb601d892015-12-20 18:24:38 -050057using ::y2014::control_loops::claw_queue;
58using ::y2014::control_loops::shooter_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080059using namespace frc;
Brian Silverman17f503e2015-08-02 18:17:18 -070060
Brian Silvermanb601d892015-12-20 18:24:38 -050061namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070062namespace 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 Silverman51091a02015-12-26 15:56:58 -080082double drivetrain_velocity_translate(double in) {
83 return (1.0 / in) / 256.0 /*cpr*/ *
84 constants::GetValues().drivetrain_encoder_ratio *
85 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
86}
87
Sabina Davis415bb6c2017-10-16 23:30:52 -070088float hall_translate(const constants::DualHallShifterHallEffect &k, float in_low,
Brian Silverman552350b2015-08-02 18:23:34 -070089 float in_high) {
90 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070091 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
92 static_cast<float>(k.low_gear_middle - k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070093 const float high_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070094 0.5 +
95 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
96 static_cast<float>(k.shifter_hall_effect.high_gear_high -
97 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070098
Brian Silverman552350b2015-08-02 18:23:34 -070099 // Return low when we are below 1/2, and high when we are above 1/2.
100 if (low_ratio + high_ratio < 1.0) {
101 return low_ratio;
102 } else {
103 return high_ratio;
104 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700105}
106
107double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700108 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700109 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700110 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700111}
112
Brian Silverman552350b2015-08-02 18:23:34 -0700113double shooter_translate(int32_t in) {
114 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
115 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
116 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700117}
118
119static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700120 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
121 18.0 / 32.0 /* big belt reduction */ *
122 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
123 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700124
125class SensorReader {
126 public:
Brian Silverman39b339e2016-01-03 13:24:22 -0800127 SensorReader() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
129 // we should ever see.
Brian Silverman552350b2015-08-02 18:23:34 -0700130 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700131 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700132 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700133 }
134
Brian Silverman552350b2015-08-02 18:23:34 -0700135 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
136 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 }
138
Brian Silverman552350b2015-08-02 18:23:34 -0700139 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
140 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800141 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700142 }
143
Brian Silverman552350b2015-08-02 18:23:34 -0700144 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
145 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800146 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700147 }
148
Brian Silverman552350b2015-08-02 18:23:34 -0700149 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
150 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700151 }
152
Brian Silverman552350b2015-08-02 18:23:34 -0700153 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
154 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700155 }
156
Brian Silverman552350b2015-08-02 18:23:34 -0700157 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
158 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700159 }
160
Brian Silverman552350b2015-08-02 18:23:34 -0700161 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
162 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700163 }
164
Brian Silverman552350b2015-08-02 18:23:34 -0700165 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
166 encoder_filter_.Add(encoder.get());
167 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700168 }
169
Austin Schuhc5e36082015-10-31 13:30:46 -0700170 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700171 hall_filter_.Add(hall.get());
172 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700173 }
174
Austin Schuhc5e36082015-10-31 13:30:46 -0700175 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700176 hall_filter_.Add(hall.get());
177 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700178 }
179
Austin Schuhc5e36082015-10-31 13:30:46 -0700180 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700181 hall_filter_.Add(hall.get());
182 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700183 }
184
Brian Silverman552350b2015-08-02 18:23:34 -0700185 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
186 encoder_filter_.Add(encoder.get());
187 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 }
189
Austin Schuhc5e36082015-10-31 13:30:46 -0700190 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700191 hall_filter_.Add(hall.get());
192 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700193 }
194
Austin Schuhc5e36082015-10-31 13:30:46 -0700195 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700196 hall_filter_.Add(hall.get());
197 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700198 }
199
Austin Schuhc5e36082015-10-31 13:30:46 -0700200 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700201 hall_filter_.Add(hall.get());
202 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700203 }
204
Brian Silverman552350b2015-08-02 18:23:34 -0700205 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
206 encoder_filter_.Add(encoder.get());
207 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700208 }
209
Austin Schuhc5e36082015-10-31 13:30:46 -0700210 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700211 hall_filter_.Add(hall.get());
212 shooter_proximal_ = ::std::move(hall);
213 }
214
Austin Schuhc5e36082015-10-31 13:30:46 -0700215 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700216 hall_filter_.Add(hall.get());
217 shooter_distal_ = ::std::move(hall);
218 }
219
Austin Schuhc5e36082015-10-31 13:30:46 -0700220 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700221 hall_filter_.Add(hall.get());
222 shooter_plunger_ = ::std::move(hall);
223 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500224 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700225 }
226
Austin Schuhc5e36082015-10-31 13:30:46 -0700227 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700228 hall_filter_.Add(hall.get());
229 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500230 shooter_latch_reader_ =
231 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700232 }
233
234 // All of the DMA-related set_* calls must be made before this, and it doesn't
235 // hurt to do all of them.
236 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500237 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700238 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500239 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700240 shooter_encoder_.get(), shooter_distal_.get());
241
Brian Silvermanb601d892015-12-20 18:24:38 -0500242 dma_synchronizer_.reset(
243 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700244 dma_synchronizer_->Add(shooter_proximal_counter_.get());
245 dma_synchronizer_->Add(shooter_distal_counter_.get());
246 dma_synchronizer_->Add(shooter_plunger_reader_.get());
247 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700248 }
249
250 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700251 ::aos::SetCurrentThreadName("SensorReader");
252
253 my_pid_ = getpid();
Brian Silverman17f503e2015-08-02 18:17:18 -0700254
Brian Silverman552350b2015-08-02 18:23:34 -0700255 top_reader_.Start();
256 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700257 dma_synchronizer_->Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700258
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700259 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
260 ::std::chrono::milliseconds(4));
Brian Silvermane7611a02015-12-30 16:20:33 -0800261
Brian Silverman5090c432016-01-02 14:44:26 -0800262 ::aos::SetCurrentThreadRealtimePriority(40);
Brian Silverman17f503e2015-08-02 18:17:18 -0700263 while (run_) {
Brian Silvermane7611a02015-12-30 16:20:33 -0800264 {
265 const int iterations = phased_loop.SleepUntilNext();
266 if (iterations != 1) {
267 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
268 }
269 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700270 RunIteration();
271 }
272
Brian Silverman552350b2015-08-02 18:23:34 -0700273 top_reader_.Quit();
274 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700275 }
276
277 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700278 ::frc971::wpilib::SendRobotState(my_pid_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700279
Brian Silverman552350b2015-08-02 18:23:34 -0700280 const auto &values = constants::GetValues();
281
Brian Silverman17f503e2015-08-02 18:17:18 -0700282 {
283 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
284 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700285 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700286 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700287 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800288 drivetrain_message->left_speed =
289 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
290 drivetrain_message->right_speed =
291 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800292
293 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
294 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700295 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800296 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
297 drivetrain_message->high_left_hall);
298
299 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
300 drivetrain_message->high_right_hall =
301 high_right_drive_hall_->GetVoltage();
302 drivetrain_message->right_shifter_position =
303 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
304 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700305
306 drivetrain_message.Send();
307 }
308
Brian Silvermanb601d892015-12-20 18:24:38 -0500309 ::y2014::sensors::auto_mode.MakeWithBuilder()
Brian Silverman552350b2015-08-02 18:23:34 -0700310 .voltage(auto_selector_analog_->GetVoltage())
311 .Send();
312
Brian Silverman17f503e2015-08-02 18:17:18 -0700313 dma_synchronizer_->RunIteration();
314
Brian Silverman17f503e2015-08-02 18:17:18 -0700315 {
Brian Silverman552350b2015-08-02 18:23:34 -0700316 auto shooter_message = shooter_queue.position.MakeMessage();
317 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800318 shooter_message->plunger = !shooter_plunger_reader_->value();
319 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700320 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
321 &shooter_message->pusher_proximal);
322 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
323 &shooter_message->pusher_distal);
324
325 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700326 }
327
328 {
329 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700330 top_reader_.RunIteration(&claw_message->top);
331 bottom_reader_.RunIteration(&claw_message->bottom);
332
Brian Silverman17f503e2015-08-02 18:17:18 -0700333 claw_message.Send();
334 }
335 }
336
337 void Quit() { run_ = false; }
338
339 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700340 class HalfClawReader {
341 public:
342 HalfClawReader(bool reversed) : reversed_(reversed) {}
343
344 void set_encoder(::std::unique_ptr<Encoder> encoder) {
345 encoder_ = ::std::move(encoder);
346 }
347
Austin Schuhc5e36082015-10-31 13:30:46 -0700348 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700349 front_hall_ = ::std::move(front_hall);
350 }
351
352 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700353 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700354 calibration_hall_ = ::std::move(calibration_hall);
355 }
356
Austin Schuhc5e36082015-10-31 13:30:46 -0700357 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700358 back_hall_ = ::std::move(back_hall);
359 }
360
361 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500362 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
363 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700364 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500365 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
366 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700367 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500368 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
369 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700370 synchronizer_.Add(back_counter_.get());
371 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500372 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
373 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700374 synchronizer_.Add(synchronized_encoder_.get());
375
376 synchronizer_.Start();
377 }
378
379 void Quit() { synchronizer_.Quit(); }
380
381 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
382 const double multiplier = reversed_ ? -1.0 : 1.0;
383
384 synchronizer_.RunIteration();
385
386 CopyPosition(front_counter_.get(), &half_claw_position->front);
387 CopyPosition(calibration_counter_.get(),
388 &half_claw_position->calibration);
389 CopyPosition(back_counter_.get(), &half_claw_position->back);
390 half_claw_position->position =
391 multiplier * claw_translate(synchronized_encoder_->get());
392 }
393
394 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500395 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
396 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700397 const double multiplier = reversed_ ? -1.0 : 1.0;
398
Austin Schuh5c25ab72015-11-01 13:17:11 -0800399 out->current = !counter->polled_value();
400 out->posedge_count = counter->negative_interrupt_count();
401 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700402 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800403 multiplier * claw_translate(counter->last_positive_encoder_value());
404 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700405 multiplier * claw_translate(counter->last_negative_encoder_value());
406 }
407
Brian Silverman5090c432016-01-02 14:44:26 -0800408 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700409
Brian Silvermanb601d892015-12-20 18:24:38 -0500410 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
411 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
412 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
413 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
414 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700415
416 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700417 ::std::unique_ptr<DigitalInput> front_hall_;
418 ::std::unique_ptr<DigitalInput> calibration_hall_;
419 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700420
421 const bool reversed_;
422 };
423
Brian Silvermanb601d892015-12-20 18:24:38 -0500424 void CopyShooterPosedgeCounts(
425 const ::frc971::wpilib::DMAEdgeCounter *counter,
426 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800427 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700428 // These are inverted because the hall effects give logical false when
429 // there's a magnet in front of them.
430 output->posedge_count = counter->negative_count();
431 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700432 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700433 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700434 }
435
Brian Silverman17f503e2015-08-02 18:17:18 -0700436 int32_t my_pid_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700437
Brian Silvermanb601d892015-12-20 18:24:38 -0500438 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700439
Brian Silverman552350b2015-08-02 18:23:34 -0700440 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700441
Brian Silverman552350b2015-08-02 18:23:34 -0700442 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
443 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
444 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
445 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
446 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
447 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700448
Brian Silverman552350b2015-08-02 18:23:34 -0700449 HalfClawReader top_reader_{false}, bottom_reader_{true};
450
451 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700452 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
453 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500454 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700455 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500456 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700457 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700458
459 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700460 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700461};
462
463class SolenoidWriter {
464 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500465 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700466 : pcm_(pcm),
Brian Silvermanc75c1ad2015-12-30 16:21:13 -0800467 shooter_(".y2014.control_loops.shooter_queue.output"),
Comran Morshed5323ecb2015-12-26 20:50:55 +0000468 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700469
Austin Schuhc5e36082015-10-31 13:30:46 -0700470 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700471 pressure_switch_ = ::std::move(pressure_switch);
472 }
473
474 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
475 compressor_relay_ = ::std::move(compressor_relay);
476 }
477
Brian Silvermanb601d892015-12-20 18:24:38 -0500478 void set_drivetrain_left(
479 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700480 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700481 }
482
Brian Silvermanb601d892015-12-20 18:24:38 -0500483 void set_drivetrain_right(
484 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700485 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700486 }
487
Brian Silvermanb601d892015-12-20 18:24:38 -0500488 void set_shooter_latch(
489 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700490 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700491 }
492
Brian Silvermanb601d892015-12-20 18:24:38 -0500493 void set_shooter_brake(
494 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700495 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700496 }
497
498 void operator()() {
499 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800500 ::aos::SetCurrentThreadRealtimePriority(27);
501
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700502 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
503 ::std::chrono::milliseconds(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700504
505 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800506 {
507 const int iterations = phased_loop.SleepUntilNext();
508 if (iterations != 1) {
509 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
510 }
511 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700512
513 {
Brian Silverman552350b2015-08-02 18:23:34 -0700514 shooter_.FetchLatest();
515 if (shooter_.get()) {
516 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
517 shooter_latch_->Set(!shooter_->latch_piston);
518 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700519 }
520 }
521
522 {
Brian Silverman552350b2015-08-02 18:23:34 -0700523 drivetrain_.FetchLatest();
524 if (drivetrain_.get()) {
525 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800526 drivetrain_left_->Set(!drivetrain_->left_high);
527 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700528 }
529 }
530
Brian Silverman17f503e2015-08-02 18:17:18 -0700531 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500532 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700533 {
534 const bool compressor_on = !pressure_switch_->Get();
535 to_log.compressor_on = compressor_on;
536 if (compressor_on) {
537 compressor_relay_->Set(Relay::kForward);
538 } else {
539 compressor_relay_->Set(Relay::kOff);
540 }
541 }
542
543 pcm_->Flush();
544 to_log.read_solenoids = pcm_->GetAll();
545 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
546 }
547 }
548 }
549
550 void Quit() { run_ = false; }
551
552 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500553 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700554
Brian Silvermanb601d892015-12-20 18:24:38 -0500555 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
556 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
557 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
558 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700559
Austin Schuhc5e36082015-10-31 13:30:46 -0700560 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700561 ::std::unique_ptr<Relay> compressor_relay_;
562
Brian Silvermanb601d892015-12-20 18:24:38 -0500563 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000564 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700565
566 ::std::atomic<bool> run_{true};
567};
568
Brian Silvermanb601d892015-12-20 18:24:38 -0500569class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700570 public:
571 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
572 left_drivetrain_talon_ = ::std::move(t);
573 }
574
575 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
576 right_drivetrain_talon_ = ::std::move(t);
577 }
578
579 private:
580 virtual void Read() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000581 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700582 }
583
584 virtual void Write() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000585 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700586 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800587 left_drivetrain_talon_->SetSpeed(-queue->left_voltage / 12.0);
588 right_drivetrain_talon_->SetSpeed(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700589 }
590
591 virtual void Stop() override {
592 LOG(WARNING, "drivetrain output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800593 left_drivetrain_talon_->SetDisabled();
594 right_drivetrain_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700595 }
596
597 ::std::unique_ptr<Talon> left_drivetrain_talon_;
598 ::std::unique_ptr<Talon> right_drivetrain_talon_;
599};
600
Brian Silvermanb601d892015-12-20 18:24:38 -0500601class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700602 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700603 void set_shooter_talon(::std::unique_ptr<Talon> t) {
604 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700605 }
606
607 private:
608 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500609 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700610 }
611
612 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500613 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700614 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800615 shooter_talon_->SetSpeed(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700616 }
617
618 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700619 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800620 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700621 }
622
Brian Silverman552350b2015-08-02 18:23:34 -0700623 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700624};
625
Brian Silvermanb601d892015-12-20 18:24:38 -0500626class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700627 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700628 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
629 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700630 }
631
Brian Silverman552350b2015-08-02 18:23:34 -0700632 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
633 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700634 }
635
Brian Silverman552350b2015-08-02 18:23:34 -0700636 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
637 left_tusk_talon_ = ::std::move(t);
638 }
639
640 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
641 right_tusk_talon_ = ::std::move(t);
642 }
643
644 void set_intake1_talon(::std::unique_ptr<Talon> t) {
645 intake1_talon_ = ::std::move(t);
646 }
647
648 void set_intake2_talon(::std::unique_ptr<Talon> t) {
649 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700650 }
651
652 private:
653 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500654 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700655 }
656
657 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500658 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700659 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800660 intake1_talon_->SetSpeed(queue->intake_voltage / 12.0);
661 intake2_talon_->SetSpeed(queue->intake_voltage / 12.0);
662 bottom_claw_talon_->SetSpeed(-queue->bottom_claw_voltage / 12.0);
663 top_claw_talon_->SetSpeed(queue->top_claw_voltage / 12.0);
664 left_tusk_talon_->SetSpeed(queue->tusk_voltage / 12.0);
665 right_tusk_talon_->SetSpeed(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700666 }
667
668 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700669 LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800670 intake1_talon_->SetDisabled();
671 intake2_talon_->SetDisabled();
672 bottom_claw_talon_->SetDisabled();
673 top_claw_talon_->SetDisabled();
674 left_tusk_talon_->SetDisabled();
675 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700676 }
677
Brian Silverman552350b2015-08-02 18:23:34 -0700678 ::std::unique_ptr<Talon> top_claw_talon_;
679 ::std::unique_ptr<Talon> bottom_claw_talon_;
680 ::std::unique_ptr<Talon> left_tusk_talon_;
681 ::std::unique_ptr<Talon> right_tusk_talon_;
682 ::std::unique_ptr<Talon> intake1_talon_;
683 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700684};
685
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800686class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700687 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700688 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700689 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
690 Encoder::k4X);
691 }
Brian Silverman552350b2015-08-02 18:23:34 -0700692
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800693 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700694 ::aos::InitNRT();
695 ::aos::SetCurrentThreadName("StartCompetition");
696
Brian Silvermanb601d892015-12-20 18:24:38 -0500697 ::frc971::wpilib::JoystickSender joystick_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700698 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700699
Brian Silverman425492b2015-12-30 15:23:55 -0800700 ::frc971::wpilib::PDPFetcher pdp_fetcher;
701 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Brian Silverman39b339e2016-01-03 13:24:22 -0800702 SensorReader reader;
Brian Silverman17f503e2015-08-02 18:17:18 -0700703
Brian Silverman85fbb602015-08-29 19:28:20 -0700704 // Create this first to make sure it ends up in one of the lower-numbered
705 // FPGA slots so we can use it with DMA.
706 auto shooter_encoder_temp = make_encoder(2);
707
Brian Silverman552350b2015-08-02 18:23:34 -0700708 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700709
Brian Silverman552350b2015-08-02 18:23:34 -0700710 reader.set_drivetrain_left_encoder(make_encoder(0));
711 reader.set_drivetrain_right_encoder(make_encoder(1));
712 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
713 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
714 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
715 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700716
Brian Silverman552350b2015-08-02 18:23:34 -0700717 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800718 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
719 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
720 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700721
Brian Silverman85fbb602015-08-29 19:28:20 -0700722 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800723 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
724 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
725 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700726
Brian Silverman85fbb602015-08-29 19:28:20 -0700727 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800728 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
729 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
730 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
731 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700732
Brian Silverman17f503e2015-08-02 18:17:18 -0700733 reader.set_dma(make_unique<DMA>());
734 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700735
Brian Silvermanb601d892015-12-20 18:24:38 -0500736 ::frc971::wpilib::GyroSender gyro_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700737 ::std::thread gyro_thread(::std::ref(gyro_sender));
738
739 DrivetrainWriter drivetrain_writer;
740 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700741 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700742 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700743 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700744 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
745
Brian Silvermanb601d892015-12-20 18:24:38 -0500746 ::y2014::wpilib::ClawWriter claw_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700747 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
748 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
749 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
750 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
751 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
752 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700753 ::std::thread claw_writer_thread(::std::ref(claw_writer));
754
Brian Silvermanb601d892015-12-20 18:24:38 -0500755 ::y2014::wpilib::ShooterWriter shooter_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700756 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
757 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
758
Brian Silverman17f503e2015-08-02 18:17:18 -0700759 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
760 new ::frc971::wpilib::BufferedPcm());
761 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700762 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
763 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
764 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
765 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700766
Austin Schuh016a6b02015-10-08 06:41:14 +0000767 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700768 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
769 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
770
771 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800772 while (true) {
773 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
774 if (r != 0) {
775 PLOG(WARNING, "infinite select failed");
776 } else {
777 PLOG(WARNING, "infinite select succeeded??\n");
778 }
779 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700780
781 LOG(ERROR, "Exiting WPILibRobot\n");
782
783 joystick_sender.Quit();
784 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800785 pdp_fetcher.Quit();
786 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700787 reader.Quit();
788 reader_thread.join();
789 gyro_sender.Quit();
790 gyro_thread.join();
791
792 drivetrain_writer.Quit();
793 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700794 shooter_writer.Quit();
795 shooter_writer_thread.join();
796 claw_writer.Quit();
797 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700798 solenoid_writer.Quit();
799 solenoid_thread.join();
800
801 ::aos::Cleanup();
802 }
803};
804
805} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500806} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700807
808
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800809AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);