blob: 7c58f15959b6c002134142cf90fbb033a46b025b [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>
9
10#include "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000015#include "frc971/wpilib/wpilib_robot_base.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000016#ifndef WPILIB2015
17#include "DigitalGlitchFilter.h"
18#endif
19#undef ERROR
20
21#include "aos/common/logging/logging.h"
22#include "aos/common/logging/queue_logging.h"
23#include "aos/common/time.h"
24#include "aos/common/util/log_interval.h"
25#include "aos/common/util/phased_loop.h"
26#include "aos/common/util/wrapping_counter.h"
27#include "aos/common/stl_mutex.h"
28#include "aos/linux_code/init.h"
29#include "aos/common/messages/robot_state.q.h"
Austin Schuhcaa1ee92016-02-27 14:45:37 -080030#include "aos/common/commonmath.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000031
Comran Morshed225f0b92016-02-10 20:34:27 +000032#include "frc971/control_loops/control_loops.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000033#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshedb79c4242016-02-06 18:27:26 +000034#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000035#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000036#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000037#include "y2016/control_loops/shooter/shooter.q.h"
38#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000039#include "y2016/queues/ball_detector.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000040
41#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
45#include "frc971/wpilib/gyro_sender.h"
46#include "frc971/wpilib/dma_edge_counting.h"
47#include "frc971/wpilib/interrupt_edge_counting.h"
48#include "frc971/wpilib/encoder_and_potentiometer.h"
49#include "frc971/wpilib/logging.q.h"
50#include "frc971/wpilib/wpilib_interface.h"
51#include "frc971/wpilib/pdp_fetcher.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050052#include "frc971/wpilib/ADIS16448.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050053#include "frc971/wpilib/dma.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000054
55#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
59using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000060using ::y2016::control_loops::shooter::shooter_queue;
61using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000062
Comran Morshed6c6a0a92016-01-17 12:45:16 +000063namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000064namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080065namespace {
66constexpr double kMaxBringupPower = 12.0;
67} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000068
69// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
70// DMA stuff and then removing the * 2.0 in *_translate.
71// The low bit is direction.
72
73// TODO(brian): Replace this with ::std::make_unique once all our toolchains
74// have support.
75template <class T, class... U>
76std::unique_ptr<T> make_unique(U &&... u) {
77 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
78}
79
Comran Morshed225f0b92016-02-10 20:34:27 +000080// Translates for the sensor values to convert raw index pulses into something
81// with proper units.
82
83// TODO(comran): Template these methods since there is a lot of repetition here.
84double hall_translate(double in) {
85 // Turn voltage from our 3-state halls into a ratio that the loop can use.
86 return in / 5.0;
87}
88
Comran Morshed9a9948c2016-01-16 15:58:04 +000089double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000090 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000091 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080092 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000093}
94
95double drivetrain_velocity_translate(double in) {
96 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000097 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080098 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000099}
100
Comran Morshed225f0b92016-02-10 20:34:27 +0000101double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +0000102 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000103 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
104}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000105
Comran Morshed225f0b92016-02-10 20:34:27 +0000106double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800107 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000108 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
109}
110
111double shoulder_translate(int32_t in) {
112 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
113 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
114}
115
116double wrist_translate(int32_t in) {
117 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
118 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
119}
120
121double intake_pot_translate(double voltage) {
122 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000123 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000124}
125
126double shoulder_pot_translate(double voltage) {
127 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000128 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000129}
130
131double wrist_pot_translate(double voltage) {
132 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000133 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000134}
135
Comran Morshed1b764322016-02-14 20:18:12 +0000136constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
137 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
138 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
139 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
140
141constexpr double kMaxShooterEncoderPulsesPerSecond =
142 18700.0 /* 775pro free speed RPM */ * 12.0 /
143 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
144 128.0 /* CPR */ * 4 /* edges per cycle */;
145
146double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
147 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
148
149constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
150 18700.0 /* 775pro free speed RPM */ * 12.0 /
151 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
152 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000153
Comran Morshed225f0b92016-02-10 20:34:27 +0000154// Class to send position messages with sensor readings to our loops.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000155class SensorReader {
156 public:
157 SensorReader() {
158 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
159 // we should ever see.
Comran Morshed1b764322016-02-14 20:18:12 +0000160 drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds(
161 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
162 kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 +
163 0.5));
164 superstructure_encoder_filter_.SetPeriodNanoSeconds(
165 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
166 kMaxSuperstructureEncoderPulsesPerSecond * 1e9 +
167 0.5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000168 hall_filter_.SetPeriodNanoSeconds(100000);
169 }
170
Comran Morshed225f0b92016-02-10 20:34:27 +0000171 // Drivetrain setters.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000172 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000173 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174 drivetrain_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000175 }
176
177 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000178 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000179 drivetrain_right_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000180 }
181
Comran Morshed225f0b92016-02-10 20:34:27 +0000182 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
183 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000184 }
185
Comran Morshed225f0b92016-02-10 20:34:27 +0000186 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
187 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000188 }
189
Comran Morshed225f0b92016-02-10 20:34:27 +0000190 // Shooter setters.
191 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000192 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000193 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000194 }
195
Comran Morshed225f0b92016-02-10 20:34:27 +0000196 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000197 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000198 shooter_right_encoder_ = ::std::move(encoder);
199 }
200
201 // Intake setters.
202 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000203 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000204 intake_encoder_.set_encoder(::std::move(encoder));
205 }
206
207 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
208 intake_encoder_.set_potentiometer(::std::move(potentiometer));
209 }
210
211 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000212 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000213 intake_encoder_.set_index(::std::move(index));
214 }
215
216 // Shoulder setters.
217 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000218 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000219 shoulder_encoder_.set_encoder(::std::move(encoder));
220 }
221
222 void set_shoulder_potentiometer(
223 ::std::unique_ptr<AnalogInput> potentiometer) {
224 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
225 }
226
227 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000228 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000229 shoulder_encoder_.set_index(::std::move(index));
230 }
231
232 // Wrist setters.
233 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000234 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000235 wrist_encoder_.set_encoder(::std::move(encoder));
236 }
237
238 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
239 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
240 }
241
242 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000243 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000244 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000245 }
246
Comran Morshedaa0573c2016-03-05 19:05:54 +0000247 // Ball detector setter.
248 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
249 ball_detector_ = ::std::move(analog);
250 }
251
252
Comran Morshed9a9948c2016-01-16 15:58:04 +0000253 // All of the DMA-related set_* calls must be made before this, and it doesn't
254 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000255
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000256 // TODO(comran): Add 2016 things down below for dma synchronization.
257 void set_dma(::std::unique_ptr<DMA> dma) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000258 dma_synchronizer_.reset(
259 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000260 dma_synchronizer_->Add(&intake_encoder_);
261 dma_synchronizer_->Add(&shoulder_encoder_);
262 dma_synchronizer_->Add(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000263 }
264
265 void operator()() {
266 ::aos::SetCurrentThreadName("SensorReader");
267
268 my_pid_ = getpid();
269 ds_ =
270#ifdef WPILIB2015
271 DriverStation::GetInstance();
272#else
273 &DriverStation::GetInstance();
274#endif
275
Comran Morshed9a9948c2016-01-16 15:58:04 +0000276 dma_synchronizer_->Start();
277
278 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
279 ::aos::time::Time::InMS(4));
280
281 ::aos::SetCurrentThreadRealtimePriority(40);
282 while (run_) {
283 {
284 const int iterations = phased_loop.SleepUntilNext();
285 if (iterations != 1) {
286 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
287 }
288 }
289 RunIteration();
290 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000291 }
292
293 void RunIteration() {
294 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
295
296 const auto &values = constants::GetValues();
297
298 {
299 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
300 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800301 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000302 drivetrain_message->left_encoder =
303 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
304 drivetrain_message->left_speed =
305 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
306 drivetrain_message->right_speed =
307 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
308
Comran Morshed9a9948c2016-01-16 15:58:04 +0000309 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000310 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000311 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000312 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000313
314 drivetrain_message.Send();
315 }
316
Comran Morshed9a9948c2016-01-16 15:58:04 +0000317 dma_synchronizer_->RunIteration();
Comran Morshed225f0b92016-02-10 20:34:27 +0000318
319 {
320 auto shooter_message = shooter_queue.position.MakeMessage();
321 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800322 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000323 shooter_message->theta_right =
324 shooter_translate(shooter_right_encoder_->GetRaw());
325 shooter_message.Send();
326 }
327
328 {
329 auto superstructure_message = superstructure_queue.position.MakeMessage();
330 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
331 intake_translate, intake_pot_translate, false,
332 values.intake.pot_offset);
333 CopyPotAndIndexPosition(shoulder_encoder_,
334 &superstructure_message->shoulder,
335 shoulder_translate, shoulder_pot_translate, false,
336 values.shoulder.pot_offset);
337 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800338 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000339 values.wrist.pot_offset);
340
341 superstructure_message.Send();
342 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000343
344 {
345 auto ball_detector_message =
346 ::y2016::sensors::ball_detector.MakeMessage();
347 ball_detector_message->voltage = ball_detector_->GetVoltage();
348 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
349 ball_detector_message.Send();
350 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000351 }
352
353 void Quit() { run_ = false; }
354
355 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000356 void CopyPotAndIndexPosition(
357 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
358 ::frc971::PotAndIndexPosition *position,
359 ::std::function<double(int32_t)> encoder_translate,
360 ::std::function<double(double)> potentiometer_translate, bool reverse,
361 double pot_offset) {
362 const double multiplier = reverse ? -1.0 : 1.0;
363 position->encoder =
364 multiplier * encoder_translate(encoder.polled_encoder_value());
365 position->pot = multiplier * potentiometer_translate(
366 encoder.polled_potentiometer_voltage()) +
367 pot_offset;
368 position->latched_encoder =
369 multiplier * encoder_translate(encoder.last_encoder_value());
370 position->latched_pot =
371 multiplier *
372 potentiometer_translate(encoder.last_potentiometer_voltage()) +
373 pot_offset;
374 position->index_pulses = encoder.index_posedge_count();
375 }
376
Comran Morshed9a9948c2016-01-16 15:58:04 +0000377 int32_t my_pid_;
378 DriverStation *ds_;
379
380 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
381
Comran Morshed225f0b92016-02-10 20:34:27 +0000382 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
383 drivetrain_right_encoder_;
384 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
385
386 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000387 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
388 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000389 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000390
Comran Morshed9a9948c2016-01-16 15:58:04 +0000391 ::std::atomic<bool> run_{true};
Comran Morshed1b764322016-02-14 20:18:12 +0000392 DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_,
393 superstructure_encoder_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000394};
395
396class SolenoidWriter {
397 public:
398 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
399 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000400 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800401 shooter_(".y2016.control_loops.shooter.shooter_queue.output") {}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000402
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800403 void set_compressor(::std::unique_ptr<Compressor> compressor) {
404 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000405 }
406
407 void set_drivetrain_left(
408 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
409 drivetrain_left_ = ::std::move(s);
410 }
411
412 void set_drivetrain_right(
413 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
414 drivetrain_right_ = ::std::move(s);
415 }
416
Comran Morshedb79c4242016-02-06 18:27:26 +0000417 void set_shooter_clamp(
418 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
419 shooter_clamp_ = ::std::move(s);
420 }
421
422 void set_shooter_pusher(
423 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
424 shooter_pusher_ = ::std::move(s);
425 }
426
Austin Schuhe0729a62016-03-12 21:54:17 -0800427 void set_lights(
428 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
429 lights_ = ::std::move(s);
430 }
431
Comran Morshed9a9948c2016-01-16 15:58:04 +0000432 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800433 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000434 ::aos::SetCurrentThreadName("Solenoids");
435 ::aos::SetCurrentThreadRealtimePriority(27);
436
437 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
438 ::aos::time::Time::InMS(1));
439
440 while (run_) {
441 {
442 const int iterations = phased_loop.SleepUntilNext();
443 if (iterations != 1) {
444 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
445 }
446 }
447
448 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000449 drivetrain_.FetchLatest();
450 if (drivetrain_.get()) {
451 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
452 drivetrain_left_->Set(!drivetrain_->left_high);
453 drivetrain_right_->Set(!drivetrain_->right_high);
454 }
455 }
456
457 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000458 shooter_.FetchLatest();
459 if (shooter_.get()) {
460 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
461 shooter_clamp_->Set(shooter_->clamp_open);
462 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800463 lights_->Set(shooter_->lights_on);
Comran Morshedb79c4242016-02-06 18:27:26 +0000464 }
465 }
466
467 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000468 ::frc971::wpilib::PneumaticsToLog to_log;
469 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800470 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000471 }
472
473 pcm_->Flush();
474 to_log.read_solenoids = pcm_->GetAll();
475 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
476 }
477 }
478 }
479
480 void Quit() { run_ = false; }
481
482 private:
483 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
484
Comran Morshed225f0b92016-02-10 20:34:27 +0000485 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Austin Schuhe0729a62016-03-12 21:54:17 -0800486 drivetrain_right_, shooter_clamp_, shooter_pusher_, lights_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800487 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000488
Comran Morshed9a9948c2016-01-16 15:58:04 +0000489 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000490 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000491
492 ::std::atomic<bool> run_{true};
493};
494
495class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
496 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000497 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
498 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000499 }
500
Comran Morshed225f0b92016-02-10 20:34:27 +0000501 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
502 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000503 }
504
505 private:
506 virtual void Read() override {
507 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
508 }
509
510 virtual void Write() override {
511 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
512 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800513 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
514 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000515 }
516
517 virtual void Stop() override {
518 LOG(WARNING, "drivetrain output too old\n");
Comran Morshed225f0b92016-02-10 20:34:27 +0000519 drivetrain_left_talon_->Disable();
520 drivetrain_right_talon_->Disable();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000521 }
522
Comran Morshed225f0b92016-02-10 20:34:27 +0000523 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
524};
525
526class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
527 public:
528 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
529 shooter_left_talon_ = ::std::move(t);
530 }
531
532 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
533 shooter_right_talon_ = ::std::move(t);
534 }
535
536 private:
537 virtual void Read() override {
538 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
539 }
540
541 virtual void Write() override {
542 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
543 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800544
Comran Morshed225f0b92016-02-10 20:34:27 +0000545 shooter_left_talon_->Set(queue->voltage_left / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800546 shooter_right_talon_->Set(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000547 }
548
549 virtual void Stop() override {
550 LOG(WARNING, "Shooter output too old.\n");
551 shooter_left_talon_->Disable();
552 shooter_right_talon_->Disable();
553 }
554
555 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
556};
557
558class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
559 public:
560 void set_intake_talon(::std::unique_ptr<Talon> t) {
561 intake_talon_ = ::std::move(t);
562 }
563
564 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
565 shoulder_talon_ = ::std::move(t);
566 }
567
568 void set_wrist_talon(::std::unique_ptr<Talon> t) {
569 wrist_talon_ = ::std::move(t);
570 }
571
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800572 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
573 top_rollers_talon_ = ::std::move(t);
574 }
575
576 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
577 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000578 }
579
Comran Morshed225f0b92016-02-10 20:34:27 +0000580 private:
581 virtual void Read() override {
582 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
583 }
584
585 virtual void Write() override {
586 auto &queue = ::y2016::control_loops::superstructure_queue.output;
587 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha9992ff2016-02-28 21:59:23 -0800588 intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower,
589 kMaxBringupPower) /
590 12.0);
591 shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder,
592 -kMaxBringupPower, kMaxBringupPower) /
593 12.0);
594 wrist_talon_->Set(
595 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
596 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800597 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
598 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000599 }
600
601 virtual void Stop() override {
602 LOG(WARNING, "Superstructure output too old.\n");
603 intake_talon_->Disable();
604 shoulder_talon_->Disable();
605 wrist_talon_->Disable();
606 }
607
Comran Morshedf4cd7642016-02-15 20:40:49 +0000608 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800609 top_rollers_talon_, bottom_rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000610};
611
Comran Morshed9a9948c2016-01-16 15:58:04 +0000612class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
613 public:
614 ::std::unique_ptr<Encoder> make_encoder(int index) {
615 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
616 Encoder::k4X);
617 }
618
619 void Run() override {
620 ::aos::InitNRT();
621 ::aos::SetCurrentThreadName("StartCompetition");
622
623 ::frc971::wpilib::JoystickSender joystick_sender;
624 ::std::thread joystick_thread(::std::ref(joystick_sender));
625
626 ::frc971::wpilib::PDPFetcher pdp_fetcher;
627 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
628 SensorReader reader;
629
Austin Schuh9f77fd22016-02-21 02:53:58 -0800630 reader.set_drivetrain_left_encoder(make_encoder(5));
631 reader.set_drivetrain_right_encoder(make_encoder(6));
632 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
633 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000634
Austin Schuh9f77fd22016-02-21 02:53:58 -0800635 reader.set_shooter_left_encoder(make_encoder(3));
636 reader.set_shooter_right_encoder(make_encoder(4));
Comran Morshed225f0b92016-02-10 20:34:27 +0000637
638 reader.set_intake_encoder(make_encoder(0));
639 reader.set_intake_index(make_unique<DigitalInput>(0));
640 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
641
Austin Schuh9f77fd22016-02-21 02:53:58 -0800642 reader.set_shoulder_encoder(make_encoder(2));
643 reader.set_shoulder_index(make_unique<DigitalInput>(2));
644 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000645
Austin Schuh9f77fd22016-02-21 02:53:58 -0800646 reader.set_wrist_encoder(make_encoder(1));
647 reader.set_wrist_index(make_unique<DigitalInput>(1));
648 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000649
Comran Morshedaa0573c2016-03-05 19:05:54 +0000650 reader.set_ball_detector(make_unique<AnalogInput>(7));
651
Comran Morshed9a9948c2016-01-16 15:58:04 +0000652 reader.set_dma(make_unique<DMA>());
653 ::std::thread reader_thread(::std::ref(reader));
654
655 ::frc971::wpilib::GyroSender gyro_sender;
656 ::std::thread gyro_thread(::std::ref(gyro_sender));
657
Brian Silverman5f17a972016-02-28 01:49:32 -0500658#if 0
659 auto imu_trigger = make_unique<DigitalInput>(100);
660 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
661 ::std::thread imu_thread(::std::ref(imu));
662#endif
663
Comran Morshed9a9948c2016-01-16 15:58:04 +0000664 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000665 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000666 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000667 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800668 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000669 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
670
Comran Morshed225f0b92016-02-10 20:34:27 +0000671 ShooterWriter shooter_writer;
672 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800673 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000674 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800675 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000676 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
677
678 SuperstructureWriter superstructure_writer;
679 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800680 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000681 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800682 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000683 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800684 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800685 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800686 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800687 superstructure_writer.set_bottom_rollers_talon(
688 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000689 ::std::thread superstructure_writer_thread(
690 ::std::ref(superstructure_writer));
691
Comran Morshed9a9948c2016-01-16 15:58:04 +0000692 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
693 new ::frc971::wpilib::BufferedPcm());
694 SolenoidWriter solenoid_writer(pcm);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800695 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1));
696 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0));
697 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
698 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800699 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000700
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800701 solenoid_writer.set_compressor(make_unique<Compressor>());
702
Comran Morshed9a9948c2016-01-16 15:58:04 +0000703 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
704
705 // Wait forever. Not much else to do...
706 while (true) {
707 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
708 if (r != 0) {
709 PLOG(WARNING, "infinite select failed");
710 } else {
711 PLOG(WARNING, "infinite select succeeded??\n");
712 }
713 }
714
715 LOG(ERROR, "Exiting WPILibRobot\n");
716
717 joystick_sender.Quit();
718 joystick_thread.join();
719 pdp_fetcher.Quit();
720 pdp_fetcher_thread.join();
721 reader.Quit();
722 reader_thread.join();
723 gyro_sender.Quit();
724 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500725#if 0
726 imu.Quit();
727 imu_thread.join();
728#endif
Comran Morshed9a9948c2016-01-16 15:58:04 +0000729
730 drivetrain_writer.Quit();
731 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000732 shooter_writer.Quit();
733 shooter_writer_thread.join();
734 superstructure_writer.Quit();
735 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000736 solenoid_writer.Quit();
737 solenoid_thread.join();
738
739 ::aos::Cleanup();
740 }
741};
742
743} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000744} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000745
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000746AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);