blob: de1ed32da061b0f8978b9c36aa2cacf9f3b0c698 [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 Silverman5d712fc2015-02-15 03:39:31 -050071double arm_potentiometer_translate(double voltage) {
Brian Silverman8bca4a92015-02-05 15:19:06 -050072 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 Silverman5d712fc2015-02-15 03:39:31 -050086double elevator_potentiometer_translate(double voltage) {
Brian Silverman8bca4a92015-02-05 15:19:06 -050087 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 Silverman5d712fc2015-02-15 03:39:31 -0500101double claw_potentiometer_translate(double voltage) {
Brian Silverman8bca4a92015-02-05 15:19:06 -0500102 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_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500209 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800210 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500211 dma_synchronizer_->Add(&elevator_right_encoder_);
212 }
213
Austin Schuh010eb812014-10-25 18:06:49 -0700214 void operator()() {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800215 LOG(INFO, "In sensor reader thread\n");
Brian Silverman2fe007c2014-12-28 12:20:01 -0800216 ::aos::SetCurrentThreadName("SensorReader");
217
Brian Silverman699f0cb2015-02-05 19:45:01 -0500218 my_pid_ = getpid();
219 ds_ = DriverStation::GetInstance();
220
Brian Silverman335c20e2015-01-26 21:47:58 -0500221 wrist_encoder_.Start();
222 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800223 LOG(INFO, "Things are now started\n");
Austin Schuh010eb812014-10-25 18:06:49 -0700224
Brian Silverman2fe007c2014-12-28 12:20:01 -0800225 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700226 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800227 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700228 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700229 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500230
231 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700232 }
233
234 void RunIteration() {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500235 {
236 auto new_state = ::aos::robot_state.MakeMessage();
Austin Schuh010eb812014-10-25 18:06:49 -0700237
Brian Silverman699f0cb2015-02-05 19:45:01 -0500238 new_state->outputs_enabled = ds_->IsSysActive();
239 new_state->browned_out = ds_->IsSysBrownedOut();
240
241 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
242 new_state->is_5v_active = ControllerPower::GetEnabled5V();
243 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
244 new_state->voltage_5v = ControllerPower::GetVoltage5V();
245
246 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
247 new_state->voltage_battery = ds_->GetBatteryVoltage();
248
249 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700250 }
251
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500252 drivetrain_queue.position.MakeWithBuilder()
Austin Schuh010eb812014-10-25 18:06:49 -0700253 .right_encoder(drivetrain_translate(right_encoder_->GetRaw()))
254 .left_encoder(-drivetrain_translate(left_encoder_->GetRaw()))
Austin Schuh010eb812014-10-25 18:06:49 -0700255 .Send();
Brian Silverman335c20e2015-01-26 21:47:58 -0500256
257 dma_synchronizer_->RunIteration();
258
Philipp Schrader82c65072015-02-16 00:47:09 +0000259 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500260
Brian Silverman335c20e2015-01-26 21:47:58 -0500261 {
262 auto fridge_message = fridge_queue.position.MakeMessage();
263 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500264 arm_translate, arm_potentiometer_translate, false,
265 values.fridge.left_elevator_potentiometer_offset);
266 CopyPotAndIndexPosition(
267 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
268 arm_potentiometer_translate, true,
269 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500270 CopyPotAndIndexPosition(
271 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500272 elevator_translate, elevator_potentiometer_translate, false,
273 values.fridge.left_arm_potentiometer_offset);
274 CopyPotAndIndexPosition(
275 elevator_right_encoder_, &fridge_message->elevator.right,
276 elevator_translate, elevator_potentiometer_translate, true,
277 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500278 fridge_message.Send();
279 }
280
281 {
282 auto claw_message = claw_queue.position.MakeMessage();
283 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500284 claw_translate, claw_potentiometer_translate,
285 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500286 claw_message.Send();
287 }
Austin Schuh010eb812014-10-25 18:06:49 -0700288 }
289
290 void Quit() { run_ = false; }
291
292 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500293 static const int kPriority = 30;
294 static const int kInterruptPriority = 55;
295
Brian Silverman699f0cb2015-02-05 19:45:01 -0500296 int32_t my_pid_;
297 DriverStation *ds_;
298
Brian Silverman335c20e2015-01-26 21:47:58 -0500299 void CopyPotAndIndexPosition(
300 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
301 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500302 ::std::function<double(double)> potentiometer_translate, bool reverse,
303 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500304 const double multiplier = reverse ? -1.0 : 1.0;
305 position->encoder =
306 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500307 position->pot = multiplier * potentiometer_translate(
308 encoder.polled_potentiometer_voltage()) +
309 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500310 position->latched_encoder =
311 multiplier * encoder_translate(encoder.last_encoder_value());
312 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500313 multiplier *
314 potentiometer_translate(encoder.last_potentiometer_voltage()) +
315 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500316 position->index_pulses = encoder.index_posedge_count();
317 }
318
319 void CopyPotAndIndexPosition(
320 const InterruptEncoderAndPotentiometer &encoder,
321 PotAndIndexPosition *position,
322 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500323 ::std::function<double(double)> potentiometer_translate, bool reverse,
324 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500325 const double multiplier = reverse ? -1.0 : 1.0;
326 position->encoder =
327 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500328 position->pot = multiplier * potentiometer_translate(
329 encoder.potentiometer()->GetVoltage()) +
330 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500331 position->latched_encoder =
332 multiplier * encoder_translate(encoder.last_encoder_value());
333 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500334 multiplier *
335 potentiometer_translate(encoder.last_potentiometer_voltage()) +
336 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500337 position->index_pulses = encoder.index_posedge_count();
338 }
339
Brian Silverman335c20e2015-01-26 21:47:58 -0500340 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
341
342 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
343 elevator_left_encoder_, elevator_right_encoder_;
344
345 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
346
Austin Schuh010eb812014-10-25 18:06:49 -0700347 ::std::unique_ptr<Encoder> left_encoder_;
348 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700349
Brian Silverman1f90d672015-01-26 20:20:45 -0500350 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700351 DigitalGlitchFilter filter_;
352};
353
Brian Silvermand8f403a2014-12-13 19:12:04 -0500354class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700355 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500356 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800357 : pcm_(pcm),
358 fridge_(".frc971.control_loops.fridge.output"),
359 claw_(".frc971.control_loops.claw.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500360
Daniel Pettiadf38432015-01-26 17:13:35 -0800361 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
362 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700363 }
364
Daniel Pettiadf38432015-01-26 17:13:35 -0800365 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
366 fridge_grabbers_top_back_ = ::std::move(s);
367 }
368
369 void set_fridge_grabbers_bottom_front(
370 ::std::unique_ptr<BufferedSolenoid> s) {
371 fridge_grabbers_bottom_front_ = ::std::move(s);
372 }
373
374 void set_fridge_grabbers_bottom_back(
375 ::std::unique_ptr<BufferedSolenoid> s) {
376 fridge_grabbers_bottom_back_ = ::std::move(s);
377 }
378
379 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
380 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500381 }
Austin Schuh010eb812014-10-25 18:06:49 -0700382
Brian Silvermand8f403a2014-12-13 19:12:04 -0500383 void operator()() {
384 ::aos::SetCurrentThreadName("Solenoids");
385 ::aos::SetCurrentThreadRealtimePriority(30);
386
387 while (run_) {
388 ::aos::time::PhasedLoopXMS(20, 1000);
389
390 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800391 fridge_.FetchLatest();
392 if (fridge_.get()) {
393 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
394 fridge_grabbers_top_front_->Set(fridge_->grabbers.top_front);
395 fridge_grabbers_top_back_->Set(fridge_->grabbers.top_back);
396 fridge_grabbers_bottom_front_->Set(fridge_->grabbers.bottom_front);
397 fridge_grabbers_bottom_back_->Set(fridge_->grabbers.bottom_back);
398 }
399 }
400
401 {
402 claw_.FetchLatest();
403 if (claw_.get()) {
404 LOG_STRUCT(DEBUG, "solenoids", *claw_);
405 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500406 }
407 }
408
Brian Silvermand8f403a2014-12-13 19:12:04 -0500409 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700410 }
411 }
412
Brian Silvermand8f403a2014-12-13 19:12:04 -0500413 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700414
Brian Silvermand8f403a2014-12-13 19:12:04 -0500415 private:
416 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800417 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
418 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
419 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
420 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
421 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Austin Schuh010eb812014-10-25 18:06:49 -0700422
Daniel Pettiadf38432015-01-26 17:13:35 -0800423 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
424 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700425
Brian Silvermand8f403a2014-12-13 19:12:04 -0500426 ::std::atomic<bool> run_{true};
427};
428
429class DrivetrainWriter : public LoopOutputHandler {
430 public:
431 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
432 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700433 }
434
Brian Silvermand8f403a2014-12-13 19:12:04 -0500435 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
436 right_drivetrain_talon_ = ::std::move(t);
437 }
Austin Schuh010eb812014-10-25 18:06:49 -0700438
Brian Silvermand8f403a2014-12-13 19:12:04 -0500439 private:
440 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500441 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500442 }
443
444 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500445 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500446 LOG_STRUCT(DEBUG, "will output", *queue);
447 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
448 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
449 }
450
451 virtual void Stop() override {
452 LOG(WARNING, "drivetrain output too old\n");
453 left_drivetrain_talon_->Disable();
454 right_drivetrain_talon_->Disable();
455 }
456
Austin Schuh010eb812014-10-25 18:06:49 -0700457 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500458 ::std::unique_ptr<Talon> right_drivetrain_talon_;
459};
460
Daniel Pettiadf38432015-01-26 17:13:35 -0800461class FridgeWriter : public LoopOutputHandler {
462 public:
463 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
464 left_arm_talon_ = ::std::move(t);
465 }
466
467 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
468 right_arm_talon_ = ::std::move(t);
469 }
470
471 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
472 left_elevator_talon_ = ::std::move(t);
473 }
474
475 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
476 right_elevator_talon_ = ::std::move(t);
477 }
478
479 private:
480 virtual void Read() override {
481 ::frc971::control_loops::fridge_queue.output.FetchAnother();
482 }
483
484 virtual void Write() override {
485 auto &queue = ::frc971::control_loops::fridge_queue.output;
486 LOG_STRUCT(DEBUG, "will output", *queue);
487 left_arm_talon_->Set(-queue->left_arm / 12.0);
488 right_arm_talon_->Set(queue->right_arm / 12.0);
489 left_elevator_talon_->Set(-queue->left_elevator / 12.0);
490 right_elevator_talon_->Set(queue->right_elevator / 12.0);
491 }
492
493 virtual void Stop() override {
494 LOG(WARNING, "Fridge output too old.\n");
495 left_arm_talon_->Disable();
496 right_arm_talon_->Disable();
497 left_elevator_talon_->Disable();
498 right_elevator_talon_->Disable();
499 }
500
501 ::std::unique_ptr<Talon> left_arm_talon_;
502 ::std::unique_ptr<Talon> right_arm_talon_;
503 ::std::unique_ptr<Talon> left_elevator_talon_;
504 ::std::unique_ptr<Talon> right_elevator_talon_;
505};
506
507class ClawWriter : public LoopOutputHandler {
508 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800509 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
510 left_intake_talon_ = ::std::move(t);
511 }
512
513 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
514 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800515 }
516
517 void set_wrist_talon(::std::unique_ptr<Talon> t) {
518 wrist_talon_ = ::std::move(t);
519 }
520
521 private:
522 virtual void Read() override {
523 ::frc971::control_loops::claw_queue.output.FetchAnother();
524 }
525
526 virtual void Write() override {
527 auto &queue = ::frc971::control_loops::claw_queue.output;
528 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800529 left_intake_talon_->Set(queue->intake_voltage / 12.0);
530 right_intake_talon_->Set(queue->intake_voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800531 wrist_talon_->Set(queue->voltage / 12.0);
532 }
533
534 virtual void Stop() override {
535 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800536 left_intake_talon_->Disable();
537 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800538 wrist_talon_->Disable();
539 }
540
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800541 ::std::unique_ptr<Talon> left_intake_talon_;
542 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800543 ::std::unique_ptr<Talon> wrist_talon_;
544};
545
Brian Silverman1f90d672015-01-26 20:20:45 -0500546// TODO(brian): Replace this with ::std::make_unique once all our toolchains
547// have support.
548template <class T, class... U>
549std::unique_ptr<T> make_unique(U &&... u) {
550 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
551}
552
Austin Schuh010eb812014-10-25 18:06:49 -0700553class WPILibRobot : public RobotBase {
554 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800555 ::std::unique_ptr<Encoder> encoder(int index) {
556 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
557 Encoder::k4X);
558 }
Austin Schuh010eb812014-10-25 18:06:49 -0700559 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500560 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800561 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500562
Brian Silverman98f6ee22015-01-26 17:50:12 -0500563 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700564 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800565 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500566 ::std::unique_ptr<Compressor> compressor(new Compressor());
567 compressor->SetClosedLoopControl(true);
568
Brian Silverman98f6ee22015-01-26 17:50:12 -0500569 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800570 LOG(INFO, "Creating the reader\n");
571 reader.set_arm_left_encoder(encoder(1));
572 reader.set_arm_left_index(make_unique<DigitalInput>(1));
573 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
574
575 reader.set_arm_right_encoder(encoder(5));
576 reader.set_arm_right_index(make_unique<DigitalInput>(5));
577 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
578
579 reader.set_elevator_left_encoder(encoder(0));
580 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
581 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
582
583 reader.set_elevator_right_encoder(encoder(4));
584 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
585 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
586
587 reader.set_wrist_encoder(encoder(6));
588 reader.set_wrist_index(make_unique<DigitalInput>(6));
589 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
590
591 reader.set_left_encoder(encoder(2));
592 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500593 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500594 ::std::thread reader_thread(::std::ref(reader));
595 GyroSender gyro_sender;
596 ::std::thread gyro_thread(::std::ref(gyro_sender));
597
598 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500599 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800600 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500601 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800602 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500603 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
604
Daniel Pettiadf38432015-01-26 17:13:35 -0800605 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
606 // claw.
607 FridgeWriter fridge_writer;
608 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800609 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800610 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800611 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800612 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800613 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800614 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800615 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800616 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
617
618 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800619 claw_writer.set_left_intake_talon(
620 ::std::unique_ptr<Talon>(new Talon(5)));
621 claw_writer.set_right_intake_talon(
622 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800623 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800624 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800625 ::std::thread claw_writer_thread(::std::ref(claw_writer));
626
627 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
628 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500629 SolenoidWriter solenoid_writer(pcm);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800630 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(1));
631 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(1));
632 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
633 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(3));
634 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500635 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
636
637 // Wait forever. Not much else to do...
638 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
639
Austin Schuh010eb812014-10-25 18:06:49 -0700640 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800641
Austin Schuh010eb812014-10-25 18:06:49 -0700642 joystick_sender.Quit();
643 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500644 reader.Quit();
645 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800646 gyro_sender.Quit();
647 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500648
649 drivetrain_writer.Quit();
650 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500651 solenoid_writer.Quit();
652 solenoid_thread.join();
653
Austin Schuh010eb812014-10-25 18:06:49 -0700654 ::aos::Cleanup();
655 }
656};
657
Brian Silverman98f6ee22015-01-26 17:50:12 -0500658} // namespace wpilib
659} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800660
Brian Silverman98f6ee22015-01-26 17:50:12 -0500661
662START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);