blob: 5567a7567a09244848dbe5b667417d0e6849102f [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"
13#include "DriverStation.h"
14#include "AnalogInput.h"
15#include "Compressor.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000016#include "frc971/wpilib/wpilib_robot_base.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000017#ifndef WPILIB2015
18#include "DigitalGlitchFilter.h"
19#endif
20#undef ERROR
21
22#include "aos/common/logging/logging.h"
23#include "aos/common/logging/queue_logging.h"
24#include "aos/common/time.h"
25#include "aos/common/util/log_interval.h"
26#include "aos/common/util/phased_loop.h"
27#include "aos/common/util/wrapping_counter.h"
28#include "aos/common/stl_mutex.h"
29#include "aos/linux_code/init.h"
30#include "aos/common/messages/robot_state.q.h"
Austin Schuhcaa1ee92016-02-27 14:45:37 -080031#include "aos/common/commonmath.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000032
Comran Morshed225f0b92016-02-10 20:34:27 +000033#include "frc971/control_loops/control_loops.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000034#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshedb79c4242016-02-06 18:27:26 +000035#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000036#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000037#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000038#include "y2016/control_loops/shooter/shooter.q.h"
39#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000040#include "y2016/queues/ball_detector.q.h"
Brian Silverman68cb5c22016-03-20 18:11:14 -070041#include "y2016/actors/autonomous_action.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000042
43#include "frc971/wpilib/joystick_sender.h"
44#include "frc971/wpilib/loop_output_handler.h"
45#include "frc971/wpilib/buffered_solenoid.h"
46#include "frc971/wpilib/buffered_pcm.h"
47#include "frc971/wpilib/gyro_sender.h"
48#include "frc971/wpilib/dma_edge_counting.h"
49#include "frc971/wpilib/interrupt_edge_counting.h"
50#include "frc971/wpilib/encoder_and_potentiometer.h"
51#include "frc971/wpilib/logging.q.h"
52#include "frc971/wpilib/wpilib_interface.h"
53#include "frc971/wpilib/pdp_fetcher.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050054#include "frc971/wpilib/ADIS16448.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050055#include "frc971/wpilib/dma.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000056
57#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;
Comran Morshed9a9948c2016-01-16 15:58:04 +000064
Comran Morshed6c6a0a92016-01-17 12:45:16 +000065namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000066namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080067namespace {
68constexpr double kMaxBringupPower = 12.0;
69} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000070
71// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
72// DMA stuff and then removing the * 2.0 in *_translate.
73// The low bit is direction.
74
75// TODO(brian): Replace this with ::std::make_unique once all our toolchains
76// have support.
77template <class T, class... U>
78std::unique_ptr<T> make_unique(U &&... u) {
79 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
80}
81
Comran Morshed225f0b92016-02-10 20:34:27 +000082// Translates for the sensor values to convert raw index pulses into something
83// with proper units.
84
85// TODO(comran): Template these methods since there is a lot of repetition here.
86double hall_translate(double in) {
87 // Turn voltage from our 3-state halls into a ratio that the loop can use.
88 return in / 5.0;
89}
90
Comran Morshed9a9948c2016-01-16 15:58:04 +000091double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000092 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000093 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080094 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000095}
96
97double drivetrain_velocity_translate(double in) {
98 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000099 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -0800100 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000101}
102
Comran Morshed225f0b92016-02-10 20:34:27 +0000103double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +0000104 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000105 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
106}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000107
Comran Morshed225f0b92016-02-10 20:34:27 +0000108double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800109 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000110 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
111}
112
113double shoulder_translate(int32_t in) {
114 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
115 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
116}
117
118double wrist_translate(int32_t in) {
119 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
120 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
121}
122
123double intake_pot_translate(double voltage) {
124 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000125 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000126}
127
128double shoulder_pot_translate(double voltage) {
129 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000130 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000131}
132
133double wrist_pot_translate(double voltage) {
134 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000135 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000136}
137
Comran Morshed1b764322016-02-14 20:18:12 +0000138constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
139 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
140 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
141 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
142
143constexpr double kMaxShooterEncoderPulsesPerSecond =
144 18700.0 /* 775pro free speed RPM */ * 12.0 /
145 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
146 128.0 /* CPR */ * 4 /* edges per cycle */;
147
148double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
149 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
150
151constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
152 18700.0 /* 775pro free speed RPM */ * 12.0 /
153 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
154 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000155
Comran Morshed225f0b92016-02-10 20:34:27 +0000156// Class to send position messages with sensor readings to our loops.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000157class SensorReader {
158 public:
159 SensorReader() {
160 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
161 // we should ever see.
Comran Morshed1b764322016-02-14 20:18:12 +0000162 drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds(
163 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
164 kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 +
165 0.5));
166 superstructure_encoder_filter_.SetPeriodNanoSeconds(
167 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
168 kMaxSuperstructureEncoderPulsesPerSecond * 1e9 +
169 0.5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000170 hall_filter_.SetPeriodNanoSeconds(100000);
171 }
172
Comran Morshed225f0b92016-02-10 20:34:27 +0000173 // Drivetrain setters.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000175 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000176 drivetrain_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000177 }
178
179 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000180 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000181 drivetrain_right_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000182 }
183
Comran Morshed225f0b92016-02-10 20:34:27 +0000184 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
185 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000186 }
187
Comran Morshed225f0b92016-02-10 20:34:27 +0000188 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
189 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000190 }
191
Comran Morshed225f0b92016-02-10 20:34:27 +0000192 // Shooter setters.
193 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000194 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000195 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000196 }
197
Comran Morshed225f0b92016-02-10 20:34:27 +0000198 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000199 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000200 shooter_right_encoder_ = ::std::move(encoder);
201 }
202
203 // Intake setters.
204 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000205 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000206 intake_encoder_.set_encoder(::std::move(encoder));
207 }
208
209 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
210 intake_encoder_.set_potentiometer(::std::move(potentiometer));
211 }
212
213 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000214 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000215 intake_encoder_.set_index(::std::move(index));
216 }
217
218 // Shoulder setters.
219 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000220 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000221 shoulder_encoder_.set_encoder(::std::move(encoder));
222 }
223
224 void set_shoulder_potentiometer(
225 ::std::unique_ptr<AnalogInput> potentiometer) {
226 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
227 }
228
229 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000230 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000231 shoulder_encoder_.set_index(::std::move(index));
232 }
233
234 // Wrist setters.
235 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000236 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000237 wrist_encoder_.set_encoder(::std::move(encoder));
238 }
239
240 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
241 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
242 }
243
244 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000245 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000246 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247 }
248
Comran Morshedaa0573c2016-03-05 19:05:54 +0000249 // Ball detector setter.
250 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
251 ball_detector_ = ::std::move(analog);
252 }
253
Brian Silverman68cb5c22016-03-20 18:11:14 -0700254 // Autonomous mode switch setter.
255 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
256 autonomous_modes_.at(i) = ::std::move(sensor);
257 }
258
Comran Morshedaa0573c2016-03-05 19:05:54 +0000259
Comran Morshed9a9948c2016-01-16 15:58:04 +0000260 // All of the DMA-related set_* calls must be made before this, and it doesn't
261 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000262
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000263 void set_dma(::std::unique_ptr<DMA> dma) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000264 dma_synchronizer_.reset(
265 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000266 dma_synchronizer_->Add(&intake_encoder_);
267 dma_synchronizer_->Add(&shoulder_encoder_);
268 dma_synchronizer_->Add(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000269 }
270
271 void operator()() {
272 ::aos::SetCurrentThreadName("SensorReader");
273
274 my_pid_ = getpid();
275 ds_ =
276#ifdef WPILIB2015
277 DriverStation::GetInstance();
278#else
279 &DriverStation::GetInstance();
280#endif
281
Comran Morshed9a9948c2016-01-16 15:58:04 +0000282 dma_synchronizer_->Start();
283
284 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
285 ::aos::time::Time::InMS(4));
286
287 ::aos::SetCurrentThreadRealtimePriority(40);
288 while (run_) {
289 {
290 const int iterations = phased_loop.SleepUntilNext();
291 if (iterations != 1) {
292 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
293 }
294 }
295 RunIteration();
296 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000297 }
298
299 void RunIteration() {
300 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
301
302 const auto &values = constants::GetValues();
303
304 {
305 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
306 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800307 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000308 drivetrain_message->left_encoder =
309 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
310 drivetrain_message->left_speed =
311 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
312 drivetrain_message->right_speed =
313 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
314
Comran Morshed9a9948c2016-01-16 15:58:04 +0000315 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000316 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000317 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000318 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000319
320 drivetrain_message.Send();
321 }
322
Comran Morshed9a9948c2016-01-16 15:58:04 +0000323 dma_synchronizer_->RunIteration();
Comran Morshed225f0b92016-02-10 20:34:27 +0000324
325 {
326 auto shooter_message = shooter_queue.position.MakeMessage();
327 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800328 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000329 shooter_message->theta_right =
330 shooter_translate(shooter_right_encoder_->GetRaw());
331 shooter_message.Send();
332 }
333
334 {
335 auto superstructure_message = superstructure_queue.position.MakeMessage();
336 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
337 intake_translate, intake_pot_translate, false,
338 values.intake.pot_offset);
339 CopyPotAndIndexPosition(shoulder_encoder_,
340 &superstructure_message->shoulder,
341 shoulder_translate, shoulder_pot_translate, false,
342 values.shoulder.pot_offset);
343 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800344 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000345 values.wrist.pot_offset);
346
347 superstructure_message.Send();
348 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000349
350 {
351 auto ball_detector_message =
352 ::y2016::sensors::ball_detector.MakeMessage();
353 ball_detector_message->voltage = ball_detector_->GetVoltage();
354 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
355 ball_detector_message.Send();
356 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700357
358 {
359 auto auto_mode_message = ::y2016::actors::auto_mode.MakeMessage();
360 auto_mode_message->mode = 0;
361 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
362 if (autonomous_modes_[i]->Get()) {
363 auto_mode_message->mode |= 1 << i;
364 }
365 }
366 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
367 auto_mode_message.Send();
368 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000369 }
370
371 void Quit() { run_ = false; }
372
373 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000374 void CopyPotAndIndexPosition(
375 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
376 ::frc971::PotAndIndexPosition *position,
377 ::std::function<double(int32_t)> encoder_translate,
378 ::std::function<double(double)> potentiometer_translate, bool reverse,
379 double pot_offset) {
380 const double multiplier = reverse ? -1.0 : 1.0;
381 position->encoder =
382 multiplier * encoder_translate(encoder.polled_encoder_value());
383 position->pot = multiplier * potentiometer_translate(
384 encoder.polled_potentiometer_voltage()) +
385 pot_offset;
386 position->latched_encoder =
387 multiplier * encoder_translate(encoder.last_encoder_value());
388 position->latched_pot =
389 multiplier *
390 potentiometer_translate(encoder.last_potentiometer_voltage()) +
391 pot_offset;
392 position->index_pulses = encoder.index_posedge_count();
393 }
394
Comran Morshed9a9948c2016-01-16 15:58:04 +0000395 int32_t my_pid_;
396 DriverStation *ds_;
397
398 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
399
Comran Morshed225f0b92016-02-10 20:34:27 +0000400 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
401 drivetrain_right_encoder_;
402 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
403
404 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000405 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
406 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000407 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000408
Brian Silverman68cb5c22016-03-20 18:11:14 -0700409 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
410
Comran Morshed9a9948c2016-01-16 15:58:04 +0000411 ::std::atomic<bool> run_{true};
Comran Morshed1b764322016-02-14 20:18:12 +0000412 DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_,
413 superstructure_encoder_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000414};
415
416class SolenoidWriter {
417 public:
418 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
419 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000420 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuh843412b2016-03-20 16:48:46 -0700421 shooter_(".y2016.control_loops.shooter.shooter_queue.output"),
422 superstructure_(
423 ".y2016.control_loops.superstructure_queue.output") {
424 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000425
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800426 void set_compressor(::std::unique_ptr<Compressor> compressor) {
427 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000428 }
429
430 void set_drivetrain_left(
431 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
432 drivetrain_left_ = ::std::move(s);
433 }
434
435 void set_drivetrain_right(
436 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
437 drivetrain_right_ = ::std::move(s);
438 }
439
Austin Schuh843412b2016-03-20 16:48:46 -0700440 void set_traverse(
441 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
442 traverse_ = ::std::move(s);
443 }
444
445 void set_traverse_latch(
446 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
447 traverse_latch_ = ::std::move(s);
448 }
449
Comran Morshedb79c4242016-02-06 18:27:26 +0000450 void set_shooter_clamp(
451 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
452 shooter_clamp_ = ::std::move(s);
453 }
454
455 void set_shooter_pusher(
456 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
457 shooter_pusher_ = ::std::move(s);
458 }
459
Austin Schuhe0729a62016-03-12 21:54:17 -0800460 void set_lights(
461 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
462 lights_ = ::std::move(s);
463 }
464
Comran Morshed9a9948c2016-01-16 15:58:04 +0000465 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800466 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000467 ::aos::SetCurrentThreadName("Solenoids");
468 ::aos::SetCurrentThreadRealtimePriority(27);
469
470 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
471 ::aos::time::Time::InMS(1));
472
473 while (run_) {
474 {
475 const int iterations = phased_loop.SleepUntilNext();
476 if (iterations != 1) {
477 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
478 }
479 }
480
481 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000482 drivetrain_.FetchLatest();
483 if (drivetrain_.get()) {
484 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
485 drivetrain_left_->Set(!drivetrain_->left_high);
486 drivetrain_right_->Set(!drivetrain_->right_high);
487 }
488 }
489
490 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000491 shooter_.FetchLatest();
492 if (shooter_.get()) {
493 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
494 shooter_clamp_->Set(shooter_->clamp_open);
495 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800496 lights_->Set(shooter_->lights_on);
Comran Morshedb79c4242016-02-06 18:27:26 +0000497 }
498 }
499
500 {
Austin Schuh843412b2016-03-20 16:48:46 -0700501 superstructure_.FetchLatest();
502 if (superstructure_.get()) {
503 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
504 traverse_->Set(superstructure_->traverse_down);
505 traverse_latch_->Set(superstructure_->traverse_unlatched);
506 }
507 }
508
509 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000510 ::frc971::wpilib::PneumaticsToLog to_log;
511 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800512 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000513 }
514
515 pcm_->Flush();
516 to_log.read_solenoids = pcm_->GetAll();
517 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
518 }
519 }
520 }
521
522 void Quit() { run_ = false; }
523
524 private:
525 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
526
Comran Morshed225f0b92016-02-10 20:34:27 +0000527 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Austin Schuh843412b2016-03-20 16:48:46 -0700528 drivetrain_right_, shooter_clamp_, shooter_pusher_, lights_, traverse_,
529 traverse_latch_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800530 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000531
Comran Morshed9a9948c2016-01-16 15:58:04 +0000532 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000533 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Austin Schuh843412b2016-03-20 16:48:46 -0700534 ::aos::Queue<
535 ::y2016::control_loops::SuperstructureQueue::Output>
536 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000537
538 ::std::atomic<bool> run_{true};
539};
540
541class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
542 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000543 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
544 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000545 }
546
Comran Morshed225f0b92016-02-10 20:34:27 +0000547 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
548 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000549 }
550
551 private:
552 virtual void Read() override {
553 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
554 }
555
556 virtual void Write() override {
557 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
558 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800559 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
560 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000561 }
562
563 virtual void Stop() override {
564 LOG(WARNING, "drivetrain output too old\n");
Comran Morshed225f0b92016-02-10 20:34:27 +0000565 drivetrain_left_talon_->Disable();
566 drivetrain_right_talon_->Disable();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000567 }
568
Comran Morshed225f0b92016-02-10 20:34:27 +0000569 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
570};
571
572class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
573 public:
574 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
575 shooter_left_talon_ = ::std::move(t);
576 }
577
578 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
579 shooter_right_talon_ = ::std::move(t);
580 }
581
582 private:
583 virtual void Read() override {
584 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
585 }
586
587 virtual void Write() override {
588 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
589 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800590
Comran Morshed225f0b92016-02-10 20:34:27 +0000591 shooter_left_talon_->Set(queue->voltage_left / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800592 shooter_right_talon_->Set(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000593 }
594
595 virtual void Stop() override {
596 LOG(WARNING, "Shooter output too old.\n");
597 shooter_left_talon_->Disable();
598 shooter_right_talon_->Disable();
599 }
600
601 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
602};
603
604class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
605 public:
606 void set_intake_talon(::std::unique_ptr<Talon> t) {
607 intake_talon_ = ::std::move(t);
608 }
609
610 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
611 shoulder_talon_ = ::std::move(t);
612 }
613
614 void set_wrist_talon(::std::unique_ptr<Talon> t) {
615 wrist_talon_ = ::std::move(t);
616 }
617
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800618 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
619 top_rollers_talon_ = ::std::move(t);
620 }
621
622 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
623 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000624 }
625
Comran Morshed225f0b92016-02-10 20:34:27 +0000626 private:
627 virtual void Read() override {
628 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
629 }
630
631 virtual void Write() override {
632 auto &queue = ::y2016::control_loops::superstructure_queue.output;
633 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha9992ff2016-02-28 21:59:23 -0800634 intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower,
635 kMaxBringupPower) /
636 12.0);
637 shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder,
638 -kMaxBringupPower, kMaxBringupPower) /
639 12.0);
640 wrist_talon_->Set(
641 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
642 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800643 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
644 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000645 }
646
647 virtual void Stop() override {
648 LOG(WARNING, "Superstructure output too old.\n");
649 intake_talon_->Disable();
650 shoulder_talon_->Disable();
651 wrist_talon_->Disable();
652 }
653
Comran Morshedf4cd7642016-02-15 20:40:49 +0000654 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800655 top_rollers_talon_, bottom_rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000656};
657
Comran Morshed9a9948c2016-01-16 15:58:04 +0000658class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
659 public:
660 ::std::unique_ptr<Encoder> make_encoder(int index) {
661 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
662 Encoder::k4X);
663 }
664
665 void Run() override {
666 ::aos::InitNRT();
667 ::aos::SetCurrentThreadName("StartCompetition");
668
669 ::frc971::wpilib::JoystickSender joystick_sender;
670 ::std::thread joystick_thread(::std::ref(joystick_sender));
671
672 ::frc971::wpilib::PDPFetcher pdp_fetcher;
673 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
674 SensorReader reader;
675
Austin Schuh9f77fd22016-02-21 02:53:58 -0800676 reader.set_drivetrain_left_encoder(make_encoder(5));
677 reader.set_drivetrain_right_encoder(make_encoder(6));
678 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
679 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000680
Austin Schuh9f77fd22016-02-21 02:53:58 -0800681 reader.set_shooter_left_encoder(make_encoder(3));
682 reader.set_shooter_right_encoder(make_encoder(4));
Comran Morshed225f0b92016-02-10 20:34:27 +0000683
684 reader.set_intake_encoder(make_encoder(0));
685 reader.set_intake_index(make_unique<DigitalInput>(0));
686 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
687
Austin Schuh9f77fd22016-02-21 02:53:58 -0800688 reader.set_shoulder_encoder(make_encoder(2));
689 reader.set_shoulder_index(make_unique<DigitalInput>(2));
690 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000691
Austin Schuh9f77fd22016-02-21 02:53:58 -0800692 reader.set_wrist_encoder(make_encoder(1));
693 reader.set_wrist_index(make_unique<DigitalInput>(1));
694 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000695
Comran Morshedaa0573c2016-03-05 19:05:54 +0000696 reader.set_ball_detector(make_unique<AnalogInput>(7));
697
Brian Silverman68cb5c22016-03-20 18:11:14 -0700698 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
699 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
700 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
701 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
702
Comran Morshed9a9948c2016-01-16 15:58:04 +0000703 reader.set_dma(make_unique<DMA>());
704 ::std::thread reader_thread(::std::ref(reader));
705
706 ::frc971::wpilib::GyroSender gyro_sender;
707 ::std::thread gyro_thread(::std::ref(gyro_sender));
708
Brian Silverman5f17a972016-02-28 01:49:32 -0500709#if 0
710 auto imu_trigger = make_unique<DigitalInput>(100);
711 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
712 ::std::thread imu_thread(::std::ref(imu));
713#endif
714
Comran Morshed9a9948c2016-01-16 15:58:04 +0000715 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000716 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000717 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000718 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800719 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000720 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
721
Comran Morshed225f0b92016-02-10 20:34:27 +0000722 ShooterWriter shooter_writer;
723 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800724 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000725 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800726 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000727 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
728
729 SuperstructureWriter superstructure_writer;
730 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800731 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000732 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800733 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000734 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800735 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800736 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800737 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800738 superstructure_writer.set_bottom_rollers_talon(
739 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000740 ::std::thread superstructure_writer_thread(
741 ::std::ref(superstructure_writer));
742
Comran Morshed9a9948c2016-01-16 15:58:04 +0000743 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
744 new ::frc971::wpilib::BufferedPcm());
745 SolenoidWriter solenoid_writer(pcm);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800746 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1));
747 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700748 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
749 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800750 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
751 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800752 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000753
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800754 solenoid_writer.set_compressor(make_unique<Compressor>());
755
Comran Morshed9a9948c2016-01-16 15:58:04 +0000756 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
757
758 // Wait forever. Not much else to do...
759 while (true) {
760 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
761 if (r != 0) {
762 PLOG(WARNING, "infinite select failed");
763 } else {
764 PLOG(WARNING, "infinite select succeeded??\n");
765 }
766 }
767
768 LOG(ERROR, "Exiting WPILibRobot\n");
769
770 joystick_sender.Quit();
771 joystick_thread.join();
772 pdp_fetcher.Quit();
773 pdp_fetcher_thread.join();
774 reader.Quit();
775 reader_thread.join();
776 gyro_sender.Quit();
777 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500778#if 0
779 imu.Quit();
780 imu_thread.join();
781#endif
Comran Morshed9a9948c2016-01-16 15:58:04 +0000782
783 drivetrain_writer.Quit();
784 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000785 shooter_writer.Quit();
786 shooter_writer_thread.join();
787 superstructure_writer.Quit();
788 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000789 solenoid_writer.Quit();
790 solenoid_thread.join();
791
792 ::aos::Cleanup();
793 }
794};
795
796} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000797} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000798
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000799AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);