blob: 4dfae3f400c1ce770e150c2a57f62bc1d26bce47 [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"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#include "DigitalInput.h"
22#undef ERROR
23
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000024#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
26#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
30#include "aos/common/stl_mutex.h"
31#include "aos/linux_code/init.h"
32#include "aos/common/messages/robot_state.q.h"
33
34#include "frc971/control_loops/control_loops.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050035
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080036#include "y2015_bot3/control_loops/drivetrain/drivetrain.q.h"
37#include "y2015_bot3/control_loops/elevator/elevator.q.h"
38#include "y2015_bot3/control_loops/intake/intake.q.h"
39#include "y2015_bot3/autonomous/auto.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050040#include "y2015_bot3/control_loops/drivetrain/drivetrain.h"
41#include "y2015_bot3/control_loops/elevator/elevator.h"
42#include "y2015_bot3/control_loops/intake/intake.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000043
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000044#include "frc971/wpilib/joystick_sender.h"
45#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/buffered_solenoid.h"
47#include "frc971/wpilib/buffered_pcm.h"
48#include "frc971/wpilib/gyro_sender.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000049#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050050#include "frc971/wpilib/wpilib_interface.h"
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;
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080057using ::y2015_bot3::control_loops::drivetrain_queue;
58using ::y2015_bot3::control_loops::elevator_queue;
59using ::y2015_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
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080066namespace y2015_bot3 {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000067namespace 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*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080071 ::y2015_bot3::control_loops::kDrivetrainEncoderRatio *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000072 (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*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080077 ::y2015_bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
78 ::y2015_bot3::control_loops::kElevChainReduction *
79 ::y2015_bot3::control_loops::kElevGearboxOutputRadianDistance;
Comran Morshede140e382015-08-19 20:35:25 +000080}
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() {
Brian Silverman811f8ec2015-12-06 01:29:42 -0500142 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000143
Comran Morshede140e382015-08-19 20:35:25 +0000144 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000145 {
146 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
147 drivetrain_message->right_encoder =
148 -drivetrain_translate(right_encoder_->GetRaw());
149 drivetrain_message->left_encoder =
150 drivetrain_translate(left_encoder_->GetRaw());
151
152 drivetrain_message.Send();
153 }
154
Comran Morshede140e382015-08-19 20:35:25 +0000155 // Elevator
156 {
157 // Update control loop positions.
158 auto elevator_message = elevator_queue.position.MakeMessage();
159 elevator_message->encoder =
160 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700161 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000162 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000163
164 elevator_message.Send();
165 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000166
167 // Intake
168 {
169 auto intake_message = intake_queue.position.MakeMessage();
170 intake_message.Send();
171 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000172 }
173
174 void Quit() { run_ = false; }
175
176 private:
177 static const int kPriority = 30;
178 static const int kInterruptPriority = 55;
179
180 int32_t my_pid_;
181 DriverStation *ds_;
182
Comran Morshede140e382015-08-19 20:35:25 +0000183 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700184 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000185 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000186
187 ::std::atomic<bool> run_{true};
188 DigitalGlitchFilter filter_;
189};
190
Comran Morshede140e382015-08-19 20:35:25 +0000191// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000192class SolenoidWriter {
193 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000194 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700195 : pcm_(pcm),
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800196 elevator_(".y2015_bot3.control_loops.elevator_queue.output"),
197 intake_(".y2015_bot3.control_loops.intake_queue.output"),
198 can_grabber_control_(".y2015_bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000199
Austin Schuh39e5abc2015-10-31 18:51:20 -0700200 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000201 pressure_switch_ = ::std::move(pressure_switch);
202 }
203
204 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
205 compressor_relay_ = ::std::move(compressor_relay);
206 }
207
Comran Morshede140e382015-08-19 20:35:25 +0000208 void set_elevator_passive_support(
209 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
210 elevator_passive_support_ = ::std::move(elevator_passive_support);
211 }
212
Austin Schuhbd01a582015-09-21 00:05:31 +0000213 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
214 can_grabber_ = ::std::move(can_grabber);
215 }
216
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700217 void set_elevator_can_support(
218 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
219 elevator_can_support_ = ::std::move(elevator_can_support);
220 }
221
222 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
223 intake_claw_ = ::std::move(intake_claw);
224 }
225
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000226 void operator()() {
227 ::aos::SetCurrentThreadName("Solenoids");
228 ::aos::SetCurrentThreadRealtimePriority(30);
229
230 while (run_) {
231 ::aos::time::PhasedLoopXMS(20, 1000);
232
Austin Schuhbd01a582015-09-21 00:05:31 +0000233 // Can Grabber
234 {
235 can_grabber_control_.FetchLatest();
236 if (can_grabber_control_.get()) {
237 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
238 can_grabber_->Set(can_grabber_control_->can_grabbers);
239 }
240 }
241
Comran Morshede140e382015-08-19 20:35:25 +0000242 // Elevator
243 {
244 elevator_.FetchLatest();
245 if (elevator_.get()) {
246 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000247 elevator_passive_support_->Set(!elevator_->passive_support);
248 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700249 }
250 }
251
252 // Intake
253 {
254 intake_.FetchLatest();
255 if (intake_.get()) {
256 LOG_STRUCT(DEBUG, "solenoids", *intake_);
257 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000258 }
259 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000260
Comran Morshede140e382015-08-19 20:35:25 +0000261 // Compressor
262 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000263 {
264 ::frc971::wpilib::PneumaticsToLog to_log;
265 {
Comran Morshede140e382015-08-19 20:35:25 +0000266 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000267 const bool compressor_on = !pressure_switch_->Get();
268 to_log.compressor_on = compressor_on;
269 if (compressor_on) {
270 compressor_relay_->Set(Relay::kForward);
271 } else {
272 compressor_relay_->Set(Relay::kOff);
273 }
274 }
275
276 pcm_->Flush();
277 to_log.read_solenoids = pcm_->GetAll();
278 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
279 }
280 }
281 }
282
283 void Quit() { run_ = false; }
284
285 private:
286 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000287
288 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700289 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
290 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000291 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000292
Austin Schuh39e5abc2015-10-31 18:51:20 -0700293 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000294 ::std::unique_ptr<Relay> compressor_relay_;
295
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800296 ::aos::Queue<::y2015_bot3::control_loops::ElevatorQueue::Output> elevator_;
297 ::aos::Queue<::y2015_bot3::control_loops::IntakeQueue::Output> intake_;
298 ::aos::Queue<::y2015_bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000299
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000300 ::std::atomic<bool> run_{true};
301};
302
Comran Morshede140e382015-08-19 20:35:25 +0000303// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000304class DrivetrainWriter : public LoopOutputHandler {
305 public:
306 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
307 left_drivetrain_talon_ = ::std::move(t);
308 }
309
310 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
311 right_drivetrain_talon_ = ::std::move(t);
312 }
313
314 private:
315 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800316 ::y2015_bot3::control_loops::drivetrain_queue.output.FetchAnother();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000317 }
318
319 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800320 auto &queue = ::y2015_bot3::control_loops::drivetrain_queue.output;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000321 LOG_STRUCT(DEBUG, "will output", *queue);
322 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
323 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
324 }
325
326 virtual void Stop() override {
327 LOG(WARNING, "drivetrain output too old\n");
328 left_drivetrain_talon_->Disable();
329 right_drivetrain_talon_->Disable();
330 }
331
332 ::std::unique_ptr<Talon> left_drivetrain_talon_;
333 ::std::unique_ptr<Talon> right_drivetrain_talon_;
334};
335
Comran Morshede140e382015-08-19 20:35:25 +0000336// Writes out elevator voltages.
337class ElevatorWriter : public LoopOutputHandler {
338 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700339 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
340 elevator_talon1_ = ::std::move(t);
341 }
342 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
343 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000344 }
345
346 private:
347 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800348 ::y2015_bot3::control_loops::elevator_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000349 }
350
351 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800352 auto &queue = ::y2015_bot3::control_loops::elevator_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000353 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700354 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000355 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000356 }
357
358 virtual void Stop() override {
359 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700360 elevator_talon1_->Disable();
361 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000362 }
363
Jasmine Zhou954419a2015-09-12 18:31:31 -0700364 ::std::unique_ptr<Talon> elevator_talon1_;
365 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000366};
367
368// Writes out intake voltages.
369class IntakeWriter : public LoopOutputHandler {
370 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700371 void set_intake_talon1(::std::unique_ptr<Talon> t) {
372 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000373 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700374 void set_intake_talon2(::std::unique_ptr<Talon> t) {
375 intake_talon2_ = ::std::move(t);
376 }
377
Comran Morshede140e382015-08-19 20:35:25 +0000378 private:
379 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800380 ::y2015_bot3::control_loops::intake_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000381 }
382
383 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800384 auto &queue = ::y2015_bot3::control_loops::intake_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000385 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700386 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000387 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000388 }
389
390 virtual void Stop() override {
391 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700392 intake_talon1_->Disable();
393 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000394 }
395
Jasmine Zhou954419a2015-09-12 18:31:31 -0700396 ::std::unique_ptr<Talon> intake_talon1_;
397 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000398};
399
Comran Morshed34f891d2015-09-15 22:04:43 +0000400// Writes out can grabber voltages.
401class CanGrabberWriter : public LoopOutputHandler {
402 public:
403 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
404
405 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
406 can_grabber_talon1_ = ::std::move(t);
407 }
408
409 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
410 can_grabber_talon2_ = ::std::move(t);
411 }
412
413 private:
414 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800415 ::y2015_bot3::autonomous::can_grabber_control.FetchAnother();
Comran Morshed34f891d2015-09-15 22:04:43 +0000416 }
417
418 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800419 auto &queue = ::y2015_bot3::autonomous::can_grabber_control;
Comran Morshed34f891d2015-09-15 22:04:43 +0000420 LOG_STRUCT(DEBUG, "will output", *queue);
421 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
422 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
423 }
424
425 virtual void Stop() override {
426 LOG(WARNING, "Can grabber output too old\n");
427 can_grabber_talon1_->Disable();
428 can_grabber_talon2_->Disable();
429 }
430
431 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
432};
433
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000434// TODO(brian): Replace this with ::std::make_unique once all our toolchains
435// have support.
436template <class T, class... U>
437std::unique_ptr<T> make_unique(U &&... u) {
438 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
439}
440
441class WPILibRobot : public RobotBase {
442 public:
443 ::std::unique_ptr<Encoder> encoder(int index) {
444 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
445 Encoder::k4X);
446 }
447 virtual void StartCompetition() {
448 ::aos::InitNRT();
449 ::aos::SetCurrentThreadName("StartCompetition");
450
451 JoystickSender joystick_sender;
452 ::std::thread joystick_thread(::std::ref(joystick_sender));
453
454 SensorReader reader;
455 LOG(INFO, "Creating the reader\n");
456
Jasmine Zhou954419a2015-09-12 18:31:31 -0700457 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700458 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000459
Jasmine Zhou954419a2015-09-12 18:31:31 -0700460 reader.set_left_encoder(encoder(0));
461 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000462 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000463
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000464 ::std::thread reader_thread(::std::ref(reader));
465 GyroSender gyro_sender;
466 ::std::thread gyro_thread(::std::ref(gyro_sender));
467
468 DrivetrainWriter drivetrain_writer;
469 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000470 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700471 drivetrain_writer.set_right_drivetrain_talon(
472 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000473 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
474
Comran Morshede140e382015-08-19 20:35:25 +0000475 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700476 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
477 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000478 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000479
480 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700481 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
482 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000483 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000484
Comran Morshed34f891d2015-09-15 22:04:43 +0000485 CanGrabberWriter can_grabber_writer;
486 can_grabber_writer.set_can_grabber_talon1(
487 ::std::unique_ptr<Talon>(new Talon(3)));
488 can_grabber_writer.set_can_grabber_talon2(
489 ::std::unique_ptr<Talon>(new Talon(4)));
490 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
491
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000492 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
493 new ::frc971::wpilib::BufferedPcm());
494 SolenoidWriter solenoid_writer(pcm);
495 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
496 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700497 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
498 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
499 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000500 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000501 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
502
503 // Wait forever. Not much else to do...
504 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
505
506 LOG(ERROR, "Exiting WPILibRobot\n");
507
508 joystick_sender.Quit();
509 joystick_thread.join();
510 reader.Quit();
511 reader_thread.join();
512 gyro_sender.Quit();
513 gyro_thread.join();
514
515 drivetrain_writer.Quit();
516 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000517
518 elevator_writer.Quit();
519 elevator_writer_thread.join();
520
521 intake_writer.Quit();
522 intake_writer_thread.join();
523
Comran Morshed34f891d2015-09-15 22:04:43 +0000524 can_grabber_writer.Quit();
525 can_grabber_writer_thread.join();
526
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000527 solenoid_writer.Quit();
528 solenoid_thread.join();
529
530 ::aos::Cleanup();
531 }
532};
533
534} // namespace wpilib
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800535} // namespace y2015_bot3
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000536
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800537START_ROBOT_CLASS(::y2015_bot3::wpilib::WPILibRobot);