blob: 8f53ee2bdcf99741f561f383fa2ccf80e95dfb30 [file] [log] [blame]
James Kuszmaul7077d342021-06-09 20:23:58 -07001#include <inttypes.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07005
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Brian Silverman17f503e2015-08-02 18:17:18 -07007#include <functional>
James Kuszmaul7077d342021-06-09 20:23:58 -07008#include <mutex>
9#include <thread>
Brian Silverman17f503e2015-08-02 18:17:18 -070010
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"
Brian Silvermanf819b442019-01-20 16:51:04 -080025#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070026#include "aos/time/time.h"
27#include "aos/util/log_interval.h"
28#include "aos/util/phased_loop.h"
29#include "aos/util/wrapping_counter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070030#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
31#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070032#include "frc971/input/robot_state_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) {
James Kuszmaul7077d342021-06-09 20:23:58 -070066 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Brian Silverman17f503e2015-08-02 18:17:18 -070067 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080068 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070069}
70
Brian Silverman51091a02015-12-26 15:56:58 -080071double drivetrain_velocity_translate(double in) {
72 return (1.0 / in) / 256.0 /*cpr*/ *
73 constants::GetValues().drivetrain_encoder_ratio *
74 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
75}
76
James Kuszmaul7077d342021-06-09 20:23:58 -070077float hall_translate(const constants::DualHallShifterHallEffect &k,
78 float in_low, float in_high) {
Brian Silverman552350b2015-08-02 18:23:34 -070079 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070080 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
James Kuszmaul7077d342021-06-09 20:23:58 -070081 static_cast<float>(k.low_gear_middle -
82 k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070083 const float high_ratio =
James Kuszmaul7077d342021-06-09 20:23:58 -070084 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
85 static_cast<float>(k.shifter_hall_effect.high_gear_high -
86 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070087
Brian Silverman552350b2015-08-02 18:23:34 -070088 // Return low when we are below 1/2, and high when we are above 1/2.
89 if (low_ratio + high_ratio < 1.0) {
90 return low_ratio;
91 } else {
92 return high_ratio;
93 }
Brian Silverman17f503e2015-08-02 18:17:18 -070094}
95
96double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070097 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -070098 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -070099 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -0700100}
101
Brian Silverman552350b2015-08-02 18:23:34 -0700102double shooter_translate(int32_t in) {
103 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
104 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
105 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700106}
107
108static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700109 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800110 18.0 / 32.0 /* big belt reduction */ * 18.0 /
111 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
112 60.0 /* seconds / minute */ * 256.0 /* CPR */ * 4.0 /* edges / pulse */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700113
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800114class SensorReader : public ::frc971::wpilib::SensorReader {
Brian Silverman17f503e2015-08-02 18:17:18 -0700115 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800116 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuh7eed2de2019-05-25 14:34:40 -0700117 : ::frc971::wpilib::SensorReader(event_loop),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700118 auto_mode_sender_(
119 event_loop->MakeSender<::y2014::sensors::AutoMode>("/aos")),
120 shooter_position_sender_(
121 event_loop->MakeSender<shooter::Position>("/shooter")),
122 claw_position_sender_(event_loop->MakeSender<claw::Position>("/claw")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700123 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700124 event_loop
125 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
126 "/drivetrain")) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700127 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
128 // we should ever see.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800129 UpdateMediumEncoderFilterHz(kMaximumEncoderPulsesPerSecond);
Brian Silverman552350b2015-08-02 18:23:34 -0700130 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700131 }
132
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800133 ~SensorReader() override {
134 top_reader_.Quit();
135 bottom_reader_.Quit();
136 }
137
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700138 void set_auto_selector_analog(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700139 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700140 }
141
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700142 void set_high_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700143 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700144 }
145
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700146 void set_low_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700147 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700148 }
149
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700150 void set_high_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700151 high_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_low_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700155 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700156 }
157
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700158 void set_top_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800159 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700160 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700161 }
162
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700163 void set_top_claw_front_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700164 hall_filter_.Add(hall.get());
165 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700166 }
167
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700168 void set_top_claw_calibration_hall(
169 ::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700170 hall_filter_.Add(hall.get());
171 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700172 }
173
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700174 void set_top_claw_back_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700175 hall_filter_.Add(hall.get());
176 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700177 }
178
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700179 void set_bottom_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800180 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700181 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700182 }
183
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700184 void set_bottom_claw_front_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700185 hall_filter_.Add(hall.get());
186 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700187 }
188
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700189 void set_bottom_claw_calibration_hall(
190 ::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700191 hall_filter_.Add(hall.get());
192 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700193 }
194
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700195 void set_bottom_claw_back_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700196 hall_filter_.Add(hall.get());
197 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700198 }
199
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700200 void set_shooter_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800201 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700202 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700203 }
204
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700205 void set_shooter_proximal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700206 hall_filter_.Add(hall.get());
207 shooter_proximal_ = ::std::move(hall);
208 }
209
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700210 void set_shooter_distal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700211 hall_filter_.Add(hall.get());
212 shooter_distal_ = ::std::move(hall);
213 }
214
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700215 void set_shooter_plunger(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700216 hall_filter_.Add(hall.get());
217 shooter_plunger_ = ::std::move(hall);
218 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500219 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700220 }
221
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700222 void set_shooter_latch(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700223 hall_filter_.Add(hall.get());
224 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500225 shooter_latch_reader_ =
226 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700227 }
228
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800229 void Start() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500230 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700231 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500232 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700233 shooter_encoder_.get(), shooter_distal_.get());
234
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800235 AddToDMA(shooter_proximal_counter_.get());
236 AddToDMA(shooter_distal_counter_.get());
237 AddToDMA(shooter_plunger_reader_.get());
238 AddToDMA(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700239
Brian Silverman552350b2015-08-02 18:23:34 -0700240 top_reader_.Start();
241 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700242 }
243
244 void RunIteration() {
Brian Silverman552350b2015-08-02 18:23:34 -0700245 const auto &values = constants::GetValues();
246
Brian Silverman17f503e2015-08-02 18:17:18 -0700247 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 auto builder = drivetrain_position_sender_.MakeBuilder();
249 frc971::control_loops::drivetrain::Position::Builder position_builder =
250 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
251 position_builder.add_right_encoder(
252 drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
253 position_builder.add_left_encoder(
254 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
255 position_builder.add_left_speed(
256 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
257 position_builder.add_right_speed(drivetrain_velocity_translate(
258 drivetrain_right_encoder_->GetPeriod()));
Austin Schuha0c1e152015-11-08 14:10:13 -0800259
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260 const double low_left_hall = low_left_drive_hall_->GetVoltage();
261 const double high_left_hall = high_left_drive_hall_->GetVoltage();
262 position_builder.add_low_left_hall(low_left_hall);
263 position_builder.add_high_left_hall(high_left_hall);
264 position_builder.add_left_shifter_position(
265 hall_translate(values.left_drive, low_left_hall, high_left_hall));
Austin Schuha0c1e152015-11-08 14:10:13 -0800266
Alex Perrycb7da4b2019-08-28 19:35:56 -0700267 const double low_right_hall = low_right_drive_hall_->GetVoltage();
268 const double high_right_hall = high_right_drive_hall_->GetVoltage();
269 position_builder.add_low_right_hall(low_right_hall);
270 position_builder.add_high_right_hall(high_right_hall);
271 position_builder.add_right_shifter_position(
272 hall_translate(values.right_drive, low_right_hall, high_right_hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700273
Alex Perrycb7da4b2019-08-28 19:35:56 -0700274 builder.Send(position_builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700275 }
276
Austin Schuh7eed2de2019-05-25 14:34:40 -0700277 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700278 auto builder = auto_mode_sender_.MakeBuilder();
279 y2014::sensors::AutoMode::Builder auto_builder =
280 builder.MakeBuilder<y2014::sensors::AutoMode>();
281 auto_builder.add_voltage(auto_selector_analog_->GetVoltage());
282 builder.Send(auto_builder.Finish());
Austin Schuh7eed2de2019-05-25 14:34:40 -0700283 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800284 }
Brian Silverman552350b2015-08-02 18:23:34 -0700285
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800286 void RunDmaIteration() {
Brian Silverman17f503e2015-08-02 18:17:18 -0700287 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700288 auto builder = shooter_position_sender_.MakeBuilder();
289 ::frc971::PosedgeOnlyCountedHallEffectStructT pusher_proximal;
Brian Silverman552350b2015-08-02 18:23:34 -0700290 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700291 &pusher_proximal);
292 flatbuffers::Offset<::frc971::PosedgeOnlyCountedHallEffectStruct>
293 pusher_proximal_offset =
294 ::frc971::PosedgeOnlyCountedHallEffectStruct::Pack(
295 *builder.fbb(), &pusher_proximal);
Brian Silverman552350b2015-08-02 18:23:34 -0700296
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 ::frc971::PosedgeOnlyCountedHallEffectStructT pusher_distal;
298 CopyShooterPosedgeCounts(shooter_distal_counter_.get(), &pusher_distal);
299 flatbuffers::Offset<::frc971::PosedgeOnlyCountedHallEffectStruct>
300 pusher_distal_offset =
301 ::frc971::PosedgeOnlyCountedHallEffectStruct::Pack(
302 *builder.fbb(), &pusher_distal);
303
304 control_loops::shooter::Position::Builder position_builder =
305 builder.MakeBuilder<control_loops::shooter::Position>();
306 position_builder.add_position(
307 shooter_translate(shooter_encoder_->GetRaw()));
308 position_builder.add_plunger(!shooter_plunger_reader_->value());
309 position_builder.add_latch(!shooter_latch_reader_->value());
310 position_builder.add_pusher_distal(pusher_distal_offset);
311 position_builder.add_pusher_proximal(pusher_proximal_offset);
312
313 builder.Send(position_builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700314 }
315
316 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700317 auto builder = claw_position_sender_.MakeBuilder();
318 flatbuffers::Offset<control_loops::claw::HalfClawPosition> top_offset =
319 top_reader_.ReadPosition(builder.fbb());
320 flatbuffers::Offset<control_loops::claw::HalfClawPosition> bottom_offset =
321 bottom_reader_.ReadPosition(builder.fbb());
Brian Silverman552350b2015-08-02 18:23:34 -0700322
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323 control_loops::claw::Position::Builder position_builder =
324 builder.MakeBuilder<control_loops::claw::Position>();
325
326 position_builder.add_top(top_offset);
327 position_builder.add_bottom(bottom_offset);
328 builder.Send(position_builder.Finish());
Brian Silverman17f503e2015-08-02 18:17:18 -0700329 }
330 }
331
Brian Silverman17f503e2015-08-02 18:17:18 -0700332 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700333 class HalfClawReader {
334 public:
335 HalfClawReader(bool reversed) : reversed_(reversed) {}
336
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700337 void set_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Brian Silverman552350b2015-08-02 18:23:34 -0700338 encoder_ = ::std::move(encoder);
339 }
340
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700341 void set_front_hall(::std::unique_ptr<::frc::DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700342 front_hall_ = ::std::move(front_hall);
343 }
344
345 void set_calibration_hall(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700346 ::std::unique_ptr<::frc::DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700347 calibration_hall_ = ::std::move(calibration_hall);
348 }
349
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700350 void set_back_hall(::std::unique_ptr<::frc::DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700351 back_hall_ = ::std::move(back_hall);
352 }
353
354 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500355 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
356 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700357 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500358 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
359 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700360 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500361 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
362 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700363 synchronizer_.Add(back_counter_.get());
364 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500365 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
366 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700367 synchronizer_.Add(synchronized_encoder_.get());
368
369 synchronizer_.Start();
370 }
371
372 void Quit() { synchronizer_.Quit(); }
373
Alex Perrycb7da4b2019-08-28 19:35:56 -0700374 flatbuffers::Offset<control_loops::claw::HalfClawPosition> ReadPosition(
375 flatbuffers::FlatBufferBuilder *fbb) {
Brian Silverman552350b2015-08-02 18:23:34 -0700376 const double multiplier = reversed_ ? -1.0 : 1.0;
377
378 synchronizer_.RunIteration();
379
Alex Perrycb7da4b2019-08-28 19:35:56 -0700380 ::frc971::HallEffectStructT front;
381 CopyPosition(front_counter_.get(), &front);
382 flatbuffers::Offset<::frc971::HallEffectStruct> front_offset =
383 ::frc971::HallEffectStruct::Pack(*fbb, &front);
384
385 ::frc971::HallEffectStructT calibration;
386 CopyPosition(calibration_counter_.get(), &calibration);
387 flatbuffers::Offset<::frc971::HallEffectStruct> calibration_offset =
388 ::frc971::HallEffectStruct::Pack(*fbb, &calibration);
389
390 ::frc971::HallEffectStructT back;
391 CopyPosition(back_counter_.get(), &back);
392 flatbuffers::Offset<::frc971::HallEffectStruct> back_offset =
393 ::frc971::HallEffectStruct::Pack(*fbb, &back);
394
395 control_loops::claw::HalfClawPosition::Builder half_claw_position_builder(
396 *fbb);
397
398 half_claw_position_builder.add_front(front_offset);
399 half_claw_position_builder.add_calibration(calibration_offset);
400 half_claw_position_builder.add_back(back_offset);
401 half_claw_position_builder.add_position(
402 multiplier * claw_translate(synchronized_encoder_->get()));
403 return half_claw_position_builder.Finish();
Brian Silverman552350b2015-08-02 18:23:34 -0700404 }
405
406 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500407 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700408 ::frc971::HallEffectStructT *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700409 const double multiplier = reversed_ ? -1.0 : 1.0;
410
Austin Schuh5c25ab72015-11-01 13:17:11 -0800411 out->current = !counter->polled_value();
412 out->posedge_count = counter->negative_interrupt_count();
413 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700414 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800415 multiplier * claw_translate(counter->last_positive_encoder_value());
416 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700417 multiplier * claw_translate(counter->last_negative_encoder_value());
418 }
419
Brian Silverman5090c432016-01-02 14:44:26 -0800420 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700421
Brian Silvermanb601d892015-12-20 18:24:38 -0500422 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
423 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
424 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
425 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
426 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700427
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700428 ::std::unique_ptr<::frc::Encoder> encoder_;
429 ::std::unique_ptr<::frc::DigitalInput> front_hall_;
430 ::std::unique_ptr<::frc::DigitalInput> calibration_hall_;
431 ::std::unique_ptr<::frc::DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700432
433 const bool reversed_;
434 };
435
Brian Silvermanb601d892015-12-20 18:24:38 -0500436 void CopyShooterPosedgeCounts(
437 const ::frc971::wpilib::DMAEdgeCounter *counter,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700438 ::frc971::PosedgeOnlyCountedHallEffectStructT *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800439 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700440 // These are inverted because the hall effects give logical false when
441 // there's a magnet in front of them.
442 output->posedge_count = counter->negative_count();
443 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700444 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700445 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700446 }
447
Austin Schuh7eed2de2019-05-25 14:34:40 -0700448 ::aos::Sender<::y2014::sensors::AutoMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449 ::aos::Sender<shooter::Position> shooter_position_sender_;
450 ::aos::Sender<claw::Position> claw_position_sender_;
451 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700452 drivetrain_position_sender_;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700453
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700454 ::std::unique_ptr<::frc::AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700455
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700456 ::std::unique_ptr<::frc::AnalogInput> low_left_drive_hall_;
457 ::std::unique_ptr<::frc::AnalogInput> high_left_drive_hall_;
458 ::std::unique_ptr<::frc::AnalogInput> low_right_drive_hall_;
459 ::std::unique_ptr<::frc::AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700460
Brian Silverman552350b2015-08-02 18:23:34 -0700461 HalfClawReader top_reader_{false}, bottom_reader_{true};
462
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700463 ::std::unique_ptr<::frc::Encoder> shooter_encoder_;
464 ::std::unique_ptr<::frc::DigitalInput> shooter_proximal_, shooter_distal_;
465 ::std::unique_ptr<::frc::DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500466 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700467 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500468 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700469 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700470
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700471 ::frc::DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700472};
473
474class SolenoidWriter {
475 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800476 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700477 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700478 : pcm_(pcm),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700479 shooter_(event_loop->MakeFetcher<shooter::Output>("/shooter")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700480 drivetrain_(
481 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700482 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
483 "/drivetrain")),
484 pneumatics_to_log_sender_(
485 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700486 event_loop->set_name("Solenoids");
487 event_loop->SetRuntimeRealtimePriority(27);
Brian Silverman17f503e2015-08-02 18:17:18 -0700488
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700489 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
490 ::std::chrono::milliseconds(20),
491 ::std::chrono::milliseconds(1));
492 }
493
494 void set_pressure_switch(
495 ::std::unique_ptr<::frc::DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700496 pressure_switch_ = ::std::move(pressure_switch);
497 }
498
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700499 void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700500 compressor_relay_ = ::std::move(compressor_relay);
501 }
502
Brian Silvermanb601d892015-12-20 18:24:38 -0500503 void set_drivetrain_left(
504 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700505 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700506 }
507
Brian Silvermanb601d892015-12-20 18:24:38 -0500508 void set_drivetrain_right(
509 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700510 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700511 }
512
Brian Silvermanb601d892015-12-20 18:24:38 -0500513 void set_shooter_latch(
514 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700515 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700516 }
517
Brian Silvermanb601d892015-12-20 18:24:38 -0500518 void set_shooter_brake(
519 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700520 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700521 }
522
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700523 void Loop(const int iterations) {
524 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700525 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700526 }
Brian Silverman5090c432016-01-02 14:44:26 -0800527
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700528 {
529 shooter_.Fetch();
530 if (shooter_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700531 shooter_latch_->Set(!shooter_->latch_piston());
532 shooter_brake_->Set(!shooter_->brake_piston());
Brian Silverman17f503e2015-08-02 18:17:18 -0700533 }
534 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700535
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700536 {
537 drivetrain_.Fetch();
538 if (drivetrain_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700539 drivetrain_left_->Set(!drivetrain_->left_high());
540 drivetrain_right_->Set(!drivetrain_->right_high());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700541 }
542 }
543
544 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700545 auto builder = pneumatics_to_log_sender_.MakeBuilder();
546
547 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
548 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700549 {
550 const bool compressor_on = !pressure_switch_->Get();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700551 to_log_builder.add_compressor_on(compressor_on);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700552 if (compressor_on) {
553 compressor_relay_->Set(::frc::Relay::kForward);
554 } else {
555 compressor_relay_->Set(::frc::Relay::kOff);
556 }
557 }
558
559 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700560 to_log_builder.add_read_solenoids(pcm_->GetAll());
561 builder.Send(to_log_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700562 }
563 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700564
565 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500566 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700567
Brian Silvermanb601d892015-12-20 18:24:38 -0500568 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
569 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
570 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
571 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700572
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700573 ::std::unique_ptr<::frc::DigitalInput> pressure_switch_;
574 ::std::unique_ptr<::frc::Relay> compressor_relay_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700575
Alex Perrycb7da4b2019-08-28 19:35:56 -0700576 ::aos::Fetcher<shooter::Output> shooter_;
577 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_;
578
579 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700580};
581
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700582class ShooterWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700583 : public ::frc971::wpilib::LoopOutputHandler<shooter::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700584 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800585 ShooterWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700586 : ::frc971::wpilib::LoopOutputHandler<shooter::Output>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700587 event_loop, ".y2014.control_loops.shooter_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800588
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700589 void set_shooter_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700590 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700591 }
592
593 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700594 virtual void Write(const shooter::Output &output) override {
595 shooter_talon_->SetSpeed(output.voltage() / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700596 }
597
598 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700599 AOS_LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800600 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700601 }
602
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700603 ::std::unique_ptr<::frc::Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700604};
605
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606class ClawWriter : public ::frc971::wpilib::LoopOutputHandler<claw::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700607 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800608 ClawWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700609 : ::frc971::wpilib::LoopOutputHandler<claw::Output>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700610 event_loop, ".y2014.control_loops.claw_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800611
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700612 void set_top_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700613 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700614 }
615
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700616 void set_bottom_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700617 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700618 }
619
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700620 void set_left_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700621 left_tusk_talon_ = ::std::move(t);
622 }
623
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700624 void set_right_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700625 right_tusk_talon_ = ::std::move(t);
626 }
627
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700628 void set_intake1_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700629 intake1_talon_ = ::std::move(t);
630 }
631
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700632 void set_intake2_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700633 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700634 }
635
636 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700637 virtual void Write(const claw::Output &output) override {
638 intake1_talon_->SetSpeed(output.intake_voltage() / 12.0);
639 intake2_talon_->SetSpeed(output.intake_voltage() / 12.0);
640 bottom_claw_talon_->SetSpeed(-output.bottom_claw_voltage() / 12.0);
641 top_claw_talon_->SetSpeed(output.top_claw_voltage() / 12.0);
642 left_tusk_talon_->SetSpeed(output.tusk_voltage() / 12.0);
643 right_tusk_talon_->SetSpeed(-output.tusk_voltage() / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700644 }
645
646 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700647 AOS_LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800648 intake1_talon_->SetDisabled();
649 intake2_talon_->SetDisabled();
650 bottom_claw_talon_->SetDisabled();
651 top_claw_talon_->SetDisabled();
652 left_tusk_talon_->SetDisabled();
653 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700654 }
655
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700656 ::std::unique_ptr<::frc::Talon> top_claw_talon_;
657 ::std::unique_ptr<::frc::Talon> bottom_claw_talon_;
658 ::std::unique_ptr<::frc::Talon> left_tusk_talon_;
659 ::std::unique_ptr<::frc::Talon> right_tusk_talon_;
660 ::std::unique_ptr<::frc::Talon> intake1_talon_;
661 ::std::unique_ptr<::frc::Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700662};
663
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800664class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700665 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700666 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
667 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
668 ::frc::Encoder::k4X);
Brian Silverman17f503e2015-08-02 18:17:18 -0700669 }
Brian Silverman552350b2015-08-02 18:23:34 -0700670
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800671 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700672 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
673 aos::configuration::ReadConfig("config.json");
674
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700675 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700676 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700677 ::frc971::wpilib::JoystickSender joystick_sender(
678 &joystick_sender_event_loop);
679 AddLoop(&joystick_sender_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700680
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700681 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700682 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700683 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
684 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800685
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700686 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700687 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700688 SensorReader sensor_reader(&sensor_reader_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700689
Brian Silverman85fbb602015-08-29 19:28:20 -0700690 // Create this first to make sure it ends up in one of the lower-numbered
691 // FPGA slots so we can use it with DMA.
692 auto shooter_encoder_temp = make_encoder(2);
693
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700694 sensor_reader.set_auto_selector_analog(make_unique<::frc::AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700695
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700696 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
697 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
698 sensor_reader.set_high_left_drive_hall(make_unique<::frc::AnalogInput>(1));
699 sensor_reader.set_low_left_drive_hall(make_unique<::frc::AnalogInput>(0));
700 sensor_reader.set_high_right_drive_hall(make_unique<::frc::AnalogInput>(2));
701 sensor_reader.set_low_right_drive_hall(make_unique<::frc::AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700702
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700703 sensor_reader.set_top_claw_encoder(make_encoder(3));
704 sensor_reader.set_top_claw_front_hall(
705 make_unique<::frc::DigitalInput>(4)); // R2
706 sensor_reader.set_top_claw_calibration_hall(
707 make_unique<::frc::DigitalInput>(3)); // R3
708 sensor_reader.set_top_claw_back_hall(
709 make_unique<::frc::DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700710
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700711 sensor_reader.set_bottom_claw_encoder(make_encoder(4));
712 sensor_reader.set_bottom_claw_front_hall(
713 make_unique<::frc::DigitalInput>(1)); // L2
714 sensor_reader.set_bottom_claw_calibration_hall(
715 make_unique<::frc::DigitalInput>(0)); // L3
716 sensor_reader.set_bottom_claw_back_hall(
717 make_unique<::frc::DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700718
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700719 sensor_reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
720 sensor_reader.set_shooter_proximal(
721 make_unique<::frc::DigitalInput>(6)); // S1
722 sensor_reader.set_shooter_distal(
723 make_unique<::frc::DigitalInput>(7)); // S2
724 sensor_reader.set_shooter_plunger(
725 make_unique<::frc::DigitalInput>(8)); // S3
726 sensor_reader.set_shooter_latch(make_unique<::frc::DigitalInput>(9)); // S4
727 AddLoop(&sensor_reader_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700728
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700729 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700730 ::aos::ShmEventLoop gyro_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700731 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
732 AddLoop(&gyro_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700733
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700734 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700735 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700736 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
737 drivetrain_writer.set_left_controller0(make_unique<::frc::Talon>(5), true);
738 drivetrain_writer.set_right_controller0(make_unique<::frc::Talon>(2),
739 false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700740
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700741 ::y2014::wpilib::ClawWriter claw_writer(&output_event_loop);
742 claw_writer.set_top_claw_talon(make_unique<::frc::Talon>(1));
743 claw_writer.set_bottom_claw_talon(make_unique<::frc::Talon>(0));
744 claw_writer.set_left_tusk_talon(make_unique<::frc::Talon>(4));
745 claw_writer.set_right_tusk_talon(make_unique<::frc::Talon>(3));
746 claw_writer.set_intake1_talon(make_unique<::frc::Talon>(7));
747 claw_writer.set_intake2_talon(make_unique<::frc::Talon>(8));
Brian Silverman17f503e2015-08-02 18:17:18 -0700748
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700749 ::y2014::wpilib::ShooterWriter shooter_writer(&output_event_loop);
750 shooter_writer.set_shooter_talon(make_unique<::frc::Talon>(6));
Brian Silverman17f503e2015-08-02 18:17:18 -0700751
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700752 AddLoop(&output_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700753
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700754 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700755 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Brian Silverman17f503e2015-08-02 18:17:18 -0700756 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
757 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700758 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700759 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
760 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
761 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
762 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700763
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700764 solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(25));
765 solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0));
766 AddLoop(&solenoid_writer_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700767
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700768 RunLoops();
Brian Silverman17f503e2015-08-02 18:17:18 -0700769 }
770};
771
772} // namespace wpilib
Brian Silvermanb601d892015-12-20 18:24:38 -0500773} // namespace y2014
Brian Silverman17f503e2015-08-02 18:17:18 -0700774
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800775AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);