blob: 1fd72e898dd95fbe6b121708b68bfba61a44ff8c [file] [log] [blame]
Austin Schuh2a3e0632018-02-19 16:24:49 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
Austin Schuh2a3e0632018-02-19 16:24:49 -08006#include <cmath>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <cstdio>
8#include <cstring>
Austin Schuh2a3e0632018-02-19 16:24:49 -08009#include <functional>
Vinay Sivae52a6b32021-07-10 15:19:26 -070010#include <memory>
Tyler Chatowbf0609c2021-07-31 16:13:27 -070011#include <thread>
Austin Schuh2a3e0632018-02-19 16:24:49 -080012
Tyler Chatowbf0609c2021-07-31 16:13:27 -070013#include "ctre/phoenix/CANifier.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080014#include "frc971/wpilib/ahal/AnalogInput.h"
Austin Schuh9950f682021-11-06 15:27:58 -070015#include "frc971/wpilib/ahal/Compressor.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080016#include "frc971/wpilib/ahal/Counter.h"
17#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
18#include "frc971/wpilib/ahal/DriverStation.h"
19#include "frc971/wpilib/ahal/Encoder.h"
20#include "frc971/wpilib/ahal/Relay.h"
21#include "frc971/wpilib/ahal/Servo.h"
22#include "frc971/wpilib/ahal/VictorSP.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080023#undef ERROR
24
John Park33858a32018-09-28 23:05:48 -070025#include "aos/commonmath.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070026#include "aos/events/shm_event_loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080027#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070028#include "aos/logging/logging.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/time/time.h"
30#include "aos/util/compiler_memory_barrier.h"
31#include "aos/util/log_interval.h"
32#include "aos/util/phased_loop.h"
33#include "aos/util/wrapping_counter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070034#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
35#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080036#include "frc971/wpilib/ADIS16448.h"
37#include "frc971/wpilib/buffered_pcm.h"
38#include "frc971/wpilib/buffered_solenoid.h"
39#include "frc971/wpilib/dma.h"
40#include "frc971/wpilib/dma_edge_counting.h"
Sabina Daviscaa2a6b2019-02-03 01:15:37 -080041#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080042#include "frc971/wpilib/encoder_and_potentiometer.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080043#include "frc971/wpilib/joystick_sender.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070044#include "frc971/wpilib/logging_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080045#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/pdp_fetcher.h"
Austin Schuh6abf5b72019-02-02 20:20:54 -080047#include "frc971/wpilib/sensor_reader.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080048#include "frc971/wpilib/wpilib_robot_base.h"
49#include "y2018/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070050#include "y2018/control_loops/superstructure/superstructure_output_generated.h"
51#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
52#include "y2018/status_light_generated.h"
53#include "y2018/vision/vision_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080054
55#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
Alex Perrycb7da4b2019-08-28 19:35:56 -070059using ::aos::monotonic_clock;
Tyler Chatowbf0609c2021-07-31 16:13:27 -070060using std::make_unique;
Alex Perrycb7da4b2019-08-28 19:35:56 -070061using ::y2018::constants::Values;
62namespace chrono = ::std::chrono;
63namespace superstructure = ::y2018::control_loops::superstructure;
Austin Schuh2a3e0632018-02-19 16:24:49 -080064
65namespace y2018 {
66namespace wpilib {
67namespace {
68
69constexpr double kMaxBringupPower = 12.0;
70
71// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
72// DMA stuff and then removing the * 2.0 in *_translate.
73// The low bit is direction.
74
Austin Schuh2a3e0632018-02-19 16:24:49 -080075// TODO(brian): Use ::std::max instead once we have C++14 so that can be
76// constexpr.
Austin Schuh2a3e0632018-02-19 16:24:49 -080077template <typename T>
78constexpr T max(T a, T b) {
79 return (a > b) ? a : b;
80}
81
82template <typename T, typename... Rest>
83constexpr T max(T a, T b, T c, Rest... rest) {
84 return max(max(a, b), c, rest...);
85}
86
87double drivetrain_translate(int32_t in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080088 return ((static_cast<double>(in) /
89 Values::kDrivetrainEncoderCountsPerRevolution()) *
90 (2.0 * M_PI)) *
91 Values::kDrivetrainEncoderRatio() *
92 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -080093}
94
95double drivetrain_velocity_translate(double in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080096 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
97 (2.0 * M_PI)) *
98 Values::kDrivetrainEncoderRatio() *
99 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800100}
101
102double proximal_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800103 return -voltage * Values::kProximalPotRatio() *
Austin Schuh2a3e0632018-02-19 16:24:49 -0800104 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
105}
106
107double distal_pot_translate(double voltage) {
108 return voltage * Values::kDistalPotRatio() *
109 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
110}
111
112double intake_pot_translate(double voltage) {
113 return voltage * Values::kIntakeMotorPotRatio() *
114 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
115}
116
117double intake_spring_translate(double voltage) {
118 return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) /
119 (5.0 /*volts*/);
120}
121
122// TODO() figure out differnce between max and min voltages on shifter pots.
123// Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be
124// passed drectly into the drivetrain position queue.
125double drivetrain_shifter_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800126 return (voltage - Values::kDrivetrainShifterPotMinVoltage()) /
127 (Values::kDrivetrainShifterPotMaxVoltage() -
128 Values::kDrivetrainShifterPotMinVoltage());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800129}
130
131constexpr double kMaxFastEncoderPulsesPerSecond =
132 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
133 Values::kMaxIntakeMotorEncoderPulsesPerSecond());
134static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
135 "fast encoders are too fast");
136
137constexpr double kMaxMediumEncoderPulsesPerSecond =
138 max(Values::kMaxProximalEncoderPulsesPerSecond(),
139 Values::kMaxDistalEncoderPulsesPerSecond());
140static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
141 "medium encoders are too fast");
142
143// Class to send position messages with sensor readings to our loops.
Austin Schuh6abf5b72019-02-02 20:20:54 -0800144class SensorReader : public ::frc971::wpilib::SensorReader {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800145 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800146 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700147 : ::frc971::wpilib::SensorReader(event_loop),
148 superstructure_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149 event_loop->MakeSender<superstructure::Position>(
150 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700151 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 event_loop
153 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
154 "/drivetrain")) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800155 // Set to filter out anything shorter than 1/4 of the minimum pulse width
156 // we should ever see.
Austin Schuh6abf5b72019-02-02 20:20:54 -0800157 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
158 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800159 }
160
161 void set_left_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800162 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800163 left_drivetrain_shifter_ = ::std::move(potentiometer);
164 }
165
Austin Schuh2a3e0632018-02-19 16:24:49 -0800166 void set_right_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800167 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800168 right_drivetrain_shifter_ = ::std::move(potentiometer);
169 }
170
171 // Proximal joint.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800172 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800173 medium_encoder_filter_.Add(encoder.get());
174 proximal_encoder_.set_encoder(::std::move(encoder));
175 }
176
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800177 void set_proximal_absolute_pwm(
178 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800179 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
180 }
181
182 void set_proximal_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800183 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800184 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
185 }
186
187 // Distal joint.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800188 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800189 medium_encoder_filter_.Add(encoder.get());
190 distal_encoder_.set_encoder(::std::move(encoder));
191 }
192
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800193 void set_distal_absolute_pwm(
194 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800195 fast_encoder_filter_.Add(absolute_pwm.get());
196 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
197 }
198
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800199 void set_distal_potentiometer(
200 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800201 distal_encoder_.set_potentiometer(::std::move(potentiometer));
202 }
203
204 // Left intake side.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800205 void set_left_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800206 fast_encoder_filter_.Add(encoder.get());
207 left_intake_encoder_.set_encoder(::std::move(encoder));
208 }
209
210 void set_left_intake_absolute_pwm(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800211 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800212 fast_encoder_filter_.Add(absolute_pwm.get());
213 left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
214 }
215
216 void set_left_intake_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800217 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800218 left_intake_encoder_.set_potentiometer(::std::move(potentiometer));
219 }
220
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800221 void set_left_intake_spring_angle(
222 ::std::unique_ptr<frc::AnalogInput> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800223 left_intake_spring_angle_ = ::std::move(encoder);
224 }
225
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800226 void set_left_intake_cube_detector(
227 ::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800228 left_intake_cube_detector_ = ::std::move(input);
229 }
230
231 // Right intake side.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800232 void set_right_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800233 fast_encoder_filter_.Add(encoder.get());
234 right_intake_encoder_.set_encoder(::std::move(encoder));
235 }
236
237 void set_right_intake_absolute_pwm(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800238 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800239 fast_encoder_filter_.Add(absolute_pwm.get());
240 right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
241 }
242
243 void set_right_intake_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800244 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800245 right_intake_encoder_.set_potentiometer(::std::move(potentiometer));
246 }
247
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800248 void set_right_intake_spring_angle(
249 ::std::unique_ptr<frc::AnalogInput> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800250 right_intake_spring_angle_ = ::std::move(encoder);
251 }
252
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800253 void set_right_intake_cube_detector(
254 ::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800255 right_intake_cube_detector_ = ::std::move(input);
256 }
257
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800258 void set_claw_beambreak(::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh4ef51af2018-03-04 01:08:45 -0800259 claw_beambreak_ = ::std::move(input);
260 }
261
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800262 void set_box_back_beambreak(::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh4ef51af2018-03-04 01:08:45 -0800263 box_back_beambreak_ = ::std::move(input);
264 }
265
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700266 void set_lidar_lite_input(
267 ::std::unique_ptr<frc::DigitalInput> lidar_lite_input) {
Austin Schuh8e5950d2018-03-21 20:29:40 -0700268 lidar_lite_input_ = ::std::move(lidar_lite_input);
269 lidar_lite_.set_input(lidar_lite_input_.get());
270 }
271
Austin Schuh6abf5b72019-02-02 20:20:54 -0800272 void Start() { AddToDMA(&lidar_lite_); }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800273
274 void RunIteration() {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800275 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700276 auto builder = drivetrain_position_sender_.MakeBuilder();
277 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
278 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800279
Alex Perrycb7da4b2019-08-28 19:35:56 -0700280 drivetrain_builder.add_left_encoder(
281 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700282 drivetrain_builder.add_left_speed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700284 drivetrain_builder.add_left_shifter_position(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800285 drivetrain_shifter_pot_translate(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 left_drivetrain_shifter_->GetVoltage()));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800287
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700288 drivetrain_builder.add_right_encoder(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700289 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700290 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
291 drivetrain_right_encoder_->GetPeriod()));
292 drivetrain_builder.add_right_shifter_position(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700293 drivetrain_shifter_pot_translate(
294 right_drivetrain_shifter_->GetVoltage()));
295
milind1f1dca32021-07-03 13:50:07 -0700296 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800297 }
Austin Schuh6abf5b72019-02-02 20:20:54 -0800298 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800299
Austin Schuh6abf5b72019-02-02 20:20:54 -0800300 void RunDmaIteration() {
301 const auto values = constants::GetValues();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800302
303 {
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700304 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800305
Alex Perrycb7da4b2019-08-28 19:35:56 -0700306 // Proximal arm
307 frc971::PotAndAbsolutePositionT arm_proximal;
308 CopyPosition(proximal_encoder_, &arm_proximal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800309 Values::kProximalEncoderCountsPerRevolution(),
310 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800311 true, values.arm_proximal.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700312 flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_proximal_offset =
313 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_proximal);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800314
Alex Perrycb7da4b2019-08-28 19:35:56 -0700315 // Distal arm
316 frc971::PotAndAbsolutePositionT arm_distal;
317 CopyPosition(distal_encoder_, &arm_distal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800318 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh6829f762018-03-02 21:36:01 -0800319 Values::kDistalEncoderRatio(), distal_pot_translate, true,
320 values.arm_distal.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700321 flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_distal_offset =
322 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_distal);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800323
Alex Perrycb7da4b2019-08-28 19:35:56 -0700324 superstructure::ArmPosition::Builder arm_position_builder =
325 builder.MakeBuilder<superstructure::ArmPosition>();
326 arm_position_builder.add_proximal(arm_proximal_offset);
327 arm_position_builder.add_distal(arm_distal_offset);
328
329 flatbuffers::Offset<superstructure::ArmPosition> arm_position_offset =
330 arm_position_builder.Finish();
331
332 // Left intake
333 frc971::PotAndAbsolutePositionT left_intake_motor_position;
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700334 CopyPosition(left_intake_encoder_, &left_intake_motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800335 Values::kIntakeMotorEncoderCountsPerRevolution(),
336 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Sabina Davis8d20ca82018-02-19 13:17:45 -0800337 false, values.left_intake.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700338 flatbuffers::Offset<frc971::PotAndAbsolutePosition>
339 left_intake_motor_position_offset =
340 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(),
341 &left_intake_motor_position);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800342
Alex Perrycb7da4b2019-08-28 19:35:56 -0700343 // Right intake
344 frc971::PotAndAbsolutePositionT right_intake_motor_position;
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700345 CopyPosition(right_intake_encoder_, &right_intake_motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800346 Values::kIntakeMotorEncoderCountsPerRevolution(),
347 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800348 true, values.right_intake.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700349 flatbuffers::Offset<frc971::PotAndAbsolutePosition>
350 right_intake_motor_position_offset =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700351 frc971::PotAndAbsolutePosition::Pack(
352 *builder.fbb(), &right_intake_motor_position);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800353
Alex Perrycb7da4b2019-08-28 19:35:56 -0700354 superstructure::IntakeElasticSensors::Builder
355 left_intake_sensors_builder =
356 builder.MakeBuilder<superstructure::IntakeElasticSensors>();
357
358 left_intake_sensors_builder.add_motor_position(
359 left_intake_motor_position_offset);
360 left_intake_sensors_builder.add_spring_angle(
Austin Schuh6829f762018-03-02 21:36:01 -0800361 intake_spring_translate(left_intake_spring_angle_->GetVoltage()) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700362 values.left_intake.spring_offset);
363 left_intake_sensors_builder.add_beam_break(
364 !left_intake_cube_detector_->Get());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800365
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 flatbuffers::Offset<superstructure::IntakeElasticSensors>
367 left_intake_offset = left_intake_sensors_builder.Finish();
368
369 superstructure::IntakeElasticSensors::Builder
370 right_intake_sensors_builder =
371 builder.MakeBuilder<superstructure::IntakeElasticSensors>();
372
373 right_intake_sensors_builder.add_motor_position(
374 right_intake_motor_position_offset);
375 right_intake_sensors_builder.add_spring_angle(
Austin Schuh6829f762018-03-02 21:36:01 -0800376 -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700377 values.right_intake.spring_offset);
378 right_intake_sensors_builder.add_beam_break(
379 !right_intake_cube_detector_->Get());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800380
Alex Perrycb7da4b2019-08-28 19:35:56 -0700381 flatbuffers::Offset<control_loops::superstructure::IntakeElasticSensors>
382 right_intake_offset = right_intake_sensors_builder.Finish();
Austin Schuh4ef51af2018-03-04 01:08:45 -0800383
Alex Perrycb7da4b2019-08-28 19:35:56 -0700384 superstructure::Position::Builder superstructure_builder =
385 builder.MakeBuilder<superstructure::Position>();
Austin Schuh8e5950d2018-03-21 20:29:40 -0700386
Alex Perrycb7da4b2019-08-28 19:35:56 -0700387 superstructure_builder.add_left_intake(left_intake_offset);
388 superstructure_builder.add_right_intake(right_intake_offset);
389 superstructure_builder.add_arm(arm_position_offset);
390
391 superstructure_builder.add_claw_beambreak_triggered(
392 !claw_beambreak_->Get());
393 superstructure_builder.add_box_back_beambreak_triggered(
394 !box_back_beambreak_->Get());
395
396 superstructure_builder.add_box_distance(lidar_lite_.last_width() /
397 0.00001 / 100.0 / 2);
398
milind1f1dca32021-07-03 13:50:07 -0700399 builder.CheckOk(builder.Send(superstructure_builder.Finish()));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800400 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800401 }
402
Austin Schuh2a3e0632018-02-19 16:24:49 -0800403 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700404 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
405 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700406 drivetrain_position_sender_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700407
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800408 ::std::unique_ptr<frc::AnalogInput> left_drivetrain_shifter_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800409 right_drivetrain_shifter_;
410
411 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
412 distal_encoder_;
413
414 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_,
415 right_intake_encoder_;
416
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800417 ::std::unique_ptr<frc::AnalogInput> left_intake_spring_angle_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800418 right_intake_spring_angle_;
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800419 ::std::unique_ptr<frc::DigitalInput> left_intake_cube_detector_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800420 right_intake_cube_detector_;
421
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800422 ::std::unique_ptr<frc::DigitalInput> claw_beambreak_;
423 ::std::unique_ptr<frc::DigitalInput> box_back_beambreak_;
Austin Schuh4ef51af2018-03-04 01:08:45 -0800424
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800425 ::std::unique_ptr<frc::DigitalInput> lidar_lite_input_;
Austin Schuh8e5950d2018-03-21 20:29:40 -0700426 ::frc971::wpilib::DMAPulseWidthReader lidar_lite_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800427};
428
429class SolenoidWriter {
430 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800431 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700432 ::frc971::wpilib::BufferedPcm *pcm)
433 : event_loop_(event_loop),
434 drivetrain_fetcher_(
435 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700436 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
437 "/drivetrain")),
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700438 superstructure_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700439 event_loop->MakeFetcher<superstructure::Output>("/superstructure")),
440 status_light_fetcher_(
441 event_loop->MakeFetcher<::y2018::StatusLight>("/superstructure")),
Austin Schuh300f2f62019-05-27 13:49:23 -0700442 vision_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700443 event_loop->MakeFetcher<::y2018::vision::VisionStatus>("/vision")),
444 pneumatics_to_log_sender_(
445 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")),
Austin Schuh9950f682021-11-06 15:27:58 -0700446 pcm_(pcm),
447 compressor_(0) {
Austin Schuh217a9782019-12-21 23:02:50 -0800448 event_loop->set_name("Solenoids");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700449 event_loop_->SetRuntimeRealtimePriority(27);
450
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700451 event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); },
452 ::std::chrono::milliseconds(20),
453 ::std::chrono::milliseconds(1));
454 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800455
456 // left drive
457 // right drive
458 //
459 // claw
460 // arm brakes
461 // hook release
462 // fork release
463 void set_left_drivetrain_shifter(
464 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
465 left_drivetrain_shifter_ = ::std::move(s);
466 }
467 void set_right_drivetrain_shifter(
468 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
469 right_drivetrain_shifter_ = ::std::move(s);
470 }
471
472 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
473 claw_ = ::std::move(s);
474 }
475
476 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
477 arm_brakes_ = ::std::move(s);
478 }
479
480 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
481 hook_ = ::std::move(s);
482 }
483
484 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
485 forks_ = ::std::move(s);
486 }
487
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488 void Loop(const int iterations) {
489 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700490 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700491 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800492
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700493 {
494 drivetrain_fetcher_.Fetch();
495 if (drivetrain_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700496 left_drivetrain_shifter_->Set(!drivetrain_fetcher_->left_high());
497 right_drivetrain_shifter_->Set(!drivetrain_fetcher_->right_high());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700498 }
499 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800500
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700501 {
502 superstructure_fetcher_.Fetch();
503 if (superstructure_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700504 claw_->Set(!superstructure_fetcher_->claw_grabbed());
505 arm_brakes_->Set(superstructure_fetcher_->release_arm_brake());
506 hook_->Set(superstructure_fetcher_->hook_release());
507 forks_->Set(superstructure_fetcher_->forks_release());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700508 }
509 }
510
511 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512 auto builder = pneumatics_to_log_sender_.MakeBuilder();
513
514 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
515 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700516
517 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700518 to_log_builder.add_read_solenoids(pcm_->GetAll());
milind1f1dca32021-07-03 13:50:07 -0700519 (void)builder.Send(to_log_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700520 }
521
522 monotonic_clock::time_point monotonic_now = event_loop_->monotonic_now();
523 status_light_fetcher_.Fetch();
524 // If we don't have a light request (or it's an old one), we are borked.
525 // Flash the red light slowly.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700526 StatusLightT color;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700527 if (!status_light_fetcher_.get() ||
Austin Schuhad154822019-12-27 15:45:13 -0800528 monotonic_now > status_light_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700529 chrono::milliseconds(100)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700530 color.red = 0.0;
531 color.green = 0.0;
532 color.blue = 0.0;
533
534 vision_status_fetcher_.Fetch();
535 ++light_flash_;
536 if (light_flash_ > 10) {
537 color.red = 0.5;
538 } else if (!vision_status_fetcher_.get() ||
539 monotonic_now >
Austin Schuhad154822019-12-27 15:45:13 -0800540 vision_status_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700541 chrono::seconds(1)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700542 color.red = 0.5;
543 color.green = 0.5;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800544 }
545
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700546 if (light_flash_ > 20) {
547 light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800548 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700549 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700550 status_light_fetcher_->UnPackTo(&color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800551 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700552 SetColor(color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800553 }
554
Alex Perrycb7da4b2019-08-28 19:35:56 -0700555 void SetColor(const StatusLightT &status_light) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700556 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
557 // it actually changes. This is pretty low priority anyways.
558 static int time_since_last_send = 0;
559 ++time_since_last_send;
560 if (time_since_last_send > 10) {
561 time_since_last_send = 0;
562 }
563 if (status_light.green != last_green_ || time_since_last_send == 0) {
564 canifier_.SetLEDOutput(1.0 - status_light.green,
565 ::ctre::phoenix::CANifier::LEDChannelB);
566 last_green_ = status_light.green;
567 }
568
569 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
570 canifier_.SetLEDOutput(1.0 - status_light.blue,
571 ::ctre::phoenix::CANifier::LEDChannelA);
572 last_blue_ = status_light.blue;
573 }
574
575 if (status_light.red != last_red_ || time_since_last_send == 0) {
576 canifier_.SetLEDOutput(1.0 - status_light.red,
577 ::ctre::phoenix::CANifier::LEDChannelC);
578 last_red_ = status_light.red;
579 }
580 }
581
Austin Schuh2a3e0632018-02-19 16:24:49 -0800582 void Quit() { run_ = false; }
583
584 private:
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700585 ::aos::EventLoop *event_loop_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700586 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output>
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700587 drivetrain_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700588 ::aos::Fetcher<superstructure::Output> superstructure_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700589 ::aos::Fetcher<::y2018::StatusLight> status_light_fetcher_;
Austin Schuh300f2f62019-05-27 13:49:23 -0700590 ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700591
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
593
Austin Schuh2a3e0632018-02-19 16:24:49 -0800594 ::frc971::wpilib::BufferedPcm *pcm_;
595
596 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
597 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
598 hook_, forks_;
599
Brian Silverman37281fc2018-03-11 18:42:17 -0700600 ::ctre::phoenix::CANifier canifier_{0};
601
Austin Schuh2a3e0632018-02-19 16:24:49 -0800602 ::std::atomic<bool> run_{true};
Austin Schuh8d5fff42018-05-30 20:44:12 -0700603
604 double last_red_ = -1.0;
605 double last_green_ = -1.0;
606 double last_blue_ = -1.0;
607
Austin Schuh9950f682021-11-06 15:27:58 -0700608 frc::Compressor compressor_;
609
Austin Schuh8d5fff42018-05-30 20:44:12 -0700610 int light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800611};
612
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700613class SuperstructureWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700614 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800615 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800616 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700617 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
618 event_loop, "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800619
Austin Schuh2a3e0632018-02-19 16:24:49 -0800620 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
621 proximal_victor_ = ::std::move(t);
622 }
623 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
624 distal_victor_ = ::std::move(t);
625 }
626
Austin Schuh17e484e2018-03-11 01:11:36 -0800627 void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) {
628 hanger_victor_ = ::std::move(t);
629 }
630
Austin Schuh2a3e0632018-02-19 16:24:49 -0800631 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
632 left_intake_elastic_victor_ = ::std::move(t);
633 }
634 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
635 right_intake_elastic_victor_ = ::std::move(t);
636 }
637
638 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
639 left_intake_rollers_victor_ = ::std::move(t);
640 }
641
642 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
643 right_intake_rollers_victor_ = ::std::move(t);
644 }
645
646 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700647 virtual void Write(const superstructure::Output &output) override {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800648 left_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700649 ::aos::Clip(-output.left_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800650 kMaxBringupPower) /
651 12.0);
652
653 right_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700654 ::aos::Clip(output.right_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800655 kMaxBringupPower) /
656 12.0);
657
658 left_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700659 ::aos::Clip(-output.left_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800660 kMaxBringupPower) /
661 12.0);
662
663 right_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700664 ::aos::Clip(output.right_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800665 kMaxBringupPower) /
666 12.0);
667
Alex Perrycb7da4b2019-08-28 19:35:56 -0700668 proximal_victor_->SetSpeed(::aos::Clip(-output.voltage_proximal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800669 -kMaxBringupPower,
670 kMaxBringupPower) /
671 12.0);
672
Alex Perrycb7da4b2019-08-28 19:35:56 -0700673 distal_victor_->SetSpeed(::aos::Clip(output.voltage_distal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800674 -kMaxBringupPower, kMaxBringupPower) /
675 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700676 hanger_victor_->SetSpeed(::aos::Clip(-output.voltage_winch(),
677 -kMaxBringupPower, kMaxBringupPower) /
678 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800679 }
680
681 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700682 AOS_LOG(WARNING, "Superstructure output too old.\n");
Austin Schuh2a3e0632018-02-19 16:24:49 -0800683
684 left_intake_rollers_victor_->SetDisabled();
685 right_intake_rollers_victor_->SetDisabled();
686 left_intake_elastic_victor_->SetDisabled();
687 right_intake_elastic_victor_->SetDisabled();
688
689 proximal_victor_->SetDisabled();
690 distal_victor_->SetDisabled();
Austin Schuh17e484e2018-03-11 01:11:36 -0800691 hanger_victor_->SetDisabled();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800692 }
693
694 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
695 right_intake_rollers_victor_, left_intake_elastic_victor_,
Austin Schuh17e484e2018-03-11 01:11:36 -0800696 right_intake_elastic_victor_, proximal_victor_, distal_victor_,
697 hanger_victor_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800698};
699
700class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
701 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800702 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
703 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
704 frc::Encoder::k4X);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800705 }
706
707 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700708 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800709 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700710
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700711 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700712 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700713 ::frc971::wpilib::JoystickSender joystick_sender(
714 &joystick_sender_event_loop);
715 AddLoop(&joystick_sender_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800716
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700717 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700718 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700719 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
720 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800721
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700722 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700723 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700724 SensorReader sensor_reader(&sensor_reader_event_loop);
725 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
726 sensor_reader.set_left_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800727 make_unique<frc::AnalogInput>(6));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700728 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
729 sensor_reader.set_right_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800730 make_unique<frc::AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800731
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700732 sensor_reader.set_proximal_encoder(make_encoder(4));
733 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(2));
734 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800735
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700736 sensor_reader.set_distal_encoder(make_encoder(2));
737 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(3));
738 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(3));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800739
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700740 sensor_reader.set_right_intake_encoder(make_encoder(5));
741 sensor_reader.set_right_intake_absolute_pwm(
742 make_unique<frc::DigitalInput>(7));
743 sensor_reader.set_right_intake_potentiometer(
744 make_unique<frc::AnalogInput>(1));
745 sensor_reader.set_right_intake_spring_angle(
746 make_unique<frc::AnalogInput>(5));
747 sensor_reader.set_right_intake_cube_detector(
748 make_unique<frc::DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800749
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700750 sensor_reader.set_left_intake_encoder(make_encoder(3));
751 sensor_reader.set_left_intake_absolute_pwm(
752 make_unique<frc::DigitalInput>(4));
753 sensor_reader.set_left_intake_potentiometer(
754 make_unique<frc::AnalogInput>(0));
755 sensor_reader.set_left_intake_spring_angle(
756 make_unique<frc::AnalogInput>(4));
757 sensor_reader.set_left_intake_cube_detector(
758 make_unique<frc::DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800759
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700760 sensor_reader.set_claw_beambreak(make_unique<frc::DigitalInput>(8));
761 sensor_reader.set_box_back_beambreak(make_unique<frc::DigitalInput>(9));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800762
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700763 sensor_reader.set_pwm_trigger(true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800764
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700765 sensor_reader.set_lidar_lite_input(make_unique<frc::DigitalInput>(22));
766 AddLoop(&sensor_reader_event_loop);
Austin Schuh8e5950d2018-03-21 20:29:40 -0700767
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700768 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700769 ::aos::ShmEventLoop imu_event_loop(&config.message());
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800770 auto imu_trigger = make_unique<frc::DigitalInput>(5);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700771 ::frc971::wpilib::ADIS16448 imu(
772 &imu_event_loop, frc::SPI::Port::kOnboardCS1, imu_trigger.get());
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800773 imu.SetDummySPI(frc::SPI::Port::kOnboardCS2);
774 auto imu_reset = make_unique<frc::DigitalOutput>(6);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800775 imu.set_reset(imu_reset.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700776 AddLoop(&imu_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800777
Austin Schuhe8a54c02018-03-05 00:25:58 -0800778 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
779 // they are identical, as far as DrivetrainWriter is concerned, to the SP
780 // variety so all the Victors are written as SPs.
Austin Schuh2a3e0632018-02-19 16:24:49 -0800781
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700782 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700783 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700784
785 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Daviscaa2a6b2019-02-03 01:15:37 -0800786 drivetrain_writer.set_left_controller0(
787 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)), false);
788 drivetrain_writer.set_right_controller0(
789 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800790
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700791 SuperstructureWriter superstructure_writer(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800792 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800793 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800794 superstructure_writer.set_left_intake_rollers_victor(
795 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800796 superstructure_writer.set_right_intake_elastic_victor(
797 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
798 superstructure_writer.set_right_intake_rollers_victor(
799 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800800 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800801 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800802 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800803 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800804 superstructure_writer.set_hanger_victor(
805 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800806
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700807 AddLoop(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800808
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700809 // Thread 6.
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700810 // This is a separate event loop because we want to run it at much lower
811 // priority.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700812 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Austin Schuhbfbaa372019-02-15 23:05:31 -0800813 ::frc971::wpilib::BufferedPcm pcm;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700814 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, &pcm);
Austin Schuhbfbaa372019-02-15 23:05:31 -0800815 solenoid_writer.set_left_drivetrain_shifter(pcm.MakeSolenoid(0));
816 solenoid_writer.set_right_drivetrain_shifter(pcm.MakeSolenoid(1));
817 solenoid_writer.set_claw(pcm.MakeSolenoid(2));
818 solenoid_writer.set_arm_brakes(pcm.MakeSolenoid(3));
819 solenoid_writer.set_hook(pcm.MakeSolenoid(4));
820 solenoid_writer.set_forks(pcm.MakeSolenoid(5));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700821 AddLoop(&solenoid_writer_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800822
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700823 RunLoops();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800824 }
825};
826
827} // namespace
828} // namespace wpilib
829} // namespace y2018
830
831AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);