blob: b6a08deebc1a77b9024e3fa921e95545ebd69da3 [file] [log] [blame]
Austin Schuh010eb812014-10-25 18:06:49 -07001#include <stdio.h>
2#include <string.h>
Austin Schuh010eb812014-10-25 18:06:49 -07003#include <unistd.h>
4#include <inttypes.h>
5
Brian Silvermand8f403a2014-12-13 19:12:04 -05006#include <thread>
7#include <mutex>
8#include <functional>
9
Austin Schuh010eb812014-10-25 18:06:49 -070010#include "aos/common/logging/logging.h"
11#include "aos/common/logging/queue_logging.h"
Austin Schuh010eb812014-10-25 18:06:49 -070012#include "aos/common/time.h"
13#include "aos/common/util/log_interval.h"
14#include "aos/common/util/phased_loop.h"
15#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040016#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070017#include "aos/linux_code/init.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050018#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070019
Daniel Pettia7827412015-02-13 20:55:57 -080020#include "frc971/constants.h"
Brian Silverman335c20e2015-01-26 21:47:58 -050021#include "frc971/control_loops/control_loops.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070022#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Daniel Pettiadf38432015-01-26 17:13:35 -080023#include "frc971/control_loops/fridge/fridge.q.h"
24#include "frc971/control_loops/claw/claw.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070025
Brian Silvermanda45b6c2014-12-28 11:36:50 -080026#include "frc971/wpilib/hall_effect.h"
27#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050028#include "frc971/wpilib/loop_output_handler.h"
29#include "frc971/wpilib/buffered_solenoid.h"
30#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080031#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050032#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050033#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050034#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080035
Brian Silvermancb77f232014-12-19 21:48:36 -080036#include "Encoder.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080037#include "Talon.h"
38#include "DriverStation.h"
39#include "AnalogInput.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080040#include "Compressor.h"
41#include "RobotBase.h"
Brian Silverman335c20e2015-01-26 21:47:58 -050042#include "dma.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050043#include "ControllerPower.h"
Austin Schuh010eb812014-10-25 18:06:49 -070044
45#ifndef M_PI
46#define M_PI 3.14159265358979323846
47#endif
48
49using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050050using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050051using ::frc971::control_loops::fridge_queue;
52using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070053
54namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080055namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070056
Austin Schuh010eb812014-10-25 18:06:49 -070057double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080058 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080059 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080060 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080061 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
62}
63
64double arm_translate(int32_t in) {
65 return static_cast<double>(in) /
66 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080067 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080068 (2 * M_PI /*radians*/);
69}
70
Brian Silverman8bca4a92015-02-05 15:19:06 -050071double arm_pot_translate(double voltage) {
72 return voltage /
Daniel Pettia7827412015-02-13 20:55:57 -080073 constants::GetValues().arm_pot_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080074 (5.0 /*volts*/ / 5.0 /*turns*/) *
75 (2 * M_PI /*radians*/);
76}
77
78double elevator_translate(int32_t in) {
79 return static_cast<double>(in) /
80 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080081 constants::GetValues().elev_encoder_ratio *
82 (2 * M_PI /*radians*/) *
83 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080084}
85
Brian Silverman8bca4a92015-02-05 15:19:06 -050086double elevator_pot_translate(double voltage) {
87 return voltage /
Daniel Pettia7827412015-02-13 20:55:57 -080088 constants::GetValues().elev_pot_ratio *
89 (2 * M_PI /*radians*/) *
90 constants::GetValues().elev_distance_per_radian *
Daniel Pettiadf38432015-01-26 17:13:35 -080091 (5.0 /*volts*/ / 5.0 /*turns*/);
92}
93
94double claw_translate(int32_t in) {
95 return static_cast<double>(in) /
96 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080097 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080098 (2 * M_PI /*radians*/);
99}
100
Brian Silverman8bca4a92015-02-05 15:19:06 -0500101double claw_pot_translate(double voltage) {
102 return voltage /
Daniel Pettia7827412015-02-13 20:55:57 -0800103 constants::GetValues().claw_pot_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800104 (5.0 /*volts*/ / 5.0 /*turns*/) *
105 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700106}
107
Brian Silverman335c20e2015-01-26 21:47:58 -0500108static const double kMaximumEncoderPulsesPerSecond =
109 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
110 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
111 4.0 /* index pulse = 1/4 cycle */;
112
Austin Schuh010eb812014-10-25 18:06:49 -0700113class SensorReader {
114 public:
Brian Silverman1f90d672015-01-26 20:20:45 -0500115 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500116 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
117 // we should ever see.
118 filter_.SetPeriodNanoSeconds(
119 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
120 }
121
122 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
123 filter_.Add(encoder.get());
124 arm_left_encoder_.set_encoder(::std::move(encoder));
125 }
126
127 void set_arm_left_index(::std::unique_ptr<DigitalSource> index) {
128 filter_.Add(index.get());
129 arm_left_encoder_.set_index(::std::move(index));
130 }
131
132 void set_arm_left_potentiometer(
133 ::std::unique_ptr<AnalogInput> potentiometer) {
134 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
135 }
136
137 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
138 filter_.Add(encoder.get());
139 arm_right_encoder_.set_encoder(::std::move(encoder));
140 }
141
142 void set_arm_right_index(::std::unique_ptr<DigitalSource> index) {
143 filter_.Add(index.get());
144 arm_right_encoder_.set_index(::std::move(index));
145 }
146
147 void set_arm_right_potentiometer(
148 ::std::unique_ptr<AnalogInput> potentiometer) {
149 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
150 }
151
152 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
153 filter_.Add(encoder.get());
154 elevator_left_encoder_.set_encoder(::std::move(encoder));
155 }
156
157 void set_elevator_left_index(::std::unique_ptr<DigitalSource> index) {
158 filter_.Add(index.get());
159 elevator_left_encoder_.set_index(::std::move(index));
160 }
161
162 void set_elevator_left_potentiometer(
163 ::std::unique_ptr<AnalogInput> potentiometer) {
164 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
165 }
166
167 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
168 filter_.Add(encoder.get());
169 elevator_right_encoder_.set_encoder(::std::move(encoder));
170 }
171
172 void set_elevator_right_index(::std::unique_ptr<DigitalSource> index) {
173 filter_.Add(index.get());
174 elevator_right_encoder_.set_index(::std::move(index));
175 }
176
177 void set_elevator_right_potentiometer(
178 ::std::unique_ptr<AnalogInput> potentiometer) {
179 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
180 }
181
182 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
183 filter_.Add(encoder.get());
184 wrist_encoder_.set_encoder(::std::move(encoder));
185 }
186
187 void set_wrist_index(::std::unique_ptr<DigitalSource> index) {
188 filter_.Add(index.get());
189 wrist_encoder_.set_index(::std::move(index));
190 }
191
192 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
193 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700194 }
195
Brian Silverman1f90d672015-01-26 20:20:45 -0500196 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
197 left_encoder_ = ::std::move(left_encoder);
198 }
199
200 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
201 right_encoder_ = ::std::move(right_encoder);
202 }
203
Brian Silverman335c20e2015-01-26 21:47:58 -0500204 // All of the DMA-related set_* calls must be made before this, and it doesn't
205 // hurt to do all of them.
206 void set_dma(::std::unique_ptr<DMA> dma) {
207 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
208 dma_synchronizer_->Add(&arm_left_encoder_);
209 dma_synchronizer_->Add(&arm_right_encoder_);
210 dma_synchronizer_->Add(&elevator_left_encoder_);
211 dma_synchronizer_->Add(&elevator_right_encoder_);
212 }
213
Austin Schuh010eb812014-10-25 18:06:49 -0700214 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -0800215 ::aos::SetCurrentThreadName("SensorReader");
216
Brian Silverman699f0cb2015-02-05 19:45:01 -0500217 my_pid_ = getpid();
218 ds_ = DriverStation::GetInstance();
219
Brian Silverman335c20e2015-01-26 21:47:58 -0500220 wrist_encoder_.Start();
221 dma_synchronizer_->Start();
Austin Schuh010eb812014-10-25 18:06:49 -0700222
Brian Silverman2fe007c2014-12-28 12:20:01 -0800223 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700224 while (run_) {
Brian Silverman20141f92015-01-05 17:39:01 -0800225 ::aos::time::PhasedLoopXMS(5, 9000);
Austin Schuh010eb812014-10-25 18:06:49 -0700226 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700227 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500228
229 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700230 }
231
232 void RunIteration() {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500233 {
234 auto new_state = ::aos::robot_state.MakeMessage();
Austin Schuh010eb812014-10-25 18:06:49 -0700235
Brian Silverman699f0cb2015-02-05 19:45:01 -0500236 new_state->outputs_enabled = ds_->IsSysActive();
237 new_state->browned_out = ds_->IsSysBrownedOut();
238
239 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
240 new_state->is_5v_active = ControllerPower::GetEnabled5V();
241 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
242 new_state->voltage_5v = ControllerPower::GetVoltage5V();
243
244 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
245 new_state->voltage_battery = ds_->GetBatteryVoltage();
246
247 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700248 }
249
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500250 drivetrain_queue.position.MakeWithBuilder()
Austin Schuh010eb812014-10-25 18:06:49 -0700251 .right_encoder(drivetrain_translate(right_encoder_->GetRaw()))
252 .left_encoder(-drivetrain_translate(left_encoder_->GetRaw()))
Austin Schuh010eb812014-10-25 18:06:49 -0700253 .Send();
Brian Silverman335c20e2015-01-26 21:47:58 -0500254
255 dma_synchronizer_->RunIteration();
256
257 {
258 auto fridge_message = fridge_queue.position.MakeMessage();
259 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
260 arm_translate, arm_pot_translate, false);
261 CopyPotAndIndexPosition(arm_right_encoder_, &fridge_message->arm.right,
262 arm_translate, arm_pot_translate, true);
263 CopyPotAndIndexPosition(
264 elevator_left_encoder_, &fridge_message->elevator.left,
265 elevator_translate, elevator_pot_translate, false);
266 CopyPotAndIndexPosition(elevator_right_encoder_,
267 &fridge_message->elevator.right,
268 elevator_translate, elevator_pot_translate, true);
269 fridge_message.Send();
270 }
271
272 {
273 auto claw_message = claw_queue.position.MakeMessage();
274 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
275 claw_translate, claw_pot_translate, false);
276 claw_message.Send();
277 }
Austin Schuh010eb812014-10-25 18:06:49 -0700278 }
279
280 void Quit() { run_ = false; }
281
282 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500283 static const int kPriority = 30;
284 static const int kInterruptPriority = 55;
285
Brian Silverman699f0cb2015-02-05 19:45:01 -0500286 int32_t my_pid_;
287 DriverStation *ds_;
288
Brian Silverman335c20e2015-01-26 21:47:58 -0500289 void CopyPotAndIndexPosition(
290 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
291 ::std::function<double(int32_t)> encoder_translate,
292 ::std::function<double(double)> pot_translate, bool reverse) {
293 const double multiplier = reverse ? -1.0 : 1.0;
294 position->encoder =
295 multiplier * encoder_translate(encoder.polled_encoder_value());
296 position->pot =
297 multiplier * pot_translate(encoder.polled_potentiometer_voltage());
298 position->latched_encoder =
299 multiplier * encoder_translate(encoder.last_encoder_value());
300 position->latched_pot =
301 multiplier * pot_translate(encoder.last_potentiometer_voltage());
302 position->index_pulses = encoder.index_posedge_count();
303 }
304
305 void CopyPotAndIndexPosition(
306 const InterruptEncoderAndPotentiometer &encoder,
307 PotAndIndexPosition *position,
308 ::std::function<double(int32_t)> encoder_translate,
309 ::std::function<double(double)> pot_translate, bool reverse) {
310 const double multiplier = reverse ? -1.0 : 1.0;
311 position->encoder =
312 multiplier * encoder_translate(encoder.encoder()->GetRaw());
313 position->pot =
314 multiplier * pot_translate(encoder.potentiometer()->GetVoltage());
315 position->latched_encoder =
316 multiplier * encoder_translate(encoder.last_encoder_value());
317 position->latched_pot =
318 multiplier * pot_translate(encoder.last_potentiometer_voltage());
319 position->index_pulses = encoder.index_posedge_count();
320 }
321
322
323 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
324
325 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
326 elevator_left_encoder_, elevator_right_encoder_;
327
328 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
329
Austin Schuh010eb812014-10-25 18:06:49 -0700330 ::std::unique_ptr<Encoder> left_encoder_;
331 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700332
Brian Silverman1f90d672015-01-26 20:20:45 -0500333 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700334 DigitalGlitchFilter filter_;
335};
336
Brian Silvermand8f403a2014-12-13 19:12:04 -0500337class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700338 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500339 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800340 : pcm_(pcm),
341 fridge_(".frc971.control_loops.fridge.output"),
342 claw_(".frc971.control_loops.claw.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500343
Daniel Pettiadf38432015-01-26 17:13:35 -0800344 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
345 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700346 }
347
Daniel Pettiadf38432015-01-26 17:13:35 -0800348 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
349 fridge_grabbers_top_back_ = ::std::move(s);
350 }
351
352 void set_fridge_grabbers_bottom_front(
353 ::std::unique_ptr<BufferedSolenoid> s) {
354 fridge_grabbers_bottom_front_ = ::std::move(s);
355 }
356
357 void set_fridge_grabbers_bottom_back(
358 ::std::unique_ptr<BufferedSolenoid> s) {
359 fridge_grabbers_bottom_back_ = ::std::move(s);
360 }
361
362 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
363 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500364 }
Austin Schuh010eb812014-10-25 18:06:49 -0700365
Brian Silvermand8f403a2014-12-13 19:12:04 -0500366 void operator()() {
367 ::aos::SetCurrentThreadName("Solenoids");
368 ::aos::SetCurrentThreadRealtimePriority(30);
369
370 while (run_) {
371 ::aos::time::PhasedLoopXMS(20, 1000);
372
373 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800374 fridge_.FetchLatest();
375 if (fridge_.get()) {
376 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
377 fridge_grabbers_top_front_->Set(fridge_->grabbers.top_front);
378 fridge_grabbers_top_back_->Set(fridge_->grabbers.top_back);
379 fridge_grabbers_bottom_front_->Set(fridge_->grabbers.bottom_front);
380 fridge_grabbers_bottom_back_->Set(fridge_->grabbers.bottom_back);
381 }
382 }
383
384 {
385 claw_.FetchLatest();
386 if (claw_.get()) {
387 LOG_STRUCT(DEBUG, "solenoids", *claw_);
388 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500389 }
390 }
391
Brian Silvermand8f403a2014-12-13 19:12:04 -0500392 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700393 }
394 }
395
Brian Silvermand8f403a2014-12-13 19:12:04 -0500396 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700397
Brian Silvermand8f403a2014-12-13 19:12:04 -0500398 private:
399 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800400 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
401 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
402 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
403 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
404 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Austin Schuh010eb812014-10-25 18:06:49 -0700405
Daniel Pettiadf38432015-01-26 17:13:35 -0800406 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
407 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700408
Brian Silvermand8f403a2014-12-13 19:12:04 -0500409 ::std::atomic<bool> run_{true};
410};
411
412class DrivetrainWriter : public LoopOutputHandler {
413 public:
414 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
415 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700416 }
417
Brian Silvermand8f403a2014-12-13 19:12:04 -0500418 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
419 right_drivetrain_talon_ = ::std::move(t);
420 }
Austin Schuh010eb812014-10-25 18:06:49 -0700421
Brian Silvermand8f403a2014-12-13 19:12:04 -0500422 private:
423 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500424 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500425 }
426
427 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500428 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500429 LOG_STRUCT(DEBUG, "will output", *queue);
430 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
431 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
432 }
433
434 virtual void Stop() override {
435 LOG(WARNING, "drivetrain output too old\n");
436 left_drivetrain_talon_->Disable();
437 right_drivetrain_talon_->Disable();
438 }
439
Austin Schuh010eb812014-10-25 18:06:49 -0700440 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500441 ::std::unique_ptr<Talon> right_drivetrain_talon_;
442};
443
Daniel Pettiadf38432015-01-26 17:13:35 -0800444class FridgeWriter : public LoopOutputHandler {
445 public:
446 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
447 left_arm_talon_ = ::std::move(t);
448 }
449
450 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
451 right_arm_talon_ = ::std::move(t);
452 }
453
454 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
455 left_elevator_talon_ = ::std::move(t);
456 }
457
458 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
459 right_elevator_talon_ = ::std::move(t);
460 }
461
462 private:
463 virtual void Read() override {
464 ::frc971::control_loops::fridge_queue.output.FetchAnother();
465 }
466
467 virtual void Write() override {
468 auto &queue = ::frc971::control_loops::fridge_queue.output;
469 LOG_STRUCT(DEBUG, "will output", *queue);
470 left_arm_talon_->Set(-queue->left_arm / 12.0);
471 right_arm_talon_->Set(queue->right_arm / 12.0);
472 left_elevator_talon_->Set(-queue->left_elevator / 12.0);
473 right_elevator_talon_->Set(queue->right_elevator / 12.0);
474 }
475
476 virtual void Stop() override {
477 LOG(WARNING, "Fridge output too old.\n");
478 left_arm_talon_->Disable();
479 right_arm_talon_->Disable();
480 left_elevator_talon_->Disable();
481 right_elevator_talon_->Disable();
482 }
483
484 ::std::unique_ptr<Talon> left_arm_talon_;
485 ::std::unique_ptr<Talon> right_arm_talon_;
486 ::std::unique_ptr<Talon> left_elevator_talon_;
487 ::std::unique_ptr<Talon> right_elevator_talon_;
488};
489
490class ClawWriter : public LoopOutputHandler {
491 public:
492 void set_intake_talon(::std::unique_ptr<Talon> t) {
493 intake_talon_ = ::std::move(t);
494 }
495
496 void set_wrist_talon(::std::unique_ptr<Talon> t) {
497 wrist_talon_ = ::std::move(t);
498 }
499
500 private:
501 virtual void Read() override {
502 ::frc971::control_loops::claw_queue.output.FetchAnother();
503 }
504
505 virtual void Write() override {
506 auto &queue = ::frc971::control_loops::claw_queue.output;
507 LOG_STRUCT(DEBUG, "will output", *queue);
508 intake_talon_->Set(queue->intake_voltage / 12.0);
509 wrist_talon_->Set(queue->voltage / 12.0);
510 }
511
512 virtual void Stop() override {
513 LOG(WARNING, "Claw output too old.\n");
514 intake_talon_->Disable();
515 wrist_talon_->Disable();
516 }
517
518 ::std::unique_ptr<Talon> intake_talon_;
519 ::std::unique_ptr<Talon> wrist_talon_;
520};
521
Brian Silverman1f90d672015-01-26 20:20:45 -0500522// TODO(brian): Replace this with ::std::make_unique once all our toolchains
523// have support.
524template <class T, class... U>
525std::unique_ptr<T> make_unique(U &&... u) {
526 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
527}
528
Austin Schuh010eb812014-10-25 18:06:49 -0700529class WPILibRobot : public RobotBase {
530 public:
531 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500532 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800533 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500534
Brian Silverman98f6ee22015-01-26 17:50:12 -0500535 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700536 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500537 ::std::unique_ptr<Compressor> compressor(new Compressor());
538 compressor->SetClosedLoopControl(true);
539
Brian Silverman98f6ee22015-01-26 17:50:12 -0500540 SensorReader reader;
Brian Silverman1f90d672015-01-26 20:20:45 -0500541 // TODO(sensors): Replace all the 99s with real port numbers.
Brian Silverman335c20e2015-01-26 21:47:58 -0500542 reader.set_arm_left_encoder(
543 make_unique<Encoder>(99, 99, false, Encoder::k4X));
544 reader.set_arm_left_index(make_unique<DigitalInput>(99));
545 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(99));
546 reader.set_arm_right_encoder(
547 make_unique<Encoder>(99, 99, false, Encoder::k4X));
548 reader.set_arm_right_index(make_unique<DigitalInput>(99));
549 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(99));
550 reader.set_elevator_left_encoder(
551 make_unique<Encoder>(99, 99, false, Encoder::k4X));
552 reader.set_elevator_left_index(make_unique<DigitalInput>(99));
553 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(99));
554 reader.set_elevator_right_encoder(
555 make_unique<Encoder>(99, 99, false, Encoder::k4X));
556 reader.set_elevator_right_index(make_unique<DigitalInput>(99));
557 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(99));
558 reader.set_wrist_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
559 reader.set_wrist_index(make_unique<DigitalInput>(99));
560 reader.set_wrist_potentiometer(make_unique<AnalogInput>(99));
Brian Silverman1f90d672015-01-26 20:20:45 -0500561 reader.set_left_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
562 reader.set_right_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
Brian Silverman335c20e2015-01-26 21:47:58 -0500563 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500564 ::std::thread reader_thread(::std::ref(reader));
565 GyroSender gyro_sender;
566 ::std::thread gyro_thread(::std::ref(gyro_sender));
567
568 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500569 drivetrain_writer.set_left_drivetrain_talon(
570 ::std::unique_ptr<Talon>(new Talon(5)));
571 drivetrain_writer.set_right_drivetrain_talon(
572 ::std::unique_ptr<Talon>(new Talon(2)));
573 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
574
Daniel Pettiadf38432015-01-26 17:13:35 -0800575 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
576 // claw.
577 FridgeWriter fridge_writer;
578 fridge_writer.set_left_arm_talon(
579 ::std::unique_ptr<Talon>(new Talon(99)));
580 fridge_writer.set_right_arm_talon(
581 ::std::unique_ptr<Talon>(new Talon(99)));
582 fridge_writer.set_left_elevator_talon(
583 ::std::unique_ptr<Talon>(new Talon(99)));
584 fridge_writer.set_right_elevator_talon(
585 ::std::unique_ptr<Talon>(new Talon(99)));
586 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
587
588 ClawWriter claw_writer;
589 claw_writer.set_intake_talon(
590 ::std::unique_ptr<Talon>(new Talon(99)));
591 claw_writer.set_wrist_talon(
592 ::std::unique_ptr<Talon>(new Talon(99)));
593 ::std::thread claw_writer_thread(::std::ref(claw_writer));
594
595 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
596 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500597 SolenoidWriter solenoid_writer(pcm);
Daniel Pettiadf38432015-01-26 17:13:35 -0800598 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(99));
599 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(99));
600 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(99));
601 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(99));
602 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(99));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500603 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
604
605 // Wait forever. Not much else to do...
606 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
607
Austin Schuh010eb812014-10-25 18:06:49 -0700608 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800609
Austin Schuh010eb812014-10-25 18:06:49 -0700610 joystick_sender.Quit();
611 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500612 reader.Quit();
613 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800614 gyro_sender.Quit();
615 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500616
617 drivetrain_writer.Quit();
618 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500619 solenoid_writer.Quit();
620 solenoid_thread.join();
621
Austin Schuh010eb812014-10-25 18:06:49 -0700622 ::aos::Cleanup();
623 }
624};
625
Brian Silverman98f6ee22015-01-26 17:50:12 -0500626} // namespace wpilib
627} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800628
Brian Silverman98f6ee22015-01-26 17:50:12 -0500629
630START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);