blob: 6d163231ce78fb81f861e51ff6832a315d86948a [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
Comran Morshed9a9948c2016-01-16 15:58:04 +0000411 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800412 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000413 ::aos::SetCurrentThreadName("Solenoids");
414 ::aos::SetCurrentThreadRealtimePriority(27);
415
416 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
417 ::aos::time::Time::InMS(1));
418
419 while (run_) {
420 {
421 const int iterations = phased_loop.SleepUntilNext();
422 if (iterations != 1) {
423 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
424 }
425 }
426
427 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000428 drivetrain_.FetchLatest();
429 if (drivetrain_.get()) {
430 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
431 drivetrain_left_->Set(!drivetrain_->left_high);
432 drivetrain_right_->Set(!drivetrain_->right_high);
433 }
434 }
435
436 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000437 shooter_.FetchLatest();
438 if (shooter_.get()) {
439 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
440 shooter_clamp_->Set(shooter_->clamp_open);
441 shooter_pusher_->Set(shooter_->push_to_shooter);
442 }
443 }
444
445 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000446 ::frc971::wpilib::PneumaticsToLog to_log;
447 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800448 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000449 }
450
451 pcm_->Flush();
452 to_log.read_solenoids = pcm_->GetAll();
453 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
454 }
455 }
456 }
457
458 void Quit() { run_ = false; }
459
460 private:
461 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
462
Comran Morshed225f0b92016-02-10 20:34:27 +0000463 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Comran Morshedb79c4242016-02-06 18:27:26 +0000464 drivetrain_right_, shooter_clamp_, shooter_pusher_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800465 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000466
Comran Morshed9a9948c2016-01-16 15:58:04 +0000467 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000468 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000469
470 ::std::atomic<bool> run_{true};
471};
472
473class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
474 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000475 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
476 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000477 }
478
Comran Morshed225f0b92016-02-10 20:34:27 +0000479 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
480 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000481 }
482
483 private:
484 virtual void Read() override {
485 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
486 }
487
488 virtual void Write() override {
489 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
490 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800491 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
492 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000493 }
494
495 virtual void Stop() override {
496 LOG(WARNING, "drivetrain output too old\n");
Comran Morshed225f0b92016-02-10 20:34:27 +0000497 drivetrain_left_talon_->Disable();
498 drivetrain_right_talon_->Disable();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000499 }
500
Comran Morshed225f0b92016-02-10 20:34:27 +0000501 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
502};
503
504class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
505 public:
506 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
507 shooter_left_talon_ = ::std::move(t);
508 }
509
510 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
511 shooter_right_talon_ = ::std::move(t);
512 }
513
514 private:
515 virtual void Read() override {
516 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
517 }
518
519 virtual void Write() override {
520 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
521 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800522
Comran Morshed225f0b92016-02-10 20:34:27 +0000523 shooter_left_talon_->Set(queue->voltage_left / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800524 shooter_right_talon_->Set(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000525 }
526
527 virtual void Stop() override {
528 LOG(WARNING, "Shooter output too old.\n");
529 shooter_left_talon_->Disable();
530 shooter_right_talon_->Disable();
531 }
532
533 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
534};
535
536class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
537 public:
538 void set_intake_talon(::std::unique_ptr<Talon> t) {
539 intake_talon_ = ::std::move(t);
540 }
541
542 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
543 shoulder_talon_ = ::std::move(t);
544 }
545
546 void set_wrist_talon(::std::unique_ptr<Talon> t) {
547 wrist_talon_ = ::std::move(t);
548 }
549
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800550 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
551 top_rollers_talon_ = ::std::move(t);
552 }
553
554 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
555 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000556 }
557
Comran Morshed225f0b92016-02-10 20:34:27 +0000558 private:
559 virtual void Read() override {
560 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
561 }
562
563 virtual void Write() override {
564 auto &queue = ::y2016::control_loops::superstructure_queue.output;
565 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha9992ff2016-02-28 21:59:23 -0800566 intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower,
567 kMaxBringupPower) /
568 12.0);
569 shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder,
570 -kMaxBringupPower, kMaxBringupPower) /
571 12.0);
572 wrist_talon_->Set(
573 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
574 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800575 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
576 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000577 }
578
579 virtual void Stop() override {
580 LOG(WARNING, "Superstructure output too old.\n");
581 intake_talon_->Disable();
582 shoulder_talon_->Disable();
583 wrist_talon_->Disable();
584 }
585
Comran Morshedf4cd7642016-02-15 20:40:49 +0000586 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800587 top_rollers_talon_, bottom_rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000588};
589
Comran Morshed9a9948c2016-01-16 15:58:04 +0000590class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
591 public:
592 ::std::unique_ptr<Encoder> make_encoder(int index) {
593 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
594 Encoder::k4X);
595 }
596
597 void Run() override {
598 ::aos::InitNRT();
599 ::aos::SetCurrentThreadName("StartCompetition");
600
601 ::frc971::wpilib::JoystickSender joystick_sender;
602 ::std::thread joystick_thread(::std::ref(joystick_sender));
603
604 ::frc971::wpilib::PDPFetcher pdp_fetcher;
605 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
606 SensorReader reader;
607
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000608 // TODO(constants): Update these input numbers.
Austin Schuh9f77fd22016-02-21 02:53:58 -0800609 reader.set_drivetrain_left_encoder(make_encoder(5));
610 reader.set_drivetrain_right_encoder(make_encoder(6));
611 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
612 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000613
Austin Schuh9f77fd22016-02-21 02:53:58 -0800614 reader.set_shooter_left_encoder(make_encoder(3));
615 reader.set_shooter_right_encoder(make_encoder(4));
Comran Morshed225f0b92016-02-10 20:34:27 +0000616
617 reader.set_intake_encoder(make_encoder(0));
618 reader.set_intake_index(make_unique<DigitalInput>(0));
619 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
620
Austin Schuh9f77fd22016-02-21 02:53:58 -0800621 reader.set_shoulder_encoder(make_encoder(2));
622 reader.set_shoulder_index(make_unique<DigitalInput>(2));
623 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000624
Austin Schuh9f77fd22016-02-21 02:53:58 -0800625 reader.set_wrist_encoder(make_encoder(1));
626 reader.set_wrist_index(make_unique<DigitalInput>(1));
627 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000628
Comran Morshed9a9948c2016-01-16 15:58:04 +0000629 reader.set_dma(make_unique<DMA>());
630 ::std::thread reader_thread(::std::ref(reader));
631
632 ::frc971::wpilib::GyroSender gyro_sender;
633 ::std::thread gyro_thread(::std::ref(gyro_sender));
634
Brian Silverman5f17a972016-02-28 01:49:32 -0500635#if 0
636 auto imu_trigger = make_unique<DigitalInput>(100);
637 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
638 ::std::thread imu_thread(::std::ref(imu));
639#endif
640
Comran Morshed9a9948c2016-01-16 15:58:04 +0000641 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000642 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000643 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000644 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800645 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000646 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
647
Comran Morshed225f0b92016-02-10 20:34:27 +0000648 ShooterWriter shooter_writer;
649 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800650 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000651 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800652 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000653 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
654
655 SuperstructureWriter superstructure_writer;
656 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800657 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000658 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800659 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000660 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800661 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800662 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800663 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800664 superstructure_writer.set_bottom_rollers_talon(
665 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000666 ::std::thread superstructure_writer_thread(
667 ::std::ref(superstructure_writer));
668
Comran Morshed9a9948c2016-01-16 15:58:04 +0000669 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
670 new ::frc971::wpilib::BufferedPcm());
671 SolenoidWriter solenoid_writer(pcm);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800672 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1));
673 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0));
674 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
675 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000676
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800677 solenoid_writer.set_compressor(make_unique<Compressor>());
678
Comran Morshed9a9948c2016-01-16 15:58:04 +0000679 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
680
681 // Wait forever. Not much else to do...
682 while (true) {
683 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
684 if (r != 0) {
685 PLOG(WARNING, "infinite select failed");
686 } else {
687 PLOG(WARNING, "infinite select succeeded??\n");
688 }
689 }
690
691 LOG(ERROR, "Exiting WPILibRobot\n");
692
693 joystick_sender.Quit();
694 joystick_thread.join();
695 pdp_fetcher.Quit();
696 pdp_fetcher_thread.join();
697 reader.Quit();
698 reader_thread.join();
699 gyro_sender.Quit();
700 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500701#if 0
702 imu.Quit();
703 imu_thread.join();
704#endif
Comran Morshed9a9948c2016-01-16 15:58:04 +0000705
706 drivetrain_writer.Quit();
707 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000708 shooter_writer.Quit();
709 shooter_writer_thread.join();
710 superstructure_writer.Quit();
711 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000712 solenoid_writer.Quit();
713 solenoid_thread.join();
714
715 ::aos::Cleanup();
716 }
717};
718
719} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000720} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000721
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000722AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);