blob: ad75401b2e81fe616a6c8a1b22be0ab558b3b5ca [file] [log] [blame]
Adam Snaider18f44172016-10-22 15:30:21 -07001#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#include <array>
10
11#include "Encoder.h"
12#include "Talon.h"
13#include "Relay.h"
14#include "DriverStation.h"
15#include "AnalogInput.h"
16#include "Compressor.h"
17#include "frc971/wpilib/wpilib_robot_base.h"
18#include "DigitalGlitchFilter.h"
19#undef ERROR
20
21#include "aos/common/logging/logging.h"
22#include "aos/common/logging/queue_logging.h"
23#include "aos/common/time.h"
24#include "aos/common/util/log_interval.h"
25#include "aos/common/util/phased_loop.h"
26#include "aos/common/util/wrapping_counter.h"
27#include "aos/common/stl_mutex.h"
28#include "aos/linux_code/init.h"
29#include "aos/common/messages/robot_state.q.h"
30#include "aos/common/commonmath.h"
31
32#include "frc971/control_loops/control_loops.q.h"
33#include "frc971/control_loops/drivetrain/drivetrain.q.h"
34#include "y2016_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
35#include "y2016_bot3/control_loops/intake/intake.q.h"
36#include "y2016_bot3/queues/ball_detector.q.h"
37#include "y2016_bot3/actors/autonomous_action.q.h"
38
39#include "frc971/wpilib/joystick_sender.h"
40#include "frc971/wpilib/loop_output_handler.h"
41#include "frc971/wpilib/buffered_solenoid.h"
42#include "frc971/wpilib/buffered_pcm.h"
43#include "frc971/wpilib/gyro_sender.h"
44#include "frc971/wpilib/dma_edge_counting.h"
45#include "frc971/wpilib/interrupt_edge_counting.h"
46#include "frc971/wpilib/encoder_and_potentiometer.h"
47#include "frc971/wpilib/logging.q.h"
48#include "frc971/wpilib/wpilib_interface.h"
49#include "frc971/wpilib/pdp_fetcher.h"
50#include "frc971/wpilib/dma.h"
51
52#include "y2016_bot3/control_loops/intake/intake.h"
53#include "y2016_bot3/control_loops/drivetrain/drivetrain_base.h"
54
55#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
59using ::frc971::control_loops::drivetrain_queue;
60using ::y2016_bot3::control_loops::intake_queue;
61
62namespace y2016_bot3 {
63namespace constants {
64IntakeZero intake_zero;
65}
66namespace wpilib {
67namespace {
68constexpr double kMaxBringupPower = 12.0;
69} // namespace
70
71// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
72// DMA stuff and then removing the * 2.0 in *_translate.
73// The low bit is direction.
74
75// TODO(brian): Replace this with ::std::make_unique once all our toolchains
76// have support.
77template <class T, class... U>
78std::unique_ptr<T> make_unique(U &&... u) {
79 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
80}
81
82// TODO(Campbell): Update values
83// Translates for the sensor values to convert raw index pulses into something
84// with proper units.
85
86double drivetrain_translate(int32_t in) {
87 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
88 ::y2016_bot3::constants::kDrivetrainEncoderRatio *
89 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
90}
91
92double drivetrain_velocity_translate(double in) {
93 return (1.0 / in) / 256.0 /*cpr*/ *
94 ::y2016_bot3::constants::kDrivetrainEncoderRatio *
95 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
96}
97
98double intake_translate(int32_t in) {
99 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
100 ::y2016_bot3::constants::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
101}
102
103double intake_pot_translate(double voltage) {
104 return voltage * ::y2016_bot3::constants::kIntakePotRatio *
105 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
106}
107
108constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
109 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
110 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
111 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
112
113// Class to send position messages with sensor readings to our loops.
114class SensorReader {
115 public:
116 SensorReader() {
117 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
118 // we should ever see.
119 drivetrain_encoder_filter_.SetPeriodNanoSeconds(
120 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
121 kMaxDrivetrainEncoderPulsesPerSecond * 1e9 +
122 0.5));
123 }
124
125 // Drivetrain setters.
126 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
127 drivetrain_encoder_filter_.Add(encoder.get());
128 drivetrain_left_encoder_ = ::std::move(encoder);
129 }
130
131 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
132 drivetrain_encoder_filter_.Add(encoder.get());
133 drivetrain_right_encoder_ = ::std::move(encoder);
134 }
135
136 // Intake setters.
137 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
138 intake_encoder_filter_.Add(encoder.get());
139 intake_encoder_.set_encoder(::std::move(encoder));
140 }
141
142 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
143 intake_encoder_.set_potentiometer(::std::move(potentiometer));
144 }
145
146 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
147 intake_encoder_filter_.Add(index.get());
148 intake_encoder_.set_index(::std::move(index));
149 }
150
151 // Ball detector setter.
152 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
153 ball_detector_ = ::std::move(analog);
154 }
155
156 // All of the DMA-related set_* calls must be made before this, and it doesn't
157 // hurt to do all of them.
158
159 void set_dma(::std::unique_ptr<DMA> dma) {
160 dma_synchronizer_.reset(
161 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
162 dma_synchronizer_->Add(&intake_encoder_);
163 }
164
165 void operator()() {
166 ::aos::SetCurrentThreadName("SensorReader");
167
168 my_pid_ = getpid();
169 ds_ = &DriverStation::GetInstance();
170
171 dma_synchronizer_->Start();
172
Campbell Crowleyabe04c42016-10-23 14:42:27 -0700173 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
174 ::std::chrono::milliseconds(4));
Adam Snaider18f44172016-10-22 15:30:21 -0700175
176 ::aos::SetCurrentThreadRealtimePriority(40);
177 while (run_) {
178 {
179 const int iterations = phased_loop.SleepUntilNext();
180 if (iterations != 1) {
181 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
182 }
183 }
184 RunIteration();
185 }
186 }
187
188 void RunIteration() {
189 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
190
191 const auto intake_pot_offset =
192 y2016_bot3::constants::intake_zero.pot_offset;
193
194 {
195 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
196 drivetrain_message->right_encoder =
197 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
198 drivetrain_message->left_encoder =
199 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
200 drivetrain_message->left_speed =
201 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
202 drivetrain_message->right_speed =
203 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
204
205 drivetrain_message.Send();
206 }
207
208 dma_synchronizer_->RunIteration();
209
210 {
211 auto intake_message = intake_queue.position.MakeMessage();
212 CopyPotAndIndexPosition(intake_encoder_, &intake_message->intake,
213 intake_translate, intake_pot_translate, false,
214 intake_pot_offset);
215
216 intake_message.Send();
217 }
218
219 {
220 auto ball_detector_message =
221 ::y2016_bot3::sensors::ball_detector.MakeMessage();
222 ball_detector_message->voltage = ball_detector_->GetVoltage();
223 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
224 ball_detector_message.Send();
225 }
226
227 {
228 auto auto_mode_message = ::y2016_bot3::actors::auto_mode.MakeMessage();
229 auto_mode_message->mode = 0;
230 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
231 auto_mode_message.Send();
232 }
233 }
234
235 void Quit() { run_ = false; }
236
237 private:
238 void CopyPotAndIndexPosition(
239 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
240 ::frc971::PotAndIndexPosition *position,
241 ::std::function<double(int32_t)> encoder_translate,
242 ::std::function<double(double)> potentiometer_translate, bool reverse,
243 double pot_offset) {
244 const double multiplier = reverse ? -1.0 : 1.0;
245 position->encoder =
246 multiplier * encoder_translate(encoder.polled_encoder_value());
247 position->pot = multiplier * potentiometer_translate(
248 encoder.polled_potentiometer_voltage()) +
249 pot_offset;
250 position->latched_encoder =
251 multiplier * encoder_translate(encoder.last_encoder_value());
252 position->latched_pot =
253 multiplier *
254 potentiometer_translate(encoder.last_potentiometer_voltage()) +
255 pot_offset;
256 position->index_pulses = encoder.index_posedge_count();
257 }
258
259 int32_t my_pid_;
260 DriverStation *ds_;
261
262 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
263
264 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
265 drivetrain_right_encoder_;
266
267 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_;
268 ::std::unique_ptr<AnalogInput> ball_detector_;
269
270 ::std::atomic<bool> run_{true};
271 DigitalGlitchFilter drivetrain_encoder_filter_, intake_encoder_filter_;
272};
273
274class SolenoidWriter {
275 public:
276 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
277 : pcm_(pcm),
278 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
279 intake_(".y2016_bot3.control_loops.intake_queue.output") {}
280
281 void set_compressor(::std::unique_ptr<Compressor> compressor) {
282 compressor_ = ::std::move(compressor);
283 }
284
285 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
286 traverse_ = ::std::move(s);
287 }
288
289 void set_traverse_latch(
290 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
291 traverse_latch_ = ::std::move(s);
292 }
293
294 void operator()() {
295 compressor_->Start();
296 ::aos::SetCurrentThreadName("Solenoids");
297 ::aos::SetCurrentThreadRealtimePriority(27);
298
Campbell Crowleyabe04c42016-10-23 14:42:27 -0700299 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
300 ::std::chrono::milliseconds(1));
Adam Snaider18f44172016-10-22 15:30:21 -0700301
302 while (run_) {
303 {
304 const int iterations = phased_loop.SleepUntilNext();
305 if (iterations != 1) {
306 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
307 }
308 }
309
310 {
311 intake_.FetchLatest();
312 if (intake_.get()) {
313 LOG_STRUCT(DEBUG, "solenoids", *intake_);
314
315 traverse_->Set(intake_->traverse_down);
316 traverse_latch_->Set(intake_->traverse_unlatched);
317 }
318 }
319
320 {
321 ::frc971::wpilib::PneumaticsToLog to_log;
322 { to_log.compressor_on = compressor_->Enabled(); }
323
324 pcm_->Flush();
325 to_log.read_solenoids = pcm_->GetAll();
326 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
327 }
328 }
329 }
330
331 void Quit() { run_ = false; }
332
333 private:
334 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
335
336 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> traverse_,
337 traverse_latch_;
338 ::std::unique_ptr<Compressor> compressor_;
339
340 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
341 ::aos::Queue<::y2016_bot3::control_loops::IntakeQueue::Output> intake_;
342
343 ::std::atomic<bool> run_{true};
344};
345
346class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
347 public:
348 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
349 drivetrain_left_talon_ = ::std::move(t);
350 }
351
352 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
353 drivetrain_right_talon_ = ::std::move(t);
354 }
355
356 private:
357 virtual void Read() override {
358 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
359 }
360
361 virtual void Write() override {
362 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
363 LOG_STRUCT(DEBUG, "will output", *queue);
364 drivetrain_left_talon_->Set(queue->left_voltage / 12.0);
365 drivetrain_right_talon_->Set(-queue->right_voltage / 12.0);
366 }
367
368 virtual void Stop() override {
369 LOG(WARNING, "drivetrain output too old\n");
370 drivetrain_left_talon_->Disable();
371 drivetrain_right_talon_->Disable();
372 }
373
374 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
375};
376
377class IntakeWriter : public ::frc971::wpilib::LoopOutputHandler {
378 public:
379 void set_intake_talon(::std::unique_ptr<Talon> t) {
380 intake_talon_ = ::std::move(t);
381 }
382
383 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
384 top_rollers_talon_ = ::std::move(t);
385 }
386
387 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
388 bottom_rollers_talon_ = ::std::move(t);
389 }
390
391 private:
392 virtual void Read() override {
393 ::y2016_bot3::control_loops::intake_queue.output.FetchAnother();
394 }
395
396 virtual void Write() override {
397 auto &queue = ::y2016_bot3::control_loops::intake_queue.output;
398 LOG_STRUCT(DEBUG, "will output", *queue);
399 intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower,
400 kMaxBringupPower) /
401 12.0);
402 top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0);
403 bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0);
404 }
405
406 virtual void Stop() override {
407 LOG(WARNING, "Intake output too old.\n");
408 intake_talon_->Disable();
409 }
410
411 ::std::unique_ptr<Talon> intake_talon_, top_rollers_talon_,
412 bottom_rollers_talon_;
413};
414
415class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
416 public:
417 ::std::unique_ptr<Encoder> make_encoder(int index) {
418 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
419 Encoder::k4X);
420 }
421
422 void Run() override {
423 ::aos::InitNRT();
424 ::aos::SetCurrentThreadName("StartCompetition");
425
426 ::frc971::wpilib::JoystickSender joystick_sender;
427 ::std::thread joystick_thread(::std::ref(joystick_sender));
428
429 ::frc971::wpilib::PDPFetcher pdp_fetcher;
430 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
431 SensorReader reader;
432
433 reader.set_drivetrain_left_encoder(make_encoder(5));
434 reader.set_drivetrain_right_encoder(make_encoder(6));
435
436 reader.set_intake_encoder(make_encoder(0));
437 reader.set_intake_index(make_unique<DigitalInput>(0));
438 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
439
440 reader.set_ball_detector(make_unique<AnalogInput>(7));
441
442 reader.set_dma(make_unique<DMA>());
443 ::std::thread reader_thread(::std::ref(reader));
444
445 ::frc971::wpilib::GyroSender gyro_sender;
446 ::std::thread gyro_thread(::std::ref(gyro_sender));
447
448 DrivetrainWriter drivetrain_writer;
449 drivetrain_writer.set_drivetrain_left_talon(
450 ::std::unique_ptr<Talon>(new Talon(5)));
451 drivetrain_writer.set_drivetrain_right_talon(
452 ::std::unique_ptr<Talon>(new Talon(4)));
453 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
454
455 IntakeWriter intake_writer;
456 intake_writer.set_intake_talon(::std::unique_ptr<Talon>(new Talon(3)));
457 intake_writer.set_top_rollers_talon(::std::unique_ptr<Talon>(new Talon(1)));
458 intake_writer.set_bottom_rollers_talon(
459 ::std::unique_ptr<Talon>(new Talon(0)));
460 ::std::thread intake_writer_thread(::std::ref(intake_writer));
461
462 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
463 new ::frc971::wpilib::BufferedPcm());
464 SolenoidWriter solenoid_writer(pcm);
465 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
466 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
467
468 solenoid_writer.set_compressor(make_unique<Compressor>());
469
470 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
471
472 // Wait forever. Not much else to do...
473 while (true) {
474 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
475 if (r != 0) {
476 PLOG(WARNING, "infinite select failed");
477 } else {
478 PLOG(WARNING, "infinite select succeeded??\n");
479 }
480 }
481
482 LOG(ERROR, "Exiting WPILibRobot\n");
483
484 joystick_sender.Quit();
485 joystick_thread.join();
486 pdp_fetcher.Quit();
487 pdp_fetcher_thread.join();
488 reader.Quit();
489 reader_thread.join();
490 gyro_sender.Quit();
491 gyro_thread.join();
492
493 drivetrain_writer.Quit();
494 drivetrain_writer_thread.join();
495 intake_writer.Quit();
496 intake_writer_thread.join();
497 solenoid_writer.Quit();
498 solenoid_thread.join();
499
500 ::aos::Cleanup();
501 }
502};
503
504} // namespace wpilib
505} // namespace y2016_bot3
506
507AOS_ROBOT_CLASS(::y2016_bot3::wpilib::WPILibRobot);