blob: 5e398dbf9da5d21a2ff0087e62e94d42076e1edc [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;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070057using ::y2014::control_loops::ClawQueue;
58using ::y2014::control_loops::ShooterQueue;
Brian Silvermanf819b442019-01-20 16:51:04 -080059using aos::make_unique;
Brian Silverman17f503e2015-08-02 18:17:18 -070060
Brian Silvermanb601d892015-12-20 18:24:38 -050061namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070062namespace wpilib {
63
Brian Silverman85fbb602015-08-29 19:28:20 -070064// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
65// DMA stuff and then removing the * 2.0 in *_translate.
66// The low bit is direction.
67
Brian Silverman17f503e2015-08-02 18:17:18 -070068double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070069 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070070 (256.0 /*cpr*/ * 4.0 /*4x*/) *
71 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080072 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070073}
74
Brian Silverman51091a02015-12-26 15:56:58 -080075double drivetrain_velocity_translate(double in) {
76 return (1.0 / in) / 256.0 /*cpr*/ *
77 constants::GetValues().drivetrain_encoder_ratio *
78 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
79}
80
Sabina Davis415bb6c2017-10-16 23:30:52 -070081float hall_translate(const constants::DualHallShifterHallEffect &k, float in_low,
Brian Silverman552350b2015-08-02 18:23:34 -070082 float in_high) {
83 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070084 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
85 static_cast<float>(k.low_gear_middle - k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070086 const float high_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070087 0.5 +
88 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
89 static_cast<float>(k.shifter_hall_effect.high_gear_high -
90 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070091
Brian Silverman552350b2015-08-02 18:23:34 -070092 // Return low when we are below 1/2, and high when we are above 1/2.
93 if (low_ratio + high_ratio < 1.0) {
94 return low_ratio;
95 } else {
96 return high_ratio;
97 }
Brian Silverman17f503e2015-08-02 18:17:18 -070098}
99
100double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -0700101 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -0700102 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700103 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700104}
105
Brian Silverman552350b2015-08-02 18:23:34 -0700106double shooter_translate(int32_t in) {
107 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
108 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
109 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700110}
111
112static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700113 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800114 18.0 / 32.0 /* big belt reduction */ * 18.0 /
115 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
116 60.0 /* seconds / minute */ * 256.0 /* CPR */ * 4.0 /* edges / pulse */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700117
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800118class SensorReader : public ::frc971::wpilib::SensorReader {
Brian Silverman17f503e2015-08-02 18:17:18 -0700119 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800120 SensorReader(::aos::EventLoop *event_loop)
Austin Schuh7eed2de2019-05-25 14:34:40 -0700121 : ::frc971::wpilib::SensorReader(event_loop),
122 auto_mode_sender_(event_loop->MakeSender<::y2014::sensors::AutoMode>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700123 ".y2014.sensors.auto_mode")),
124 shooter_position_sender_(event_loop->MakeSender<ShooterQueue::Position>(
125 ".y2014.control_loops.shooter_queue.position")),
126 claw_position_sender_(event_loop->MakeSender<ClawQueue::Position>(
127 ".y2014.control_loops.claw_queue.position")) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700128 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
129 // we should ever see.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800130 UpdateMediumEncoderFilterHz(kMaximumEncoderPulsesPerSecond);
Brian Silverman552350b2015-08-02 18:23:34 -0700131 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700132 }
133
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800134 ~SensorReader() override {
135 top_reader_.Quit();
136 bottom_reader_.Quit();
137 }
138
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700139 void set_auto_selector_analog(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700140 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700141 }
142
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700143 void set_high_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700144 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700145 }
146
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700147 void set_low_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700148 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700149 }
150
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700151 void set_high_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700152 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700153 }
154
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700155 void set_low_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700156 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700157 }
158
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700159 void set_top_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800160 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700161 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700162 }
163
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700164 void set_top_claw_front_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700165 hall_filter_.Add(hall.get());
166 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700167 }
168
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700169 void set_top_claw_calibration_hall(
170 ::std::unique_ptr<::frc::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 Schuhbd1fe9c2019-06-29 16:35:48 -0700175 void set_top_claw_back_hall(::std::unique_ptr<::frc::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
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700180 void set_bottom_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800181 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700182 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700183 }
184
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700185 void set_bottom_claw_front_hall(::std::unique_ptr<::frc::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 Schuhbd1fe9c2019-06-29 16:35:48 -0700190 void set_bottom_claw_calibration_hall(
191 ::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700192 hall_filter_.Add(hall.get());
193 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700194 }
195
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700196 void set_bottom_claw_back_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700197 hall_filter_.Add(hall.get());
198 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700199 }
200
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700201 void set_shooter_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800202 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700203 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700204 }
205
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700206 void set_shooter_proximal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700207 hall_filter_.Add(hall.get());
208 shooter_proximal_ = ::std::move(hall);
209 }
210
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700211 void set_shooter_distal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700212 hall_filter_.Add(hall.get());
213 shooter_distal_ = ::std::move(hall);
214 }
215
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700216 void set_shooter_plunger(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700217 hall_filter_.Add(hall.get());
218 shooter_plunger_ = ::std::move(hall);
219 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500220 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700221 }
222
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700223 void set_shooter_latch(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700224 hall_filter_.Add(hall.get());
225 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500226 shooter_latch_reader_ =
227 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700228 }
229
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800230 void Start() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500231 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700232 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500233 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700234 shooter_encoder_.get(), shooter_distal_.get());
235
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800236 AddToDMA(shooter_proximal_counter_.get());
237 AddToDMA(shooter_distal_counter_.get());
238 AddToDMA(shooter_plunger_reader_.get());
239 AddToDMA(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700240
Brian Silverman552350b2015-08-02 18:23:34 -0700241 top_reader_.Start();
242 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700243 }
244
245 void RunIteration() {
Brian Silverman552350b2015-08-02 18:23:34 -0700246 const auto &values = constants::GetValues();
247
Brian Silverman17f503e2015-08-02 18:17:18 -0700248 {
249 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
250 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700251 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700252 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700253 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800254 drivetrain_message->left_speed =
255 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
256 drivetrain_message->right_speed =
257 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800258
259 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
260 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700261 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800262 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
263 drivetrain_message->high_left_hall);
264
265 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
266 drivetrain_message->high_right_hall =
267 high_right_drive_hall_->GetVoltage();
268 drivetrain_message->right_shifter_position =
269 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
270 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700271
272 drivetrain_message.Send();
273 }
274
Austin Schuh7eed2de2019-05-25 14:34:40 -0700275 {
276 auto auto_mode_message = auto_mode_sender_.MakeMessage();
277 auto_mode_message->voltage = auto_selector_analog_->GetVoltage();
278 auto_mode_message.Send();
279 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800280 }
Brian Silverman552350b2015-08-02 18:23:34 -0700281
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800282 void RunDmaIteration() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700283 {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700284 auto shooter_message = shooter_position_sender_.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700285 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800286 shooter_message->plunger = !shooter_plunger_reader_->value();
287 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700288 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
289 &shooter_message->pusher_proximal);
290 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
291 &shooter_message->pusher_distal);
292
293 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700294 }
295
296 {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700297 auto claw_message = claw_position_sender_.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700298 top_reader_.RunIteration(&claw_message->top);
299 bottom_reader_.RunIteration(&claw_message->bottom);
300
Brian Silverman17f503e2015-08-02 18:17:18 -0700301 claw_message.Send();
302 }
303 }
304
Brian Silverman17f503e2015-08-02 18:17:18 -0700305 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700306 class HalfClawReader {
307 public:
308 HalfClawReader(bool reversed) : reversed_(reversed) {}
309
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700310 void set_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Brian Silverman552350b2015-08-02 18:23:34 -0700311 encoder_ = ::std::move(encoder);
312 }
313
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700314 void set_front_hall(::std::unique_ptr<::frc::DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700315 front_hall_ = ::std::move(front_hall);
316 }
317
318 void set_calibration_hall(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700319 ::std::unique_ptr<::frc::DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700320 calibration_hall_ = ::std::move(calibration_hall);
321 }
322
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700323 void set_back_hall(::std::unique_ptr<::frc::DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700324 back_hall_ = ::std::move(back_hall);
325 }
326
327 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500328 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
329 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700330 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500331 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
332 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700333 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500334 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
335 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700336 synchronizer_.Add(back_counter_.get());
337 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500338 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
339 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700340 synchronizer_.Add(synchronized_encoder_.get());
341
342 synchronizer_.Start();
343 }
344
345 void Quit() { synchronizer_.Quit(); }
346
347 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
348 const double multiplier = reversed_ ? -1.0 : 1.0;
349
350 synchronizer_.RunIteration();
351
352 CopyPosition(front_counter_.get(), &half_claw_position->front);
353 CopyPosition(calibration_counter_.get(),
354 &half_claw_position->calibration);
355 CopyPosition(back_counter_.get(), &half_claw_position->back);
356 half_claw_position->position =
357 multiplier * claw_translate(synchronized_encoder_->get());
358 }
359
360 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500361 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
362 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700363 const double multiplier = reversed_ ? -1.0 : 1.0;
364
Austin Schuh5c25ab72015-11-01 13:17:11 -0800365 out->current = !counter->polled_value();
366 out->posedge_count = counter->negative_interrupt_count();
367 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700368 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800369 multiplier * claw_translate(counter->last_positive_encoder_value());
370 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700371 multiplier * claw_translate(counter->last_negative_encoder_value());
372 }
373
Brian Silverman5090c432016-01-02 14:44:26 -0800374 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700375
Brian Silvermanb601d892015-12-20 18:24:38 -0500376 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
377 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
378 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
379 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
380 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700381
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700382 ::std::unique_ptr<::frc::Encoder> encoder_;
383 ::std::unique_ptr<::frc::DigitalInput> front_hall_;
384 ::std::unique_ptr<::frc::DigitalInput> calibration_hall_;
385 ::std::unique_ptr<::frc::DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700386
387 const bool reversed_;
388 };
389
Brian Silvermanb601d892015-12-20 18:24:38 -0500390 void CopyShooterPosedgeCounts(
391 const ::frc971::wpilib::DMAEdgeCounter *counter,
392 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800393 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700394 // These are inverted because the hall effects give logical false when
395 // there's a magnet in front of them.
396 output->posedge_count = counter->negative_count();
397 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700398 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700399 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700400 }
401
Austin Schuh7eed2de2019-05-25 14:34:40 -0700402 ::aos::Sender<::y2014::sensors::AutoMode> auto_mode_sender_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700403 ::aos::Sender<ShooterQueue::Position> shooter_position_sender_;
404 ::aos::Sender<ClawQueue::Position> claw_position_sender_;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700405
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700406 ::std::unique_ptr<::frc::AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700407
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700408 ::std::unique_ptr<::frc::AnalogInput> low_left_drive_hall_;
409 ::std::unique_ptr<::frc::AnalogInput> high_left_drive_hall_;
410 ::std::unique_ptr<::frc::AnalogInput> low_right_drive_hall_;
411 ::std::unique_ptr<::frc::AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700412
Brian Silverman552350b2015-08-02 18:23:34 -0700413 HalfClawReader top_reader_{false}, bottom_reader_{true};
414
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700415 ::std::unique_ptr<::frc::Encoder> shooter_encoder_;
416 ::std::unique_ptr<::frc::DigitalInput> shooter_proximal_, shooter_distal_;
417 ::std::unique_ptr<::frc::DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500418 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700419 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500420 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700421 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700422
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700423 ::frc::DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700424};
425
426class SolenoidWriter {
427 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700428 SolenoidWriter(::aos::EventLoop *event_loop,
429 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700430 : pcm_(pcm),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700431 shooter_(event_loop->MakeFetcher<ShooterQueue::Output>(
432 ".y2014.control_loops.shooter_queue.output")),
433 drivetrain_(
434 event_loop
435 ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Output>(
436 ".frc971.control_loops.drivetrain_queue.output")) {
437 event_loop->set_name("Solenoids");
438 event_loop->SetRuntimeRealtimePriority(27);
Brian Silverman17f503e2015-08-02 18:17:18 -0700439
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700440 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
441 ::std::chrono::milliseconds(20),
442 ::std::chrono::milliseconds(1));
443 }
444
445 void set_pressure_switch(
446 ::std::unique_ptr<::frc::DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700447 pressure_switch_ = ::std::move(pressure_switch);
448 }
449
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700450 void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700451 compressor_relay_ = ::std::move(compressor_relay);
452 }
453
Brian Silvermanb601d892015-12-20 18:24:38 -0500454 void set_drivetrain_left(
455 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700456 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700457 }
458
Brian Silvermanb601d892015-12-20 18:24:38 -0500459 void set_drivetrain_right(
460 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700461 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700462 }
463
Brian Silvermanb601d892015-12-20 18:24:38 -0500464 void set_shooter_latch(
465 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700466 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 }
468
Brian Silvermanb601d892015-12-20 18:24:38 -0500469 void set_shooter_brake(
470 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700471 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700472 }
473
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700474 void Loop(const int iterations) {
475 if (iterations != 1) {
476 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
477 }
Brian Silverman5090c432016-01-02 14:44:26 -0800478
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700479 {
480 shooter_.Fetch();
481 if (shooter_.get()) {
482 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
483 shooter_latch_->Set(!shooter_->latch_piston);
484 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700485 }
486 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700487
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488 {
489 drivetrain_.Fetch();
490 if (drivetrain_.get()) {
491 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
492 drivetrain_left_->Set(!drivetrain_->left_high);
493 drivetrain_right_->Set(!drivetrain_->right_high);
494 }
495 }
496
497 {
498 ::frc971::wpilib::PneumaticsToLog to_log;
499 {
500 const bool compressor_on = !pressure_switch_->Get();
501 to_log.compressor_on = compressor_on;
502 if (compressor_on) {
503 compressor_relay_->Set(::frc::Relay::kForward);
504 } else {
505 compressor_relay_->Set(::frc::Relay::kOff);
506 }
507 }
508
509 pcm_->Flush();
510 to_log.read_solenoids = pcm_->GetAll();
511 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
512 }
513 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700514
515 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500516 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700517
Brian Silvermanb601d892015-12-20 18:24:38 -0500518 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
519 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
520 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
521 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700522
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700523 ::std::unique_ptr<::frc::DigitalInput> pressure_switch_;
524 ::std::unique_ptr<::frc::Relay> compressor_relay_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700525
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700526 ::aos::Fetcher<ShooterQueue::Output> shooter_;
527 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700528};
529
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700530class ShooterWriter
531 : public ::frc971::wpilib::LoopOutputHandler<ShooterQueue::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700532 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800533 ShooterWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700534 : ::frc971::wpilib::LoopOutputHandler<ShooterQueue::Output>(
535 event_loop, ".y2014.control_loops.shooter_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800536
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700537 void set_shooter_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700538 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700539 }
540
541 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700542 virtual void Write(const ShooterQueue::Output &output) override {
543 LOG_STRUCT(DEBUG, "will output", output);
544 shooter_talon_->SetSpeed(output.voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700545 }
546
547 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700548 LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800549 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700550 }
551
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700552 ::std::unique_ptr<::frc::Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700553};
554
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700555class ClawWriter
556 : public ::frc971::wpilib::LoopOutputHandler<ClawQueue::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700557 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800558 ClawWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700559 : ::frc971::wpilib::LoopOutputHandler<ClawQueue::Output>(
560 event_loop, ".y2014.control_loops.claw_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800561
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700562 void set_top_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700563 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700564 }
565
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700566 void set_bottom_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700567 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700568 }
569
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700570 void set_left_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700571 left_tusk_talon_ = ::std::move(t);
572 }
573
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700574 void set_right_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700575 right_tusk_talon_ = ::std::move(t);
576 }
577
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700578 void set_intake1_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700579 intake1_talon_ = ::std::move(t);
580 }
581
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700582 void set_intake2_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700583 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700584 }
585
586 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700587 virtual void Write(const ClawQueue::Output &output) override {
588 LOG_STRUCT(DEBUG, "will output", output);
589 intake1_talon_->SetSpeed(output.intake_voltage / 12.0);
590 intake2_talon_->SetSpeed(output.intake_voltage / 12.0);
591 bottom_claw_talon_->SetSpeed(-output.bottom_claw_voltage / 12.0);
592 top_claw_talon_->SetSpeed(output.top_claw_voltage / 12.0);
593 left_tusk_talon_->SetSpeed(output.tusk_voltage / 12.0);
594 right_tusk_talon_->SetSpeed(-output.tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700595 }
596
597 virtual void Stop() override {
Brian Silverman552350b2015-08-02 18:23:34 -0700598 LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800599 intake1_talon_->SetDisabled();
600 intake2_talon_->SetDisabled();
601 bottom_claw_talon_->SetDisabled();
602 top_claw_talon_->SetDisabled();
603 left_tusk_talon_->SetDisabled();
604 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700605 }
606
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700607 ::std::unique_ptr<::frc::Talon> top_claw_talon_;
608 ::std::unique_ptr<::frc::Talon> bottom_claw_talon_;
609 ::std::unique_ptr<::frc::Talon> left_tusk_talon_;
610 ::std::unique_ptr<::frc::Talon> right_tusk_talon_;
611 ::std::unique_ptr<::frc::Talon> intake1_talon_;
612 ::std::unique_ptr<::frc::Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700613};
614
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800615class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700616 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700617 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
618 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
619 ::frc::Encoder::k4X);
Brian Silverman17f503e2015-08-02 18:17:18 -0700620 }
Brian Silverman552350b2015-08-02 18:23:34 -0700621
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800622 void Run() override {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700623 // Thread 1.
624 ::aos::ShmEventLoop joystick_sender_event_loop;
625 ::frc971::wpilib::JoystickSender joystick_sender(
626 &joystick_sender_event_loop);
627 AddLoop(&joystick_sender_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700628
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700629 // Thread 2.
630 ::aos::ShmEventLoop pdp_fetcher_event_loop;
631 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
632 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800633
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700634 // Thread 3.
635 ::aos::ShmEventLoop sensor_reader_event_loop;
636 SensorReader sensor_reader(&sensor_reader_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700637
Brian Silverman85fbb602015-08-29 19:28:20 -0700638 // Create this first to make sure it ends up in one of the lower-numbered
639 // FPGA slots so we can use it with DMA.
640 auto shooter_encoder_temp = make_encoder(2);
641
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700642 sensor_reader.set_auto_selector_analog(make_unique<::frc::AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700643
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700644 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
645 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
646 sensor_reader.set_high_left_drive_hall(make_unique<::frc::AnalogInput>(1));
647 sensor_reader.set_low_left_drive_hall(make_unique<::frc::AnalogInput>(0));
648 sensor_reader.set_high_right_drive_hall(make_unique<::frc::AnalogInput>(2));
649 sensor_reader.set_low_right_drive_hall(make_unique<::frc::AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700650
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700651 sensor_reader.set_top_claw_encoder(make_encoder(3));
652 sensor_reader.set_top_claw_front_hall(
653 make_unique<::frc::DigitalInput>(4)); // R2
654 sensor_reader.set_top_claw_calibration_hall(
655 make_unique<::frc::DigitalInput>(3)); // R3
656 sensor_reader.set_top_claw_back_hall(
657 make_unique<::frc::DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700658
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700659 sensor_reader.set_bottom_claw_encoder(make_encoder(4));
660 sensor_reader.set_bottom_claw_front_hall(
661 make_unique<::frc::DigitalInput>(1)); // L2
662 sensor_reader.set_bottom_claw_calibration_hall(
663 make_unique<::frc::DigitalInput>(0)); // L3
664 sensor_reader.set_bottom_claw_back_hall(
665 make_unique<::frc::DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700666
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700667 sensor_reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
668 sensor_reader.set_shooter_proximal(
669 make_unique<::frc::DigitalInput>(6)); // S1
670 sensor_reader.set_shooter_distal(
671 make_unique<::frc::DigitalInput>(7)); // S2
672 sensor_reader.set_shooter_plunger(
673 make_unique<::frc::DigitalInput>(8)); // S3
674 sensor_reader.set_shooter_latch(make_unique<::frc::DigitalInput>(9)); // S4
675 AddLoop(&sensor_reader_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700676
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700677 // Thread 4.
678 ::aos::ShmEventLoop gyro_event_loop;
679 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
680 AddLoop(&gyro_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700681
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700682 // Thread 5.
683 ::aos::ShmEventLoop output_event_loop;
684 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
685 drivetrain_writer.set_left_controller0(make_unique<::frc::Talon>(5), true);
686 drivetrain_writer.set_right_controller0(make_unique<::frc::Talon>(2),
687 false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700688
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700689 ::y2014::wpilib::ClawWriter claw_writer(&output_event_loop);
690 claw_writer.set_top_claw_talon(make_unique<::frc::Talon>(1));
691 claw_writer.set_bottom_claw_talon(make_unique<::frc::Talon>(0));
692 claw_writer.set_left_tusk_talon(make_unique<::frc::Talon>(4));
693 claw_writer.set_right_tusk_talon(make_unique<::frc::Talon>(3));
694 claw_writer.set_intake1_talon(make_unique<::frc::Talon>(7));
695 claw_writer.set_intake2_talon(make_unique<::frc::Talon>(8));
Brian Silverman17f503e2015-08-02 18:17:18 -0700696
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700697 ::y2014::wpilib::ShooterWriter shooter_writer(&output_event_loop);
698 shooter_writer.set_shooter_talon(make_unique<::frc::Talon>(6));
Brian Silverman17f503e2015-08-02 18:17:18 -0700699
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700700 AddLoop(&output_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700701
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700702 // Thread 6.
703 ::aos::ShmEventLoop solenoid_writer_event_loop;
Brian Silverman17f503e2015-08-02 18:17:18 -0700704 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
705 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700706 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700707 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
708 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
709 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
710 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700711
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700712 solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(25));
713 solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0));
714 AddLoop(&solenoid_writer_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700715
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700716 RunLoops();
Brian Silverman17f503e2015-08-02 18:17:18 -0700717 }
718};
719
720} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500721} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700722
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800723AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);