blob: c9d15dd0818c7762a63f189e1979328d149e94a2 [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),
459 ::std::chrono::milliseconds(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700460
461 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800462 {
463 const int iterations = phased_loop.SleepUntilNext();
464 if (iterations != 1) {
465 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
466 }
467 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700468
469 {
Brian Silverman552350b2015-08-02 18:23:34 -0700470 shooter_.FetchLatest();
471 if (shooter_.get()) {
472 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
473 shooter_latch_->Set(!shooter_->latch_piston);
474 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700475 }
476 }
477
478 {
Brian Silverman552350b2015-08-02 18:23:34 -0700479 drivetrain_.FetchLatest();
480 if (drivetrain_.get()) {
481 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800482 drivetrain_left_->Set(!drivetrain_->left_high);
483 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700484 }
485 }
486
Brian Silverman17f503e2015-08-02 18:17:18 -0700487 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500488 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700489 {
490 const bool compressor_on = !pressure_switch_->Get();
491 to_log.compressor_on = compressor_on;
492 if (compressor_on) {
493 compressor_relay_->Set(Relay::kForward);
494 } else {
495 compressor_relay_->Set(Relay::kOff);
496 }
497 }
498
499 pcm_->Flush();
500 to_log.read_solenoids = pcm_->GetAll();
501 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
502 }
503 }
504 }
505
506 void Quit() { run_ = false; }
507
508 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500509 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700510
Brian Silvermanb601d892015-12-20 18:24:38 -0500511 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
512 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
513 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
514 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700515
Austin Schuhc5e36082015-10-31 13:30:46 -0700516 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700517 ::std::unique_ptr<Relay> compressor_relay_;
518
Brian Silvermanb601d892015-12-20 18:24:38 -0500519 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000520 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700521
522 ::std::atomic<bool> run_{true};
523};
524
Brian Silvermanb601d892015-12-20 18:24:38 -0500525class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700526 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800527 ShooterWriter(::aos::EventLoop *event_loop)
528 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
529
Brian Silverman552350b2015-08-02 18:23:34 -0700530 void set_shooter_talon(::std::unique_ptr<Talon> t) {
531 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700532 }
533
534 private:
535 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500536 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700537 }
538
539 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500540 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700541 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800542 shooter_talon_->SetSpeed(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700543 }
544
545 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700546 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800547 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700548 }
549
Brian Silverman552350b2015-08-02 18:23:34 -0700550 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700551};
552
Brian Silvermanb601d892015-12-20 18:24:38 -0500553class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700554 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800555 ClawWriter(::aos::EventLoop *event_loop)
556 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
557
Brian Silverman552350b2015-08-02 18:23:34 -0700558 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
559 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700560 }
561
Brian Silverman552350b2015-08-02 18:23:34 -0700562 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
563 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700564 }
565
Brian Silverman552350b2015-08-02 18:23:34 -0700566 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
567 left_tusk_talon_ = ::std::move(t);
568 }
569
570 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
571 right_tusk_talon_ = ::std::move(t);
572 }
573
574 void set_intake1_talon(::std::unique_ptr<Talon> t) {
575 intake1_talon_ = ::std::move(t);
576 }
577
578 void set_intake2_talon(::std::unique_ptr<Talon> t) {
579 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700580 }
581
582 private:
583 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500584 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700585 }
586
587 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500588 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700589 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800590 intake1_talon_->SetSpeed(queue->intake_voltage / 12.0);
591 intake2_talon_->SetSpeed(queue->intake_voltage / 12.0);
592 bottom_claw_talon_->SetSpeed(-queue->bottom_claw_voltage / 12.0);
593 top_claw_talon_->SetSpeed(queue->top_claw_voltage / 12.0);
594 left_tusk_talon_->SetSpeed(queue->tusk_voltage / 12.0);
595 right_tusk_talon_->SetSpeed(-queue->tusk_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, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800600 intake1_talon_->SetDisabled();
601 intake2_talon_->SetDisabled();
602 bottom_claw_talon_->SetDisabled();
603 top_claw_talon_->SetDisabled();
604 left_tusk_talon_->SetDisabled();
605 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700606 }
607
Brian Silverman552350b2015-08-02 18:23:34 -0700608 ::std::unique_ptr<Talon> top_claw_talon_;
609 ::std::unique_ptr<Talon> bottom_claw_talon_;
610 ::std::unique_ptr<Talon> left_tusk_talon_;
611 ::std::unique_ptr<Talon> right_tusk_talon_;
612 ::std::unique_ptr<Talon> intake1_talon_;
613 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700614};
615
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800616class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700617 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700618 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700619 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
620 Encoder::k4X);
621 }
Brian Silverman552350b2015-08-02 18:23:34 -0700622
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800623 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700624 ::aos::InitNRT();
625 ::aos::SetCurrentThreadName("StartCompetition");
626
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800627 ::aos::ShmEventLoop event_loop;
628
629 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700630 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700631
Austin Schuh0b545432019-05-12 15:46:12 -0700632 ::frc971::wpilib::PDPFetcher pdp_fetcher(&event_loop);
Brian Silverman425492b2015-12-30 15:23:55 -0800633 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800634 SensorReader reader(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700635
Brian Silverman85fbb602015-08-29 19:28:20 -0700636 // Create this first to make sure it ends up in one of the lower-numbered
637 // FPGA slots so we can use it with DMA.
638 auto shooter_encoder_temp = make_encoder(2);
639
Brian Silverman552350b2015-08-02 18:23:34 -0700640 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700641
Brian Silverman552350b2015-08-02 18:23:34 -0700642 reader.set_drivetrain_left_encoder(make_encoder(0));
643 reader.set_drivetrain_right_encoder(make_encoder(1));
644 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
645 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
646 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
647 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700648
Brian Silverman552350b2015-08-02 18:23:34 -0700649 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800650 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
651 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
652 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700653
Brian Silverman85fbb602015-08-29 19:28:20 -0700654 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800655 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
656 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
657 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700658
Brian Silverman85fbb602015-08-29 19:28:20 -0700659 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800660 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
661 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
662 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
663 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700664
Brian Silverman17f503e2015-08-02 18:17:18 -0700665 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700666
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800667 ::frc971::wpilib::GyroSender gyro_sender(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700668 ::std::thread gyro_thread(::std::ref(gyro_sender));
669
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800670 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davis05f580f2019-02-03 01:17:35 -0800671 drivetrain_writer.set_left_controller0(
672 ::std::unique_ptr<Talon>(new Talon(5)), true);
673 drivetrain_writer.set_right_controller0(
674 ::std::unique_ptr<Talon>(new Talon(2)), false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700675 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
676
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800677 ::y2014::wpilib::ClawWriter claw_writer(&event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700678 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
679 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
680 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
681 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
682 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
683 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700684 ::std::thread claw_writer_thread(::std::ref(claw_writer));
685
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800686 ::y2014::wpilib::ShooterWriter shooter_writer(&event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700687 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
688 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
689
Brian Silverman17f503e2015-08-02 18:17:18 -0700690 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
691 new ::frc971::wpilib::BufferedPcm());
692 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700693 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
694 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
695 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
696 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700697
Austin Schuh016a6b02015-10-08 06:41:14 +0000698 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700699 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
700 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
701
702 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800703 while (true) {
704 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
705 if (r != 0) {
706 PLOG(WARNING, "infinite select failed");
707 } else {
708 PLOG(WARNING, "infinite select succeeded??\n");
709 }
710 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700711
712 LOG(ERROR, "Exiting WPILibRobot\n");
713
714 joystick_sender.Quit();
715 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800716 pdp_fetcher.Quit();
717 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700718 reader.Quit();
719 reader_thread.join();
720 gyro_sender.Quit();
721 gyro_thread.join();
722
723 drivetrain_writer.Quit();
724 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700725 shooter_writer.Quit();
726 shooter_writer_thread.join();
727 claw_writer.Quit();
728 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700729 solenoid_writer.Quit();
730 solenoid_thread.join();
731
732 ::aos::Cleanup();
733 }
734};
735
736} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500737} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700738
739
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800740AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);