blob: 685b1255f776c3786cbafe92638bb7ea0198d8e5 [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
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000026#include "frc971/wpilib/joystick_sender.h"
27#include "frc971/wpilib/loop_output_handler.h"
28#include "frc971/wpilib/buffered_solenoid.h"
29#include "frc971/wpilib/buffered_pcm.h"
30#include "frc971/wpilib/gyro_sender.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000031#include "frc971/wpilib/logging.q.h"
32#include "bot3/control_loops/drivetrain/drivetrain.h"
Comran Morshede140e382015-08-19 20:35:25 +000033#include "bot3/control_loops/elevator/elevator.h"
34#include "bot3/control_loops/intake/intake.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000035
36#include "Encoder.h"
37#include "Talon.h"
38#include "DriverStation.h"
39#include "AnalogInput.h"
40#include "Compressor.h"
41#include "Relay.h"
42#include "RobotBase.h"
43#include "dma.h"
44#include "ControllerPower.h"
45
46#ifndef M_PI
47#define M_PI 3.14159265358979323846
48#endif
49
50using ::aos::util::SimpleLogInterval;
51using ::bot3::control_loops::drivetrain_queue;
Comran Morshede140e382015-08-19 20:35:25 +000052using ::bot3::control_loops::elevator_queue;
53using ::bot3::control_loops::intake_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000054using ::frc971::wpilib::BufferedPcm;
Comran Morshede140e382015-08-19 20:35:25 +000055using ::frc971::wpilib::BufferedSolenoid;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000056using ::frc971::wpilib::LoopOutputHandler;
57using ::frc971::wpilib::JoystickSender;
58using ::frc971::wpilib::GyroSender;
59
60namespace bot3 {
61namespace wpilib {
62
63double drivetrain_translate(int32_t in) {
Austin Schuh316ab462015-09-13 04:44:40 +000064 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000065 ::bot3::control_loops::kDrivetrainEncoderRatio *
66 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
67}
68
Comran Morshede140e382015-08-19 20:35:25 +000069double elevator_translate(int32_t in) {
70 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
71 ::bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
Austin Schuh316ab462015-09-13 04:44:40 +000072 ::bot3::control_loops::kElevChainReduction *
Comran Morshede140e382015-08-19 20:35:25 +000073 ::bot3::control_loops::kElevGearboxOutputRadianDistance;
74}
75
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000076static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000077 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
78 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000079 4.0 /* index pulse = 1/4 cycle */;
80
Comran Morshede140e382015-08-19 20:35:25 +000081// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000082class SensorReader {
83 public:
84 SensorReader() {
85 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
86 // we should ever see.
87 filter_.SetPeriodNanoSeconds(
88 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
89 }
90
Comran Morshede140e382015-08-19 20:35:25 +000091 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000092 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
93 left_encoder_ = ::std::move(left_encoder);
94 }
95
96 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
97 right_encoder_ = ::std::move(right_encoder);
98 }
99
Comran Morshede140e382015-08-19 20:35:25 +0000100 // Elevator setters.
101 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
102 filter_.Add(encoder.get());
103 elevator_encoder_ = ::std::move(encoder);
104 }
105
Austin Schuh39e5abc2015-10-31 18:51:20 -0700106 void set_elevator_zeroing_hall_effect(::std::unique_ptr<DigitalInput> hall) {
Comran Morshede140e382015-08-19 20:35:25 +0000107 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000108 }
109
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000110 void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) {
111 tote_sensor_ = ::std::move(tote_sensor);
112 }
113
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000114 void operator()() {
115 LOG(INFO, "In sensor reader thread\n");
116 ::aos::SetCurrentThreadName("SensorReader");
117
118 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700119 ds_ =
120#ifdef WPILIB2015
121 DriverStation::GetInstance();
122#else
123 &DriverStation::GetInstance();
124#endif
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000125
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000126 LOG(INFO, "Things are now started\n");
127
128 ::aos::SetCurrentThreadRealtimePriority(kPriority);
129 while (run_) {
130 ::aos::time::PhasedLoopXMS(5, 4000);
131 RunIteration();
132 }
133 }
134
135 void RunIteration() {
Comran Morshede140e382015-08-19 20:35:25 +0000136 // General
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000137 {
138 auto new_state = ::aos::robot_state.MakeMessage();
139
140 new_state->reader_pid = my_pid_;
141 new_state->outputs_enabled = ds_->IsSysActive();
142 new_state->browned_out = ds_->IsSysBrownedOut();
143
144 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
145 new_state->is_5v_active = ControllerPower::GetEnabled5V();
146 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
147 new_state->voltage_5v = ControllerPower::GetVoltage5V();
148
149 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
150 new_state->voltage_battery = ds_->GetBatteryVoltage();
151
152 LOG_STRUCT(DEBUG, "robot_state", *new_state);
153
154 new_state.Send();
155 }
156
Comran Morshede140e382015-08-19 20:35:25 +0000157 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000158 {
159 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
160 drivetrain_message->right_encoder =
161 -drivetrain_translate(right_encoder_->GetRaw());
162 drivetrain_message->left_encoder =
163 drivetrain_translate(left_encoder_->GetRaw());
164
165 drivetrain_message.Send();
166 }
167
Comran Morshede140e382015-08-19 20:35:25 +0000168 // Elevator
169 {
170 // Update control loop positions.
171 auto elevator_message = elevator_queue.position.MakeMessage();
172 elevator_message->encoder =
173 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700174 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000175 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000176
177 elevator_message.Send();
178 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000179
180 // Intake
181 {
182 auto intake_message = intake_queue.position.MakeMessage();
183 intake_message.Send();
184 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000185 }
186
187 void Quit() { run_ = false; }
188
189 private:
190 static const int kPriority = 30;
191 static const int kInterruptPriority = 55;
192
193 int32_t my_pid_;
194 DriverStation *ds_;
195
Comran Morshede140e382015-08-19 20:35:25 +0000196 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700197 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000198 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000199
200 ::std::atomic<bool> run_{true};
201 DigitalGlitchFilter filter_;
202};
203
Comran Morshede140e382015-08-19 20:35:25 +0000204// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000205class SolenoidWriter {
206 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000207 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700208 : pcm_(pcm),
209 elevator_(".bot3.control_loops.elevator_queue.output"),
Austin Schuhbd01a582015-09-21 00:05:31 +0000210 intake_(".bot3.control_loops.intake_queue.output"),
211 can_grabber_control_(".bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000212
Austin Schuh39e5abc2015-10-31 18:51:20 -0700213 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000214 pressure_switch_ = ::std::move(pressure_switch);
215 }
216
217 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
218 compressor_relay_ = ::std::move(compressor_relay);
219 }
220
Comran Morshede140e382015-08-19 20:35:25 +0000221 void set_elevator_passive_support(
222 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
223 elevator_passive_support_ = ::std::move(elevator_passive_support);
224 }
225
Austin Schuhbd01a582015-09-21 00:05:31 +0000226 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
227 can_grabber_ = ::std::move(can_grabber);
228 }
229
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700230 void set_elevator_can_support(
231 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
232 elevator_can_support_ = ::std::move(elevator_can_support);
233 }
234
235 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
236 intake_claw_ = ::std::move(intake_claw);
237 }
238
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000239 void operator()() {
240 ::aos::SetCurrentThreadName("Solenoids");
241 ::aos::SetCurrentThreadRealtimePriority(30);
242
243 while (run_) {
244 ::aos::time::PhasedLoopXMS(20, 1000);
245
Austin Schuhbd01a582015-09-21 00:05:31 +0000246 // Can Grabber
247 {
248 can_grabber_control_.FetchLatest();
249 if (can_grabber_control_.get()) {
250 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
251 can_grabber_->Set(can_grabber_control_->can_grabbers);
252 }
253 }
254
Comran Morshede140e382015-08-19 20:35:25 +0000255 // Elevator
256 {
257 elevator_.FetchLatest();
258 if (elevator_.get()) {
259 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000260 elevator_passive_support_->Set(!elevator_->passive_support);
261 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700262 }
263 }
264
265 // Intake
266 {
267 intake_.FetchLatest();
268 if (intake_.get()) {
269 LOG_STRUCT(DEBUG, "solenoids", *intake_);
270 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000271 }
272 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000273
Comran Morshede140e382015-08-19 20:35:25 +0000274 // Compressor
275 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000276 {
277 ::frc971::wpilib::PneumaticsToLog to_log;
278 {
Comran Morshede140e382015-08-19 20:35:25 +0000279 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000280 const bool compressor_on = !pressure_switch_->Get();
281 to_log.compressor_on = compressor_on;
282 if (compressor_on) {
283 compressor_relay_->Set(Relay::kForward);
284 } else {
285 compressor_relay_->Set(Relay::kOff);
286 }
287 }
288
289 pcm_->Flush();
290 to_log.read_solenoids = pcm_->GetAll();
291 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
292 }
293 }
294 }
295
296 void Quit() { run_ = false; }
297
298 private:
299 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000300
301 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700302 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
303 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000304 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000305
Austin Schuh39e5abc2015-10-31 18:51:20 -0700306 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000307 ::std::unique_ptr<Relay> compressor_relay_;
308
Comran Morshede140e382015-08-19 20:35:25 +0000309 ::aos::Queue<::bot3::control_loops::ElevatorQueue::Output> elevator_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700310 ::aos::Queue<::bot3::control_loops::IntakeQueue::Output> intake_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000311 ::aos::Queue<::bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000312
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000313 ::std::atomic<bool> run_{true};
314};
315
Comran Morshede140e382015-08-19 20:35:25 +0000316// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000317class DrivetrainWriter : public LoopOutputHandler {
318 public:
319 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
320 left_drivetrain_talon_ = ::std::move(t);
321 }
322
323 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
324 right_drivetrain_talon_ = ::std::move(t);
325 }
326
327 private:
328 virtual void Read() override {
329 ::bot3::control_loops::drivetrain_queue.output.FetchAnother();
330 }
331
332 virtual void Write() override {
333 auto &queue = ::bot3::control_loops::drivetrain_queue.output;
334 LOG_STRUCT(DEBUG, "will output", *queue);
335 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
336 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
337 }
338
339 virtual void Stop() override {
340 LOG(WARNING, "drivetrain output too old\n");
341 left_drivetrain_talon_->Disable();
342 right_drivetrain_talon_->Disable();
343 }
344
345 ::std::unique_ptr<Talon> left_drivetrain_talon_;
346 ::std::unique_ptr<Talon> right_drivetrain_talon_;
347};
348
Comran Morshede140e382015-08-19 20:35:25 +0000349// Writes out elevator voltages.
350class ElevatorWriter : public LoopOutputHandler {
351 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700352 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
353 elevator_talon1_ = ::std::move(t);
354 }
355 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
356 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000357 }
358
359 private:
360 virtual void Read() override {
361 ::bot3::control_loops::elevator_queue.output.FetchAnother();
362 }
363
364 virtual void Write() override {
365 auto &queue = ::bot3::control_loops::elevator_queue.output;
366 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700367 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000368 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000369 }
370
371 virtual void Stop() override {
372 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700373 elevator_talon1_->Disable();
374 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000375 }
376
Jasmine Zhou954419a2015-09-12 18:31:31 -0700377 ::std::unique_ptr<Talon> elevator_talon1_;
378 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000379};
380
381// Writes out intake voltages.
382class IntakeWriter : public LoopOutputHandler {
383 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700384 void set_intake_talon1(::std::unique_ptr<Talon> t) {
385 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000386 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700387 void set_intake_talon2(::std::unique_ptr<Talon> t) {
388 intake_talon2_ = ::std::move(t);
389 }
390
Comran Morshede140e382015-08-19 20:35:25 +0000391 private:
392 virtual void Read() override {
393 ::bot3::control_loops::intake_queue.output.FetchAnother();
394 }
395
396 virtual void Write() override {
397 auto &queue = ::bot3::control_loops::intake_queue.output;
398 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700399 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000400 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000401 }
402
403 virtual void Stop() override {
404 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700405 intake_talon1_->Disable();
406 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000407 }
408
Jasmine Zhou954419a2015-09-12 18:31:31 -0700409 ::std::unique_ptr<Talon> intake_talon1_;
410 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000411};
412
Comran Morshed34f891d2015-09-15 22:04:43 +0000413// Writes out can grabber voltages.
414class CanGrabberWriter : public LoopOutputHandler {
415 public:
416 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
417
418 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
419 can_grabber_talon1_ = ::std::move(t);
420 }
421
422 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
423 can_grabber_talon2_ = ::std::move(t);
424 }
425
426 private:
427 virtual void Read() override {
428 ::bot3::autonomous::can_grabber_control.FetchAnother();
429 }
430
431 virtual void Write() override {
432 auto &queue = ::bot3::autonomous::can_grabber_control;
433 LOG_STRUCT(DEBUG, "will output", *queue);
434 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
435 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
436 }
437
438 virtual void Stop() override {
439 LOG(WARNING, "Can grabber output too old\n");
440 can_grabber_talon1_->Disable();
441 can_grabber_talon2_->Disable();
442 }
443
444 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
445};
446
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000447// TODO(brian): Replace this with ::std::make_unique once all our toolchains
448// have support.
449template <class T, class... U>
450std::unique_ptr<T> make_unique(U &&... u) {
451 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
452}
453
454class WPILibRobot : public RobotBase {
455 public:
456 ::std::unique_ptr<Encoder> encoder(int index) {
457 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
458 Encoder::k4X);
459 }
460 virtual void StartCompetition() {
461 ::aos::InitNRT();
462 ::aos::SetCurrentThreadName("StartCompetition");
463
464 JoystickSender joystick_sender;
465 ::std::thread joystick_thread(::std::ref(joystick_sender));
466
467 SensorReader reader;
468 LOG(INFO, "Creating the reader\n");
469
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();
523 reader.Quit();
524 reader_thread.join();
525 gyro_sender.Quit();
526 gyro_thread.join();
527
528 drivetrain_writer.Quit();
529 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000530
531 elevator_writer.Quit();
532 elevator_writer_thread.join();
533
534 intake_writer.Quit();
535 intake_writer_thread.join();
536
Comran Morshed34f891d2015-09-15 22:04:43 +0000537 can_grabber_writer.Quit();
538 can_grabber_writer_thread.join();
539
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000540 solenoid_writer.Quit();
541 solenoid_thread.join();
542
543 ::aos::Cleanup();
544 }
545};
546
547} // namespace wpilib
548} // namespace bot3
549
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000550START_ROBOT_CLASS(::bot3::wpilib::WPILibRobot);