blob: cc340cb52d7dd1a865d38c7baee7452a1aca6ad1 [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();
119 ds_ = DriverStation::GetInstance();
120
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000121 LOG(INFO, "Things are now started\n");
122
123 ::aos::SetCurrentThreadRealtimePriority(kPriority);
124 while (run_) {
125 ::aos::time::PhasedLoopXMS(5, 4000);
126 RunIteration();
127 }
128 }
129
130 void RunIteration() {
Comran Morshede140e382015-08-19 20:35:25 +0000131 // General
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000132 {
133 auto new_state = ::aos::robot_state.MakeMessage();
134
135 new_state->reader_pid = my_pid_;
136 new_state->outputs_enabled = ds_->IsSysActive();
137 new_state->browned_out = ds_->IsSysBrownedOut();
138
139 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
140 new_state->is_5v_active = ControllerPower::GetEnabled5V();
141 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
142 new_state->voltage_5v = ControllerPower::GetVoltage5V();
143
144 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
145 new_state->voltage_battery = ds_->GetBatteryVoltage();
146
147 LOG_STRUCT(DEBUG, "robot_state", *new_state);
148
149 new_state.Send();
150 }
151
Comran Morshede140e382015-08-19 20:35:25 +0000152 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000153 {
154 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
155 drivetrain_message->right_encoder =
156 -drivetrain_translate(right_encoder_->GetRaw());
157 drivetrain_message->left_encoder =
158 drivetrain_translate(left_encoder_->GetRaw());
159
160 drivetrain_message.Send();
161 }
162
Comran Morshede140e382015-08-19 20:35:25 +0000163 // Elevator
164 {
165 // Update control loop positions.
166 auto elevator_message = elevator_queue.position.MakeMessage();
167 elevator_message->encoder =
168 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700169 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000170 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000171
172 elevator_message.Send();
173 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000174
175 // Intake
176 {
177 auto intake_message = intake_queue.position.MakeMessage();
178 intake_message.Send();
179 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000180 }
181
182 void Quit() { run_ = false; }
183
184 private:
185 static const int kPriority = 30;
186 static const int kInterruptPriority = 55;
187
188 int32_t my_pid_;
189 DriverStation *ds_;
190
Comran Morshede140e382015-08-19 20:35:25 +0000191 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700192 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000193 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000194
195 ::std::atomic<bool> run_{true};
196 DigitalGlitchFilter filter_;
197};
198
Comran Morshede140e382015-08-19 20:35:25 +0000199// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000200class SolenoidWriter {
201 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000202 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700203 : pcm_(pcm),
204 elevator_(".bot3.control_loops.elevator_queue.output"),
Austin Schuhbd01a582015-09-21 00:05:31 +0000205 intake_(".bot3.control_loops.intake_queue.output"),
206 can_grabber_control_(".bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000207
Austin Schuh39e5abc2015-10-31 18:51:20 -0700208 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000209 pressure_switch_ = ::std::move(pressure_switch);
210 }
211
212 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
213 compressor_relay_ = ::std::move(compressor_relay);
214 }
215
Comran Morshede140e382015-08-19 20:35:25 +0000216 void set_elevator_passive_support(
217 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
218 elevator_passive_support_ = ::std::move(elevator_passive_support);
219 }
220
Austin Schuhbd01a582015-09-21 00:05:31 +0000221 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
222 can_grabber_ = ::std::move(can_grabber);
223 }
224
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700225 void set_elevator_can_support(
226 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
227 elevator_can_support_ = ::std::move(elevator_can_support);
228 }
229
230 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
231 intake_claw_ = ::std::move(intake_claw);
232 }
233
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000234 void operator()() {
235 ::aos::SetCurrentThreadName("Solenoids");
236 ::aos::SetCurrentThreadRealtimePriority(30);
237
238 while (run_) {
239 ::aos::time::PhasedLoopXMS(20, 1000);
240
Austin Schuhbd01a582015-09-21 00:05:31 +0000241 // Can Grabber
242 {
243 can_grabber_control_.FetchLatest();
244 if (can_grabber_control_.get()) {
245 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
246 can_grabber_->Set(can_grabber_control_->can_grabbers);
247 }
248 }
249
Comran Morshede140e382015-08-19 20:35:25 +0000250 // Elevator
251 {
252 elevator_.FetchLatest();
253 if (elevator_.get()) {
254 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000255 elevator_passive_support_->Set(!elevator_->passive_support);
256 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700257 }
258 }
259
260 // Intake
261 {
262 intake_.FetchLatest();
263 if (intake_.get()) {
264 LOG_STRUCT(DEBUG, "solenoids", *intake_);
265 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000266 }
267 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000268
Comran Morshede140e382015-08-19 20:35:25 +0000269 // Compressor
270 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000271 {
272 ::frc971::wpilib::PneumaticsToLog to_log;
273 {
Comran Morshede140e382015-08-19 20:35:25 +0000274 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000275 const bool compressor_on = !pressure_switch_->Get();
276 to_log.compressor_on = compressor_on;
277 if (compressor_on) {
278 compressor_relay_->Set(Relay::kForward);
279 } else {
280 compressor_relay_->Set(Relay::kOff);
281 }
282 }
283
284 pcm_->Flush();
285 to_log.read_solenoids = pcm_->GetAll();
286 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
287 }
288 }
289 }
290
291 void Quit() { run_ = false; }
292
293 private:
294 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000295
296 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700297 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
298 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000299 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000300
Austin Schuh39e5abc2015-10-31 18:51:20 -0700301 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000302 ::std::unique_ptr<Relay> compressor_relay_;
303
Comran Morshede140e382015-08-19 20:35:25 +0000304 ::aos::Queue<::bot3::control_loops::ElevatorQueue::Output> elevator_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700305 ::aos::Queue<::bot3::control_loops::IntakeQueue::Output> intake_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000306 ::aos::Queue<::bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000307
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000308 ::std::atomic<bool> run_{true};
309};
310
Comran Morshede140e382015-08-19 20:35:25 +0000311// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000312class DrivetrainWriter : public LoopOutputHandler {
313 public:
314 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
315 left_drivetrain_talon_ = ::std::move(t);
316 }
317
318 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
319 right_drivetrain_talon_ = ::std::move(t);
320 }
321
322 private:
323 virtual void Read() override {
324 ::bot3::control_loops::drivetrain_queue.output.FetchAnother();
325 }
326
327 virtual void Write() override {
328 auto &queue = ::bot3::control_loops::drivetrain_queue.output;
329 LOG_STRUCT(DEBUG, "will output", *queue);
330 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
331 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
332 }
333
334 virtual void Stop() override {
335 LOG(WARNING, "drivetrain output too old\n");
336 left_drivetrain_talon_->Disable();
337 right_drivetrain_talon_->Disable();
338 }
339
340 ::std::unique_ptr<Talon> left_drivetrain_talon_;
341 ::std::unique_ptr<Talon> right_drivetrain_talon_;
342};
343
Comran Morshede140e382015-08-19 20:35:25 +0000344// Writes out elevator voltages.
345class ElevatorWriter : public LoopOutputHandler {
346 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700347 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
348 elevator_talon1_ = ::std::move(t);
349 }
350 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
351 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000352 }
353
354 private:
355 virtual void Read() override {
356 ::bot3::control_loops::elevator_queue.output.FetchAnother();
357 }
358
359 virtual void Write() override {
360 auto &queue = ::bot3::control_loops::elevator_queue.output;
361 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700362 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000363 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000364 }
365
366 virtual void Stop() override {
367 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700368 elevator_talon1_->Disable();
369 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000370 }
371
Jasmine Zhou954419a2015-09-12 18:31:31 -0700372 ::std::unique_ptr<Talon> elevator_talon1_;
373 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000374};
375
376// Writes out intake voltages.
377class IntakeWriter : public LoopOutputHandler {
378 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700379 void set_intake_talon1(::std::unique_ptr<Talon> t) {
380 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000381 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700382 void set_intake_talon2(::std::unique_ptr<Talon> t) {
383 intake_talon2_ = ::std::move(t);
384 }
385
Comran Morshede140e382015-08-19 20:35:25 +0000386 private:
387 virtual void Read() override {
388 ::bot3::control_loops::intake_queue.output.FetchAnother();
389 }
390
391 virtual void Write() override {
392 auto &queue = ::bot3::control_loops::intake_queue.output;
393 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700394 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000395 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000396 }
397
398 virtual void Stop() override {
399 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700400 intake_talon1_->Disable();
401 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000402 }
403
Jasmine Zhou954419a2015-09-12 18:31:31 -0700404 ::std::unique_ptr<Talon> intake_talon1_;
405 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000406};
407
Comran Morshed34f891d2015-09-15 22:04:43 +0000408// Writes out can grabber voltages.
409class CanGrabberWriter : public LoopOutputHandler {
410 public:
411 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
412
413 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
414 can_grabber_talon1_ = ::std::move(t);
415 }
416
417 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
418 can_grabber_talon2_ = ::std::move(t);
419 }
420
421 private:
422 virtual void Read() override {
423 ::bot3::autonomous::can_grabber_control.FetchAnother();
424 }
425
426 virtual void Write() override {
427 auto &queue = ::bot3::autonomous::can_grabber_control;
428 LOG_STRUCT(DEBUG, "will output", *queue);
429 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
430 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
431 }
432
433 virtual void Stop() override {
434 LOG(WARNING, "Can grabber output too old\n");
435 can_grabber_talon1_->Disable();
436 can_grabber_talon2_->Disable();
437 }
438
439 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
440};
441
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000442// TODO(brian): Replace this with ::std::make_unique once all our toolchains
443// have support.
444template <class T, class... U>
445std::unique_ptr<T> make_unique(U &&... u) {
446 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
447}
448
449class WPILibRobot : public RobotBase {
450 public:
451 ::std::unique_ptr<Encoder> encoder(int index) {
452 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
453 Encoder::k4X);
454 }
455 virtual void StartCompetition() {
456 ::aos::InitNRT();
457 ::aos::SetCurrentThreadName("StartCompetition");
458
459 JoystickSender joystick_sender;
460 ::std::thread joystick_thread(::std::ref(joystick_sender));
461
462 SensorReader reader;
463 LOG(INFO, "Creating the reader\n");
464
Jasmine Zhou954419a2015-09-12 18:31:31 -0700465 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700466 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000467
Jasmine Zhou954419a2015-09-12 18:31:31 -0700468 reader.set_left_encoder(encoder(0));
469 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000470 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000471
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000472 ::std::thread reader_thread(::std::ref(reader));
473 GyroSender gyro_sender;
474 ::std::thread gyro_thread(::std::ref(gyro_sender));
475
476 DrivetrainWriter drivetrain_writer;
477 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000478 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700479 drivetrain_writer.set_right_drivetrain_talon(
480 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000481 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
482
Comran Morshede140e382015-08-19 20:35:25 +0000483 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700484 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
485 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000486 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000487
488 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700489 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
490 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000491 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000492
Comran Morshed34f891d2015-09-15 22:04:43 +0000493 CanGrabberWriter can_grabber_writer;
494 can_grabber_writer.set_can_grabber_talon1(
495 ::std::unique_ptr<Talon>(new Talon(3)));
496 can_grabber_writer.set_can_grabber_talon2(
497 ::std::unique_ptr<Talon>(new Talon(4)));
498 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
499
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000500 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
501 new ::frc971::wpilib::BufferedPcm());
502 SolenoidWriter solenoid_writer(pcm);
503 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
504 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700505 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
506 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
507 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000508 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000509 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
510
511 // Wait forever. Not much else to do...
512 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
513
514 LOG(ERROR, "Exiting WPILibRobot\n");
515
516 joystick_sender.Quit();
517 joystick_thread.join();
518 reader.Quit();
519 reader_thread.join();
520 gyro_sender.Quit();
521 gyro_thread.join();
522
523 drivetrain_writer.Quit();
524 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000525
526 elevator_writer.Quit();
527 elevator_writer_thread.join();
528
529 intake_writer.Quit();
530 intake_writer_thread.join();
531
Comran Morshed34f891d2015-09-15 22:04:43 +0000532 can_grabber_writer.Quit();
533 can_grabber_writer_thread.join();
534
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000535 solenoid_writer.Quit();
536 solenoid_thread.join();
537
538 ::aos::Cleanup();
539 }
540};
541
542} // namespace wpilib
543} // namespace bot3
544
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000545START_ROBOT_CLASS(::bot3::wpilib::WPILibRobot);