blob: ae2ced0106db9c1c934d09b58f9fb68c181ae3db [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
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#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"
Brian Silvermanf819b442019-01-20 16:51:04 -080024#include "aos/make_unique.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025#include "aos/robot_state/robot_state_generated.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080026#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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070031#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
32#include "frc971/control_loops/drivetrain/drivetrain_position_generated.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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070043#include "frc971/wpilib/logging_generated.h"
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080044#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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070048#include "y2014/control_loops/claw/claw_output_generated.h"
49#include "y2014/control_loops/claw/claw_position_generated.h"
50#include "y2014/control_loops/shooter/shooter_output_generated.h"
51#include "y2014/control_loops/shooter/shooter_position_generated.h"
52#include "y2014/queues/auto_mode_generated.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070053
Alex Perrycb7da4b2019-08-28 19:35:56 -070054namespace claw = ::y2014::control_loops::claw;
55namespace shooter = ::y2014::control_loops::shooter;
Brian Silvermanf819b442019-01-20 16:51:04 -080056using aos::make_unique;
Brian Silverman17f503e2015-08-02 18:17:18 -070057
Brian Silvermanb601d892015-12-20 18:24:38 -050058namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070059namespace wpilib {
60
Brian Silverman85fbb602015-08-29 19:28:20 -070061// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
62// DMA stuff and then removing the * 2.0 in *_translate.
63// The low bit is direction.
64
Brian Silverman17f503e2015-08-02 18:17:18 -070065double drivetrain_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070066 return -static_cast<double>(in) /
Brian Silverman17f503e2015-08-02 18:17:18 -070067 (256.0 /*cpr*/ * 4.0 /*4x*/) *
68 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080069 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070070}
71
Brian Silverman51091a02015-12-26 15:56:58 -080072double drivetrain_velocity_translate(double in) {
73 return (1.0 / in) / 256.0 /*cpr*/ *
74 constants::GetValues().drivetrain_encoder_ratio *
75 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
76}
77
Sabina Davis415bb6c2017-10-16 23:30:52 -070078float hall_translate(const constants::DualHallShifterHallEffect &k, float in_low,
Brian Silverman552350b2015-08-02 18:23:34 -070079 float in_high) {
80 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070081 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
82 static_cast<float>(k.low_gear_middle - k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070083 const float high_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070084 0.5 +
85 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
86 static_cast<float>(k.shifter_hall_effect.high_gear_high -
87 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070088
Brian Silverman552350b2015-08-02 18:23:34 -070089 // Return low when we are below 1/2, and high when we are above 1/2.
90 if (low_ratio + high_ratio < 1.0) {
91 return low_ratio;
92 } else {
93 return high_ratio;
94 }
Brian Silverman17f503e2015-08-02 18:17:18 -070095}
96
97double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070098 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -070099 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -0700100 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700101}
102
Brian Silverman552350b2015-08-02 18:23:34 -0700103double shooter_translate(int32_t in) {
104 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
105 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
106 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700107}
108
109static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700110 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800111 18.0 / 32.0 /* big belt reduction */ * 18.0 /
112 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
113 60.0 /* seconds / minute */ * 256.0 /* CPR */ * 4.0 /* edges / pulse */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700114
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800115class SensorReader : public ::frc971::wpilib::SensorReader {
Brian Silverman17f503e2015-08-02 18:17:18 -0700116 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800117 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuh7eed2de2019-05-25 14:34:40 -0700118 : ::frc971::wpilib::SensorReader(event_loop),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700119 auto_mode_sender_(
120 event_loop->MakeSender<::y2014::sensors::AutoMode>("/aos")),
121 shooter_position_sender_(
122 event_loop->MakeSender<shooter::Position>("/shooter")),
123 claw_position_sender_(event_loop->MakeSender<claw::Position>("/claw")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700124 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700125 event_loop
126 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
127 "/drivetrain")) {
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 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 auto builder = drivetrain_position_sender_.MakeBuilder();
250 frc971::control_loops::drivetrain::Position::Builder position_builder =
251 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
252 position_builder.add_right_encoder(
253 drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
254 position_builder.add_left_encoder(
255 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
256 position_builder.add_left_speed(
257 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
258 position_builder.add_right_speed(drivetrain_velocity_translate(
259 drivetrain_right_encoder_->GetPeriod()));
Austin Schuha0c1e152015-11-08 14:10:13 -0800260
Alex Perrycb7da4b2019-08-28 19:35:56 -0700261 const double low_left_hall = low_left_drive_hall_->GetVoltage();
262 const double high_left_hall = high_left_drive_hall_->GetVoltage();
263 position_builder.add_low_left_hall(low_left_hall);
264 position_builder.add_high_left_hall(high_left_hall);
265 position_builder.add_left_shifter_position(
266 hall_translate(values.left_drive, low_left_hall, high_left_hall));
Austin Schuha0c1e152015-11-08 14:10:13 -0800267
Alex Perrycb7da4b2019-08-28 19:35:56 -0700268 const double low_right_hall = low_right_drive_hall_->GetVoltage();
269 const double high_right_hall = high_right_drive_hall_->GetVoltage();
270 position_builder.add_low_right_hall(low_right_hall);
271 position_builder.add_high_right_hall(high_right_hall);
272 position_builder.add_right_shifter_position(
273 hall_translate(values.right_drive, low_right_hall, high_right_hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700274
Alex Perrycb7da4b2019-08-28 19:35:56 -0700275 builder.Send(position_builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700276 }
277
Austin Schuh7eed2de2019-05-25 14:34:40 -0700278 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700279 auto builder = auto_mode_sender_.MakeBuilder();
280 y2014::sensors::AutoMode::Builder auto_builder =
281 builder.MakeBuilder<y2014::sensors::AutoMode>();
282 auto_builder.add_voltage(auto_selector_analog_->GetVoltage());
283 builder.Send(auto_builder.Finish());
Austin Schuh7eed2de2019-05-25 14:34:40 -0700284 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800285 }
Brian Silverman552350b2015-08-02 18:23:34 -0700286
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800287 void RunDmaIteration() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700288 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700289 auto builder = shooter_position_sender_.MakeBuilder();
290 ::frc971::PosedgeOnlyCountedHallEffectStructT pusher_proximal;
Brian Silverman552350b2015-08-02 18:23:34 -0700291 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700292 &pusher_proximal);
293 flatbuffers::Offset<::frc971::PosedgeOnlyCountedHallEffectStruct>
294 pusher_proximal_offset =
295 ::frc971::PosedgeOnlyCountedHallEffectStruct::Pack(
296 *builder.fbb(), &pusher_proximal);
Brian Silverman552350b2015-08-02 18:23:34 -0700297
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 ::frc971::PosedgeOnlyCountedHallEffectStructT pusher_distal;
299 CopyShooterPosedgeCounts(shooter_distal_counter_.get(), &pusher_distal);
300 flatbuffers::Offset<::frc971::PosedgeOnlyCountedHallEffectStruct>
301 pusher_distal_offset =
302 ::frc971::PosedgeOnlyCountedHallEffectStruct::Pack(
303 *builder.fbb(), &pusher_distal);
304
305 control_loops::shooter::Position::Builder position_builder =
306 builder.MakeBuilder<control_loops::shooter::Position>();
307 position_builder.add_position(
308 shooter_translate(shooter_encoder_->GetRaw()));
309 position_builder.add_plunger(!shooter_plunger_reader_->value());
310 position_builder.add_latch(!shooter_latch_reader_->value());
311 position_builder.add_pusher_distal(pusher_distal_offset);
312 position_builder.add_pusher_proximal(pusher_proximal_offset);
313
314 builder.Send(position_builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700315 }
316
317 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700318 auto builder = claw_position_sender_.MakeBuilder();
319 flatbuffers::Offset<control_loops::claw::HalfClawPosition> top_offset =
320 top_reader_.ReadPosition(builder.fbb());
321 flatbuffers::Offset<control_loops::claw::HalfClawPosition> bottom_offset =
322 bottom_reader_.ReadPosition(builder.fbb());
Brian Silverman552350b2015-08-02 18:23:34 -0700323
Alex Perrycb7da4b2019-08-28 19:35:56 -0700324 control_loops::claw::Position::Builder position_builder =
325 builder.MakeBuilder<control_loops::claw::Position>();
326
327 position_builder.add_top(top_offset);
328 position_builder.add_bottom(bottom_offset);
329 builder.Send(position_builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700330 }
331 }
332
Brian Silverman17f503e2015-08-02 18:17:18 -0700333 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700334 class HalfClawReader {
335 public:
336 HalfClawReader(bool reversed) : reversed_(reversed) {}
337
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700338 void set_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Brian Silverman552350b2015-08-02 18:23:34 -0700339 encoder_ = ::std::move(encoder);
340 }
341
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700342 void set_front_hall(::std::unique_ptr<::frc::DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700343 front_hall_ = ::std::move(front_hall);
344 }
345
346 void set_calibration_hall(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700347 ::std::unique_ptr<::frc::DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700348 calibration_hall_ = ::std::move(calibration_hall);
349 }
350
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700351 void set_back_hall(::std::unique_ptr<::frc::DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700352 back_hall_ = ::std::move(back_hall);
353 }
354
355 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500356 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
357 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700358 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500359 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
360 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700361 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500362 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
363 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700364 synchronizer_.Add(back_counter_.get());
365 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500366 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
367 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700368 synchronizer_.Add(synchronized_encoder_.get());
369
370 synchronizer_.Start();
371 }
372
373 void Quit() { synchronizer_.Quit(); }
374
Alex Perrycb7da4b2019-08-28 19:35:56 -0700375 flatbuffers::Offset<control_loops::claw::HalfClawPosition> ReadPosition(
376 flatbuffers::FlatBufferBuilder *fbb) {
Brian Silverman552350b2015-08-02 18:23:34 -0700377 const double multiplier = reversed_ ? -1.0 : 1.0;
378
379 synchronizer_.RunIteration();
380
Alex Perrycb7da4b2019-08-28 19:35:56 -0700381
382 ::frc971::HallEffectStructT front;
383 CopyPosition(front_counter_.get(), &front);
384 flatbuffers::Offset<::frc971::HallEffectStruct> front_offset =
385 ::frc971::HallEffectStruct::Pack(*fbb, &front);
386
387 ::frc971::HallEffectStructT calibration;
388 CopyPosition(calibration_counter_.get(), &calibration);
389 flatbuffers::Offset<::frc971::HallEffectStruct> calibration_offset =
390 ::frc971::HallEffectStruct::Pack(*fbb, &calibration);
391
392 ::frc971::HallEffectStructT back;
393 CopyPosition(back_counter_.get(), &back);
394 flatbuffers::Offset<::frc971::HallEffectStruct> back_offset =
395 ::frc971::HallEffectStruct::Pack(*fbb, &back);
396
397 control_loops::claw::HalfClawPosition::Builder half_claw_position_builder(
398 *fbb);
399
400 half_claw_position_builder.add_front(front_offset);
401 half_claw_position_builder.add_calibration(calibration_offset);
402 half_claw_position_builder.add_back(back_offset);
403 half_claw_position_builder.add_position(
404 multiplier * claw_translate(synchronized_encoder_->get()));
405 return half_claw_position_builder.Finish();
Brian Silverman552350b2015-08-02 18:23:34 -0700406 }
407
408 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500409 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700410 ::frc971::HallEffectStructT *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700411 const double multiplier = reversed_ ? -1.0 : 1.0;
412
Austin Schuh5c25ab72015-11-01 13:17:11 -0800413 out->current = !counter->polled_value();
414 out->posedge_count = counter->negative_interrupt_count();
415 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700416 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800417 multiplier * claw_translate(counter->last_positive_encoder_value());
418 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700419 multiplier * claw_translate(counter->last_negative_encoder_value());
420 }
421
Brian Silverman5090c432016-01-02 14:44:26 -0800422 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700423
Brian Silvermanb601d892015-12-20 18:24:38 -0500424 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
425 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
426 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
427 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
428 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700429
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700430 ::std::unique_ptr<::frc::Encoder> encoder_;
431 ::std::unique_ptr<::frc::DigitalInput> front_hall_;
432 ::std::unique_ptr<::frc::DigitalInput> calibration_hall_;
433 ::std::unique_ptr<::frc::DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700434
435 const bool reversed_;
436 };
437
Brian Silvermanb601d892015-12-20 18:24:38 -0500438 void CopyShooterPosedgeCounts(
439 const ::frc971::wpilib::DMAEdgeCounter *counter,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700440 ::frc971::PosedgeOnlyCountedHallEffectStructT *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800441 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700442 // These are inverted because the hall effects give logical false when
443 // there's a magnet in front of them.
444 output->posedge_count = counter->negative_count();
445 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700446 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700447 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700448 }
449
Austin Schuh7eed2de2019-05-25 14:34:40 -0700450 ::aos::Sender<::y2014::sensors::AutoMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451 ::aos::Sender<shooter::Position> shooter_position_sender_;
452 ::aos::Sender<claw::Position> claw_position_sender_;
453 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700454 drivetrain_position_sender_;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700455
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700456 ::std::unique_ptr<::frc::AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700457
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700458 ::std::unique_ptr<::frc::AnalogInput> low_left_drive_hall_;
459 ::std::unique_ptr<::frc::AnalogInput> high_left_drive_hall_;
460 ::std::unique_ptr<::frc::AnalogInput> low_right_drive_hall_;
461 ::std::unique_ptr<::frc::AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700462
Brian Silverman552350b2015-08-02 18:23:34 -0700463 HalfClawReader top_reader_{false}, bottom_reader_{true};
464
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700465 ::std::unique_ptr<::frc::Encoder> shooter_encoder_;
466 ::std::unique_ptr<::frc::DigitalInput> shooter_proximal_, shooter_distal_;
467 ::std::unique_ptr<::frc::DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500468 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700469 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500470 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700471 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700472
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700473 ::frc::DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700474};
475
476class SolenoidWriter {
477 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800478 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700479 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700480 : pcm_(pcm),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700481 shooter_(event_loop->MakeFetcher<shooter::Output>("/shooter")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700482 drivetrain_(
483 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700484 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
485 "/drivetrain")),
486 pneumatics_to_log_sender_(
487 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488 event_loop->set_name("Solenoids");
489 event_loop->SetRuntimeRealtimePriority(27);
Brian Silverman17f503e2015-08-02 18:17:18 -0700490
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700491 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
492 ::std::chrono::milliseconds(20),
493 ::std::chrono::milliseconds(1));
494 }
495
496 void set_pressure_switch(
497 ::std::unique_ptr<::frc::DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700498 pressure_switch_ = ::std::move(pressure_switch);
499 }
500
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700501 void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700502 compressor_relay_ = ::std::move(compressor_relay);
503 }
504
Brian Silvermanb601d892015-12-20 18:24:38 -0500505 void set_drivetrain_left(
506 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700507 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700508 }
509
Brian Silvermanb601d892015-12-20 18:24:38 -0500510 void set_drivetrain_right(
511 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700512 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700513 }
514
Brian Silvermanb601d892015-12-20 18:24:38 -0500515 void set_shooter_latch(
516 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700517 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700518 }
519
Brian Silvermanb601d892015-12-20 18:24:38 -0500520 void set_shooter_brake(
521 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700522 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700523 }
524
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700525 void Loop(const int iterations) {
526 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700527 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700528 }
Brian Silverman5090c432016-01-02 14:44:26 -0800529
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700530 {
531 shooter_.Fetch();
532 if (shooter_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700533 shooter_latch_->Set(!shooter_->latch_piston());
534 shooter_brake_->Set(!shooter_->brake_piston());
Brian Silverman17f503e2015-08-02 18:17:18 -0700535 }
536 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700537
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700538 {
539 drivetrain_.Fetch();
540 if (drivetrain_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700541 drivetrain_left_->Set(!drivetrain_->left_high());
542 drivetrain_right_->Set(!drivetrain_->right_high());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700543 }
544 }
545
546 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700547 auto builder = pneumatics_to_log_sender_.MakeBuilder();
548
549 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
550 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700551 {
552 const bool compressor_on = !pressure_switch_->Get();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700553 to_log_builder.add_compressor_on(compressor_on);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700554 if (compressor_on) {
555 compressor_relay_->Set(::frc::Relay::kForward);
556 } else {
557 compressor_relay_->Set(::frc::Relay::kOff);
558 }
559 }
560
561 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700562 to_log_builder.add_read_solenoids(pcm_->GetAll());
563 builder.Send(to_log_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700564 }
565 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700566
567 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500568 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700569
Brian Silvermanb601d892015-12-20 18:24:38 -0500570 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
571 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
572 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
573 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700574
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700575 ::std::unique_ptr<::frc::DigitalInput> pressure_switch_;
576 ::std::unique_ptr<::frc::Relay> compressor_relay_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700577
Alex Perrycb7da4b2019-08-28 19:35:56 -0700578 ::aos::Fetcher<shooter::Output> shooter_;
579 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_;
580
581 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700582};
583
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700584class ShooterWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700585 : public ::frc971::wpilib::LoopOutputHandler<shooter::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700586 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800587 ShooterWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700588 : ::frc971::wpilib::LoopOutputHandler<shooter::Output>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700589 event_loop, ".y2014.control_loops.shooter_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800590
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700591 void set_shooter_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700592 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700593 }
594
595 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700596 virtual void Write(const shooter::Output &output) override {
597 shooter_talon_->SetSpeed(output.voltage() / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700598 }
599
600 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700601 AOS_LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800602 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700603 }
604
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700605 ::std::unique_ptr<::frc::Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700606};
607
Alex Perrycb7da4b2019-08-28 19:35:56 -0700608class ClawWriter : public ::frc971::wpilib::LoopOutputHandler<claw::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700609 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800610 ClawWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611 : ::frc971::wpilib::LoopOutputHandler<claw::Output>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700612 event_loop, ".y2014.control_loops.claw_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800613
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700614 void set_top_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700615 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700616 }
617
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700618 void set_bottom_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700619 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700620 }
621
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700622 void set_left_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700623 left_tusk_talon_ = ::std::move(t);
624 }
625
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700626 void set_right_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700627 right_tusk_talon_ = ::std::move(t);
628 }
629
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700630 void set_intake1_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700631 intake1_talon_ = ::std::move(t);
632 }
633
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700634 void set_intake2_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700635 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700636 }
637
638 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700639 virtual void Write(const claw::Output &output) override {
640 intake1_talon_->SetSpeed(output.intake_voltage() / 12.0);
641 intake2_talon_->SetSpeed(output.intake_voltage() / 12.0);
642 bottom_claw_talon_->SetSpeed(-output.bottom_claw_voltage() / 12.0);
643 top_claw_talon_->SetSpeed(output.top_claw_voltage() / 12.0);
644 left_tusk_talon_->SetSpeed(output.tusk_voltage() / 12.0);
645 right_tusk_talon_->SetSpeed(-output.tusk_voltage() / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700646 }
647
648 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700649 AOS_LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800650 intake1_talon_->SetDisabled();
651 intake2_talon_->SetDisabled();
652 bottom_claw_talon_->SetDisabled();
653 top_claw_talon_->SetDisabled();
654 left_tusk_talon_->SetDisabled();
655 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700656 }
657
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700658 ::std::unique_ptr<::frc::Talon> top_claw_talon_;
659 ::std::unique_ptr<::frc::Talon> bottom_claw_talon_;
660 ::std::unique_ptr<::frc::Talon> left_tusk_talon_;
661 ::std::unique_ptr<::frc::Talon> right_tusk_talon_;
662 ::std::unique_ptr<::frc::Talon> intake1_talon_;
663 ::std::unique_ptr<::frc::Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700664};
665
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800666class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700667 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700668 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
669 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
670 ::frc::Encoder::k4X);
Brian Silverman17f503e2015-08-02 18:17:18 -0700671 }
Brian Silverman552350b2015-08-02 18:23:34 -0700672
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800673 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700674 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
675 aos::configuration::ReadConfig("config.json");
676
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700677 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700678 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700679 ::frc971::wpilib::JoystickSender joystick_sender(
680 &joystick_sender_event_loop);
681 AddLoop(&joystick_sender_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700682
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700683 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700684 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700685 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
686 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800687
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700688 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700689 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700690 SensorReader sensor_reader(&sensor_reader_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700691
Brian Silverman85fbb602015-08-29 19:28:20 -0700692 // Create this first to make sure it ends up in one of the lower-numbered
693 // FPGA slots so we can use it with DMA.
694 auto shooter_encoder_temp = make_encoder(2);
695
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700696 sensor_reader.set_auto_selector_analog(make_unique<::frc::AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700697
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700698 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
699 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
700 sensor_reader.set_high_left_drive_hall(make_unique<::frc::AnalogInput>(1));
701 sensor_reader.set_low_left_drive_hall(make_unique<::frc::AnalogInput>(0));
702 sensor_reader.set_high_right_drive_hall(make_unique<::frc::AnalogInput>(2));
703 sensor_reader.set_low_right_drive_hall(make_unique<::frc::AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700704
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700705 sensor_reader.set_top_claw_encoder(make_encoder(3));
706 sensor_reader.set_top_claw_front_hall(
707 make_unique<::frc::DigitalInput>(4)); // R2
708 sensor_reader.set_top_claw_calibration_hall(
709 make_unique<::frc::DigitalInput>(3)); // R3
710 sensor_reader.set_top_claw_back_hall(
711 make_unique<::frc::DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700712
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700713 sensor_reader.set_bottom_claw_encoder(make_encoder(4));
714 sensor_reader.set_bottom_claw_front_hall(
715 make_unique<::frc::DigitalInput>(1)); // L2
716 sensor_reader.set_bottom_claw_calibration_hall(
717 make_unique<::frc::DigitalInput>(0)); // L3
718 sensor_reader.set_bottom_claw_back_hall(
719 make_unique<::frc::DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700720
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700721 sensor_reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
722 sensor_reader.set_shooter_proximal(
723 make_unique<::frc::DigitalInput>(6)); // S1
724 sensor_reader.set_shooter_distal(
725 make_unique<::frc::DigitalInput>(7)); // S2
726 sensor_reader.set_shooter_plunger(
727 make_unique<::frc::DigitalInput>(8)); // S3
728 sensor_reader.set_shooter_latch(make_unique<::frc::DigitalInput>(9)); // S4
729 AddLoop(&sensor_reader_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700730
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700731 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700732 ::aos::ShmEventLoop gyro_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700733 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
734 AddLoop(&gyro_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700735
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700736 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700737 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700738 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
739 drivetrain_writer.set_left_controller0(make_unique<::frc::Talon>(5), true);
740 drivetrain_writer.set_right_controller0(make_unique<::frc::Talon>(2),
741 false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700742
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700743 ::y2014::wpilib::ClawWriter claw_writer(&output_event_loop);
744 claw_writer.set_top_claw_talon(make_unique<::frc::Talon>(1));
745 claw_writer.set_bottom_claw_talon(make_unique<::frc::Talon>(0));
746 claw_writer.set_left_tusk_talon(make_unique<::frc::Talon>(4));
747 claw_writer.set_right_tusk_talon(make_unique<::frc::Talon>(3));
748 claw_writer.set_intake1_talon(make_unique<::frc::Talon>(7));
749 claw_writer.set_intake2_talon(make_unique<::frc::Talon>(8));
Brian Silverman17f503e2015-08-02 18:17:18 -0700750
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700751 ::y2014::wpilib::ShooterWriter shooter_writer(&output_event_loop);
752 shooter_writer.set_shooter_talon(make_unique<::frc::Talon>(6));
Brian Silverman17f503e2015-08-02 18:17:18 -0700753
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700754 AddLoop(&output_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700755
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700756 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700757 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Brian Silverman17f503e2015-08-02 18:17:18 -0700758 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
759 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700760 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700761 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
762 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
763 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
764 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700765
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700766 solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(25));
767 solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0));
768 AddLoop(&solenoid_writer_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700769
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700770 RunLoops();
Brian Silverman17f503e2015-08-02 18:17:18 -0700771 }
772};
773
774} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500775} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700776
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800777AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);