blob: ad87fa5d9a6fa32ead044378af82d96e676d264c [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"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080016#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070017#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"
Brian Silverman425492b2015-12-30 15:23:55 -080051#include "frc971/wpilib/pdp_fetcher.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
Brian Silverman51091a02015-12-26 15:56:58 -080076double drivetrain_velocity_translate(double in) {
77 return (1.0 / in) / 256.0 /*cpr*/ *
78 ::y2015_bot3::control_loops::kDrivetrainEncoderRatio *
79 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
80}
81
Comran Morshede140e382015-08-19 20:35:25 +000082double elevator_translate(int32_t in) {
83 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080084 ::y2015_bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
85 ::y2015_bot3::control_loops::kElevChainReduction *
86 ::y2015_bot3::control_loops::kElevGearboxOutputRadianDistance;
Comran Morshede140e382015-08-19 20:35:25 +000087}
88
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000089static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000090 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
91 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000092 4.0 /* index pulse = 1/4 cycle */;
93
Comran Morshede140e382015-08-19 20:35:25 +000094// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000095class SensorReader {
96 public:
Brian Silverman39b339e2016-01-03 13:24:22 -080097 SensorReader() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000098 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
99 // we should ever see.
100 filter_.SetPeriodNanoSeconds(
101 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
102 }
103
Comran Morshede140e382015-08-19 20:35:25 +0000104 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000105 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
106 left_encoder_ = ::std::move(left_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800107 left_encoder_->SetMaxPeriod(0.005);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000108 }
109
110 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
111 right_encoder_ = ::std::move(right_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800112 right_encoder_->SetMaxPeriod(0.005);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000113 }
114
Comran Morshede140e382015-08-19 20:35:25 +0000115 // Elevator setters.
116 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
117 filter_.Add(encoder.get());
118 elevator_encoder_ = ::std::move(encoder);
119 }
120
Austin Schuh39e5abc2015-10-31 18:51:20 -0700121 void set_elevator_zeroing_hall_effect(::std::unique_ptr<DigitalInput> hall) {
Comran Morshede140e382015-08-19 20:35:25 +0000122 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000123 }
124
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000125 void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) {
126 tote_sensor_ = ::std::move(tote_sensor);
127 }
128
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000129 void operator()() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000130 ::aos::SetCurrentThreadName("SensorReader");
131
132 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700133 ds_ =
134#ifdef WPILIB2015
135 DriverStation::GetInstance();
136#else
137 &DriverStation::GetInstance();
138#endif
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000139
Brian Silverman5090c432016-01-02 14:44:26 -0800140 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
141 ::aos::time::Time::InMS(4));
142
143 ::aos::SetCurrentThreadRealtimePriority(40);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000144 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800145 {
146 const int iterations = phased_loop.SleepUntilNext();
147 if (iterations != 1) {
148 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
149 }
150 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000151 RunIteration();
152 }
153 }
154
155 void RunIteration() {
Brian Silverman39b339e2016-01-03 13:24:22 -0800156 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000157
Comran Morshede140e382015-08-19 20:35:25 +0000158 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000159 {
160 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
161 drivetrain_message->right_encoder =
162 -drivetrain_translate(right_encoder_->GetRaw());
163 drivetrain_message->left_encoder =
164 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800165 drivetrain_message->left_speed =
166 drivetrain_velocity_translate(left_encoder_->GetPeriod());
167 drivetrain_message->right_speed =
168 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000169
170 drivetrain_message.Send();
171 }
172
Comran Morshede140e382015-08-19 20:35:25 +0000173 // Elevator
174 {
175 // Update control loop positions.
176 auto elevator_message = elevator_queue.position.MakeMessage();
177 elevator_message->encoder =
178 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700179 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000180 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000181
182 elevator_message.Send();
183 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000184
185 // Intake
186 {
187 auto intake_message = intake_queue.position.MakeMessage();
188 intake_message.Send();
189 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000190 }
191
192 void Quit() { run_ = false; }
193
194 private:
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000195 int32_t my_pid_;
196 DriverStation *ds_;
197
Comran Morshede140e382015-08-19 20:35:25 +0000198 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700199 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000200 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000201
202 ::std::atomic<bool> run_{true};
203 DigitalGlitchFilter filter_;
204};
205
Comran Morshede140e382015-08-19 20:35:25 +0000206// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000207class SolenoidWriter {
208 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000209 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700210 : pcm_(pcm),
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800211 elevator_(".y2015_bot3.control_loops.elevator_queue.output"),
212 intake_(".y2015_bot3.control_loops.intake_queue.output"),
213 can_grabber_control_(".y2015_bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000214
Austin Schuh39e5abc2015-10-31 18:51:20 -0700215 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000216 pressure_switch_ = ::std::move(pressure_switch);
217 }
218
219 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
220 compressor_relay_ = ::std::move(compressor_relay);
221 }
222
Comran Morshede140e382015-08-19 20:35:25 +0000223 void set_elevator_passive_support(
224 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
225 elevator_passive_support_ = ::std::move(elevator_passive_support);
226 }
227
Austin Schuhbd01a582015-09-21 00:05:31 +0000228 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
229 can_grabber_ = ::std::move(can_grabber);
230 }
231
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700232 void set_elevator_can_support(
233 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
234 elevator_can_support_ = ::std::move(elevator_can_support);
235 }
236
237 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
238 intake_claw_ = ::std::move(intake_claw);
239 }
240
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000241 void operator()() {
242 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800243 ::aos::SetCurrentThreadRealtimePriority(27);
244
245 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
246 ::aos::time::Time::InMS(1));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000247
248 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800249 {
250 const int iterations = phased_loop.SleepUntilNext();
251 if (iterations != 1) {
252 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
253 }
254 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000255
Austin Schuhbd01a582015-09-21 00:05:31 +0000256 // Can Grabber
257 {
258 can_grabber_control_.FetchLatest();
259 if (can_grabber_control_.get()) {
260 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
261 can_grabber_->Set(can_grabber_control_->can_grabbers);
262 }
263 }
264
Comran Morshede140e382015-08-19 20:35:25 +0000265 // Elevator
266 {
267 elevator_.FetchLatest();
268 if (elevator_.get()) {
269 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000270 elevator_passive_support_->Set(!elevator_->passive_support);
271 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700272 }
273 }
274
275 // Intake
276 {
277 intake_.FetchLatest();
278 if (intake_.get()) {
279 LOG_STRUCT(DEBUG, "solenoids", *intake_);
280 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000281 }
282 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000283
Comran Morshede140e382015-08-19 20:35:25 +0000284 // Compressor
285 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000286 {
287 ::frc971::wpilib::PneumaticsToLog to_log;
288 {
Comran Morshede140e382015-08-19 20:35:25 +0000289 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000290 const bool compressor_on = !pressure_switch_->Get();
291 to_log.compressor_on = compressor_on;
292 if (compressor_on) {
293 compressor_relay_->Set(Relay::kForward);
294 } else {
295 compressor_relay_->Set(Relay::kOff);
296 }
297 }
298
299 pcm_->Flush();
300 to_log.read_solenoids = pcm_->GetAll();
301 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
302 }
303 }
304 }
305
306 void Quit() { run_ = false; }
307
308 private:
309 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000310
311 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700312 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
313 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000314 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000315
Austin Schuh39e5abc2015-10-31 18:51:20 -0700316 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000317 ::std::unique_ptr<Relay> compressor_relay_;
318
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800319 ::aos::Queue<::y2015_bot3::control_loops::ElevatorQueue::Output> elevator_;
320 ::aos::Queue<::y2015_bot3::control_loops::IntakeQueue::Output> intake_;
321 ::aos::Queue<::y2015_bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000322
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000323 ::std::atomic<bool> run_{true};
324};
325
Comran Morshede140e382015-08-19 20:35:25 +0000326// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000327class DrivetrainWriter : public LoopOutputHandler {
328 public:
329 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
330 left_drivetrain_talon_ = ::std::move(t);
331 }
332
333 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
334 right_drivetrain_talon_ = ::std::move(t);
335 }
336
337 private:
338 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800339 ::y2015_bot3::control_loops::drivetrain_queue.output.FetchAnother();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000340 }
341
342 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800343 auto &queue = ::y2015_bot3::control_loops::drivetrain_queue.output;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000344 LOG_STRUCT(DEBUG, "will output", *queue);
345 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
346 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
347 }
348
349 virtual void Stop() override {
350 LOG(WARNING, "drivetrain output too old\n");
351 left_drivetrain_talon_->Disable();
352 right_drivetrain_talon_->Disable();
353 }
354
355 ::std::unique_ptr<Talon> left_drivetrain_talon_;
356 ::std::unique_ptr<Talon> right_drivetrain_talon_;
357};
358
Comran Morshede140e382015-08-19 20:35:25 +0000359// Writes out elevator voltages.
360class ElevatorWriter : public LoopOutputHandler {
361 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700362 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
363 elevator_talon1_ = ::std::move(t);
364 }
365 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
366 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000367 }
368
369 private:
370 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800371 ::y2015_bot3::control_loops::elevator_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000372 }
373
374 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800375 auto &queue = ::y2015_bot3::control_loops::elevator_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000376 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700377 elevator_talon1_->Set(queue->elevator / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000378 elevator_talon2_->Set(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000379 }
380
381 virtual void Stop() override {
382 LOG(WARNING, "Elevator output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700383 elevator_talon1_->Disable();
384 elevator_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000385 }
386
Jasmine Zhou954419a2015-09-12 18:31:31 -0700387 ::std::unique_ptr<Talon> elevator_talon1_;
388 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000389};
390
391// Writes out intake voltages.
392class IntakeWriter : public LoopOutputHandler {
393 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700394 void set_intake_talon1(::std::unique_ptr<Talon> t) {
395 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000396 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700397 void set_intake_talon2(::std::unique_ptr<Talon> t) {
398 intake_talon2_ = ::std::move(t);
399 }
400
Comran Morshede140e382015-08-19 20:35:25 +0000401 private:
402 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800403 ::y2015_bot3::control_loops::intake_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000404 }
405
406 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800407 auto &queue = ::y2015_bot3::control_loops::intake_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000408 LOG_STRUCT(DEBUG, "will output", *queue);
Jasmine Zhou954419a2015-09-12 18:31:31 -0700409 intake_talon1_->Set(queue->intake / 12.0);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000410 intake_talon2_->Set(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000411 }
412
413 virtual void Stop() override {
414 LOG(WARNING, "Intake output too old.\n");
Jasmine Zhou954419a2015-09-12 18:31:31 -0700415 intake_talon1_->Disable();
416 intake_talon2_->Disable();
Comran Morshede140e382015-08-19 20:35:25 +0000417 }
418
Jasmine Zhou954419a2015-09-12 18:31:31 -0700419 ::std::unique_ptr<Talon> intake_talon1_;
420 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000421};
422
Comran Morshed34f891d2015-09-15 22:04:43 +0000423// Writes out can grabber voltages.
424class CanGrabberWriter : public LoopOutputHandler {
425 public:
426 CanGrabberWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.05)) {}
427
428 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
429 can_grabber_talon1_ = ::std::move(t);
430 }
431
432 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
433 can_grabber_talon2_ = ::std::move(t);
434 }
435
436 private:
437 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800438 ::y2015_bot3::autonomous::can_grabber_control.FetchAnother();
Comran Morshed34f891d2015-09-15 22:04:43 +0000439 }
440
441 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800442 auto &queue = ::y2015_bot3::autonomous::can_grabber_control;
Comran Morshed34f891d2015-09-15 22:04:43 +0000443 LOG_STRUCT(DEBUG, "will output", *queue);
444 can_grabber_talon1_->Set(queue->can_grabber_voltage / 12.0);
445 can_grabber_talon2_->Set(-queue->can_grabber_voltage / 12.0);
446 }
447
448 virtual void Stop() override {
449 LOG(WARNING, "Can grabber output too old\n");
450 can_grabber_talon1_->Disable();
451 can_grabber_talon2_->Disable();
452 }
453
454 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
455};
456
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000457// TODO(brian): Replace this with ::std::make_unique once all our toolchains
458// have support.
459template <class T, class... U>
460std::unique_ptr<T> make_unique(U &&... u) {
461 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
462}
463
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800464class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000465 public:
466 ::std::unique_ptr<Encoder> encoder(int index) {
467 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
468 Encoder::k4X);
469 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800470 virtual void Run() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000471 ::aos::InitNRT();
472 ::aos::SetCurrentThreadName("StartCompetition");
473
474 JoystickSender joystick_sender;
475 ::std::thread joystick_thread(::std::ref(joystick_sender));
476
Brian Silverman425492b2015-12-30 15:23:55 -0800477 ::frc971::wpilib::PDPFetcher pdp_fetcher;
478 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Brian Silverman39b339e2016-01-03 13:24:22 -0800479 SensorReader reader;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000480
Jasmine Zhou954419a2015-09-12 18:31:31 -0700481 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700482 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000483
Jasmine Zhou954419a2015-09-12 18:31:31 -0700484 reader.set_left_encoder(encoder(0));
485 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000486 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000487
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000488 ::std::thread reader_thread(::std::ref(reader));
489 GyroSender gyro_sender;
490 ::std::thread gyro_thread(::std::ref(gyro_sender));
491
492 DrivetrainWriter drivetrain_writer;
493 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000494 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700495 drivetrain_writer.set_right_drivetrain_talon(
496 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000497 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
498
Comran Morshede140e382015-08-19 20:35:25 +0000499 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700500 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
501 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000502 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000503
504 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700505 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
506 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000507 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000508
Comran Morshed34f891d2015-09-15 22:04:43 +0000509 CanGrabberWriter can_grabber_writer;
510 can_grabber_writer.set_can_grabber_talon1(
511 ::std::unique_ptr<Talon>(new Talon(3)));
512 can_grabber_writer.set_can_grabber_talon2(
513 ::std::unique_ptr<Talon>(new Talon(4)));
514 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
515
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000516 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
517 new ::frc971::wpilib::BufferedPcm());
518 SolenoidWriter solenoid_writer(pcm);
519 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
520 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700521 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
522 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
523 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000524 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000525 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
526
527 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800528 while (true) {
529 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
530 if (r != 0) {
531 PLOG(WARNING, "infinite select failed");
532 } else {
533 PLOG(WARNING, "infinite select succeeded??\n");
534 }
535 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000536
537 LOG(ERROR, "Exiting WPILibRobot\n");
538
539 joystick_sender.Quit();
540 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800541 pdp_fetcher.Quit();
542 pdp_fetcher_thread.join();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000543 reader.Quit();
544 reader_thread.join();
545 gyro_sender.Quit();
546 gyro_thread.join();
547
548 drivetrain_writer.Quit();
549 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000550
551 elevator_writer.Quit();
552 elevator_writer_thread.join();
553
554 intake_writer.Quit();
555 intake_writer_thread.join();
556
Comran Morshed34f891d2015-09-15 22:04:43 +0000557 can_grabber_writer.Quit();
558 can_grabber_writer_thread.join();
559
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000560 solenoid_writer.Quit();
561 solenoid_thread.join();
562
563 ::aos::Cleanup();
564 }
565};
566
567} // namespace wpilib
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800568} // namespace y2015_bot3
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000569
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800570AOS_ROBOT_CLASS(::y2015_bot3::wpilib::WPILibRobot);