blob: af2801272f4fafd6b44d8ee022b9ad2910f69b05 [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"
Brian Silverman17f503e2015-08-02 18:17:18 -070031
Brian Silverman552350b2015-08-02 18:23:34 -070032#include "frc971/shifter_hall_effect.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050033
Comran Morshed5323ecb2015-12-26 20:50:55 +000034#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070035#include "y2014/control_loops/claw/claw.q.h"
Brian Silverman552350b2015-08-02 18:23:34 -070036#include "y2014/control_loops/shooter/shooter.q.h"
37#include "y2014/constants.h"
38#include "y2014/queues/auto_mode.q.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070039
Brian Silverman17f503e2015-08-02 18:17:18 -070040#include "frc971/wpilib/joystick_sender.h"
41#include "frc971/wpilib/loop_output_handler.h"
42#include "frc971/wpilib/buffered_solenoid.h"
43#include "frc971/wpilib/buffered_pcm.h"
44#include "frc971/wpilib/gyro_sender.h"
45#include "frc971/wpilib/dma_edge_counting.h"
46#include "frc971/wpilib/interrupt_edge_counting.h"
47#include "frc971/wpilib/encoder_and_potentiometer.h"
48#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050049#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman425492b2015-12-30 15:23:55 -080050#include "frc971/wpilib/pdp_fetcher.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050051#include "frc971/wpilib/dma.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070052
Brian Silverman17f503e2015-08-02 18:17:18 -070053#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
Comran Morshed5323ecb2015-12-26 20:50:55 +000057using ::frc971::control_loops::drivetrain_queue;
Brian Silvermanb601d892015-12-20 18:24:38 -050058using ::y2014::control_loops::claw_queue;
59using ::y2014::control_loops::shooter_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080060using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080061using aos::make_unique;
Brian Silverman17f503e2015-08-02 18:17:18 -070062
Brian Silvermanb601d892015-12-20 18:24:38 -050063namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070064namespace wpilib {
65
Brian Silverman85fbb602015-08-29 19:28:20 -070066// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
67// DMA stuff and then removing the * 2.0 in *_translate.
68// The low bit is direction.
69
Brian Silverman17f503e2015-08-02 18:17:18 -070070double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070071 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070072 (256.0 /*cpr*/ * 4.0 /*4x*/) *
73 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080074 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070075}
76
Brian Silverman51091a02015-12-26 15:56:58 -080077double drivetrain_velocity_translate(double in) {
78 return (1.0 / in) / 256.0 /*cpr*/ *
79 constants::GetValues().drivetrain_encoder_ratio *
80 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
81}
82
Sabina Davis415bb6c2017-10-16 23:30:52 -070083float hall_translate(const constants::DualHallShifterHallEffect &k, float in_low,
Brian Silverman552350b2015-08-02 18:23:34 -070084 float in_high) {
85 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070086 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
87 static_cast<float>(k.low_gear_middle - k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070088 const float high_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070089 0.5 +
90 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
91 static_cast<float>(k.shifter_hall_effect.high_gear_high -
92 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070093
Brian Silverman552350b2015-08-02 18:23:34 -070094 // Return low when we are below 1/2, and high when we are above 1/2.
95 if (low_ratio + high_ratio < 1.0) {
96 return low_ratio;
97 } else {
98 return high_ratio;
99 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700100}
101
102double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700103 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700104 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700105 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700106}
107
Brian Silverman552350b2015-08-02 18:23:34 -0700108double shooter_translate(int32_t in) {
109 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
110 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
111 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700112}
113
114static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700115 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
116 18.0 / 32.0 /* big belt reduction */ *
117 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
118 60.0 /* seconds / minute */ * 256.0 /* CPR */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700119
120class SensorReader {
121 public:
Brian Silverman39b339e2016-01-03 13:24:22 -0800122 SensorReader() {
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.
Brian Silverman552350b2015-08-02 18:23:34 -0700125 encoder_filter_.SetPeriodNanoSeconds(
Brian Silverman17f503e2015-08-02 18:17:18 -0700126 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
Brian Silverman552350b2015-08-02 18:23:34 -0700127 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 }
129
Brian Silverman552350b2015-08-02 18:23:34 -0700130 void set_auto_selector_analog(::std::unique_ptr<AnalogInput> analog) {
131 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Brian Silverman552350b2015-08-02 18:23:34 -0700134 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
135 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800136 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700137 }
138
Brian Silverman552350b2015-08-02 18:23:34 -0700139 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
140 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800141 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Brian Silverman17f503e2015-08-02 18:17:18 -0700142 }
143
Brian Silverman552350b2015-08-02 18:23:34 -0700144 void set_high_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
145 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700146 }
147
Brian Silverman552350b2015-08-02 18:23:34 -0700148 void set_low_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
149 low_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_high_right_drive_hall(::std::unique_ptr<AnalogInput> analog) {
153 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700154 }
155
Brian Silverman552350b2015-08-02 18:23:34 -0700156 void set_low_left_drive_hall(::std::unique_ptr<AnalogInput> analog) {
157 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700158 }
159
Brian Silverman552350b2015-08-02 18:23:34 -0700160 void set_top_claw_encoder(::std::unique_ptr<Encoder> encoder) {
161 encoder_filter_.Add(encoder.get());
162 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700163 }
164
Austin Schuhc5e36082015-10-31 13:30:46 -0700165 void set_top_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700166 hall_filter_.Add(hall.get());
167 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700168 }
169
Austin Schuhc5e36082015-10-31 13:30:46 -0700170 void set_top_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700171 hall_filter_.Add(hall.get());
172 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700173 }
174
Austin Schuhc5e36082015-10-31 13:30:46 -0700175 void set_top_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700176 hall_filter_.Add(hall.get());
177 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700178 }
179
Brian Silverman552350b2015-08-02 18:23:34 -0700180 void set_bottom_claw_encoder(::std::unique_ptr<Encoder> encoder) {
181 encoder_filter_.Add(encoder.get());
182 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700183 }
184
Austin Schuhc5e36082015-10-31 13:30:46 -0700185 void set_bottom_claw_front_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700186 hall_filter_.Add(hall.get());
187 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 }
189
Austin Schuhc5e36082015-10-31 13:30:46 -0700190 void set_bottom_claw_calibration_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700191 hall_filter_.Add(hall.get());
192 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700193 }
194
Austin Schuhc5e36082015-10-31 13:30:46 -0700195 void set_bottom_claw_back_hall(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700196 hall_filter_.Add(hall.get());
197 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700198 }
199
Brian Silverman552350b2015-08-02 18:23:34 -0700200 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
201 encoder_filter_.Add(encoder.get());
202 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700203 }
204
Austin Schuhc5e36082015-10-31 13:30:46 -0700205 void set_shooter_proximal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700206 hall_filter_.Add(hall.get());
207 shooter_proximal_ = ::std::move(hall);
208 }
209
Austin Schuhc5e36082015-10-31 13:30:46 -0700210 void set_shooter_distal(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700211 hall_filter_.Add(hall.get());
212 shooter_distal_ = ::std::move(hall);
213 }
214
Austin Schuhc5e36082015-10-31 13:30:46 -0700215 void set_shooter_plunger(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700216 hall_filter_.Add(hall.get());
217 shooter_plunger_ = ::std::move(hall);
218 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500219 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700220 }
221
Austin Schuhc5e36082015-10-31 13:30:46 -0700222 void set_shooter_latch(::std::unique_ptr<DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700223 hall_filter_.Add(hall.get());
224 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500225 shooter_latch_reader_ =
226 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700227 }
228
229 // All of the DMA-related set_* calls must be made before this, and it doesn't
230 // hurt to do all of them.
231 void set_dma(::std::unique_ptr<DMA> dma) {
Brian Silvermanb601d892015-12-20 18:24:38 -0500232 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700233 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500234 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700235 shooter_encoder_.get(), shooter_distal_.get());
236
Brian Silvermanb601d892015-12-20 18:24:38 -0500237 dma_synchronizer_.reset(
238 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brian Silverman552350b2015-08-02 18:23:34 -0700239 dma_synchronizer_->Add(shooter_proximal_counter_.get());
240 dma_synchronizer_->Add(shooter_distal_counter_.get());
241 dma_synchronizer_->Add(shooter_plunger_reader_.get());
242 dma_synchronizer_->Add(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700243 }
244
245 void operator()() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700246 ::aos::SetCurrentThreadName("SensorReader");
247
248 my_pid_ = getpid();
Brian Silverman17f503e2015-08-02 18:17:18 -0700249
Brian Silverman552350b2015-08-02 18:23:34 -0700250 top_reader_.Start();
251 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700252 dma_synchronizer_->Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700253
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700254 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
255 ::std::chrono::milliseconds(4));
Brian Silvermane7611a02015-12-30 16:20:33 -0800256
Brian Silverman5090c432016-01-02 14:44:26 -0800257 ::aos::SetCurrentThreadRealtimePriority(40);
Brian Silverman17f503e2015-08-02 18:17:18 -0700258 while (run_) {
Brian Silvermane7611a02015-12-30 16:20:33 -0800259 {
260 const int iterations = phased_loop.SleepUntilNext();
261 if (iterations != 1) {
262 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
263 }
264 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700265 RunIteration();
266 }
267
Brian Silverman552350b2015-08-02 18:23:34 -0700268 top_reader_.Quit();
269 bottom_reader_.Quit();
Brian Silverman17f503e2015-08-02 18:17:18 -0700270 }
271
272 void RunIteration() {
Austin Schuh94f51e92017-10-30 19:25:32 -0700273 ::frc971::wpilib::SendRobotState(my_pid_);
Brian Silverman17f503e2015-08-02 18:17:18 -0700274
Brian Silverman552350b2015-08-02 18:23:34 -0700275 const auto &values = constants::GetValues();
276
Brian Silverman17f503e2015-08-02 18:17:18 -0700277 {
278 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
279 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700280 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700281 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700282 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800283 drivetrain_message->left_speed =
284 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
285 drivetrain_message->right_speed =
286 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800287
288 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
289 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700290 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800291 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
292 drivetrain_message->high_left_hall);
293
294 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
295 drivetrain_message->high_right_hall =
296 high_right_drive_hall_->GetVoltage();
297 drivetrain_message->right_shifter_position =
298 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
299 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700300
301 drivetrain_message.Send();
302 }
303
Brian Silvermanb601d892015-12-20 18:24:38 -0500304 ::y2014::sensors::auto_mode.MakeWithBuilder()
Brian Silverman552350b2015-08-02 18:23:34 -0700305 .voltage(auto_selector_analog_->GetVoltage())
306 .Send();
307
Brian Silverman17f503e2015-08-02 18:17:18 -0700308 dma_synchronizer_->RunIteration();
309
Brian Silverman17f503e2015-08-02 18:17:18 -0700310 {
Brian Silverman552350b2015-08-02 18:23:34 -0700311 auto shooter_message = shooter_queue.position.MakeMessage();
312 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800313 shooter_message->plunger = !shooter_plunger_reader_->value();
314 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700315 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
316 &shooter_message->pusher_proximal);
317 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
318 &shooter_message->pusher_distal);
319
320 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700321 }
322
323 {
324 auto claw_message = claw_queue.position.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700325 top_reader_.RunIteration(&claw_message->top);
326 bottom_reader_.RunIteration(&claw_message->bottom);
327
Brian Silverman17f503e2015-08-02 18:17:18 -0700328 claw_message.Send();
329 }
330 }
331
332 void Quit() { run_ = false; }
333
334 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700335 class HalfClawReader {
336 public:
337 HalfClawReader(bool reversed) : reversed_(reversed) {}
338
339 void set_encoder(::std::unique_ptr<Encoder> encoder) {
340 encoder_ = ::std::move(encoder);
341 }
342
Austin Schuhc5e36082015-10-31 13:30:46 -0700343 void set_front_hall(::std::unique_ptr<DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700344 front_hall_ = ::std::move(front_hall);
345 }
346
347 void set_calibration_hall(
Austin Schuhc5e36082015-10-31 13:30:46 -0700348 ::std::unique_ptr<DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700349 calibration_hall_ = ::std::move(calibration_hall);
350 }
351
Austin Schuhc5e36082015-10-31 13:30:46 -0700352 void set_back_hall(::std::unique_ptr<DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700353 back_hall_ = ::std::move(back_hall);
354 }
355
356 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500357 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
358 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700359 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500360 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
361 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700362 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500363 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
364 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700365 synchronizer_.Add(back_counter_.get());
366 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500367 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
368 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700369 synchronizer_.Add(synchronized_encoder_.get());
370
371 synchronizer_.Start();
372 }
373
374 void Quit() { synchronizer_.Quit(); }
375
376 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
377 const double multiplier = reversed_ ? -1.0 : 1.0;
378
379 synchronizer_.RunIteration();
380
381 CopyPosition(front_counter_.get(), &half_claw_position->front);
382 CopyPosition(calibration_counter_.get(),
383 &half_claw_position->calibration);
384 CopyPosition(back_counter_.get(), &half_claw_position->back);
385 half_claw_position->position =
386 multiplier * claw_translate(synchronized_encoder_->get());
387 }
388
389 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500390 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
391 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700392 const double multiplier = reversed_ ? -1.0 : 1.0;
393
Austin Schuh5c25ab72015-11-01 13:17:11 -0800394 out->current = !counter->polled_value();
395 out->posedge_count = counter->negative_interrupt_count();
396 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700397 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800398 multiplier * claw_translate(counter->last_positive_encoder_value());
399 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700400 multiplier * claw_translate(counter->last_negative_encoder_value());
401 }
402
Brian Silverman5090c432016-01-02 14:44:26 -0800403 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700404
Brian Silvermanb601d892015-12-20 18:24:38 -0500405 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
406 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
407 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
408 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
409 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700410
411 ::std::unique_ptr<Encoder> encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700412 ::std::unique_ptr<DigitalInput> front_hall_;
413 ::std::unique_ptr<DigitalInput> calibration_hall_;
414 ::std::unique_ptr<DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700415
416 const bool reversed_;
417 };
418
Brian Silvermanb601d892015-12-20 18:24:38 -0500419 void CopyShooterPosedgeCounts(
420 const ::frc971::wpilib::DMAEdgeCounter *counter,
421 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800422 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700423 // These are inverted because the hall effects give logical false when
424 // there's a magnet in front of them.
425 output->posedge_count = counter->negative_count();
426 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700427 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700428 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700429 }
430
Brian Silverman17f503e2015-08-02 18:17:18 -0700431 int32_t my_pid_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700432
Brian Silvermanb601d892015-12-20 18:24:38 -0500433 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700434
Brian Silverman552350b2015-08-02 18:23:34 -0700435 ::std::unique_ptr<AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700436
Brian Silverman552350b2015-08-02 18:23:34 -0700437 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
438 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
439 ::std::unique_ptr<AnalogInput> low_left_drive_hall_;
440 ::std::unique_ptr<AnalogInput> high_left_drive_hall_;
441 ::std::unique_ptr<AnalogInput> low_right_drive_hall_;
442 ::std::unique_ptr<AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700443
Brian Silverman552350b2015-08-02 18:23:34 -0700444 HalfClawReader top_reader_{false}, bottom_reader_{true};
445
446 ::std::unique_ptr<Encoder> shooter_encoder_;
Austin Schuhc5e36082015-10-31 13:30:46 -0700447 ::std::unique_ptr<DigitalInput> shooter_proximal_, shooter_distal_;
448 ::std::unique_ptr<DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500449 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700450 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500451 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700452 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700453
454 ::std::atomic<bool> run_{true};
Brian Silverman552350b2015-08-02 18:23:34 -0700455 DigitalGlitchFilter encoder_filter_, hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700456};
457
458class SolenoidWriter {
459 public:
Brian Silvermanb601d892015-12-20 18:24:38 -0500460 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700461 : pcm_(pcm),
Brian Silvermanc75c1ad2015-12-30 16:21:13 -0800462 shooter_(".y2014.control_loops.shooter_queue.output"),
Comran Morshed5323ecb2015-12-26 20:50:55 +0000463 drivetrain_(".frc971.control_loops.drivetrain_queue.output") {}
Brian Silverman17f503e2015-08-02 18:17:18 -0700464
Austin Schuhc5e36082015-10-31 13:30:46 -0700465 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700466 pressure_switch_ = ::std::move(pressure_switch);
467 }
468
469 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
470 compressor_relay_ = ::std::move(compressor_relay);
471 }
472
Brian Silvermanb601d892015-12-20 18:24:38 -0500473 void set_drivetrain_left(
474 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700475 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700476 }
477
Brian Silvermanb601d892015-12-20 18:24:38 -0500478 void set_drivetrain_right(
479 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700480 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700481 }
482
Brian Silvermanb601d892015-12-20 18:24:38 -0500483 void set_shooter_latch(
484 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700485 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700486 }
487
Brian Silvermanb601d892015-12-20 18:24:38 -0500488 void set_shooter_brake(
489 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700490 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700491 }
492
493 void operator()() {
494 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800495 ::aos::SetCurrentThreadRealtimePriority(27);
496
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700497 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
498 ::std::chrono::milliseconds(1));
Brian Silverman17f503e2015-08-02 18:17:18 -0700499
500 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800501 {
502 const int iterations = phased_loop.SleepUntilNext();
503 if (iterations != 1) {
504 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
505 }
506 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700507
508 {
Brian Silverman552350b2015-08-02 18:23:34 -0700509 shooter_.FetchLatest();
510 if (shooter_.get()) {
511 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
512 shooter_latch_->Set(!shooter_->latch_piston);
513 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700514 }
515 }
516
517 {
Brian Silverman552350b2015-08-02 18:23:34 -0700518 drivetrain_.FetchLatest();
519 if (drivetrain_.get()) {
520 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuh86f895e2015-11-08 13:40:51 -0800521 drivetrain_left_->Set(!drivetrain_->left_high);
522 drivetrain_right_->Set(!drivetrain_->right_high);
Brian Silverman17f503e2015-08-02 18:17:18 -0700523 }
524 }
525
Brian Silverman17f503e2015-08-02 18:17:18 -0700526 {
Brian Silvermanb601d892015-12-20 18:24:38 -0500527 ::frc971::wpilib::PneumaticsToLog to_log;
Brian Silverman17f503e2015-08-02 18:17:18 -0700528 {
529 const bool compressor_on = !pressure_switch_->Get();
530 to_log.compressor_on = compressor_on;
531 if (compressor_on) {
532 compressor_relay_->Set(Relay::kForward);
533 } else {
534 compressor_relay_->Set(Relay::kOff);
535 }
536 }
537
538 pcm_->Flush();
539 to_log.read_solenoids = pcm_->GetAll();
540 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
541 }
542 }
543 }
544
545 void Quit() { run_ = false; }
546
547 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500548 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700549
Brian Silvermanb601d892015-12-20 18:24:38 -0500550 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
551 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
552 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
553 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700554
Austin Schuhc5e36082015-10-31 13:30:46 -0700555 ::std::unique_ptr<DigitalInput> pressure_switch_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700556 ::std::unique_ptr<Relay> compressor_relay_;
557
Brian Silvermanb601d892015-12-20 18:24:38 -0500558 ::aos::Queue<::y2014::control_loops::ShooterQueue::Output> shooter_;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000559 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700560
561 ::std::atomic<bool> run_{true};
562};
563
Brian Silvermanb601d892015-12-20 18:24:38 -0500564class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700565 public:
566 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
567 left_drivetrain_talon_ = ::std::move(t);
568 }
569
570 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
571 right_drivetrain_talon_ = ::std::move(t);
572 }
573
574 private:
575 virtual void Read() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000576 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700577 }
578
579 virtual void Write() override {
Comran Morshed5323ecb2015-12-26 20:50:55 +0000580 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700581 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800582 left_drivetrain_talon_->SetSpeed(-queue->left_voltage / 12.0);
583 right_drivetrain_talon_->SetSpeed(queue->right_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700584 }
585
586 virtual void Stop() override {
587 LOG(WARNING, "drivetrain output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800588 left_drivetrain_talon_->SetDisabled();
589 right_drivetrain_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 }
591
592 ::std::unique_ptr<Talon> left_drivetrain_talon_;
593 ::std::unique_ptr<Talon> right_drivetrain_talon_;
594};
595
Brian Silvermanb601d892015-12-20 18:24:38 -0500596class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700597 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700598 void set_shooter_talon(::std::unique_ptr<Talon> t) {
599 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700600 }
601
602 private:
603 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500604 ::y2014::control_loops::shooter_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700605 }
606
607 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500608 auto &queue = ::y2014::control_loops::shooter_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700609 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800610 shooter_talon_->SetSpeed(queue->voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700611 }
612
613 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700614 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800615 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700616 }
617
Brian Silverman552350b2015-08-02 18:23:34 -0700618 ::std::unique_ptr<Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700619};
620
Brian Silvermanb601d892015-12-20 18:24:38 -0500621class ClawWriter : public ::frc971::wpilib::LoopOutputHandler {
Brian Silverman17f503e2015-08-02 18:17:18 -0700622 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700623 void set_top_claw_talon(::std::unique_ptr<Talon> t) {
624 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700625 }
626
Brian Silverman552350b2015-08-02 18:23:34 -0700627 void set_bottom_claw_talon(::std::unique_ptr<Talon> t) {
628 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700629 }
630
Brian Silverman552350b2015-08-02 18:23:34 -0700631 void set_left_tusk_talon(::std::unique_ptr<Talon> t) {
632 left_tusk_talon_ = ::std::move(t);
633 }
634
635 void set_right_tusk_talon(::std::unique_ptr<Talon> t) {
636 right_tusk_talon_ = ::std::move(t);
637 }
638
639 void set_intake1_talon(::std::unique_ptr<Talon> t) {
640 intake1_talon_ = ::std::move(t);
641 }
642
643 void set_intake2_talon(::std::unique_ptr<Talon> t) {
644 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700645 }
646
647 private:
648 virtual void Read() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500649 ::y2014::control_loops::claw_queue.output.FetchAnother();
Brian Silverman17f503e2015-08-02 18:17:18 -0700650 }
651
652 virtual void Write() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500653 auto &queue = ::y2014::control_loops::claw_queue.output;
Brian Silverman17f503e2015-08-02 18:17:18 -0700654 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800655 intake1_talon_->SetSpeed(queue->intake_voltage / 12.0);
656 intake2_talon_->SetSpeed(queue->intake_voltage / 12.0);
657 bottom_claw_talon_->SetSpeed(-queue->bottom_claw_voltage / 12.0);
658 top_claw_talon_->SetSpeed(queue->top_claw_voltage / 12.0);
659 left_tusk_talon_->SetSpeed(queue->tusk_voltage / 12.0);
660 right_tusk_talon_->SetSpeed(-queue->tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700661 }
662
663 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700664 LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800665 intake1_talon_->SetDisabled();
666 intake2_talon_->SetDisabled();
667 bottom_claw_talon_->SetDisabled();
668 top_claw_talon_->SetDisabled();
669 left_tusk_talon_->SetDisabled();
670 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700671 }
672
Brian Silverman552350b2015-08-02 18:23:34 -0700673 ::std::unique_ptr<Talon> top_claw_talon_;
674 ::std::unique_ptr<Talon> bottom_claw_talon_;
675 ::std::unique_ptr<Talon> left_tusk_talon_;
676 ::std::unique_ptr<Talon> right_tusk_talon_;
677 ::std::unique_ptr<Talon> intake1_talon_;
678 ::std::unique_ptr<Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700679};
680
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800681class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700682 public:
Brian Silverman552350b2015-08-02 18:23:34 -0700683 ::std::unique_ptr<Encoder> make_encoder(int index) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700684 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
685 Encoder::k4X);
686 }
Brian Silverman552350b2015-08-02 18:23:34 -0700687
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800688 void Run() override {
Brian Silverman17f503e2015-08-02 18:17:18 -0700689 ::aos::InitNRT();
690 ::aos::SetCurrentThreadName("StartCompetition");
691
Brian Silvermanb601d892015-12-20 18:24:38 -0500692 ::frc971::wpilib::JoystickSender joystick_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700693 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silverman17f503e2015-08-02 18:17:18 -0700694
Brian Silverman425492b2015-12-30 15:23:55 -0800695 ::frc971::wpilib::PDPFetcher pdp_fetcher;
696 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Brian Silverman39b339e2016-01-03 13:24:22 -0800697 SensorReader reader;
Brian Silverman17f503e2015-08-02 18:17:18 -0700698
Brian Silverman85fbb602015-08-29 19:28:20 -0700699 // Create this first to make sure it ends up in one of the lower-numbered
700 // FPGA slots so we can use it with DMA.
701 auto shooter_encoder_temp = make_encoder(2);
702
Brian Silverman552350b2015-08-02 18:23:34 -0700703 reader.set_auto_selector_analog(make_unique<AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700704
Brian Silverman552350b2015-08-02 18:23:34 -0700705 reader.set_drivetrain_left_encoder(make_encoder(0));
706 reader.set_drivetrain_right_encoder(make_encoder(1));
707 reader.set_high_left_drive_hall(make_unique<AnalogInput>(1));
708 reader.set_low_left_drive_hall(make_unique<AnalogInput>(0));
709 reader.set_high_right_drive_hall(make_unique<AnalogInput>(2));
710 reader.set_low_right_drive_hall(make_unique<AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700711
Brian Silverman552350b2015-08-02 18:23:34 -0700712 reader.set_top_claw_encoder(make_encoder(3));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800713 reader.set_top_claw_front_hall(make_unique<DigitalInput>(4)); // R2
714 reader.set_top_claw_calibration_hall(make_unique<DigitalInput>(3)); // R3
715 reader.set_top_claw_back_hall(make_unique<DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700716
Brian Silverman85fbb602015-08-29 19:28:20 -0700717 reader.set_bottom_claw_encoder(make_encoder(4));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800718 reader.set_bottom_claw_front_hall(make_unique<DigitalInput>(1)); // L2
719 reader.set_bottom_claw_calibration_hall(make_unique<DigitalInput>(0)); // L3
720 reader.set_bottom_claw_back_hall(make_unique<DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700721
Brian Silverman85fbb602015-08-29 19:28:20 -0700722 reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
Austin Schuh5c25ab72015-11-01 13:17:11 -0800723 reader.set_shooter_proximal(make_unique<DigitalInput>(6)); // S1
724 reader.set_shooter_distal(make_unique<DigitalInput>(7)); // S2
725 reader.set_shooter_plunger(make_unique<DigitalInput>(8)); // S3
726 reader.set_shooter_latch(make_unique<DigitalInput>(9)); // S4
Brian Silverman552350b2015-08-02 18:23:34 -0700727
Brian Silverman17f503e2015-08-02 18:17:18 -0700728 reader.set_dma(make_unique<DMA>());
729 ::std::thread reader_thread(::std::ref(reader));
Brian Silverman552350b2015-08-02 18:23:34 -0700730
Brian Silvermanb601d892015-12-20 18:24:38 -0500731 ::frc971::wpilib::GyroSender gyro_sender;
Brian Silverman17f503e2015-08-02 18:17:18 -0700732 ::std::thread gyro_thread(::std::ref(gyro_sender));
733
734 DrivetrainWriter drivetrain_writer;
735 drivetrain_writer.set_left_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700736 ::std::unique_ptr<Talon>(new Talon(5)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700737 drivetrain_writer.set_right_drivetrain_talon(
Brian Silverman552350b2015-08-02 18:23:34 -0700738 ::std::unique_ptr<Talon>(new Talon(2)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700739 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
740
Brian Silvermanb601d892015-12-20 18:24:38 -0500741 ::y2014::wpilib::ClawWriter claw_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700742 claw_writer.set_top_claw_talon(::std::unique_ptr<Talon>(new Talon(1)));
743 claw_writer.set_bottom_claw_talon(::std::unique_ptr<Talon>(new Talon(0)));
744 claw_writer.set_left_tusk_talon(::std::unique_ptr<Talon>(new Talon(4)));
745 claw_writer.set_right_tusk_talon(::std::unique_ptr<Talon>(new Talon(3)));
746 claw_writer.set_intake1_talon(::std::unique_ptr<Talon>(new Talon(7)));
747 claw_writer.set_intake2_talon(::std::unique_ptr<Talon>(new Talon(8)));
Brian Silverman17f503e2015-08-02 18:17:18 -0700748 ::std::thread claw_writer_thread(::std::ref(claw_writer));
749
Brian Silvermanb601d892015-12-20 18:24:38 -0500750 ::y2014::wpilib::ShooterWriter shooter_writer;
Brian Silverman552350b2015-08-02 18:23:34 -0700751 shooter_writer.set_shooter_talon(::std::unique_ptr<Talon>(new Talon(6)));
752 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
753
Brian Silverman17f503e2015-08-02 18:17:18 -0700754 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
755 new ::frc971::wpilib::BufferedPcm());
756 SolenoidWriter solenoid_writer(pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700757 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
758 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
759 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
760 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700761
Austin Schuh016a6b02015-10-08 06:41:14 +0000762 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
Brian Silverman17f503e2015-08-02 18:17:18 -0700763 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
764 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
765
766 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800767 while (true) {
768 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
769 if (r != 0) {
770 PLOG(WARNING, "infinite select failed");
771 } else {
772 PLOG(WARNING, "infinite select succeeded??\n");
773 }
774 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700775
776 LOG(ERROR, "Exiting WPILibRobot\n");
777
778 joystick_sender.Quit();
779 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800780 pdp_fetcher.Quit();
781 pdp_fetcher_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700782 reader.Quit();
783 reader_thread.join();
784 gyro_sender.Quit();
785 gyro_thread.join();
786
787 drivetrain_writer.Quit();
788 drivetrain_writer_thread.join();
Brian Silverman552350b2015-08-02 18:23:34 -0700789 shooter_writer.Quit();
790 shooter_writer_thread.join();
791 claw_writer.Quit();
792 claw_writer_thread.join();
Brian Silverman17f503e2015-08-02 18:17:18 -0700793 solenoid_writer.Quit();
794 solenoid_thread.join();
795
796 ::aos::Cleanup();
797 }
798};
799
800} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500801} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700802
803
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800804AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);