blob: 52bfaf37fd06662f3a3b04753575053c69ae6e9f [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
249 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700250 }
251
Austin Schuh35d06612015-02-15 23:35:23 -0800252 {
253 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
254 drivetrain_message->right_encoder =
255 drivetrain_translate(right_encoder_->GetRaw());
256 drivetrain_message->left_encoder =
257 -drivetrain_translate(left_encoder_->GetRaw());
258
259 drivetrain_message.Send();
260 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500261
262 dma_synchronizer_->RunIteration();
263
Philipp Schrader82c65072015-02-16 00:47:09 +0000264 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500265
Brian Silverman335c20e2015-01-26 21:47:58 -0500266 {
267 auto fridge_message = fridge_queue.position.MakeMessage();
268 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500269 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800270 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500271 CopyPotAndIndexPosition(
272 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
273 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800274 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500275 CopyPotAndIndexPosition(
276 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500277 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800278 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500279 CopyPotAndIndexPosition(
280 elevator_right_encoder_, &fridge_message->elevator.right,
281 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800282 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500283 fridge_message.Send();
284 }
285
286 {
287 auto claw_message = claw_queue.position.MakeMessage();
288 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500289 claw_translate, claw_potentiometer_translate,
290 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500291 claw_message.Send();
292 }
Austin Schuh010eb812014-10-25 18:06:49 -0700293 }
294
295 void Quit() { run_ = false; }
296
297 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500298 static const int kPriority = 30;
299 static const int kInterruptPriority = 55;
300
Brian Silverman699f0cb2015-02-05 19:45:01 -0500301 int32_t my_pid_;
302 DriverStation *ds_;
303
Brian Silverman335c20e2015-01-26 21:47:58 -0500304 void CopyPotAndIndexPosition(
305 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
306 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500307 ::std::function<double(double)> potentiometer_translate, bool reverse,
308 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500309 const double multiplier = reverse ? -1.0 : 1.0;
310 position->encoder =
311 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500312 position->pot = multiplier * potentiometer_translate(
313 encoder.polled_potentiometer_voltage()) +
314 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500315 position->latched_encoder =
316 multiplier * encoder_translate(encoder.last_encoder_value());
317 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500318 multiplier *
319 potentiometer_translate(encoder.last_potentiometer_voltage()) +
320 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500321 position->index_pulses = encoder.index_posedge_count();
322 }
323
324 void CopyPotAndIndexPosition(
325 const InterruptEncoderAndPotentiometer &encoder,
326 PotAndIndexPosition *position,
327 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500328 ::std::function<double(double)> potentiometer_translate, bool reverse,
329 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500330 const double multiplier = reverse ? -1.0 : 1.0;
331 position->encoder =
332 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500333 position->pot = multiplier * potentiometer_translate(
334 encoder.potentiometer()->GetVoltage()) +
335 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500336 position->latched_encoder =
337 multiplier * encoder_translate(encoder.last_encoder_value());
338 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500339 multiplier *
340 potentiometer_translate(encoder.last_potentiometer_voltage()) +
341 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500342 position->index_pulses = encoder.index_posedge_count();
343 }
344
Brian Silverman335c20e2015-01-26 21:47:58 -0500345 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
346
347 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
348 elevator_left_encoder_, elevator_right_encoder_;
349
350 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
351
Austin Schuh010eb812014-10-25 18:06:49 -0700352 ::std::unique_ptr<Encoder> left_encoder_;
353 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700354
Brian Silverman1f90d672015-01-26 20:20:45 -0500355 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700356 DigitalGlitchFilter filter_;
357};
358
Brian Silvermand8f403a2014-12-13 19:12:04 -0500359class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700360 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500361 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800362 : pcm_(pcm),
363 fridge_(".frc971.control_loops.fridge.output"),
364 claw_(".frc971.control_loops.claw.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500365
Daniel Pettiadf38432015-01-26 17:13:35 -0800366 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
367 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700368 }
369
Daniel Pettiadf38432015-01-26 17:13:35 -0800370 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
371 fridge_grabbers_top_back_ = ::std::move(s);
372 }
373
374 void set_fridge_grabbers_bottom_front(
375 ::std::unique_ptr<BufferedSolenoid> s) {
376 fridge_grabbers_bottom_front_ = ::std::move(s);
377 }
378
379 void set_fridge_grabbers_bottom_back(
380 ::std::unique_ptr<BufferedSolenoid> s) {
381 fridge_grabbers_bottom_back_ = ::std::move(s);
382 }
383
384 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
385 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500386 }
Austin Schuh010eb812014-10-25 18:06:49 -0700387
Brian Silvermand8f403a2014-12-13 19:12:04 -0500388 void operator()() {
389 ::aos::SetCurrentThreadName("Solenoids");
390 ::aos::SetCurrentThreadRealtimePriority(30);
391
392 while (run_) {
393 ::aos::time::PhasedLoopXMS(20, 1000);
394
395 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800396 fridge_.FetchLatest();
397 if (fridge_.get()) {
398 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
399 fridge_grabbers_top_front_->Set(fridge_->grabbers.top_front);
400 fridge_grabbers_top_back_->Set(fridge_->grabbers.top_back);
401 fridge_grabbers_bottom_front_->Set(fridge_->grabbers.bottom_front);
402 fridge_grabbers_bottom_back_->Set(fridge_->grabbers.bottom_back);
403 }
404 }
405
406 {
407 claw_.FetchLatest();
408 if (claw_.get()) {
409 LOG_STRUCT(DEBUG, "solenoids", *claw_);
410 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500411 }
412 }
413
Brian Silvermand8f403a2014-12-13 19:12:04 -0500414 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700415 }
416 }
417
Brian Silvermand8f403a2014-12-13 19:12:04 -0500418 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700419
Brian Silvermand8f403a2014-12-13 19:12:04 -0500420 private:
421 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800422 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
423 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
424 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
425 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
426 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Austin Schuh010eb812014-10-25 18:06:49 -0700427
Daniel Pettiadf38432015-01-26 17:13:35 -0800428 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
429 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700430
Brian Silvermand8f403a2014-12-13 19:12:04 -0500431 ::std::atomic<bool> run_{true};
432};
433
434class DrivetrainWriter : public LoopOutputHandler {
435 public:
436 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
437 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700438 }
439
Brian Silvermand8f403a2014-12-13 19:12:04 -0500440 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
441 right_drivetrain_talon_ = ::std::move(t);
442 }
Austin Schuh010eb812014-10-25 18:06:49 -0700443
Brian Silvermand8f403a2014-12-13 19:12:04 -0500444 private:
445 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500446 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500447 }
448
449 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500450 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500451 LOG_STRUCT(DEBUG, "will output", *queue);
452 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
453 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
454 }
455
456 virtual void Stop() override {
457 LOG(WARNING, "drivetrain output too old\n");
458 left_drivetrain_talon_->Disable();
459 right_drivetrain_talon_->Disable();
460 }
461
Austin Schuh010eb812014-10-25 18:06:49 -0700462 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500463 ::std::unique_ptr<Talon> right_drivetrain_talon_;
464};
465
Daniel Pettiadf38432015-01-26 17:13:35 -0800466class FridgeWriter : public LoopOutputHandler {
467 public:
468 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
469 left_arm_talon_ = ::std::move(t);
470 }
471
472 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
473 right_arm_talon_ = ::std::move(t);
474 }
475
476 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
477 left_elevator_talon_ = ::std::move(t);
478 }
479
480 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
481 right_elevator_talon_ = ::std::move(t);
482 }
483
484 private:
485 virtual void Read() override {
486 ::frc971::control_loops::fridge_queue.output.FetchAnother();
487 }
488
489 virtual void Write() override {
490 auto &queue = ::frc971::control_loops::fridge_queue.output;
491 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800492 left_arm_talon_->Set(queue->left_arm / 12.0);
493 right_arm_talon_->Set(-queue->right_arm / 12.0);
494 left_elevator_talon_->Set(queue->left_elevator / 12.0);
495 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800496 }
497
498 virtual void Stop() override {
499 LOG(WARNING, "Fridge output too old.\n");
500 left_arm_talon_->Disable();
501 right_arm_talon_->Disable();
502 left_elevator_talon_->Disable();
503 right_elevator_talon_->Disable();
504 }
505
506 ::std::unique_ptr<Talon> left_arm_talon_;
507 ::std::unique_ptr<Talon> right_arm_talon_;
508 ::std::unique_ptr<Talon> left_elevator_talon_;
509 ::std::unique_ptr<Talon> right_elevator_talon_;
510};
511
512class ClawWriter : public LoopOutputHandler {
513 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800514 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
515 left_intake_talon_ = ::std::move(t);
516 }
517
518 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
519 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800520 }
521
522 void set_wrist_talon(::std::unique_ptr<Talon> t) {
523 wrist_talon_ = ::std::move(t);
524 }
525
526 private:
527 virtual void Read() override {
528 ::frc971::control_loops::claw_queue.output.FetchAnother();
529 }
530
531 virtual void Write() override {
532 auto &queue = ::frc971::control_loops::claw_queue.output;
533 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800534 left_intake_talon_->Set(queue->intake_voltage / 12.0);
535 right_intake_talon_->Set(queue->intake_voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800536 wrist_talon_->Set(queue->voltage / 12.0);
537 }
538
539 virtual void Stop() override {
540 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800541 left_intake_talon_->Disable();
542 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800543 wrist_talon_->Disable();
544 }
545
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800546 ::std::unique_ptr<Talon> left_intake_talon_;
547 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800548 ::std::unique_ptr<Talon> wrist_talon_;
549};
550
Brian Silverman1f90d672015-01-26 20:20:45 -0500551// TODO(brian): Replace this with ::std::make_unique once all our toolchains
552// have support.
553template <class T, class... U>
554std::unique_ptr<T> make_unique(U &&... u) {
555 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
556}
557
Austin Schuh010eb812014-10-25 18:06:49 -0700558class WPILibRobot : public RobotBase {
559 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800560 ::std::unique_ptr<Encoder> encoder(int index) {
561 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
562 Encoder::k4X);
563 }
Austin Schuh010eb812014-10-25 18:06:49 -0700564 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500565 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800566 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500567
Brian Silverman98f6ee22015-01-26 17:50:12 -0500568 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700569 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800570 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500571 ::std::unique_ptr<Compressor> compressor(new Compressor());
572 compressor->SetClosedLoopControl(true);
573
Brian Silverman98f6ee22015-01-26 17:50:12 -0500574 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800575 LOG(INFO, "Creating the reader\n");
576 reader.set_arm_left_encoder(encoder(1));
577 reader.set_arm_left_index(make_unique<DigitalInput>(1));
578 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
579
580 reader.set_arm_right_encoder(encoder(5));
581 reader.set_arm_right_index(make_unique<DigitalInput>(5));
582 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
583
584 reader.set_elevator_left_encoder(encoder(0));
585 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
586 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
587
588 reader.set_elevator_right_encoder(encoder(4));
589 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
590 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
591
592 reader.set_wrist_encoder(encoder(6));
593 reader.set_wrist_index(make_unique<DigitalInput>(6));
594 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
595
596 reader.set_left_encoder(encoder(2));
597 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500598 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500599 ::std::thread reader_thread(::std::ref(reader));
600 GyroSender gyro_sender;
601 ::std::thread gyro_thread(::std::ref(gyro_sender));
602
603 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500604 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800605 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500606 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800607 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500608 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
609
Daniel Pettiadf38432015-01-26 17:13:35 -0800610 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
611 // claw.
612 FridgeWriter fridge_writer;
613 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800614 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800615 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800616 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800617 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800618 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800619 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800620 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800621 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
622
623 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800624 claw_writer.set_left_intake_talon(
625 ::std::unique_ptr<Talon>(new Talon(5)));
626 claw_writer.set_right_intake_talon(
627 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800628 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800629 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800630 ::std::thread claw_writer_thread(::std::ref(claw_writer));
631
632 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
633 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500634 SolenoidWriter solenoid_writer(pcm);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800635 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(1));
636 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(1));
637 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
638 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(3));
639 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500640 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
641
642 // Wait forever. Not much else to do...
643 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
644
Austin Schuh010eb812014-10-25 18:06:49 -0700645 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800646
Austin Schuh010eb812014-10-25 18:06:49 -0700647 joystick_sender.Quit();
648 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500649 reader.Quit();
650 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800651 gyro_sender.Quit();
652 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500653
654 drivetrain_writer.Quit();
655 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500656 solenoid_writer.Quit();
657 solenoid_thread.join();
658
Austin Schuh010eb812014-10-25 18:06:49 -0700659 ::aos::Cleanup();
660 }
661};
662
Brian Silverman98f6ee22015-01-26 17:50:12 -0500663} // namespace wpilib
664} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800665
Brian Silverman98f6ee22015-01-26 17:50:12 -0500666
667START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);