blob: 016b17afeffeb07099fa7955d4ea404e9913e45a [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include <unistd.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07002
Austin Schuh8aec1ed2016-05-01 13:29:20 -07003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
5#include <cstdio>
6#include <cstring>
Brian Silverman17f503e2015-08-02 18:17:18 -07007#include <functional>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008#include <memory>
James Kuszmaul7077d342021-06-09 20:23:58 -07009#include <mutex>
10#include <thread>
Brian Silverman17f503e2015-08-02 18:17:18 -070011
Parker Schuhd3b7a8872018-02-19 16:42:27 -080012#include "frc971/wpilib/ahal/AnalogInput.h"
13#include "frc971/wpilib/ahal/Compressor.h"
14#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
15#include "frc971/wpilib/ahal/DriverStation.h"
16#include "frc971/wpilib/ahal/Encoder.h"
17#include "frc971/wpilib/ahal/Relay.h"
18#include "frc971/wpilib/ahal/Talon.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080019#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070020#undef ERROR
21
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "aos/events/shm_event_loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080023#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070024#include "aos/logging/logging.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;
Vinay Sivae52a6b32021-07-10 15:19:26 -070056using std::make_unique;
Brian Silverman17f503e2015-08-02 18:17:18 -070057
Stephan Pleinesf63bde82024-01-13 15:59:33 -080058namespace y2014::wpilib {
Brian Silverman17f503e2015-08-02 18:17:18 -070059
Brian Silverman85fbb602015-08-29 19:28:20 -070060// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
61// DMA stuff and then removing the * 2.0 in *_translate.
62// The low bit is direction.
63
Brian Silverman17f503e2015-08-02 18:17:18 -070064double drivetrain_translate(int32_t in) {
James Kuszmaul7077d342021-06-09 20:23:58 -070065 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Brian Silverman17f503e2015-08-02 18:17:18 -070066 constants::GetValues().drivetrain_encoder_ratio *
Austin Schuh86f895e2015-11-08 13:40:51 -080067 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070068}
69
Brian Silverman51091a02015-12-26 15:56:58 -080070double drivetrain_velocity_translate(double in) {
71 return (1.0 / in) / 256.0 /*cpr*/ *
72 constants::GetValues().drivetrain_encoder_ratio *
73 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
74}
75
James Kuszmaul7077d342021-06-09 20:23:58 -070076float hall_translate(const constants::DualHallShifterHallEffect &k,
77 float in_low, float in_high) {
Brian Silverman552350b2015-08-02 18:23:34 -070078 const float low_ratio =
Sabina Davis415bb6c2017-10-16 23:30:52 -070079 0.5 * (in_low - static_cast<float>(k.shifter_hall_effect.low_gear_low)) /
James Kuszmaul7077d342021-06-09 20:23:58 -070080 static_cast<float>(k.low_gear_middle -
81 k.shifter_hall_effect.low_gear_low);
Brian Silverman552350b2015-08-02 18:23:34 -070082 const float high_ratio =
James Kuszmaul7077d342021-06-09 20:23:58 -070083 0.5 + 0.5 * (in_high - static_cast<float>(k.high_gear_middle)) /
84 static_cast<float>(k.shifter_hall_effect.high_gear_high -
85 k.high_gear_middle);
Brian Silverman17f503e2015-08-02 18:17:18 -070086
Brian Silverman552350b2015-08-02 18:23:34 -070087 // Return low when we are below 1/2, and high when we are above 1/2.
88 if (low_ratio + high_ratio < 1.0) {
89 return low_ratio;
90 } else {
91 return high_ratio;
92 }
Brian Silverman17f503e2015-08-02 18:17:18 -070093}
94
95double claw_translate(int32_t in) {
Brian Silverman85fbb602015-08-29 19:28:20 -070096 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) /
Brian Silverman552350b2015-08-02 18:23:34 -070097 (18.0 / 48.0 /*encoder gears*/) / (12.0 / 60.0 /*chain reduction*/) *
Brian Silverman85fbb602015-08-29 19:28:20 -070098 (M_PI / 180.0) * 2.0;
Brian Silverman17f503e2015-08-02 18:17:18 -070099}
100
Brian Silverman552350b2015-08-02 18:23:34 -0700101double shooter_translate(int32_t in) {
102 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
103 16 /*sprocket teeth*/ * 0.375 /*chain pitch*/
104 * (2.54 / 100.0 /*in to m*/);
Brian Silverman17f503e2015-08-02 18:17:18 -0700105}
106
107static const double kMaximumEncoderPulsesPerSecond =
Brian Silverman552350b2015-08-02 18:23:34 -0700108 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800109 18.0 / 32.0 /* big belt reduction */ * 18.0 /
110 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
111 60.0 /* seconds / minute */ * 256.0 /* CPR */ * 4.0 /* edges / pulse */;
Brian Silverman17f503e2015-08-02 18:17:18 -0700112
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800113class SensorReader : public ::frc971::wpilib::SensorReader {
Brian Silverman17f503e2015-08-02 18:17:18 -0700114 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800115 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuh7eed2de2019-05-25 14:34:40 -0700116 : ::frc971::wpilib::SensorReader(event_loop),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117 auto_mode_sender_(
118 event_loop->MakeSender<::y2014::sensors::AutoMode>("/aos")),
119 shooter_position_sender_(
120 event_loop->MakeSender<shooter::Position>("/shooter")),
121 claw_position_sender_(event_loop->MakeSender<claw::Position>("/claw")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700122 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123 event_loop
124 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
125 "/drivetrain")) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700126 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
127 // we should ever see.
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800128 UpdateMediumEncoderFilterHz(kMaximumEncoderPulsesPerSecond);
Brian Silverman552350b2015-08-02 18:23:34 -0700129 hall_filter_.SetPeriodNanoSeconds(100000);
Brian Silverman17f503e2015-08-02 18:17:18 -0700130 }
131
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800132 ~SensorReader() override {
133 top_reader_.Quit();
134 bottom_reader_.Quit();
135 }
136
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700137 void set_auto_selector_analog(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700138 auto_selector_analog_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700139 }
140
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700141 void set_high_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700142 high_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700143 }
144
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700145 void set_low_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700146 low_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700147 }
148
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700149 void set_high_right_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700150 high_right_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700151 }
152
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700153 void set_low_left_drive_hall(::std::unique_ptr<::frc::AnalogInput> analog) {
Brian Silverman552350b2015-08-02 18:23:34 -0700154 low_left_drive_hall_ = ::std::move(analog);
Brian Silverman17f503e2015-08-02 18:17:18 -0700155 }
156
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700157 void set_top_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800158 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700159 top_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700160 }
161
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700162 void set_top_claw_front_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700163 hall_filter_.Add(hall.get());
164 top_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700165 }
166
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700167 void set_top_claw_calibration_hall(
168 ::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700169 hall_filter_.Add(hall.get());
170 top_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700171 }
172
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700173 void set_top_claw_back_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700174 hall_filter_.Add(hall.get());
175 top_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700176 }
177
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700178 void set_bottom_claw_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800179 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700180 bottom_reader_.set_encoder(::std::move(encoder));
Brian Silverman17f503e2015-08-02 18:17:18 -0700181 }
182
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700183 void set_bottom_claw_front_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700184 hall_filter_.Add(hall.get());
185 bottom_reader_.set_front_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700186 }
187
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700188 void set_bottom_claw_calibration_hall(
189 ::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700190 hall_filter_.Add(hall.get());
191 bottom_reader_.set_calibration_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700192 }
193
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700194 void set_bottom_claw_back_hall(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700195 hall_filter_.Add(hall.get());
196 bottom_reader_.set_back_hall(::std::move(hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700197 }
198
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700199 void set_shooter_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800200 medium_encoder_filter_.Add(encoder.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700201 shooter_encoder_ = ::std::move(encoder);
Brian Silverman17f503e2015-08-02 18:17:18 -0700202 }
203
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700204 void set_shooter_proximal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700205 hall_filter_.Add(hall.get());
206 shooter_proximal_ = ::std::move(hall);
207 }
208
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700209 void set_shooter_distal(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700210 hall_filter_.Add(hall.get());
211 shooter_distal_ = ::std::move(hall);
212 }
213
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700214 void set_shooter_plunger(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700215 hall_filter_.Add(hall.get());
216 shooter_plunger_ = ::std::move(hall);
217 shooter_plunger_reader_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500218 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_plunger_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700219 }
220
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700221 void set_shooter_latch(::std::unique_ptr<::frc::DigitalInput> hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700222 hall_filter_.Add(hall.get());
223 shooter_latch_ = ::std::move(hall);
Brian Silvermanb601d892015-12-20 18:24:38 -0500224 shooter_latch_reader_ =
225 make_unique<::frc971::wpilib::DMADigitalReader>(shooter_latch_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700226 }
227
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800228 void Start() override {
Brian Silvermanb601d892015-12-20 18:24:38 -0500229 shooter_proximal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700230 shooter_encoder_.get(), shooter_proximal_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500231 shooter_distal_counter_ = make_unique<::frc971::wpilib::DMAEdgeCounter>(
Brian Silverman552350b2015-08-02 18:23:34 -0700232 shooter_encoder_.get(), shooter_distal_.get());
233
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800234 AddToDMA(shooter_proximal_counter_.get());
235 AddToDMA(shooter_distal_counter_.get());
236 AddToDMA(shooter_plunger_reader_.get());
237 AddToDMA(shooter_latch_reader_.get());
Brian Silverman17f503e2015-08-02 18:17:18 -0700238
Brian Silverman552350b2015-08-02 18:23:34 -0700239 top_reader_.Start();
240 bottom_reader_.Start();
Brian Silverman17f503e2015-08-02 18:17:18 -0700241 }
242
243 void RunIteration() {
Brian Silverman552350b2015-08-02 18:23:34 -0700244 const auto &values = constants::GetValues();
245
Brian Silverman17f503e2015-08-02 18:17:18 -0700246 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 auto builder = drivetrain_position_sender_.MakeBuilder();
248 frc971::control_loops::drivetrain::Position::Builder position_builder =
249 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
250 position_builder.add_right_encoder(
251 drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
252 position_builder.add_left_encoder(
253 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
254 position_builder.add_left_speed(
255 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
256 position_builder.add_right_speed(drivetrain_velocity_translate(
257 drivetrain_right_encoder_->GetPeriod()));
Austin Schuha0c1e152015-11-08 14:10:13 -0800258
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259 const double low_left_hall = low_left_drive_hall_->GetVoltage();
260 const double high_left_hall = high_left_drive_hall_->GetVoltage();
261 position_builder.add_low_left_hall(low_left_hall);
262 position_builder.add_high_left_hall(high_left_hall);
263 position_builder.add_left_shifter_position(
264 hall_translate(values.left_drive, low_left_hall, high_left_hall));
Austin Schuha0c1e152015-11-08 14:10:13 -0800265
Alex Perrycb7da4b2019-08-28 19:35:56 -0700266 const double low_right_hall = low_right_drive_hall_->GetVoltage();
267 const double high_right_hall = high_right_drive_hall_->GetVoltage();
268 position_builder.add_low_right_hall(low_right_hall);
269 position_builder.add_high_right_hall(high_right_hall);
270 position_builder.add_right_shifter_position(
271 hall_translate(values.right_drive, low_right_hall, high_right_hall));
Brian Silverman17f503e2015-08-02 18:17:18 -0700272
milind1f1dca32021-07-03 13:50:07 -0700273 builder.CheckOk(builder.Send(position_builder.Finish()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700274 }
275
Austin Schuh7eed2de2019-05-25 14:34:40 -0700276 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 auto builder = auto_mode_sender_.MakeBuilder();
278 y2014::sensors::AutoMode::Builder auto_builder =
279 builder.MakeBuilder<y2014::sensors::AutoMode>();
280 auto_builder.add_voltage(auto_selector_analog_->GetVoltage());
milind1f1dca32021-07-03 13:50:07 -0700281 builder.CheckOk(builder.Send(auto_builder.Finish()));
Austin Schuh7eed2de2019-05-25 14:34:40 -0700282 }
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 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700287 auto builder = shooter_position_sender_.MakeBuilder();
288 ::frc971::PosedgeOnlyCountedHallEffectStructT pusher_proximal;
Brian Silverman552350b2015-08-02 18:23:34 -0700289 CopyShooterPosedgeCounts(shooter_proximal_counter_.get(),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700290 &pusher_proximal);
291 flatbuffers::Offset<::frc971::PosedgeOnlyCountedHallEffectStruct>
292 pusher_proximal_offset =
293 ::frc971::PosedgeOnlyCountedHallEffectStruct::Pack(
294 *builder.fbb(), &pusher_proximal);
Brian Silverman552350b2015-08-02 18:23:34 -0700295
Alex Perrycb7da4b2019-08-28 19:35:56 -0700296 ::frc971::PosedgeOnlyCountedHallEffectStructT pusher_distal;
297 CopyShooterPosedgeCounts(shooter_distal_counter_.get(), &pusher_distal);
298 flatbuffers::Offset<::frc971::PosedgeOnlyCountedHallEffectStruct>
299 pusher_distal_offset =
300 ::frc971::PosedgeOnlyCountedHallEffectStruct::Pack(
301 *builder.fbb(), &pusher_distal);
302
303 control_loops::shooter::Position::Builder position_builder =
304 builder.MakeBuilder<control_loops::shooter::Position>();
305 position_builder.add_position(
306 shooter_translate(shooter_encoder_->GetRaw()));
307 position_builder.add_plunger(!shooter_plunger_reader_->value());
308 position_builder.add_latch(!shooter_latch_reader_->value());
309 position_builder.add_pusher_distal(pusher_distal_offset);
310 position_builder.add_pusher_proximal(pusher_proximal_offset);
311
milind1f1dca32021-07-03 13:50:07 -0700312 builder.CheckOk(builder.Send(position_builder.Finish()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700313 }
314
315 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700316 auto builder = claw_position_sender_.MakeBuilder();
317 flatbuffers::Offset<control_loops::claw::HalfClawPosition> top_offset =
318 top_reader_.ReadPosition(builder.fbb());
319 flatbuffers::Offset<control_loops::claw::HalfClawPosition> bottom_offset =
320 bottom_reader_.ReadPosition(builder.fbb());
Brian Silverman552350b2015-08-02 18:23:34 -0700321
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322 control_loops::claw::Position::Builder position_builder =
323 builder.MakeBuilder<control_loops::claw::Position>();
324
325 position_builder.add_top(top_offset);
326 position_builder.add_bottom(bottom_offset);
milind1f1dca32021-07-03 13:50:07 -0700327 builder.CheckOk(builder.Send(position_builder.Finish()));
Brian Silverman17f503e2015-08-02 18:17:18 -0700328 }
329 }
330
Brian Silverman17f503e2015-08-02 18:17:18 -0700331 private:
Brian Silverman552350b2015-08-02 18:23:34 -0700332 class HalfClawReader {
333 public:
334 HalfClawReader(bool reversed) : reversed_(reversed) {}
335
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700336 void set_encoder(::std::unique_ptr<::frc::Encoder> encoder) {
Brian Silverman552350b2015-08-02 18:23:34 -0700337 encoder_ = ::std::move(encoder);
338 }
339
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700340 void set_front_hall(::std::unique_ptr<::frc::DigitalInput> front_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700341 front_hall_ = ::std::move(front_hall);
342 }
343
344 void set_calibration_hall(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700345 ::std::unique_ptr<::frc::DigitalInput> calibration_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700346 calibration_hall_ = ::std::move(calibration_hall);
347 }
348
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700349 void set_back_hall(::std::unique_ptr<::frc::DigitalInput> back_hall) {
Brian Silverman552350b2015-08-02 18:23:34 -0700350 back_hall_ = ::std::move(back_hall);
351 }
352
353 void Start() {
Brian Silvermanb601d892015-12-20 18:24:38 -0500354 front_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
355 encoder_.get(), front_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700356 synchronizer_.Add(front_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500357 calibration_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
358 encoder_.get(), calibration_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700359 synchronizer_.Add(calibration_counter_.get());
Brian Silvermanb601d892015-12-20 18:24:38 -0500360 back_counter_ = make_unique<::frc971::wpilib::EdgeCounter>(
361 encoder_.get(), back_hall_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700362 synchronizer_.Add(back_counter_.get());
363 synchronized_encoder_ =
Brian Silvermanb601d892015-12-20 18:24:38 -0500364 make_unique<::frc971::wpilib::InterruptSynchronizedEncoder>(
365 encoder_.get());
Brian Silverman552350b2015-08-02 18:23:34 -0700366 synchronizer_.Add(synchronized_encoder_.get());
367
368 synchronizer_.Start();
369 }
370
371 void Quit() { synchronizer_.Quit(); }
372
Alex Perrycb7da4b2019-08-28 19:35:56 -0700373 flatbuffers::Offset<control_loops::claw::HalfClawPosition> ReadPosition(
374 flatbuffers::FlatBufferBuilder *fbb) {
Brian Silverman552350b2015-08-02 18:23:34 -0700375 const double multiplier = reversed_ ? -1.0 : 1.0;
376
377 synchronizer_.RunIteration();
378
Alex Perrycb7da4b2019-08-28 19:35:56 -0700379 ::frc971::HallEffectStructT front;
380 CopyPosition(front_counter_.get(), &front);
381 flatbuffers::Offset<::frc971::HallEffectStruct> front_offset =
382 ::frc971::HallEffectStruct::Pack(*fbb, &front);
383
384 ::frc971::HallEffectStructT calibration;
385 CopyPosition(calibration_counter_.get(), &calibration);
386 flatbuffers::Offset<::frc971::HallEffectStruct> calibration_offset =
387 ::frc971::HallEffectStruct::Pack(*fbb, &calibration);
388
389 ::frc971::HallEffectStructT back;
390 CopyPosition(back_counter_.get(), &back);
391 flatbuffers::Offset<::frc971::HallEffectStruct> back_offset =
392 ::frc971::HallEffectStruct::Pack(*fbb, &back);
393
394 control_loops::claw::HalfClawPosition::Builder half_claw_position_builder(
395 *fbb);
396
397 half_claw_position_builder.add_front(front_offset);
398 half_claw_position_builder.add_calibration(calibration_offset);
399 half_claw_position_builder.add_back(back_offset);
400 half_claw_position_builder.add_position(
401 multiplier * claw_translate(synchronized_encoder_->get()));
402 return half_claw_position_builder.Finish();
Brian Silverman552350b2015-08-02 18:23:34 -0700403 }
404
405 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500406 void CopyPosition(const ::frc971::wpilib::EdgeCounter *counter,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700407 ::frc971::HallEffectStructT *out) {
Brian Silverman552350b2015-08-02 18:23:34 -0700408 const double multiplier = reversed_ ? -1.0 : 1.0;
409
Austin Schuh5c25ab72015-11-01 13:17:11 -0800410 out->current = !counter->polled_value();
411 out->posedge_count = counter->negative_interrupt_count();
412 out->negedge_count = counter->positive_interrupt_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700413 out->negedge_value =
Austin Schuh5c25ab72015-11-01 13:17:11 -0800414 multiplier * claw_translate(counter->last_positive_encoder_value());
415 out->posedge_value =
Brian Silverman552350b2015-08-02 18:23:34 -0700416 multiplier * claw_translate(counter->last_negative_encoder_value());
417 }
418
Brian Silverman5090c432016-01-02 14:44:26 -0800419 ::frc971::wpilib::InterruptSynchronizer synchronizer_{55};
Brian Silverman552350b2015-08-02 18:23:34 -0700420
Brian Silvermanb601d892015-12-20 18:24:38 -0500421 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> front_counter_;
422 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> calibration_counter_;
423 ::std::unique_ptr<::frc971::wpilib::EdgeCounter> back_counter_;
424 ::std::unique_ptr<::frc971::wpilib::InterruptSynchronizedEncoder>
425 synchronized_encoder_;
Brian Silverman552350b2015-08-02 18:23:34 -0700426
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700427 ::std::unique_ptr<::frc::Encoder> encoder_;
428 ::std::unique_ptr<::frc::DigitalInput> front_hall_;
429 ::std::unique_ptr<::frc::DigitalInput> calibration_hall_;
430 ::std::unique_ptr<::frc::DigitalInput> back_hall_;
Brian Silverman552350b2015-08-02 18:23:34 -0700431
432 const bool reversed_;
433 };
434
Brian Silvermanb601d892015-12-20 18:24:38 -0500435 void CopyShooterPosedgeCounts(
436 const ::frc971::wpilib::DMAEdgeCounter *counter,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700437 ::frc971::PosedgeOnlyCountedHallEffectStructT *output) {
Austin Schuh5c25ab72015-11-01 13:17:11 -0800438 output->current = !counter->polled_value();
Brian Silverman85fbb602015-08-29 19:28:20 -0700439 // These are inverted because the hall effects give logical false when
440 // there's a magnet in front of them.
441 output->posedge_count = counter->negative_count();
442 output->negedge_count = counter->positive_count();
Brian Silverman552350b2015-08-02 18:23:34 -0700443 output->posedge_value =
Brian Silverman85fbb602015-08-29 19:28:20 -0700444 shooter_translate(counter->last_negative_encoder_value());
Brian Silverman552350b2015-08-02 18:23:34 -0700445 }
446
Austin Schuh7eed2de2019-05-25 14:34:40 -0700447 ::aos::Sender<::y2014::sensors::AutoMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700448 ::aos::Sender<shooter::Position> shooter_position_sender_;
449 ::aos::Sender<claw::Position> claw_position_sender_;
450 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700451 drivetrain_position_sender_;
Austin Schuh7eed2de2019-05-25 14:34:40 -0700452
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700453 ::std::unique_ptr<::frc::AnalogInput> auto_selector_analog_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700454
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700455 ::std::unique_ptr<::frc::AnalogInput> low_left_drive_hall_;
456 ::std::unique_ptr<::frc::AnalogInput> high_left_drive_hall_;
457 ::std::unique_ptr<::frc::AnalogInput> low_right_drive_hall_;
458 ::std::unique_ptr<::frc::AnalogInput> high_right_drive_hall_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700459
Brian Silverman552350b2015-08-02 18:23:34 -0700460 HalfClawReader top_reader_{false}, bottom_reader_{true};
461
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700462 ::std::unique_ptr<::frc::Encoder> shooter_encoder_;
463 ::std::unique_ptr<::frc::DigitalInput> shooter_proximal_, shooter_distal_;
464 ::std::unique_ptr<::frc::DigitalInput> shooter_plunger_, shooter_latch_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500465 ::std::unique_ptr<::frc971::wpilib::DMAEdgeCounter> shooter_proximal_counter_,
Brian Silverman552350b2015-08-02 18:23:34 -0700466 shooter_distal_counter_;
Brian Silvermanb601d892015-12-20 18:24:38 -0500467 ::std::unique_ptr<::frc971::wpilib::DMADigitalReader> shooter_plunger_reader_,
Brian Silverman552350b2015-08-02 18:23:34 -0700468 shooter_latch_reader_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700469
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700470 ::frc::DigitalGlitchFilter hall_filter_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700471};
472
473class SolenoidWriter {
474 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800475 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700476 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Brian Silverman17f503e2015-08-02 18:17:18 -0700477 : pcm_(pcm),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700478 shooter_(event_loop->MakeFetcher<shooter::Output>("/shooter")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700479 drivetrain_(
480 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700481 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
482 "/drivetrain")),
483 pneumatics_to_log_sender_(
484 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700485 event_loop->set_name("Solenoids");
486 event_loop->SetRuntimeRealtimePriority(27);
Brian Silverman17f503e2015-08-02 18:17:18 -0700487
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
489 ::std::chrono::milliseconds(20),
490 ::std::chrono::milliseconds(1));
491 }
492
493 void set_pressure_switch(
494 ::std::unique_ptr<::frc::DigitalInput> pressure_switch) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700495 pressure_switch_ = ::std::move(pressure_switch);
496 }
497
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700498 void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700499 compressor_relay_ = ::std::move(compressor_relay);
500 }
501
Brian Silvermanb601d892015-12-20 18:24:38 -0500502 void set_drivetrain_left(
503 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700504 drivetrain_left_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700505 }
506
Brian Silvermanb601d892015-12-20 18:24:38 -0500507 void set_drivetrain_right(
508 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700509 drivetrain_right_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700510 }
511
Brian Silvermanb601d892015-12-20 18:24:38 -0500512 void set_shooter_latch(
513 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700514 shooter_latch_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700515 }
516
Brian Silvermanb601d892015-12-20 18:24:38 -0500517 void set_shooter_brake(
518 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Brian Silverman552350b2015-08-02 18:23:34 -0700519 shooter_brake_ = ::std::move(s);
Brian Silverman17f503e2015-08-02 18:17:18 -0700520 }
521
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700522 void Loop(const int iterations) {
523 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700524 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700525 }
Brian Silverman5090c432016-01-02 14:44:26 -0800526
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700527 {
528 shooter_.Fetch();
529 if (shooter_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700530 shooter_latch_->Set(!shooter_->latch_piston());
531 shooter_brake_->Set(!shooter_->brake_piston());
Brian Silverman17f503e2015-08-02 18:17:18 -0700532 }
533 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700534
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700535 {
536 drivetrain_.Fetch();
537 if (drivetrain_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538 drivetrain_left_->Set(!drivetrain_->left_high());
539 drivetrain_right_->Set(!drivetrain_->right_high());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700540 }
541 }
542
543 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700544 auto builder = pneumatics_to_log_sender_.MakeBuilder();
545
546 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
547 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700548 {
549 const bool compressor_on = !pressure_switch_->Get();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700550 to_log_builder.add_compressor_on(compressor_on);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700551 if (compressor_on) {
552 compressor_relay_->Set(::frc::Relay::kForward);
553 } else {
554 compressor_relay_->Set(::frc::Relay::kOff);
555 }
556 }
557
558 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700559 to_log_builder.add_read_solenoids(pcm_->GetAll());
milind1f1dca32021-07-03 13:50:07 -0700560 (void)builder.Send(to_log_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700561 }
562 }
Brian Silverman17f503e2015-08-02 18:17:18 -0700563
564 private:
Brian Silvermanb601d892015-12-20 18:24:38 -0500565 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
Brian Silverman552350b2015-08-02 18:23:34 -0700566
Brian Silvermanb601d892015-12-20 18:24:38 -0500567 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_;
568 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_right_;
569 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_latch_;
570 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> shooter_brake_;
Brian Silverman552350b2015-08-02 18:23:34 -0700571
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700572 ::std::unique_ptr<::frc::DigitalInput> pressure_switch_;
573 ::std::unique_ptr<::frc::Relay> compressor_relay_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700574
Alex Perrycb7da4b2019-08-28 19:35:56 -0700575 ::aos::Fetcher<shooter::Output> shooter_;
576 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> drivetrain_;
577
578 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700579};
580
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700581class ShooterWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700582 : public ::frc971::wpilib::LoopOutputHandler<shooter::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700583 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800584 ShooterWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700585 : ::frc971::wpilib::LoopOutputHandler<shooter::Output>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700586 event_loop, ".y2014.control_loops.shooter_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800587
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700588 void set_shooter_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700589 shooter_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700590 }
591
592 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700593 virtual void Write(const shooter::Output &output) override {
594 shooter_talon_->SetSpeed(output.voltage() / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700595 }
596
597 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700598 AOS_LOG(WARNING, "shooter output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800599 shooter_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700600 }
601
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700602 ::std::unique_ptr<::frc::Talon> shooter_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700603};
604
Alex Perrycb7da4b2019-08-28 19:35:56 -0700605class ClawWriter : public ::frc971::wpilib::LoopOutputHandler<claw::Output> {
Brian Silverman17f503e2015-08-02 18:17:18 -0700606 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800607 ClawWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700608 : ::frc971::wpilib::LoopOutputHandler<claw::Output>(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700609 event_loop, ".y2014.control_loops.claw_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800610
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700611 void set_top_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700612 top_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700613 }
614
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700615 void set_bottom_claw_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700616 bottom_claw_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700617 }
618
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700619 void set_left_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700620 left_tusk_talon_ = ::std::move(t);
621 }
622
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700623 void set_right_tusk_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700624 right_tusk_talon_ = ::std::move(t);
625 }
626
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700627 void set_intake1_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700628 intake1_talon_ = ::std::move(t);
629 }
630
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700631 void set_intake2_talon(::std::unique_ptr<::frc::Talon> t) {
Brian Silverman552350b2015-08-02 18:23:34 -0700632 intake2_talon_ = ::std::move(t);
Brian Silverman17f503e2015-08-02 18:17:18 -0700633 }
634
635 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700636 virtual void Write(const claw::Output &output) override {
637 intake1_talon_->SetSpeed(output.intake_voltage() / 12.0);
638 intake2_talon_->SetSpeed(output.intake_voltage() / 12.0);
639 bottom_claw_talon_->SetSpeed(-output.bottom_claw_voltage() / 12.0);
640 top_claw_talon_->SetSpeed(output.top_claw_voltage() / 12.0);
641 left_tusk_talon_->SetSpeed(output.tusk_voltage() / 12.0);
642 right_tusk_talon_->SetSpeed(-output.tusk_voltage() / 12.0);
Brian Silverman17f503e2015-08-02 18:17:18 -0700643 }
644
645 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700646 AOS_LOG(WARNING, "claw output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800647 intake1_talon_->SetDisabled();
648 intake2_talon_->SetDisabled();
649 bottom_claw_talon_->SetDisabled();
650 top_claw_talon_->SetDisabled();
651 left_tusk_talon_->SetDisabled();
652 right_tusk_talon_->SetDisabled();
Brian Silverman17f503e2015-08-02 18:17:18 -0700653 }
654
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700655 ::std::unique_ptr<::frc::Talon> top_claw_talon_;
656 ::std::unique_ptr<::frc::Talon> bottom_claw_talon_;
657 ::std::unique_ptr<::frc::Talon> left_tusk_talon_;
658 ::std::unique_ptr<::frc::Talon> right_tusk_talon_;
659 ::std::unique_ptr<::frc::Talon> intake1_talon_;
660 ::std::unique_ptr<::frc::Talon> intake2_talon_;
Brian Silverman17f503e2015-08-02 18:17:18 -0700661};
662
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800663class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Brian Silverman17f503e2015-08-02 18:17:18 -0700664 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700665 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
666 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
667 ::frc::Encoder::k4X);
Brian Silverman17f503e2015-08-02 18:17:18 -0700668 }
Brian Silverman552350b2015-08-02 18:23:34 -0700669
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800670 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700671 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800672 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700673
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700674 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700675 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700676 ::frc971::wpilib::JoystickSender joystick_sender(
677 &joystick_sender_event_loop);
678 AddLoop(&joystick_sender_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700679
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700680 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700681 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700682 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
683 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800684
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700685 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700686 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700687 SensorReader sensor_reader(&sensor_reader_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700688
Brian Silverman85fbb602015-08-29 19:28:20 -0700689 // Create this first to make sure it ends up in one of the lower-numbered
690 // FPGA slots so we can use it with DMA.
691 auto shooter_encoder_temp = make_encoder(2);
692
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700693 sensor_reader.set_auto_selector_analog(make_unique<::frc::AnalogInput>(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700694
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700695 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
696 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
697 sensor_reader.set_high_left_drive_hall(make_unique<::frc::AnalogInput>(1));
698 sensor_reader.set_low_left_drive_hall(make_unique<::frc::AnalogInput>(0));
699 sensor_reader.set_high_right_drive_hall(make_unique<::frc::AnalogInput>(2));
700 sensor_reader.set_low_right_drive_hall(make_unique<::frc::AnalogInput>(3));
Brian Silverman17f503e2015-08-02 18:17:18 -0700701
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700702 sensor_reader.set_top_claw_encoder(make_encoder(3));
703 sensor_reader.set_top_claw_front_hall(
704 make_unique<::frc::DigitalInput>(4)); // R2
705 sensor_reader.set_top_claw_calibration_hall(
706 make_unique<::frc::DigitalInput>(3)); // R3
707 sensor_reader.set_top_claw_back_hall(
708 make_unique<::frc::DigitalInput>(5)); // R1
Brian Silverman17f503e2015-08-02 18:17:18 -0700709
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700710 sensor_reader.set_bottom_claw_encoder(make_encoder(4));
711 sensor_reader.set_bottom_claw_front_hall(
712 make_unique<::frc::DigitalInput>(1)); // L2
713 sensor_reader.set_bottom_claw_calibration_hall(
714 make_unique<::frc::DigitalInput>(0)); // L3
715 sensor_reader.set_bottom_claw_back_hall(
716 make_unique<::frc::DigitalInput>(2)); // L1
Brian Silverman17f503e2015-08-02 18:17:18 -0700717
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700718 sensor_reader.set_shooter_encoder(::std::move(shooter_encoder_temp));
719 sensor_reader.set_shooter_proximal(
720 make_unique<::frc::DigitalInput>(6)); // S1
721 sensor_reader.set_shooter_distal(
722 make_unique<::frc::DigitalInput>(7)); // S2
723 sensor_reader.set_shooter_plunger(
724 make_unique<::frc::DigitalInput>(8)); // S3
725 sensor_reader.set_shooter_latch(make_unique<::frc::DigitalInput>(9)); // S4
726 AddLoop(&sensor_reader_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700727
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700728 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700729 ::aos::ShmEventLoop gyro_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700730 ::frc971::wpilib::GyroSender gyro_sender(&gyro_event_loop);
731 AddLoop(&gyro_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700732
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700733 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700734 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700735 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
736 drivetrain_writer.set_left_controller0(make_unique<::frc::Talon>(5), true);
737 drivetrain_writer.set_right_controller0(make_unique<::frc::Talon>(2),
738 false);
Brian Silverman17f503e2015-08-02 18:17:18 -0700739
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700740 ::y2014::wpilib::ClawWriter claw_writer(&output_event_loop);
741 claw_writer.set_top_claw_talon(make_unique<::frc::Talon>(1));
742 claw_writer.set_bottom_claw_talon(make_unique<::frc::Talon>(0));
743 claw_writer.set_left_tusk_talon(make_unique<::frc::Talon>(4));
744 claw_writer.set_right_tusk_talon(make_unique<::frc::Talon>(3));
745 claw_writer.set_intake1_talon(make_unique<::frc::Talon>(7));
746 claw_writer.set_intake2_talon(make_unique<::frc::Talon>(8));
Brian Silverman17f503e2015-08-02 18:17:18 -0700747
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700748 ::y2014::wpilib::ShooterWriter shooter_writer(&output_event_loop);
749 shooter_writer.set_shooter_talon(make_unique<::frc::Talon>(6));
Brian Silverman17f503e2015-08-02 18:17:18 -0700750
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700751 AddLoop(&output_event_loop);
Brian Silverman552350b2015-08-02 18:23:34 -0700752
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700753 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700754 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Brian Silverman17f503e2015-08-02 18:17:18 -0700755 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
756 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700757 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Brian Silverman552350b2015-08-02 18:23:34 -0700758 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
759 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
760 solenoid_writer.set_shooter_latch(pcm->MakeSolenoid(5));
761 solenoid_writer.set_shooter_brake(pcm->MakeSolenoid(4));
Brian Silverman17f503e2015-08-02 18:17:18 -0700762
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700763 solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(25));
764 solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0));
765 AddLoop(&solenoid_writer_event_loop);
Brian Silverman17f503e2015-08-02 18:17:18 -0700766
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700767 RunLoops();
Brian Silverman17f503e2015-08-02 18:17:18 -0700768 }
769};
770
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800771} // namespace y2014::wpilib
Brian Silverman17f503e2015-08-02 18:17:18 -0700772
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800773AOS_ROBOT_CLASS(::y2014::wpilib::WPILibRobot);