blob: 18989519f38ff23074095696c8cfc7c7719079e7 [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"
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 {
Austin Schuha9992ff2016-02-28 21:59:23 -080063namespace {
64constexpr double kMaxBringupPower = 12.0;
65} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000066
67// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
68// DMA stuff and then removing the * 2.0 in *_translate.
69// The low bit is direction.
70
71// TODO(brian): Replace this with ::std::make_unique once all our toolchains
72// have support.
73template <class T, class... U>
74std::unique_ptr<T> make_unique(U &&... u) {
75 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
76}
77
Comran Morshed225f0b92016-02-10 20:34:27 +000078// Translates for the sensor values to convert raw index pulses into something
79// with proper units.
80
81// TODO(comran): Template these methods since there is a lot of repetition here.
82double hall_translate(double in) {
83 // Turn voltage from our 3-state halls into a ratio that the loop can use.
84 return in / 5.0;
85}
86
Comran Morshed9a9948c2016-01-16 15:58:04 +000087double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000088 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000089 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080090 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000091}
92
93double drivetrain_velocity_translate(double in) {
94 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000095 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080096 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000097}
98
Comran Morshed225f0b92016-02-10 20:34:27 +000099double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +0000100 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000101 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
102}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000103
Comran Morshed225f0b92016-02-10 20:34:27 +0000104double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800105 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000106 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
107}
108
109double shoulder_translate(int32_t in) {
110 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
111 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
112}
113
114double wrist_translate(int32_t in) {
115 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
116 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
117}
118
119double intake_pot_translate(double voltage) {
120 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000121 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000122}
123
124double shoulder_pot_translate(double voltage) {
125 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000126 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000127}
128
129double wrist_pot_translate(double voltage) {
130 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000131 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000132}
133
Comran Morshed1b764322016-02-14 20:18:12 +0000134constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
135 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
136 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
137 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
138
139constexpr double kMaxShooterEncoderPulsesPerSecond =
140 18700.0 /* 775pro free speed RPM */ * 12.0 /
141 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
142 128.0 /* CPR */ * 4 /* edges per cycle */;
143
144double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
145 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
146
147constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
148 18700.0 /* 775pro free speed RPM */ * 12.0 /
149 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
150 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000151
Comran Morshed225f0b92016-02-10 20:34:27 +0000152// Class to send position messages with sensor readings to our loops.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000153class SensorReader {
154 public:
155 SensorReader() {
156 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
157 // we should ever see.
Comran Morshed1b764322016-02-14 20:18:12 +0000158 drivetrain_shooter_encoder_filter_.SetPeriodNanoSeconds(
159 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
160 kMaxDrivetrainShooterEncoderPulsesPerSecond * 1e9 +
161 0.5));
162 superstructure_encoder_filter_.SetPeriodNanoSeconds(
163 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
164 kMaxSuperstructureEncoderPulsesPerSecond * 1e9 +
165 0.5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000166 hall_filter_.SetPeriodNanoSeconds(100000);
167 }
168
Comran Morshed225f0b92016-02-10 20:34:27 +0000169 // Drivetrain setters.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000170 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000171 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000172 drivetrain_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173 }
174
175 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000176 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000177 drivetrain_right_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000178 }
179
Comran Morshed225f0b92016-02-10 20:34:27 +0000180 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
181 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000182 }
183
Comran Morshed225f0b92016-02-10 20:34:27 +0000184 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
185 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000186 }
187
Comran Morshed225f0b92016-02-10 20:34:27 +0000188 // Shooter setters.
189 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000190 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000191 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000192 }
193
Comran Morshed225f0b92016-02-10 20:34:27 +0000194 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000195 drivetrain_shooter_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000196 shooter_right_encoder_ = ::std::move(encoder);
197 }
198
199 // Intake setters.
200 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000201 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000202 intake_encoder_.set_encoder(::std::move(encoder));
203 }
204
205 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
206 intake_encoder_.set_potentiometer(::std::move(potentiometer));
207 }
208
209 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000210 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000211 intake_encoder_.set_index(::std::move(index));
212 }
213
214 // Shoulder setters.
215 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000216 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000217 shoulder_encoder_.set_encoder(::std::move(encoder));
218 }
219
220 void set_shoulder_potentiometer(
221 ::std::unique_ptr<AnalogInput> potentiometer) {
222 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
223 }
224
225 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000226 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000227 shoulder_encoder_.set_index(::std::move(index));
228 }
229
230 // Wrist setters.
231 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Comran Morshed1b764322016-02-14 20:18:12 +0000232 superstructure_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000233 wrist_encoder_.set_encoder(::std::move(encoder));
234 }
235
236 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
237 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
238 }
239
240 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Comran Morshed1b764322016-02-14 20:18:12 +0000241 superstructure_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000242 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000243 }
244
Comran Morshed9a9948c2016-01-16 15:58:04 +0000245 // All of the DMA-related set_* calls must be made before this, and it doesn't
246 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000248 // TODO(comran): Add 2016 things down below for dma synchronization.
249 void set_dma(::std::unique_ptr<DMA> dma) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000250 dma_synchronizer_.reset(
251 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000252 dma_synchronizer_->Add(&intake_encoder_);
253 dma_synchronizer_->Add(&shoulder_encoder_);
254 dma_synchronizer_->Add(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000255 }
256
257 void operator()() {
258 ::aos::SetCurrentThreadName("SensorReader");
259
260 my_pid_ = getpid();
261 ds_ =
262#ifdef WPILIB2015
263 DriverStation::GetInstance();
264#else
265 &DriverStation::GetInstance();
266#endif
267
Comran Morshed9a9948c2016-01-16 15:58:04 +0000268 dma_synchronizer_->Start();
269
270 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
271 ::aos::time::Time::InMS(4));
272
273 ::aos::SetCurrentThreadRealtimePriority(40);
274 while (run_) {
275 {
276 const int iterations = phased_loop.SleepUntilNext();
277 if (iterations != 1) {
278 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
279 }
280 }
281 RunIteration();
282 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000283 }
284
285 void RunIteration() {
286 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
287
288 const auto &values = constants::GetValues();
289
290 {
291 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
292 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800293 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000294 drivetrain_message->left_encoder =
295 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
296 drivetrain_message->left_speed =
297 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
298 drivetrain_message->right_speed =
299 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
300
Comran Morshed9a9948c2016-01-16 15:58:04 +0000301 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000302 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000303 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000304 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000305
306 drivetrain_message.Send();
307 }
308
Comran Morshed9a9948c2016-01-16 15:58:04 +0000309 dma_synchronizer_->RunIteration();
Comran Morshed225f0b92016-02-10 20:34:27 +0000310
311 {
312 auto shooter_message = shooter_queue.position.MakeMessage();
313 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800314 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000315 shooter_message->theta_right =
316 shooter_translate(shooter_right_encoder_->GetRaw());
317 shooter_message.Send();
318 }
319
320 {
321 auto superstructure_message = superstructure_queue.position.MakeMessage();
322 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
323 intake_translate, intake_pot_translate, false,
324 values.intake.pot_offset);
325 CopyPotAndIndexPosition(shoulder_encoder_,
326 &superstructure_message->shoulder,
327 shoulder_translate, shoulder_pot_translate, false,
328 values.shoulder.pot_offset);
329 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800330 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000331 values.wrist.pot_offset);
332
333 superstructure_message.Send();
334 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000335 }
336
337 void Quit() { run_ = false; }
338
339 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000340 void CopyPotAndIndexPosition(
341 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
342 ::frc971::PotAndIndexPosition *position,
343 ::std::function<double(int32_t)> encoder_translate,
344 ::std::function<double(double)> potentiometer_translate, bool reverse,
345 double pot_offset) {
346 const double multiplier = reverse ? -1.0 : 1.0;
347 position->encoder =
348 multiplier * encoder_translate(encoder.polled_encoder_value());
349 position->pot = multiplier * potentiometer_translate(
350 encoder.polled_potentiometer_voltage()) +
351 pot_offset;
352 position->latched_encoder =
353 multiplier * encoder_translate(encoder.last_encoder_value());
354 position->latched_pot =
355 multiplier *
356 potentiometer_translate(encoder.last_potentiometer_voltage()) +
357 pot_offset;
358 position->index_pulses = encoder.index_posedge_count();
359 }
360
Comran Morshed9a9948c2016-01-16 15:58:04 +0000361 int32_t my_pid_;
362 DriverStation *ds_;
363
364 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
365
Comran Morshed225f0b92016-02-10 20:34:27 +0000366 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
367 drivetrain_right_encoder_;
368 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
369
370 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000371 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
372 shoulder_encoder_, wrist_encoder_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000373
Comran Morshed9a9948c2016-01-16 15:58:04 +0000374 ::std::atomic<bool> run_{true};
Comran Morshed1b764322016-02-14 20:18:12 +0000375 DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_,
376 superstructure_encoder_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000377};
378
379class SolenoidWriter {
380 public:
381 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
382 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000383 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800384 shooter_(".y2016.control_loops.shooter.shooter_queue.output") {}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000385
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800386 void set_compressor(::std::unique_ptr<Compressor> compressor) {
387 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000388 }
389
390 void set_drivetrain_left(
391 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
392 drivetrain_left_ = ::std::move(s);
393 }
394
395 void set_drivetrain_right(
396 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
397 drivetrain_right_ = ::std::move(s);
398 }
399
Comran Morshedb79c4242016-02-06 18:27:26 +0000400 void set_shooter_clamp(
401 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
402 shooter_clamp_ = ::std::move(s);
403 }
404
405 void set_shooter_pusher(
406 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
407 shooter_pusher_ = ::std::move(s);
408 }
409
Comran Morshed9a9948c2016-01-16 15:58:04 +0000410 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800411 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000412 ::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 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800447 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000448 }
449
450 pcm_->Flush();
451 to_log.read_solenoids = pcm_->GetAll();
452 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
453 }
454 }
455 }
456
457 void Quit() { run_ = false; }
458
459 private:
460 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
461
Comran Morshed225f0b92016-02-10 20:34:27 +0000462 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Comran Morshedb79c4242016-02-06 18:27:26 +0000463 drivetrain_right_, shooter_clamp_, shooter_pusher_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800464 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000465
Comran Morshed9a9948c2016-01-16 15:58:04 +0000466 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000467 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000468
469 ::std::atomic<bool> run_{true};
470};
471
472class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
473 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000474 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
475 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000476 }
477
Comran Morshed225f0b92016-02-10 20:34:27 +0000478 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
479 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000480 }
481
482 private:
483 virtual void Read() override {
484 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
485 }
486
487 virtual void Write() override {
488 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
489 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800490 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
491 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000492 }
493
494 virtual void Stop() override {
495 LOG(WARNING, "drivetrain output too old\n");
Comran Morshed225f0b92016-02-10 20:34:27 +0000496 drivetrain_left_talon_->Disable();
497 drivetrain_right_talon_->Disable();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000498 }
499
Comran Morshed225f0b92016-02-10 20:34:27 +0000500 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
501};
502
503class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
504 public:
505 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
506 shooter_left_talon_ = ::std::move(t);
507 }
508
509 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
510 shooter_right_talon_ = ::std::move(t);
511 }
512
513 private:
514 virtual void Read() override {
515 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
516 }
517
518 virtual void Write() override {
519 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
520 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800521
Comran Morshed225f0b92016-02-10 20:34:27 +0000522 shooter_left_talon_->Set(queue->voltage_left / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800523 shooter_right_talon_->Set(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000524 }
525
526 virtual void Stop() override {
527 LOG(WARNING, "Shooter output too old.\n");
528 shooter_left_talon_->Disable();
529 shooter_right_talon_->Disable();
530 }
531
532 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
533};
534
535class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
536 public:
537 void set_intake_talon(::std::unique_ptr<Talon> t) {
538 intake_talon_ = ::std::move(t);
539 }
540
541 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
542 shoulder_talon_ = ::std::move(t);
543 }
544
545 void set_wrist_talon(::std::unique_ptr<Talon> t) {
546 wrist_talon_ = ::std::move(t);
547 }
548
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800549 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
550 top_rollers_talon_ = ::std::move(t);
551 }
552
553 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
554 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000555 }
556
Comran Morshed225f0b92016-02-10 20:34:27 +0000557 private:
558 virtual void Read() override {
559 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
560 }
561
562 virtual void Write() override {
563 auto &queue = ::y2016::control_loops::superstructure_queue.output;
564 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha9992ff2016-02-28 21:59:23 -0800565 intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower,
566 kMaxBringupPower) /
567 12.0);
568 shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder,
569 -kMaxBringupPower, kMaxBringupPower) /
570 12.0);
571 wrist_talon_->Set(
572 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
573 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800574 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
575 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000576 }
577
578 virtual void Stop() override {
579 LOG(WARNING, "Superstructure output too old.\n");
580 intake_talon_->Disable();
581 shoulder_talon_->Disable();
582 wrist_talon_->Disable();
583 }
584
Comran Morshedf4cd7642016-02-15 20:40:49 +0000585 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800586 top_rollers_talon_, bottom_rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000587};
588
Comran Morshed9a9948c2016-01-16 15:58:04 +0000589class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
590 public:
591 ::std::unique_ptr<Encoder> make_encoder(int index) {
592 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
593 Encoder::k4X);
594 }
595
596 void Run() override {
597 ::aos::InitNRT();
598 ::aos::SetCurrentThreadName("StartCompetition");
599
600 ::frc971::wpilib::JoystickSender joystick_sender;
601 ::std::thread joystick_thread(::std::ref(joystick_sender));
602
603 ::frc971::wpilib::PDPFetcher pdp_fetcher;
604 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
605 SensorReader reader;
606
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000607 // TODO(constants): Update these input numbers.
Austin Schuh9f77fd22016-02-21 02:53:58 -0800608 reader.set_drivetrain_left_encoder(make_encoder(5));
609 reader.set_drivetrain_right_encoder(make_encoder(6));
610 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
611 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000612
Austin Schuh9f77fd22016-02-21 02:53:58 -0800613 reader.set_shooter_left_encoder(make_encoder(3));
614 reader.set_shooter_right_encoder(make_encoder(4));
Comran Morshed225f0b92016-02-10 20:34:27 +0000615
616 reader.set_intake_encoder(make_encoder(0));
617 reader.set_intake_index(make_unique<DigitalInput>(0));
618 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
619
Austin Schuh9f77fd22016-02-21 02:53:58 -0800620 reader.set_shoulder_encoder(make_encoder(2));
621 reader.set_shoulder_index(make_unique<DigitalInput>(2));
622 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000623
Austin Schuh9f77fd22016-02-21 02:53:58 -0800624 reader.set_wrist_encoder(make_encoder(1));
625 reader.set_wrist_index(make_unique<DigitalInput>(1));
626 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000627
Comran Morshed9a9948c2016-01-16 15:58:04 +0000628 reader.set_dma(make_unique<DMA>());
629 ::std::thread reader_thread(::std::ref(reader));
630
631 ::frc971::wpilib::GyroSender gyro_sender;
632 ::std::thread gyro_thread(::std::ref(gyro_sender));
633
634 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000635 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000636 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000637 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800638 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000639 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
640
Comran Morshed225f0b92016-02-10 20:34:27 +0000641 ShooterWriter shooter_writer;
642 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800643 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000644 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800645 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000646 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
647
648 SuperstructureWriter superstructure_writer;
649 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800650 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000651 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800652 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000653 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800654 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800655 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800656 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800657 superstructure_writer.set_bottom_rollers_talon(
658 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000659 ::std::thread superstructure_writer_thread(
660 ::std::ref(superstructure_writer));
661
Comran Morshed9a9948c2016-01-16 15:58:04 +0000662 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
663 new ::frc971::wpilib::BufferedPcm());
664 SolenoidWriter solenoid_writer(pcm);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800665 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1));
666 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0));
667 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
668 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000669
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800670 solenoid_writer.set_compressor(make_unique<Compressor>());
671
Comran Morshed9a9948c2016-01-16 15:58:04 +0000672 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
673
674 // Wait forever. Not much else to do...
675 while (true) {
676 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
677 if (r != 0) {
678 PLOG(WARNING, "infinite select failed");
679 } else {
680 PLOG(WARNING, "infinite select succeeded??\n");
681 }
682 }
683
684 LOG(ERROR, "Exiting WPILibRobot\n");
685
686 joystick_sender.Quit();
687 joystick_thread.join();
688 pdp_fetcher.Quit();
689 pdp_fetcher_thread.join();
690 reader.Quit();
691 reader_thread.join();
692 gyro_sender.Quit();
693 gyro_thread.join();
694
695 drivetrain_writer.Quit();
696 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000697 shooter_writer.Quit();
698 shooter_writer_thread.join();
699 superstructure_writer.Quit();
700 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000701 solenoid_writer.Quit();
702 solenoid_thread.join();
703
704 ::aos::Cleanup();
705 }
706};
707
708} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000709} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000710
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000711AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);