blob: c7c493a4a3717ac7044db6e7d1f6a980489a9e28 [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 {
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 *
Austin Schuh9f77fd22016-02-21 02:53:58 -080087 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
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 *
Austin Schuh9f77fd22016-02-21 02:53:58 -080093 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
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) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800102 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000103 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 =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800290 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000291 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 =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800311 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000312 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,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800327 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000328 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"),
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800381 shooter_(".y2016.control_loops.shooter.shooter_queue.output") {}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000382
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800383 void set_compressor(::std::unique_ptr<Compressor> compressor) {
384 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000385 }
386
387 void set_drivetrain_left(
388 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
389 drivetrain_left_ = ::std::move(s);
390 }
391
392 void set_drivetrain_right(
393 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
394 drivetrain_right_ = ::std::move(s);
395 }
396
Comran Morshedb79c4242016-02-06 18:27:26 +0000397 void set_shooter_clamp(
398 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
399 shooter_clamp_ = ::std::move(s);
400 }
401
402 void set_shooter_pusher(
403 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
404 shooter_pusher_ = ::std::move(s);
405 }
406
Comran Morshed9a9948c2016-01-16 15:58:04 +0000407 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800408 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000409 ::aos::SetCurrentThreadName("Solenoids");
410 ::aos::SetCurrentThreadRealtimePriority(27);
411
412 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
413 ::aos::time::Time::InMS(1));
414
415 while (run_) {
416 {
417 const int iterations = phased_loop.SleepUntilNext();
418 if (iterations != 1) {
419 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
420 }
421 }
422
423 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000424 drivetrain_.FetchLatest();
425 if (drivetrain_.get()) {
426 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
427 drivetrain_left_->Set(!drivetrain_->left_high);
428 drivetrain_right_->Set(!drivetrain_->right_high);
429 }
430 }
431
432 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000433 shooter_.FetchLatest();
434 if (shooter_.get()) {
435 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
436 shooter_clamp_->Set(shooter_->clamp_open);
437 shooter_pusher_->Set(shooter_->push_to_shooter);
438 }
439 }
440
441 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000442 ::frc971::wpilib::PneumaticsToLog to_log;
443 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800444 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000445 }
446
447 pcm_->Flush();
448 to_log.read_solenoids = pcm_->GetAll();
449 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
450 }
451 }
452 }
453
454 void Quit() { run_ = false; }
455
456 private:
457 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
458
Comran Morshed225f0b92016-02-10 20:34:27 +0000459 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_,
Comran Morshedb79c4242016-02-06 18:27:26 +0000460 drivetrain_right_, shooter_clamp_, shooter_pusher_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800461 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000462
Comran Morshed9a9948c2016-01-16 15:58:04 +0000463 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000464 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000465
466 ::std::atomic<bool> run_{true};
467};
468
469class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
470 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000471 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
472 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000473 }
474
Comran Morshed225f0b92016-02-10 20:34:27 +0000475 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
476 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000477 }
478
479 private:
480 virtual void Read() override {
481 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
482 }
483
484 virtual void Write() override {
485 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
486 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800487 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
488 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000489 }
490
491 virtual void Stop() override {
492 LOG(WARNING, "drivetrain output too old\n");
Comran Morshed225f0b92016-02-10 20:34:27 +0000493 drivetrain_left_talon_->Disable();
494 drivetrain_right_talon_->Disable();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000495 }
496
Comran Morshed225f0b92016-02-10 20:34:27 +0000497 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
498};
499
500class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
501 public:
502 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
503 shooter_left_talon_ = ::std::move(t);
504 }
505
506 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
507 shooter_right_talon_ = ::std::move(t);
508 }
509
510 private:
511 virtual void Read() override {
512 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
513 }
514
515 virtual void Write() override {
516 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
517 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800518
Comran Morshed225f0b92016-02-10 20:34:27 +0000519 shooter_left_talon_->Set(queue->voltage_left / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800520 shooter_right_talon_->Set(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000521 }
522
523 virtual void Stop() override {
524 LOG(WARNING, "Shooter output too old.\n");
525 shooter_left_talon_->Disable();
526 shooter_right_talon_->Disable();
527 }
528
529 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
530};
531
532class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
533 public:
534 void set_intake_talon(::std::unique_ptr<Talon> t) {
535 intake_talon_ = ::std::move(t);
536 }
537
538 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
539 shoulder_talon_ = ::std::move(t);
540 }
541
542 void set_wrist_talon(::std::unique_ptr<Talon> t) {
543 wrist_talon_ = ::std::move(t);
544 }
545
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800546 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
547 top_rollers_talon_ = ::std::move(t);
548 }
549
550 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
551 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000552 }
553
Comran Morshed225f0b92016-02-10 20:34:27 +0000554 private:
555 virtual void Read() override {
556 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
557 }
558
559 virtual void Write() override {
560 auto &queue = ::y2016::control_loops::superstructure_queue.output;
561 LOG_STRUCT(DEBUG, "will output", *queue);
562 intake_talon_->Set(queue->voltage_intake / 12.0);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800563 shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder, -6.0, 6.0) / 12.0);
564 wrist_talon_->Set(::aos::Clip(queue->voltage_wrist, -6.0, 6.0) / 12.0);
565 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
566 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000567 }
568
569 virtual void Stop() override {
570 LOG(WARNING, "Superstructure output too old.\n");
571 intake_talon_->Disable();
572 shoulder_talon_->Disable();
573 wrist_talon_->Disable();
574 }
575
Comran Morshedf4cd7642016-02-15 20:40:49 +0000576 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800577 top_rollers_talon_, bottom_rollers_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000578};
579
Comran Morshed9a9948c2016-01-16 15:58:04 +0000580class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
581 public:
582 ::std::unique_ptr<Encoder> make_encoder(int index) {
583 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
584 Encoder::k4X);
585 }
586
587 void Run() override {
588 ::aos::InitNRT();
589 ::aos::SetCurrentThreadName("StartCompetition");
590
591 ::frc971::wpilib::JoystickSender joystick_sender;
592 ::std::thread joystick_thread(::std::ref(joystick_sender));
593
594 ::frc971::wpilib::PDPFetcher pdp_fetcher;
595 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
596 SensorReader reader;
597
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000598 // TODO(constants): Update these input numbers.
Austin Schuh9f77fd22016-02-21 02:53:58 -0800599 reader.set_drivetrain_left_encoder(make_encoder(5));
600 reader.set_drivetrain_right_encoder(make_encoder(6));
601 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
602 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000603
Austin Schuh9f77fd22016-02-21 02:53:58 -0800604 reader.set_shooter_left_encoder(make_encoder(3));
605 reader.set_shooter_right_encoder(make_encoder(4));
Comran Morshed225f0b92016-02-10 20:34:27 +0000606
607 reader.set_intake_encoder(make_encoder(0));
608 reader.set_intake_index(make_unique<DigitalInput>(0));
609 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
610
Austin Schuh9f77fd22016-02-21 02:53:58 -0800611 reader.set_shoulder_encoder(make_encoder(2));
612 reader.set_shoulder_index(make_unique<DigitalInput>(2));
613 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000614
Austin Schuh9f77fd22016-02-21 02:53:58 -0800615 reader.set_wrist_encoder(make_encoder(1));
616 reader.set_wrist_index(make_unique<DigitalInput>(1));
617 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000618
Comran Morshed9a9948c2016-01-16 15:58:04 +0000619 reader.set_dma(make_unique<DMA>());
620 ::std::thread reader_thread(::std::ref(reader));
621
622 ::frc971::wpilib::GyroSender gyro_sender;
623 ::std::thread gyro_thread(::std::ref(gyro_sender));
624
625 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000626 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000627 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000628 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800629 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000630 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
631
Comran Morshed225f0b92016-02-10 20:34:27 +0000632 ShooterWriter shooter_writer;
633 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800634 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000635 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800636 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000637 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
638
639 SuperstructureWriter superstructure_writer;
640 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800641 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000642 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800643 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000644 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800645 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800646 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800647 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800648 superstructure_writer.set_bottom_rollers_talon(
649 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000650 ::std::thread superstructure_writer_thread(
651 ::std::ref(superstructure_writer));
652
Comran Morshed9a9948c2016-01-16 15:58:04 +0000653 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
654 new ::frc971::wpilib::BufferedPcm());
655 SolenoidWriter solenoid_writer(pcm);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800656 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1));
657 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0));
658 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
659 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000660
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800661 solenoid_writer.set_compressor(make_unique<Compressor>());
662
Comran Morshed9a9948c2016-01-16 15:58:04 +0000663 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
664
665 // Wait forever. Not much else to do...
666 while (true) {
667 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
668 if (r != 0) {
669 PLOG(WARNING, "infinite select failed");
670 } else {
671 PLOG(WARNING, "infinite select succeeded??\n");
672 }
673 }
674
675 LOG(ERROR, "Exiting WPILibRobot\n");
676
677 joystick_sender.Quit();
678 joystick_thread.join();
679 pdp_fetcher.Quit();
680 pdp_fetcher_thread.join();
681 reader.Quit();
682 reader_thread.join();
683 gyro_sender.Quit();
684 gyro_thread.join();
685
686 drivetrain_writer.Quit();
687 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000688 shooter_writer.Quit();
689 shooter_writer_thread.join();
690 superstructure_writer.Quit();
691 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000692 solenoid_writer.Quit();
693 solenoid_thread.join();
694
695 ::aos::Cleanup();
696 }
697};
698
699} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000700} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000701
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000702AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);