blob: 1941144a7d2114693e92ee6a61c8c1f8487978a2 [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_);
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
Brian Silverman5d712fc2015-02-15 03:39:31 -0500257 const auto values = constants::GetValues();
258
Brian Silverman335c20e2015-01-26 21:47:58 -0500259 {
260 auto fridge_message = fridge_queue.position.MakeMessage();
261 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500262 arm_translate, arm_potentiometer_translate, false,
263 values.fridge.left_elevator_potentiometer_offset);
264 CopyPotAndIndexPosition(
265 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
266 arm_potentiometer_translate, true,
267 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500268 CopyPotAndIndexPosition(
269 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500270 elevator_translate, elevator_potentiometer_translate, false,
271 values.fridge.left_arm_potentiometer_offset);
272 CopyPotAndIndexPosition(
273 elevator_right_encoder_, &fridge_message->elevator.right,
274 elevator_translate, elevator_potentiometer_translate, true,
275 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500276 fridge_message.Send();
277 }
278
279 {
280 auto claw_message = claw_queue.position.MakeMessage();
281 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500282 claw_translate, claw_potentiometer_translate,
283 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500284 claw_message.Send();
285 }
Austin Schuh010eb812014-10-25 18:06:49 -0700286 }
287
288 void Quit() { run_ = false; }
289
290 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500291 static const int kPriority = 30;
292 static const int kInterruptPriority = 55;
293
Brian Silverman699f0cb2015-02-05 19:45:01 -0500294 int32_t my_pid_;
295 DriverStation *ds_;
296
Brian Silverman335c20e2015-01-26 21:47:58 -0500297 void CopyPotAndIndexPosition(
298 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
299 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500300 ::std::function<double(double)> potentiometer_translate, bool reverse,
301 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500302 const double multiplier = reverse ? -1.0 : 1.0;
303 position->encoder =
304 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500305 position->pot = multiplier * potentiometer_translate(
306 encoder.polled_potentiometer_voltage()) +
307 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500308 position->latched_encoder =
309 multiplier * encoder_translate(encoder.last_encoder_value());
310 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500311 multiplier *
312 potentiometer_translate(encoder.last_potentiometer_voltage()) +
313 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500314 position->index_pulses = encoder.index_posedge_count();
315 }
316
317 void CopyPotAndIndexPosition(
318 const InterruptEncoderAndPotentiometer &encoder,
319 PotAndIndexPosition *position,
320 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500321 ::std::function<double(double)> potentiometer_translate, bool reverse,
322 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500323 const double multiplier = reverse ? -1.0 : 1.0;
324 position->encoder =
325 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500326 position->pot = multiplier * potentiometer_translate(
327 encoder.potentiometer()->GetVoltage()) +
328 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500329 position->latched_encoder =
330 multiplier * encoder_translate(encoder.last_encoder_value());
331 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500332 multiplier *
333 potentiometer_translate(encoder.last_potentiometer_voltage()) +
334 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500335 position->index_pulses = encoder.index_posedge_count();
336 }
337
Brian Silverman335c20e2015-01-26 21:47:58 -0500338 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
339
340 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
341 elevator_left_encoder_, elevator_right_encoder_;
342
343 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
344
Austin Schuh010eb812014-10-25 18:06:49 -0700345 ::std::unique_ptr<Encoder> left_encoder_;
346 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700347
Brian Silverman1f90d672015-01-26 20:20:45 -0500348 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700349 DigitalGlitchFilter filter_;
350};
351
Brian Silvermand8f403a2014-12-13 19:12:04 -0500352class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700353 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500354 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800355 : pcm_(pcm),
356 fridge_(".frc971.control_loops.fridge.output"),
357 claw_(".frc971.control_loops.claw.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500358
Daniel Pettiadf38432015-01-26 17:13:35 -0800359 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
360 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700361 }
362
Daniel Pettiadf38432015-01-26 17:13:35 -0800363 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
364 fridge_grabbers_top_back_ = ::std::move(s);
365 }
366
367 void set_fridge_grabbers_bottom_front(
368 ::std::unique_ptr<BufferedSolenoid> s) {
369 fridge_grabbers_bottom_front_ = ::std::move(s);
370 }
371
372 void set_fridge_grabbers_bottom_back(
373 ::std::unique_ptr<BufferedSolenoid> s) {
374 fridge_grabbers_bottom_back_ = ::std::move(s);
375 }
376
377 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
378 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500379 }
Austin Schuh010eb812014-10-25 18:06:49 -0700380
Brian Silvermand8f403a2014-12-13 19:12:04 -0500381 void operator()() {
382 ::aos::SetCurrentThreadName("Solenoids");
383 ::aos::SetCurrentThreadRealtimePriority(30);
384
385 while (run_) {
386 ::aos::time::PhasedLoopXMS(20, 1000);
387
388 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800389 fridge_.FetchLatest();
390 if (fridge_.get()) {
391 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
392 fridge_grabbers_top_front_->Set(fridge_->grabbers.top_front);
393 fridge_grabbers_top_back_->Set(fridge_->grabbers.top_back);
394 fridge_grabbers_bottom_front_->Set(fridge_->grabbers.bottom_front);
395 fridge_grabbers_bottom_back_->Set(fridge_->grabbers.bottom_back);
396 }
397 }
398
399 {
400 claw_.FetchLatest();
401 if (claw_.get()) {
402 LOG_STRUCT(DEBUG, "solenoids", *claw_);
403 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500404 }
405 }
406
Brian Silvermand8f403a2014-12-13 19:12:04 -0500407 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700408 }
409 }
410
Brian Silvermand8f403a2014-12-13 19:12:04 -0500411 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700412
Brian Silvermand8f403a2014-12-13 19:12:04 -0500413 private:
414 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800415 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
416 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
417 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
418 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
419 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Austin Schuh010eb812014-10-25 18:06:49 -0700420
Daniel Pettiadf38432015-01-26 17:13:35 -0800421 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
422 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700423
Brian Silvermand8f403a2014-12-13 19:12:04 -0500424 ::std::atomic<bool> run_{true};
425};
426
427class DrivetrainWriter : public LoopOutputHandler {
428 public:
429 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
430 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700431 }
432
Brian Silvermand8f403a2014-12-13 19:12:04 -0500433 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
434 right_drivetrain_talon_ = ::std::move(t);
435 }
Austin Schuh010eb812014-10-25 18:06:49 -0700436
Brian Silvermand8f403a2014-12-13 19:12:04 -0500437 private:
438 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500439 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500440 }
441
442 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500443 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500444 LOG_STRUCT(DEBUG, "will output", *queue);
445 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
446 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
447 }
448
449 virtual void Stop() override {
450 LOG(WARNING, "drivetrain output too old\n");
451 left_drivetrain_talon_->Disable();
452 right_drivetrain_talon_->Disable();
453 }
454
Austin Schuh010eb812014-10-25 18:06:49 -0700455 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500456 ::std::unique_ptr<Talon> right_drivetrain_talon_;
457};
458
Daniel Pettiadf38432015-01-26 17:13:35 -0800459class FridgeWriter : public LoopOutputHandler {
460 public:
461 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
462 left_arm_talon_ = ::std::move(t);
463 }
464
465 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
466 right_arm_talon_ = ::std::move(t);
467 }
468
469 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
470 left_elevator_talon_ = ::std::move(t);
471 }
472
473 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
474 right_elevator_talon_ = ::std::move(t);
475 }
476
477 private:
478 virtual void Read() override {
479 ::frc971::control_loops::fridge_queue.output.FetchAnother();
480 }
481
482 virtual void Write() override {
483 auto &queue = ::frc971::control_loops::fridge_queue.output;
484 LOG_STRUCT(DEBUG, "will output", *queue);
485 left_arm_talon_->Set(-queue->left_arm / 12.0);
486 right_arm_talon_->Set(queue->right_arm / 12.0);
487 left_elevator_talon_->Set(-queue->left_elevator / 12.0);
488 right_elevator_talon_->Set(queue->right_elevator / 12.0);
489 }
490
491 virtual void Stop() override {
492 LOG(WARNING, "Fridge output too old.\n");
493 left_arm_talon_->Disable();
494 right_arm_talon_->Disable();
495 left_elevator_talon_->Disable();
496 right_elevator_talon_->Disable();
497 }
498
499 ::std::unique_ptr<Talon> left_arm_talon_;
500 ::std::unique_ptr<Talon> right_arm_talon_;
501 ::std::unique_ptr<Talon> left_elevator_talon_;
502 ::std::unique_ptr<Talon> right_elevator_talon_;
503};
504
505class ClawWriter : public LoopOutputHandler {
506 public:
507 void set_intake_talon(::std::unique_ptr<Talon> t) {
508 intake_talon_ = ::std::move(t);
509 }
510
511 void set_wrist_talon(::std::unique_ptr<Talon> t) {
512 wrist_talon_ = ::std::move(t);
513 }
514
515 private:
516 virtual void Read() override {
517 ::frc971::control_loops::claw_queue.output.FetchAnother();
518 }
519
520 virtual void Write() override {
521 auto &queue = ::frc971::control_loops::claw_queue.output;
522 LOG_STRUCT(DEBUG, "will output", *queue);
523 intake_talon_->Set(queue->intake_voltage / 12.0);
524 wrist_talon_->Set(queue->voltage / 12.0);
525 }
526
527 virtual void Stop() override {
528 LOG(WARNING, "Claw output too old.\n");
529 intake_talon_->Disable();
530 wrist_talon_->Disable();
531 }
532
533 ::std::unique_ptr<Talon> intake_talon_;
534 ::std::unique_ptr<Talon> wrist_talon_;
535};
536
Brian Silverman1f90d672015-01-26 20:20:45 -0500537// TODO(brian): Replace this with ::std::make_unique once all our toolchains
538// have support.
539template <class T, class... U>
540std::unique_ptr<T> make_unique(U &&... u) {
541 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
542}
543
Austin Schuh010eb812014-10-25 18:06:49 -0700544class WPILibRobot : public RobotBase {
545 public:
546 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500547 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800548 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500549
Brian Silverman98f6ee22015-01-26 17:50:12 -0500550 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700551 ::std::thread joystick_thread(::std::ref(joystick_sender));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500552 ::std::unique_ptr<Compressor> compressor(new Compressor());
553 compressor->SetClosedLoopControl(true);
554
Brian Silverman98f6ee22015-01-26 17:50:12 -0500555 SensorReader reader;
Brian Silverman1f90d672015-01-26 20:20:45 -0500556 // TODO(sensors): Replace all the 99s with real port numbers.
Brian Silverman335c20e2015-01-26 21:47:58 -0500557 reader.set_arm_left_encoder(
558 make_unique<Encoder>(99, 99, false, Encoder::k4X));
559 reader.set_arm_left_index(make_unique<DigitalInput>(99));
560 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(99));
561 reader.set_arm_right_encoder(
562 make_unique<Encoder>(99, 99, false, Encoder::k4X));
563 reader.set_arm_right_index(make_unique<DigitalInput>(99));
564 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(99));
565 reader.set_elevator_left_encoder(
566 make_unique<Encoder>(99, 99, false, Encoder::k4X));
567 reader.set_elevator_left_index(make_unique<DigitalInput>(99));
568 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(99));
569 reader.set_elevator_right_encoder(
570 make_unique<Encoder>(99, 99, false, Encoder::k4X));
571 reader.set_elevator_right_index(make_unique<DigitalInput>(99));
572 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(99));
573 reader.set_wrist_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
574 reader.set_wrist_index(make_unique<DigitalInput>(99));
575 reader.set_wrist_potentiometer(make_unique<AnalogInput>(99));
Brian Silverman1f90d672015-01-26 20:20:45 -0500576 reader.set_left_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
577 reader.set_right_encoder(make_unique<Encoder>(99, 99, false, Encoder::k4X));
Brian Silverman335c20e2015-01-26 21:47:58 -0500578 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500579 ::std::thread reader_thread(::std::ref(reader));
580 GyroSender gyro_sender;
581 ::std::thread gyro_thread(::std::ref(gyro_sender));
582
583 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500584 drivetrain_writer.set_left_drivetrain_talon(
585 ::std::unique_ptr<Talon>(new Talon(5)));
586 drivetrain_writer.set_right_drivetrain_talon(
587 ::std::unique_ptr<Talon>(new Talon(2)));
588 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
589
Daniel Pettiadf38432015-01-26 17:13:35 -0800590 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
591 // claw.
592 FridgeWriter fridge_writer;
593 fridge_writer.set_left_arm_talon(
594 ::std::unique_ptr<Talon>(new Talon(99)));
595 fridge_writer.set_right_arm_talon(
596 ::std::unique_ptr<Talon>(new Talon(99)));
597 fridge_writer.set_left_elevator_talon(
598 ::std::unique_ptr<Talon>(new Talon(99)));
599 fridge_writer.set_right_elevator_talon(
600 ::std::unique_ptr<Talon>(new Talon(99)));
601 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
602
603 ClawWriter claw_writer;
604 claw_writer.set_intake_talon(
605 ::std::unique_ptr<Talon>(new Talon(99)));
606 claw_writer.set_wrist_talon(
607 ::std::unique_ptr<Talon>(new Talon(99)));
608 ::std::thread claw_writer_thread(::std::ref(claw_writer));
609
610 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
611 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500612 SolenoidWriter solenoid_writer(pcm);
Daniel Pettiadf38432015-01-26 17:13:35 -0800613 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(99));
614 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(99));
615 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(99));
616 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(99));
617 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(99));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500618 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
619
620 // Wait forever. Not much else to do...
621 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
622
Austin Schuh010eb812014-10-25 18:06:49 -0700623 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800624
Austin Schuh010eb812014-10-25 18:06:49 -0700625 joystick_sender.Quit();
626 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500627 reader.Quit();
628 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800629 gyro_sender.Quit();
630 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500631
632 drivetrain_writer.Quit();
633 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500634 solenoid_writer.Quit();
635 solenoid_thread.join();
636
Austin Schuh010eb812014-10-25 18:06:49 -0700637 ::aos::Cleanup();
638 }
639};
640
Brian Silverman98f6ee22015-01-26 17:50:12 -0500641} // namespace wpilib
642} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800643
Brian Silverman98f6ee22015-01-26 17:50:12 -0500644
645START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);