blob: cf71bdd4eaed7732c7e7093a1f694283811773a5 [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"
Philipp Schrader790cb542023-07-05 21:06:52 -070014
Parker Schuhd3b7a8872018-02-19 16:42:27 -080015#include "frc971/wpilib/ahal/AnalogInput.h"
Austin Schuh9950f682021-11-06 15:27:58 -070016#include "frc971/wpilib/ahal/Compressor.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080017#include "frc971/wpilib/ahal/Counter.h"
18#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
19#include "frc971/wpilib/ahal/DriverStation.h"
20#include "frc971/wpilib/ahal/Encoder.h"
21#include "frc971/wpilib/ahal/Relay.h"
22#include "frc971/wpilib/ahal/Servo.h"
23#include "frc971/wpilib/ahal/VictorSP.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080024#undef ERROR
25
John Park33858a32018-09-28 23:05:48 -070026#include "aos/commonmath.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070027#include "aos/events/shm_event_loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080028#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/logging/logging.h"
John Park33858a32018-09-28 23:05:48 -070030#include "aos/time/time.h"
31#include "aos/util/compiler_memory_barrier.h"
32#include "aos/util/log_interval.h"
33#include "aos/util/phased_loop.h"
34#include "aos/util/wrapping_counter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070035#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
36#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080037#include "frc971/wpilib/ADIS16448.h"
38#include "frc971/wpilib/buffered_pcm.h"
39#include "frc971/wpilib/buffered_solenoid.h"
40#include "frc971/wpilib/dma.h"
41#include "frc971/wpilib/dma_edge_counting.h"
Sabina Daviscaa2a6b2019-02-03 01:15:37 -080042#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080043#include "frc971/wpilib/encoder_and_potentiometer.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080044#include "frc971/wpilib/joystick_sender.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070045#include "frc971/wpilib/logging_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080046#include "frc971/wpilib/loop_output_handler.h"
47#include "frc971/wpilib/pdp_fetcher.h"
Austin Schuh6abf5b72019-02-02 20:20:54 -080048#include "frc971/wpilib/sensor_reader.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080049#include "frc971/wpilib/wpilib_robot_base.h"
50#include "y2018/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070051#include "y2018/control_loops/superstructure/superstructure_output_generated.h"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080052#include "y2018/control_loops/superstructure/superstructure_position_static.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070053#include "y2018/status_light_generated.h"
54#include "y2018/vision/vision_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080055
56#ifndef M_PI
57#define M_PI 3.14159265358979323846
58#endif
59
Alex Perrycb7da4b2019-08-28 19:35:56 -070060using ::aos::monotonic_clock;
Tyler Chatowbf0609c2021-07-31 16:13:27 -070061using std::make_unique;
Alex Perrycb7da4b2019-08-28 19:35:56 -070062using ::y2018::constants::Values;
63namespace chrono = ::std::chrono;
64namespace superstructure = ::y2018::control_loops::superstructure;
Austin Schuh2a3e0632018-02-19 16:24:49 -080065
66namespace y2018 {
67namespace wpilib {
68namespace {
69
70constexpr double kMaxBringupPower = 12.0;
71
72// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
73// DMA stuff and then removing the * 2.0 in *_translate.
74// The low bit is direction.
75
Austin Schuh2a3e0632018-02-19 16:24:49 -080076// TODO(brian): Use ::std::max instead once we have C++14 so that can be
77// constexpr.
Austin Schuh2a3e0632018-02-19 16:24:49 -080078template <typename T>
79constexpr T max(T a, T b) {
80 return (a > b) ? a : b;
81}
82
83template <typename T, typename... Rest>
84constexpr T max(T a, T b, T c, Rest... rest) {
85 return max(max(a, b), c, rest...);
86}
87
88double drivetrain_translate(int32_t in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080089 return ((static_cast<double>(in) /
90 Values::kDrivetrainEncoderCountsPerRevolution()) *
91 (2.0 * M_PI)) *
92 Values::kDrivetrainEncoderRatio() *
93 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -080094}
95
96double drivetrain_velocity_translate(double in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080097 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
98 (2.0 * M_PI)) *
99 Values::kDrivetrainEncoderRatio() *
100 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800101}
102
103double proximal_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800104 return -voltage * Values::kProximalPotRatio() *
Austin Schuh2a3e0632018-02-19 16:24:49 -0800105 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
106}
107
108double distal_pot_translate(double voltage) {
109 return voltage * Values::kDistalPotRatio() *
110 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
111}
112
113double intake_pot_translate(double voltage) {
114 return voltage * Values::kIntakeMotorPotRatio() *
115 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
116}
117
118double intake_spring_translate(double voltage) {
119 return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) /
120 (5.0 /*volts*/);
121}
122
123// TODO() figure out differnce between max and min voltages on shifter pots.
124// Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be
125// passed drectly into the drivetrain position queue.
126double drivetrain_shifter_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800127 return (voltage - Values::kDrivetrainShifterPotMinVoltage()) /
128 (Values::kDrivetrainShifterPotMaxVoltage() -
129 Values::kDrivetrainShifterPotMinVoltage());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800130}
131
132constexpr double kMaxFastEncoderPulsesPerSecond =
133 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
134 Values::kMaxIntakeMotorEncoderPulsesPerSecond());
135static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
136 "fast encoders are too fast");
137
138constexpr double kMaxMediumEncoderPulsesPerSecond =
139 max(Values::kMaxProximalEncoderPulsesPerSecond(),
140 Values::kMaxDistalEncoderPulsesPerSecond());
141static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
142 "medium encoders are too fast");
143
144// Class to send position messages with sensor readings to our loops.
Austin Schuh6abf5b72019-02-02 20:20:54 -0800145class SensorReader : public ::frc971::wpilib::SensorReader {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800146 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800147 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700148 : ::frc971::wpilib::SensorReader(event_loop),
149 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800150 event_loop->MakeSender<superstructure::PositionStatic>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700152 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153 event_loop
154 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
155 "/drivetrain")) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800156 // Set to filter out anything shorter than 1/4 of the minimum pulse width
157 // we should ever see.
Austin Schuh6abf5b72019-02-02 20:20:54 -0800158 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
159 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800160 }
161
162 void set_left_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800163 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800164 left_drivetrain_shifter_ = ::std::move(potentiometer);
165 }
166
Austin Schuh2a3e0632018-02-19 16:24:49 -0800167 void set_right_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800168 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800169 right_drivetrain_shifter_ = ::std::move(potentiometer);
170 }
171
172 // Proximal joint.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800173 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800174 medium_encoder_filter_.Add(encoder.get());
175 proximal_encoder_.set_encoder(::std::move(encoder));
176 }
177
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800178 void set_proximal_absolute_pwm(
179 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800180 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
181 }
182
183 void set_proximal_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800184 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800185 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
186 }
187
188 // Distal joint.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800189 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800190 medium_encoder_filter_.Add(encoder.get());
191 distal_encoder_.set_encoder(::std::move(encoder));
192 }
193
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800194 void set_distal_absolute_pwm(
195 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800196 fast_encoder_filter_.Add(absolute_pwm.get());
197 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
198 }
199
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800200 void set_distal_potentiometer(
201 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800202 distal_encoder_.set_potentiometer(::std::move(potentiometer));
203 }
204
205 // Left intake side.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800206 void set_left_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800207 fast_encoder_filter_.Add(encoder.get());
208 left_intake_encoder_.set_encoder(::std::move(encoder));
209 }
210
211 void set_left_intake_absolute_pwm(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800212 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800213 fast_encoder_filter_.Add(absolute_pwm.get());
214 left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
215 }
216
217 void set_left_intake_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800218 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800219 left_intake_encoder_.set_potentiometer(::std::move(potentiometer));
220 }
221
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800222 void set_left_intake_spring_angle(
223 ::std::unique_ptr<frc::AnalogInput> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800224 left_intake_spring_angle_ = ::std::move(encoder);
225 }
226
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800227 void set_left_intake_cube_detector(
228 ::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800229 left_intake_cube_detector_ = ::std::move(input);
230 }
231
232 // Right intake side.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800233 void set_right_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800234 fast_encoder_filter_.Add(encoder.get());
235 right_intake_encoder_.set_encoder(::std::move(encoder));
236 }
237
238 void set_right_intake_absolute_pwm(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800239 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800240 fast_encoder_filter_.Add(absolute_pwm.get());
241 right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
242 }
243
244 void set_right_intake_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800245 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800246 right_intake_encoder_.set_potentiometer(::std::move(potentiometer));
247 }
248
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800249 void set_right_intake_spring_angle(
250 ::std::unique_ptr<frc::AnalogInput> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800251 right_intake_spring_angle_ = ::std::move(encoder);
252 }
253
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800254 void set_right_intake_cube_detector(
255 ::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800256 right_intake_cube_detector_ = ::std::move(input);
257 }
258
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800259 void set_claw_beambreak(::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh4ef51af2018-03-04 01:08:45 -0800260 claw_beambreak_ = ::std::move(input);
261 }
262
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800263 void set_box_back_beambreak(::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh4ef51af2018-03-04 01:08:45 -0800264 box_back_beambreak_ = ::std::move(input);
265 }
266
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700267 void set_lidar_lite_input(
268 ::std::unique_ptr<frc::DigitalInput> lidar_lite_input) {
Austin Schuh8e5950d2018-03-21 20:29:40 -0700269 lidar_lite_input_ = ::std::move(lidar_lite_input);
270 lidar_lite_.set_input(lidar_lite_input_.get());
271 }
272
Austin Schuh6abf5b72019-02-02 20:20:54 -0800273 void Start() { AddToDMA(&lidar_lite_); }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800274
275 void RunIteration() {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800276 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 auto builder = drivetrain_position_sender_.MakeBuilder();
278 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
279 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800280
Alex Perrycb7da4b2019-08-28 19:35:56 -0700281 drivetrain_builder.add_left_encoder(
282 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700283 drivetrain_builder.add_left_speed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700285 drivetrain_builder.add_left_shifter_position(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800286 drivetrain_shifter_pot_translate(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700287 left_drivetrain_shifter_->GetVoltage()));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800288
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700289 drivetrain_builder.add_right_encoder(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700290 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700291 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
292 drivetrain_right_encoder_->GetPeriod()));
293 drivetrain_builder.add_right_shifter_position(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294 drivetrain_shifter_pot_translate(
295 right_drivetrain_shifter_->GetVoltage()));
296
milind1f1dca32021-07-03 13:50:07 -0700297 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800298 }
Austin Schuh6abf5b72019-02-02 20:20:54 -0800299 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800300
Austin Schuh6abf5b72019-02-02 20:20:54 -0800301 void RunDmaIteration() {
302 const auto values = constants::GetValues();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800303
304 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800305 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
306 superstructure_position_sender_.MakeStaticBuilder();
307
308 superstructure::ArmPositionStatic *arm_position = builder->add_arm();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800309
Alex Perrycb7da4b2019-08-28 19:35:56 -0700310 // Proximal arm
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800311 CopyPosition(proximal_encoder_, arm_position->add_proximal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800312 Values::kProximalEncoderCountsPerRevolution(),
313 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800314 true, values.arm_proximal.potentiometer_offset);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800315
Alex Perrycb7da4b2019-08-28 19:35:56 -0700316 // Distal arm
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800317 CopyPosition(distal_encoder_, arm_position->add_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
322 // Left intake
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800323 superstructure::IntakeElasticSensorsStatic *left_intake =
324 builder->add_left_intake();
325
326 CopyPosition(left_intake_encoder_, left_intake->add_motor_position(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800327 Values::kIntakeMotorEncoderCountsPerRevolution(),
328 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Sabina Davis8d20ca82018-02-19 13:17:45 -0800329 false, values.left_intake.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800330
331 left_intake->set_spring_angle(
332 intake_spring_translate(left_intake_spring_angle_->GetVoltage()) +
333 values.left_intake.spring_offset);
334 left_intake->set_beam_break(!left_intake_cube_detector_->Get());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800335
Alex Perrycb7da4b2019-08-28 19:35:56 -0700336 // Right intake
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800337 superstructure::IntakeElasticSensorsStatic *right_intake =
338 builder->add_right_intake();
339 CopyPosition(right_intake_encoder_, right_intake->add_motor_position(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800340 Values::kIntakeMotorEncoderCountsPerRevolution(),
341 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800342 true, values.right_intake.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800343 right_intake->set_spring_angle(
Austin Schuh6829f762018-03-02 21:36:01 -0800344 -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700345 values.right_intake.spring_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800346 right_intake->set_beam_break(!right_intake_cube_detector_->Get());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800347
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800348 builder->set_claw_beambreak_triggered(!claw_beambreak_->Get());
349 builder->set_box_back_beambreak_triggered(!box_back_beambreak_->Get());
Austin Schuh4ef51af2018-03-04 01:08:45 -0800350
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800351 builder->set_box_distance(lidar_lite_.last_width() / 0.00001 / 100.0 / 2);
Austin Schuh8e5950d2018-03-21 20:29:40 -0700352
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800353 builder.CheckOk(builder.Send());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800354 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800355 }
356
Austin Schuh2a3e0632018-02-19 16:24:49 -0800357 private:
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800358 ::aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700359 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700360 drivetrain_position_sender_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700361
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800362 ::std::unique_ptr<frc::AnalogInput> left_drivetrain_shifter_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800363 right_drivetrain_shifter_;
364
365 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
366 distal_encoder_;
367
368 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_,
369 right_intake_encoder_;
370
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800371 ::std::unique_ptr<frc::AnalogInput> left_intake_spring_angle_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800372 right_intake_spring_angle_;
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800373 ::std::unique_ptr<frc::DigitalInput> left_intake_cube_detector_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800374 right_intake_cube_detector_;
375
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800376 ::std::unique_ptr<frc::DigitalInput> claw_beambreak_;
377 ::std::unique_ptr<frc::DigitalInput> box_back_beambreak_;
Austin Schuh4ef51af2018-03-04 01:08:45 -0800378
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800379 ::std::unique_ptr<frc::DigitalInput> lidar_lite_input_;
Austin Schuh8e5950d2018-03-21 20:29:40 -0700380 ::frc971::wpilib::DMAPulseWidthReader lidar_lite_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800381};
382
383class SolenoidWriter {
384 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800385 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700386 ::frc971::wpilib::BufferedPcm *pcm)
387 : event_loop_(event_loop),
388 drivetrain_fetcher_(
389 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700390 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
391 "/drivetrain")),
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700392 superstructure_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700393 event_loop->MakeFetcher<superstructure::Output>("/superstructure")),
394 status_light_fetcher_(
395 event_loop->MakeFetcher<::y2018::StatusLight>("/superstructure")),
Austin Schuh300f2f62019-05-27 13:49:23 -0700396 vision_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700397 event_loop->MakeFetcher<::y2018::vision::VisionStatus>("/vision")),
398 pneumatics_to_log_sender_(
399 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")),
Austin Schuh9950f682021-11-06 15:27:58 -0700400 pcm_(pcm),
401 compressor_(0) {
Austin Schuh217a9782019-12-21 23:02:50 -0800402 event_loop->set_name("Solenoids");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700403 event_loop_->SetRuntimeRealtimePriority(27);
404
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700405 event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); },
406 ::std::chrono::milliseconds(20),
407 ::std::chrono::milliseconds(1));
408 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800409
410 // left drive
411 // right drive
412 //
413 // claw
414 // arm brakes
415 // hook release
416 // fork release
417 void set_left_drivetrain_shifter(
418 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
419 left_drivetrain_shifter_ = ::std::move(s);
420 }
421 void set_right_drivetrain_shifter(
422 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
423 right_drivetrain_shifter_ = ::std::move(s);
424 }
425
426 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
427 claw_ = ::std::move(s);
428 }
429
430 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
431 arm_brakes_ = ::std::move(s);
432 }
433
434 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
435 hook_ = ::std::move(s);
436 }
437
438 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
439 forks_ = ::std::move(s);
440 }
441
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700442 void Loop(const int iterations) {
443 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700444 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700445 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800446
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700447 {
448 drivetrain_fetcher_.Fetch();
449 if (drivetrain_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450 left_drivetrain_shifter_->Set(!drivetrain_fetcher_->left_high());
451 right_drivetrain_shifter_->Set(!drivetrain_fetcher_->right_high());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700452 }
453 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800454
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700455 {
456 superstructure_fetcher_.Fetch();
457 if (superstructure_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700458 claw_->Set(!superstructure_fetcher_->claw_grabbed());
459 arm_brakes_->Set(superstructure_fetcher_->release_arm_brake());
460 hook_->Set(superstructure_fetcher_->hook_release());
461 forks_->Set(superstructure_fetcher_->forks_release());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700462 }
463 }
464
465 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 auto builder = pneumatics_to_log_sender_.MakeBuilder();
467
468 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
469 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700470
471 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700472 to_log_builder.add_read_solenoids(pcm_->GetAll());
milind1f1dca32021-07-03 13:50:07 -0700473 (void)builder.Send(to_log_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700474 }
475
476 monotonic_clock::time_point monotonic_now = event_loop_->monotonic_now();
477 status_light_fetcher_.Fetch();
478 // If we don't have a light request (or it's an old one), we are borked.
479 // Flash the red light slowly.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480 StatusLightT color;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700481 if (!status_light_fetcher_.get() ||
Austin Schuhad154822019-12-27 15:45:13 -0800482 monotonic_now > status_light_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700483 chrono::milliseconds(100)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700484 color.red = 0.0;
485 color.green = 0.0;
486 color.blue = 0.0;
487
488 vision_status_fetcher_.Fetch();
489 ++light_flash_;
490 if (light_flash_ > 10) {
491 color.red = 0.5;
492 } else if (!vision_status_fetcher_.get() ||
493 monotonic_now >
Austin Schuhad154822019-12-27 15:45:13 -0800494 vision_status_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700495 chrono::seconds(1)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700496 color.red = 0.5;
497 color.green = 0.5;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800498 }
499
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700500 if (light_flash_ > 20) {
501 light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800502 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700503 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700504 status_light_fetcher_->UnPackTo(&color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800505 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700506 SetColor(color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800507 }
508
Alex Perrycb7da4b2019-08-28 19:35:56 -0700509 void SetColor(const StatusLightT &status_light) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700510 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
511 // it actually changes. This is pretty low priority anyways.
512 static int time_since_last_send = 0;
513 ++time_since_last_send;
514 if (time_since_last_send > 10) {
515 time_since_last_send = 0;
516 }
517 if (status_light.green != last_green_ || time_since_last_send == 0) {
518 canifier_.SetLEDOutput(1.0 - status_light.green,
519 ::ctre::phoenix::CANifier::LEDChannelB);
520 last_green_ = status_light.green;
521 }
522
523 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
524 canifier_.SetLEDOutput(1.0 - status_light.blue,
525 ::ctre::phoenix::CANifier::LEDChannelA);
526 last_blue_ = status_light.blue;
527 }
528
529 if (status_light.red != last_red_ || time_since_last_send == 0) {
530 canifier_.SetLEDOutput(1.0 - status_light.red,
531 ::ctre::phoenix::CANifier::LEDChannelC);
532 last_red_ = status_light.red;
533 }
534 }
535
Austin Schuh2a3e0632018-02-19 16:24:49 -0800536 void Quit() { run_ = false; }
537
538 private:
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700539 ::aos::EventLoop *event_loop_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700540 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output>
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700541 drivetrain_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700542 ::aos::Fetcher<superstructure::Output> superstructure_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700543 ::aos::Fetcher<::y2018::StatusLight> status_light_fetcher_;
Austin Schuh300f2f62019-05-27 13:49:23 -0700544 ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700545
Alex Perrycb7da4b2019-08-28 19:35:56 -0700546 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
547
Austin Schuh2a3e0632018-02-19 16:24:49 -0800548 ::frc971::wpilib::BufferedPcm *pcm_;
549
550 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
551 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
552 hook_, forks_;
553
Brian Silverman37281fc2018-03-11 18:42:17 -0700554 ::ctre::phoenix::CANifier canifier_{0};
555
Austin Schuh2a3e0632018-02-19 16:24:49 -0800556 ::std::atomic<bool> run_{true};
Austin Schuh8d5fff42018-05-30 20:44:12 -0700557
558 double last_red_ = -1.0;
559 double last_green_ = -1.0;
560 double last_blue_ = -1.0;
561
Austin Schuh9950f682021-11-06 15:27:58 -0700562 frc::Compressor compressor_;
563
Austin Schuh8d5fff42018-05-30 20:44:12 -0700564 int light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800565};
566
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700567class SuperstructureWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700568 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800569 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800570 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700571 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
572 event_loop, "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800573
Austin Schuh2a3e0632018-02-19 16:24:49 -0800574 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
575 proximal_victor_ = ::std::move(t);
576 }
577 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
578 distal_victor_ = ::std::move(t);
579 }
580
Austin Schuh17e484e2018-03-11 01:11:36 -0800581 void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) {
582 hanger_victor_ = ::std::move(t);
583 }
584
Austin Schuh2a3e0632018-02-19 16:24:49 -0800585 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
586 left_intake_elastic_victor_ = ::std::move(t);
587 }
588 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
589 right_intake_elastic_victor_ = ::std::move(t);
590 }
591
592 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
593 left_intake_rollers_victor_ = ::std::move(t);
594 }
595
596 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
597 right_intake_rollers_victor_ = ::std::move(t);
598 }
599
600 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700601 virtual void Write(const superstructure::Output &output) override {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800602 left_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700603 ::aos::Clip(-output.left_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800604 kMaxBringupPower) /
605 12.0);
606
607 right_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700608 ::aos::Clip(output.right_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800609 kMaxBringupPower) /
610 12.0);
611
612 left_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700613 ::aos::Clip(-output.left_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800614 kMaxBringupPower) /
615 12.0);
616
617 right_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618 ::aos::Clip(output.right_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800619 kMaxBringupPower) /
620 12.0);
621
Alex Perrycb7da4b2019-08-28 19:35:56 -0700622 proximal_victor_->SetSpeed(::aos::Clip(-output.voltage_proximal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800623 -kMaxBringupPower,
624 kMaxBringupPower) /
625 12.0);
626
Alex Perrycb7da4b2019-08-28 19:35:56 -0700627 distal_victor_->SetSpeed(::aos::Clip(output.voltage_distal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800628 -kMaxBringupPower, kMaxBringupPower) /
629 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700630 hanger_victor_->SetSpeed(::aos::Clip(-output.voltage_winch(),
631 -kMaxBringupPower, kMaxBringupPower) /
632 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800633 }
634
635 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700636 AOS_LOG(WARNING, "Superstructure output too old.\n");
Austin Schuh2a3e0632018-02-19 16:24:49 -0800637
638 left_intake_rollers_victor_->SetDisabled();
639 right_intake_rollers_victor_->SetDisabled();
640 left_intake_elastic_victor_->SetDisabled();
641 right_intake_elastic_victor_->SetDisabled();
642
643 proximal_victor_->SetDisabled();
644 distal_victor_->SetDisabled();
Austin Schuh17e484e2018-03-11 01:11:36 -0800645 hanger_victor_->SetDisabled();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800646 }
647
648 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
649 right_intake_rollers_victor_, left_intake_elastic_victor_,
Austin Schuh17e484e2018-03-11 01:11:36 -0800650 right_intake_elastic_victor_, proximal_victor_, distal_victor_,
651 hanger_victor_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800652};
653
654class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
655 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800656 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
657 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
658 frc::Encoder::k4X);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800659 }
660
661 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700662 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800663 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700664
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700665 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700666 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700667 ::frc971::wpilib::JoystickSender joystick_sender(
668 &joystick_sender_event_loop);
669 AddLoop(&joystick_sender_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800670
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700671 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700672 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700673 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
674 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800675
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700676 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700677 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700678 SensorReader sensor_reader(&sensor_reader_event_loop);
679 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
680 sensor_reader.set_left_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800681 make_unique<frc::AnalogInput>(6));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700682 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
683 sensor_reader.set_right_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800684 make_unique<frc::AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800685
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700686 sensor_reader.set_proximal_encoder(make_encoder(4));
687 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(2));
688 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800689
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700690 sensor_reader.set_distal_encoder(make_encoder(2));
691 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(3));
692 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(3));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800693
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700694 sensor_reader.set_right_intake_encoder(make_encoder(5));
695 sensor_reader.set_right_intake_absolute_pwm(
696 make_unique<frc::DigitalInput>(7));
697 sensor_reader.set_right_intake_potentiometer(
698 make_unique<frc::AnalogInput>(1));
699 sensor_reader.set_right_intake_spring_angle(
700 make_unique<frc::AnalogInput>(5));
701 sensor_reader.set_right_intake_cube_detector(
702 make_unique<frc::DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800703
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700704 sensor_reader.set_left_intake_encoder(make_encoder(3));
705 sensor_reader.set_left_intake_absolute_pwm(
706 make_unique<frc::DigitalInput>(4));
707 sensor_reader.set_left_intake_potentiometer(
708 make_unique<frc::AnalogInput>(0));
709 sensor_reader.set_left_intake_spring_angle(
710 make_unique<frc::AnalogInput>(4));
711 sensor_reader.set_left_intake_cube_detector(
712 make_unique<frc::DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800713
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700714 sensor_reader.set_claw_beambreak(make_unique<frc::DigitalInput>(8));
715 sensor_reader.set_box_back_beambreak(make_unique<frc::DigitalInput>(9));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800716
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700717 sensor_reader.set_pwm_trigger(true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800718
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700719 sensor_reader.set_lidar_lite_input(make_unique<frc::DigitalInput>(22));
720 AddLoop(&sensor_reader_event_loop);
Austin Schuh8e5950d2018-03-21 20:29:40 -0700721
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700722 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700723 ::aos::ShmEventLoop imu_event_loop(&config.message());
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800724 auto imu_trigger = make_unique<frc::DigitalInput>(5);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700725 ::frc971::wpilib::ADIS16448 imu(
726 &imu_event_loop, frc::SPI::Port::kOnboardCS1, imu_trigger.get());
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800727 imu.SetDummySPI(frc::SPI::Port::kOnboardCS2);
728 auto imu_reset = make_unique<frc::DigitalOutput>(6);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800729 imu.set_reset(imu_reset.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700730 AddLoop(&imu_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800731
Austin Schuhe8a54c02018-03-05 00:25:58 -0800732 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
733 // they are identical, as far as DrivetrainWriter is concerned, to the SP
734 // variety so all the Victors are written as SPs.
Austin Schuh2a3e0632018-02-19 16:24:49 -0800735
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700736 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700737 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700738
739 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Daviscaa2a6b2019-02-03 01:15:37 -0800740 drivetrain_writer.set_left_controller0(
741 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)), false);
742 drivetrain_writer.set_right_controller0(
743 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800744
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700745 SuperstructureWriter superstructure_writer(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800746 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800747 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800748 superstructure_writer.set_left_intake_rollers_victor(
749 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800750 superstructure_writer.set_right_intake_elastic_victor(
751 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
752 superstructure_writer.set_right_intake_rollers_victor(
753 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800754 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800755 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800756 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800757 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800758 superstructure_writer.set_hanger_victor(
759 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800760
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700761 AddLoop(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800762
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700763 // Thread 6.
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700764 // This is a separate event loop because we want to run it at much lower
765 // priority.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700766 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Austin Schuhbfbaa372019-02-15 23:05:31 -0800767 ::frc971::wpilib::BufferedPcm pcm;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700768 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, &pcm);
Austin Schuhbfbaa372019-02-15 23:05:31 -0800769 solenoid_writer.set_left_drivetrain_shifter(pcm.MakeSolenoid(0));
770 solenoid_writer.set_right_drivetrain_shifter(pcm.MakeSolenoid(1));
771 solenoid_writer.set_claw(pcm.MakeSolenoid(2));
772 solenoid_writer.set_arm_brakes(pcm.MakeSolenoid(3));
773 solenoid_writer.set_hook(pcm.MakeSolenoid(4));
774 solenoid_writer.set_forks(pcm.MakeSolenoid(5));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700775 AddLoop(&solenoid_writer_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800776
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700777 RunLoops();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800778 }
779};
780
781} // namespace
782} // namespace wpilib
783} // namespace y2018
784
785AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);