blob: b3ed0a810f9a31b1842b1e0fb46fb0abb416e17b [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
6#include <thread>
7#include <mutex>
8#include <functional>
Brian Silverman68cb5c22016-03-20 18:11:14 -07009#include <array>
Comran Morshed9a9948c2016-01-16 15:58:04 +000010
11#include "Encoder.h"
12#include "Talon.h"
Austin Schuh8b89d332016-03-24 20:19:12 -070013#include "Relay.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000014#include "DriverStation.h"
15#include "AnalogInput.h"
16#include "Compressor.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000017#include "frc971/wpilib/wpilib_robot_base.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#undef ERROR
22
23#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
25#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
29#include "aos/common/stl_mutex.h"
30#include "aos/linux_code/init.h"
31#include "aos/common/messages/robot_state.q.h"
Austin Schuhcaa1ee92016-02-27 14:45:37 -080032#include "aos/common/commonmath.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000033
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"
Comran Morshedb79c4242016-02-06 18:27:26 +000036#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000037#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000038#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000039#include "y2016/control_loops/shooter/shooter.q.h"
40#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000041#include "y2016/queues/ball_detector.q.h"
Brian Silverman68cb5c22016-03-20 18:11:14 -070042#include "y2016/actors/autonomous_action.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000043
44#include "frc971/wpilib/joystick_sender.h"
45#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/buffered_solenoid.h"
47#include "frc971/wpilib/buffered_pcm.h"
48#include "frc971/wpilib/gyro_sender.h"
49#include "frc971/wpilib/dma_edge_counting.h"
50#include "frc971/wpilib/interrupt_edge_counting.h"
51#include "frc971/wpilib/encoder_and_potentiometer.h"
52#include "frc971/wpilib/logging.q.h"
53#include "frc971/wpilib/wpilib_interface.h"
54#include "frc971/wpilib/pdp_fetcher.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050055#include "frc971/wpilib/ADIS16448.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050056#include "frc971/wpilib/dma.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000057
58#ifndef M_PI
59#define M_PI 3.14159265358979323846
60#endif
61
62using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000063using ::y2016::control_loops::shooter::shooter_queue;
64using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000065
Comran Morshed6c6a0a92016-01-17 12:45:16 +000066namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000067namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080068namespace {
69constexpr double kMaxBringupPower = 12.0;
70} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000071
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
76// TODO(brian): Replace this with ::std::make_unique once all our toolchains
77// have support.
78template <class T, class... U>
79std::unique_ptr<T> make_unique(U &&... u) {
80 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
81}
82
Comran Morshed225f0b92016-02-10 20:34:27 +000083// Translates for the sensor values to convert raw index pulses into something
84// with proper units.
85
86// TODO(comran): Template these methods since there is a lot of repetition here.
87double hall_translate(double in) {
88 // Turn voltage from our 3-state halls into a ratio that the loop can use.
89 return in / 5.0;
90}
91
Comran Morshed9a9948c2016-01-16 15:58:04 +000092double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000093 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
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
98double drivetrain_velocity_translate(double in) {
99 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +0000100 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -0800101 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000102}
103
Comran Morshed225f0b92016-02-10 20:34:27 +0000104double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +0000105 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000106 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
107}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000108
Comran Morshed225f0b92016-02-10 20:34:27 +0000109double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800110 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000111 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
112}
113
114double shoulder_translate(int32_t in) {
115 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
116 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
117}
118
119double wrist_translate(int32_t in) {
120 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
121 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
122}
123
124double intake_pot_translate(double voltage) {
125 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000126 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000127}
128
129double shoulder_pot_translate(double voltage) {
130 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000131 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000132}
133
134double wrist_pot_translate(double voltage) {
135 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000136 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000137}
138
Comran Morshed1b764322016-02-14 20:18:12 +0000139constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
140 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
141 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
142 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
143
144constexpr double kMaxShooterEncoderPulsesPerSecond =
145 18700.0 /* 775pro free speed RPM */ * 12.0 /
146 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
147 128.0 /* CPR */ * 4 /* edges per cycle */;
148
149double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
150 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
151
152constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
153 18700.0 /* 775pro free speed RPM */ * 12.0 /
154 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
155 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000156
Comran Morshed225f0b92016-02-10 20:34:27 +0000157// Class to send position messages with sensor readings to our loops.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000158class SensorReader {
159 public:
160 SensorReader() {
161 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
162 // we should ever see.
Comran Morshed1b764322016-02-14 20:18:12 +0000163 drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds(
164 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
165 kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 +
166 0.5));
167 superstructure_encoder_filter_.SetPeriodNanoSeconds(
168 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
169 kMaxSuperstructureEncoderPulsesPerSecond * 1e9 +
170 0.5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000171 hall_filter_.SetPeriodNanoSeconds(100000);
172 }
173
Comran Morshed225f0b92016-02-10 20:34:27 +0000174 // Drivetrain setters.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000175 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000176 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000177 drivetrain_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000178 }
179
180 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000181 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000182 drivetrain_right_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000183 }
184
Comran Morshed225f0b92016-02-10 20:34:27 +0000185 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
186 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000187 }
188
Comran Morshed225f0b92016-02-10 20:34:27 +0000189 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
190 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000191 }
192
Comran Morshed225f0b92016-02-10 20:34:27 +0000193 // Shooter setters.
194 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000195 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000196 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000197 }
198
Comran Morshed225f0b92016-02-10 20:34:27 +0000199 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000200 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000201 shooter_right_encoder_ = ::std::move(encoder);
202 }
203
204 // Intake setters.
205 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000206 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000207 intake_encoder_.set_encoder(::std::move(encoder));
208 }
209
210 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
211 intake_encoder_.set_potentiometer(::std::move(potentiometer));
212 }
213
214 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000215 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000216 intake_encoder_.set_index(::std::move(index));
217 }
218
219 // Shoulder setters.
220 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000221 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000222 shoulder_encoder_.set_encoder(::std::move(encoder));
223 }
224
225 void set_shoulder_potentiometer(
226 ::std::unique_ptr<AnalogInput> potentiometer) {
227 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
228 }
229
230 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000231 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000232 shoulder_encoder_.set_index(::std::move(index));
233 }
234
235 // Wrist setters.
236 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000237 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000238 wrist_encoder_.set_encoder(::std::move(encoder));
239 }
240
241 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
242 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
243 }
244
245 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000246 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000247 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000248 }
249
Comran Morshedaa0573c2016-03-05 19:05:54 +0000250 // Ball detector setter.
251 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
252 ball_detector_ = ::std::move(analog);
253 }
254
Brian Silverman68cb5c22016-03-20 18:11:14 -0700255 // Autonomous mode switch setter.
256 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
257 autonomous_modes_.at(i) = ::std::move(sensor);
258 }
259
Comran Morshedaa0573c2016-03-05 19:05:54 +0000260
Comran Morshed9a9948c2016-01-16 15:58:04 +0000261 // All of the DMA-related set_* calls must be made before this, and it doesn't
262 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000263
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000264 void set_dma(::std::unique_ptr<DMA> dma) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000265 dma_synchronizer_.reset(
266 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000267 dma_synchronizer_->Add(&intake_encoder_);
268 dma_synchronizer_->Add(&shoulder_encoder_);
269 dma_synchronizer_->Add(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000270 }
271
272 void operator()() {
273 ::aos::SetCurrentThreadName("SensorReader");
274
275 my_pid_ = getpid();
276 ds_ =
277#ifdef WPILIB2015
278 DriverStation::GetInstance();
279#else
280 &DriverStation::GetInstance();
281#endif
282
Comran Morshed9a9948c2016-01-16 15:58:04 +0000283 dma_synchronizer_->Start();
284
285 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
286 ::aos::time::Time::InMS(4));
287
288 ::aos::SetCurrentThreadRealtimePriority(40);
289 while (run_) {
290 {
291 const int iterations = phased_loop.SleepUntilNext();
292 if (iterations != 1) {
293 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
294 }
295 }
296 RunIteration();
297 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000298 }
299
300 void RunIteration() {
301 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
302
303 const auto &values = constants::GetValues();
304
305 {
306 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
307 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800308 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000309 drivetrain_message->left_encoder =
310 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
311 drivetrain_message->left_speed =
312 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
313 drivetrain_message->right_speed =
314 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
315
Comran Morshed9a9948c2016-01-16 15:58:04 +0000316 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000317 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000318 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000319 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000320
321 drivetrain_message.Send();
322 }
323
Comran Morshed9a9948c2016-01-16 15:58:04 +0000324 dma_synchronizer_->RunIteration();
Comran Morshed225f0b92016-02-10 20:34:27 +0000325
326 {
327 auto shooter_message = shooter_queue.position.MakeMessage();
328 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800329 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000330 shooter_message->theta_right =
331 shooter_translate(shooter_right_encoder_->GetRaw());
332 shooter_message.Send();
333 }
334
335 {
336 auto superstructure_message = superstructure_queue.position.MakeMessage();
337 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
338 intake_translate, intake_pot_translate, false,
339 values.intake.pot_offset);
340 CopyPotAndIndexPosition(shoulder_encoder_,
341 &superstructure_message->shoulder,
342 shoulder_translate, shoulder_pot_translate, false,
343 values.shoulder.pot_offset);
344 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800345 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000346 values.wrist.pot_offset);
347
348 superstructure_message.Send();
349 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000350
351 {
352 auto ball_detector_message =
353 ::y2016::sensors::ball_detector.MakeMessage();
354 ball_detector_message->voltage = ball_detector_->GetVoltage();
355 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
356 ball_detector_message.Send();
357 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700358
359 {
360 auto auto_mode_message = ::y2016::actors::auto_mode.MakeMessage();
361 auto_mode_message->mode = 0;
362 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
363 if (autonomous_modes_[i]->Get()) {
364 auto_mode_message->mode |= 1 << i;
365 }
366 }
367 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
368 auto_mode_message.Send();
369 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000370 }
371
372 void Quit() { run_ = false; }
373
374 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000375 void CopyPotAndIndexPosition(
376 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
377 ::frc971::PotAndIndexPosition *position,
378 ::std::function<double(int32_t)> encoder_translate,
379 ::std::function<double(double)> potentiometer_translate, bool reverse,
380 double pot_offset) {
381 const double multiplier = reverse ? -1.0 : 1.0;
382 position->encoder =
383 multiplier * encoder_translate(encoder.polled_encoder_value());
384 position->pot = multiplier * potentiometer_translate(
385 encoder.polled_potentiometer_voltage()) +
386 pot_offset;
387 position->latched_encoder =
388 multiplier * encoder_translate(encoder.last_encoder_value());
389 position->latched_pot =
390 multiplier *
391 potentiometer_translate(encoder.last_potentiometer_voltage()) +
392 pot_offset;
393 position->index_pulses = encoder.index_posedge_count();
394 }
395
Comran Morshed9a9948c2016-01-16 15:58:04 +0000396 int32_t my_pid_;
397 DriverStation *ds_;
398
399 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
400
Comran Morshed225f0b92016-02-10 20:34:27 +0000401 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
402 drivetrain_right_encoder_;
403 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
404
405 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000406 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
407 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000408 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000409
Brian Silverman68cb5c22016-03-20 18:11:14 -0700410 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
411
Comran Morshed9a9948c2016-01-16 15:58:04 +0000412 ::std::atomic<bool> run_{true};
Comran Morshed1b764322016-02-14 20:18:12 +0000413 DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_,
414 superstructure_encoder_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000415};
416
417class SolenoidWriter {
418 public:
419 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
420 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000421 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuh843412b2016-03-20 16:48:46 -0700422 shooter_(".y2016.control_loops.shooter.shooter_queue.output"),
423 superstructure_(
424 ".y2016.control_loops.superstructure_queue.output") {
425 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000426
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800427 void set_compressor(::std::unique_ptr<Compressor> compressor) {
428 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000429 }
430
431 void set_drivetrain_left(
432 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
433 drivetrain_left_ = ::std::move(s);
434 }
435
436 void set_drivetrain_right(
437 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
438 drivetrain_right_ = ::std::move(s);
439 }
440
Austin Schuh843412b2016-03-20 16:48:46 -0700441 void set_traverse(
442 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
443 traverse_ = ::std::move(s);
444 }
445
446 void set_traverse_latch(
447 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
448 traverse_latch_ = ::std::move(s);
449 }
450
Comran Morshedb79c4242016-02-06 18:27:26 +0000451 void set_shooter_clamp(
452 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
453 shooter_clamp_ = ::std::move(s);
454 }
455
456 void set_shooter_pusher(
457 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
458 shooter_pusher_ = ::std::move(s);
459 }
460
Austin Schuhe0729a62016-03-12 21:54:17 -0800461 void set_lights(
462 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
463 lights_ = ::std::move(s);
464 }
465
Austin Schuh8b89d332016-03-24 20:19:12 -0700466 void set_flashlight(::std::unique_ptr<Relay> relay) {
467 flashlight_ = ::std::move(relay);
468 }
469
Comran Morshed9a9948c2016-01-16 15:58:04 +0000470 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800471 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000472 ::aos::SetCurrentThreadName("Solenoids");
473 ::aos::SetCurrentThreadRealtimePriority(27);
474
475 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
476 ::aos::time::Time::InMS(1));
477
478 while (run_) {
479 {
480 const int iterations = phased_loop.SleepUntilNext();
481 if (iterations != 1) {
482 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
483 }
484 }
485
486 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000487 drivetrain_.FetchLatest();
488 if (drivetrain_.get()) {
489 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
490 drivetrain_left_->Set(!drivetrain_->left_high);
491 drivetrain_right_->Set(!drivetrain_->right_high);
492 }
493 }
494
495 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000496 shooter_.FetchLatest();
497 if (shooter_.get()) {
498 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
499 shooter_clamp_->Set(shooter_->clamp_open);
500 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800501 lights_->Set(shooter_->lights_on);
Austin Schuhb2c33382016-04-03 16:09:17 -0700502 if (shooter_->forwards_flashlight) {
503 if (shooter_->backwards_flashlight) {
504 flashlight_->Set(Relay::kOn);
505 } else {
506 flashlight_->Set(Relay::kReverse);
507 }
508 } else {
509 if (shooter_->backwards_flashlight) {
510 flashlight_->Set(Relay::kForward);
511 } else {
512 flashlight_->Set(Relay::kOff);
513 }
514 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000515 }
516 }
517
518 {
Austin Schuh843412b2016-03-20 16:48:46 -0700519 superstructure_.FetchLatest();
520 if (superstructure_.get()) {
521 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
522 traverse_->Set(superstructure_->traverse_down);
523 traverse_latch_->Set(superstructure_->traverse_unlatched);
524 }
525 }
526
527 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000528 ::frc971::wpilib::PneumaticsToLog to_log;
529 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800530 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000531 }
532
533 pcm_->Flush();
534 to_log.read_solenoids = pcm_->GetAll();
535 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
536 }
537 }
538 }
539
540 void Quit() { run_ = false; }
541
542 private:
543 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
544
Comran Morshed225f0b92016-02-10 20:34:27 +0000545 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Austin Schuh843412b2016-03-20 16:48:46 -0700546 drivetrain_right_, shooter_clamp_, shooter_pusher_, lights_, traverse_,
547 traverse_latch_;
Austin Schuh8b89d332016-03-24 20:19:12 -0700548 ::std::unique_ptr<Relay> flashlight_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800549 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000550
Comran Morshed9a9948c2016-01-16 15:58:04 +0000551 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000552 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Austin Schuh843412b2016-03-20 16:48:46 -0700553 ::aos::Queue<
554 ::y2016::control_loops::SuperstructureQueue::Output>
555 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000556
557 ::std::atomic<bool> run_{true};
558};
559
560class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
561 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000562 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
563 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000564 }
565
Comran Morshed225f0b92016-02-10 20:34:27 +0000566 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
567 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000568 }
569
570 private:
571 virtual void Read() override {
572 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
573 }
574
575 virtual void Write() override {
576 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
577 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800578 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
579 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000580 }
581
582 virtual void Stop() override {
583 LOG(WARNING, "drivetrain output too old\n");
Comran Morshed225f0b92016-02-10 20:34:27 +0000584 drivetrain_left_talon_->Disable();
585 drivetrain_right_talon_->Disable();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000586 }
587
Comran Morshed225f0b92016-02-10 20:34:27 +0000588 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
589};
590
591class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
592 public:
593 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
594 shooter_left_talon_ = ::std::move(t);
595 }
596
597 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
598 shooter_right_talon_ = ::std::move(t);
599 }
600
601 private:
602 virtual void Read() override {
603 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
604 }
605
606 virtual void Write() override {
607 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
608 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800609
Comran Morshed225f0b92016-02-10 20:34:27 +0000610 shooter_left_talon_->Set(queue->voltage_left / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800611 shooter_right_talon_->Set(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000612 }
613
614 virtual void Stop() override {
615 LOG(WARNING, "Shooter output too old.\n");
616 shooter_left_talon_->Disable();
617 shooter_right_talon_->Disable();
618 }
619
620 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
621};
622
623class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
624 public:
625 void set_intake_talon(::std::unique_ptr<Talon> t) {
626 intake_talon_ = ::std::move(t);
627 }
628
629 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
630 shoulder_talon_ = ::std::move(t);
631 }
632
633 void set_wrist_talon(::std::unique_ptr<Talon> t) {
634 wrist_talon_ = ::std::move(t);
635 }
636
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800637 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
638 top_rollers_talon_ = ::std::move(t);
639 }
640
641 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
642 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000643 }
644
Comran Morshed225f0b92016-02-10 20:34:27 +0000645 private:
646 virtual void Read() override {
647 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
648 }
649
650 virtual void Write() override {
651 auto &queue = ::y2016::control_loops::superstructure_queue.output;
652 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha9992ff2016-02-28 21:59:23 -0800653 intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower,
654 kMaxBringupPower) /
655 12.0);
656 shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder,
657 -kMaxBringupPower, kMaxBringupPower) /
658 12.0);
659 wrist_talon_->Set(
660 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
661 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800662 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
663 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000664 }
665
666 virtual void Stop() override {
667 LOG(WARNING, "Superstructure output too old.\n");
668 intake_talon_->Disable();
669 shoulder_talon_->Disable();
670 wrist_talon_->Disable();
671 }
672
Comran Morshedf4cd7642016-02-15 20:40:49 +0000673 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800674 top_rollers_talon_, bottom_rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000675};
676
Comran Morshed9a9948c2016-01-16 15:58:04 +0000677class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
678 public:
679 ::std::unique_ptr<Encoder> make_encoder(int index) {
680 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
681 Encoder::k4X);
682 }
683
684 void Run() override {
685 ::aos::InitNRT();
686 ::aos::SetCurrentThreadName("StartCompetition");
687
688 ::frc971::wpilib::JoystickSender joystick_sender;
689 ::std::thread joystick_thread(::std::ref(joystick_sender));
690
691 ::frc971::wpilib::PDPFetcher pdp_fetcher;
692 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
693 SensorReader reader;
694
Austin Schuh9f77fd22016-02-21 02:53:58 -0800695 reader.set_drivetrain_left_encoder(make_encoder(5));
696 reader.set_drivetrain_right_encoder(make_encoder(6));
697 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
698 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000699
Austin Schuhf0c05762016-04-03 16:06:49 -0700700 reader.set_shooter_left_encoder(make_encoder(7));
701 reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000702
703 reader.set_intake_encoder(make_encoder(0));
704 reader.set_intake_index(make_unique<DigitalInput>(0));
705 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
706
Austin Schuhf0c05762016-04-03 16:06:49 -0700707 reader.set_shoulder_encoder(make_encoder(4));
Austin Schuh9f77fd22016-02-21 02:53:58 -0800708 reader.set_shoulder_index(make_unique<DigitalInput>(2));
709 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000710
Austin Schuh9f77fd22016-02-21 02:53:58 -0800711 reader.set_wrist_encoder(make_encoder(1));
712 reader.set_wrist_index(make_unique<DigitalInput>(1));
713 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000714
Comran Morshedaa0573c2016-03-05 19:05:54 +0000715 reader.set_ball_detector(make_unique<AnalogInput>(7));
716
Brian Silverman68cb5c22016-03-20 18:11:14 -0700717 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
718 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
719 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
720 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
721
Comran Morshed9a9948c2016-01-16 15:58:04 +0000722 reader.set_dma(make_unique<DMA>());
723 ::std::thread reader_thread(::std::ref(reader));
724
725 ::frc971::wpilib::GyroSender gyro_sender;
726 ::std::thread gyro_thread(::std::ref(gyro_sender));
727
Austin Schuhf0c05762016-04-03 16:06:49 -0700728 auto imu_trigger = make_unique<DigitalInput>(3);
729 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get());
Brian Silverman5f17a972016-02-28 01:49:32 -0500730 ::std::thread imu_thread(::std::ref(imu));
Brian Silverman5f17a972016-02-28 01:49:32 -0500731
Comran Morshed9a9948c2016-01-16 15:58:04 +0000732 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000733 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000734 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000735 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800736 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000737 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
738
Comran Morshed225f0b92016-02-10 20:34:27 +0000739 ShooterWriter shooter_writer;
740 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800741 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000742 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800743 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000744 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
745
746 SuperstructureWriter superstructure_writer;
747 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800748 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000749 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800750 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000751 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800752 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800753 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800754 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800755 superstructure_writer.set_bottom_rollers_talon(
756 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000757 ::std::thread superstructure_writer_thread(
758 ::std::ref(superstructure_writer));
759
Comran Morshed9a9948c2016-01-16 15:58:04 +0000760 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
761 new ::frc971::wpilib::BufferedPcm());
762 SolenoidWriter solenoid_writer(pcm);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800763 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1));
764 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700765 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
766 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800767 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
768 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800769 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Austin Schuh8b89d332016-03-24 20:19:12 -0700770 solenoid_writer.set_flashlight(make_unique<Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000771
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800772 solenoid_writer.set_compressor(make_unique<Compressor>());
773
Comran Morshed9a9948c2016-01-16 15:58:04 +0000774 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
775
776 // Wait forever. Not much else to do...
777 while (true) {
778 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
779 if (r != 0) {
780 PLOG(WARNING, "infinite select failed");
781 } else {
782 PLOG(WARNING, "infinite select succeeded??\n");
783 }
784 }
785
786 LOG(ERROR, "Exiting WPILibRobot\n");
787
788 joystick_sender.Quit();
789 joystick_thread.join();
790 pdp_fetcher.Quit();
791 pdp_fetcher_thread.join();
792 reader.Quit();
793 reader_thread.join();
794 gyro_sender.Quit();
795 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500796 imu.Quit();
797 imu_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000798
799 drivetrain_writer.Quit();
800 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000801 shooter_writer.Quit();
802 shooter_writer_thread.join();
803 superstructure_writer.Quit();
804 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000805 solenoid_writer.Quit();
806 solenoid_thread.join();
807
808 ::aos::Cleanup();
809 }
810};
811
812} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000813} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000814
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000815AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);