blob: 787d9a51cae63f156c388529876d078947647df3 [file] [log] [blame]
Sabina Davis2243cab2019-02-05 21:45:08 -08001#include <inttypes.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00005
Brian Silverman68cb5c22016-03-20 18:11:14 -07006#include <array>
Sabina Davis2243cab2019-02-05 21:45:08 -08007#include <chrono>
8#include <functional>
9#include <mutex>
10#include <thread>
Comran Morshed9a9948c2016-01-16 15:58:04 +000011
Parker Schuhd3b7a8872018-02-19 16:42:27 -080012#include "frc971/wpilib/ahal/AnalogInput.h"
13#include "frc971/wpilib/ahal/Compressor.h"
14#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
15#include "frc971/wpilib/ahal/DriverStation.h"
16#include "frc971/wpilib/ahal/Encoder.h"
17#include "frc971/wpilib/ahal/Relay.h"
18#include "frc971/wpilib/ahal/Talon.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000019#include "frc971/wpilib/wpilib_robot_base.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000020#undef ERROR
21
Brian Silvermanf819b442019-01-20 16:51:04 -080022#include "aos/commonmath.h"
23#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070024#include "aos/logging/logging.h"
25#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080026#include "aos/make_unique.h"
27#include "aos/robot_state/robot_state.q.h"
28#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/time/time.h"
30#include "aos/util/log_interval.h"
31#include "aos/util/phased_loop.h"
32#include "aos/util/wrapping_counter.h"
Philipp Schrader4bd29b12017-02-22 04:42:27 +000033#include "frc971/autonomous/auto.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000034#include "frc971/control_loops/control_loops.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000035#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080036#include "frc971/wpilib/ADIS16448.h"
37#include "frc971/wpilib/buffered_pcm.h"
38#include "frc971/wpilib/buffered_solenoid.h"
39#include "frc971/wpilib/dma.h"
40#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davisb71bc282019-02-03 01:17:23 -080041#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080042#include "frc971/wpilib/encoder_and_potentiometer.h"
43#include "frc971/wpilib/gyro_sender.h"
44#include "frc971/wpilib/interrupt_edge_counting.h"
45#include "frc971/wpilib/joystick_sender.h"
46#include "frc971/wpilib/logging.q.h"
47#include "frc971/wpilib/loop_output_handler.h"
48#include "frc971/wpilib/pdp_fetcher.h"
49#include "frc971/wpilib/sensor_reader.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000050#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000051#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000052#include "y2016/control_loops/shooter/shooter.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080053#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000054#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000055#include "y2016/queues/ball_detector.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000056
Comran Morshed9a9948c2016-01-16 15:58:04 +000057#ifndef M_PI
58#define M_PI 3.14159265358979323846
59#endif
60
61using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000062using ::y2016::control_loops::shooter::shooter_queue;
63using ::y2016::control_loops::superstructure_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080064using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080065using aos::make_unique;
Comran Morshed9a9948c2016-01-16 15:58:04 +000066
Comran Morshed6c6a0a92016-01-17 12:45:16 +000067namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000068namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080069namespace {
70constexpr double kMaxBringupPower = 12.0;
71} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000072
73// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
74// DMA stuff and then removing the * 2.0 in *_translate.
75// The low bit is direction.
76
Comran Morshed225f0b92016-02-10 20:34:27 +000077// Translates for the sensor values to convert raw index pulses into something
78// with proper units.
79
80// TODO(comran): Template these methods since there is a lot of repetition here.
81double hall_translate(double in) {
82 // Turn voltage from our 3-state halls into a ratio that the loop can use.
83 return in / 5.0;
84}
85
Comran Morshed9a9948c2016-01-16 15:58:04 +000086double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000087 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000088 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080089 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000090}
91
92double drivetrain_velocity_translate(double in) {
93 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000094 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080095 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000096}
97
Comran Morshed225f0b92016-02-10 20:34:27 +000098double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000099 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000100 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
101}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000102
Comran Morshed225f0b92016-02-10 20:34:27 +0000103double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800104 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000105 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
106}
107
108double shoulder_translate(int32_t in) {
109 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
110 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
111}
112
113double wrist_translate(int32_t in) {
114 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
115 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
116}
117
118double intake_pot_translate(double voltage) {
119 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000120 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000121}
122
123double shoulder_pot_translate(double voltage) {
124 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000125 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000126}
127
128double wrist_pot_translate(double voltage) {
129 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000130 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000131}
132
Comran Morshed1b764322016-02-14 20:18:12 +0000133constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
134 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
135 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
136 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
137
138constexpr double kMaxShooterEncoderPulsesPerSecond =
139 18700.0 /* 775pro free speed RPM */ * 12.0 /
140 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
141 128.0 /* CPR */ * 4 /* edges per cycle */;
142
143double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
144 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
145
146constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
147 18700.0 /* 775pro free speed RPM */ * 12.0 /
148 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
149 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000150
Comran Morshed225f0b92016-02-10 20:34:27 +0000151// Class to send position messages with sensor readings to our loops.
Austin Schuh54667ac2019-02-02 16:44:49 -0800152class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000153 public:
154 SensorReader() {
155 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
156 // we should ever see.
Austin Schuh54667ac2019-02-02 16:44:49 -0800157 UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond);
158 UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000159 hall_filter_.SetPeriodNanoSeconds(100000);
160 }
161
Comran Morshed225f0b92016-02-10 20:34:27 +0000162 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
163 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000164 }
165
Comran Morshed225f0b92016-02-10 20:34:27 +0000166 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
167 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000168 }
169
Comran Morshed225f0b92016-02-10 20:34:27 +0000170 // Shooter setters.
171 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800172 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000173 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174 }
175
Comran Morshed225f0b92016-02-10 20:34:27 +0000176 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800177 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000178 shooter_right_encoder_ = ::std::move(encoder);
179 }
180
181 // Intake setters.
182 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800183 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000184 intake_encoder_.set_encoder(::std::move(encoder));
185 }
186
187 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
188 intake_encoder_.set_potentiometer(::std::move(potentiometer));
189 }
190
191 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800192 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000193 intake_encoder_.set_index(::std::move(index));
194 }
195
196 // Shoulder setters.
197 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800198 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000199 shoulder_encoder_.set_encoder(::std::move(encoder));
200 }
201
202 void set_shoulder_potentiometer(
203 ::std::unique_ptr<AnalogInput> potentiometer) {
204 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
205 }
206
207 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800208 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000209 shoulder_encoder_.set_index(::std::move(index));
210 }
211
212 // Wrist setters.
213 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800214 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000215 wrist_encoder_.set_encoder(::std::move(encoder));
216 }
217
218 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
219 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
220 }
221
222 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800223 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000224 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000225 }
226
Comran Morshedaa0573c2016-03-05 19:05:54 +0000227 // Ball detector setter.
228 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
229 ball_detector_ = ::std::move(analog);
230 }
231
Brian Silverman68cb5c22016-03-20 18:11:14 -0700232 // Autonomous mode switch setter.
233 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
234 autonomous_modes_.at(i) = ::std::move(sensor);
235 }
236
Comran Morshed9a9948c2016-01-16 15:58:04 +0000237 // All of the DMA-related set_* calls must be made before this, and it doesn't
238 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000239
Austin Schuh54667ac2019-02-02 16:44:49 -0800240 void Start() {
241 AddToDMA(&intake_encoder_);
242 AddToDMA(&shoulder_encoder_);
243 AddToDMA(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000244 }
245
246 void RunIteration() {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247 {
248 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
249 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800250 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251 drivetrain_message->left_encoder =
252 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
253 drivetrain_message->left_speed =
254 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
255 drivetrain_message->right_speed =
256 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
257
Comran Morshed9a9948c2016-01-16 15:58:04 +0000258 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000259 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000260 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000261 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000262
263 drivetrain_message.Send();
264 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800265 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000266
Austin Schuh54667ac2019-02-02 16:44:49 -0800267 void RunDmaIteration() {
268 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000269 {
270 auto shooter_message = shooter_queue.position.MakeMessage();
271 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800272 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000273 shooter_message->theta_right =
274 shooter_translate(shooter_right_encoder_->GetRaw());
275 shooter_message.Send();
276 }
277
278 {
279 auto superstructure_message = superstructure_queue.position.MakeMessage();
Sabina Davis2243cab2019-02-05 21:45:08 -0800280 CopyPosition(intake_encoder_, &superstructure_message->intake,
281 intake_translate, intake_pot_translate, false,
282 values.intake.pot_offset);
283 CopyPosition(shoulder_encoder_, &superstructure_message->shoulder,
284 shoulder_translate, shoulder_pot_translate, false,
285 values.shoulder.pot_offset);
286 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
287 wrist_translate, wrist_pot_translate, true,
288 values.wrist.pot_offset);
Comran Morshed225f0b92016-02-10 20:34:27 +0000289
290 superstructure_message.Send();
291 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000292
293 {
294 auto ball_detector_message =
295 ::y2016::sensors::ball_detector.MakeMessage();
296 ball_detector_message->voltage = ball_detector_->GetVoltage();
297 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
298 ball_detector_message.Send();
299 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700300
301 {
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000302 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Brian Silverman68cb5c22016-03-20 18:11:14 -0700303 auto_mode_message->mode = 0;
304 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
305 if (autonomous_modes_[i]->Get()) {
306 auto_mode_message->mode |= 1 << i;
307 }
308 }
309 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
310 auto_mode_message.Send();
311 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000312 }
313
Comran Morshed9a9948c2016-01-16 15:58:04 +0000314 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000315 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
316
317 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000318 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
319 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000320 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000321
Brian Silverman68cb5c22016-03-20 18:11:14 -0700322 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
323
Austin Schuh54667ac2019-02-02 16:44:49 -0800324 DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000325};
326
327class SolenoidWriter {
328 public:
329 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
330 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000331 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuh843412b2016-03-20 16:48:46 -0700332 shooter_(".y2016.control_loops.shooter.shooter_queue.output"),
Sabina Davis2243cab2019-02-05 21:45:08 -0800333 superstructure_(".y2016.control_loops.superstructure_queue.output") {}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000334
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800335 void set_compressor(::std::unique_ptr<Compressor> compressor) {
336 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000337 }
338
Comran Morshed71466fe2016-04-21 20:21:14 -0700339 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000340 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700341 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000342 }
343
Comran Morshed71466fe2016-04-21 20:21:14 -0700344 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000345 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700346 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000347 }
348
Sabina Davis2243cab2019-02-05 21:45:08 -0800349 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuh843412b2016-03-20 16:48:46 -0700350 traverse_ = ::std::move(s);
351 }
352
353 void set_traverse_latch(
354 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
355 traverse_latch_ = ::std::move(s);
356 }
357
Comran Morshedb79c4242016-02-06 18:27:26 +0000358 void set_shooter_clamp(
359 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
360 shooter_clamp_ = ::std::move(s);
361 }
362
363 void set_shooter_pusher(
364 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
365 shooter_pusher_ = ::std::move(s);
366 }
367
Sabina Davis2243cab2019-02-05 21:45:08 -0800368 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhe0729a62016-03-12 21:54:17 -0800369 lights_ = ::std::move(s);
370 }
371
Austin Schuh8b89d332016-03-24 20:19:12 -0700372 void set_flashlight(::std::unique_ptr<Relay> relay) {
373 flashlight_ = ::std::move(relay);
374 }
375
Comran Morshed9a9948c2016-01-16 15:58:04 +0000376 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800377 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000378 ::aos::SetCurrentThreadName("Solenoids");
379 ::aos::SetCurrentThreadRealtimePriority(27);
380
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700381 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
382 ::std::chrono::milliseconds(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000383
384 while (run_) {
385 {
386 const int iterations = phased_loop.SleepUntilNext();
387 if (iterations != 1) {
388 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
389 }
390 }
391
392 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000393 drivetrain_.FetchLatest();
394 if (drivetrain_.get()) {
395 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Comran Morshed71466fe2016-04-21 20:21:14 -0700396 drivetrain_shifter_->Set(
397 !(drivetrain_->left_high || drivetrain_->right_high));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000398 }
399 }
400
401 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000402 shooter_.FetchLatest();
403 if (shooter_.get()) {
404 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
405 shooter_clamp_->Set(shooter_->clamp_open);
406 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800407 lights_->Set(shooter_->lights_on);
Austin Schuhb2c33382016-04-03 16:09:17 -0700408 if (shooter_->forwards_flashlight) {
409 if (shooter_->backwards_flashlight) {
410 flashlight_->Set(Relay::kOn);
411 } else {
412 flashlight_->Set(Relay::kReverse);
413 }
414 } else {
415 if (shooter_->backwards_flashlight) {
416 flashlight_->Set(Relay::kForward);
417 } else {
418 flashlight_->Set(Relay::kOff);
419 }
420 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000421 }
422 }
423
424 {
Austin Schuh843412b2016-03-20 16:48:46 -0700425 superstructure_.FetchLatest();
426 if (superstructure_.get()) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700427 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
428
429 climber_trigger_->Set(superstructure_->unfold_climber);
430
Austin Schuh843412b2016-03-20 16:48:46 -0700431 traverse_->Set(superstructure_->traverse_down);
432 traverse_latch_->Set(superstructure_->traverse_unlatched);
433 }
434 }
435
436 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000437 ::frc971::wpilib::PneumaticsToLog to_log;
Sabina Davis2243cab2019-02-05 21:45:08 -0800438 { to_log.compressor_on = compressor_->Enabled(); }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000439
440 pcm_->Flush();
441 to_log.read_solenoids = pcm_->GetAll();
442 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
443 }
444 }
445 }
446
447 void Quit() { run_ = false; }
448
449 private:
450 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
451
Comran Morshed71466fe2016-04-21 20:21:14 -0700452 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
453 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
454 climber_trigger_;
Austin Schuh8b89d332016-03-24 20:19:12 -0700455 ::std::unique_ptr<Relay> flashlight_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800456 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000457
Comran Morshed9a9948c2016-01-16 15:58:04 +0000458 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000459 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Sabina Davis2243cab2019-02-05 21:45:08 -0800460 ::aos::Queue<::y2016::control_loops::SuperstructureQueue::Output>
Austin Schuh843412b2016-03-20 16:48:46 -0700461 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000462
463 ::std::atomic<bool> run_{true};
464};
465
Comran Morshed225f0b92016-02-10 20:34:27 +0000466class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
467 public:
468 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
469 shooter_left_talon_ = ::std::move(t);
470 }
471
472 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
473 shooter_right_talon_ = ::std::move(t);
474 }
475
476 private:
477 virtual void Read() override {
478 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
479 }
480
481 virtual void Write() override {
482 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
483 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800484
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800485 shooter_left_talon_->SetSpeed(queue->voltage_left / 12.0);
486 shooter_right_talon_->SetSpeed(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000487 }
488
489 virtual void Stop() override {
490 LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800491 shooter_left_talon_->SetDisabled();
492 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000493 }
494
495 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
496};
497
498class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
499 public:
500 void set_intake_talon(::std::unique_ptr<Talon> t) {
501 intake_talon_ = ::std::move(t);
502 }
503
504 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
505 shoulder_talon_ = ::std::move(t);
506 }
507
508 void set_wrist_talon(::std::unique_ptr<Talon> t) {
509 wrist_talon_ = ::std::move(t);
510 }
511
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800512 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
513 top_rollers_talon_ = ::std::move(t);
514 }
515
516 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
517 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000518 }
519
Comran Morshed71466fe2016-04-21 20:21:14 -0700520 void set_climber_talon(::std::unique_ptr<Talon> t) {
521 climber_talon_ = ::std::move(t);
522 }
523
Comran Morshed225f0b92016-02-10 20:34:27 +0000524 private:
525 virtual void Read() override {
526 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
527 }
528
529 virtual void Write() override {
530 auto &queue = ::y2016::control_loops::superstructure_queue.output;
531 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800532 intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake,
533 -kMaxBringupPower, kMaxBringupPower) /
534 12.0);
535 shoulder_talon_->SetSpeed(::aos::Clip(-queue->voltage_shoulder,
536 -kMaxBringupPower, kMaxBringupPower) /
537 12.0);
538 wrist_talon_->SetSpeed(
Austin Schuha9992ff2016-02-28 21:59:23 -0800539 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
540 12.0);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800541 top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0);
542 bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0);
543 climber_talon_->SetSpeed(-queue->voltage_climber / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000544 }
545
546 virtual void Stop() override {
547 LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800548 intake_talon_->SetDisabled();
549 shoulder_talon_->SetDisabled();
550 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000551 }
552
Comran Morshedf4cd7642016-02-15 20:40:49 +0000553 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700554 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000555};
556
Comran Morshed9a9948c2016-01-16 15:58:04 +0000557class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
558 public:
559 ::std::unique_ptr<Encoder> make_encoder(int index) {
560 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
561 Encoder::k4X);
562 }
563
564 void Run() override {
565 ::aos::InitNRT();
566 ::aos::SetCurrentThreadName("StartCompetition");
567
568 ::frc971::wpilib::JoystickSender joystick_sender;
569 ::std::thread joystick_thread(::std::ref(joystick_sender));
570
571 ::frc971::wpilib::PDPFetcher pdp_fetcher;
572 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
573 SensorReader reader;
574
Austin Schuh9f77fd22016-02-21 02:53:58 -0800575 reader.set_drivetrain_left_encoder(make_encoder(5));
576 reader.set_drivetrain_right_encoder(make_encoder(6));
577 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
578 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000579
Austin Schuhf0c05762016-04-03 16:06:49 -0700580 reader.set_shooter_left_encoder(make_encoder(7));
581 reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000582
583 reader.set_intake_encoder(make_encoder(0));
584 reader.set_intake_index(make_unique<DigitalInput>(0));
585 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
586
Austin Schuhf0c05762016-04-03 16:06:49 -0700587 reader.set_shoulder_encoder(make_encoder(4));
Austin Schuh9f77fd22016-02-21 02:53:58 -0800588 reader.set_shoulder_index(make_unique<DigitalInput>(2));
589 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000590
Austin Schuh9f77fd22016-02-21 02:53:58 -0800591 reader.set_wrist_encoder(make_encoder(1));
592 reader.set_wrist_index(make_unique<DigitalInput>(1));
593 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000594
Comran Morshedaa0573c2016-03-05 19:05:54 +0000595 reader.set_ball_detector(make_unique<AnalogInput>(7));
596
Brian Silverman68cb5c22016-03-20 18:11:14 -0700597 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
598 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
599 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
600 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
601
Comran Morshed9a9948c2016-01-16 15:58:04 +0000602 ::std::thread reader_thread(::std::ref(reader));
603
Brian Silverman003a4732018-03-11 14:02:15 -0700604 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000605 ::frc971::wpilib::GyroSender gyro_sender;
606 ::std::thread gyro_thread(::std::ref(gyro_sender));
607
Austin Schuhf0c05762016-04-03 16:06:49 -0700608 auto imu_trigger = make_unique<DigitalInput>(3);
609 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get());
Brian Silverman5f17a972016-02-28 01:49:32 -0500610 ::std::thread imu_thread(::std::ref(imu));
Brian Silverman5f17a972016-02-28 01:49:32 -0500611
Sabina Davisb71bc282019-02-03 01:17:23 -0800612 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
613 drivetrain_writer.set_left_controller0(
614 ::std::unique_ptr<Talon>(new Talon(5)), false);
615 drivetrain_writer.set_right_controller0(
616 ::std::unique_ptr<Talon>(new Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000617 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
618
Comran Morshed225f0b92016-02-10 20:34:27 +0000619 ShooterWriter shooter_writer;
620 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800621 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000622 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800623 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000624 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
625
626 SuperstructureWriter superstructure_writer;
627 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800628 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000629 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800630 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000631 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800632 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800633 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800634 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800635 superstructure_writer.set_bottom_rollers_talon(
636 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700637 superstructure_writer.set_climber_talon(
638 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000639 ::std::thread superstructure_writer_thread(
640 ::std::ref(superstructure_writer));
641
Comran Morshed9a9948c2016-01-16 15:58:04 +0000642 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
643 new ::frc971::wpilib::BufferedPcm());
644 SolenoidWriter solenoid_writer(pcm);
Comran Morshed71466fe2016-04-21 20:21:14 -0700645 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700646 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
647 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800648 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
649 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800650 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700651 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuh8b89d332016-03-24 20:19:12 -0700652 solenoid_writer.set_flashlight(make_unique<Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000653
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800654 solenoid_writer.set_compressor(make_unique<Compressor>());
655
Comran Morshed9a9948c2016-01-16 15:58:04 +0000656 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
657
658 // Wait forever. Not much else to do...
659 while (true) {
660 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
661 if (r != 0) {
662 PLOG(WARNING, "infinite select failed");
663 } else {
664 PLOG(WARNING, "infinite select succeeded??\n");
665 }
666 }
667
668 LOG(ERROR, "Exiting WPILibRobot\n");
669
670 joystick_sender.Quit();
671 joystick_thread.join();
672 pdp_fetcher.Quit();
673 pdp_fetcher_thread.join();
674 reader.Quit();
675 reader_thread.join();
676 gyro_sender.Quit();
677 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500678 imu.Quit();
679 imu_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000680
681 drivetrain_writer.Quit();
682 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000683 shooter_writer.Quit();
684 shooter_writer_thread.join();
685 superstructure_writer.Quit();
686 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000687 solenoid_writer.Quit();
688 solenoid_thread.join();
689
690 ::aos::Cleanup();
691 }
692};
693
694} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000695} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000696
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000697AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);