blob: 02230f057d7a6f9b0e4c300a6c5e13f2882b0010 [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
6#include <thread>
7#include <mutex>
8#include <functional>
9
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070010#include "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
15#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080016#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070017#include "dma.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"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000052
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::aos::util::SimpleLogInterval;
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080058using ::y2015_bot3::control_loops::drivetrain_queue;
59using ::y2015_bot3::control_loops::elevator_queue;
60using ::y2015_bot3::control_loops::intake_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000061using ::frc971::wpilib::BufferedPcm;
Comran Morshede140e382015-08-19 20:35:25 +000062using ::frc971::wpilib::BufferedSolenoid;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000063using ::frc971::wpilib::LoopOutputHandler;
64using ::frc971::wpilib::JoystickSender;
65using ::frc971::wpilib::GyroSender;
66
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080067namespace y2015_bot3 {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000068namespace wpilib {
69
70double drivetrain_translate(int32_t in) {
Austin Schuh316ab462015-09-13 04:44:40 +000071 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080072 ::y2015_bot3::control_loops::kDrivetrainEncoderRatio *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000073 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
74}
75
Brian Silverman51091a02015-12-26 15:56:58 -080076double drivetrain_velocity_translate(double in) {
77 return (1.0 / in) / 256.0 /*cpr*/ *
78 ::y2015_bot3::control_loops::kDrivetrainEncoderRatio *
79 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
80}
81
Comran Morshede140e382015-08-19 20:35:25 +000082double elevator_translate(int32_t in) {
83 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080084 ::y2015_bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
85 ::y2015_bot3::control_loops::kElevChainReduction *
86 ::y2015_bot3::control_loops::kElevGearboxOutputRadianDistance;
Comran Morshede140e382015-08-19 20:35:25 +000087}
88
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000089static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000090 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
91 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000092 4.0 /* index pulse = 1/4 cycle */;
93
Comran Morshede140e382015-08-19 20:35:25 +000094// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000095class SensorReader {
96 public:
Brian Silverman425492b2015-12-30 15:23:55 -080097 SensorReader(::frc971::wpilib::PDPFetcher *pdp_fetcher)
98 : pdp_fetcher_(pdp_fetcher) {
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
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000141 ::aos::SetCurrentThreadRealtimePriority(kPriority);
142 while (run_) {
143 ::aos::time::PhasedLoopXMS(5, 4000);
144 RunIteration();
145 }
146 }
147
148 void RunIteration() {
Brian Silverman425492b2015-12-30 15:23:55 -0800149 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_fetcher_);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000150
Comran Morshede140e382015-08-19 20:35:25 +0000151 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000152 {
153 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
154 drivetrain_message->right_encoder =
155 -drivetrain_translate(right_encoder_->GetRaw());
156 drivetrain_message->left_encoder =
157 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800158 drivetrain_message->left_speed =
159 drivetrain_velocity_translate(left_encoder_->GetPeriod());
160 drivetrain_message->right_speed =
161 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000162
163 drivetrain_message.Send();
164 }
165
Comran Morshede140e382015-08-19 20:35:25 +0000166 // Elevator
167 {
168 // Update control loop positions.
169 auto elevator_message = elevator_queue.position.MakeMessage();
170 elevator_message->encoder =
171 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700172 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000173 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000174
175 elevator_message.Send();
176 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000177
178 // Intake
179 {
180 auto intake_message = intake_queue.position.MakeMessage();
181 intake_message.Send();
182 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000183 }
184
185 void Quit() { run_ = false; }
186
187 private:
188 static const int kPriority = 30;
189 static const int kInterruptPriority = 55;
190
191 int32_t my_pid_;
192 DriverStation *ds_;
Brian Silverman425492b2015-12-30 15:23:55 -0800193 ::frc971::wpilib::PDPFetcher *const pdp_fetcher_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000194
Comran Morshede140e382015-08-19 20:35:25 +0000195 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700196 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000197 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000198
199 ::std::atomic<bool> run_{true};
200 DigitalGlitchFilter filter_;
201};
202
Comran Morshede140e382015-08-19 20:35:25 +0000203// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000204class SolenoidWriter {
205 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000206 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700207 : pcm_(pcm),
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800208 elevator_(".y2015_bot3.control_loops.elevator_queue.output"),
209 intake_(".y2015_bot3.control_loops.intake_queue.output"),
210 can_grabber_control_(".y2015_bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000211
Austin Schuh39e5abc2015-10-31 18:51:20 -0700212 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000213 pressure_switch_ = ::std::move(pressure_switch);
214 }
215
216 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
217 compressor_relay_ = ::std::move(compressor_relay);
218 }
219
Comran Morshede140e382015-08-19 20:35:25 +0000220 void set_elevator_passive_support(
221 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
222 elevator_passive_support_ = ::std::move(elevator_passive_support);
223 }
224
Austin Schuhbd01a582015-09-21 00:05:31 +0000225 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
226 can_grabber_ = ::std::move(can_grabber);
227 }
228
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700229 void set_elevator_can_support(
230 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
231 elevator_can_support_ = ::std::move(elevator_can_support);
232 }
233
234 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
235 intake_claw_ = ::std::move(intake_claw);
236 }
237
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000238 void operator()() {
239 ::aos::SetCurrentThreadName("Solenoids");
240 ::aos::SetCurrentThreadRealtimePriority(30);
241
242 while (run_) {
243 ::aos::time::PhasedLoopXMS(20, 1000);
244
Austin Schuhbd01a582015-09-21 00:05:31 +0000245 // Can Grabber
246 {
247 can_grabber_control_.FetchLatest();
248 if (can_grabber_control_.get()) {
249 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
250 can_grabber_->Set(can_grabber_control_->can_grabbers);
251 }
252 }
253
Comran Morshede140e382015-08-19 20:35:25 +0000254 // Elevator
255 {
256 elevator_.FetchLatest();
257 if (elevator_.get()) {
258 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000259 elevator_passive_support_->Set(!elevator_->passive_support);
260 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700261 }
262 }
263
264 // Intake
265 {
266 intake_.FetchLatest();
267 if (intake_.get()) {
268 LOG_STRUCT(DEBUG, "solenoids", *intake_);
269 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000270 }
271 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000272
Comran Morshede140e382015-08-19 20:35:25 +0000273 // Compressor
274 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000275 {
276 ::frc971::wpilib::PneumaticsToLog to_log;
277 {
Comran Morshede140e382015-08-19 20:35:25 +0000278 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000279 const bool compressor_on = !pressure_switch_->Get();
280 to_log.compressor_on = compressor_on;
281 if (compressor_on) {
282 compressor_relay_->Set(Relay::kForward);
283 } else {
284 compressor_relay_->Set(Relay::kOff);
285 }
286 }
287
288 pcm_->Flush();
289 to_log.read_solenoids = pcm_->GetAll();
290 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
291 }
292 }
293 }
294
295 void Quit() { run_ = false; }
296
297 private:
298 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000299
300 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700301 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
302 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000303 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000304
Austin Schuh39e5abc2015-10-31 18:51:20 -0700305 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000306 ::std::unique_ptr<Relay> compressor_relay_;
307
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800308 ::aos::Queue<::y2015_bot3::control_loops::ElevatorQueue::Output> elevator_;
309 ::aos::Queue<::y2015_bot3::control_loops::IntakeQueue::Output> intake_;
310 ::aos::Queue<::y2015_bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000311
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000312 ::std::atomic<bool> run_{true};
313};
314
Comran Morshede140e382015-08-19 20:35:25 +0000315// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000316class DrivetrainWriter : public LoopOutputHandler {
317 public:
318 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
319 left_drivetrain_talon_ = ::std::move(t);
320 }
321
322 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
323 right_drivetrain_talon_ = ::std::move(t);
324 }
325
326 private:
327 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800328 ::y2015_bot3::control_loops::drivetrain_queue.output.FetchAnother();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000329 }
330
331 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800332 auto &queue = ::y2015_bot3::control_loops::drivetrain_queue.output;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000333 LOG_STRUCT(DEBUG, "will output", *queue);
334 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
335 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
336 }
337
338 virtual void Stop() override {
339 LOG(WARNING, "drivetrain output too old\n");
340 left_drivetrain_talon_->Disable();
341 right_drivetrain_talon_->Disable();
342 }
343
344 ::std::unique_ptr<Talon> left_drivetrain_talon_;
345 ::std::unique_ptr<Talon> right_drivetrain_talon_;
346};
347
Comran Morshede140e382015-08-19 20:35:25 +0000348// Writes out elevator voltages.
349class ElevatorWriter : public LoopOutputHandler {
350 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700351 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
352 elevator_talon1_ = ::std::move(t);
353 }
354 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
355 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000356 }
357
358 private:
359 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800360 ::y2015_bot3::control_loops::elevator_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000361 }
362
363 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800364 auto &queue = ::y2015_bot3::control_loops::elevator_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000365 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700366 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000367 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000368 }
369
370 virtual void Stop() override {
371 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700372 elevator_talon1_->Disable();
373 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000374 }
375
Jasmine Zhou954419a2015-09-12 18:31:31 -0700376 ::std::unique_ptr<Talon> elevator_talon1_;
377 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000378};
379
380// Writes out intake voltages.
381class IntakeWriter : public LoopOutputHandler {
382 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700383 void set_intake_talon1(::std::unique_ptr<Talon> t) {
384 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000385 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700386 void set_intake_talon2(::std::unique_ptr<Talon> t) {
387 intake_talon2_ = ::std::move(t);
388 }
389
Comran Morshede140e382015-08-19 20:35:25 +0000390 private:
391 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800392 ::y2015_bot3::control_loops::intake_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000393 }
394
395 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800396 auto &queue = ::y2015_bot3::control_loops::intake_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000397 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700398 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000399 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000400 }
401
402 virtual void Stop() override {
403 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700404 intake_talon1_->Disable();
405 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000406 }
407
Jasmine Zhou954419a2015-09-12 18:31:31 -0700408 ::std::unique_ptr<Talon> intake_talon1_;
409 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000410};
411
Comran Morshed34f891d2015-09-15 22:04:43 +0000412// Writes out can grabber voltages.
413class CanGrabberWriter : public LoopOutputHandler {
414 public:
415 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
416
417 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
418 can_grabber_talon1_ = ::std::move(t);
419 }
420
421 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
422 can_grabber_talon2_ = ::std::move(t);
423 }
424
425 private:
426 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800427 ::y2015_bot3::autonomous::can_grabber_control.FetchAnother();
Comran Morshed34f891d2015-09-15 22:04:43 +0000428 }
429
430 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800431 auto &queue = ::y2015_bot3::autonomous::can_grabber_control;
Comran Morshed34f891d2015-09-15 22:04:43 +0000432 LOG_STRUCT(DEBUG, "will output", *queue);
433 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
434 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
435 }
436
437 virtual void Stop() override {
438 LOG(WARNING, "Can grabber output too old\n");
439 can_grabber_talon1_->Disable();
440 can_grabber_talon2_->Disable();
441 }
442
443 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
444};
445
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000446// TODO(brian): Replace this with ::std::make_unique once all our toolchains
447// have support.
448template <class T, class... U>
449std::unique_ptr<T> make_unique(U &&... u) {
450 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
451}
452
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800453class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000454 public:
455 ::std::unique_ptr<Encoder> encoder(int index) {
456 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
457 Encoder::k4X);
458 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800459 virtual void Run() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000460 ::aos::InitNRT();
461 ::aos::SetCurrentThreadName("StartCompetition");
462
463 JoystickSender joystick_sender;
464 ::std::thread joystick_thread(::std::ref(joystick_sender));
465
Brian Silverman425492b2015-12-30 15:23:55 -0800466 ::frc971::wpilib::PDPFetcher pdp_fetcher;
467 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
468 SensorReader reader(&pdp_fetcher);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000469
Jasmine Zhou954419a2015-09-12 18:31:31 -0700470 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700471 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000472
Jasmine Zhou954419a2015-09-12 18:31:31 -0700473 reader.set_left_encoder(encoder(0));
474 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000475 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000476
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000477 ::std::thread reader_thread(::std::ref(reader));
478 GyroSender gyro_sender;
479 ::std::thread gyro_thread(::std::ref(gyro_sender));
480
481 DrivetrainWriter drivetrain_writer;
482 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000483 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700484 drivetrain_writer.set_right_drivetrain_talon(
485 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000486 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
487
Comran Morshede140e382015-08-19 20:35:25 +0000488 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700489 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
490 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000491 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000492
493 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700494 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
495 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000496 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000497
Comran Morshed34f891d2015-09-15 22:04:43 +0000498 CanGrabberWriter can_grabber_writer;
499 can_grabber_writer.set_can_grabber_talon1(
500 ::std::unique_ptr<Talon>(new Talon(3)));
501 can_grabber_writer.set_can_grabber_talon2(
502 ::std::unique_ptr<Talon>(new Talon(4)));
503 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
504
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000505 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
506 new ::frc971::wpilib::BufferedPcm());
507 SolenoidWriter solenoid_writer(pcm);
508 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
509 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700510 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
511 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
512 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000513 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000514 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
515
516 // Wait forever. Not much else to do...
517 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
518
519 LOG(ERROR, "Exiting WPILibRobot\n");
520
521 joystick_sender.Quit();
522 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800523 pdp_fetcher.Quit();
524 pdp_fetcher_thread.join();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000525 reader.Quit();
526 reader_thread.join();
527 gyro_sender.Quit();
528 gyro_thread.join();
529
530 drivetrain_writer.Quit();
531 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000532
533 elevator_writer.Quit();
534 elevator_writer_thread.join();
535
536 intake_writer.Quit();
537 intake_writer_thread.join();
538
Comran Morshed34f891d2015-09-15 22:04:43 +0000539 can_grabber_writer.Quit();
540 can_grabber_writer_thread.join();
541
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000542 solenoid_writer.Quit();
543 solenoid_thread.join();
544
545 ::aos::Cleanup();
546 }
547};
548
549} // namespace wpilib
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800550} // namespace y2015_bot3
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000551
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800552AOS_ROBOT_CLASS(::y2015_bot3::wpilib::WPILibRobot);