blob: e1cc670719d435705ef1871d4ae19a8ecfef08c7 [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"
16#include "RobotBase.h"
17#include "dma.h"
18#include "ControllerPower.h"
19#ifndef WPILIB2015
20#include "DigitalGlitchFilter.h"
21#endif
22#include "DigitalInput.h"
23#undef ERROR
24
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000025#include "aos/common/logging/logging.h"
26#include "aos/common/logging/queue_logging.h"
27#include "aos/common/time.h"
28#include "aos/common/util/log_interval.h"
29#include "aos/common/util/phased_loop.h"
30#include "aos/common/util/wrapping_counter.h"
31#include "aos/common/stl_mutex.h"
32#include "aos/linux_code/init.h"
33#include "aos/common/messages/robot_state.q.h"
34
35#include "frc971/control_loops/control_loops.q.h"
36#include "bot3/control_loops/drivetrain/drivetrain.q.h"
Comran Morshede140e382015-08-19 20:35:25 +000037#include "bot3/control_loops/elevator/elevator.q.h"
38#include "bot3/control_loops/intake/intake.q.h"
Comran Morshed34f891d2015-09-15 22:04:43 +000039#include "bot3/autonomous/auto.q.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000040
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000041#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
45#include "frc971/wpilib/gyro_sender.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000046#include "frc971/wpilib/logging.q.h"
47#include "bot3/control_loops/drivetrain/drivetrain.h"
Comran Morshede140e382015-08-19 20:35:25 +000048#include "bot3/control_loops/elevator/elevator.h"
49#include "bot3/control_loops/intake/intake.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000050
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000051
52#ifndef M_PI
53#define M_PI 3.14159265358979323846
54#endif
55
56using ::aos::util::SimpleLogInterval;
57using ::bot3::control_loops::drivetrain_queue;
Comran Morshede140e382015-08-19 20:35:25 +000058using ::bot3::control_loops::elevator_queue;
59using ::bot3::control_loops::intake_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000060using ::frc971::wpilib::BufferedPcm;
Comran Morshede140e382015-08-19 20:35:25 +000061using ::frc971::wpilib::BufferedSolenoid;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000062using ::frc971::wpilib::LoopOutputHandler;
63using ::frc971::wpilib::JoystickSender;
64using ::frc971::wpilib::GyroSender;
65
66namespace bot3 {
67namespace wpilib {
68
69double drivetrain_translate(int32_t in) {
Austin Schuh316ab462015-09-13 04:44:40 +000070 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000071 ::bot3::control_loops::kDrivetrainEncoderRatio *
72 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
73}
74
Comran Morshede140e382015-08-19 20:35:25 +000075double elevator_translate(int32_t in) {
76 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
77 ::bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
Austin Schuh316ab462015-09-13 04:44:40 +000078 ::bot3::control_loops::kElevChainReduction *
Comran Morshede140e382015-08-19 20:35:25 +000079 ::bot3::control_loops::kElevGearboxOutputRadianDistance;
80}
81
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000082static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000083 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
84 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000085 4.0 /* index pulse = 1/4 cycle */;
86
Comran Morshede140e382015-08-19 20:35:25 +000087// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000088class SensorReader {
89 public:
90 SensorReader() {
91 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
92 // we should ever see.
93 filter_.SetPeriodNanoSeconds(
94 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
95 }
96
Comran Morshede140e382015-08-19 20:35:25 +000097 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000098 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
99 left_encoder_ = ::std::move(left_encoder);
100 }
101
102 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
103 right_encoder_ = ::std::move(right_encoder);
104 }
105
Comran Morshede140e382015-08-19 20:35:25 +0000106 // Elevator setters.
107 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
108 filter_.Add(encoder.get());
109 elevator_encoder_ = ::std::move(encoder);
110 }
111
Austin Schuh39e5abc2015-10-31 18:51:20 -0700112 void set_elevator_zeroing_hall_effect(::std::unique_ptr<DigitalInput> hall) {
Comran Morshede140e382015-08-19 20:35:25 +0000113 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000114 }
115
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000116 void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) {
117 tote_sensor_ = ::std::move(tote_sensor);
118 }
119
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000120 void operator()() {
121 LOG(INFO, "In sensor reader thread\n");
122 ::aos::SetCurrentThreadName("SensorReader");
123
124 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700125 ds_ =
126#ifdef WPILIB2015
127 DriverStation::GetInstance();
128#else
129 &DriverStation::GetInstance();
130#endif
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000131
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000132 LOG(INFO, "Things are now started\n");
133
134 ::aos::SetCurrentThreadRealtimePriority(kPriority);
135 while (run_) {
136 ::aos::time::PhasedLoopXMS(5, 4000);
137 RunIteration();
138 }
139 }
140
141 void RunIteration() {
Comran Morshede140e382015-08-19 20:35:25 +0000142 // General
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000143 {
144 auto new_state = ::aos::robot_state.MakeMessage();
145
146 new_state->reader_pid = my_pid_;
147 new_state->outputs_enabled = ds_->IsSysActive();
148 new_state->browned_out = ds_->IsSysBrownedOut();
149
150 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
151 new_state->is_5v_active = ControllerPower::GetEnabled5V();
152 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
153 new_state->voltage_5v = ControllerPower::GetVoltage5V();
154
155 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
156 new_state->voltage_battery = ds_->GetBatteryVoltage();
157
158 LOG_STRUCT(DEBUG, "robot_state", *new_state);
159
160 new_state.Send();
161 }
162
Comran Morshede140e382015-08-19 20:35:25 +0000163 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000164 {
165 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
166 drivetrain_message->right_encoder =
167 -drivetrain_translate(right_encoder_->GetRaw());
168 drivetrain_message->left_encoder =
169 drivetrain_translate(left_encoder_->GetRaw());
170
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:
196 static const int kPriority = 30;
197 static const int kInterruptPriority = 55;
198
199 int32_t my_pid_;
200 DriverStation *ds_;
201
Comran Morshede140e382015-08-19 20:35:25 +0000202 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700203 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000204 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000205
206 ::std::atomic<bool> run_{true};
207 DigitalGlitchFilter filter_;
208};
209
Comran Morshede140e382015-08-19 20:35:25 +0000210// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000211class SolenoidWriter {
212 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000213 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700214 : pcm_(pcm),
215 elevator_(".bot3.control_loops.elevator_queue.output"),
Austin Schuhbd01a582015-09-21 00:05:31 +0000216 intake_(".bot3.control_loops.intake_queue.output"),
217 can_grabber_control_(".bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000218
Austin Schuh39e5abc2015-10-31 18:51:20 -0700219 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000220 pressure_switch_ = ::std::move(pressure_switch);
221 }
222
223 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
224 compressor_relay_ = ::std::move(compressor_relay);
225 }
226
Comran Morshede140e382015-08-19 20:35:25 +0000227 void set_elevator_passive_support(
228 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
229 elevator_passive_support_ = ::std::move(elevator_passive_support);
230 }
231
Austin Schuhbd01a582015-09-21 00:05:31 +0000232 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
233 can_grabber_ = ::std::move(can_grabber);
234 }
235
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700236 void set_elevator_can_support(
237 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
238 elevator_can_support_ = ::std::move(elevator_can_support);
239 }
240
241 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
242 intake_claw_ = ::std::move(intake_claw);
243 }
244
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000245 void operator()() {
246 ::aos::SetCurrentThreadName("Solenoids");
247 ::aos::SetCurrentThreadRealtimePriority(30);
248
249 while (run_) {
250 ::aos::time::PhasedLoopXMS(20, 1000);
251
Austin Schuhbd01a582015-09-21 00:05:31 +0000252 // Can Grabber
253 {
254 can_grabber_control_.FetchLatest();
255 if (can_grabber_control_.get()) {
256 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
257 can_grabber_->Set(can_grabber_control_->can_grabbers);
258 }
259 }
260
Comran Morshede140e382015-08-19 20:35:25 +0000261 // Elevator
262 {
263 elevator_.FetchLatest();
264 if (elevator_.get()) {
265 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000266 elevator_passive_support_->Set(!elevator_->passive_support);
267 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700268 }
269 }
270
271 // Intake
272 {
273 intake_.FetchLatest();
274 if (intake_.get()) {
275 LOG_STRUCT(DEBUG, "solenoids", *intake_);
276 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000277 }
278 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000279
Comran Morshede140e382015-08-19 20:35:25 +0000280 // Compressor
281 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000282 {
283 ::frc971::wpilib::PneumaticsToLog to_log;
284 {
Comran Morshede140e382015-08-19 20:35:25 +0000285 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000286 const bool compressor_on = !pressure_switch_->Get();
287 to_log.compressor_on = compressor_on;
288 if (compressor_on) {
289 compressor_relay_->Set(Relay::kForward);
290 } else {
291 compressor_relay_->Set(Relay::kOff);
292 }
293 }
294
295 pcm_->Flush();
296 to_log.read_solenoids = pcm_->GetAll();
297 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
298 }
299 }
300 }
301
302 void Quit() { run_ = false; }
303
304 private:
305 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000306
307 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700308 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
309 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000310 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000311
Austin Schuh39e5abc2015-10-31 18:51:20 -0700312 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000313 ::std::unique_ptr<Relay> compressor_relay_;
314
Comran Morshede140e382015-08-19 20:35:25 +0000315 ::aos::Queue<::bot3::control_loops::ElevatorQueue::Output> elevator_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700316 ::aos::Queue<::bot3::control_loops::IntakeQueue::Output> intake_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000317 ::aos::Queue<::bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000318
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000319 ::std::atomic<bool> run_{true};
320};
321
Comran Morshede140e382015-08-19 20:35:25 +0000322// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000323class DrivetrainWriter : public LoopOutputHandler {
324 public:
325 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
326 left_drivetrain_talon_ = ::std::move(t);
327 }
328
329 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
330 right_drivetrain_talon_ = ::std::move(t);
331 }
332
333 private:
334 virtual void Read() override {
335 ::bot3::control_loops::drivetrain_queue.output.FetchAnother();
336 }
337
338 virtual void Write() override {
339 auto &queue = ::bot3::control_loops::drivetrain_queue.output;
340 LOG_STRUCT(DEBUG, "will output", *queue);
341 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
342 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
343 }
344
345 virtual void Stop() override {
346 LOG(WARNING, "drivetrain output too old\n");
347 left_drivetrain_talon_->Disable();
348 right_drivetrain_talon_->Disable();
349 }
350
351 ::std::unique_ptr<Talon> left_drivetrain_talon_;
352 ::std::unique_ptr<Talon> right_drivetrain_talon_;
353};
354
Comran Morshede140e382015-08-19 20:35:25 +0000355// Writes out elevator voltages.
356class ElevatorWriter : public LoopOutputHandler {
357 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700358 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
359 elevator_talon1_ = ::std::move(t);
360 }
361 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
362 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000363 }
364
365 private:
366 virtual void Read() override {
367 ::bot3::control_loops::elevator_queue.output.FetchAnother();
368 }
369
370 virtual void Write() override {
371 auto &queue = ::bot3::control_loops::elevator_queue.output;
372 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700373 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000374 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000375 }
376
377 virtual void Stop() override {
378 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700379 elevator_talon1_->Disable();
380 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000381 }
382
Jasmine Zhou954419a2015-09-12 18:31:31 -0700383 ::std::unique_ptr<Talon> elevator_talon1_;
384 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000385};
386
387// Writes out intake voltages.
388class IntakeWriter : public LoopOutputHandler {
389 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700390 void set_intake_talon1(::std::unique_ptr<Talon> t) {
391 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000392 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700393 void set_intake_talon2(::std::unique_ptr<Talon> t) {
394 intake_talon2_ = ::std::move(t);
395 }
396
Comran Morshede140e382015-08-19 20:35:25 +0000397 private:
398 virtual void Read() override {
399 ::bot3::control_loops::intake_queue.output.FetchAnother();
400 }
401
402 virtual void Write() override {
403 auto &queue = ::bot3::control_loops::intake_queue.output;
404 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700405 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000406 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000407 }
408
409 virtual void Stop() override {
410 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700411 intake_talon1_->Disable();
412 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000413 }
414
Jasmine Zhou954419a2015-09-12 18:31:31 -0700415 ::std::unique_ptr<Talon> intake_talon1_;
416 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000417};
418
Comran Morshed34f891d2015-09-15 22:04:43 +0000419// Writes out can grabber voltages.
420class CanGrabberWriter : public LoopOutputHandler {
421 public:
422 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
423
424 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
425 can_grabber_talon1_ = ::std::move(t);
426 }
427
428 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
429 can_grabber_talon2_ = ::std::move(t);
430 }
431
432 private:
433 virtual void Read() override {
434 ::bot3::autonomous::can_grabber_control.FetchAnother();
435 }
436
437 virtual void Write() override {
438 auto &queue = ::bot3::autonomous::can_grabber_control;
439 LOG_STRUCT(DEBUG, "will output", *queue);
440 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
441 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
442 }
443
444 virtual void Stop() override {
445 LOG(WARNING, "Can grabber output too old\n");
446 can_grabber_talon1_->Disable();
447 can_grabber_talon2_->Disable();
448 }
449
450 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
451};
452
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000453// TODO(brian): Replace this with ::std::make_unique once all our toolchains
454// have support.
455template <class T, class... U>
456std::unique_ptr<T> make_unique(U &&... u) {
457 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
458}
459
460class WPILibRobot : public RobotBase {
461 public:
462 ::std::unique_ptr<Encoder> encoder(int index) {
463 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
464 Encoder::k4X);
465 }
466 virtual void StartCompetition() {
467 ::aos::InitNRT();
468 ::aos::SetCurrentThreadName("StartCompetition");
469
470 JoystickSender joystick_sender;
471 ::std::thread joystick_thread(::std::ref(joystick_sender));
472
473 SensorReader reader;
474 LOG(INFO, "Creating the reader\n");
475
Jasmine Zhou954419a2015-09-12 18:31:31 -0700476 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700477 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000478
Jasmine Zhou954419a2015-09-12 18:31:31 -0700479 reader.set_left_encoder(encoder(0));
480 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000481 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000482
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000483 ::std::thread reader_thread(::std::ref(reader));
484 GyroSender gyro_sender;
485 ::std::thread gyro_thread(::std::ref(gyro_sender));
486
487 DrivetrainWriter drivetrain_writer;
488 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000489 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700490 drivetrain_writer.set_right_drivetrain_talon(
491 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000492 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
493
Comran Morshede140e382015-08-19 20:35:25 +0000494 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700495 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
496 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000497 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000498
499 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700500 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
501 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000502 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000503
Comran Morshed34f891d2015-09-15 22:04:43 +0000504 CanGrabberWriter can_grabber_writer;
505 can_grabber_writer.set_can_grabber_talon1(
506 ::std::unique_ptr<Talon>(new Talon(3)));
507 can_grabber_writer.set_can_grabber_talon2(
508 ::std::unique_ptr<Talon>(new Talon(4)));
509 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
510
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000511 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
512 new ::frc971::wpilib::BufferedPcm());
513 SolenoidWriter solenoid_writer(pcm);
514 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
515 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700516 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
517 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
518 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000519 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000520 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
521
522 // Wait forever. Not much else to do...
523 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
524
525 LOG(ERROR, "Exiting WPILibRobot\n");
526
527 joystick_sender.Quit();
528 joystick_thread.join();
529 reader.Quit();
530 reader_thread.join();
531 gyro_sender.Quit();
532 gyro_thread.join();
533
534 drivetrain_writer.Quit();
535 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000536
537 elevator_writer.Quit();
538 elevator_writer_thread.join();
539
540 intake_writer.Quit();
541 intake_writer_thread.join();
542
Comran Morshed34f891d2015-09-15 22:04:43 +0000543 can_grabber_writer.Quit();
544 can_grabber_writer_thread.join();
545
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000546 solenoid_writer.Quit();
547 solenoid_thread.join();
548
549 ::aos::Cleanup();
550 }
551};
552
553} // namespace wpilib
554} // namespace bot3
555
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000556START_ROBOT_CLASS(::bot3::wpilib::WPILibRobot);