blob: 554e0a6d8635290611418fe6859e7b63dea02434 [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"
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -050022#include "PowerDistributionPanel.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070023#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"
Brian Silverman811f8ec2015-12-06 01:29:42 -050036
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080037#include "y2015_bot3/control_loops/drivetrain/drivetrain.q.h"
38#include "y2015_bot3/control_loops/elevator/elevator.q.h"
39#include "y2015_bot3/control_loops/intake/intake.q.h"
40#include "y2015_bot3/autonomous/auto.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050041#include "y2015_bot3/control_loops/drivetrain/drivetrain.h"
42#include "y2015_bot3/control_loops/elevator/elevator.h"
43#include "y2015_bot3/control_loops/intake/intake.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000044
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000045#include "frc971/wpilib/joystick_sender.h"
46#include "frc971/wpilib/loop_output_handler.h"
47#include "frc971/wpilib/buffered_solenoid.h"
48#include "frc971/wpilib/buffered_pcm.h"
49#include "frc971/wpilib/gyro_sender.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000050#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050051#include "frc971/wpilib/wpilib_interface.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000052
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::aos::util::SimpleLogInterval;
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080058using ::y2015_bot3::control_loops::drivetrain_queue;
59using ::y2015_bot3::control_loops::elevator_queue;
60using ::y2015_bot3::control_loops::intake_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000061using ::frc971::wpilib::BufferedPcm;
Comran Morshede140e382015-08-19 20:35:25 +000062using ::frc971::wpilib::BufferedSolenoid;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000063using ::frc971::wpilib::LoopOutputHandler;
64using ::frc971::wpilib::JoystickSender;
65using ::frc971::wpilib::GyroSender;
66
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080067namespace y2015_bot3 {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000068namespace wpilib {
69
70double drivetrain_translate(int32_t in) {
Austin Schuh316ab462015-09-13 04:44:40 +000071 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080072 ::y2015_bot3::control_loops::kDrivetrainEncoderRatio *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000073 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
74}
75
Comran Morshede140e382015-08-19 20:35:25 +000076double elevator_translate(int32_t in) {
77 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080078 ::y2015_bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
79 ::y2015_bot3::control_loops::kElevChainReduction *
80 ::y2015_bot3::control_loops::kElevGearboxOutputRadianDistance;
Comran Morshede140e382015-08-19 20:35:25 +000081}
82
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000083static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000084 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
85 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000086 4.0 /* index pulse = 1/4 cycle */;
87
Comran Morshede140e382015-08-19 20:35:25 +000088// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000089class SensorReader {
90 public:
91 SensorReader() {
92 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
93 // we should ever see.
94 filter_.SetPeriodNanoSeconds(
95 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
96 }
97
Comran Morshede140e382015-08-19 20:35:25 +000098 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000099 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
100 left_encoder_ = ::std::move(left_encoder);
101 }
102
103 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
104 right_encoder_ = ::std::move(right_encoder);
105 }
106
Comran Morshede140e382015-08-19 20:35:25 +0000107 // Elevator setters.
108 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
109 filter_.Add(encoder.get());
110 elevator_encoder_ = ::std::move(encoder);
111 }
112
Austin Schuh39e5abc2015-10-31 18:51:20 -0700113 void set_elevator_zeroing_hall_effect(::std::unique_ptr<DigitalInput> hall) {
Comran Morshede140e382015-08-19 20:35:25 +0000114 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000115 }
116
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000117 void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) {
118 tote_sensor_ = ::std::move(tote_sensor);
119 }
120
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000121 void operator()() {
122 LOG(INFO, "In sensor reader thread\n");
123 ::aos::SetCurrentThreadName("SensorReader");
124
125 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700126 ds_ =
127#ifdef WPILIB2015
128 DriverStation::GetInstance();
129#else
130 &DriverStation::GetInstance();
131#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500132 pdp_.reset(new PowerDistributionPanel());
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000133
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000134 LOG(INFO, "Things are now started\n");
135
136 ::aos::SetCurrentThreadRealtimePriority(kPriority);
137 while (run_) {
138 ::aos::time::PhasedLoopXMS(5, 4000);
139 RunIteration();
140 }
141 }
142
143 void RunIteration() {
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500144 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000145
Comran Morshede140e382015-08-19 20:35:25 +0000146 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000147 {
148 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
149 drivetrain_message->right_encoder =
150 -drivetrain_translate(right_encoder_->GetRaw());
151 drivetrain_message->left_encoder =
152 drivetrain_translate(left_encoder_->GetRaw());
153
154 drivetrain_message.Send();
155 }
156
Comran Morshede140e382015-08-19 20:35:25 +0000157 // Elevator
158 {
159 // Update control loop positions.
160 auto elevator_message = elevator_queue.position.MakeMessage();
161 elevator_message->encoder =
162 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700163 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000164 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000165
166 elevator_message.Send();
167 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000168
169 // Intake
170 {
171 auto intake_message = intake_queue.position.MakeMessage();
172 intake_message.Send();
173 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000174 }
175
176 void Quit() { run_ = false; }
177
178 private:
179 static const int kPriority = 30;
180 static const int kInterruptPriority = 55;
181
182 int32_t my_pid_;
183 DriverStation *ds_;
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500184 ::std::unique_ptr<PowerDistributionPanel> pdp_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000185
Comran Morshede140e382015-08-19 20:35:25 +0000186 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700187 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000188 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000189
190 ::std::atomic<bool> run_{true};
191 DigitalGlitchFilter filter_;
192};
193
Comran Morshede140e382015-08-19 20:35:25 +0000194// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000195class SolenoidWriter {
196 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000197 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700198 : pcm_(pcm),
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800199 elevator_(".y2015_bot3.control_loops.elevator_queue.output"),
200 intake_(".y2015_bot3.control_loops.intake_queue.output"),
201 can_grabber_control_(".y2015_bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000202
Austin Schuh39e5abc2015-10-31 18:51:20 -0700203 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000204 pressure_switch_ = ::std::move(pressure_switch);
205 }
206
207 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
208 compressor_relay_ = ::std::move(compressor_relay);
209 }
210
Comran Morshede140e382015-08-19 20:35:25 +0000211 void set_elevator_passive_support(
212 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
213 elevator_passive_support_ = ::std::move(elevator_passive_support);
214 }
215
Austin Schuhbd01a582015-09-21 00:05:31 +0000216 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
217 can_grabber_ = ::std::move(can_grabber);
218 }
219
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700220 void set_elevator_can_support(
221 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
222 elevator_can_support_ = ::std::move(elevator_can_support);
223 }
224
225 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
226 intake_claw_ = ::std::move(intake_claw);
227 }
228
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000229 void operator()() {
230 ::aos::SetCurrentThreadName("Solenoids");
231 ::aos::SetCurrentThreadRealtimePriority(30);
232
233 while (run_) {
234 ::aos::time::PhasedLoopXMS(20, 1000);
235
Austin Schuhbd01a582015-09-21 00:05:31 +0000236 // Can Grabber
237 {
238 can_grabber_control_.FetchLatest();
239 if (can_grabber_control_.get()) {
240 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
241 can_grabber_->Set(can_grabber_control_->can_grabbers);
242 }
243 }
244
Comran Morshede140e382015-08-19 20:35:25 +0000245 // Elevator
246 {
247 elevator_.FetchLatest();
248 if (elevator_.get()) {
249 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000250 elevator_passive_support_->Set(!elevator_->passive_support);
251 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700252 }
253 }
254
255 // Intake
256 {
257 intake_.FetchLatest();
258 if (intake_.get()) {
259 LOG_STRUCT(DEBUG, "solenoids", *intake_);
260 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000261 }
262 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000263
Comran Morshede140e382015-08-19 20:35:25 +0000264 // Compressor
265 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000266 {
267 ::frc971::wpilib::PneumaticsToLog to_log;
268 {
Comran Morshede140e382015-08-19 20:35:25 +0000269 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000270 const bool compressor_on = !pressure_switch_->Get();
271 to_log.compressor_on = compressor_on;
272 if (compressor_on) {
273 compressor_relay_->Set(Relay::kForward);
274 } else {
275 compressor_relay_->Set(Relay::kOff);
276 }
277 }
278
279 pcm_->Flush();
280 to_log.read_solenoids = pcm_->GetAll();
281 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
282 }
283 }
284 }
285
286 void Quit() { run_ = false; }
287
288 private:
289 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000290
291 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700292 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
293 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000294 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000295
Austin Schuh39e5abc2015-10-31 18:51:20 -0700296 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000297 ::std::unique_ptr<Relay> compressor_relay_;
298
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800299 ::aos::Queue<::y2015_bot3::control_loops::ElevatorQueue::Output> elevator_;
300 ::aos::Queue<::y2015_bot3::control_loops::IntakeQueue::Output> intake_;
301 ::aos::Queue<::y2015_bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000302
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000303 ::std::atomic<bool> run_{true};
304};
305
Comran Morshede140e382015-08-19 20:35:25 +0000306// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000307class DrivetrainWriter : public LoopOutputHandler {
308 public:
309 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
310 left_drivetrain_talon_ = ::std::move(t);
311 }
312
313 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
314 right_drivetrain_talon_ = ::std::move(t);
315 }
316
317 private:
318 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800319 ::y2015_bot3::control_loops::drivetrain_queue.output.FetchAnother();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000320 }
321
322 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800323 auto &queue = ::y2015_bot3::control_loops::drivetrain_queue.output;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000324 LOG_STRUCT(DEBUG, "will output", *queue);
325 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
326 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
327 }
328
329 virtual void Stop() override {
330 LOG(WARNING, "drivetrain output too old\n");
331 left_drivetrain_talon_->Disable();
332 right_drivetrain_talon_->Disable();
333 }
334
335 ::std::unique_ptr<Talon> left_drivetrain_talon_;
336 ::std::unique_ptr<Talon> right_drivetrain_talon_;
337};
338
Comran Morshede140e382015-08-19 20:35:25 +0000339// Writes out elevator voltages.
340class ElevatorWriter : public LoopOutputHandler {
341 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700342 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
343 elevator_talon1_ = ::std::move(t);
344 }
345 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
346 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000347 }
348
349 private:
350 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800351 ::y2015_bot3::control_loops::elevator_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000352 }
353
354 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800355 auto &queue = ::y2015_bot3::control_loops::elevator_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000356 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700357 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000358 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000359 }
360
361 virtual void Stop() override {
362 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700363 elevator_talon1_->Disable();
364 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000365 }
366
Jasmine Zhou954419a2015-09-12 18:31:31 -0700367 ::std::unique_ptr<Talon> elevator_talon1_;
368 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000369};
370
371// Writes out intake voltages.
372class IntakeWriter : public LoopOutputHandler {
373 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700374 void set_intake_talon1(::std::unique_ptr<Talon> t) {
375 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000376 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700377 void set_intake_talon2(::std::unique_ptr<Talon> t) {
378 intake_talon2_ = ::std::move(t);
379 }
380
Comran Morshede140e382015-08-19 20:35:25 +0000381 private:
382 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800383 ::y2015_bot3::control_loops::intake_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000384 }
385
386 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800387 auto &queue = ::y2015_bot3::control_loops::intake_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000388 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700389 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000390 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000391 }
392
393 virtual void Stop() override {
394 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700395 intake_talon1_->Disable();
396 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000397 }
398
Jasmine Zhou954419a2015-09-12 18:31:31 -0700399 ::std::unique_ptr<Talon> intake_talon1_;
400 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000401};
402
Comran Morshed34f891d2015-09-15 22:04:43 +0000403// Writes out can grabber voltages.
404class CanGrabberWriter : public LoopOutputHandler {
405 public:
406 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
407
408 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
409 can_grabber_talon1_ = ::std::move(t);
410 }
411
412 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
413 can_grabber_talon2_ = ::std::move(t);
414 }
415
416 private:
417 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800418 ::y2015_bot3::autonomous::can_grabber_control.FetchAnother();
Comran Morshed34f891d2015-09-15 22:04:43 +0000419 }
420
421 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800422 auto &queue = ::y2015_bot3::autonomous::can_grabber_control;
Comran Morshed34f891d2015-09-15 22:04:43 +0000423 LOG_STRUCT(DEBUG, "will output", *queue);
424 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
425 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
426 }
427
428 virtual void Stop() override {
429 LOG(WARNING, "Can grabber output too old\n");
430 can_grabber_talon1_->Disable();
431 can_grabber_talon2_->Disable();
432 }
433
434 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
435};
436
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000437// TODO(brian): Replace this with ::std::make_unique once all our toolchains
438// have support.
439template <class T, class... U>
440std::unique_ptr<T> make_unique(U &&... u) {
441 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
442}
443
444class WPILibRobot : public RobotBase {
445 public:
446 ::std::unique_ptr<Encoder> encoder(int index) {
447 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
448 Encoder::k4X);
449 }
450 virtual void StartCompetition() {
451 ::aos::InitNRT();
452 ::aos::SetCurrentThreadName("StartCompetition");
453
454 JoystickSender joystick_sender;
455 ::std::thread joystick_thread(::std::ref(joystick_sender));
456
457 SensorReader reader;
458 LOG(INFO, "Creating the reader\n");
459
Jasmine Zhou954419a2015-09-12 18:31:31 -0700460 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700461 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000462
Jasmine Zhou954419a2015-09-12 18:31:31 -0700463 reader.set_left_encoder(encoder(0));
464 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000465 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000466
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000467 ::std::thread reader_thread(::std::ref(reader));
468 GyroSender gyro_sender;
469 ::std::thread gyro_thread(::std::ref(gyro_sender));
470
471 DrivetrainWriter drivetrain_writer;
472 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000473 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700474 drivetrain_writer.set_right_drivetrain_talon(
475 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000476 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
477
Comran Morshede140e382015-08-19 20:35:25 +0000478 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700479 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
480 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000481 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000482
483 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700484 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
485 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000486 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000487
Comran Morshed34f891d2015-09-15 22:04:43 +0000488 CanGrabberWriter can_grabber_writer;
489 can_grabber_writer.set_can_grabber_talon1(
490 ::std::unique_ptr<Talon>(new Talon(3)));
491 can_grabber_writer.set_can_grabber_talon2(
492 ::std::unique_ptr<Talon>(new Talon(4)));
493 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
494
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000495 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
496 new ::frc971::wpilib::BufferedPcm());
497 SolenoidWriter solenoid_writer(pcm);
498 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
499 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700500 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
501 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
502 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000503 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000504 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
505
506 // Wait forever. Not much else to do...
507 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
508
509 LOG(ERROR, "Exiting WPILibRobot\n");
510
511 joystick_sender.Quit();
512 joystick_thread.join();
513 reader.Quit();
514 reader_thread.join();
515 gyro_sender.Quit();
516 gyro_thread.join();
517
518 drivetrain_writer.Quit();
519 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000520
521 elevator_writer.Quit();
522 elevator_writer_thread.join();
523
524 intake_writer.Quit();
525 intake_writer_thread.join();
526
Comran Morshed34f891d2015-09-15 22:04:43 +0000527 can_grabber_writer.Quit();
528 can_grabber_writer_thread.join();
529
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000530 solenoid_writer.Quit();
531 solenoid_thread.join();
532
533 ::aos::Cleanup();
534 }
535};
536
537} // namespace wpilib
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800538} // namespace y2015_bot3
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000539
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800540START_ROBOT_CLASS(::y2015_bot3::wpilib::WPILibRobot);