blob: 36e14ad311002eea31e6f4ed4adfd618234751fe [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
10#include "aos/common/logging/logging.h"
11#include "aos/common/logging/queue_logging.h"
12#include "aos/common/time.h"
13#include "aos/common/util/log_interval.h"
14#include "aos/common/util/phased_loop.h"
15#include "aos/common/util/wrapping_counter.h"
16#include "aos/common/stl_mutex.h"
17#include "aos/linux_code/init.h"
18#include "aos/common/messages/robot_state.q.h"
19
20#include "frc971/control_loops/control_loops.q.h"
21#include "bot3/control_loops/drivetrain/drivetrain.q.h"
Comran Morshede140e382015-08-19 20:35:25 +000022#include "bot3/control_loops/elevator/elevator.q.h"
23#include "bot3/control_loops/intake/intake.q.h"
Comran Morshed34f891d2015-09-15 22:04:43 +000024#include "bot3/autonomous/auto.q.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000025
26#include "frc971/wpilib/hall_effect.h"
27#include "frc971/wpilib/joystick_sender.h"
28#include "frc971/wpilib/loop_output_handler.h"
29#include "frc971/wpilib/buffered_solenoid.h"
30#include "frc971/wpilib/buffered_pcm.h"
31#include "frc971/wpilib/gyro_sender.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000032#include "frc971/wpilib/logging.q.h"
33#include "bot3/control_loops/drivetrain/drivetrain.h"
Comran Morshede140e382015-08-19 20:35:25 +000034#include "bot3/control_loops/elevator/elevator.h"
35#include "bot3/control_loops/intake/intake.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000036
37#include "Encoder.h"
38#include "Talon.h"
39#include "DriverStation.h"
40#include "AnalogInput.h"
41#include "Compressor.h"
42#include "Relay.h"
43#include "RobotBase.h"
44#include "dma.h"
45#include "ControllerPower.h"
46
47#ifndef M_PI
48#define M_PI 3.14159265358979323846
49#endif
50
51using ::aos::util::SimpleLogInterval;
52using ::bot3::control_loops::drivetrain_queue;
Comran Morshede140e382015-08-19 20:35:25 +000053using ::bot3::control_loops::elevator_queue;
54using ::bot3::control_loops::intake_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000055using ::frc971::wpilib::BufferedPcm;
Comran Morshede140e382015-08-19 20:35:25 +000056using ::frc971::wpilib::BufferedSolenoid;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000057using ::frc971::wpilib::LoopOutputHandler;
58using ::frc971::wpilib::JoystickSender;
59using ::frc971::wpilib::GyroSender;
Comran Morshede140e382015-08-19 20:35:25 +000060using ::frc971::wpilib::HallEffect;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000061
62namespace bot3 {
63namespace wpilib {
64
65double drivetrain_translate(int32_t in) {
Austin Schuh316ab462015-09-13 04:44:40 +000066 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000067 ::bot3::control_loops::kDrivetrainEncoderRatio *
68 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
69}
70
Comran Morshede140e382015-08-19 20:35:25 +000071double elevator_translate(int32_t in) {
72 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
73 ::bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
Austin Schuh316ab462015-09-13 04:44:40 +000074 ::bot3::control_loops::kElevChainReduction *
Comran Morshede140e382015-08-19 20:35:25 +000075 ::bot3::control_loops::kElevGearboxOutputRadianDistance;
76}
77
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000078static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000079 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
80 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000081 4.0 /* index pulse = 1/4 cycle */;
82
Comran Morshede140e382015-08-19 20:35:25 +000083// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000084class SensorReader {
85 public:
86 SensorReader() {
87 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
88 // we should ever see.
89 filter_.SetPeriodNanoSeconds(
90 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
91 }
92
Comran Morshede140e382015-08-19 20:35:25 +000093 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000094 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
95 left_encoder_ = ::std::move(left_encoder);
96 }
97
98 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
99 right_encoder_ = ::std::move(right_encoder);
100 }
101
Comran Morshede140e382015-08-19 20:35:25 +0000102 // Elevator setters.
103 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
104 filter_.Add(encoder.get());
105 elevator_encoder_ = ::std::move(encoder);
106 }
107
108 void set_elevator_zeroing_hall_effect(::std::unique_ptr<HallEffect> hall) {
109 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000110 }
111
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000112 void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) {
113 tote_sensor_ = ::std::move(tote_sensor);
114 }
115
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000116 void operator()() {
117 LOG(INFO, "In sensor reader thread\n");
118 ::aos::SetCurrentThreadName("SensorReader");
119
120 my_pid_ = getpid();
121 ds_ = DriverStation::GetInstance();
122
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000123 LOG(INFO, "Things are now started\n");
124
125 ::aos::SetCurrentThreadRealtimePriority(kPriority);
126 while (run_) {
127 ::aos::time::PhasedLoopXMS(5, 4000);
128 RunIteration();
129 }
130 }
131
132 void RunIteration() {
Comran Morshede140e382015-08-19 20:35:25 +0000133 // General
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000134 {
135 auto new_state = ::aos::robot_state.MakeMessage();
136
137 new_state->reader_pid = my_pid_;
138 new_state->outputs_enabled = ds_->IsSysActive();
139 new_state->browned_out = ds_->IsSysBrownedOut();
140
141 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
142 new_state->is_5v_active = ControllerPower::GetEnabled5V();
143 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
144 new_state->voltage_5v = ControllerPower::GetVoltage5V();
145
146 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
147 new_state->voltage_battery = ds_->GetBatteryVoltage();
148
149 LOG_STRUCT(DEBUG, "robot_state", *new_state);
150
151 new_state.Send();
152 }
153
Comran Morshede140e382015-08-19 20:35:25 +0000154 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000155 {
156 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
157 drivetrain_message->right_encoder =
158 -drivetrain_translate(right_encoder_->GetRaw());
159 drivetrain_message->left_encoder =
160 drivetrain_translate(left_encoder_->GetRaw());
161
162 drivetrain_message.Send();
163 }
164
Comran Morshede140e382015-08-19 20:35:25 +0000165 // Elevator
166 {
167 // Update control loop positions.
168 auto elevator_message = elevator_queue.position.MakeMessage();
169 elevator_message->encoder =
170 elevator_translate(elevator_encoder_->GetRaw());
Comran Morshed34f891d2015-09-15 22:04:43 +0000171 elevator_message->bottom_hall_effect = zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000172 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000173
174 elevator_message.Send();
175 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000176
177 // Intake
178 {
179 auto intake_message = intake_queue.position.MakeMessage();
180 intake_message.Send();
181 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000182 }
183
184 void Quit() { run_ = false; }
185
186 private:
187 static const int kPriority = 30;
188 static const int kInterruptPriority = 55;
189
190 int32_t my_pid_;
191 DriverStation *ds_;
192
Comran Morshede140e382015-08-19 20:35:25 +0000193 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
194 ::std::unique_ptr<HallEffect> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000195 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000196
197 ::std::atomic<bool> run_{true};
198 DigitalGlitchFilter filter_;
199};
200
Comran Morshede140e382015-08-19 20:35:25 +0000201// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000202class SolenoidWriter {
203 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000204 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700205 : pcm_(pcm),
206 elevator_(".bot3.control_loops.elevator_queue.output"),
Austin Schuhbd01a582015-09-21 00:05:31 +0000207 intake_(".bot3.control_loops.intake_queue.output"),
208 can_grabber_control_(".bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000209
210 void set_pressure_switch(::std::unique_ptr<DigitalSource> pressure_switch) {
211 pressure_switch_ = ::std::move(pressure_switch);
212 }
213
214 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
215 compressor_relay_ = ::std::move(compressor_relay);
216 }
217
Comran Morshede140e382015-08-19 20:35:25 +0000218 void set_elevator_passive_support(
219 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
220 elevator_passive_support_ = ::std::move(elevator_passive_support);
221 }
222
Austin Schuhbd01a582015-09-21 00:05:31 +0000223 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
224 can_grabber_ = ::std::move(can_grabber);
225 }
226
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700227 void set_elevator_can_support(
228 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
229 elevator_can_support_ = ::std::move(elevator_can_support);
230 }
231
232 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
233 intake_claw_ = ::std::move(intake_claw);
234 }
235
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000236 void operator()() {
237 ::aos::SetCurrentThreadName("Solenoids");
238 ::aos::SetCurrentThreadRealtimePriority(30);
239
240 while (run_) {
241 ::aos::time::PhasedLoopXMS(20, 1000);
242
Austin Schuhbd01a582015-09-21 00:05:31 +0000243 // Can Grabber
244 {
245 can_grabber_control_.FetchLatest();
246 if (can_grabber_control_.get()) {
247 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
248 can_grabber_->Set(can_grabber_control_->can_grabbers);
249 }
250 }
251
Comran Morshede140e382015-08-19 20:35:25 +0000252 // Elevator
253 {
254 elevator_.FetchLatest();
255 if (elevator_.get()) {
256 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000257 elevator_passive_support_->Set(!elevator_->passive_support);
258 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700259 }
260 }
261
262 // Intake
263 {
264 intake_.FetchLatest();
265 if (intake_.get()) {
266 LOG_STRUCT(DEBUG, "solenoids", *intake_);
267 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000268 }
269 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000270
Comran Morshede140e382015-08-19 20:35:25 +0000271 // Compressor
272 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000273 {
274 ::frc971::wpilib::PneumaticsToLog to_log;
275 {
Comran Morshede140e382015-08-19 20:35:25 +0000276 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000277 const bool compressor_on = !pressure_switch_->Get();
278 to_log.compressor_on = compressor_on;
279 if (compressor_on) {
280 compressor_relay_->Set(Relay::kForward);
281 } else {
282 compressor_relay_->Set(Relay::kOff);
283 }
284 }
285
286 pcm_->Flush();
287 to_log.read_solenoids = pcm_->GetAll();
288 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
289 }
290 }
291 }
292
293 void Quit() { run_ = false; }
294
295 private:
296 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000297
298 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700299 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
300 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000301 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000302
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000303 ::std::unique_ptr<DigitalSource> pressure_switch_;
304 ::std::unique_ptr<Relay> compressor_relay_;
305
Comran Morshede140e382015-08-19 20:35:25 +0000306 ::aos::Queue<::bot3::control_loops::ElevatorQueue::Output> elevator_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700307 ::aos::Queue<::bot3::control_loops::IntakeQueue::Output> intake_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000308 ::aos::Queue<::bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000309
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000310 ::std::atomic<bool> run_{true};
311};
312
Comran Morshede140e382015-08-19 20:35:25 +0000313// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000314class DrivetrainWriter : public LoopOutputHandler {
315 public:
316 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
317 left_drivetrain_talon_ = ::std::move(t);
318 }
319
320 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
321 right_drivetrain_talon_ = ::std::move(t);
322 }
323
324 private:
325 virtual void Read() override {
326 ::bot3::control_loops::drivetrain_queue.output.FetchAnother();
327 }
328
329 virtual void Write() override {
330 auto &queue = ::bot3::control_loops::drivetrain_queue.output;
331 LOG_STRUCT(DEBUG, "will output", *queue);
332 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
333 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
334 }
335
336 virtual void Stop() override {
337 LOG(WARNING, "drivetrain output too old\n");
338 left_drivetrain_talon_->Disable();
339 right_drivetrain_talon_->Disable();
340 }
341
342 ::std::unique_ptr<Talon> left_drivetrain_talon_;
343 ::std::unique_ptr<Talon> right_drivetrain_talon_;
344};
345
Comran Morshede140e382015-08-19 20:35:25 +0000346// Writes out elevator voltages.
347class ElevatorWriter : public LoopOutputHandler {
348 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700349 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
350 elevator_talon1_ = ::std::move(t);
351 }
352 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
353 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000354 }
355
356 private:
357 virtual void Read() override {
358 ::bot3::control_loops::elevator_queue.output.FetchAnother();
359 }
360
361 virtual void Write() override {
362 auto &queue = ::bot3::control_loops::elevator_queue.output;
363 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700364 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000365 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000366 }
367
368 virtual void Stop() override {
369 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700370 elevator_talon1_->Disable();
371 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000372 }
373
Jasmine Zhou954419a2015-09-12 18:31:31 -0700374 ::std::unique_ptr<Talon> elevator_talon1_;
375 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000376};
377
378// Writes out intake voltages.
379class IntakeWriter : public LoopOutputHandler {
380 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700381 void set_intake_talon1(::std::unique_ptr<Talon> t) {
382 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000383 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700384 void set_intake_talon2(::std::unique_ptr<Talon> t) {
385 intake_talon2_ = ::std::move(t);
386 }
387
Comran Morshede140e382015-08-19 20:35:25 +0000388 private:
389 virtual void Read() override {
390 ::bot3::control_loops::intake_queue.output.FetchAnother();
391 }
392
393 virtual void Write() override {
394 auto &queue = ::bot3::control_loops::intake_queue.output;
395 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700396 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000397 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000398 }
399
400 virtual void Stop() override {
401 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700402 intake_talon1_->Disable();
403 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000404 }
405
Jasmine Zhou954419a2015-09-12 18:31:31 -0700406 ::std::unique_ptr<Talon> intake_talon1_;
407 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000408};
409
Comran Morshed34f891d2015-09-15 22:04:43 +0000410// Writes out can grabber voltages.
411class CanGrabberWriter : public LoopOutputHandler {
412 public:
413 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
414
415 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
416 can_grabber_talon1_ = ::std::move(t);
417 }
418
419 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
420 can_grabber_talon2_ = ::std::move(t);
421 }
422
423 private:
424 virtual void Read() override {
425 ::bot3::autonomous::can_grabber_control.FetchAnother();
426 }
427
428 virtual void Write() override {
429 auto &queue = ::bot3::autonomous::can_grabber_control;
430 LOG_STRUCT(DEBUG, "will output", *queue);
431 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
432 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
433 }
434
435 virtual void Stop() override {
436 LOG(WARNING, "Can grabber output too old\n");
437 can_grabber_talon1_->Disable();
438 can_grabber_talon2_->Disable();
439 }
440
441 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
442};
443
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000444// TODO(brian): Replace this with ::std::make_unique once all our toolchains
445// have support.
446template <class T, class... U>
447std::unique_ptr<T> make_unique(U &&... u) {
448 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
449}
450
451class WPILibRobot : public RobotBase {
452 public:
453 ::std::unique_ptr<Encoder> encoder(int index) {
454 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
455 Encoder::k4X);
456 }
457 virtual void StartCompetition() {
458 ::aos::InitNRT();
459 ::aos::SetCurrentThreadName("StartCompetition");
460
461 JoystickSender joystick_sender;
462 ::std::thread joystick_thread(::std::ref(joystick_sender));
463
464 SensorReader reader;
465 LOG(INFO, "Creating the reader\n");
466
Jasmine Zhou954419a2015-09-12 18:31:31 -0700467 reader.set_elevator_encoder(encoder(6));
Comran Morshed34f891d2015-09-15 22:04:43 +0000468 reader.set_elevator_zeroing_hall_effect(make_unique<HallEffect>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000469
Jasmine Zhou954419a2015-09-12 18:31:31 -0700470 reader.set_left_encoder(encoder(0));
471 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000472 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000473
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000474 ::std::thread reader_thread(::std::ref(reader));
475 GyroSender gyro_sender;
476 ::std::thread gyro_thread(::std::ref(gyro_sender));
477
478 DrivetrainWriter drivetrain_writer;
479 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000480 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700481 drivetrain_writer.set_right_drivetrain_talon(
482 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000483 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
484
Comran Morshede140e382015-08-19 20:35:25 +0000485 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700486 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
487 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000488 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000489
490 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700491 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
492 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000493 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000494
Comran Morshed34f891d2015-09-15 22:04:43 +0000495 CanGrabberWriter can_grabber_writer;
496 can_grabber_writer.set_can_grabber_talon1(
497 ::std::unique_ptr<Talon>(new Talon(3)));
498 can_grabber_writer.set_can_grabber_talon2(
499 ::std::unique_ptr<Talon>(new Talon(4)));
500 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
501
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000502 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
503 new ::frc971::wpilib::BufferedPcm());
504 SolenoidWriter solenoid_writer(pcm);
505 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
506 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700507 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
508 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
509 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000510 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000511 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
512
513 // Wait forever. Not much else to do...
514 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
515
516 LOG(ERROR, "Exiting WPILibRobot\n");
517
518 joystick_sender.Quit();
519 joystick_thread.join();
520 reader.Quit();
521 reader_thread.join();
522 gyro_sender.Quit();
523 gyro_thread.join();
524
525 drivetrain_writer.Quit();
526 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000527
528 elevator_writer.Quit();
529 elevator_writer_thread.join();
530
531 intake_writer.Quit();
532 intake_writer_thread.join();
533
Comran Morshed34f891d2015-09-15 22:04:43 +0000534 can_grabber_writer.Quit();
535 can_grabber_writer_thread.join();
536
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000537 solenoid_writer.Quit();
538 solenoid_thread.join();
539
540 ::aos::Cleanup();
541 }
542};
543
544} // namespace wpilib
545} // namespace bot3
546
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000547START_ROBOT_CLASS(::bot3::wpilib::WPILibRobot);