blob: 641a4740e3f97c55dd92acb6f1651e4b6c498b22 [file] [log] [blame]
Austin Schuh2a3e0632018-02-19 16:24:49 -08001#include <inttypes.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6#include <array>
7#include <chrono>
8#include <cmath>
9#include <functional>
Austin Schuh2a3e0632018-02-19 16:24:49 -080010#include <thread>
11
Parker Schuhd3b7a8872018-02-19 16:42:27 -080012#include "frc971/wpilib/ahal/AnalogInput.h"
13#include "frc971/wpilib/ahal/Counter.h"
14#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
15#include "frc971/wpilib/ahal/DriverStation.h"
16#include "frc971/wpilib/ahal/Encoder.h"
17#include "frc971/wpilib/ahal/Relay.h"
18#include "frc971/wpilib/ahal/Servo.h"
19#include "frc971/wpilib/ahal/VictorSP.h"
Brian Silverman37281fc2018-03-11 18:42:17 -070020#include "ctre/phoenix/CANifier.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080021#undef ERROR
22
John Park33858a32018-09-28 23:05:48 -070023#include "aos/commonmath.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024#include "aos/events/shm_event_loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080025#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070026#include "aos/logging/logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080027#include "aos/make_unique.h"
John Park33858a32018-09-28 23:05:48 -070028#include "aos/time/time.h"
29#include "aos/util/compiler_memory_barrier.h"
30#include "aos/util/log_interval.h"
31#include "aos/util/phased_loop.h"
32#include "aos/util/wrapping_counter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070033#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
34#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080035#include "frc971/wpilib/ADIS16448.h"
36#include "frc971/wpilib/buffered_pcm.h"
37#include "frc971/wpilib/buffered_solenoid.h"
38#include "frc971/wpilib/dma.h"
39#include "frc971/wpilib/dma_edge_counting.h"
Sabina Daviscaa2a6b2019-02-03 01:15:37 -080040#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080041#include "frc971/wpilib/encoder_and_potentiometer.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080042#include "frc971/wpilib/joystick_sender.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070043#include "frc971/wpilib/logging_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080044#include "frc971/wpilib/loop_output_handler.h"
45#include "frc971/wpilib/pdp_fetcher.h"
Austin Schuh6abf5b72019-02-02 20:20:54 -080046#include "frc971/wpilib/sensor_reader.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080047#include "frc971/wpilib/wpilib_robot_base.h"
48#include "y2018/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070049#include "y2018/control_loops/superstructure/superstructure_output_generated.h"
50#include "y2018/control_loops/superstructure/superstructure_position_generated.h"
51#include "y2018/status_light_generated.h"
52#include "y2018/vision/vision_generated.h"
Austin Schuh2a3e0632018-02-19 16:24:49 -080053
54#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
Brian Silvermanf819b442019-01-20 16:51:04 -080058using aos::make_unique;
Alex Perrycb7da4b2019-08-28 19:35:56 -070059using ::aos::monotonic_clock;
60using ::y2018::constants::Values;
61namespace chrono = ::std::chrono;
62namespace superstructure = ::y2018::control_loops::superstructure;
Austin Schuh2a3e0632018-02-19 16:24:49 -080063
64namespace y2018 {
65namespace wpilib {
66namespace {
67
68constexpr double kMaxBringupPower = 12.0;
69
70// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
71// DMA stuff and then removing the * 2.0 in *_translate.
72// The low bit is direction.
73
Austin Schuh2a3e0632018-02-19 16:24:49 -080074// TODO(brian): Use ::std::max instead once we have C++14 so that can be
75// constexpr.
Austin Schuh2a3e0632018-02-19 16:24:49 -080076template <typename T>
77constexpr T max(T a, T b) {
78 return (a > b) ? a : b;
79}
80
81template <typename T, typename... Rest>
82constexpr T max(T a, T b, T c, Rest... rest) {
83 return max(max(a, b), c, rest...);
84}
85
86double drivetrain_translate(int32_t in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080087 return ((static_cast<double>(in) /
88 Values::kDrivetrainEncoderCountsPerRevolution()) *
89 (2.0 * M_PI)) *
90 Values::kDrivetrainEncoderRatio() *
91 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -080092}
93
94double drivetrain_velocity_translate(double in) {
Austin Schuhe8a54c02018-03-05 00:25:58 -080095 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
96 (2.0 * M_PI)) *
97 Values::kDrivetrainEncoderRatio() *
98 control_loops::drivetrain::kWheelRadius;
Austin Schuh2a3e0632018-02-19 16:24:49 -080099}
100
101double proximal_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800102 return -voltage * Values::kProximalPotRatio() *
Austin Schuh2a3e0632018-02-19 16:24:49 -0800103 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
104}
105
106double distal_pot_translate(double voltage) {
107 return voltage * Values::kDistalPotRatio() *
108 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
109}
110
111double intake_pot_translate(double voltage) {
112 return voltage * Values::kIntakeMotorPotRatio() *
113 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
114}
115
116double intake_spring_translate(double voltage) {
117 return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) /
118 (5.0 /*volts*/);
119}
120
121// TODO() figure out differnce between max and min voltages on shifter pots.
122// Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be
123// passed drectly into the drivetrain position queue.
124double drivetrain_shifter_pot_translate(double voltage) {
Austin Schuh6829f762018-03-02 21:36:01 -0800125 return (voltage - Values::kDrivetrainShifterPotMinVoltage()) /
126 (Values::kDrivetrainShifterPotMaxVoltage() -
127 Values::kDrivetrainShifterPotMinVoltage());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800128}
129
130constexpr double kMaxFastEncoderPulsesPerSecond =
131 max(Values::kMaxDrivetrainEncoderPulsesPerSecond(),
132 Values::kMaxIntakeMotorEncoderPulsesPerSecond());
133static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
134 "fast encoders are too fast");
135
136constexpr double kMaxMediumEncoderPulsesPerSecond =
137 max(Values::kMaxProximalEncoderPulsesPerSecond(),
138 Values::kMaxDistalEncoderPulsesPerSecond());
139static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
140 "medium encoders are too fast");
141
142// Class to send position messages with sensor readings to our loops.
Austin Schuh6abf5b72019-02-02 20:20:54 -0800143class SensorReader : public ::frc971::wpilib::SensorReader {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800144 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800145 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700146 : ::frc971::wpilib::SensorReader(event_loop),
147 superstructure_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 event_loop->MakeSender<superstructure::Position>(
149 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700150 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 event_loop
152 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
153 "/drivetrain")) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800154 // Set to filter out anything shorter than 1/4 of the minimum pulse width
155 // we should ever see.
Austin Schuh6abf5b72019-02-02 20:20:54 -0800156 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
157 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800158 }
159
160 void set_left_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800161 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800162 left_drivetrain_shifter_ = ::std::move(potentiometer);
163 }
164
Austin Schuh2a3e0632018-02-19 16:24:49 -0800165 void set_right_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800166 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800167 right_drivetrain_shifter_ = ::std::move(potentiometer);
168 }
169
170 // Proximal joint.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800171 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800172 medium_encoder_filter_.Add(encoder.get());
173 proximal_encoder_.set_encoder(::std::move(encoder));
174 }
175
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800176 void set_proximal_absolute_pwm(
177 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800178 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
179 }
180
181 void set_proximal_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800182 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800183 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
184 }
185
186 // Distal joint.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800187 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800188 medium_encoder_filter_.Add(encoder.get());
189 distal_encoder_.set_encoder(::std::move(encoder));
190 }
191
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800192 void set_distal_absolute_pwm(
193 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800194 fast_encoder_filter_.Add(absolute_pwm.get());
195 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
196 }
197
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800198 void set_distal_potentiometer(
199 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800200 distal_encoder_.set_potentiometer(::std::move(potentiometer));
201 }
202
203 // Left intake side.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800204 void set_left_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800205 fast_encoder_filter_.Add(encoder.get());
206 left_intake_encoder_.set_encoder(::std::move(encoder));
207 }
208
209 void set_left_intake_absolute_pwm(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800210 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800211 fast_encoder_filter_.Add(absolute_pwm.get());
212 left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
213 }
214
215 void set_left_intake_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800216 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800217 left_intake_encoder_.set_potentiometer(::std::move(potentiometer));
218 }
219
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800220 void set_left_intake_spring_angle(
221 ::std::unique_ptr<frc::AnalogInput> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800222 left_intake_spring_angle_ = ::std::move(encoder);
223 }
224
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800225 void set_left_intake_cube_detector(
226 ::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800227 left_intake_cube_detector_ = ::std::move(input);
228 }
229
230 // Right intake side.
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800231 void set_right_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800232 fast_encoder_filter_.Add(encoder.get());
233 right_intake_encoder_.set_encoder(::std::move(encoder));
234 }
235
236 void set_right_intake_absolute_pwm(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800237 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800238 fast_encoder_filter_.Add(absolute_pwm.get());
239 right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
240 }
241
242 void set_right_intake_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800243 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800244 right_intake_encoder_.set_potentiometer(::std::move(potentiometer));
245 }
246
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800247 void set_right_intake_spring_angle(
248 ::std::unique_ptr<frc::AnalogInput> encoder) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800249 right_intake_spring_angle_ = ::std::move(encoder);
250 }
251
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800252 void set_right_intake_cube_detector(
253 ::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800254 right_intake_cube_detector_ = ::std::move(input);
255 }
256
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800257 void set_claw_beambreak(::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh4ef51af2018-03-04 01:08:45 -0800258 claw_beambreak_ = ::std::move(input);
259 }
260
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800261 void set_box_back_beambreak(::std::unique_ptr<frc::DigitalInput> input) {
Austin Schuh4ef51af2018-03-04 01:08:45 -0800262 box_back_beambreak_ = ::std::move(input);
263 }
264
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800265 void set_lidar_lite_input(::std::unique_ptr<frc::DigitalInput> lidar_lite_input) {
Austin Schuh8e5950d2018-03-21 20:29:40 -0700266 lidar_lite_input_ = ::std::move(lidar_lite_input);
267 lidar_lite_.set_input(lidar_lite_input_.get());
268 }
269
Austin Schuh6abf5b72019-02-02 20:20:54 -0800270 void Start() { AddToDMA(&lidar_lite_); }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800271
272 void RunIteration() {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800273 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700274 auto builder = drivetrain_position_sender_.MakeBuilder();
275 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
276 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800277
Alex Perrycb7da4b2019-08-28 19:35:56 -0700278 drivetrain_builder.add_left_encoder(
279 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
280 drivetrain_builder.add_left_speed (
281 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
282 drivetrain_builder.add_left_shifter_position (
Austin Schuh2a3e0632018-02-19 16:24:49 -0800283 drivetrain_shifter_pot_translate(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 left_drivetrain_shifter_->GetVoltage()));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800285
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 drivetrain_builder.add_right_encoder (
287 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
288 drivetrain_builder.add_right_speed (
289 -drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()));
290 drivetrain_builder.add_right_shifter_position (
291 drivetrain_shifter_pot_translate(
292 right_drivetrain_shifter_->GetVoltage()));
293
294 builder.Send(drivetrain_builder.Finish());
Austin Schuh2a3e0632018-02-19 16:24:49 -0800295 }
Austin Schuh6abf5b72019-02-02 20:20:54 -0800296 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800297
Austin Schuh6abf5b72019-02-02 20:20:54 -0800298 void RunDmaIteration() {
299 const auto values = constants::GetValues();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800300
301 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 auto builder =
303 superstructure_position_sender_.MakeBuilder();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800304
Alex Perrycb7da4b2019-08-28 19:35:56 -0700305 // Proximal arm
306 frc971::PotAndAbsolutePositionT arm_proximal;
307 CopyPosition(proximal_encoder_, &arm_proximal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800308 Values::kProximalEncoderCountsPerRevolution(),
309 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh6829f762018-03-02 21:36:01 -0800310 true, values.arm_proximal.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311 flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_proximal_offset =
312 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_proximal);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800313
Alex Perrycb7da4b2019-08-28 19:35:56 -0700314 // Distal arm
315 frc971::PotAndAbsolutePositionT arm_distal;
316 CopyPosition(distal_encoder_, &arm_distal,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800317 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh6829f762018-03-02 21:36:01 -0800318 Values::kDistalEncoderRatio(), distal_pot_translate, true,
319 values.arm_distal.potentiometer_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700320 flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_distal_offset =
321 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_distal);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800322
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323 superstructure::ArmPosition::Builder arm_position_builder =
324 builder.MakeBuilder<superstructure::ArmPosition>();
325 arm_position_builder.add_proximal(arm_proximal_offset);
326 arm_position_builder.add_distal(arm_distal_offset);
327
328 flatbuffers::Offset<superstructure::ArmPosition> arm_position_offset =
329 arm_position_builder.Finish();
330
331 // Left intake
332 frc971::PotAndAbsolutePositionT left_intake_motor_position;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800333 CopyPosition(left_intake_encoder_,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700334 &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;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800345 CopyPosition(right_intake_encoder_,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346 &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 =
352 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(),
353 &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
400 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 Schuhbd1fe9c2019-06-29 16:35:48 -0700447 pcm_(pcm) {
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
451 int32_t status = 0;
452 HAL_CompressorHandle compressor_ = HAL_InitializeCompressor(0, &status);
453 if (status != 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700454 AOS_LOG(ERROR, "Compressor status is nonzero, %d\n",
455 static_cast<int>(status));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700456 }
457 HAL_SetCompressorClosedLoopControl(compressor_, true, &status);
458 if (status != 0) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700459 AOS_LOG(ERROR, "Compressor status is nonzero, %d\n",
460 static_cast<int>(status));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700461 }
462
463 event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); },
464 ::std::chrono::milliseconds(20),
465 ::std::chrono::milliseconds(1));
466 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800467
468 // left drive
469 // right drive
470 //
471 // claw
472 // arm brakes
473 // hook release
474 // fork release
475 void set_left_drivetrain_shifter(
476 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
477 left_drivetrain_shifter_ = ::std::move(s);
478 }
479 void set_right_drivetrain_shifter(
480 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
481 right_drivetrain_shifter_ = ::std::move(s);
482 }
483
484 void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
485 claw_ = ::std::move(s);
486 }
487
488 void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
489 arm_brakes_ = ::std::move(s);
490 }
491
492 void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
493 hook_ = ::std::move(s);
494 }
495
496 void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
497 forks_ = ::std::move(s);
498 }
499
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700500 void Loop(const int iterations) {
501 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700502 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700503 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800504
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700505 {
506 drivetrain_fetcher_.Fetch();
507 if (drivetrain_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700508 left_drivetrain_shifter_->Set(!drivetrain_fetcher_->left_high());
509 right_drivetrain_shifter_->Set(!drivetrain_fetcher_->right_high());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700510 }
511 }
Austin Schuh2a3e0632018-02-19 16:24:49 -0800512
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700513 {
514 superstructure_fetcher_.Fetch();
515 if (superstructure_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700516 claw_->Set(!superstructure_fetcher_->claw_grabbed());
517 arm_brakes_->Set(superstructure_fetcher_->release_arm_brake());
518 hook_->Set(superstructure_fetcher_->hook_release());
519 forks_->Set(superstructure_fetcher_->forks_release());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700520 }
521 }
522
523 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700524 auto builder = pneumatics_to_log_sender_.MakeBuilder();
525
526 ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder =
527 builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700528
529 pcm_->Flush();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700530 to_log_builder.add_read_solenoids(pcm_->GetAll());
531 builder.Send(to_log_builder.Finish());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700532 }
533
534 monotonic_clock::time_point monotonic_now = event_loop_->monotonic_now();
535 status_light_fetcher_.Fetch();
536 // If we don't have a light request (or it's an old one), we are borked.
537 // Flash the red light slowly.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538 StatusLightT color;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700539 if (!status_light_fetcher_.get() ||
Austin Schuhad154822019-12-27 15:45:13 -0800540 monotonic_now > status_light_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700541 chrono::milliseconds(100)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700542 color.red = 0.0;
543 color.green = 0.0;
544 color.blue = 0.0;
545
546 vision_status_fetcher_.Fetch();
547 ++light_flash_;
548 if (light_flash_ > 10) {
549 color.red = 0.5;
550 } else if (!vision_status_fetcher_.get() ||
551 monotonic_now >
Austin Schuhad154822019-12-27 15:45:13 -0800552 vision_status_fetcher_.context().monotonic_event_time +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700553 chrono::seconds(1)) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700554 color.red = 0.5;
555 color.green = 0.5;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800556 }
557
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700558 if (light_flash_ > 20) {
559 light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800560 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700561 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700562 status_light_fetcher_->UnPackTo(&color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800563 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700564 SetColor(color);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800565 }
566
Alex Perrycb7da4b2019-08-28 19:35:56 -0700567 void SetColor(const StatusLightT &status_light) {
Austin Schuh8d5fff42018-05-30 20:44:12 -0700568 // Save CAN bandwidth and CPU at the cost of RT. Only change the light when
569 // it actually changes. This is pretty low priority anyways.
570 static int time_since_last_send = 0;
571 ++time_since_last_send;
572 if (time_since_last_send > 10) {
573 time_since_last_send = 0;
574 }
575 if (status_light.green != last_green_ || time_since_last_send == 0) {
576 canifier_.SetLEDOutput(1.0 - status_light.green,
577 ::ctre::phoenix::CANifier::LEDChannelB);
578 last_green_ = status_light.green;
579 }
580
581 if (status_light.blue != last_blue_ || time_since_last_send == 0) {
582 canifier_.SetLEDOutput(1.0 - status_light.blue,
583 ::ctre::phoenix::CANifier::LEDChannelA);
584 last_blue_ = status_light.blue;
585 }
586
587 if (status_light.red != last_red_ || time_since_last_send == 0) {
588 canifier_.SetLEDOutput(1.0 - status_light.red,
589 ::ctre::phoenix::CANifier::LEDChannelC);
590 last_red_ = status_light.red;
591 }
592 }
593
Austin Schuh2a3e0632018-02-19 16:24:49 -0800594 void Quit() { run_ = false; }
595
596 private:
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700597 ::aos::EventLoop *event_loop_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700598 ::aos::Fetcher<::frc971::control_loops::drivetrain::Output>
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700599 drivetrain_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700600 ::aos::Fetcher<superstructure::Output> superstructure_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700601 ::aos::Fetcher<::y2018::StatusLight> status_light_fetcher_;
Austin Schuh300f2f62019-05-27 13:49:23 -0700602 ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700603
Alex Perrycb7da4b2019-08-28 19:35:56 -0700604 aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_;
605
Austin Schuh2a3e0632018-02-19 16:24:49 -0800606 ::frc971::wpilib::BufferedPcm *pcm_;
607
608 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid>
609 left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_,
610 hook_, forks_;
611
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700612 HAL_CompressorHandle compressor_;
613
Brian Silverman37281fc2018-03-11 18:42:17 -0700614 ::ctre::phoenix::CANifier canifier_{0};
615
Austin Schuh2a3e0632018-02-19 16:24:49 -0800616 ::std::atomic<bool> run_{true};
Austin Schuh8d5fff42018-05-30 20:44:12 -0700617
618 double last_red_ = -1.0;
619 double last_green_ = -1.0;
620 double last_blue_ = -1.0;
621
622 int light_flash_ = 0;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800623};
624
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700625class SuperstructureWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700626 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800627 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800628 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700629 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
630 event_loop, "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800631
Austin Schuh2a3e0632018-02-19 16:24:49 -0800632 void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) {
633 proximal_victor_ = ::std::move(t);
634 }
635 void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) {
636 distal_victor_ = ::std::move(t);
637 }
638
Austin Schuh17e484e2018-03-11 01:11:36 -0800639 void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) {
640 hanger_victor_ = ::std::move(t);
641 }
642
Austin Schuh2a3e0632018-02-19 16:24:49 -0800643 void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
644 left_intake_elastic_victor_ = ::std::move(t);
645 }
646 void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) {
647 right_intake_elastic_victor_ = ::std::move(t);
648 }
649
650 void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
651 left_intake_rollers_victor_ = ::std::move(t);
652 }
653
654 void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
655 right_intake_rollers_victor_ = ::std::move(t);
656 }
657
658 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700659 virtual void Write(const superstructure::Output &output) override {
Austin Schuh2a3e0632018-02-19 16:24:49 -0800660 left_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700661 ::aos::Clip(-output.left_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800662 kMaxBringupPower) /
663 12.0);
664
665 right_intake_elastic_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700666 ::aos::Clip(output.right_intake()->voltage_elastic(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800667 kMaxBringupPower) /
668 12.0);
669
670 left_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700671 ::aos::Clip(-output.left_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800672 kMaxBringupPower) /
673 12.0);
674
675 right_intake_rollers_victor_->SetSpeed(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700676 ::aos::Clip(output.right_intake()->voltage_rollers(), -kMaxBringupPower,
Austin Schuh2a3e0632018-02-19 16:24:49 -0800677 kMaxBringupPower) /
678 12.0);
679
Alex Perrycb7da4b2019-08-28 19:35:56 -0700680 proximal_victor_->SetSpeed(::aos::Clip(-output.voltage_proximal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800681 -kMaxBringupPower,
682 kMaxBringupPower) /
683 12.0);
684
Alex Perrycb7da4b2019-08-28 19:35:56 -0700685 distal_victor_->SetSpeed(::aos::Clip(output.voltage_distal(),
Austin Schuh2a3e0632018-02-19 16:24:49 -0800686 -kMaxBringupPower, kMaxBringupPower) /
687 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700688 hanger_victor_->SetSpeed(::aos::Clip(-output.voltage_winch(),
689 -kMaxBringupPower, kMaxBringupPower) /
690 12.0);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800691 }
692
693 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700694 AOS_LOG(WARNING, "Superstructure output too old.\n");
Austin Schuh2a3e0632018-02-19 16:24:49 -0800695
696 left_intake_rollers_victor_->SetDisabled();
697 right_intake_rollers_victor_->SetDisabled();
698 left_intake_elastic_victor_->SetDisabled();
699 right_intake_elastic_victor_->SetDisabled();
700
701 proximal_victor_->SetDisabled();
702 distal_victor_->SetDisabled();
Austin Schuh17e484e2018-03-11 01:11:36 -0800703 hanger_victor_->SetDisabled();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800704 }
705
706 ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_,
707 right_intake_rollers_victor_, left_intake_elastic_victor_,
Austin Schuh17e484e2018-03-11 01:11:36 -0800708 right_intake_elastic_victor_, proximal_victor_, distal_victor_,
709 hanger_victor_;
Austin Schuh2a3e0632018-02-19 16:24:49 -0800710};
711
712class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
713 public:
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800714 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
715 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
716 frc::Encoder::k4X);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800717 }
718
719 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700720 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
721 aos::configuration::ReadConfig("config.json");
722
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700723 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700724 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700725 ::frc971::wpilib::JoystickSender joystick_sender(
726 &joystick_sender_event_loop);
727 AddLoop(&joystick_sender_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800728
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700729 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700730 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700731 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
732 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800733
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700734 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700735 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700736 SensorReader sensor_reader(&sensor_reader_event_loop);
737 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
738 sensor_reader.set_left_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800739 make_unique<frc::AnalogInput>(6));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700740 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
741 sensor_reader.set_right_drivetrain_shifter_potentiometer(
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800742 make_unique<frc::AnalogInput>(7));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800743
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700744 sensor_reader.set_proximal_encoder(make_encoder(4));
745 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(2));
746 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(2));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800747
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700748 sensor_reader.set_distal_encoder(make_encoder(2));
749 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(3));
750 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(3));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800751
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700752 sensor_reader.set_right_intake_encoder(make_encoder(5));
753 sensor_reader.set_right_intake_absolute_pwm(
754 make_unique<frc::DigitalInput>(7));
755 sensor_reader.set_right_intake_potentiometer(
756 make_unique<frc::AnalogInput>(1));
757 sensor_reader.set_right_intake_spring_angle(
758 make_unique<frc::AnalogInput>(5));
759 sensor_reader.set_right_intake_cube_detector(
760 make_unique<frc::DigitalInput>(1));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800761
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700762 sensor_reader.set_left_intake_encoder(make_encoder(3));
763 sensor_reader.set_left_intake_absolute_pwm(
764 make_unique<frc::DigitalInput>(4));
765 sensor_reader.set_left_intake_potentiometer(
766 make_unique<frc::AnalogInput>(0));
767 sensor_reader.set_left_intake_spring_angle(
768 make_unique<frc::AnalogInput>(4));
769 sensor_reader.set_left_intake_cube_detector(
770 make_unique<frc::DigitalInput>(0));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800771
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700772 sensor_reader.set_claw_beambreak(make_unique<frc::DigitalInput>(8));
773 sensor_reader.set_box_back_beambreak(make_unique<frc::DigitalInput>(9));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800774
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700775 sensor_reader.set_pwm_trigger(true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800776
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700777 sensor_reader.set_lidar_lite_input(make_unique<frc::DigitalInput>(22));
778 AddLoop(&sensor_reader_event_loop);
Austin Schuh8e5950d2018-03-21 20:29:40 -0700779
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700780 // Thread 4.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700781 ::aos::ShmEventLoop imu_event_loop(&config.message());
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800782 auto imu_trigger = make_unique<frc::DigitalInput>(5);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700783 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, frc::SPI::Port::kOnboardCS1,
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800784 imu_trigger.get());
785 imu.SetDummySPI(frc::SPI::Port::kOnboardCS2);
786 auto imu_reset = make_unique<frc::DigitalOutput>(6);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800787 imu.set_reset(imu_reset.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700788 AddLoop(&imu_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800789
Austin Schuhe8a54c02018-03-05 00:25:58 -0800790 // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though
791 // they are identical, as far as DrivetrainWriter is concerned, to the SP
792 // variety so all the Victors are written as SPs.
Austin Schuh2a3e0632018-02-19 16:24:49 -0800793
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700794 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700795 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700796
797 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Daviscaa2a6b2019-02-03 01:15:37 -0800798 drivetrain_writer.set_left_controller0(
799 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)), false);
800 drivetrain_writer.set_right_controller0(
801 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), true);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800802
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700803 SuperstructureWriter superstructure_writer(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800804 superstructure_writer.set_left_intake_elastic_victor(
Austin Schuh2a3e0632018-02-19 16:24:49 -0800805 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800806 superstructure_writer.set_left_intake_rollers_victor(
807 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Austin Schuh6829f762018-03-02 21:36:01 -0800808 superstructure_writer.set_right_intake_elastic_victor(
809 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)));
810 superstructure_writer.set_right_intake_rollers_victor(
811 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800812 superstructure_writer.set_proximal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800813 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800814 superstructure_writer.set_distal_victor(
Austin Schuh6829f762018-03-02 21:36:01 -0800815 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Austin Schuh17e484e2018-03-11 01:11:36 -0800816 superstructure_writer.set_hanger_victor(
817 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuh2a3e0632018-02-19 16:24:49 -0800818
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700819 AddLoop(&output_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800820
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700821 // Thread 6.
Austin Schuh01a9f2a2019-05-27 13:36:30 -0700822 // This is a separate event loop because we want to run it at much lower
823 // priority.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700824 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Austin Schuhbfbaa372019-02-15 23:05:31 -0800825 ::frc971::wpilib::BufferedPcm pcm;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700826 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, &pcm);
Austin Schuhbfbaa372019-02-15 23:05:31 -0800827 solenoid_writer.set_left_drivetrain_shifter(pcm.MakeSolenoid(0));
828 solenoid_writer.set_right_drivetrain_shifter(pcm.MakeSolenoid(1));
829 solenoid_writer.set_claw(pcm.MakeSolenoid(2));
830 solenoid_writer.set_arm_brakes(pcm.MakeSolenoid(3));
831 solenoid_writer.set_hook(pcm.MakeSolenoid(4));
832 solenoid_writer.set_forks(pcm.MakeSolenoid(5));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700833 AddLoop(&solenoid_writer_event_loop);
Austin Schuh2a3e0632018-02-19 16:24:49 -0800834
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700835 RunLoops();
Austin Schuh2a3e0632018-02-19 16:24:49 -0800836 }
837};
838
839} // namespace
840} // namespace wpilib
841} // namespace y2018
842
843AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot);