blob: 5315b0803747b4b48d42edb22a160c847896ceda [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
Austin Schuhfae69172015-02-20 22:11:26 -0800238 new_state->reader_pid = my_pid_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500239 new_state->outputs_enabled = ds_->IsSysActive();
240 new_state->browned_out = ds_->IsSysBrownedOut();
241
242 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
243 new_state->is_5v_active = ControllerPower::GetEnabled5V();
244 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
245 new_state->voltage_5v = ControllerPower::GetVoltage5V();
246
247 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
248 new_state->voltage_battery = ds_->GetBatteryVoltage();
249
Brian Silverman4d882aa2015-02-20 13:40:42 -0500250 LOG_STRUCT(DEBUG, "robot_state", *new_state);
251
Brian Silverman699f0cb2015-02-05 19:45:01 -0500252 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700253 }
254
Austin Schuh35d06612015-02-15 23:35:23 -0800255 {
256 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
257 drivetrain_message->right_encoder =
258 drivetrain_translate(right_encoder_->GetRaw());
259 drivetrain_message->left_encoder =
260 -drivetrain_translate(left_encoder_->GetRaw());
261
262 drivetrain_message.Send();
263 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500264
265 dma_synchronizer_->RunIteration();
266
Philipp Schrader82c65072015-02-16 00:47:09 +0000267 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500268
Brian Silverman335c20e2015-01-26 21:47:58 -0500269 {
270 auto fridge_message = fridge_queue.position.MakeMessage();
271 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500272 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800273 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500274 CopyPotAndIndexPosition(
275 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
276 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800277 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500278 CopyPotAndIndexPosition(
279 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500280 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800281 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500282 CopyPotAndIndexPosition(
283 elevator_right_encoder_, &fridge_message->elevator.right,
284 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800285 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500286 fridge_message.Send();
287 }
288
289 {
290 auto claw_message = claw_queue.position.MakeMessage();
291 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500292 claw_translate, claw_potentiometer_translate,
293 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500294 claw_message.Send();
295 }
Austin Schuh010eb812014-10-25 18:06:49 -0700296 }
297
298 void Quit() { run_ = false; }
299
300 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500301 static const int kPriority = 30;
302 static const int kInterruptPriority = 55;
303
Brian Silverman699f0cb2015-02-05 19:45:01 -0500304 int32_t my_pid_;
305 DriverStation *ds_;
306
Brian Silverman335c20e2015-01-26 21:47:58 -0500307 void CopyPotAndIndexPosition(
308 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
309 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500310 ::std::function<double(double)> potentiometer_translate, bool reverse,
311 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500312 const double multiplier = reverse ? -1.0 : 1.0;
313 position->encoder =
314 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500315 position->pot = multiplier * potentiometer_translate(
316 encoder.polled_potentiometer_voltage()) +
317 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500318 position->latched_encoder =
319 multiplier * encoder_translate(encoder.last_encoder_value());
320 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500321 multiplier *
322 potentiometer_translate(encoder.last_potentiometer_voltage()) +
323 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500324 position->index_pulses = encoder.index_posedge_count();
325 }
326
327 void CopyPotAndIndexPosition(
328 const InterruptEncoderAndPotentiometer &encoder,
329 PotAndIndexPosition *position,
330 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500331 ::std::function<double(double)> potentiometer_translate, bool reverse,
332 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500333 const double multiplier = reverse ? -1.0 : 1.0;
334 position->encoder =
335 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500336 position->pot = multiplier * potentiometer_translate(
337 encoder.potentiometer()->GetVoltage()) +
338 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500339 position->latched_encoder =
340 multiplier * encoder_translate(encoder.last_encoder_value());
341 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500342 multiplier *
343 potentiometer_translate(encoder.last_potentiometer_voltage()) +
344 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500345 position->index_pulses = encoder.index_posedge_count();
346 }
347
Brian Silverman335c20e2015-01-26 21:47:58 -0500348 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
349
350 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
351 elevator_left_encoder_, elevator_right_encoder_;
352
353 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
354
Austin Schuh010eb812014-10-25 18:06:49 -0700355 ::std::unique_ptr<Encoder> left_encoder_;
356 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700357
Brian Silverman1f90d672015-01-26 20:20:45 -0500358 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700359 DigitalGlitchFilter filter_;
360};
361
Brian Silvermand8f403a2014-12-13 19:12:04 -0500362class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700363 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500364 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800365 : pcm_(pcm),
366 fridge_(".frc971.control_loops.fridge.output"),
367 claw_(".frc971.control_loops.claw.output") {}
Brian Silvermand8f403a2014-12-13 19:12:04 -0500368
Daniel Pettiadf38432015-01-26 17:13:35 -0800369 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
370 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700371 }
372
Daniel Pettiadf38432015-01-26 17:13:35 -0800373 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
374 fridge_grabbers_top_back_ = ::std::move(s);
375 }
376
377 void set_fridge_grabbers_bottom_front(
378 ::std::unique_ptr<BufferedSolenoid> s) {
379 fridge_grabbers_bottom_front_ = ::std::move(s);
380 }
381
382 void set_fridge_grabbers_bottom_back(
383 ::std::unique_ptr<BufferedSolenoid> s) {
384 fridge_grabbers_bottom_back_ = ::std::move(s);
385 }
386
387 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
388 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500389 }
Austin Schuh010eb812014-10-25 18:06:49 -0700390
Brian Silvermand8f403a2014-12-13 19:12:04 -0500391 void operator()() {
392 ::aos::SetCurrentThreadName("Solenoids");
393 ::aos::SetCurrentThreadRealtimePriority(30);
394
395 while (run_) {
396 ::aos::time::PhasedLoopXMS(20, 1000);
397
398 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800399 fridge_.FetchLatest();
400 if (fridge_.get()) {
401 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
402 fridge_grabbers_top_front_->Set(fridge_->grabbers.top_front);
403 fridge_grabbers_top_back_->Set(fridge_->grabbers.top_back);
404 fridge_grabbers_bottom_front_->Set(fridge_->grabbers.bottom_front);
405 fridge_grabbers_bottom_back_->Set(fridge_->grabbers.bottom_back);
406 }
407 }
408
409 {
410 claw_.FetchLatest();
411 if (claw_.get()) {
412 LOG_STRUCT(DEBUG, "solenoids", *claw_);
413 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500414 }
415 }
416
Brian Silvermand8f403a2014-12-13 19:12:04 -0500417 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700418 }
419 }
420
Brian Silvermand8f403a2014-12-13 19:12:04 -0500421 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700422
Brian Silvermand8f403a2014-12-13 19:12:04 -0500423 private:
424 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800425 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
426 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
427 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
428 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
429 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Austin Schuh010eb812014-10-25 18:06:49 -0700430
Daniel Pettiadf38432015-01-26 17:13:35 -0800431 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
432 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700433
Brian Silvermand8f403a2014-12-13 19:12:04 -0500434 ::std::atomic<bool> run_{true};
435};
436
437class DrivetrainWriter : public LoopOutputHandler {
438 public:
439 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
440 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700441 }
442
Brian Silvermand8f403a2014-12-13 19:12:04 -0500443 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
444 right_drivetrain_talon_ = ::std::move(t);
445 }
Austin Schuh010eb812014-10-25 18:06:49 -0700446
Brian Silvermand8f403a2014-12-13 19:12:04 -0500447 private:
448 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500449 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500450 }
451
452 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500453 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500454 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800455 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
456 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500457 }
458
459 virtual void Stop() override {
460 LOG(WARNING, "drivetrain output too old\n");
461 left_drivetrain_talon_->Disable();
462 right_drivetrain_talon_->Disable();
463 }
464
Austin Schuh010eb812014-10-25 18:06:49 -0700465 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500466 ::std::unique_ptr<Talon> right_drivetrain_talon_;
467};
468
Daniel Pettiadf38432015-01-26 17:13:35 -0800469class FridgeWriter : public LoopOutputHandler {
470 public:
471 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
472 left_arm_talon_ = ::std::move(t);
473 }
474
475 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
476 right_arm_talon_ = ::std::move(t);
477 }
478
479 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
480 left_elevator_talon_ = ::std::move(t);
481 }
482
483 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
484 right_elevator_talon_ = ::std::move(t);
485 }
486
487 private:
488 virtual void Read() override {
489 ::frc971::control_loops::fridge_queue.output.FetchAnother();
490 }
491
492 virtual void Write() override {
493 auto &queue = ::frc971::control_loops::fridge_queue.output;
494 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800495 left_arm_talon_->Set(queue->left_arm / 12.0);
496 right_arm_talon_->Set(-queue->right_arm / 12.0);
497 left_elevator_talon_->Set(queue->left_elevator / 12.0);
498 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800499 }
500
501 virtual void Stop() override {
502 LOG(WARNING, "Fridge output too old.\n");
503 left_arm_talon_->Disable();
504 right_arm_talon_->Disable();
505 left_elevator_talon_->Disable();
506 right_elevator_talon_->Disable();
507 }
508
509 ::std::unique_ptr<Talon> left_arm_talon_;
510 ::std::unique_ptr<Talon> right_arm_talon_;
511 ::std::unique_ptr<Talon> left_elevator_talon_;
512 ::std::unique_ptr<Talon> right_elevator_talon_;
513};
514
515class ClawWriter : public LoopOutputHandler {
516 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800517 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
518 left_intake_talon_ = ::std::move(t);
519 }
520
521 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
522 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800523 }
524
525 void set_wrist_talon(::std::unique_ptr<Talon> t) {
526 wrist_talon_ = ::std::move(t);
527 }
528
529 private:
530 virtual void Read() override {
531 ::frc971::control_loops::claw_queue.output.FetchAnother();
532 }
533
534 virtual void Write() override {
535 auto &queue = ::frc971::control_loops::claw_queue.output;
536 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800537 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800538 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
539 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800540 }
541
542 virtual void Stop() override {
543 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800544 left_intake_talon_->Disable();
545 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800546 wrist_talon_->Disable();
547 }
548
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800549 ::std::unique_ptr<Talon> left_intake_talon_;
550 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800551 ::std::unique_ptr<Talon> wrist_talon_;
552};
553
Brian Silverman1f90d672015-01-26 20:20:45 -0500554// TODO(brian): Replace this with ::std::make_unique once all our toolchains
555// have support.
556template <class T, class... U>
557std::unique_ptr<T> make_unique(U &&... u) {
558 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
559}
560
Austin Schuh010eb812014-10-25 18:06:49 -0700561class WPILibRobot : public RobotBase {
562 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800563 ::std::unique_ptr<Encoder> encoder(int index) {
564 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
565 Encoder::k4X);
566 }
Austin Schuh010eb812014-10-25 18:06:49 -0700567 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500568 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800569 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500570
Brian Silverman98f6ee22015-01-26 17:50:12 -0500571 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700572 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800573 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500574 ::std::unique_ptr<Compressor> compressor(new Compressor());
575 compressor->SetClosedLoopControl(true);
576
Brian Silverman98f6ee22015-01-26 17:50:12 -0500577 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800578 LOG(INFO, "Creating the reader\n");
579 reader.set_arm_left_encoder(encoder(1));
580 reader.set_arm_left_index(make_unique<DigitalInput>(1));
581 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
582
583 reader.set_arm_right_encoder(encoder(5));
584 reader.set_arm_right_index(make_unique<DigitalInput>(5));
585 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
586
587 reader.set_elevator_left_encoder(encoder(0));
588 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
589 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
590
591 reader.set_elevator_right_encoder(encoder(4));
592 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
593 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
594
595 reader.set_wrist_encoder(encoder(6));
596 reader.set_wrist_index(make_unique<DigitalInput>(6));
597 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
598
599 reader.set_left_encoder(encoder(2));
600 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500601 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500602 ::std::thread reader_thread(::std::ref(reader));
603 GyroSender gyro_sender;
604 ::std::thread gyro_thread(::std::ref(gyro_sender));
605
606 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500607 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800608 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500609 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800610 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500611 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
612
Daniel Pettiadf38432015-01-26 17:13:35 -0800613 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
614 // claw.
615 FridgeWriter fridge_writer;
616 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800617 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800618 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800619 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800620 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800621 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800622 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800623 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800624 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
625
626 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800627 claw_writer.set_left_intake_talon(
628 ::std::unique_ptr<Talon>(new Talon(5)));
629 claw_writer.set_right_intake_talon(
630 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800631 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800632 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800633 ::std::thread claw_writer_thread(::std::ref(claw_writer));
634
635 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
636 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500637 SolenoidWriter solenoid_writer(pcm);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800638 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(1));
639 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(1));
640 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
641 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(3));
642 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500643 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
644
645 // Wait forever. Not much else to do...
646 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
647
Austin Schuh010eb812014-10-25 18:06:49 -0700648 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800649
Austin Schuh010eb812014-10-25 18:06:49 -0700650 joystick_sender.Quit();
651 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500652 reader.Quit();
653 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800654 gyro_sender.Quit();
655 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500656
657 drivetrain_writer.Quit();
658 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500659 solenoid_writer.Quit();
660 solenoid_thread.join();
661
Austin Schuh010eb812014-10-25 18:06:49 -0700662 ::aos::Cleanup();
663 }
664};
665
Brian Silverman98f6ee22015-01-26 17:50:12 -0500666} // namespace wpilib
667} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800668
Brian Silverman98f6ee22015-01-26 17:50:12 -0500669
670START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);