blob: 09edf96ee54279741428560782cc22d74c07ae70 [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
Austin Schuhdf6cbb12019-02-02 13:46:52 -080021#include "aos/events/shm-event-loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080022#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070023#include "aos/logging/logging.h"
24#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080025#include "aos/make_unique.h"
26#include "aos/robot_state/robot_state.q.h"
27#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070028#include "aos/time/time.h"
29#include "aos/util/log_interval.h"
30#include "aos/util/phased_loop.h"
31#include "aos/util/wrapping_counter.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000032#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080033#include "frc971/shifter_hall_effect.h"
34#include "frc971/wpilib/buffered_pcm.h"
35#include "frc971/wpilib/buffered_solenoid.h"
36#include "frc971/wpilib/dma.h"
37#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davis05f580f2019-02-03 01:17:35 -080038#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080039#include "frc971/wpilib/encoder_and_potentiometer.h"
40#include "frc971/wpilib/gyro_sender.h"
41#include "frc971/wpilib/interrupt_edge_counting.h"
42#include "frc971/wpilib/joystick_sender.h"
43#include "frc971/wpilib/logging.q.h"
44#include "frc971/wpilib/loop_output_handler.h"
45#include "frc971/wpilib/pdp_fetcher.h"
46#include "frc971/wpilib/sensor_reader.h"
47#include "y2014/constants.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070048#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070049#include "y2014/control_loops/shooter/shooter.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070050#include "y2014/queues/auto_mode.q.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 Silvermanf819b442019-01-20 16:51:04 -080060using aos::make_unique;
Brian Silverman17f503e2015-08-02 18:17:18 -070061
Brian Silvermanb601d892015-12-20 18:24:38 -050062namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070063namespace wpilib {
64
Brian Silverman85fbb602015-08-29 19:28:20 -070065// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
66// DMA stuff and then removing the * 2.0 in *_translate.
67// The low bit is direction.
68
Brian Silverman17f503e2015-08-02 18:17:18 -070069double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070070 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070071 (256.0 /*cpr*/ * 4.0 /*4x*/) *
72 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080073 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070074}
75
Brian Silverman51091a02015-12-26 15:56:58 -080076double drivetrain_velocity_translate(double in) {
77 return (1.0 / in) / 256.0 /*cpr*/ *
78 constants::GetValues().drivetrain_encoder_ratio *
79 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
80}
81
Sabina Davis415bb6c2017-10-16 23:30:52 -070082float hall_translate(const constants::DualHallShifterHallEffect &k, float in_low,
Brian Silverman552350b2015-08-02 18:23:34 -070083 float in_high) {
84 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070085 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
86 static_cast<float>(k.low_gear_middle - k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070087 const float high_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070088 0.5 +
89 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
90 static_cast<float>(k.shifter_hall_effect.high_gear_high -
91 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070092
Brian Silverman552350b2015-08-02 18:23:34 -070093 // Return low when we are below 1/2, and high when we are above 1/2.
94 if (low_ratio + high_ratio < 1.0) {
95 return low_ratio;
96 } else {
97 return high_ratio;
98 }
Brian Silverman17f503e2015-08-02 18:17:18 -070099}
100
101double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700102 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700103 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700104 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700105}
106
Brian Silverman552350b2015-08-02 18:23:34 -0700107double shooter_translate(int32_t in) {
108 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
109 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
110 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700111}
112
113static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700114 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800115 18.0 / 32.0 /* big belt reduction */ * 18.0 /
116 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
117 60.0 /* seconds / minute */ * 256.0 /* CPR */ * 4.0 /* edges / pulse */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700118
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800119class SensorReader : public ::frc971::wpilib::SensorReader {
Brian Silverman17f503e2015-08-02 18:17:18 -0700120 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800121 SensorReader(::aos::EventLoop *event_loop)
Austin Schuh7eed2de2019-05-25 14:34:40 -0700122 : ::frc971::wpilib::SensorReader(event_loop),
123 auto_mode_sender_(event_loop->MakeSender<::y2014::sensors::AutoMode>(
124 ".y2014.sensors.auto_mode")) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700125 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
126 // we should ever see.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800127 UpdateMediumEncoderFilterHz(kMaximumEncoderPulsesPerSecond);
Brian Silverman552350b2015-08-02 18:23:34 -0700128 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700129 }
130
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800131 ~SensorReader() override {
132 top_reader_.Quit();
133 bottom_reader_.Quit();
134 }
135
Brian Silverman552350b2015-08-02 18:23:34 -0700136 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
137 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700138 }
139
Brian Silverman552350b2015-08-02 18:23:34 -0700140 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
141 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700142 }
143
Brian Silverman552350b2015-08-02 18:23:34 -0700144 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
145 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700146 }
147
Brian Silverman552350b2015-08-02 18:23:34 -0700148 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
149 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700150 }
151
Brian Silverman552350b2015-08-02 18:23:34 -0700152 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
153 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 }
155
Brian Silverman552350b2015-08-02 18:23:34 -0700156 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800157 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700158 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700159 }
160
Austin Schuhc5e36082015-10-31 13:30:46 -0700161 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700162 hall_filter_.Add(hall.get());
163 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700164 }
165
Austin Schuhc5e36082015-10-31 13:30:46 -0700166 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700167 hall_filter_.Add(hall.get());
168 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700169 }
170
Austin Schuhc5e36082015-10-31 13:30:46 -0700171 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700172 hall_filter_.Add(hall.get());
173 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700174 }
175
Brian Silverman552350b2015-08-02 18:23:34 -0700176 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800177 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700178 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700179 }
180
Austin Schuhc5e36082015-10-31 13:30:46 -0700181 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700182 hall_filter_.Add(hall.get());
183 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700184 }
185
Austin Schuhc5e36082015-10-31 13:30:46 -0700186 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700187 hall_filter_.Add(hall.get());
188 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700189 }
190
Austin Schuhc5e36082015-10-31 13:30:46 -0700191 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700192 hall_filter_.Add(hall.get());
193 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700194 }
195
Brian Silverman552350b2015-08-02 18:23:34 -0700196 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800197 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700198 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700199 }
200
Austin Schuhc5e36082015-10-31 13:30:46 -0700201 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700202 hall_filter_.Add(hall.get());
203 shooter_proximal_ = ::std::move(hall);
204 }
205
Austin Schuhc5e36082015-10-31 13:30:46 -0700206 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700207 hall_filter_.Add(hall.get());
208 shooter_distal_ = ::std::move(hall);
209 }
210
Austin Schuhc5e36082015-10-31 13:30:46 -0700211 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700212 hall_filter_.Add(hall.get());
213 shooter_plunger_ = ::std::move(hall);
214 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500215 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700216 }
217
Austin Schuhc5e36082015-10-31 13:30:46 -0700218 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700219 hall_filter_.Add(hall.get());
220 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500221 shooter_latch_reader_ =
222 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700223 }
224
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800225 void Start() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500226 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700227 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500228 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700229 shooter_encoder_.get(), shooter_distal_.get());
230
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800231 AddToDMA(shooter_proximal_counter_.get());
232 AddToDMA(shooter_distal_counter_.get());
233 AddToDMA(shooter_plunger_reader_.get());
234 AddToDMA(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700235
Brian Silverman552350b2015-08-02 18:23:34 -0700236 top_reader_.Start();
237 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700238 }
239
240 void RunIteration() {
Brian Silverman552350b2015-08-02 18:23:34 -0700241 const auto &values = constants::GetValues();
242
Brian Silverman17f503e2015-08-02 18:17:18 -0700243 {
244 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
245 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700246 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700247 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700248 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800249 drivetrain_message->left_speed =
250 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
251 drivetrain_message->right_speed =
252 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800253
254 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
255 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700256 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800257 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
258 drivetrain_message->high_left_hall);
259
260 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
261 drivetrain_message->high_right_hall =
262 high_right_drive_hall_->GetVoltage();
263 drivetrain_message->right_shifter_position =
264 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
265 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700266
267 drivetrain_message.Send();
268 }
269
Austin Schuh7eed2de2019-05-25 14:34:40 -0700270 {
271 auto auto_mode_message = auto_mode_sender_.MakeMessage();
272 auto_mode_message->voltage = auto_selector_analog_->GetVoltage();
273 auto_mode_message.Send();
274 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800275 }
Brian Silverman552350b2015-08-02 18:23:34 -0700276
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800277 void RunDmaIteration() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700278 {
Brian Silverman552350b2015-08-02 18:23:34 -0700279 auto shooter_message = shooter_queue.position.MakeMessage();
280 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800281 shooter_message->plunger = !shooter_plunger_reader_->value();
282 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700283 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
284 &shooter_message->pusher_proximal);
285 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
286 &shooter_message->pusher_distal);
287
288 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700289 }
290
291 {
292 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700293 top_reader_.RunIteration(&claw_message->top);
294 bottom_reader_.RunIteration(&claw_message->bottom);
295
Brian Silverman17f503e2015-08-02 18:17:18 -0700296 claw_message.Send();
297 }
298 }
299
Brian Silverman17f503e2015-08-02 18:17:18 -0700300 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700301 class HalfClawReader {
302 public:
303 HalfClawReader(bool reversed) : reversed_(reversed) {}
304
305 void set_encoder(::std::unique_ptr<Encoder> encoder) {
306 encoder_ = ::std::move(encoder);
307 }
308
Austin Schuhc5e36082015-10-31 13:30:46 -0700309 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700310 front_hall_ = ::std::move(front_hall);
311 }
312
313 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700314 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700315 calibration_hall_ = ::std::move(calibration_hall);
316 }
317
Austin Schuhc5e36082015-10-31 13:30:46 -0700318 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700319 back_hall_ = ::std::move(back_hall);
320 }
321
322 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500323 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
324 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700325 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500326 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
327 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700328 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500329 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
330 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700331 synchronizer_.Add(back_counter_.get());
332 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500333 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
334 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700335 synchronizer_.Add(synchronized_encoder_.get());
336
337 synchronizer_.Start();
338 }
339
340 void Quit() { synchronizer_.Quit(); }
341
342 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
343 const double multiplier = reversed_ ? -1.0 : 1.0;
344
345 synchronizer_.RunIteration();
346
347 CopyPosition(front_counter_.get(), &half_claw_position->front);
348 CopyPosition(calibration_counter_.get(),
349 &half_claw_position->calibration);
350 CopyPosition(back_counter_.get(), &half_claw_position->back);
351 half_claw_position->position =
352 multiplier * claw_translate(synchronized_encoder_->get());
353 }
354
355 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500356 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
357 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700358 const double multiplier = reversed_ ? -1.0 : 1.0;
359
Austin Schuh5c25ab72015-11-01 13:17:11 -0800360 out->current = !counter->polled_value();
361 out->posedge_count = counter->negative_interrupt_count();
362 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700363 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800364 multiplier * claw_translate(counter->last_positive_encoder_value());
365 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700366 multiplier * claw_translate(counter->last_negative_encoder_value());
367 }
368
Brian Silverman5090c432016-01-02 14:44:26 -0800369 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700370
Brian Silvermanb601d892015-12-20 18:24:38 -0500371 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
372 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
373 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
374 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
375 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700376
377 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700378 ::std::unique_ptr<DigitalInput> front_hall_;
379 ::std::unique_ptr<DigitalInput> calibration_hall_;
380 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700381
382 const bool reversed_;
383 };
384
Brian Silvermanb601d892015-12-20 18:24:38 -0500385 void CopyShooterPosedgeCounts(
386 const ::frc971::wpilib::DMAEdgeCounter *counter,
387 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800388 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700389 // These are inverted because the hall effects give logical false when
390 // there's a magnet in front of them.
391 output->posedge_count = counter->negative_count();
392 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700393 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700394 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700395 }
396
Austin Schuh7eed2de2019-05-25 14:34:40 -0700397 ::aos::Sender<::y2014::sensors::AutoMode> auto_mode_sender_;
398
Brian Silverman552350b2015-08-02 18:23:34 -0700399 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700400
Brian Silverman552350b2015-08-02 18:23:34 -0700401 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
402 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
403 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
404 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700405
Brian Silverman552350b2015-08-02 18:23:34 -0700406 HalfClawReader top_reader_{false}, bottom_reader_{true};
407
408 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700409 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
410 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500411 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700412 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500413 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700414 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700415
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800416 DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700417};
418
419class SolenoidWriter {
420 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500421 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700422 : pcm_(pcm),
Brian Silvermanc75c1ad2015-12-30 16:21:13 -0800423 shooter_(".y2014.control_loops.shooter_queue.output"),
Comran Morshed5323ecb2015-12-26 20:50:55 +0000424 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700425
Austin Schuhc5e36082015-10-31 13:30:46 -0700426 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700427 pressure_switch_ = ::std::move(pressure_switch);
428 }
429
430 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
431 compressor_relay_ = ::std::move(compressor_relay);
432 }
433
Brian Silvermanb601d892015-12-20 18:24:38 -0500434 void set_drivetrain_left(
435 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700436 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700437 }
438
Brian Silvermanb601d892015-12-20 18:24:38 -0500439 void set_drivetrain_right(
440 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700441 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700442 }
443
Brian Silvermanb601d892015-12-20 18:24:38 -0500444 void set_shooter_latch(
445 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700446 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700447 }
448
Brian Silvermanb601d892015-12-20 18:24:38 -0500449 void set_shooter_brake(
450 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700451 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700452 }
453
454 void operator()() {
455 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800456 ::aos::SetCurrentThreadRealtimePriority(27);
457
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700458 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
Austin Schuhd32b3622019-06-23 18:49:06 -0700459 ::aos::monotonic_clock::now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700460 ::std::chrono::milliseconds(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700461
462 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800463 {
464 const int iterations = phased_loop.SleepUntilNext();
465 if (iterations != 1) {
466 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
467 }
468 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700469
470 {
Brian Silverman552350b2015-08-02 18:23:34 -0700471 shooter_.FetchLatest();
472 if (shooter_.get()) {
473 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
474 shooter_latch_->Set(!shooter_->latch_piston);
475 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700476 }
477 }
478
479 {
Brian Silverman552350b2015-08-02 18:23:34 -0700480 drivetrain_.FetchLatest();
481 if (drivetrain_.get()) {
482 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800483 drivetrain_left_->Set(!drivetrain_->left_high);
484 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700485 }
486 }
487
Brian Silverman17f503e2015-08-02 18:17:18 -0700488 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500489 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700490 {
491 const bool compressor_on = !pressure_switch_->Get();
492 to_log.compressor_on = compressor_on;
493 if (compressor_on) {
494 compressor_relay_->Set(Relay::kForward);
495 } else {
496 compressor_relay_->Set(Relay::kOff);
497 }
498 }
499
500 pcm_->Flush();
501 to_log.read_solenoids = pcm_->GetAll();
502 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
503 }
504 }
505 }
506
507 void Quit() { run_ = false; }
508
509 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500510 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700511
Brian Silvermanb601d892015-12-20 18:24:38 -0500512 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
513 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
514 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
515 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700516
Austin Schuhc5e36082015-10-31 13:30:46 -0700517 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700518 ::std::unique_ptr<Relay> compressor_relay_;
519
Brian Silvermanb601d892015-12-20 18:24:38 -0500520 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000521 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700522
523 ::std::atomic<bool> run_{true};
524};
525
Brian Silvermanb601d892015-12-20 18:24:38 -0500526class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700527 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800528 ShooterWriter(::aos::EventLoop *event_loop)
529 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
530
Brian Silverman552350b2015-08-02 18:23:34 -0700531 void set_shooter_talon(::std::unique_ptr<Talon> t) {
532 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700533 }
534
535 private:
536 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500537 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700538 }
539
540 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500541 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700542 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800543 shooter_talon_->SetSpeed(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700544 }
545
546 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700547 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800548 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700549 }
550
Brian Silverman552350b2015-08-02 18:23:34 -0700551 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700552};
553
Brian Silvermanb601d892015-12-20 18:24:38 -0500554class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700555 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800556 ClawWriter(::aos::EventLoop *event_loop)
557 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
558
Brian Silverman552350b2015-08-02 18:23:34 -0700559 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
560 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700561 }
562
Brian Silverman552350b2015-08-02 18:23:34 -0700563 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
564 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700565 }
566
Brian Silverman552350b2015-08-02 18:23:34 -0700567 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
568 left_tusk_talon_ = ::std::move(t);
569 }
570
571 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
572 right_tusk_talon_ = ::std::move(t);
573 }
574
575 void set_intake1_talon(::std::unique_ptr<Talon> t) {
576 intake1_talon_ = ::std::move(t);
577 }
578
579 void set_intake2_talon(::std::unique_ptr<Talon> t) {
580 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700581 }
582
583 private:
584 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500585 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700586 }
587
588 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500589 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800591 intake1_talon_->SetSpeed(queue->intake_voltage / 12.0);
592 intake2_talon_->SetSpeed(queue->intake_voltage / 12.0);
593 bottom_claw_talon_->SetSpeed(-queue->bottom_claw_voltage / 12.0);
594 top_claw_talon_->SetSpeed(queue->top_claw_voltage / 12.0);
595 left_tusk_talon_->SetSpeed(queue->tusk_voltage / 12.0);
596 right_tusk_talon_->SetSpeed(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700597 }
598
599 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700600 LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800601 intake1_talon_->SetDisabled();
602 intake2_talon_->SetDisabled();
603 bottom_claw_talon_->SetDisabled();
604 top_claw_talon_->SetDisabled();
605 left_tusk_talon_->SetDisabled();
606 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700607 }
608
Brian Silverman552350b2015-08-02 18:23:34 -0700609 ::std::unique_ptr<Talon> top_claw_talon_;
610 ::std::unique_ptr<Talon> bottom_claw_talon_;
611 ::std::unique_ptr<Talon> left_tusk_talon_;
612 ::std::unique_ptr<Talon> right_tusk_talon_;
613 ::std::unique_ptr<Talon> intake1_talon_;
614 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700615};
616
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800617class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700618 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700619 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700620 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
621 Encoder::k4X);
622 }
Brian Silverman552350b2015-08-02 18:23:34 -0700623
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800624 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700625 ::aos::InitNRT();
626 ::aos::SetCurrentThreadName("StartCompetition");
627
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800628 ::aos::ShmEventLoop event_loop;
629
630 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700631 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700632
Austin Schuh0b545432019-05-12 15:46:12 -0700633 ::frc971::wpilib::PDPFetcher pdp_fetcher(&event_loop);
Brian Silverman425492b2015-12-30 15:23:55 -0800634 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800635 SensorReader reader(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700636
Brian Silverman85fbb602015-08-29 19:28:20 -0700637 // Create this first to make sure it ends up in one of the lower-numbered
638 // FPGA slots so we can use it with DMA.
639 auto shooter_encoder_temp = make_encoder(2);
640
Brian Silverman552350b2015-08-02 18:23:34 -0700641 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700642
Brian Silverman552350b2015-08-02 18:23:34 -0700643 reader.set_drivetrain_left_encoder(make_encoder(0));
644 reader.set_drivetrain_right_encoder(make_encoder(1));
645 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
646 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
647 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
648 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700649
Brian Silverman552350b2015-08-02 18:23:34 -0700650 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800651 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
652 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
653 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700654
Brian Silverman85fbb602015-08-29 19:28:20 -0700655 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800656 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
657 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
658 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700659
Brian Silverman85fbb602015-08-29 19:28:20 -0700660 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800661 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
662 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
663 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
664 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700665
Brian Silverman17f503e2015-08-02 18:17:18 -0700666 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700667
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800668 ::frc971::wpilib::GyroSender gyro_sender(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700669 ::std::thread gyro_thread(::std::ref(gyro_sender));
670
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800671 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davis05f580f2019-02-03 01:17:35 -0800672 drivetrain_writer.set_left_controller0(
673 ::std::unique_ptr<Talon>(new Talon(5)), true);
674 drivetrain_writer.set_right_controller0(
675 ::std::unique_ptr<Talon>(new Talon(2)), false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700676 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
677
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800678 ::y2014::wpilib::ClawWriter claw_writer(&event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700679 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
680 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
681 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
682 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
683 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
684 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700685 ::std::thread claw_writer_thread(::std::ref(claw_writer));
686
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800687 ::y2014::wpilib::ShooterWriter shooter_writer(&event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700688 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
689 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
690
Brian Silverman17f503e2015-08-02 18:17:18 -0700691 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
692 new ::frc971::wpilib::BufferedPcm());
693 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700694 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
695 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
696 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
697 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700698
Austin Schuh016a6b02015-10-08 06:41:14 +0000699 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700700 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
701 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
702
703 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800704 while (true) {
705 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
706 if (r != 0) {
707 PLOG(WARNING, "infinite select failed");
708 } else {
709 PLOG(WARNING, "infinite select succeeded??\n");
710 }
711 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700712
713 LOG(ERROR, "Exiting WPILibRobot\n");
714
715 joystick_sender.Quit();
716 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800717 pdp_fetcher.Quit();
718 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700719 reader.Quit();
720 reader_thread.join();
721 gyro_sender.Quit();
722 gyro_thread.join();
723
724 drivetrain_writer.Quit();
725 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700726 shooter_writer.Quit();
727 shooter_writer_thread.join();
728 claw_writer.Quit();
729 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700730 solenoid_writer.Quit();
731 solenoid_thread.join();
732
733 ::aos::Cleanup();
734 }
735};
736
737} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500738} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700739
740
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800741AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);