blob: 408ea8c6bb5c22c851c2406157d8a110a0f4181c [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"
15#include "Relay.h"
16#include "frc971/wpilib/wpilib_robot_base.h"
17#include "dma.h"
18#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#undef ERROR
22
23#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
25#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
29#include "aos/common/stl_mutex.h"
30#include "aos/linux_code/init.h"
31#include "aos/common/messages/robot_state.q.h"
32
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"
52
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000058using ::y2016::control_loops::shooter::shooter_queue;
59using ::y2016::control_loops::superstructure_queue;
Comran Morshed9a9948c2016-01-16 15:58:04 +000060
Comran Morshed6c6a0a92016-01-17 12:45:16 +000061namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000062namespace wpilib {
63
64// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
65// DMA stuff and then removing the * 2.0 in *_translate.
66// The low bit is direction.
67
68// TODO(brian): Replace this with ::std::make_unique once all our toolchains
69// have support.
70template <class T, class... U>
71std::unique_ptr<T> make_unique(U &&... u) {
72 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
73}
74
Comran Morshed225f0b92016-02-10 20:34:27 +000075// Translates for the sensor values to convert raw index pulses into something
76// with proper units.
77
78// TODO(comran): Template these methods since there is a lot of repetition here.
79double hall_translate(double in) {
80 // Turn voltage from our 3-state halls into a ratio that the loop can use.
81 return in / 5.0;
82}
83
Comran Morshed9a9948c2016-01-16 15:58:04 +000084double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000085 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000086 constants::Values::kDrivetrainEncoderRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +000087 control_loops::drivetrain::kWheelRadius * 2.0 / 2.0;
Comran Morshed9a9948c2016-01-16 15:58:04 +000088}
89
90double drivetrain_velocity_translate(double in) {
91 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000092 constants::Values::kDrivetrainEncoderRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +000093 control_loops::drivetrain::kWheelRadius * 2.0 / 2.0;
Comran Morshed9a9948c2016-01-16 15:58:04 +000094}
95
Comran Morshed225f0b92016-02-10 20:34:27 +000096double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000097 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000098 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
99}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000100
Comran Morshed225f0b92016-02-10 20:34:27 +0000101double intake_translate(int32_t in) {
102 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
103 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
104}
105
106double shoulder_translate(int32_t in) {
107 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
108 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
109}
110
111double wrist_translate(int32_t in) {
112 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
113 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
114}
115
116double intake_pot_translate(double voltage) {
117 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000118 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000119}
120
121double shoulder_pot_translate(double voltage) {
122 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000123 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000124}
125
126double wrist_pot_translate(double voltage) {
127 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000128 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000129}
130
Comran Morshed1b764322016-02-14 20:18:12 +0000131constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
132 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
133 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
134 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
135
136constexpr double kMaxShooterEncoderPulsesPerSecond =
137 18700.0 /* 775pro free speed RPM */ * 12.0 /
138 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
139 128.0 /* CPR */ * 4 /* edges per cycle */;
140
141double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
142 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
143
144constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
145 18700.0 /* 775pro free speed RPM */ * 12.0 /
146 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
147 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000148
Comran Morshed225f0b92016-02-10 20:34:27 +0000149// Class to send position messages with sensor readings to our loops.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000150class SensorReader {
151 public:
152 SensorReader() {
153 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
154 // we should ever see.
Comran Morshed1b764322016-02-14 20:18:12 +0000155 drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds(
156 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
157 kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 +
158 0.5));
159 superstructure_encoder_filter_.SetPeriodNanoSeconds(
160 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
161 kMaxSuperstructureEncoderPulsesPerSecond * 1e9 +
162 0.5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000163 hall_filter_.SetPeriodNanoSeconds(100000);
164 }
165
Comran Morshed225f0b92016-02-10 20:34:27 +0000166 // Drivetrain setters.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000167 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000168 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000169 drivetrain_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000170 }
171
172 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000173 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174 drivetrain_right_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000175 }
176
Comran Morshed225f0b92016-02-10 20:34:27 +0000177 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
178 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000179 }
180
Comran Morshed225f0b92016-02-10 20:34:27 +0000181 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
182 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000183 }
184
Comran Morshed225f0b92016-02-10 20:34:27 +0000185 // Shooter setters.
186 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000187 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000188 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000189 }
190
Comran Morshed225f0b92016-02-10 20:34:27 +0000191 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000192 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000193 shooter_right_encoder_ = ::std::move(encoder);
194 }
195
196 // Intake setters.
197 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000198 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000199 intake_encoder_.set_encoder(::std::move(encoder));
200 }
201
202 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
203 intake_encoder_.set_potentiometer(::std::move(potentiometer));
204 }
205
206 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000207 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000208 intake_encoder_.set_index(::std::move(index));
209 }
210
211 // Shoulder setters.
212 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000213 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000214 shoulder_encoder_.set_encoder(::std::move(encoder));
215 }
216
217 void set_shoulder_potentiometer(
218 ::std::unique_ptr<AnalogInput> potentiometer) {
219 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
220 }
221
222 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000223 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000224 shoulder_encoder_.set_index(::std::move(index));
225 }
226
227 // Wrist setters.
228 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000229 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000230 wrist_encoder_.set_encoder(::std::move(encoder));
231 }
232
233 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
234 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
235 }
236
237 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000238 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000239 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000240 }
241
Comran Morshed9a9948c2016-01-16 15:58:04 +0000242 // All of the DMA-related set_* calls must be made before this, and it doesn't
243 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000244
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000245 // TODO(comran): Add 2016 things down below for dma synchronization.
246 void set_dma(::std::unique_ptr<DMA> dma) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247 dma_synchronizer_.reset(
248 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000249 dma_synchronizer_->Add(&intake_encoder_);
250 dma_synchronizer_->Add(&shoulder_encoder_);
251 dma_synchronizer_->Add(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000252 }
253
254 void operator()() {
255 ::aos::SetCurrentThreadName("SensorReader");
256
257 my_pid_ = getpid();
258 ds_ =
259#ifdef WPILIB2015
260 DriverStation::GetInstance();
261#else
262 &DriverStation::GetInstance();
263#endif
264
Comran Morshed9a9948c2016-01-16 15:58:04 +0000265 dma_synchronizer_->Start();
266
267 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
268 ::aos::time::Time::InMS(4));
269
270 ::aos::SetCurrentThreadRealtimePriority(40);
271 while (run_) {
272 {
273 const int iterations = phased_loop.SleepUntilNext();
274 if (iterations != 1) {
275 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
276 }
277 }
278 RunIteration();
279 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000280 }
281
282 void RunIteration() {
283 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
284
285 const auto &values = constants::GetValues();
286
287 {
288 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
289 drivetrain_message->right_encoder =
290 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
291 drivetrain_message->left_encoder =
292 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
293 drivetrain_message->left_speed =
294 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
295 drivetrain_message->right_speed =
296 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
297
Comran Morshed9a9948c2016-01-16 15:58:04 +0000298 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000299 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000300 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000301 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000302
303 drivetrain_message.Send();
304 }
305
Comran Morshed9a9948c2016-01-16 15:58:04 +0000306 dma_synchronizer_->RunIteration();
Comran Morshed225f0b92016-02-10 20:34:27 +0000307
308 {
309 auto shooter_message = shooter_queue.position.MakeMessage();
310 shooter_message->theta_left =
311 shooter_translate(shooter_left_encoder_->GetRaw());
312 shooter_message->theta_right =
313 shooter_translate(shooter_right_encoder_->GetRaw());
314 shooter_message.Send();
315 }
316
317 {
318 auto superstructure_message = superstructure_queue.position.MakeMessage();
319 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
320 intake_translate, intake_pot_translate, false,
321 values.intake.pot_offset);
322 CopyPotAndIndexPosition(shoulder_encoder_,
323 &superstructure_message->shoulder,
324 shoulder_translate, shoulder_pot_translate, false,
325 values.shoulder.pot_offset);
326 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
327 wrist_translate, wrist_pot_translate, false,
328 values.wrist.pot_offset);
329
330 superstructure_message.Send();
331 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000332 }
333
334 void Quit() { run_ = false; }
335
336 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000337 void CopyPotAndIndexPosition(
338 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
339 ::frc971::PotAndIndexPosition *position,
340 ::std::function<double(int32_t)> encoder_translate,
341 ::std::function<double(double)> potentiometer_translate, bool reverse,
342 double pot_offset) {
343 const double multiplier = reverse ? -1.0 : 1.0;
344 position->encoder =
345 multiplier * encoder_translate(encoder.polled_encoder_value());
346 position->pot = multiplier * potentiometer_translate(
347 encoder.polled_potentiometer_voltage()) +
348 pot_offset;
349 position->latched_encoder =
350 multiplier * encoder_translate(encoder.last_encoder_value());
351 position->latched_pot =
352 multiplier *
353 potentiometer_translate(encoder.last_potentiometer_voltage()) +
354 pot_offset;
355 position->index_pulses = encoder.index_posedge_count();
356 }
357
Comran Morshed9a9948c2016-01-16 15:58:04 +0000358 int32_t my_pid_;
359 DriverStation *ds_;
360
361 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
362
Comran Morshed225f0b92016-02-10 20:34:27 +0000363 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
364 drivetrain_right_encoder_;
365 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
366
367 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000368 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
369 shoulder_encoder_, wrist_encoder_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000370
Comran Morshed9a9948c2016-01-16 15:58:04 +0000371 ::std::atomic<bool> run_{true};
Comran Morshed1b764322016-02-14 20:18:12 +0000372 DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_,
373 superstructure_encoder_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000374};
375
376class SolenoidWriter {
377 public:
378 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
379 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000380 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
381 shooter_(".y2016.control_loops.shooter_queue.output") {}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000382
383 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
384 pressure_switch_ = ::std::move(pressure_switch);
385 }
386
387 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
388 compressor_relay_ = ::std::move(compressor_relay);
389 }
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()() {
412 ::aos::SetCurrentThreadName("Solenoids");
413 ::aos::SetCurrentThreadRealtimePriority(27);
414
415 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
416 ::aos::time::Time::InMS(1));
417
418 while (run_) {
419 {
420 const int iterations = phased_loop.SleepUntilNext();
421 if (iterations != 1) {
422 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
423 }
424 }
425
426 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000427 drivetrain_.FetchLatest();
428 if (drivetrain_.get()) {
429 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
430 drivetrain_left_->Set(!drivetrain_->left_high);
431 drivetrain_right_->Set(!drivetrain_->right_high);
432 }
433 }
434
435 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000436 shooter_.FetchLatest();
437 if (shooter_.get()) {
438 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
439 shooter_clamp_->Set(shooter_->clamp_open);
440 shooter_pusher_->Set(shooter_->push_to_shooter);
441 }
442 }
443
444 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000445 ::frc971::wpilib::PneumaticsToLog to_log;
446 {
447 const bool compressor_on = !pressure_switch_->Get();
448 to_log.compressor_on = compressor_on;
449 if (compressor_on) {
450 compressor_relay_->Set(Relay::kForward);
451 } else {
452 compressor_relay_->Set(Relay::kOff);
453 }
454 }
455
456 pcm_->Flush();
457 to_log.read_solenoids = pcm_->GetAll();
458 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
459 }
460 }
461 }
462
463 void Quit() { run_ = false; }
464
465 private:
466 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
467
Comran Morshed225f0b92016-02-10 20:34:27 +0000468 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Comran Morshedb79c4242016-02-06 18:27:26 +0000469 drivetrain_right_, shooter_clamp_, shooter_pusher_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000470 ::std::unique_ptr<DigitalInput> pressure_switch_;
471 ::std::unique_ptr<Relay> compressor_relay_;
472
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);
Comran Morshed225f0b92016-02-10 20:34:27 +0000497 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);
528 shooter_left_talon_->Set(queue->voltage_left / 12.0);
529 shooter_right_talon_->Set(queue->voltage_right / 12.0);
530 }
531
532 virtual void Stop() override {
533 LOG(WARNING, "Shooter output too old.\n");
534 shooter_left_talon_->Disable();
535 shooter_right_talon_->Disable();
536 }
537
538 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
539};
540
541class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
542 public:
543 void set_intake_talon(::std::unique_ptr<Talon> t) {
544 intake_talon_ = ::std::move(t);
545 }
546
547 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
548 shoulder_talon_ = ::std::move(t);
549 }
550
551 void set_wrist_talon(::std::unique_ptr<Talon> t) {
552 wrist_talon_ = ::std::move(t);
553 }
554
Comran Morshedf4cd7642016-02-15 20:40:49 +0000555 void set_rollers_talon(::std::unique_ptr<Talon> t) {
556 rollers_talon_ = ::std::move(t);
557 }
558
Comran Morshed225f0b92016-02-10 20:34:27 +0000559 private:
560 virtual void Read() override {
561 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
562 }
563
564 virtual void Write() override {
565 auto &queue = ::y2016::control_loops::superstructure_queue.output;
566 LOG_STRUCT(DEBUG, "will output", *queue);
567 intake_talon_->Set(queue->voltage_intake / 12.0);
568 shoulder_talon_->Set(-queue->voltage_shoulder / 12.0);
569 wrist_talon_->Set(queue->voltage_wrist / 12.0);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000570 rollers_talon_->Set(queue->voltage_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000571 }
572
573 virtual void Stop() override {
574 LOG(WARNING, "Superstructure output too old.\n");
575 intake_talon_->Disable();
576 shoulder_talon_->Disable();
577 wrist_talon_->Disable();
578 }
579
Comran Morshedf4cd7642016-02-15 20:40:49 +0000580 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
581 rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000582};
583
Comran Morshed9a9948c2016-01-16 15:58:04 +0000584class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
585 public:
586 ::std::unique_ptr<Encoder> make_encoder(int index) {
587 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
588 Encoder::k4X);
589 }
590
591 void Run() override {
592 ::aos::InitNRT();
593 ::aos::SetCurrentThreadName("StartCompetition");
594
595 ::frc971::wpilib::JoystickSender joystick_sender;
596 ::std::thread joystick_thread(::std::ref(joystick_sender));
597
598 ::frc971::wpilib::PDPFetcher pdp_fetcher;
599 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
600 SensorReader reader;
601
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000602 // TODO(constants): Update these input numbers.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000603 reader.set_drivetrain_left_encoder(make_encoder(0));
604 reader.set_drivetrain_right_encoder(make_encoder(1));
Comran Morshed225f0b92016-02-10 20:34:27 +0000605 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(1));
606 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(2));
607
608 reader.set_shooter_left_encoder(make_encoder(0));
609 reader.set_shooter_right_encoder(make_encoder(0));
610
611 reader.set_intake_encoder(make_encoder(0));
612 reader.set_intake_index(make_unique<DigitalInput>(0));
613 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
614
615 reader.set_shoulder_encoder(make_encoder(0));
616 reader.set_shoulder_index(make_unique<DigitalInput>(0));
617 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(0));
618
619 reader.set_wrist_encoder(make_encoder(0));
620 reader.set_wrist_index(make_unique<DigitalInput>(0));
621 reader.set_wrist_potentiometer(make_unique<AnalogInput>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000622
Comran Morshed9a9948c2016-01-16 15:58:04 +0000623 reader.set_dma(make_unique<DMA>());
624 ::std::thread reader_thread(::std::ref(reader));
625
626 ::frc971::wpilib::GyroSender gyro_sender;
627 ::std::thread gyro_thread(::std::ref(gyro_sender));
628
629 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000630 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000631 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000632 drivetrain_writer.set_drivetrain_right_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000633 ::std::unique_ptr<Talon>(new Talon(2)));
634 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
635
Comran Morshed225f0b92016-02-10 20:34:27 +0000636 ShooterWriter shooter_writer;
637 shooter_writer.set_shooter_left_talon(
638 ::std::unique_ptr<Talon>(new Talon(0)));
639 shooter_writer.set_shooter_right_talon(
640 ::std::unique_ptr<Talon>(new Talon(0)));
641 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
642
643 SuperstructureWriter superstructure_writer;
644 superstructure_writer.set_intake_talon(
645 ::std::unique_ptr<Talon>(new Talon(0)));
646 superstructure_writer.set_shoulder_talon(
647 ::std::unique_ptr<Talon>(new Talon(0)));
648 superstructure_writer.set_wrist_talon(
649 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshedf4cd7642016-02-15 20:40:49 +0000650 superstructure_writer.set_rollers_talon(
651 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000652 ::std::thread superstructure_writer_thread(
653 ::std::ref(superstructure_writer));
654
Comran Morshed9a9948c2016-01-16 15:58:04 +0000655 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
656 new ::frc971::wpilib::BufferedPcm());
657 SolenoidWriter solenoid_writer(pcm);
658 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
659 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7));
Comran Morshedb79c4242016-02-06 18:27:26 +0000660 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(6));
661 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(7));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000662
663 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(25));
664 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
665 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
666
667 // Wait forever. Not much else to do...
668 while (true) {
669 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
670 if (r != 0) {
671 PLOG(WARNING, "infinite select failed");
672 } else {
673 PLOG(WARNING, "infinite select succeeded??\n");
674 }
675 }
676
677 LOG(ERROR, "Exiting WPILibRobot\n");
678
679 joystick_sender.Quit();
680 joystick_thread.join();
681 pdp_fetcher.Quit();
682 pdp_fetcher_thread.join();
683 reader.Quit();
684 reader_thread.join();
685 gyro_sender.Quit();
686 gyro_thread.join();
687
688 drivetrain_writer.Quit();
689 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000690 shooter_writer.Quit();
691 shooter_writer_thread.join();
692 superstructure_writer.Quit();
693 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000694 solenoid_writer.Quit();
695 solenoid_thread.join();
696
697 ::aos::Cleanup();
698 }
699};
700
701} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000702} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000703
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000704AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);