blob: b1c52a45affa03af583d341a57dd500e6bc4ef2a [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"
16#include "dma.h"
17#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 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"
Comran Morshed9a9948c2016-01-16 15:58:04 +000053
54#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
58using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000059using ::y2016::control_loops::shooter::shooter_queue;
60using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000061
Comran Morshed6c6a0a92016-01-17 12:45:16 +000062namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000063namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080064namespace {
65constexpr double kMaxBringupPower = 12.0;
66} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000067
68// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
69// DMA stuff and then removing the * 2.0 in *_translate.
70// The low bit is direction.
71
72// TODO(brian): Replace this with ::std::make_unique once all our toolchains
73// have support.
74template <class T, class... U>
75std::unique_ptr<T> make_unique(U &&... u) {
76 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
77}
78
Comran Morshed225f0b92016-02-10 20:34:27 +000079// Translates for the sensor values to convert raw index pulses into something
80// with proper units.
81
82// TODO(comran): Template these methods since there is a lot of repetition here.
83double hall_translate(double in) {
84 // Turn voltage from our 3-state halls into a ratio that the loop can use.
85 return in / 5.0;
86}
87
Comran Morshed9a9948c2016-01-16 15:58:04 +000088double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000089 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000090 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080091 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000092}
93
94double drivetrain_velocity_translate(double in) {
95 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000096 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080097 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000098}
99
Comran Morshed225f0b92016-02-10 20:34:27 +0000100double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +0000101 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000102 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
103}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000104
Comran Morshed225f0b92016-02-10 20:34:27 +0000105double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800106 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000107 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
108}
109
110double shoulder_translate(int32_t in) {
111 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
112 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
113}
114
115double wrist_translate(int32_t in) {
116 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
117 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
118}
119
120double intake_pot_translate(double voltage) {
121 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000122 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000123}
124
125double shoulder_pot_translate(double voltage) {
126 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000127 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000128}
129
130double wrist_pot_translate(double voltage) {
131 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000132 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000133}
134
Comran Morshed1b764322016-02-14 20:18:12 +0000135constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
136 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
137 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
138 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
139
140constexpr double kMaxShooterEncoderPulsesPerSecond =
141 18700.0 /* 775pro free speed RPM */ * 12.0 /
142 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
143 128.0 /* CPR */ * 4 /* edges per cycle */;
144
145double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
146 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
147
148constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
149 18700.0 /* 775pro free speed RPM */ * 12.0 /
150 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
151 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000152
Comran Morshed225f0b92016-02-10 20:34:27 +0000153// Class to send position messages with sensor readings to our loops.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000154class SensorReader {
155 public:
156 SensorReader() {
157 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
158 // we should ever see.
Comran Morshed1b764322016-02-14 20:18:12 +0000159 drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds(
160 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
161 kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 +
162 0.5));
163 superstructure_encoder_filter_.SetPeriodNanoSeconds(
164 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
165 kMaxSuperstructureEncoderPulsesPerSecond * 1e9 +
166 0.5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000167 hall_filter_.SetPeriodNanoSeconds(100000);
168 }
169
Comran Morshed225f0b92016-02-10 20:34:27 +0000170 // Drivetrain setters.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000171 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000172 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173 drivetrain_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174 }
175
176 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000177 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000178 drivetrain_right_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000179 }
180
Comran Morshed225f0b92016-02-10 20:34:27 +0000181 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
182 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000183 }
184
Comran Morshed225f0b92016-02-10 20:34:27 +0000185 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
186 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000187 }
188
Comran Morshed225f0b92016-02-10 20:34:27 +0000189 // Shooter setters.
190 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000191 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000192 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000193 }
194
Comran Morshed225f0b92016-02-10 20:34:27 +0000195 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000196 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000197 shooter_right_encoder_ = ::std::move(encoder);
198 }
199
200 // Intake setters.
201 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000202 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000203 intake_encoder_.set_encoder(::std::move(encoder));
204 }
205
206 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
207 intake_encoder_.set_potentiometer(::std::move(potentiometer));
208 }
209
210 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000211 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000212 intake_encoder_.set_index(::std::move(index));
213 }
214
215 // Shoulder setters.
216 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000217 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000218 shoulder_encoder_.set_encoder(::std::move(encoder));
219 }
220
221 void set_shoulder_potentiometer(
222 ::std::unique_ptr<AnalogInput> potentiometer) {
223 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
224 }
225
226 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000227 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000228 shoulder_encoder_.set_index(::std::move(index));
229 }
230
231 // Wrist setters.
232 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000233 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000234 wrist_encoder_.set_encoder(::std::move(encoder));
235 }
236
237 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
238 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
239 }
240
241 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000242 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000243 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000244 }
245
Comran Morshed9a9948c2016-01-16 15:58:04 +0000246 // All of the DMA-related set_* calls must be made before this, and it doesn't
247 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000248
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000249 // TODO(comran): Add 2016 things down below for dma synchronization.
250 void set_dma(::std::unique_ptr<DMA> dma) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251 dma_synchronizer_.reset(
252 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000253 dma_synchronizer_->Add(&intake_encoder_);
254 dma_synchronizer_->Add(&shoulder_encoder_);
255 dma_synchronizer_->Add(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000256 }
257
258 void operator()() {
259 ::aos::SetCurrentThreadName("SensorReader");
260
261 my_pid_ = getpid();
262 ds_ =
263#ifdef WPILIB2015
264 DriverStation::GetInstance();
265#else
266 &DriverStation::GetInstance();
267#endif
268
Comran Morshed9a9948c2016-01-16 15:58:04 +0000269 dma_synchronizer_->Start();
270
271 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
272 ::aos::time::Time::InMS(4));
273
274 ::aos::SetCurrentThreadRealtimePriority(40);
275 while (run_) {
276 {
277 const int iterations = phased_loop.SleepUntilNext();
278 if (iterations != 1) {
279 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
280 }
281 }
282 RunIteration();
283 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000284 }
285
286 void RunIteration() {
287 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
288
289 const auto &values = constants::GetValues();
290
291 {
292 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
293 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800294 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000295 drivetrain_message->left_encoder =
296 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
297 drivetrain_message->left_speed =
298 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
299 drivetrain_message->right_speed =
300 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
301
Comran Morshed9a9948c2016-01-16 15:58:04 +0000302 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000303 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000304 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000305 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000306
307 drivetrain_message.Send();
308 }
309
Comran Morshed9a9948c2016-01-16 15:58:04 +0000310 dma_synchronizer_->RunIteration();
Comran Morshed225f0b92016-02-10 20:34:27 +0000311
312 {
313 auto shooter_message = shooter_queue.position.MakeMessage();
314 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800315 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000316 shooter_message->theta_right =
317 shooter_translate(shooter_right_encoder_->GetRaw());
318 shooter_message.Send();
319 }
320
321 {
322 auto superstructure_message = superstructure_queue.position.MakeMessage();
323 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
324 intake_translate, intake_pot_translate, false,
325 values.intake.pot_offset);
326 CopyPotAndIndexPosition(shoulder_encoder_,
327 &superstructure_message->shoulder,
328 shoulder_translate, shoulder_pot_translate, false,
329 values.shoulder.pot_offset);
330 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800331 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000332 values.wrist.pot_offset);
333
334 superstructure_message.Send();
335 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000336 }
337
338 void Quit() { run_ = false; }
339
340 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000341 void CopyPotAndIndexPosition(
342 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
343 ::frc971::PotAndIndexPosition *position,
344 ::std::function<double(int32_t)> encoder_translate,
345 ::std::function<double(double)> potentiometer_translate, bool reverse,
346 double pot_offset) {
347 const double multiplier = reverse ? -1.0 : 1.0;
348 position->encoder =
349 multiplier * encoder_translate(encoder.polled_encoder_value());
350 position->pot = multiplier * potentiometer_translate(
351 encoder.polled_potentiometer_voltage()) +
352 pot_offset;
353 position->latched_encoder =
354 multiplier * encoder_translate(encoder.last_encoder_value());
355 position->latched_pot =
356 multiplier *
357 potentiometer_translate(encoder.last_potentiometer_voltage()) +
358 pot_offset;
359 position->index_pulses = encoder.index_posedge_count();
360 }
361
Comran Morshed9a9948c2016-01-16 15:58:04 +0000362 int32_t my_pid_;
363 DriverStation *ds_;
364
365 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
366
Comran Morshed225f0b92016-02-10 20:34:27 +0000367 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
368 drivetrain_right_encoder_;
369 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
370
371 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000372 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
373 shoulder_encoder_, wrist_encoder_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000374
Comran Morshed9a9948c2016-01-16 15:58:04 +0000375 ::std::atomic<bool> run_{true};
Comran Morshed1b764322016-02-14 20:18:12 +0000376 DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_,
377 superstructure_encoder_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000378};
379
380class SolenoidWriter {
381 public:
382 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
383 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000384 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800385 shooter_(".y2016.control_loops.shooter.shooter_queue.output") {}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000386
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800387 void set_compressor(::std::unique_ptr<Compressor> compressor) {
388 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000389 }
390
391 void set_drivetrain_left(
392 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
393 drivetrain_left_ = ::std::move(s);
394 }
395
396 void set_drivetrain_right(
397 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
398 drivetrain_right_ = ::std::move(s);
399 }
400
Comran Morshedb79c4242016-02-06 18:27:26 +0000401 void set_shooter_clamp(
402 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
403 shooter_clamp_ = ::std::move(s);
404 }
405
406 void set_shooter_pusher(
407 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
408 shooter_pusher_ = ::std::move(s);
409 }
410
Austin Schuhe0729a62016-03-12 21:54:17 -0800411 void set_lights(
412 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
413 lights_ = ::std::move(s);
414 }
415
Comran Morshed9a9948c2016-01-16 15:58:04 +0000416 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800417 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000418 ::aos::SetCurrentThreadName("Solenoids");
419 ::aos::SetCurrentThreadRealtimePriority(27);
420
421 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
422 ::aos::time::Time::InMS(1));
423
424 while (run_) {
425 {
426 const int iterations = phased_loop.SleepUntilNext();
427 if (iterations != 1) {
428 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
429 }
430 }
431
432 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000433 drivetrain_.FetchLatest();
434 if (drivetrain_.get()) {
435 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
436 drivetrain_left_->Set(!drivetrain_->left_high);
437 drivetrain_right_->Set(!drivetrain_->right_high);
438 }
439 }
440
441 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000442 shooter_.FetchLatest();
443 if (shooter_.get()) {
444 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
445 shooter_clamp_->Set(shooter_->clamp_open);
446 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800447 lights_->Set(shooter_->lights_on);
Comran Morshedb79c4242016-02-06 18:27:26 +0000448 }
449 }
450
451 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000452 ::frc971::wpilib::PneumaticsToLog to_log;
453 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800454 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000455 }
456
457 pcm_->Flush();
458 to_log.read_solenoids = pcm_->GetAll();
459 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
460 }
461 }
462 }
463
464 void Quit() { run_ = false; }
465
466 private:
467 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
468
Comran Morshed225f0b92016-02-10 20:34:27 +0000469 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Austin Schuhe0729a62016-03-12 21:54:17 -0800470 drivetrain_right_, shooter_clamp_, shooter_pusher_, lights_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800471 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000472
Comran Morshed9a9948c2016-01-16 15:58:04 +0000473 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000474 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000475
476 ::std::atomic<bool> run_{true};
477};
478
479class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
480 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000481 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
482 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000483 }
484
Comran Morshed225f0b92016-02-10 20:34:27 +0000485 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
486 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000487 }
488
489 private:
490 virtual void Read() override {
491 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
492 }
493
494 virtual void Write() override {
495 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
496 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800497 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
498 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000499 }
500
501 virtual void Stop() override {
502 LOG(WARNING, "drivetrain output too old\n");
Comran Morshed225f0b92016-02-10 20:34:27 +0000503 drivetrain_left_talon_->Disable();
504 drivetrain_right_talon_->Disable();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000505 }
506
Comran Morshed225f0b92016-02-10 20:34:27 +0000507 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
508};
509
510class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
511 public:
512 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
513 shooter_left_talon_ = ::std::move(t);
514 }
515
516 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
517 shooter_right_talon_ = ::std::move(t);
518 }
519
520 private:
521 virtual void Read() override {
522 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
523 }
524
525 virtual void Write() override {
526 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
527 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800528
Comran Morshed225f0b92016-02-10 20:34:27 +0000529 shooter_left_talon_->Set(queue->voltage_left / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800530 shooter_right_talon_->Set(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000531 }
532
533 virtual void Stop() override {
534 LOG(WARNING, "Shooter output too old.\n");
535 shooter_left_talon_->Disable();
536 shooter_right_talon_->Disable();
537 }
538
539 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
540};
541
542class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
543 public:
544 void set_intake_talon(::std::unique_ptr<Talon> t) {
545 intake_talon_ = ::std::move(t);
546 }
547
548 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
549 shoulder_talon_ = ::std::move(t);
550 }
551
552 void set_wrist_talon(::std::unique_ptr<Talon> t) {
553 wrist_talon_ = ::std::move(t);
554 }
555
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800556 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
557 top_rollers_talon_ = ::std::move(t);
558 }
559
560 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
561 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000562 }
563
Comran Morshed225f0b92016-02-10 20:34:27 +0000564 private:
565 virtual void Read() override {
566 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
567 }
568
569 virtual void Write() override {
570 auto &queue = ::y2016::control_loops::superstructure_queue.output;
571 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha9992ff2016-02-28 21:59:23 -0800572 intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower,
573 kMaxBringupPower) /
574 12.0);
575 shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder,
576 -kMaxBringupPower, kMaxBringupPower) /
577 12.0);
578 wrist_talon_->Set(
579 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
580 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800581 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
582 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000583 }
584
585 virtual void Stop() override {
586 LOG(WARNING, "Superstructure output too old.\n");
587 intake_talon_->Disable();
588 shoulder_talon_->Disable();
589 wrist_talon_->Disable();
590 }
591
Comran Morshedf4cd7642016-02-15 20:40:49 +0000592 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800593 top_rollers_talon_, bottom_rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000594};
595
Comran Morshed9a9948c2016-01-16 15:58:04 +0000596class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
597 public:
598 ::std::unique_ptr<Encoder> make_encoder(int index) {
599 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
600 Encoder::k4X);
601 }
602
603 void Run() override {
604 ::aos::InitNRT();
605 ::aos::SetCurrentThreadName("StartCompetition");
606
607 ::frc971::wpilib::JoystickSender joystick_sender;
608 ::std::thread joystick_thread(::std::ref(joystick_sender));
609
610 ::frc971::wpilib::PDPFetcher pdp_fetcher;
611 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
612 SensorReader reader;
613
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000614 // TODO(constants): Update these input numbers.
Austin Schuh9f77fd22016-02-21 02:53:58 -0800615 reader.set_drivetrain_left_encoder(make_encoder(5));
616 reader.set_drivetrain_right_encoder(make_encoder(6));
617 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
618 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000619
Austin Schuh9f77fd22016-02-21 02:53:58 -0800620 reader.set_shooter_left_encoder(make_encoder(3));
621 reader.set_shooter_right_encoder(make_encoder(4));
Comran Morshed225f0b92016-02-10 20:34:27 +0000622
623 reader.set_intake_encoder(make_encoder(0));
624 reader.set_intake_index(make_unique<DigitalInput>(0));
625 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
626
Austin Schuh9f77fd22016-02-21 02:53:58 -0800627 reader.set_shoulder_encoder(make_encoder(2));
628 reader.set_shoulder_index(make_unique<DigitalInput>(2));
629 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000630
Austin Schuh9f77fd22016-02-21 02:53:58 -0800631 reader.set_wrist_encoder(make_encoder(1));
632 reader.set_wrist_index(make_unique<DigitalInput>(1));
633 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000634
Comran Morshed9a9948c2016-01-16 15:58:04 +0000635 reader.set_dma(make_unique<DMA>());
636 ::std::thread reader_thread(::std::ref(reader));
637
638 ::frc971::wpilib::GyroSender gyro_sender;
639 ::std::thread gyro_thread(::std::ref(gyro_sender));
640
Brian Silverman5f17a972016-02-28 01:49:32 -0500641#if 0
642 auto imu_trigger = make_unique<DigitalInput>(100);
643 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
644 ::std::thread imu_thread(::std::ref(imu));
645#endif
646
Comran Morshed9a9948c2016-01-16 15:58:04 +0000647 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000648 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000649 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000650 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800651 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000652 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
653
Comran Morshed225f0b92016-02-10 20:34:27 +0000654 ShooterWriter shooter_writer;
655 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800656 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000657 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800658 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000659 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
660
661 SuperstructureWriter superstructure_writer;
662 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800663 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000664 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800665 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000666 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800667 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800668 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800669 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800670 superstructure_writer.set_bottom_rollers_talon(
671 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000672 ::std::thread superstructure_writer_thread(
673 ::std::ref(superstructure_writer));
674
Comran Morshed9a9948c2016-01-16 15:58:04 +0000675 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
676 new ::frc971::wpilib::BufferedPcm());
677 SolenoidWriter solenoid_writer(pcm);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800678 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1));
679 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0));
680 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
681 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800682 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000683
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800684 solenoid_writer.set_compressor(make_unique<Compressor>());
685
Comran Morshed9a9948c2016-01-16 15:58:04 +0000686 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
687
688 // Wait forever. Not much else to do...
689 while (true) {
690 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
691 if (r != 0) {
692 PLOG(WARNING, "infinite select failed");
693 } else {
694 PLOG(WARNING, "infinite select succeeded??\n");
695 }
696 }
697
698 LOG(ERROR, "Exiting WPILibRobot\n");
699
700 joystick_sender.Quit();
701 joystick_thread.join();
702 pdp_fetcher.Quit();
703 pdp_fetcher_thread.join();
704 reader.Quit();
705 reader_thread.join();
706 gyro_sender.Quit();
707 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500708#if 0
709 imu.Quit();
710 imu_thread.join();
711#endif
Comran Morshed9a9948c2016-01-16 15:58:04 +0000712
713 drivetrain_writer.Quit();
714 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000715 shooter_writer.Quit();
716 shooter_writer_thread.join();
717 superstructure_writer.Quit();
718 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000719 solenoid_writer.Quit();
720 solenoid_thread.join();
721
722 ::aos::Cleanup();
723 }
724};
725
726} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000727} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000728
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000729AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);