blob: 84e8664a8ef33c444692ab3647eb461033976100 [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
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Comran Morshed0d6cf9b2015-06-17 19:29:57 +00007#include <thread>
8#include <mutex>
9#include <functional>
10
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070011#include "Encoder.h"
12#include "Talon.h"
13#include "DriverStation.h"
14#include "AnalogInput.h"
15#include "Compressor.h"
16#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080017#include "frc971/wpilib/wpilib_robot_base.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"
Austin Schuh178d5152016-11-26 14:58:40 -080035#include "frc971/control_loops/drivetrain/drivetrain.h"
36#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000037#include "frc971/wpilib/buffered_pcm.h"
Austin Schuh178d5152016-11-26 14:58:40 -080038#include "frc971/wpilib/buffered_solenoid.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050039#include "frc971/wpilib/dma.h"
Austin Schuh178d5152016-11-26 14:58:40 -080040#include "frc971/wpilib/gyro_sender.h"
41#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/logging.q.h"
43#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/pdp_fetcher.h"
45#include "frc971/wpilib/wpilib_interface.h"
46#include "y2015_bot3/autonomous/auto.q.h"
47#include "y2015_bot3/control_loops/drivetrain/drivetrain_base.h"
48#include "y2015_bot3/control_loops/elevator/elevator.h"
49#include "y2015_bot3/control_loops/elevator/elevator.q.h"
50#include "y2015_bot3/control_loops/intake/intake.h"
51#include "y2015_bot3/control_loops/intake/intake.q.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 Schuh178d5152016-11-26 14:58:40 -080058using ::frc971::control_loops::drivetrain_queue;
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080059using ::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
Austin Schuhf2a50ba2016-12-24 16:16:26 -080070namespace chrono = ::std::chrono;
71
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000072double drivetrain_translate(int32_t in) {
Austin Schuh316ab462015-09-13 04:44:40 +000073 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh178d5152016-11-26 14:58:40 -080074 ::y2015_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000075 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
76}
77
Brian Silverman51091a02015-12-26 15:56:58 -080078double drivetrain_velocity_translate(double in) {
79 return (1.0 / in) / 256.0 /*cpr*/ *
Austin Schuh178d5152016-11-26 14:58:40 -080080 ::y2015_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Brian Silverman51091a02015-12-26 15:56:58 -080081 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
82}
83
Comran Morshede140e382015-08-19 20:35:25 +000084double elevator_translate(int32_t in) {
85 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Austin Schuh6d1ee0c2015-11-21 14:36:04 -080086 ::y2015_bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
87 ::y2015_bot3::control_loops::kElevChainReduction *
88 ::y2015_bot3::control_loops::kElevGearboxOutputRadianDistance;
Comran Morshede140e382015-08-19 20:35:25 +000089}
90
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000091static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000092 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
93 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000094 4.0 /* index pulse = 1/4 cycle */;
95
Comran Morshede140e382015-08-19 20:35:25 +000096// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000097class SensorReader {
98 public:
Brian Silverman39b339e2016-01-03 13:24:22 -080099 SensorReader() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000100 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
101 // we should ever see.
102 filter_.SetPeriodNanoSeconds(
103 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
104 }
105
Comran Morshede140e382015-08-19 20:35:25 +0000106 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000107 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
108 left_encoder_ = ::std::move(left_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800109 left_encoder_->SetMaxPeriod(0.005);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000110 }
111
112 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
113 right_encoder_ = ::std::move(right_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800114 right_encoder_->SetMaxPeriod(0.005);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000115 }
116
Comran Morshede140e382015-08-19 20:35:25 +0000117 // Elevator setters.
118 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
119 filter_.Add(encoder.get());
120 elevator_encoder_ = ::std::move(encoder);
121 }
122
Austin Schuh39e5abc2015-10-31 18:51:20 -0700123 void set_elevator_zeroing_hall_effect(::std::unique_ptr<DigitalInput> hall) {
Comran Morshede140e382015-08-19 20:35:25 +0000124 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000125 }
126
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000127 void set_elevator_tote_sensor(::std::unique_ptr<AnalogInput> tote_sensor) {
128 tote_sensor_ = ::std::move(tote_sensor);
129 }
130
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000131 void operator()() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000132 ::aos::SetCurrentThreadName("SensorReader");
133
134 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700135 ds_ =
136#ifdef WPILIB2015
137 DriverStation::GetInstance();
138#else
139 &DriverStation::GetInstance();
140#endif
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000141
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800142 ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(5),
143 chrono::milliseconds(4));
Brian Silverman5090c432016-01-02 14:44:26 -0800144
145 ::aos::SetCurrentThreadRealtimePriority(40);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000146 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800147 {
148 const int iterations = phased_loop.SleepUntilNext();
149 if (iterations != 1) {
150 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
151 }
152 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000153 RunIteration();
154 }
155 }
156
157 void RunIteration() {
Brian Silverman39b339e2016-01-03 13:24:22 -0800158 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000159
Comran Morshede140e382015-08-19 20:35:25 +0000160 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000161 {
162 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
163 drivetrain_message->right_encoder =
164 -drivetrain_translate(right_encoder_->GetRaw());
165 drivetrain_message->left_encoder =
166 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800167 drivetrain_message->left_speed =
168 drivetrain_velocity_translate(left_encoder_->GetPeriod());
169 drivetrain_message->right_speed =
170 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000171
172 drivetrain_message.Send();
173 }
174
Comran Morshede140e382015-08-19 20:35:25 +0000175 // Elevator
176 {
177 // Update control loop positions.
178 auto elevator_message = elevator_queue.position.MakeMessage();
179 elevator_message->encoder =
180 elevator_translate(elevator_encoder_->GetRaw());
Austin Schuh39e5abc2015-10-31 18:51:20 -0700181 elevator_message->bottom_hall_effect = !zeroing_hall_effect_->Get();
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000182 elevator_message->has_tote = tote_sensor_->GetVoltage() > 2.5;
Comran Morshede140e382015-08-19 20:35:25 +0000183
184 elevator_message.Send();
185 }
Austin Schuha3b2eef2015-09-13 07:52:02 +0000186
187 // Intake
188 {
189 auto intake_message = intake_queue.position.MakeMessage();
190 intake_message.Send();
191 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000192 }
193
194 void Quit() { run_ = false; }
195
196 private:
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000197 int32_t my_pid_;
198 DriverStation *ds_;
199
Comran Morshede140e382015-08-19 20:35:25 +0000200 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
Austin Schuh39e5abc2015-10-31 18:51:20 -0700201 ::std::unique_ptr<DigitalInput> zeroing_hall_effect_;
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000202 ::std::unique_ptr<AnalogInput> tote_sensor_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000203
204 ::std::atomic<bool> run_{true};
205 DigitalGlitchFilter filter_;
206};
207
Comran Morshede140e382015-08-19 20:35:25 +0000208// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000209class SolenoidWriter {
210 public:
Austin Schuhbd01a582015-09-21 00:05:31 +0000211 SolenoidWriter(const ::std::unique_ptr< ::frc971::wpilib::BufferedPcm> &pcm)
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700212 : pcm_(pcm),
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800213 elevator_(".y2015_bot3.control_loops.elevator_queue.output"),
214 intake_(".y2015_bot3.control_loops.intake_queue.output"),
215 can_grabber_control_(".y2015_bot3.autonomous.can_grabber_control") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000216
Austin Schuh39e5abc2015-10-31 18:51:20 -0700217 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000218 pressure_switch_ = ::std::move(pressure_switch);
219 }
220
221 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
222 compressor_relay_ = ::std::move(compressor_relay);
223 }
224
Comran Morshede140e382015-08-19 20:35:25 +0000225 void set_elevator_passive_support(
226 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
227 elevator_passive_support_ = ::std::move(elevator_passive_support);
228 }
229
Austin Schuhbd01a582015-09-21 00:05:31 +0000230 void set_can_grabber(::std::unique_ptr<BufferedSolenoid> can_grabber) {
231 can_grabber_ = ::std::move(can_grabber);
232 }
233
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700234 void set_elevator_can_support(
235 ::std::unique_ptr<BufferedSolenoid> elevator_can_support) {
236 elevator_can_support_ = ::std::move(elevator_can_support);
237 }
238
239 void set_intake_claw(::std::unique_ptr<BufferedSolenoid> intake_claw) {
240 intake_claw_ = ::std::move(intake_claw);
241 }
242
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000243 void operator()() {
244 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800245 ::aos::SetCurrentThreadRealtimePriority(27);
246
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800247 ::aos::time::PhasedLoop phased_loop(chrono::milliseconds(20),
248 chrono::milliseconds(1));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000249
250 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800251 {
252 const int iterations = phased_loop.SleepUntilNext();
253 if (iterations != 1) {
254 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
255 }
256 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000257
Austin Schuhbd01a582015-09-21 00:05:31 +0000258 // Can Grabber
259 {
260 can_grabber_control_.FetchLatest();
261 if (can_grabber_control_.get()) {
262 LOG_STRUCT(DEBUG, "solenoids", *can_grabber_control_);
263 can_grabber_->Set(can_grabber_control_->can_grabbers);
264 }
265 }
266
Comran Morshede140e382015-08-19 20:35:25 +0000267 // Elevator
268 {
269 elevator_.FetchLatest();
270 if (elevator_.get()) {
271 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
Austin Schuha3b2eef2015-09-13 07:52:02 +0000272 elevator_passive_support_->Set(!elevator_->passive_support);
273 elevator_can_support_->Set(!elevator_->can_support);
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700274 }
275 }
276
277 // Intake
278 {
279 intake_.FetchLatest();
280 if (intake_.get()) {
281 LOG_STRUCT(DEBUG, "solenoids", *intake_);
282 intake_claw_->Set(intake_->claw_closed);
Comran Morshede140e382015-08-19 20:35:25 +0000283 }
284 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000285
Comran Morshede140e382015-08-19 20:35:25 +0000286 // Compressor
287 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000288 {
289 ::frc971::wpilib::PneumaticsToLog to_log;
290 {
Comran Morshede140e382015-08-19 20:35:25 +0000291 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000292 const bool compressor_on = !pressure_switch_->Get();
293 to_log.compressor_on = compressor_on;
294 if (compressor_on) {
295 compressor_relay_->Set(Relay::kForward);
296 } else {
297 compressor_relay_->Set(Relay::kOff);
298 }
299 }
300
301 pcm_->Flush();
302 to_log.read_solenoids = pcm_->GetAll();
303 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
304 }
305 }
306 }
307
308 void Quit() { run_ = false; }
309
310 private:
311 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000312
313 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700314 ::std::unique_ptr<BufferedSolenoid> elevator_can_support_;
315 ::std::unique_ptr<BufferedSolenoid> intake_claw_;
Austin Schuhbd01a582015-09-21 00:05:31 +0000316 ::std::unique_ptr<BufferedSolenoid> can_grabber_;
Comran Morshede140e382015-08-19 20:35:25 +0000317
Austin Schuh39e5abc2015-10-31 18:51:20 -0700318 ::std::unique_ptr<DigitalInput> pressure_switch_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000319 ::std::unique_ptr<Relay> compressor_relay_;
320
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800321 ::aos::Queue<::y2015_bot3::control_loops::ElevatorQueue::Output> elevator_;
322 ::aos::Queue<::y2015_bot3::control_loops::IntakeQueue::Output> intake_;
323 ::aos::Queue<::y2015_bot3::autonomous::CanGrabberControl> can_grabber_control_;
Comran Morshede140e382015-08-19 20:35:25 +0000324
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000325 ::std::atomic<bool> run_{true};
326};
327
Comran Morshede140e382015-08-19 20:35:25 +0000328// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000329class DrivetrainWriter : public LoopOutputHandler {
330 public:
331 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
332 left_drivetrain_talon_ = ::std::move(t);
333 }
334
335 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
336 right_drivetrain_talon_ = ::std::move(t);
337 }
338
339 private:
340 virtual void Read() override {
Austin Schuh178d5152016-11-26 14:58:40 -0800341 drivetrain_queue.output.FetchAnother();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000342 }
343
344 virtual void Write() override {
Austin Schuh178d5152016-11-26 14:58:40 -0800345 auto &queue = drivetrain_queue.output;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000346 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800347 left_drivetrain_talon_->SetSpeed(queue->left_voltage / 12.0);
348 right_drivetrain_talon_->SetSpeed(-queue->right_voltage / 12.0);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000349 }
350
351 virtual void Stop() override {
352 LOG(WARNING, "drivetrain output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800353 left_drivetrain_talon_->SetDisabled();
354 right_drivetrain_talon_->SetDisabled();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000355 }
356
357 ::std::unique_ptr<Talon> left_drivetrain_talon_;
358 ::std::unique_ptr<Talon> right_drivetrain_talon_;
359};
360
Comran Morshede140e382015-08-19 20:35:25 +0000361// Writes out elevator voltages.
362class ElevatorWriter : public LoopOutputHandler {
363 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700364 void set_elevator_talon1(::std::unique_ptr<Talon> t) {
365 elevator_talon1_ = ::std::move(t);
366 }
367 void set_elevator_talon2(::std::unique_ptr<Talon> t) {
368 elevator_talon2_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000369 }
370
371 private:
372 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800373 ::y2015_bot3::control_loops::elevator_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000374 }
375
376 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800377 auto &queue = ::y2015_bot3::control_loops::elevator_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000378 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800379 elevator_talon1_->SetSpeed(queue->elevator / 12.0);
380 elevator_talon2_->SetSpeed(-queue->elevator / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000381 }
382
383 virtual void Stop() override {
384 LOG(WARNING, "Elevator output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800385 elevator_talon1_->SetDisabled();
386 elevator_talon2_->SetDisabled();
Comran Morshede140e382015-08-19 20:35:25 +0000387 }
388
Jasmine Zhou954419a2015-09-12 18:31:31 -0700389 ::std::unique_ptr<Talon> elevator_talon1_;
390 ::std::unique_ptr<Talon> elevator_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000391};
392
393// Writes out intake voltages.
394class IntakeWriter : public LoopOutputHandler {
395 public:
Jasmine Zhou954419a2015-09-12 18:31:31 -0700396 void set_intake_talon1(::std::unique_ptr<Talon> t) {
397 intake_talon1_ = ::std::move(t);
Comran Morshede140e382015-08-19 20:35:25 +0000398 }
Jasmine Zhou954419a2015-09-12 18:31:31 -0700399 void set_intake_talon2(::std::unique_ptr<Talon> t) {
400 intake_talon2_ = ::std::move(t);
401 }
402
Comran Morshede140e382015-08-19 20:35:25 +0000403 private:
404 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800405 ::y2015_bot3::control_loops::intake_queue.output.FetchAnother();
Comran Morshede140e382015-08-19 20:35:25 +0000406 }
407
408 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800409 auto &queue = ::y2015_bot3::control_loops::intake_queue.output;
Comran Morshede140e382015-08-19 20:35:25 +0000410 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800411 intake_talon1_->SetSpeed(queue->intake / 12.0);
412 intake_talon2_->SetSpeed(-queue->intake / 12.0);
Comran Morshede140e382015-08-19 20:35:25 +0000413 }
414
415 virtual void Stop() override {
416 LOG(WARNING, "Intake output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800417 intake_talon1_->SetDisabled();
418 intake_talon2_->SetDisabled();
Comran Morshede140e382015-08-19 20:35:25 +0000419 }
420
Jasmine Zhou954419a2015-09-12 18:31:31 -0700421 ::std::unique_ptr<Talon> intake_talon1_;
422 ::std::unique_ptr<Talon> intake_talon2_;
Comran Morshede140e382015-08-19 20:35:25 +0000423};
424
Comran Morshed34f891d2015-09-15 22:04:43 +0000425// Writes out can grabber voltages.
426class CanGrabberWriter : public LoopOutputHandler {
427 public:
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800428 CanGrabberWriter() : LoopOutputHandler(chrono::milliseconds(50)) {}
Comran Morshed34f891d2015-09-15 22:04:43 +0000429
430 void set_can_grabber_talon1(::std::unique_ptr<Talon> t) {
431 can_grabber_talon1_ = ::std::move(t);
432 }
433
434 void set_can_grabber_talon2(::std::unique_ptr<Talon> t) {
435 can_grabber_talon2_ = ::std::move(t);
436 }
437
438 private:
439 virtual void Read() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800440 ::y2015_bot3::autonomous::can_grabber_control.FetchAnother();
Comran Morshed34f891d2015-09-15 22:04:43 +0000441 }
442
443 virtual void Write() override {
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800444 auto &queue = ::y2015_bot3::autonomous::can_grabber_control;
Comran Morshed34f891d2015-09-15 22:04:43 +0000445 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800446 can_grabber_talon1_->SetSpeed(queue->can_grabber_voltage / 12.0);
447 can_grabber_talon2_->SetSpeed(-queue->can_grabber_voltage / 12.0);
Comran Morshed34f891d2015-09-15 22:04:43 +0000448 }
449
450 virtual void Stop() override {
451 LOG(WARNING, "Can grabber output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800452 can_grabber_talon1_->SetDisabled();
453 can_grabber_talon2_->SetDisabled();
Comran Morshed34f891d2015-09-15 22:04:43 +0000454 }
455
456 ::std::unique_ptr<Talon> can_grabber_talon1_, can_grabber_talon2_;
457};
458
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000459// TODO(brian): Replace this with ::std::make_unique once all our toolchains
460// have support.
461template <class T, class... U>
462std::unique_ptr<T> make_unique(U &&... u) {
463 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
464}
465
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800466class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000467 public:
468 ::std::unique_ptr<Encoder> encoder(int index) {
469 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
470 Encoder::k4X);
471 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800472 virtual void Run() {
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000473 ::aos::InitNRT();
474 ::aos::SetCurrentThreadName("StartCompetition");
475
476 JoystickSender joystick_sender;
477 ::std::thread joystick_thread(::std::ref(joystick_sender));
478
Brian Silverman425492b2015-12-30 15:23:55 -0800479 ::frc971::wpilib::PDPFetcher pdp_fetcher;
480 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Brian Silverman39b339e2016-01-03 13:24:22 -0800481 SensorReader reader;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000482
Jasmine Zhou954419a2015-09-12 18:31:31 -0700483 reader.set_elevator_encoder(encoder(6));
Austin Schuh39e5abc2015-10-31 18:51:20 -0700484 reader.set_elevator_zeroing_hall_effect(make_unique<DigitalInput>(6));
Comran Morshede140e382015-08-19 20:35:25 +0000485
Jasmine Zhou954419a2015-09-12 18:31:31 -0700486 reader.set_left_encoder(encoder(0));
487 reader.set_right_encoder(encoder(1));
Austin Schuhd6bc8ba2015-09-13 19:39:38 +0000488 reader.set_elevator_tote_sensor(make_unique<AnalogInput>(0));
Comran Morshede140e382015-08-19 20:35:25 +0000489
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000490 ::std::thread reader_thread(::std::ref(reader));
491 GyroSender gyro_sender;
492 ::std::thread gyro_thread(::std::ref(gyro_sender));
493
494 DrivetrainWriter drivetrain_writer;
495 drivetrain_writer.set_left_drivetrain_talon(
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000496 ::std::unique_ptr<Talon>(new Talon(0)));
Jasmine Zhou954419a2015-09-12 18:31:31 -0700497 drivetrain_writer.set_right_drivetrain_talon(
498 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000499 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
500
Comran Morshede140e382015-08-19 20:35:25 +0000501 ElevatorWriter elevator_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700502 elevator_writer.set_elevator_talon1(::std::unique_ptr<Talon>(new Talon(1)));
503 elevator_writer.set_elevator_talon2(::std::unique_ptr<Talon>(new Talon(6)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000504 ::std::thread elevator_writer_thread(::std::ref(elevator_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000505
506 IntakeWriter intake_writer;
Jasmine Zhou954419a2015-09-12 18:31:31 -0700507 intake_writer.set_intake_talon1(::std::unique_ptr<Talon>(new Talon(2)));
508 intake_writer.set_intake_talon2(::std::unique_ptr<Talon>(new Talon(5)));
Austin Schuha3b2eef2015-09-13 07:52:02 +0000509 ::std::thread intake_writer_thread(::std::ref(intake_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000510
Comran Morshed34f891d2015-09-15 22:04:43 +0000511 CanGrabberWriter can_grabber_writer;
512 can_grabber_writer.set_can_grabber_talon1(
513 ::std::unique_ptr<Talon>(new Talon(3)));
514 can_grabber_writer.set_can_grabber_talon2(
515 ::std::unique_ptr<Talon>(new Talon(4)));
516 ::std::thread can_grabber_writer_thread(::std::ref(can_grabber_writer));
517
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000518 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
519 new ::frc971::wpilib::BufferedPcm());
520 SolenoidWriter solenoid_writer(pcm);
521 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
522 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Jasmine Zhouda77c5f2015-09-12 15:16:10 -0700523 solenoid_writer.set_elevator_passive_support(pcm->MakeSolenoid(0));
524 solenoid_writer.set_elevator_can_support(pcm->MakeSolenoid(1));
525 solenoid_writer.set_intake_claw(pcm->MakeSolenoid(2));
Austin Schuhbd01a582015-09-21 00:05:31 +0000526 solenoid_writer.set_can_grabber(pcm->MakeSolenoid(3));
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000527 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
528
529 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800530 while (true) {
531 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
532 if (r != 0) {
533 PLOG(WARNING, "infinite select failed");
534 } else {
535 PLOG(WARNING, "infinite select succeeded??\n");
536 }
537 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000538
539 LOG(ERROR, "Exiting WPILibRobot\n");
540
541 joystick_sender.Quit();
542 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800543 pdp_fetcher.Quit();
544 pdp_fetcher_thread.join();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000545 reader.Quit();
546 reader_thread.join();
547 gyro_sender.Quit();
548 gyro_thread.join();
549
550 drivetrain_writer.Quit();
551 drivetrain_writer_thread.join();
Austin Schuha3b2eef2015-09-13 07:52:02 +0000552
553 elevator_writer.Quit();
554 elevator_writer_thread.join();
555
556 intake_writer.Quit();
557 intake_writer_thread.join();
558
Comran Morshed34f891d2015-09-15 22:04:43 +0000559 can_grabber_writer.Quit();
560 can_grabber_writer_thread.join();
561
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000562 solenoid_writer.Quit();
563 solenoid_thread.join();
564
565 ::aos::Cleanup();
566 }
567};
568
569} // namespace wpilib
Austin Schuh6d1ee0c2015-11-21 14:36:04 -0800570} // namespace y2015_bot3
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000571
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800572AOS_ROBOT_CLASS(::y2015_bot3::wpilib::WPILibRobot);