blob: 7952adc005abb5f23f06271ff02acb630ddc2aad [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
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070056using ::y2014::control_loops::ClawQueue;
57using ::y2014::control_loops::ShooterQueue;
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:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800119 SensorReader(::aos::EventLoop *event_loop)
Austin Schuh7eed2de2019-05-25 14:34:40 -0700120 : ::frc971::wpilib::SensorReader(event_loop),
121 auto_mode_sender_(event_loop->MakeSender<::y2014::sensors::AutoMode>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700122 ".y2014.sensors.auto_mode")),
123 shooter_position_sender_(event_loop->MakeSender<ShooterQueue::Position>(
124 ".y2014.control_loops.shooter_queue.position")),
125 claw_position_sender_(event_loop->MakeSender<ClawQueue::Position>(
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700126 ".y2014.control_loops.claw_queue.position")),
127 drivetrain_position_sender_(
128 event_loop->MakeSender<
129 ::frc971::control_loops::DrivetrainQueue::Position>(
130 ".frc971.control_loops.drivetrain_queue.position")) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700131 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
132 // we should ever see.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800133 UpdateMediumEncoderFilterHz(kMaximumEncoderPulsesPerSecond);
Brian Silverman552350b2015-08-02 18:23:34 -0700134 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700135 }
136
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800137 ~SensorReader() override {
138 top_reader_.Quit();
139 bottom_reader_.Quit();
140 }
141
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700142 void set_auto_selector_analog(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700143 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 }
145
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700146 void set_high_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700147 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700150 void set_low_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700151 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700152 }
153
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700154 void set_high_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700155 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700156 }
157
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700158 void set_low_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700159 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700160 }
161
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700162 void set_top_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800163 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700164 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700165 }
166
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700167 void set_top_claw_front_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700168 hall_filter_.Add(hall.get());
169 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700170 }
171
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700172 void set_top_claw_calibration_hall(
173 ::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700174 hall_filter_.Add(hall.get());
175 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700176 }
177
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700178 void set_top_claw_back_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700179 hall_filter_.Add(hall.get());
180 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700181 }
182
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700183 void set_bottom_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800184 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700185 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700186 }
187
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700188 void set_bottom_claw_front_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700189 hall_filter_.Add(hall.get());
190 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700191 }
192
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700193 void set_bottom_claw_calibration_hall(
194 ::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700195 hall_filter_.Add(hall.get());
196 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700197 }
198
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700199 void set_bottom_claw_back_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700200 hall_filter_.Add(hall.get());
201 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700202 }
203
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700204 void set_shooter_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800205 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700206 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700207 }
208
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700209 void set_shooter_proximal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700210 hall_filter_.Add(hall.get());
211 shooter_proximal_ = ::std::move(hall);
212 }
213
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700214 void set_shooter_distal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700215 hall_filter_.Add(hall.get());
216 shooter_distal_ = ::std::move(hall);
217 }
218
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700219 void set_shooter_plunger(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700220 hall_filter_.Add(hall.get());
221 shooter_plunger_ = ::std::move(hall);
222 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500223 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700224 }
225
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700226 void set_shooter_latch(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700227 hall_filter_.Add(hall.get());
228 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500229 shooter_latch_reader_ =
230 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700231 }
232
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800233 void Start() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500234 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700235 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500236 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700237 shooter_encoder_.get(), shooter_distal_.get());
238
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800239 AddToDMA(shooter_proximal_counter_.get());
240 AddToDMA(shooter_distal_counter_.get());
241 AddToDMA(shooter_plunger_reader_.get());
242 AddToDMA(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700243
Brian Silverman552350b2015-08-02 18:23:34 -0700244 top_reader_.Start();
245 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700246 }
247
248 void RunIteration() {
Brian Silverman552350b2015-08-02 18:23:34 -0700249 const auto &values = constants::GetValues();
250
Brian Silverman17f503e2015-08-02 18:17:18 -0700251 {
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700252 auto drivetrain_message = drivetrain_position_sender_.MakeMessage();
Brian Silverman17f503e2015-08-02 18:17:18 -0700253 drivetrain_message->right_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700254 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Brian Silverman17f503e2015-08-02 18:17:18 -0700255 drivetrain_message->left_encoder =
Brian Silverman552350b2015-08-02 18:23:34 -0700256 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800257 drivetrain_message->left_speed =
258 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
259 drivetrain_message->right_speed =
260 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Austin Schuha0c1e152015-11-08 14:10:13 -0800261
262 drivetrain_message->low_left_hall = low_left_drive_hall_->GetVoltage();
263 drivetrain_message->high_left_hall = high_left_drive_hall_->GetVoltage();
Brian Silverman552350b2015-08-02 18:23:34 -0700264 drivetrain_message->left_shifter_position =
Austin Schuha0c1e152015-11-08 14:10:13 -0800265 hall_translate(values.left_drive, drivetrain_message->low_left_hall,
266 drivetrain_message->high_left_hall);
267
268 drivetrain_message->low_right_hall = low_right_drive_hall_->GetVoltage();
269 drivetrain_message->high_right_hall =
270 high_right_drive_hall_->GetVoltage();
271 drivetrain_message->right_shifter_position =
272 hall_translate(values.right_drive, drivetrain_message->low_right_hall,
273 drivetrain_message->high_right_hall);
Brian Silverman17f503e2015-08-02 18:17:18 -0700274
275 drivetrain_message.Send();
276 }
277
Austin Schuh7eed2de2019-05-25 14:34:40 -0700278 {
279 auto auto_mode_message = auto_mode_sender_.MakeMessage();
280 auto_mode_message->voltage = auto_selector_analog_->GetVoltage();
281 auto_mode_message.Send();
282 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800283 }
Brian Silverman552350b2015-08-02 18:23:34 -0700284
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800285 void RunDmaIteration() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700286 {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700287 auto shooter_message = shooter_position_sender_.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700288 shooter_message->position = shooter_translate(shooter_encoder_->GetRaw());
Austin Schuh5c25ab72015-11-01 13:17:11 -0800289 shooter_message->plunger = !shooter_plunger_reader_->value();
290 shooter_message->latch = !shooter_latch_reader_->value();
Brian Silverman552350b2015-08-02 18:23:34 -0700291 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
292 &shooter_message->pusher_proximal);
293 CopyShooterPosedgeCounts(shooter_distal_counter_.get(),
294 &shooter_message->pusher_distal);
295
296 shooter_message.Send();
Brian Silverman17f503e2015-08-02 18:17:18 -0700297 }
298
299 {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700300 auto claw_message = claw_position_sender_.MakeMessage();
Brian Silverman552350b2015-08-02 18:23:34 -0700301 top_reader_.RunIteration(&claw_message->top);
302 bottom_reader_.RunIteration(&claw_message->bottom);
303
Brian Silverman17f503e2015-08-02 18:17:18 -0700304 claw_message.Send();
305 }
306 }
307
Brian Silverman17f503e2015-08-02 18:17:18 -0700308 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700309 class HalfClawReader {
310 public:
311 HalfClawReader(bool reversed) : reversed_(reversed) {}
312
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700313 void set_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Brian Silverman552350b2015-08-02 18:23:34 -0700314 encoder_ = ::std::move(encoder);
315 }
316
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700317 void set_front_hall(::std::unique_ptr<::frc::DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700318 front_hall_ = ::std::move(front_hall);
319 }
320
321 void set_calibration_hall(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700322 ::std::unique_ptr<::frc::DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700323 calibration_hall_ = ::std::move(calibration_hall);
324 }
325
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700326 void set_back_hall(::std::unique_ptr<::frc::DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700327 back_hall_ = ::std::move(back_hall);
328 }
329
330 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500331 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
332 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700333 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500334 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
335 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700336 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500337 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
338 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700339 synchronizer_.Add(back_counter_.get());
340 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500341 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
342 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700343 synchronizer_.Add(synchronized_encoder_.get());
344
345 synchronizer_.Start();
346 }
347
348 void Quit() { synchronizer_.Quit(); }
349
350 void RunIteration(control_loops::HalfClawPosition *half_claw_position) {
351 const double multiplier = reversed_ ? -1.0 : 1.0;
352
353 synchronizer_.RunIteration();
354
355 CopyPosition(front_counter_.get(), &half_claw_position->front);
356 CopyPosition(calibration_counter_.get(),
357 &half_claw_position->calibration);
358 CopyPosition(back_counter_.get(), &half_claw_position->back);
359 half_claw_position->position =
360 multiplier * claw_translate(synchronized_encoder_->get());
361 }
362
363 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500364 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
365 ::frc971::HallEffectStruct *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700366 const double multiplier = reversed_ ? -1.0 : 1.0;
367
Austin Schuh5c25ab72015-11-01 13:17:11 -0800368 out->current = !counter->polled_value();
369 out->posedge_count = counter->negative_interrupt_count();
370 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700371 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800372 multiplier * claw_translate(counter->last_positive_encoder_value());
373 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700374 multiplier * claw_translate(counter->last_negative_encoder_value());
375 }
376
Brian Silverman5090c432016-01-02 14:44:26 -0800377 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700378
Brian Silvermanb601d892015-12-20 18:24:38 -0500379 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
380 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
381 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
382 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
383 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700384
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700385 ::std::unique_ptr<::frc::Encoder> encoder_;
386 ::std::unique_ptr<::frc::DigitalInput> front_hall_;
387 ::std::unique_ptr<::frc::DigitalInput> calibration_hall_;
388 ::std::unique_ptr<::frc::DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700389
390 const bool reversed_;
391 };
392
Brian Silvermanb601d892015-12-20 18:24:38 -0500393 void CopyShooterPosedgeCounts(
394 const ::frc971::wpilib::DMAEdgeCounter *counter,
395 ::frc971::PosedgeOnlyCountedHallEffectStruct *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800396 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700397 // These are inverted because the hall effects give logical false when
398 // there's a magnet in front of them.
399 output->posedge_count = counter->negative_count();
400 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700401 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700402 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700403 }
404
Austin Schuh7eed2de2019-05-25 14:34:40 -0700405 ::aos::Sender<::y2014::sensors::AutoMode> auto_mode_sender_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700406 ::aos::Sender<ShooterQueue::Position> shooter_position_sender_;
407 ::aos::Sender<ClawQueue::Position> claw_position_sender_;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700408 ::aos::Sender<::frc971::control_loops::DrivetrainQueue::Position>
409 drivetrain_position_sender_;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700410
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700411 ::std::unique_ptr<::frc::AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700412
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700413 ::std::unique_ptr<::frc::AnalogInput> low_left_drive_hall_;
414 ::std::unique_ptr<::frc::AnalogInput> high_left_drive_hall_;
415 ::std::unique_ptr<::frc::AnalogInput> low_right_drive_hall_;
416 ::std::unique_ptr<::frc::AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700417
Brian Silverman552350b2015-08-02 18:23:34 -0700418 HalfClawReader top_reader_{false}, bottom_reader_{true};
419
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700420 ::std::unique_ptr<::frc::Encoder> shooter_encoder_;
421 ::std::unique_ptr<::frc::DigitalInput> shooter_proximal_, shooter_distal_;
422 ::std::unique_ptr<::frc::DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500423 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700424 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500425 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700426 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700427
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700428 ::frc::DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700429};
430
431class SolenoidWriter {
432 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700433 SolenoidWriter(::aos::EventLoop *event_loop,
434 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700435 : pcm_(pcm),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700436 shooter_(event_loop->MakeFetcher<ShooterQueue::Output>(
437 ".y2014.control_loops.shooter_queue.output")),
438 drivetrain_(
439 event_loop
440 ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Output>(
441 ".frc971.control_loops.drivetrain_queue.output")) {
442 event_loop->set_name("Solenoids");
443 event_loop->SetRuntimeRealtimePriority(27);
Brian Silverman17f503e2015-08-02 18:17:18 -0700444
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700445 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
446 ::std::chrono::milliseconds(20),
447 ::std::chrono::milliseconds(1));
448 }
449
450 void set_pressure_switch(
451 ::std::unique_ptr<::frc::DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700452 pressure_switch_ = ::std::move(pressure_switch);
453 }
454
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700455 void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700456 compressor_relay_ = ::std::move(compressor_relay);
457 }
458
Brian Silvermanb601d892015-12-20 18:24:38 -0500459 void set_drivetrain_left(
460 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700461 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700462 }
463
Brian Silvermanb601d892015-12-20 18:24:38 -0500464 void set_drivetrain_right(
465 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700466 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700467 }
468
Brian Silvermanb601d892015-12-20 18:24:38 -0500469 void set_shooter_latch(
470 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700471 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700472 }
473
Brian Silvermanb601d892015-12-20 18:24:38 -0500474 void set_shooter_brake(
475 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700476 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700477 }
478
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700479 void Loop(const int iterations) {
480 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700481 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700482 }
Brian Silverman5090c432016-01-02 14:44:26 -0800483
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700484 {
485 shooter_.Fetch();
486 if (shooter_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700487 AOS_LOG_STRUCT(DEBUG, "solenoids", *shooter_);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488 shooter_latch_->Set(!shooter_->latch_piston);
489 shooter_brake_->Set(!shooter_->brake_piston);
Brian Silverman17f503e2015-08-02 18:17:18 -0700490 }
491 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700492
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700493 {
494 drivetrain_.Fetch();
495 if (drivetrain_.get()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700496 AOS_LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700497 drivetrain_left_->Set(!drivetrain_->left_high);
498 drivetrain_right_->Set(!drivetrain_->right_high);
499 }
500 }
501
502 {
503 ::frc971::wpilib::PneumaticsToLog to_log;
504 {
505 const bool compressor_on = !pressure_switch_->Get();
506 to_log.compressor_on = compressor_on;
507 if (compressor_on) {
508 compressor_relay_->Set(::frc::Relay::kForward);
509 } else {
510 compressor_relay_->Set(::frc::Relay::kOff);
511 }
512 }
513
514 pcm_->Flush();
515 to_log.read_solenoids = pcm_->GetAll();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700516 AOS_LOG_STRUCT(DEBUG, "pneumatics info", to_log);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700517 }
518 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700519
520 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500521 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700522
Brian Silvermanb601d892015-12-20 18:24:38 -0500523 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
524 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
525 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
526 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700527
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700528 ::std::unique_ptr<::frc::DigitalInput> pressure_switch_;
529 ::std::unique_ptr<::frc::Relay> compressor_relay_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700530
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700531 ::aos::Fetcher<ShooterQueue::Output> shooter_;
532 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700533};
534
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700535class ShooterWriter
536 : public ::frc971::wpilib::LoopOutputHandler<ShooterQueue::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700537 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800538 ShooterWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700539 : ::frc971::wpilib::LoopOutputHandler<ShooterQueue::Output>(
540 event_loop, ".y2014.control_loops.shooter_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800541
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700542 void set_shooter_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700543 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700544 }
545
546 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700547 virtual void Write(const ShooterQueue::Output &output) override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700548 AOS_LOG_STRUCT(DEBUG, "will output", output);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700549 shooter_talon_->SetSpeed(output.voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700550 }
551
552 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700553 AOS_LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800554 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700555 }
556
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700557 ::std::unique_ptr<::frc::Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700558};
559
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700560class ClawWriter
561 : public ::frc971::wpilib::LoopOutputHandler<ClawQueue::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700562 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800563 ClawWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700564 : ::frc971::wpilib::LoopOutputHandler<ClawQueue::Output>(
565 event_loop, ".y2014.control_loops.claw_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800566
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700567 void set_top_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700568 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700569 }
570
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700571 void set_bottom_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700572 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700573 }
574
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700575 void set_left_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700576 left_tusk_talon_ = ::std::move(t);
577 }
578
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700579 void set_right_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700580 right_tusk_talon_ = ::std::move(t);
581 }
582
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700583 void set_intake1_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700584 intake1_talon_ = ::std::move(t);
585 }
586
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700587 void set_intake2_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700588 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700589 }
590
591 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700592 virtual void Write(const ClawQueue::Output &output) override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700593 AOS_LOG_STRUCT(DEBUG, "will output", output);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700594 intake1_talon_->SetSpeed(output.intake_voltage / 12.0);
595 intake2_talon_->SetSpeed(output.intake_voltage / 12.0);
596 bottom_claw_talon_->SetSpeed(-output.bottom_claw_voltage / 12.0);
597 top_claw_talon_->SetSpeed(output.top_claw_voltage / 12.0);
598 left_tusk_talon_->SetSpeed(output.tusk_voltage / 12.0);
599 right_tusk_talon_->SetSpeed(-output.tusk_voltage / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700600 }
601
602 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700603 AOS_LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800604 intake1_talon_->SetDisabled();
605 intake2_talon_->SetDisabled();
606 bottom_claw_talon_->SetDisabled();
607 top_claw_talon_->SetDisabled();
608 left_tusk_talon_->SetDisabled();
609 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700610 }
611
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700612 ::std::unique_ptr<::frc::Talon> top_claw_talon_;
613 ::std::unique_ptr<::frc::Talon> bottom_claw_talon_;
614 ::std::unique_ptr<::frc::Talon> left_tusk_talon_;
615 ::std::unique_ptr<::frc::Talon> right_tusk_talon_;
616 ::std::unique_ptr<::frc::Talon> intake1_talon_;
617 ::std::unique_ptr<::frc::Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700618};
619
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800620class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700621 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700622 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
623 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
624 ::frc::Encoder::k4X);
Brian Silverman17f503e2015-08-02 18:17:18 -0700625 }
Brian Silverman552350b2015-08-02 18:23:34 -0700626
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800627 void Run() override {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700628 // Thread 1.
629 ::aos::ShmEventLoop joystick_sender_event_loop;
630 ::frc971::wpilib::JoystickSender joystick_sender(
631 &joystick_sender_event_loop);
632 AddLoop(&joystick_sender_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700633
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700634 // Thread 2.
635 ::aos::ShmEventLoop pdp_fetcher_event_loop;
636 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
637 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800638
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700639 // Thread 3.
640 ::aos::ShmEventLoop sensor_reader_event_loop;
641 SensorReader sensor_reader(&sensor_reader_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700642
Brian Silverman85fbb602015-08-29 19:28:20 -0700643 // Create this first to make sure it ends up in one of the lower-numbered
644 // FPGA slots so we can use it with DMA.
645 auto shooter_encoder_temp = make_encoder(2);
646
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700647 sensor_reader.set_auto_selector_analog(make_unique<::frc::AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700648
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700649 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
650 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
651 sensor_reader.set_high_left_drive_hall(make_unique<::frc::AnalogInput>(1));
652 sensor_reader.set_low_left_drive_hall(make_unique<::frc::AnalogInput>(0));
653 sensor_reader.set_high_right_drive_hall(make_unique<::frc::AnalogInput>(2));
654 sensor_reader.set_low_right_drive_hall(make_unique<::frc::AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700655
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700656 sensor_reader.set_top_claw_encoder(make_encoder(3));
657 sensor_reader.set_top_claw_front_hall(
658 make_unique<::frc::DigitalInput>(4)); // R2
659 sensor_reader.set_top_claw_calibration_hall(
660 make_unique<::frc::DigitalInput>(3)); // R3
661 sensor_reader.set_top_claw_back_hall(
662 make_unique<::frc::DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700663
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700664 sensor_reader.set_bottom_claw_encoder(make_encoder(4));
665 sensor_reader.set_bottom_claw_front_hall(
666 make_unique<::frc::DigitalInput>(1)); // L2
667 sensor_reader.set_bottom_claw_calibration_hall(
668 make_unique<::frc::DigitalInput>(0)); // L3
669 sensor_reader.set_bottom_claw_back_hall(
670 make_unique<::frc::DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700671
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700672 sensor_reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
673 sensor_reader.set_shooter_proximal(
674 make_unique<::frc::DigitalInput>(6)); // S1
675 sensor_reader.set_shooter_distal(
676 make_unique<::frc::DigitalInput>(7)); // S2
677 sensor_reader.set_shooter_plunger(
678 make_unique<::frc::DigitalInput>(8)); // S3
679 sensor_reader.set_shooter_latch(make_unique<::frc::DigitalInput>(9)); // S4
680 AddLoop(&sensor_reader_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700681
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700682 // Thread 4.
683 ::aos::ShmEventLoop gyro_event_loop;
684 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
685 AddLoop(&gyro_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700686
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700687 // Thread 5.
688 ::aos::ShmEventLoop output_event_loop;
689 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
690 drivetrain_writer.set_left_controller0(make_unique<::frc::Talon>(5), true);
691 drivetrain_writer.set_right_controller0(make_unique<::frc::Talon>(2),
692 false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700693
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700694 ::y2014::wpilib::ClawWriter claw_writer(&output_event_loop);
695 claw_writer.set_top_claw_talon(make_unique<::frc::Talon>(1));
696 claw_writer.set_bottom_claw_talon(make_unique<::frc::Talon>(0));
697 claw_writer.set_left_tusk_talon(make_unique<::frc::Talon>(4));
698 claw_writer.set_right_tusk_talon(make_unique<::frc::Talon>(3));
699 claw_writer.set_intake1_talon(make_unique<::frc::Talon>(7));
700 claw_writer.set_intake2_talon(make_unique<::frc::Talon>(8));
Brian Silverman17f503e2015-08-02 18:17:18 -0700701
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700702 ::y2014::wpilib::ShooterWriter shooter_writer(&output_event_loop);
703 shooter_writer.set_shooter_talon(make_unique<::frc::Talon>(6));
Brian Silverman17f503e2015-08-02 18:17:18 -0700704
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700705 AddLoop(&output_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700706
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700707 // Thread 6.
708 ::aos::ShmEventLoop solenoid_writer_event_loop;
Brian Silverman17f503e2015-08-02 18:17:18 -0700709 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
710 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700711 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700712 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
713 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
714 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
715 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700716
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700717 solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(25));
718 solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0));
719 AddLoop(&solenoid_writer_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700720
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700721 RunLoops();
Brian Silverman17f503e2015-08-02 18:17:18 -0700722 }
723};
724
725} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500726} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700727
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800728AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);