blob: 9f5255d0231452f198ab01a2af228bea959541d2 [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"
52#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
53#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_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700150 event_loop->MakeSender<superstructure::Position>(
151 "/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 {
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700305 auto builder = superstructure_position_sender_.MakeBuilder();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800306
Alex Perrycb7da4b2019-08-28 19:35:56 -0700307 // Proximal arm
308 frc971::PotAndAbsolutePositionT arm_proximal;
309 CopyPosition(proximal_encoder_, &arm_proximal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800310 Values::kProximalEncoderCountsPerRevolution(),
311 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800312 true, values.arm_proximal.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700313 flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_proximal_offset =
314 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_proximal);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800315
Alex Perrycb7da4b2019-08-28 19:35:56 -0700316 // Distal arm
317 frc971::PotAndAbsolutePositionT arm_distal;
318 CopyPosition(distal_encoder_, &arm_distal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800319 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh6829f762018-03-02 21:36:01 -0800320 Values::kDistalEncoderRatio(), distal_pot_translate, true,
321 values.arm_distal.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322 flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_distal_offset =
323 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_distal);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800324
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325 superstructure::ArmPosition::Builder arm_position_builder =
326 builder.MakeBuilder<superstructure::ArmPosition>();
327 arm_position_builder.add_proximal(arm_proximal_offset);
328 arm_position_builder.add_distal(arm_distal_offset);
329
330 flatbuffers::Offset<superstructure::ArmPosition> arm_position_offset =
331 arm_position_builder.Finish();
332
333 // Left intake
334 frc971::PotAndAbsolutePositionT left_intake_motor_position;
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700335 CopyPosition(left_intake_encoder_, &left_intake_motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800336 Values::kIntakeMotorEncoderCountsPerRevolution(),
337 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Sabina Davis8d20ca82018-02-19 13:17:45 -0800338 false, values.left_intake.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700339 flatbuffers::Offset<frc971::PotAndAbsolutePosition>
340 left_intake_motor_position_offset =
341 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(),
342 &left_intake_motor_position);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800343
Alex Perrycb7da4b2019-08-28 19:35:56 -0700344 // Right intake
345 frc971::PotAndAbsolutePositionT right_intake_motor_position;
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700346 CopyPosition(right_intake_encoder_, &right_intake_motor_position,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800347 Values::kIntakeMotorEncoderCountsPerRevolution(),
348 Values::kIntakeMotorEncoderRatio(), intake_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800349 true, values.right_intake.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700350 flatbuffers::Offset<frc971::PotAndAbsolutePosition>
351 right_intake_motor_position_offset =
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700352 frc971::PotAndAbsolutePosition::Pack(
353 *builder.fbb(), &right_intake_motor_position);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800354
Alex Perrycb7da4b2019-08-28 19:35:56 -0700355 superstructure::IntakeElasticSensors::Builder
356 left_intake_sensors_builder =
357 builder.MakeBuilder<superstructure::IntakeElasticSensors>();
358
359 left_intake_sensors_builder.add_motor_position(
360 left_intake_motor_position_offset);
361 left_intake_sensors_builder.add_spring_angle(
Austin Schuh6829f762018-03-02 21:36:01 -0800362 intake_spring_translate(left_intake_spring_angle_->GetVoltage()) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700363 values.left_intake.spring_offset);
364 left_intake_sensors_builder.add_beam_break(
365 !left_intake_cube_detector_->Get());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800366
Alex Perrycb7da4b2019-08-28 19:35:56 -0700367 flatbuffers::Offset<superstructure::IntakeElasticSensors>
368 left_intake_offset = left_intake_sensors_builder.Finish();
369
370 superstructure::IntakeElasticSensors::Builder
371 right_intake_sensors_builder =
372 builder.MakeBuilder<superstructure::IntakeElasticSensors>();
373
374 right_intake_sensors_builder.add_motor_position(
375 right_intake_motor_position_offset);
376 right_intake_sensors_builder.add_spring_angle(
Austin Schuh6829f762018-03-02 21:36:01 -0800377 -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700378 values.right_intake.spring_offset);
379 right_intake_sensors_builder.add_beam_break(
380 !right_intake_cube_detector_->Get());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800381
Alex Perrycb7da4b2019-08-28 19:35:56 -0700382 flatbuffers::Offset<control_loops::superstructure::IntakeElasticSensors>
383 right_intake_offset = right_intake_sensors_builder.Finish();
Austin Schuh4ef51af2018-03-04 01:08:45 -0800384
Alex Perrycb7da4b2019-08-28 19:35:56 -0700385 superstructure::Position::Builder superstructure_builder =
386 builder.MakeBuilder<superstructure::Position>();
Austin Schuh8e5950d2018-03-21 20:29:40 -0700387
Alex Perrycb7da4b2019-08-28 19:35:56 -0700388 superstructure_builder.add_left_intake(left_intake_offset);
389 superstructure_builder.add_right_intake(right_intake_offset);
390 superstructure_builder.add_arm(arm_position_offset);
391
392 superstructure_builder.add_claw_beambreak_triggered(
393 !claw_beambreak_->Get());
394 superstructure_builder.add_box_back_beambreak_triggered(
395 !box_back_beambreak_->Get());
396
397 superstructure_builder.add_box_distance(lidar_lite_.last_width() /
398 0.00001 / 100.0 / 2);
399
milind1f1dca32021-07-03 13:50:07 -0700400 builder.CheckOk(builder.Send(superstructure_builder.Finish()));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800401 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800402 }
403
Austin Schuh2a3e0632018-02-19 16:24:49 -0800404 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700405 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
406 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700407 drivetrain_position_sender_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700408
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800409 ::std::unique_ptr<frc::AnalogInput> left_drivetrain_shifter_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800410 right_drivetrain_shifter_;
411
412 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
413 distal_encoder_;
414
415 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_,
416 right_intake_encoder_;
417
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800418 ::std::unique_ptr<frc::AnalogInput> left_intake_spring_angle_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800419 right_intake_spring_angle_;
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800420 ::std::unique_ptr<frc::DigitalInput> left_intake_cube_detector_,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800421 right_intake_cube_detector_;
422
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800423 ::std::unique_ptr<frc::DigitalInput> claw_beambreak_;
424 ::std::unique_ptr<frc::DigitalInput> box_back_beambreak_;
Austin Schuh4ef51af2018-03-04 01:08:45 -0800425
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800426 ::std::unique_ptr<frc::DigitalInput> lidar_lite_input_;
Austin Schuh8e5950d2018-03-21 20:29:40 -0700427 ::frc971::wpilib::DMAPulseWidthReader lidar_lite_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800428};
429
430class SolenoidWriter {
431 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800432 SolenoidWriter(::aos::ShmEventLoop *event_loop,
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700433 ::frc971::wpilib::BufferedPcm *pcm)
434 : event_loop_(event_loop),
435 drivetrain_fetcher_(
436 event_loop
Alex Perrycb7da4b2019-08-28 19:35:56 -0700437 ->MakeFetcher<::frc971::control_loops::drivetrain::Output>(
438 "/drivetrain")),
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700439 superstructure_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700440 event_loop->MakeFetcher<superstructure::Output>("/superstructure")),
441 status_light_fetcher_(
442 event_loop->MakeFetcher<::y2018::StatusLight>("/superstructure")),
Austin Schuh300f2f62019-05-27 13:49:23 -0700443 vision_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700444 event_loop->MakeFetcher<::y2018::vision::VisionStatus>("/vision")),
445 pneumatics_to_log_sender_(
446 event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")),
Austin Schuh9950f682021-11-06 15:27:58 -0700447 pcm_(pcm),
448 compressor_(0) {
Austin Schuh217a9782019-12-21 23:02:50 -0800449 event_loop->set_name("Solenoids");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700450 event_loop_->SetRuntimeRealtimePriority(27);
451
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700452 event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); },
453 ::std::chrono::milliseconds(20),
454 ::std::chrono::milliseconds(1));
455 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800456
457 // left drive
458 // right drive
459 //
460 // claw
461 // arm brakes
462 // hook release
463 // fork release
464 void set_left_drivetrain_shifter(
465 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
466 left_drivetrain_shifter_ = ::std::move(s);
467 }
468 void set_right_drivetrain_shifter(
469 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
470 right_drivetrain_shifter_ = ::std::move(s);
471 }
472
473 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
474 claw_ = ::std::move(s);
475 }
476
477 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
478 arm_brakes_ = ::std::move(s);
479 }
480
481 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
482 hook_ = ::std::move(s);
483 }
484
485 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
486 forks_ = ::std::move(s);
487 }
488
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700489 void Loop(const int iterations) {
490 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700491 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700492 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800493
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700494 {
495 drivetrain_fetcher_.Fetch();
496 if (drivetrain_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700497 left_drivetrain_shifter_->Set(!drivetrain_fetcher_->left_high());
498 right_drivetrain_shifter_->Set(!drivetrain_fetcher_->right_high());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700499 }
500 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800501
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700502 {
503 superstructure_fetcher_.Fetch();
504 if (superstructure_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700505 claw_->Set(!superstructure_fetcher_->claw_grabbed());
506 arm_brakes_->Set(superstructure_fetcher_->release_arm_brake());
507 hook_->Set(superstructure_fetcher_->hook_release());
508 forks_->Set(superstructure_fetcher_->forks_release());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700509 }
510 }
511
512 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700513 auto builder = pneumatics_to_log_sender_.MakeBuilder();
514
515 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
516 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700517
518 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700519 to_log_builder.add_read_solenoids(pcm_->GetAll());
milind1f1dca32021-07-03 13:50:07 -0700520 (void)builder.Send(to_log_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700521 }
522
523 monotonic_clock::time_point monotonic_now = event_loop_->monotonic_now();
524 status_light_fetcher_.Fetch();
525 // If we don't have a light request (or it's an old one), we are borked.
526 // Flash the red light slowly.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700527 StatusLightT color;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700528 if (!status_light_fetcher_.get() ||
Austin Schuhad154822019-12-27 15:45:13 -0800529 monotonic_now > status_light_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700530 chrono::milliseconds(100)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700531 color.red = 0.0;
532 color.green = 0.0;
533 color.blue = 0.0;
534
535 vision_status_fetcher_.Fetch();
536 ++light_flash_;
537 if (light_flash_ > 10) {
538 color.red = 0.5;
539 } else if (!vision_status_fetcher_.get() ||
540 monotonic_now >
Austin Schuhad154822019-12-27 15:45:13 -0800541 vision_status_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700542 chrono::seconds(1)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700543 color.red = 0.5;
544 color.green = 0.5;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800545 }
546
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700547 if (light_flash_ > 20) {
548 light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800549 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700550 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700551 status_light_fetcher_->UnPackTo(&color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800552 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700553 SetColor(color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800554 }
555
Alex Perrycb7da4b2019-08-28 19:35:56 -0700556 void SetColor(const StatusLightT &status_light) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700557 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
558 // it actually changes. This is pretty low priority anyways.
559 static int time_since_last_send = 0;
560 ++time_since_last_send;
561 if (time_since_last_send > 10) {
562 time_since_last_send = 0;
563 }
564 if (status_light.green != last_green_ || time_since_last_send == 0) {
565 canifier_.SetLEDOutput(1.0 - status_light.green,
566 ::ctre::phoenix::CANifier::LEDChannelB);
567 last_green_ = status_light.green;
568 }
569
570 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
571 canifier_.SetLEDOutput(1.0 - status_light.blue,
572 ::ctre::phoenix::CANifier::LEDChannelA);
573 last_blue_ = status_light.blue;
574 }
575
576 if (status_light.red != last_red_ || time_since_last_send == 0) {
577 canifier_.SetLEDOutput(1.0 - status_light.red,
578 ::ctre::phoenix::CANifier::LEDChannelC);
579 last_red_ = status_light.red;
580 }
581 }
582
Austin Schuh2a3e0632018-02-19 16:24:49 -0800583 void Quit() { run_ = false; }
584
585 private:
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700586 ::aos::EventLoop *event_loop_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700587 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output>
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700588 drivetrain_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700589 ::aos::Fetcher<superstructure::Output> superstructure_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700590 ::aos::Fetcher<::y2018::StatusLight> status_light_fetcher_;
Austin Schuh300f2f62019-05-27 13:49:23 -0700591 ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700592
Alex Perrycb7da4b2019-08-28 19:35:56 -0700593 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
594
Austin Schuh2a3e0632018-02-19 16:24:49 -0800595 ::frc971::wpilib::BufferedPcm *pcm_;
596
597 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
598 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
599 hook_, forks_;
600
Brian Silverman37281fc2018-03-11 18:42:17 -0700601 ::ctre::phoenix::CANifier canifier_{0};
602
Austin Schuh2a3e0632018-02-19 16:24:49 -0800603 ::std::atomic<bool> run_{true};
Austin Schuh8d5fff42018-05-30 20:44:12 -0700604
605 double last_red_ = -1.0;
606 double last_green_ = -1.0;
607 double last_blue_ = -1.0;
608
Austin Schuh9950f682021-11-06 15:27:58 -0700609 frc::Compressor compressor_;
610
Austin Schuh8d5fff42018-05-30 20:44:12 -0700611 int light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800612};
613
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700614class SuperstructureWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700615 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800616 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800617 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
619 event_loop, "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800620
Austin Schuh2a3e0632018-02-19 16:24:49 -0800621 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
622 proximal_victor_ = ::std::move(t);
623 }
624 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
625 distal_victor_ = ::std::move(t);
626 }
627
Austin Schuh17e484e2018-03-11 01:11:36 -0800628 void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) {
629 hanger_victor_ = ::std::move(t);
630 }
631
Austin Schuh2a3e0632018-02-19 16:24:49 -0800632 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
633 left_intake_elastic_victor_ = ::std::move(t);
634 }
635 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
636 right_intake_elastic_victor_ = ::std::move(t);
637 }
638
639 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
640 left_intake_rollers_victor_ = ::std::move(t);
641 }
642
643 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
644 right_intake_rollers_victor_ = ::std::move(t);
645 }
646
647 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700648 virtual void Write(const superstructure::Output &output) override {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800649 left_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700650 ::aos::Clip(-output.left_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800651 kMaxBringupPower) /
652 12.0);
653
654 right_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700655 ::aos::Clip(output.right_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800656 kMaxBringupPower) /
657 12.0);
658
659 left_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700660 ::aos::Clip(-output.left_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800661 kMaxBringupPower) /
662 12.0);
663
664 right_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700665 ::aos::Clip(output.right_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800666 kMaxBringupPower) /
667 12.0);
668
Alex Perrycb7da4b2019-08-28 19:35:56 -0700669 proximal_victor_->SetSpeed(::aos::Clip(-output.voltage_proximal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800670 -kMaxBringupPower,
671 kMaxBringupPower) /
672 12.0);
673
Alex Perrycb7da4b2019-08-28 19:35:56 -0700674 distal_victor_->SetSpeed(::aos::Clip(output.voltage_distal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800675 -kMaxBringupPower, kMaxBringupPower) /
676 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700677 hanger_victor_->SetSpeed(::aos::Clip(-output.voltage_winch(),
678 -kMaxBringupPower, kMaxBringupPower) /
679 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800680 }
681
682 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700683 AOS_LOG(WARNING, "Superstructure output too old.\n");
Austin Schuh2a3e0632018-02-19 16:24:49 -0800684
685 left_intake_rollers_victor_->SetDisabled();
686 right_intake_rollers_victor_->SetDisabled();
687 left_intake_elastic_victor_->SetDisabled();
688 right_intake_elastic_victor_->SetDisabled();
689
690 proximal_victor_->SetDisabled();
691 distal_victor_->SetDisabled();
Austin Schuh17e484e2018-03-11 01:11:36 -0800692 hanger_victor_->SetDisabled();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800693 }
694
695 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
696 right_intake_rollers_victor_, left_intake_elastic_victor_,
Austin Schuh17e484e2018-03-11 01:11:36 -0800697 right_intake_elastic_victor_, proximal_victor_, distal_victor_,
698 hanger_victor_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800699};
700
701class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
702 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800703 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
704 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
705 frc::Encoder::k4X);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800706 }
707
708 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700709 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800710 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700711
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700712 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700713 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700714 ::frc971::wpilib::JoystickSender joystick_sender(
715 &joystick_sender_event_loop);
716 AddLoop(&joystick_sender_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800717
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700718 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700719 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700720 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
721 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800722
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700723 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700724 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700725 SensorReader sensor_reader(&sensor_reader_event_loop);
726 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
727 sensor_reader.set_left_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800728 make_unique<frc::AnalogInput>(6));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700729 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
730 sensor_reader.set_right_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800731 make_unique<frc::AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800732
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700733 sensor_reader.set_proximal_encoder(make_encoder(4));
734 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(2));
735 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800736
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700737 sensor_reader.set_distal_encoder(make_encoder(2));
738 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(3));
739 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(3));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800740
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700741 sensor_reader.set_right_intake_encoder(make_encoder(5));
742 sensor_reader.set_right_intake_absolute_pwm(
743 make_unique<frc::DigitalInput>(7));
744 sensor_reader.set_right_intake_potentiometer(
745 make_unique<frc::AnalogInput>(1));
746 sensor_reader.set_right_intake_spring_angle(
747 make_unique<frc::AnalogInput>(5));
748 sensor_reader.set_right_intake_cube_detector(
749 make_unique<frc::DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800750
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700751 sensor_reader.set_left_intake_encoder(make_encoder(3));
752 sensor_reader.set_left_intake_absolute_pwm(
753 make_unique<frc::DigitalInput>(4));
754 sensor_reader.set_left_intake_potentiometer(
755 make_unique<frc::AnalogInput>(0));
756 sensor_reader.set_left_intake_spring_angle(
757 make_unique<frc::AnalogInput>(4));
758 sensor_reader.set_left_intake_cube_detector(
759 make_unique<frc::DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800760
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700761 sensor_reader.set_claw_beambreak(make_unique<frc::DigitalInput>(8));
762 sensor_reader.set_box_back_beambreak(make_unique<frc::DigitalInput>(9));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800763
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700764 sensor_reader.set_pwm_trigger(true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800765
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700766 sensor_reader.set_lidar_lite_input(make_unique<frc::DigitalInput>(22));
767 AddLoop(&sensor_reader_event_loop);
Austin Schuh8e5950d2018-03-21 20:29:40 -0700768
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700769 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700770 ::aos::ShmEventLoop imu_event_loop(&config.message());
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800771 auto imu_trigger = make_unique<frc::DigitalInput>(5);
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700772 ::frc971::wpilib::ADIS16448 imu(
773 &imu_event_loop, frc::SPI::Port::kOnboardCS1, imu_trigger.get());
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800774 imu.SetDummySPI(frc::SPI::Port::kOnboardCS2);
775 auto imu_reset = make_unique<frc::DigitalOutput>(6);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800776 imu.set_reset(imu_reset.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700777 AddLoop(&imu_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800778
Austin Schuhe8a54c02018-03-05 00:25:58 -0800779 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
780 // they are identical, as far as DrivetrainWriter is concerned, to the SP
781 // variety so all the Victors are written as SPs.
Austin Schuh2a3e0632018-02-19 16:24:49 -0800782
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700783 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700784 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700785
786 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Daviscaa2a6b2019-02-03 01:15:37 -0800787 drivetrain_writer.set_left_controller0(
788 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)), false);
789 drivetrain_writer.set_right_controller0(
790 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800791
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700792 SuperstructureWriter superstructure_writer(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800793 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800794 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800795 superstructure_writer.set_left_intake_rollers_victor(
796 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800797 superstructure_writer.set_right_intake_elastic_victor(
798 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
799 superstructure_writer.set_right_intake_rollers_victor(
800 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800801 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800802 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800803 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800804 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800805 superstructure_writer.set_hanger_victor(
806 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800807
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700808 AddLoop(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800809
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700810 // Thread 6.
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700811 // This is a separate event loop because we want to run it at much lower
812 // priority.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700813 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Austin Schuhbfbaa372019-02-15 23:05:31 -0800814 ::frc971::wpilib::BufferedPcm pcm;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700815 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, &pcm);
Austin Schuhbfbaa372019-02-15 23:05:31 -0800816 solenoid_writer.set_left_drivetrain_shifter(pcm.MakeSolenoid(0));
817 solenoid_writer.set_right_drivetrain_shifter(pcm.MakeSolenoid(1));
818 solenoid_writer.set_claw(pcm.MakeSolenoid(2));
819 solenoid_writer.set_arm_brakes(pcm.MakeSolenoid(3));
820 solenoid_writer.set_hook(pcm.MakeSolenoid(4));
821 solenoid_writer.set_forks(pcm.MakeSolenoid(5));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700822 AddLoop(&solenoid_writer_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800823
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700824 RunLoops();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800825 }
826};
827
828} // namespace
829} // namespace wpilib
830} // namespace y2018
831
832AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);