blob: 3bc5e39eb878355596abc0521a01eccf15a9a718 [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)
122 : ::frc971::wpilib::SensorReader(event_loop) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700123 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
124 // we should ever see.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800125 UpdateMediumEncoderFilterHz(kMaximumEncoderPulsesPerSecond);
Brian Silverman552350b2015-08-02 18:23:34 -0700126 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700127 }
128
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800129 ~SensorReader() override {
130 top_reader_.Quit();
131 bottom_reader_.Quit();
132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
135 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700136 }
137
Brian Silverman552350b2015-08-02 18:23:34 -0700138 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
139 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 }
141
Brian Silverman552350b2015-08-02 18:23:34 -0700142 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
143 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 }
145
Brian Silverman552350b2015-08-02 18:23:34 -0700146 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
147 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Brian Silverman552350b2015-08-02 18:23:34 -0700150 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
151 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700152 }
153
Brian Silverman552350b2015-08-02 18:23:34 -0700154 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800155 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700156 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700157 }
158
Austin Schuhc5e36082015-10-31 13:30:46 -0700159 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700160 hall_filter_.Add(hall.get());
161 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700162 }
163
Austin Schuhc5e36082015-10-31 13:30:46 -0700164 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700165 hall_filter_.Add(hall.get());
166 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700167 }
168
Austin Schuhc5e36082015-10-31 13:30:46 -0700169 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700170 hall_filter_.Add(hall.get());
171 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700172 }
173
Brian Silverman552350b2015-08-02 18:23:34 -0700174 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800175 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700176 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 }
178
Austin Schuhc5e36082015-10-31 13:30:46 -0700179 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700180 hall_filter_.Add(hall.get());
181 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700182 }
183
Austin Schuhc5e36082015-10-31 13:30:46 -0700184 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700185 hall_filter_.Add(hall.get());
186 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 }
188
Austin Schuhc5e36082015-10-31 13:30:46 -0700189 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700190 hall_filter_.Add(hall.get());
191 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700192 }
193
Brian Silverman552350b2015-08-02 18:23:34 -0700194 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800195 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700196 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700197 }
198
Austin Schuhc5e36082015-10-31 13:30:46 -0700199 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700200 hall_filter_.Add(hall.get());
201 shooter_proximal_ = ::std::move(hall);
202 }
203
Austin Schuhc5e36082015-10-31 13:30:46 -0700204 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700205 hall_filter_.Add(hall.get());
206 shooter_distal_ = ::std::move(hall);
207 }
208
Austin Schuhc5e36082015-10-31 13:30:46 -0700209 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700210 hall_filter_.Add(hall.get());
211 shooter_plunger_ = ::std::move(hall);
212 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500213 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700214 }
215
Austin Schuhc5e36082015-10-31 13:30:46 -0700216 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700217 hall_filter_.Add(hall.get());
218 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500219 shooter_latch_reader_ =
220 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700221 }
222
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800223 void Start() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500224 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700225 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500226 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700227 shooter_encoder_.get(), shooter_distal_.get());
228
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800229 AddToDMA(shooter_proximal_counter_.get());
230 AddToDMA(shooter_distal_counter_.get());
231 AddToDMA(shooter_plunger_reader_.get());
232 AddToDMA(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700233
Brian Silverman552350b2015-08-02 18:23:34 -0700234 top_reader_.Start();
235 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700236 }
237
238 void RunIteration() {
Brian Silverman552350b2015-08-02 18:23:34 -0700239 const auto &values = constants::GetValues();
240
Brian Silverman17f503e2015-08-02 18:17:18 -0700241 {
242 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
243 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700244 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700245 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700246 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800247 drivetrain_message->left_speed =
248 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
249 drivetrain_message->right_speed =
250 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800251
252 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
253 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700254 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800255 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
256 drivetrain_message->high_left_hall);
257
258 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
259 drivetrain_message->high_right_hall =
260 high_right_drive_hall_->GetVoltage();
261 drivetrain_message->right_shifter_position =
262 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
263 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700264
265 drivetrain_message.Send();
266 }
267
Brian Silvermanb601d892015-12-20 18:24:38 -0500268 ::y2014::sensors::auto_mode.MakeWithBuilder()
Brian Silverman552350b2015-08-02 18:23:34 -0700269 .voltage(auto_selector_analog_->GetVoltage())
270 .Send();
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800271 }
Brian Silverman552350b2015-08-02 18:23:34 -0700272
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800273 void RunDmaIteration() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700274 {
Brian Silverman552350b2015-08-02 18:23:34 -0700275 auto shooter_message = shooter_queue.position.MakeMessage();
276 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800277 shooter_message->plunger = !shooter_plunger_reader_->value();
278 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700279 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
280 &shooter_message->pusher_proximal);
281 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
282 &shooter_message->pusher_distal);
283
284 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700285 }
286
287 {
288 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700289 top_reader_.RunIteration(&claw_message->top);
290 bottom_reader_.RunIteration(&claw_message->bottom);
291
Brian Silverman17f503e2015-08-02 18:17:18 -0700292 claw_message.Send();
293 }
294 }
295
Brian Silverman17f503e2015-08-02 18:17:18 -0700296 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700297 class HalfClawReader {
298 public:
299 HalfClawReader(bool reversed) : reversed_(reversed) {}
300
301 void set_encoder(::std::unique_ptr<Encoder> encoder) {
302 encoder_ = ::std::move(encoder);
303 }
304
Austin Schuhc5e36082015-10-31 13:30:46 -0700305 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700306 front_hall_ = ::std::move(front_hall);
307 }
308
309 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700310 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700311 calibration_hall_ = ::std::move(calibration_hall);
312 }
313
Austin Schuhc5e36082015-10-31 13:30:46 -0700314 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700315 back_hall_ = ::std::move(back_hall);
316 }
317
318 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500319 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
320 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700321 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500322 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
323 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700324 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500325 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
326 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700327 synchronizer_.Add(back_counter_.get());
328 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500329 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
330 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700331 synchronizer_.Add(synchronized_encoder_.get());
332
333 synchronizer_.Start();
334 }
335
336 void Quit() { synchronizer_.Quit(); }
337
338 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
339 const double multiplier = reversed_ ? -1.0 : 1.0;
340
341 synchronizer_.RunIteration();
342
343 CopyPosition(front_counter_.get(), &half_claw_position->front);
344 CopyPosition(calibration_counter_.get(),
345 &half_claw_position->calibration);
346 CopyPosition(back_counter_.get(), &half_claw_position->back);
347 half_claw_position->position =
348 multiplier * claw_translate(synchronized_encoder_->get());
349 }
350
351 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500352 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
353 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700354 const double multiplier = reversed_ ? -1.0 : 1.0;
355
Austin Schuh5c25ab72015-11-01 13:17:11 -0800356 out->current = !counter->polled_value();
357 out->posedge_count = counter->negative_interrupt_count();
358 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700359 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800360 multiplier * claw_translate(counter->last_positive_encoder_value());
361 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700362 multiplier * claw_translate(counter->last_negative_encoder_value());
363 }
364
Brian Silverman5090c432016-01-02 14:44:26 -0800365 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700366
Brian Silvermanb601d892015-12-20 18:24:38 -0500367 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
368 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
369 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
370 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
371 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700372
373 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700374 ::std::unique_ptr<DigitalInput> front_hall_;
375 ::std::unique_ptr<DigitalInput> calibration_hall_;
376 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700377
378 const bool reversed_;
379 };
380
Brian Silvermanb601d892015-12-20 18:24:38 -0500381 void CopyShooterPosedgeCounts(
382 const ::frc971::wpilib::DMAEdgeCounter *counter,
383 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800384 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700385 // These are inverted because the hall effects give logical false when
386 // there's a magnet in front of them.
387 output->posedge_count = counter->negative_count();
388 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700389 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700390 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700391 }
392
Brian Silverman552350b2015-08-02 18:23:34 -0700393 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700394
Brian Silverman552350b2015-08-02 18:23:34 -0700395 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
396 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
397 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
398 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700399
Brian Silverman552350b2015-08-02 18:23:34 -0700400 HalfClawReader top_reader_{false}, bottom_reader_{true};
401
402 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700403 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
404 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500405 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700406 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500407 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700408 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700409
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800410 DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700411};
412
413class SolenoidWriter {
414 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500415 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700416 : pcm_(pcm),
Brian Silvermanc75c1ad2015-12-30 16:21:13 -0800417 shooter_(".y2014.control_loops.shooter_queue.output"),
Comran Morshed5323ecb2015-12-26 20:50:55 +0000418 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700419
Austin Schuhc5e36082015-10-31 13:30:46 -0700420 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700421 pressure_switch_ = ::std::move(pressure_switch);
422 }
423
424 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
425 compressor_relay_ = ::std::move(compressor_relay);
426 }
427
Brian Silvermanb601d892015-12-20 18:24:38 -0500428 void set_drivetrain_left(
429 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700430 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700431 }
432
Brian Silvermanb601d892015-12-20 18:24:38 -0500433 void set_drivetrain_right(
434 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700435 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700436 }
437
Brian Silvermanb601d892015-12-20 18:24:38 -0500438 void set_shooter_latch(
439 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700440 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700441 }
442
Brian Silvermanb601d892015-12-20 18:24:38 -0500443 void set_shooter_brake(
444 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700445 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700446 }
447
448 void operator()() {
449 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800450 ::aos::SetCurrentThreadRealtimePriority(27);
451
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700452 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
453 ::std::chrono::milliseconds(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700454
455 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800456 {
457 const int iterations = phased_loop.SleepUntilNext();
458 if (iterations != 1) {
459 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
460 }
461 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700462
463 {
Brian Silverman552350b2015-08-02 18:23:34 -0700464 shooter_.FetchLatest();
465 if (shooter_.get()) {
466 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
467 shooter_latch_->Set(!shooter_->latch_piston);
468 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700469 }
470 }
471
472 {
Brian Silverman552350b2015-08-02 18:23:34 -0700473 drivetrain_.FetchLatest();
474 if (drivetrain_.get()) {
475 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800476 drivetrain_left_->Set(!drivetrain_->left_high);
477 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700478 }
479 }
480
Brian Silverman17f503e2015-08-02 18:17:18 -0700481 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500482 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700483 {
484 const bool compressor_on = !pressure_switch_->Get();
485 to_log.compressor_on = compressor_on;
486 if (compressor_on) {
487 compressor_relay_->Set(Relay::kForward);
488 } else {
489 compressor_relay_->Set(Relay::kOff);
490 }
491 }
492
493 pcm_->Flush();
494 to_log.read_solenoids = pcm_->GetAll();
495 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
496 }
497 }
498 }
499
500 void Quit() { run_ = false; }
501
502 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500503 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700504
Brian Silvermanb601d892015-12-20 18:24:38 -0500505 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
506 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
507 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
508 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700509
Austin Schuhc5e36082015-10-31 13:30:46 -0700510 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700511 ::std::unique_ptr<Relay> compressor_relay_;
512
Brian Silvermanb601d892015-12-20 18:24:38 -0500513 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000514 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700515
516 ::std::atomic<bool> run_{true};
517};
518
Brian Silvermanb601d892015-12-20 18:24:38 -0500519class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700520 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800521 ShooterWriter(::aos::EventLoop *event_loop)
522 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
523
Brian Silverman552350b2015-08-02 18:23:34 -0700524 void set_shooter_talon(::std::unique_ptr<Talon> t) {
525 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700526 }
527
528 private:
529 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500530 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700531 }
532
533 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500534 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700535 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800536 shooter_talon_->SetSpeed(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700537 }
538
539 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700540 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800541 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700542 }
543
Brian Silverman552350b2015-08-02 18:23:34 -0700544 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700545};
546
Brian Silvermanb601d892015-12-20 18:24:38 -0500547class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700548 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800549 ClawWriter(::aos::EventLoop *event_loop)
550 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
551
Brian Silverman552350b2015-08-02 18:23:34 -0700552 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
553 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700554 }
555
Brian Silverman552350b2015-08-02 18:23:34 -0700556 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
557 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700558 }
559
Brian Silverman552350b2015-08-02 18:23:34 -0700560 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
561 left_tusk_talon_ = ::std::move(t);
562 }
563
564 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
565 right_tusk_talon_ = ::std::move(t);
566 }
567
568 void set_intake1_talon(::std::unique_ptr<Talon> t) {
569 intake1_talon_ = ::std::move(t);
570 }
571
572 void set_intake2_talon(::std::unique_ptr<Talon> t) {
573 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700574 }
575
576 private:
577 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500578 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700579 }
580
581 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500582 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700583 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800584 intake1_talon_->SetSpeed(queue->intake_voltage / 12.0);
585 intake2_talon_->SetSpeed(queue->intake_voltage / 12.0);
586 bottom_claw_talon_->SetSpeed(-queue->bottom_claw_voltage / 12.0);
587 top_claw_talon_->SetSpeed(queue->top_claw_voltage / 12.0);
588 left_tusk_talon_->SetSpeed(queue->tusk_voltage / 12.0);
589 right_tusk_talon_->SetSpeed(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 }
591
592 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700593 LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800594 intake1_talon_->SetDisabled();
595 intake2_talon_->SetDisabled();
596 bottom_claw_talon_->SetDisabled();
597 top_claw_talon_->SetDisabled();
598 left_tusk_talon_->SetDisabled();
599 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700600 }
601
Brian Silverman552350b2015-08-02 18:23:34 -0700602 ::std::unique_ptr<Talon> top_claw_talon_;
603 ::std::unique_ptr<Talon> bottom_claw_talon_;
604 ::std::unique_ptr<Talon> left_tusk_talon_;
605 ::std::unique_ptr<Talon> right_tusk_talon_;
606 ::std::unique_ptr<Talon> intake1_talon_;
607 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700608};
609
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800610class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700611 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700612 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700613 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
614 Encoder::k4X);
615 }
Brian Silverman552350b2015-08-02 18:23:34 -0700616
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800617 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700618 ::aos::InitNRT();
619 ::aos::SetCurrentThreadName("StartCompetition");
620
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800621 ::aos::ShmEventLoop event_loop;
622
623 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700624 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700625
Austin Schuh0b545432019-05-12 15:46:12 -0700626 ::frc971::wpilib::PDPFetcher pdp_fetcher(&event_loop);
Brian Silverman425492b2015-12-30 15:23:55 -0800627 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800628 SensorReader reader(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700629
Brian Silverman85fbb602015-08-29 19:28:20 -0700630 // Create this first to make sure it ends up in one of the lower-numbered
631 // FPGA slots so we can use it with DMA.
632 auto shooter_encoder_temp = make_encoder(2);
633
Brian Silverman552350b2015-08-02 18:23:34 -0700634 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700635
Brian Silverman552350b2015-08-02 18:23:34 -0700636 reader.set_drivetrain_left_encoder(make_encoder(0));
637 reader.set_drivetrain_right_encoder(make_encoder(1));
638 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
639 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
640 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
641 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700642
Brian Silverman552350b2015-08-02 18:23:34 -0700643 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800644 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
645 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
646 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700647
Brian Silverman85fbb602015-08-29 19:28:20 -0700648 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800649 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
650 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
651 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700652
Brian Silverman85fbb602015-08-29 19:28:20 -0700653 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800654 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
655 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
656 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
657 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700658
Brian Silverman17f503e2015-08-02 18:17:18 -0700659 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700660
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800661 ::frc971::wpilib::GyroSender gyro_sender(&event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700662 ::std::thread gyro_thread(::std::ref(gyro_sender));
663
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800664 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davis05f580f2019-02-03 01:17:35 -0800665 drivetrain_writer.set_left_controller0(
666 ::std::unique_ptr<Talon>(new Talon(5)), true);
667 drivetrain_writer.set_right_controller0(
668 ::std::unique_ptr<Talon>(new Talon(2)), false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700669 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
670
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800671 ::y2014::wpilib::ClawWriter claw_writer(&event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700672 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
673 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
674 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
675 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
676 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
677 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700678 ::std::thread claw_writer_thread(::std::ref(claw_writer));
679
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800680 ::y2014::wpilib::ShooterWriter shooter_writer(&event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700681 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
682 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
683
Brian Silverman17f503e2015-08-02 18:17:18 -0700684 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
685 new ::frc971::wpilib::BufferedPcm());
686 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700687 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
688 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
689 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
690 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700691
Austin Schuh016a6b02015-10-08 06:41:14 +0000692 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700693 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
694 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
695
696 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800697 while (true) {
698 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
699 if (r != 0) {
700 PLOG(WARNING, "infinite select failed");
701 } else {
702 PLOG(WARNING, "infinite select succeeded??\n");
703 }
704 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700705
706 LOG(ERROR, "Exiting WPILibRobot\n");
707
708 joystick_sender.Quit();
709 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800710 pdp_fetcher.Quit();
711 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700712 reader.Quit();
713 reader_thread.join();
714 gyro_sender.Quit();
715 gyro_thread.join();
716
717 drivetrain_writer.Quit();
718 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700719 shooter_writer.Quit();
720 shooter_writer_thread.join();
721 claw_writer.Quit();
722 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700723 solenoid_writer.Quit();
724 solenoid_thread.join();
725
726 ::aos::Cleanup();
727 }
728};
729
730} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500731} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700732
733
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800734AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);