blob: bbfa91e6b968add34675eb272d20d32314594ea7 [file] [log] [blame]
Comran Morshed0d6cf9b2015-06-17 19:29:57 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Comran Morshed0d6cf9b2015-06-17 19:29:57 +00007#include <thread>
8#include <mutex>
9#include <functional>
10
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070011#include "Encoder.h"
12#include "Talon.h"
13#include "DriverStation.h"
14#include "AnalogInput.h"
15#include "Compressor.h"
16#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080017#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#include "DigitalInput.h"
22#undef ERROR
23
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000024#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
26#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
30#include "aos/common/stl_mutex.h"
31#include "aos/linux_code/init.h"
32#include "aos/common/messages/robot_state.q.h"
33
34#include "frc971/control_loops/control_loops.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050035
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080036#include "y2015_bot3/control_loops/drivetrain/drivetrain.q.h"
37#include "y2015_bot3/control_loops/elevator/elevator.q.h"
38#include "y2015_bot3/control_loops/intake/intake.q.h"
39#include "y2015_bot3/autonomous/auto.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050040#include "y2015_bot3/control_loops/drivetrain/drivetrain.h"
41#include "y2015_bot3/control_loops/elevator/elevator.h"
42#include "y2015_bot3/control_loops/intake/intake.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000043
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000044#include "frc971/wpilib/joystick_sender.h"
45#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/buffered_solenoid.h"
47#include "frc971/wpilib/buffered_pcm.h"
48#include "frc971/wpilib/gyro_sender.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000049#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050050#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman425492b2015-12-30 15:23:55 -080051#include "frc971/wpilib/pdp_fetcher.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050052#include "frc971/wpilib/dma.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000053
54#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
58using ::aos::util::SimpleLogInterval;
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080059using ::y2015_bot3::control_loops::drivetrain_queue;
60using ::y2015_bot3::control_loops::elevator_queue;
61using ::y2015_bot3::control_loops::intake_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000062using ::frc971::wpilib::BufferedPcm;
Comran Morshede140e382015-08-19 20:35:25 +000063using ::frc971::wpilib::BufferedSolenoid;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000064using ::frc971::wpilib::LoopOutputHandler;
65using ::frc971::wpilib::JoystickSender;
66using ::frc971::wpilib::GyroSender;
67
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080068namespace y2015_bot3 {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000069namespace wpilib {
70
71double drivetrain_translate(int32_t in) {
Austin Schuh316ab462015-09-13 04:44:40 +000072 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080073 ::y2015_bot3::control_loops::kDrivetrainEncoderRatio *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000074 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
75}
76
Brian Silverman51091a02015-12-26 15:56:58 -080077double drivetrain_velocity_translate(double in) {
78 return (1.0 / in) / 256.0 /*cpr*/ *
79 ::y2015_bot3::control_loops::kDrivetrainEncoderRatio *
80 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
81}
82
Comran Morshede140e382015-08-19 20:35:25 +000083double elevator_translate(int32_t in) {
84 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080085 ::y2015_bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
86 ::y2015_bot3::control_loops::kElevChainReduction *
87 ::y2015_bot3::control_loops::kElevGearboxOutputRadianDistance;
Comran Morshede140e382015-08-19 20:35:25 +000088}
89
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000090static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000091 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
92 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000093 4.0 /* index pulse = 1/4 cycle */;
94
Comran Morshede140e382015-08-19 20:35:25 +000095// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000096class SensorReader {
97 public:
Brian Silverman39b339e2016-01-03 13:24:22 -080098 SensorReader() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000099 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
100 // we should ever see.
101 filter_.SetPeriodNanoSeconds(
102 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
103 }
104
Comran Morshede140e382015-08-19 20:35:25 +0000105 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000106 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
107 left_encoder_ = ::std::move(left_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800108 left_encoder_->SetMaxPeriod(0.005);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000109 }
110
111 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
112 right_encoder_ = ::std::move(right_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800113 right_encoder_->SetMaxPeriod(0.005);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000114 }
115
Comran Morshede140e382015-08-19 20:35:25 +0000116 // Elevator setters.
117 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
118 filter_.Add(encoder.get());
119 elevator_encoder_ = ::std::move(encoder);
120 }
121
Austin Schuh39e5abc2015-10-31 18:51:20 -0700122 void set_elevator_zeroing_hall_effect(::std::unique_ptr<DigitalInput> hall) {
Comran Morshede140e382015-08-19 20:35:25 +0000123 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000124 }
125
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000126 void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) {
127 tote_sensor_ = ::std::move(tote_sensor);
128 }
129
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000130 void operator()() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000131 ::aos::SetCurrentThreadName("SensorReader");
132
133 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700134 ds_ =
135#ifdef WPILIB2015
136 DriverStation::GetInstance();
137#else
138 &DriverStation::GetInstance();
139#endif
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000140
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700141 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
142 ::std::chrono::milliseconds(4));
Brian Silverman5090c432016-01-02 14:44:26 -0800143
144 ::aos::SetCurrentThreadRealtimePriority(40);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000145 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800146 {
147 const int iterations = phased_loop.SleepUntilNext();
148 if (iterations != 1) {
149 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
150 }
151 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000152 RunIteration();
153 }
154 }
155
156 void RunIteration() {
Brian Silverman39b339e2016-01-03 13:24:22 -0800157 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000158
Comran Morshede140e382015-08-19 20:35:25 +0000159 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000160 {
161 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
162 drivetrain_message->right_encoder =
163 -drivetrain_translate(right_encoder_->GetRaw());
164 drivetrain_message->left_encoder =
165 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800166 drivetrain_message->left_speed =
167 drivetrain_velocity_translate(left_encoder_->GetPeriod());
168 drivetrain_message->right_speed =
169 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000170
171 drivetrain_message.Send();
172 }
173
Comran Morshede140e382015-08-19 20:35:25 +0000174 // Elevator
175 {
176 // Update control loop positions.
177 auto elevator_message = elevator_queue.position.MakeMessage();
178 elevator_message->encoder =
179 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700180 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000181 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000182
183 elevator_message.Send();
184 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000185
186 // Intake
187 {
188 auto intake_message = intake_queue.position.MakeMessage();
189 intake_message.Send();
190 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000191 }
192
193 void Quit() { run_ = false; }
194
195 private:
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000196 int32_t my_pid_;
197 DriverStation *ds_;
198
Comran Morshede140e382015-08-19 20:35:25 +0000199 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700200 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000201 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000202
203 ::std::atomic<bool> run_{true};
204 DigitalGlitchFilter filter_;
205};
206
Comran Morshede140e382015-08-19 20:35:25 +0000207// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000208class SolenoidWriter {
209 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000210 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700211 : pcm_(pcm),
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800212 elevator_(".y2015_bot3.control_loops.elevator_queue.output"),
213 intake_(".y2015_bot3.control_loops.intake_queue.output"),
214 can_grabber_control_(".y2015_bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000215
Austin Schuh39e5abc2015-10-31 18:51:20 -0700216 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000217 pressure_switch_ = ::std::move(pressure_switch);
218 }
219
220 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
221 compressor_relay_ = ::std::move(compressor_relay);
222 }
223
Comran Morshede140e382015-08-19 20:35:25 +0000224 void set_elevator_passive_support(
225 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
226 elevator_passive_support_ = ::std::move(elevator_passive_support);
227 }
228
Austin Schuhbd01a582015-09-21 00:05:31 +0000229 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
230 can_grabber_ = ::std::move(can_grabber);
231 }
232
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700233 void set_elevator_can_support(
234 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
235 elevator_can_support_ = ::std::move(elevator_can_support);
236 }
237
238 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
239 intake_claw_ = ::std::move(intake_claw);
240 }
241
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000242 void operator()() {
243 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800244 ::aos::SetCurrentThreadRealtimePriority(27);
245
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700246 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
247 ::std::chrono::milliseconds(1));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000248
249 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800250 {
251 const int iterations = phased_loop.SleepUntilNext();
252 if (iterations != 1) {
253 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
254 }
255 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000256
Austin Schuhbd01a582015-09-21 00:05:31 +0000257 // Can Grabber
258 {
259 can_grabber_control_.FetchLatest();
260 if (can_grabber_control_.get()) {
261 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
262 can_grabber_->Set(can_grabber_control_->can_grabbers);
263 }
264 }
265
Comran Morshede140e382015-08-19 20:35:25 +0000266 // Elevator
267 {
268 elevator_.FetchLatest();
269 if (elevator_.get()) {
270 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000271 elevator_passive_support_->Set(!elevator_->passive_support);
272 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700273 }
274 }
275
276 // Intake
277 {
278 intake_.FetchLatest();
279 if (intake_.get()) {
280 LOG_STRUCT(DEBUG, "solenoids", *intake_);
281 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000282 }
283 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000284
Comran Morshede140e382015-08-19 20:35:25 +0000285 // Compressor
286 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000287 {
288 ::frc971::wpilib::PneumaticsToLog to_log;
289 {
Comran Morshede140e382015-08-19 20:35:25 +0000290 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000291 const bool compressor_on = !pressure_switch_->Get();
292 to_log.compressor_on = compressor_on;
293 if (compressor_on) {
294 compressor_relay_->Set(Relay::kForward);
295 } else {
296 compressor_relay_->Set(Relay::kOff);
297 }
298 }
299
300 pcm_->Flush();
301 to_log.read_solenoids = pcm_->GetAll();
302 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
303 }
304 }
305 }
306
307 void Quit() { run_ = false; }
308
309 private:
310 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000311
312 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700313 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
314 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000315 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000316
Austin Schuh39e5abc2015-10-31 18:51:20 -0700317 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000318 ::std::unique_ptr<Relay> compressor_relay_;
319
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800320 ::aos::Queue<::y2015_bot3::control_loops::ElevatorQueue::Output> elevator_;
321 ::aos::Queue<::y2015_bot3::control_loops::IntakeQueue::Output> intake_;
322 ::aos::Queue<::y2015_bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000323
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000324 ::std::atomic<bool> run_{true};
325};
326
Comran Morshede140e382015-08-19 20:35:25 +0000327// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000328class DrivetrainWriter : public LoopOutputHandler {
329 public:
330 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
331 left_drivetrain_talon_ = ::std::move(t);
332 }
333
334 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
335 right_drivetrain_talon_ = ::std::move(t);
336 }
337
338 private:
339 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800340 ::y2015_bot3::control_loops::drivetrain_queue.output.FetchAnother();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000341 }
342
343 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800344 auto &queue = ::y2015_bot3::control_loops::drivetrain_queue.output;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000345 LOG_STRUCT(DEBUG, "will output", *queue);
346 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
347 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
348 }
349
350 virtual void Stop() override {
351 LOG(WARNING, "drivetrain output too old\n");
352 left_drivetrain_talon_->Disable();
353 right_drivetrain_talon_->Disable();
354 }
355
356 ::std::unique_ptr<Talon> left_drivetrain_talon_;
357 ::std::unique_ptr<Talon> right_drivetrain_talon_;
358};
359
Comran Morshede140e382015-08-19 20:35:25 +0000360// Writes out elevator voltages.
361class ElevatorWriter : public LoopOutputHandler {
362 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700363 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
364 elevator_talon1_ = ::std::move(t);
365 }
366 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
367 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000368 }
369
370 private:
371 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800372 ::y2015_bot3::control_loops::elevator_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000373 }
374
375 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800376 auto &queue = ::y2015_bot3::control_loops::elevator_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000377 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700378 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000379 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000380 }
381
382 virtual void Stop() override {
383 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700384 elevator_talon1_->Disable();
385 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000386 }
387
Jasmine Zhou954419a2015-09-12 18:31:31 -0700388 ::std::unique_ptr<Talon> elevator_talon1_;
389 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000390};
391
392// Writes out intake voltages.
393class IntakeWriter : public LoopOutputHandler {
394 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700395 void set_intake_talon1(::std::unique_ptr<Talon> t) {
396 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000397 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700398 void set_intake_talon2(::std::unique_ptr<Talon> t) {
399 intake_talon2_ = ::std::move(t);
400 }
401
Comran Morshede140e382015-08-19 20:35:25 +0000402 private:
403 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800404 ::y2015_bot3::control_loops::intake_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000405 }
406
407 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800408 auto &queue = ::y2015_bot3::control_loops::intake_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000409 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700410 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000411 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000412 }
413
414 virtual void Stop() override {
415 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700416 intake_talon1_->Disable();
417 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000418 }
419
Jasmine Zhou954419a2015-09-12 18:31:31 -0700420 ::std::unique_ptr<Talon> intake_talon1_;
421 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000422};
423
Comran Morshed34f891d2015-09-15 22:04:43 +0000424// Writes out can grabber voltages.
425class CanGrabberWriter : public LoopOutputHandler {
426 public:
427 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
428
429 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
430 can_grabber_talon1_ = ::std::move(t);
431 }
432
433 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
434 can_grabber_talon2_ = ::std::move(t);
435 }
436
437 private:
438 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800439 ::y2015_bot3::autonomous::can_grabber_control.FetchAnother();
Comran Morshed34f891d2015-09-15 22:04:43 +0000440 }
441
442 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800443 auto &queue = ::y2015_bot3::autonomous::can_grabber_control;
Comran Morshed34f891d2015-09-15 22:04:43 +0000444 LOG_STRUCT(DEBUG, "will output", *queue);
445 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
446 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
447 }
448
449 virtual void Stop() override {
450 LOG(WARNING, "Can grabber output too old\n");
451 can_grabber_talon1_->Disable();
452 can_grabber_talon2_->Disable();
453 }
454
455 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
456};
457
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000458// TODO(brian): Replace this with ::std::make_unique once all our toolchains
459// have support.
460template <class T, class... U>
461std::unique_ptr<T> make_unique(U &&... u) {
462 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
463}
464
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800465class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000466 public:
467 ::std::unique_ptr<Encoder> encoder(int index) {
468 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
469 Encoder::k4X);
470 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800471 virtual void Run() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000472 ::aos::InitNRT();
473 ::aos::SetCurrentThreadName("StartCompetition");
474
475 JoystickSender joystick_sender;
476 ::std::thread joystick_thread(::std::ref(joystick_sender));
477
Brian Silverman425492b2015-12-30 15:23:55 -0800478 ::frc971::wpilib::PDPFetcher pdp_fetcher;
479 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Brian Silverman39b339e2016-01-03 13:24:22 -0800480 SensorReader reader;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000481
Jasmine Zhou954419a2015-09-12 18:31:31 -0700482 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700483 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000484
Jasmine Zhou954419a2015-09-12 18:31:31 -0700485 reader.set_left_encoder(encoder(0));
486 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000487 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000488
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000489 ::std::thread reader_thread(::std::ref(reader));
490 GyroSender gyro_sender;
491 ::std::thread gyro_thread(::std::ref(gyro_sender));
492
493 DrivetrainWriter drivetrain_writer;
494 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000495 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700496 drivetrain_writer.set_right_drivetrain_talon(
497 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000498 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
499
Comran Morshede140e382015-08-19 20:35:25 +0000500 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700501 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
502 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000503 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000504
505 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700506 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
507 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000508 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000509
Comran Morshed34f891d2015-09-15 22:04:43 +0000510 CanGrabberWriter can_grabber_writer;
511 can_grabber_writer.set_can_grabber_talon1(
512 ::std::unique_ptr<Talon>(new Talon(3)));
513 can_grabber_writer.set_can_grabber_talon2(
514 ::std::unique_ptr<Talon>(new Talon(4)));
515 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
516
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000517 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
518 new ::frc971::wpilib::BufferedPcm());
519 SolenoidWriter solenoid_writer(pcm);
520 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
521 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700522 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
523 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
524 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000525 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000526 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
527
528 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800529 while (true) {
530 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
531 if (r != 0) {
532 PLOG(WARNING, "infinite select failed");
533 } else {
534 PLOG(WARNING, "infinite select succeeded??\n");
535 }
536 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000537
538 LOG(ERROR, "Exiting WPILibRobot\n");
539
540 joystick_sender.Quit();
541 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800542 pdp_fetcher.Quit();
543 pdp_fetcher_thread.join();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000544 reader.Quit();
545 reader_thread.join();
546 gyro_sender.Quit();
547 gyro_thread.join();
548
549 drivetrain_writer.Quit();
550 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000551
552 elevator_writer.Quit();
553 elevator_writer_thread.join();
554
555 intake_writer.Quit();
556 intake_writer_thread.join();
557
Comran Morshed34f891d2015-09-15 22:04:43 +0000558 can_grabber_writer.Quit();
559 can_grabber_writer_thread.join();
560
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000561 solenoid_writer.Quit();
562 solenoid_thread.join();
563
564 ::aos::Cleanup();
565 }
566};
567
568} // namespace wpilib
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800569} // namespace y2015_bot3
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000570
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800571AOS_ROBOT_CLASS(::y2015_bot3::wpilib::WPILibRobot);