blob: d7aa8ee847a0458f25a33dfdeb28a0170cdb1d74 [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
Brian Silvermanf819b442019-01-20 16:51:04 -080021#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070022#include "aos/logging/logging.h"
23#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080024#include "aos/make_unique.h"
25#include "aos/robot_state/robot_state.q.h"
26#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070027#include "aos/time/time.h"
28#include "aos/util/log_interval.h"
29#include "aos/util/phased_loop.h"
30#include "aos/util/wrapping_counter.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000031#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080032#include "frc971/shifter_hall_effect.h"
33#include "frc971/wpilib/buffered_pcm.h"
34#include "frc971/wpilib/buffered_solenoid.h"
35#include "frc971/wpilib/dma.h"
36#include "frc971/wpilib/dma_edge_counting.h"
37#include "frc971/wpilib/encoder_and_potentiometer.h"
38#include "frc971/wpilib/gyro_sender.h"
39#include "frc971/wpilib/interrupt_edge_counting.h"
40#include "frc971/wpilib/joystick_sender.h"
41#include "frc971/wpilib/logging.q.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/pdp_fetcher.h"
44#include "frc971/wpilib/sensor_reader.h"
45#include "y2014/constants.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070046#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070047#include "y2014/control_loops/shooter/shooter.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070048#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070049
Brian Silverman17f503e2015-08-02 18:17:18 -070050#ifndef M_PI
51#define M_PI 3.14159265358979323846
52#endif
53
Comran Morshed5323ecb2015-12-26 20:50:55 +000054using ::frc971::control_loops::drivetrain_queue;
Brian Silvermanb601d892015-12-20 18:24:38 -050055using ::y2014::control_loops::claw_queue;
56using ::y2014::control_loops::shooter_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080057using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080058using aos::make_unique;
Brian Silverman17f503e2015-08-02 18:17:18 -070059
Brian Silvermanb601d892015-12-20 18:24:38 -050060namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070061namespace wpilib {
62
Brian Silverman85fbb602015-08-29 19:28:20 -070063// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
64// DMA stuff and then removing the * 2.0 in *_translate.
65// The low bit is direction.
66
Brian Silverman17f503e2015-08-02 18:17:18 -070067double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070068 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070069 (256.0 /*cpr*/ * 4.0 /*4x*/) *
70 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080071 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070072}
73
Brian Silverman51091a02015-12-26 15:56:58 -080074double drivetrain_velocity_translate(double in) {
75 return (1.0 / in) / 256.0 /*cpr*/ *
76 constants::GetValues().drivetrain_encoder_ratio *
77 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
78}
79
Sabina Davis415bb6c2017-10-16 23:30:52 -070080float hall_translate(const constants::DualHallShifterHallEffect &k, float in_low,
Brian Silverman552350b2015-08-02 18:23:34 -070081 float in_high) {
82 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070083 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
84 static_cast<float>(k.low_gear_middle - k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070085 const float high_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070086 0.5 +
87 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
88 static_cast<float>(k.shifter_hall_effect.high_gear_high -
89 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070090
Brian Silverman552350b2015-08-02 18:23:34 -070091 // Return low when we are below 1/2, and high when we are above 1/2.
92 if (low_ratio + high_ratio < 1.0) {
93 return low_ratio;
94 } else {
95 return high_ratio;
96 }
Brian Silverman17f503e2015-08-02 18:17:18 -070097}
98
99double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700100 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700101 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700102 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700103}
104
Brian Silverman552350b2015-08-02 18:23:34 -0700105double shooter_translate(int32_t in) {
106 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
107 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
108 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700109}
110
111static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700112 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800113 18.0 / 32.0 /* big belt reduction */ * 18.0 /
114 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
115 60.0 /* seconds / minute */ * 256.0 /* CPR */ * 4.0 /* edges / pulse */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700116
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800117class SensorReader : public ::frc971::wpilib::SensorReader {
Brian Silverman17f503e2015-08-02 18:17:18 -0700118 public:
Brian Silverman39b339e2016-01-03 13:24:22 -0800119 SensorReader() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700120 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
121 // we should ever see.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800122 UpdateMediumEncoderFilterHz(kMaximumEncoderPulsesPerSecond);
Brian Silverman552350b2015-08-02 18:23:34 -0700123 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700124 }
125
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800126 ~SensorReader() override {
127 top_reader_.Quit();
128 bottom_reader_.Quit();
129 }
130
Brian Silverman552350b2015-08-02 18:23:34 -0700131 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
132 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700133 }
134
Brian Silverman552350b2015-08-02 18:23:34 -0700135 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
136 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 }
138
Brian Silverman552350b2015-08-02 18:23:34 -0700139 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
140 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700141 }
142
Brian Silverman552350b2015-08-02 18:23:34 -0700143 void set_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
144 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700145 }
146
Brian Silverman552350b2015-08-02 18:23:34 -0700147 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
148 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700149 }
150
Brian Silverman552350b2015-08-02 18:23:34 -0700151 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800152 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700153 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 }
155
Austin Schuhc5e36082015-10-31 13:30:46 -0700156 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700157 hall_filter_.Add(hall.get());
158 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700159 }
160
Austin Schuhc5e36082015-10-31 13:30:46 -0700161 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700162 hall_filter_.Add(hall.get());
163 top_reader_.set_calibration_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_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700167 hall_filter_.Add(hall.get());
168 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700169 }
170
Brian Silverman552350b2015-08-02 18:23:34 -0700171 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800172 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700173 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700174 }
175
Austin Schuhc5e36082015-10-31 13:30:46 -0700176 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700177 hall_filter_.Add(hall.get());
178 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700179 }
180
Austin Schuhc5e36082015-10-31 13:30:46 -0700181 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700182 hall_filter_.Add(hall.get());
183 bottom_reader_.set_calibration_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_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700187 hall_filter_.Add(hall.get());
188 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700189 }
190
Brian Silverman552350b2015-08-02 18:23:34 -0700191 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800192 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700193 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700194 }
195
Austin Schuhc5e36082015-10-31 13:30:46 -0700196 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700197 hall_filter_.Add(hall.get());
198 shooter_proximal_ = ::std::move(hall);
199 }
200
Austin Schuhc5e36082015-10-31 13:30:46 -0700201 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700202 hall_filter_.Add(hall.get());
203 shooter_distal_ = ::std::move(hall);
204 }
205
Austin Schuhc5e36082015-10-31 13:30:46 -0700206 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700207 hall_filter_.Add(hall.get());
208 shooter_plunger_ = ::std::move(hall);
209 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500210 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700211 }
212
Austin Schuhc5e36082015-10-31 13:30:46 -0700213 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700214 hall_filter_.Add(hall.get());
215 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500216 shooter_latch_reader_ =
217 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700218 }
219
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800220 void Start() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500221 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700222 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500223 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700224 shooter_encoder_.get(), shooter_distal_.get());
225
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800226 AddToDMA(shooter_proximal_counter_.get());
227 AddToDMA(shooter_distal_counter_.get());
228 AddToDMA(shooter_plunger_reader_.get());
229 AddToDMA(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700230
Brian Silverman552350b2015-08-02 18:23:34 -0700231 top_reader_.Start();
232 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700233 }
234
235 void RunIteration() {
Brian Silverman552350b2015-08-02 18:23:34 -0700236 const auto &values = constants::GetValues();
237
Brian Silverman17f503e2015-08-02 18:17:18 -0700238 {
239 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
240 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700241 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700242 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700243 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800244 drivetrain_message->left_speed =
245 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
246 drivetrain_message->right_speed =
247 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800248
249 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
250 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700251 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800252 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
253 drivetrain_message->high_left_hall);
254
255 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
256 drivetrain_message->high_right_hall =
257 high_right_drive_hall_->GetVoltage();
258 drivetrain_message->right_shifter_position =
259 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
260 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700261
262 drivetrain_message.Send();
263 }
264
Brian Silvermanb601d892015-12-20 18:24:38 -0500265 ::y2014::sensors::auto_mode.MakeWithBuilder()
Brian Silverman552350b2015-08-02 18:23:34 -0700266 .voltage(auto_selector_analog_->GetVoltage())
267 .Send();
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800268 }
Brian Silverman552350b2015-08-02 18:23:34 -0700269
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800270 void RunDmaIteration() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700271 {
Brian Silverman552350b2015-08-02 18:23:34 -0700272 auto shooter_message = shooter_queue.position.MakeMessage();
273 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800274 shooter_message->plunger = !shooter_plunger_reader_->value();
275 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700276 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
277 &shooter_message->pusher_proximal);
278 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
279 &shooter_message->pusher_distal);
280
281 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700282 }
283
284 {
285 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700286 top_reader_.RunIteration(&claw_message->top);
287 bottom_reader_.RunIteration(&claw_message->bottom);
288
Brian Silverman17f503e2015-08-02 18:17:18 -0700289 claw_message.Send();
290 }
291 }
292
Brian Silverman17f503e2015-08-02 18:17:18 -0700293 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700294 class HalfClawReader {
295 public:
296 HalfClawReader(bool reversed) : reversed_(reversed) {}
297
298 void set_encoder(::std::unique_ptr<Encoder> encoder) {
299 encoder_ = ::std::move(encoder);
300 }
301
Austin Schuhc5e36082015-10-31 13:30:46 -0700302 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700303 front_hall_ = ::std::move(front_hall);
304 }
305
306 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700307 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700308 calibration_hall_ = ::std::move(calibration_hall);
309 }
310
Austin Schuhc5e36082015-10-31 13:30:46 -0700311 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700312 back_hall_ = ::std::move(back_hall);
313 }
314
315 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500316 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
317 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700318 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500319 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
320 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700321 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500322 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
323 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700324 synchronizer_.Add(back_counter_.get());
325 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500326 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
327 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700328 synchronizer_.Add(synchronized_encoder_.get());
329
330 synchronizer_.Start();
331 }
332
333 void Quit() { synchronizer_.Quit(); }
334
335 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
336 const double multiplier = reversed_ ? -1.0 : 1.0;
337
338 synchronizer_.RunIteration();
339
340 CopyPosition(front_counter_.get(), &half_claw_position->front);
341 CopyPosition(calibration_counter_.get(),
342 &half_claw_position->calibration);
343 CopyPosition(back_counter_.get(), &half_claw_position->back);
344 half_claw_position->position =
345 multiplier * claw_translate(synchronized_encoder_->get());
346 }
347
348 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500349 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
350 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700351 const double multiplier = reversed_ ? -1.0 : 1.0;
352
Austin Schuh5c25ab72015-11-01 13:17:11 -0800353 out->current = !counter->polled_value();
354 out->posedge_count = counter->negative_interrupt_count();
355 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700356 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800357 multiplier * claw_translate(counter->last_positive_encoder_value());
358 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700359 multiplier * claw_translate(counter->last_negative_encoder_value());
360 }
361
Brian Silverman5090c432016-01-02 14:44:26 -0800362 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700363
Brian Silvermanb601d892015-12-20 18:24:38 -0500364 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
365 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
366 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
367 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
368 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700369
370 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700371 ::std::unique_ptr<DigitalInput> front_hall_;
372 ::std::unique_ptr<DigitalInput> calibration_hall_;
373 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700374
375 const bool reversed_;
376 };
377
Brian Silvermanb601d892015-12-20 18:24:38 -0500378 void CopyShooterPosedgeCounts(
379 const ::frc971::wpilib::DMAEdgeCounter *counter,
380 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800381 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700382 // These are inverted because the hall effects give logical false when
383 // there's a magnet in front of them.
384 output->posedge_count = counter->negative_count();
385 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700386 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700387 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700388 }
389
Brian Silverman552350b2015-08-02 18:23:34 -0700390 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700391
Brian Silverman552350b2015-08-02 18:23:34 -0700392 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
393 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
394 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
395 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700396
Brian Silverman552350b2015-08-02 18:23:34 -0700397 HalfClawReader top_reader_{false}, bottom_reader_{true};
398
399 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700400 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
401 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500402 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700403 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500404 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700405 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700406
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800407 DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700408};
409
410class SolenoidWriter {
411 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500412 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700413 : pcm_(pcm),
Brian Silvermanc75c1ad2015-12-30 16:21:13 -0800414 shooter_(".y2014.control_loops.shooter_queue.output"),
Comran Morshed5323ecb2015-12-26 20:50:55 +0000415 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700416
Austin Schuhc5e36082015-10-31 13:30:46 -0700417 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700418 pressure_switch_ = ::std::move(pressure_switch);
419 }
420
421 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
422 compressor_relay_ = ::std::move(compressor_relay);
423 }
424
Brian Silvermanb601d892015-12-20 18:24:38 -0500425 void set_drivetrain_left(
426 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700427 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700428 }
429
Brian Silvermanb601d892015-12-20 18:24:38 -0500430 void set_drivetrain_right(
431 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700432 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700433 }
434
Brian Silvermanb601d892015-12-20 18:24:38 -0500435 void set_shooter_latch(
436 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700437 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700438 }
439
Brian Silvermanb601d892015-12-20 18:24:38 -0500440 void set_shooter_brake(
441 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700442 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700443 }
444
445 void operator()() {
446 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800447 ::aos::SetCurrentThreadRealtimePriority(27);
448
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700449 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
450 ::std::chrono::milliseconds(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700451
452 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800453 {
454 const int iterations = phased_loop.SleepUntilNext();
455 if (iterations != 1) {
456 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
457 }
458 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700459
460 {
Brian Silverman552350b2015-08-02 18:23:34 -0700461 shooter_.FetchLatest();
462 if (shooter_.get()) {
463 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
464 shooter_latch_->Set(!shooter_->latch_piston);
465 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700466 }
467 }
468
469 {
Brian Silverman552350b2015-08-02 18:23:34 -0700470 drivetrain_.FetchLatest();
471 if (drivetrain_.get()) {
472 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800473 drivetrain_left_->Set(!drivetrain_->left_high);
474 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700475 }
476 }
477
Brian Silverman17f503e2015-08-02 18:17:18 -0700478 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500479 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700480 {
481 const bool compressor_on = !pressure_switch_->Get();
482 to_log.compressor_on = compressor_on;
483 if (compressor_on) {
484 compressor_relay_->Set(Relay::kForward);
485 } else {
486 compressor_relay_->Set(Relay::kOff);
487 }
488 }
489
490 pcm_->Flush();
491 to_log.read_solenoids = pcm_->GetAll();
492 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
493 }
494 }
495 }
496
497 void Quit() { run_ = false; }
498
499 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500500 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700501
Brian Silvermanb601d892015-12-20 18:24:38 -0500502 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
503 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
504 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
505 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700506
Austin Schuhc5e36082015-10-31 13:30:46 -0700507 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700508 ::std::unique_ptr<Relay> compressor_relay_;
509
Brian Silvermanb601d892015-12-20 18:24:38 -0500510 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000511 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700512
513 ::std::atomic<bool> run_{true};
514};
515
Brian Silvermanb601d892015-12-20 18:24:38 -0500516class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700517 public:
518 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
519 left_drivetrain_talon_ = ::std::move(t);
520 }
521
522 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
523 right_drivetrain_talon_ = ::std::move(t);
524 }
525
526 private:
527 virtual void Read() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000528 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700529 }
530
531 virtual void Write() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000532 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700533 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800534 left_drivetrain_talon_->SetSpeed(-queue->left_voltage / 12.0);
535 right_drivetrain_talon_->SetSpeed(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700536 }
537
538 virtual void Stop() override {
539 LOG(WARNING, "drivetrain output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800540 left_drivetrain_talon_->SetDisabled();
541 right_drivetrain_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700542 }
543
544 ::std::unique_ptr<Talon> left_drivetrain_talon_;
545 ::std::unique_ptr<Talon> right_drivetrain_talon_;
546};
547
Brian Silvermanb601d892015-12-20 18:24:38 -0500548class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700549 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700550 void set_shooter_talon(::std::unique_ptr<Talon> t) {
551 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700552 }
553
554 private:
555 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500556 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700557 }
558
559 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500560 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700561 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800562 shooter_talon_->SetSpeed(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700563 }
564
565 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700566 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800567 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700568 }
569
Brian Silverman552350b2015-08-02 18:23:34 -0700570 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700571};
572
Brian Silvermanb601d892015-12-20 18:24:38 -0500573class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700574 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700575 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
576 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700577 }
578
Brian Silverman552350b2015-08-02 18:23:34 -0700579 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
580 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700581 }
582
Brian Silverman552350b2015-08-02 18:23:34 -0700583 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
584 left_tusk_talon_ = ::std::move(t);
585 }
586
587 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
588 right_tusk_talon_ = ::std::move(t);
589 }
590
591 void set_intake1_talon(::std::unique_ptr<Talon> t) {
592 intake1_talon_ = ::std::move(t);
593 }
594
595 void set_intake2_talon(::std::unique_ptr<Talon> t) {
596 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700597 }
598
599 private:
600 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500601 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700602 }
603
604 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500605 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700606 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800607 intake1_talon_->SetSpeed(queue->intake_voltage / 12.0);
608 intake2_talon_->SetSpeed(queue->intake_voltage / 12.0);
609 bottom_claw_talon_->SetSpeed(-queue->bottom_claw_voltage / 12.0);
610 top_claw_talon_->SetSpeed(queue->top_claw_voltage / 12.0);
611 left_tusk_talon_->SetSpeed(queue->tusk_voltage / 12.0);
612 right_tusk_talon_->SetSpeed(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700613 }
614
615 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700616 LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800617 intake1_talon_->SetDisabled();
618 intake2_talon_->SetDisabled();
619 bottom_claw_talon_->SetDisabled();
620 top_claw_talon_->SetDisabled();
621 left_tusk_talon_->SetDisabled();
622 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700623 }
624
Brian Silverman552350b2015-08-02 18:23:34 -0700625 ::std::unique_ptr<Talon> top_claw_talon_;
626 ::std::unique_ptr<Talon> bottom_claw_talon_;
627 ::std::unique_ptr<Talon> left_tusk_talon_;
628 ::std::unique_ptr<Talon> right_tusk_talon_;
629 ::std::unique_ptr<Talon> intake1_talon_;
630 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700631};
632
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800633class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700634 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700635 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700636 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
637 Encoder::k4X);
638 }
Brian Silverman552350b2015-08-02 18:23:34 -0700639
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800640 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700641 ::aos::InitNRT();
642 ::aos::SetCurrentThreadName("StartCompetition");
643
Brian Silvermanb601d892015-12-20 18:24:38 -0500644 ::frc971::wpilib::JoystickSender joystick_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700645 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700646
Brian Silverman425492b2015-12-30 15:23:55 -0800647 ::frc971::wpilib::PDPFetcher pdp_fetcher;
648 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Brian Silverman39b339e2016-01-03 13:24:22 -0800649 SensorReader reader;
Brian Silverman17f503e2015-08-02 18:17:18 -0700650
Brian Silverman85fbb602015-08-29 19:28:20 -0700651 // Create this first to make sure it ends up in one of the lower-numbered
652 // FPGA slots so we can use it with DMA.
653 auto shooter_encoder_temp = make_encoder(2);
654
Brian Silverman552350b2015-08-02 18:23:34 -0700655 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700656
Brian Silverman552350b2015-08-02 18:23:34 -0700657 reader.set_drivetrain_left_encoder(make_encoder(0));
658 reader.set_drivetrain_right_encoder(make_encoder(1));
659 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
660 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
661 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
662 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700663
Brian Silverman552350b2015-08-02 18:23:34 -0700664 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800665 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
666 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
667 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700668
Brian Silverman85fbb602015-08-29 19:28:20 -0700669 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800670 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
671 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
672 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700673
Brian Silverman85fbb602015-08-29 19:28:20 -0700674 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800675 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
676 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
677 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
678 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700679
Brian Silverman17f503e2015-08-02 18:17:18 -0700680 reader.set_dma(make_unique<DMA>());
681 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700682
Brian Silvermanb601d892015-12-20 18:24:38 -0500683 ::frc971::wpilib::GyroSender gyro_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700684 ::std::thread gyro_thread(::std::ref(gyro_sender));
685
686 DrivetrainWriter drivetrain_writer;
687 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700688 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700689 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700690 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700691 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
692
Brian Silvermanb601d892015-12-20 18:24:38 -0500693 ::y2014::wpilib::ClawWriter claw_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700694 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
695 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
696 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
697 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
698 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
699 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700700 ::std::thread claw_writer_thread(::std::ref(claw_writer));
701
Brian Silvermanb601d892015-12-20 18:24:38 -0500702 ::y2014::wpilib::ShooterWriter shooter_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700703 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
704 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
705
Brian Silverman17f503e2015-08-02 18:17:18 -0700706 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
707 new ::frc971::wpilib::BufferedPcm());
708 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700709 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
710 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
711 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
712 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700713
Austin Schuh016a6b02015-10-08 06:41:14 +0000714 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700715 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
716 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
717
718 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800719 while (true) {
720 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
721 if (r != 0) {
722 PLOG(WARNING, "infinite select failed");
723 } else {
724 PLOG(WARNING, "infinite select succeeded??\n");
725 }
726 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700727
728 LOG(ERROR, "Exiting WPILibRobot\n");
729
730 joystick_sender.Quit();
731 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800732 pdp_fetcher.Quit();
733 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700734 reader.Quit();
735 reader_thread.join();
736 gyro_sender.Quit();
737 gyro_thread.join();
738
739 drivetrain_writer.Quit();
740 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700741 shooter_writer.Quit();
742 shooter_writer_thread.join();
743 claw_writer.Quit();
744 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700745 solenoid_writer.Quit();
746 solenoid_thread.join();
747
748 ::aos::Cleanup();
749 }
750};
751
752} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500753} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700754
755
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800756AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);