blob: d7c23ce90ade98c264ab8101a54b15e877abd6a5 [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) {
Austin Schuh6246c542015-02-16 02:59:09 -080065 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080066 (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) {
Austin Schuh35d06612015-02-15 23:35:23 -080072 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080073 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080074 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080075 (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) {
Austin Schuh6246c542015-02-16 02:59:09 -080087 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 *
Austin Schuh35d06612015-02-15 23:35:23 -080091 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -080092}
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) {
Austin Schuh6246c542015-02-16 02:59:09 -0800102 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800103 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800104 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800105 (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
Brian Silverman4d882aa2015-02-20 13:40:42 -0500249 LOG_STRUCT(DEBUG, "robot_state", *new_state);
250
Brian Silverman699f0cb2015-02-05 19:45:01 -0500251 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700252 }
253
Austin Schuh35d06612015-02-15 23:35:23 -0800254 {
255 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
256 drivetrain_message->right_encoder =
257 drivetrain_translate(right_encoder_->GetRaw());
258 drivetrain_message->left_encoder =
259 -drivetrain_translate(left_encoder_->GetRaw());
260
261 drivetrain_message.Send();
262 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500263
264 dma_synchronizer_->RunIteration();
265
Philipp Schrader82c65072015-02-16 00:47:09 +0000266 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500267
Brian Silverman335c20e2015-01-26 21:47:58 -0500268 {
269 auto fridge_message = fridge_queue.position.MakeMessage();
270 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500271 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800272 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500273 CopyPotAndIndexPosition(
274 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
275 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800276 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500277 CopyPotAndIndexPosition(
278 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500279 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800280 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500281 CopyPotAndIndexPosition(
282 elevator_right_encoder_, &fridge_message->elevator.right,
283 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800284 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500285 fridge_message.Send();
286 }
287
288 {
289 auto claw_message = claw_queue.position.MakeMessage();
290 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500291 claw_translate, claw_potentiometer_translate,
292 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500293 claw_message.Send();
294 }
Austin Schuh010eb812014-10-25 18:06:49 -0700295 }
296
297 void Quit() { run_ = false; }
298
299 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500300 static const int kPriority = 30;
301 static const int kInterruptPriority = 55;
302
Brian Silverman699f0cb2015-02-05 19:45:01 -0500303 int32_t my_pid_;
304 DriverStation *ds_;
305
Brian Silverman335c20e2015-01-26 21:47:58 -0500306 void CopyPotAndIndexPosition(
307 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
308 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500309 ::std::function<double(double)> potentiometer_translate, bool reverse,
310 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500311 const double multiplier = reverse ? -1.0 : 1.0;
312 position->encoder =
313 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500314 position->pot = multiplier * potentiometer_translate(
315 encoder.polled_potentiometer_voltage()) +
316 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500317 position->latched_encoder =
318 multiplier * encoder_translate(encoder.last_encoder_value());
319 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500320 multiplier *
321 potentiometer_translate(encoder.last_potentiometer_voltage()) +
322 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500323 position->index_pulses = encoder.index_posedge_count();
324 }
325
326 void CopyPotAndIndexPosition(
327 const InterruptEncoderAndPotentiometer &encoder,
328 PotAndIndexPosition *position,
329 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500330 ::std::function<double(double)> potentiometer_translate, bool reverse,
331 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500332 const double multiplier = reverse ? -1.0 : 1.0;
333 position->encoder =
334 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500335 position->pot = multiplier * potentiometer_translate(
336 encoder.potentiometer()->GetVoltage()) +
337 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500338 position->latched_encoder =
339 multiplier * encoder_translate(encoder.last_encoder_value());
340 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500341 multiplier *
342 potentiometer_translate(encoder.last_potentiometer_voltage()) +
343 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500344 position->index_pulses = encoder.index_posedge_count();
345 }
346
Brian Silverman335c20e2015-01-26 21:47:58 -0500347 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
348
349 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
350 elevator_left_encoder_, elevator_right_encoder_;
351
352 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
353
Austin Schuh010eb812014-10-25 18:06:49 -0700354 ::std::unique_ptr<Encoder> left_encoder_;
355 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700356
Brian Silverman1f90d672015-01-26 20:20:45 -0500357 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700358 DigitalGlitchFilter filter_;
359};
360
Brian Silvermand8f403a2014-12-13 19:12:04 -0500361class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700362 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500363 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800364 : pcm_(pcm),
365 fridge_(".frc971.control_loops.fridge.output"),
366 claw_(".frc971.control_loops.claw.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500367
Daniel Pettiadf38432015-01-26 17:13:35 -0800368 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
369 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700370 }
371
Daniel Pettiadf38432015-01-26 17:13:35 -0800372 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
373 fridge_grabbers_top_back_ = ::std::move(s);
374 }
375
376 void set_fridge_grabbers_bottom_front(
377 ::std::unique_ptr<BufferedSolenoid> s) {
378 fridge_grabbers_bottom_front_ = ::std::move(s);
379 }
380
381 void set_fridge_grabbers_bottom_back(
382 ::std::unique_ptr<BufferedSolenoid> s) {
383 fridge_grabbers_bottom_back_ = ::std::move(s);
384 }
385
386 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
387 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500388 }
Austin Schuh010eb812014-10-25 18:06:49 -0700389
Brian Silvermand8f403a2014-12-13 19:12:04 -0500390 void operator()() {
391 ::aos::SetCurrentThreadName("Solenoids");
392 ::aos::SetCurrentThreadRealtimePriority(30);
393
394 while (run_) {
395 ::aos::time::PhasedLoopXMS(20, 1000);
396
397 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800398 fridge_.FetchLatest();
399 if (fridge_.get()) {
400 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
401 fridge_grabbers_top_front_->Set(fridge_->grabbers.top_front);
402 fridge_grabbers_top_back_->Set(fridge_->grabbers.top_back);
403 fridge_grabbers_bottom_front_->Set(fridge_->grabbers.bottom_front);
404 fridge_grabbers_bottom_back_->Set(fridge_->grabbers.bottom_back);
405 }
406 }
407
408 {
409 claw_.FetchLatest();
410 if (claw_.get()) {
411 LOG_STRUCT(DEBUG, "solenoids", *claw_);
412 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500413 }
414 }
415
Brian Silvermand8f403a2014-12-13 19:12:04 -0500416 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700417 }
418 }
419
Brian Silvermand8f403a2014-12-13 19:12:04 -0500420 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700421
Brian Silvermand8f403a2014-12-13 19:12:04 -0500422 private:
423 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800424 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
425 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
426 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
427 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
428 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Austin Schuh010eb812014-10-25 18:06:49 -0700429
Daniel Pettiadf38432015-01-26 17:13:35 -0800430 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
431 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700432
Brian Silvermand8f403a2014-12-13 19:12:04 -0500433 ::std::atomic<bool> run_{true};
434};
435
436class DrivetrainWriter : public LoopOutputHandler {
437 public:
438 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
439 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700440 }
441
Brian Silvermand8f403a2014-12-13 19:12:04 -0500442 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
443 right_drivetrain_talon_ = ::std::move(t);
444 }
Austin Schuh010eb812014-10-25 18:06:49 -0700445
Brian Silvermand8f403a2014-12-13 19:12:04 -0500446 private:
447 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500448 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500449 }
450
451 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500452 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500453 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800454 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
455 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500456 }
457
458 virtual void Stop() override {
459 LOG(WARNING, "drivetrain output too old\n");
460 left_drivetrain_talon_->Disable();
461 right_drivetrain_talon_->Disable();
462 }
463
Austin Schuh010eb812014-10-25 18:06:49 -0700464 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500465 ::std::unique_ptr<Talon> right_drivetrain_talon_;
466};
467
Daniel Pettiadf38432015-01-26 17:13:35 -0800468class FridgeWriter : public LoopOutputHandler {
469 public:
470 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
471 left_arm_talon_ = ::std::move(t);
472 }
473
474 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
475 right_arm_talon_ = ::std::move(t);
476 }
477
478 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
479 left_elevator_talon_ = ::std::move(t);
480 }
481
482 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
483 right_elevator_talon_ = ::std::move(t);
484 }
485
486 private:
487 virtual void Read() override {
488 ::frc971::control_loops::fridge_queue.output.FetchAnother();
489 }
490
491 virtual void Write() override {
492 auto &queue = ::frc971::control_loops::fridge_queue.output;
493 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800494 left_arm_talon_->Set(queue->left_arm / 12.0);
495 right_arm_talon_->Set(-queue->right_arm / 12.0);
496 left_elevator_talon_->Set(queue->left_elevator / 12.0);
497 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800498 }
499
500 virtual void Stop() override {
501 LOG(WARNING, "Fridge output too old.\n");
502 left_arm_talon_->Disable();
503 right_arm_talon_->Disable();
504 left_elevator_talon_->Disable();
505 right_elevator_talon_->Disable();
506 }
507
508 ::std::unique_ptr<Talon> left_arm_talon_;
509 ::std::unique_ptr<Talon> right_arm_talon_;
510 ::std::unique_ptr<Talon> left_elevator_talon_;
511 ::std::unique_ptr<Talon> right_elevator_talon_;
512};
513
514class ClawWriter : public LoopOutputHandler {
515 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800516 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
517 left_intake_talon_ = ::std::move(t);
518 }
519
520 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
521 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800522 }
523
524 void set_wrist_talon(::std::unique_ptr<Talon> t) {
525 wrist_talon_ = ::std::move(t);
526 }
527
528 private:
529 virtual void Read() override {
530 ::frc971::control_loops::claw_queue.output.FetchAnother();
531 }
532
533 virtual void Write() override {
534 auto &queue = ::frc971::control_loops::claw_queue.output;
535 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800536 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800537 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
538 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800539 }
540
541 virtual void Stop() override {
542 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800543 left_intake_talon_->Disable();
544 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800545 wrist_talon_->Disable();
546 }
547
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800548 ::std::unique_ptr<Talon> left_intake_talon_;
549 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800550 ::std::unique_ptr<Talon> wrist_talon_;
551};
552
Brian Silverman1f90d672015-01-26 20:20:45 -0500553// TODO(brian): Replace this with ::std::make_unique once all our toolchains
554// have support.
555template <class T, class... U>
556std::unique_ptr<T> make_unique(U &&... u) {
557 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
558}
559
Austin Schuh010eb812014-10-25 18:06:49 -0700560class WPILibRobot : public RobotBase {
561 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800562 ::std::unique_ptr<Encoder> encoder(int index) {
563 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
564 Encoder::k4X);
565 }
Austin Schuh010eb812014-10-25 18:06:49 -0700566 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500567 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800568 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500569
Brian Silverman98f6ee22015-01-26 17:50:12 -0500570 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700571 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800572 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500573 ::std::unique_ptr<Compressor> compressor(new Compressor());
574 compressor->SetClosedLoopControl(true);
575
Brian Silverman98f6ee22015-01-26 17:50:12 -0500576 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800577 LOG(INFO, "Creating the reader\n");
578 reader.set_arm_left_encoder(encoder(1));
579 reader.set_arm_left_index(make_unique<DigitalInput>(1));
580 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
581
582 reader.set_arm_right_encoder(encoder(5));
583 reader.set_arm_right_index(make_unique<DigitalInput>(5));
584 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
585
586 reader.set_elevator_left_encoder(encoder(0));
587 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
588 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
589
590 reader.set_elevator_right_encoder(encoder(4));
591 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
592 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
593
594 reader.set_wrist_encoder(encoder(6));
595 reader.set_wrist_index(make_unique<DigitalInput>(6));
596 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
597
598 reader.set_left_encoder(encoder(2));
599 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500600 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500601 ::std::thread reader_thread(::std::ref(reader));
602 GyroSender gyro_sender;
603 ::std::thread gyro_thread(::std::ref(gyro_sender));
604
605 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500606 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800607 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500608 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800609 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500610 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
611
Daniel Pettiadf38432015-01-26 17:13:35 -0800612 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
613 // claw.
614 FridgeWriter fridge_writer;
615 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800616 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800617 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800618 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800619 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800620 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800621 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800622 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800623 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
624
625 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800626 claw_writer.set_left_intake_talon(
627 ::std::unique_ptr<Talon>(new Talon(5)));
628 claw_writer.set_right_intake_talon(
629 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800630 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800631 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800632 ::std::thread claw_writer_thread(::std::ref(claw_writer));
633
634 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
635 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500636 SolenoidWriter solenoid_writer(pcm);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800637 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(1));
638 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(1));
639 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
640 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(3));
641 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500642 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
643
644 // Wait forever. Not much else to do...
645 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
646
Austin Schuh010eb812014-10-25 18:06:49 -0700647 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800648
Austin Schuh010eb812014-10-25 18:06:49 -0700649 joystick_sender.Quit();
650 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500651 reader.Quit();
652 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800653 gyro_sender.Quit();
654 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500655
656 drivetrain_writer.Quit();
657 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500658 solenoid_writer.Quit();
659 solenoid_thread.join();
660
Austin Schuh010eb812014-10-25 18:06:49 -0700661 ::aos::Cleanup();
662 }
663};
664
Brian Silverman98f6ee22015-01-26 17:50:12 -0500665} // namespace wpilib
666} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800667
Brian Silverman98f6ee22015-01-26 17:50:12 -0500668
669START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);