blob: 05ef948e4d251935396826d81fe83041bc26167e [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 Schuh6d5d9ae2015-10-31 19:39:57 -070010#include "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
15#include "Relay.h"
16#include "RobotBase.h"
17#include "dma.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -050021#include "PowerDistributionPanel.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070022#undef ERROR
23
Austin Schuh010eb812014-10-25 18:06:49 -070024#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
Austin Schuh010eb812014-10-25 18:06:49 -070026#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040030#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070031#include "aos/linux_code/init.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050032#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070033
Brian Silverman335c20e2015-01-26 21:47:58 -050034#include "frc971/control_loops/control_loops.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050035
36#include "y2015/constants.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070037#include "y2015/control_loops/drivetrain/drivetrain.q.h"
38#include "y2015/control_loops/fridge/fridge.q.h"
39#include "y2015/control_loops/claw/claw.q.h"
Austin Schuhbb227f82015-09-06 15:27:52 -070040#include "y2015/autonomous/auto.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070041
Brian Silvermanda45b6c2014-12-28 11:36:50 -080042#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050043#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/buffered_solenoid.h"
45#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080046#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050047#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050048#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050049#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silverman87541532015-03-19 23:35:12 -070050#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050051#include "frc971/wpilib/wpilib_interface.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080052
Austin Schuh010eb812014-10-25 18:06:49 -070053#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050058using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050059using ::frc971::control_loops::fridge_queue;
60using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070061
62namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080063namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070064
Austin Schuh010eb812014-10-25 18:06:49 -070065double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080066 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080067 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080068 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080069 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
70}
71
72double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080073 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080074 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080075 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080076 (2 * M_PI /*radians*/);
77}
78
Brian Silverman5d712fc2015-02-15 03:39:31 -050079double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080080 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080081 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080082 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080083 (2 * M_PI /*radians*/);
84}
85
86double elevator_translate(int32_t in) {
87 return static_cast<double>(in) /
88 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080089 constants::GetValues().elev_encoder_ratio *
90 (2 * M_PI /*radians*/) *
91 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080092}
93
Brian Silverman5d712fc2015-02-15 03:39:31 -050094double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -080095 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080096 constants::GetValues().elev_pot_ratio *
97 (2 * M_PI /*radians*/) *
98 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -080099 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -0800100}
101
102double claw_translate(int32_t in) {
103 return static_cast<double>(in) /
104 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800105 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800106 (2 * M_PI /*radians*/);
107}
108
Brian Silverman5d712fc2015-02-15 03:39:31 -0500109double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800110 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800111 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800112 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800113 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700114}
115
Brian Silverman335c20e2015-01-26 21:47:58 -0500116static const double kMaximumEncoderPulsesPerSecond =
117 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
118 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
119 4.0 /* index pulse = 1/4 cycle */;
120
Austin Schuh010eb812014-10-25 18:06:49 -0700121class SensorReader {
122 public:
Brian Silverman1f90d672015-01-26 20:20:45 -0500123 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500124 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
125 // we should ever see.
126 filter_.SetPeriodNanoSeconds(
127 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
128 }
129
130 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
131 filter_.Add(encoder.get());
132 arm_left_encoder_.set_encoder(::std::move(encoder));
133 }
134
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700135 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500136 filter_.Add(index.get());
137 arm_left_encoder_.set_index(::std::move(index));
138 }
139
140 void set_arm_left_potentiometer(
141 ::std::unique_ptr<AnalogInput> potentiometer) {
142 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
143 }
144
145 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
146 filter_.Add(encoder.get());
147 arm_right_encoder_.set_encoder(::std::move(encoder));
148 }
149
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700150 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500151 filter_.Add(index.get());
152 arm_right_encoder_.set_index(::std::move(index));
153 }
154
155 void set_arm_right_potentiometer(
156 ::std::unique_ptr<AnalogInput> potentiometer) {
157 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
158 }
159
160 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
161 filter_.Add(encoder.get());
162 elevator_left_encoder_.set_encoder(::std::move(encoder));
163 }
164
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700165 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500166 filter_.Add(index.get());
167 elevator_left_encoder_.set_index(::std::move(index));
168 }
169
170 void set_elevator_left_potentiometer(
171 ::std::unique_ptr<AnalogInput> potentiometer) {
172 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
173 }
174
175 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
176 filter_.Add(encoder.get());
177 elevator_right_encoder_.set_encoder(::std::move(encoder));
178 }
179
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700180 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500181 filter_.Add(index.get());
182 elevator_right_encoder_.set_index(::std::move(index));
183 }
184
185 void set_elevator_right_potentiometer(
186 ::std::unique_ptr<AnalogInput> potentiometer) {
187 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
188 }
189
190 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
191 filter_.Add(encoder.get());
192 wrist_encoder_.set_encoder(::std::move(encoder));
193 }
194
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700195 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500196 filter_.Add(index.get());
197 wrist_encoder_.set_index(::std::move(index));
198 }
199
200 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
201 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700202 }
203
Brian Silverman1f90d672015-01-26 20:20:45 -0500204 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
205 left_encoder_ = ::std::move(left_encoder);
206 }
207
208 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
209 right_encoder_ = ::std::move(right_encoder);
210 }
211
Brian Silverman335c20e2015-01-26 21:47:58 -0500212 // All of the DMA-related set_* calls must be made before this, and it doesn't
213 // hurt to do all of them.
214 void set_dma(::std::unique_ptr<DMA> dma) {
215 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
216 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500217 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800218 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500219 dma_synchronizer_->Add(&elevator_right_encoder_);
220 }
221
Austin Schuh010eb812014-10-25 18:06:49 -0700222 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -0800223 ::aos::SetCurrentThreadName("SensorReader");
224
Brian Silverman699f0cb2015-02-05 19:45:01 -0500225 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700226 ds_ =
227#ifdef WPILIB2015
228 DriverStation::GetInstance();
229#else
230 &DriverStation::GetInstance();
231#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500232 pdp_.reset(new PowerDistributionPanel());
Brian Silverman699f0cb2015-02-05 19:45:01 -0500233
Brian Silverman335c20e2015-01-26 21:47:58 -0500234 wrist_encoder_.Start();
235 dma_synchronizer_->Start();
Austin Schuh010eb812014-10-25 18:06:49 -0700236
Brian Silverman2fe007c2014-12-28 12:20:01 -0800237 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700238 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800239 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700240 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700241 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500242
243 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700244 }
245
246 void RunIteration() {
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500247 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
Austin Schuh010eb812014-10-25 18:06:49 -0700248
Austin Schuh35d06612015-02-15 23:35:23 -0800249 {
250 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
251 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800252 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800253 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800254 drivetrain_translate(left_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800255
256 drivetrain_message.Send();
257 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500258
259 dma_synchronizer_->RunIteration();
260
Philipp Schrader82c65072015-02-16 00:47:09 +0000261 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500262
Brian Silverman335c20e2015-01-26 21:47:58 -0500263 {
264 auto fridge_message = fridge_queue.position.MakeMessage();
265 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500266 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800267 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500268 CopyPotAndIndexPosition(
269 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
270 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800271 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500272 CopyPotAndIndexPosition(
273 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500274 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800275 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500276 CopyPotAndIndexPosition(
277 elevator_right_encoder_, &fridge_message->elevator.right,
278 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800279 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500280 fridge_message.Send();
281 }
282
283 {
284 auto claw_message = claw_queue.position.MakeMessage();
285 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500286 claw_translate, claw_potentiometer_translate,
287 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500288 claw_message.Send();
289 }
Austin Schuh010eb812014-10-25 18:06:49 -0700290 }
291
292 void Quit() { run_ = false; }
293
294 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500295 static const int kPriority = 30;
296 static const int kInterruptPriority = 55;
297
Brian Silverman699f0cb2015-02-05 19:45:01 -0500298 int32_t my_pid_;
299 DriverStation *ds_;
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500300 ::std::unique_ptr<PowerDistributionPanel> pdp_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500301
Brian Silverman335c20e2015-01-26 21:47:58 -0500302 void CopyPotAndIndexPosition(
303 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
304 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500305 ::std::function<double(double)> potentiometer_translate, bool reverse,
306 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500307 const double multiplier = reverse ? -1.0 : 1.0;
308 position->encoder =
309 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500310 position->pot = multiplier * potentiometer_translate(
311 encoder.polled_potentiometer_voltage()) +
312 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500313 position->latched_encoder =
314 multiplier * encoder_translate(encoder.last_encoder_value());
315 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500316 multiplier *
317 potentiometer_translate(encoder.last_potentiometer_voltage()) +
318 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500319 position->index_pulses = encoder.index_posedge_count();
320 }
321
322 void CopyPotAndIndexPosition(
323 const InterruptEncoderAndPotentiometer &encoder,
324 PotAndIndexPosition *position,
325 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500326 ::std::function<double(double)> potentiometer_translate, bool reverse,
327 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500328 const double multiplier = reverse ? -1.0 : 1.0;
329 position->encoder =
330 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500331 position->pot = multiplier * potentiometer_translate(
332 encoder.potentiometer()->GetVoltage()) +
333 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500334 position->latched_encoder =
335 multiplier * encoder_translate(encoder.last_encoder_value());
336 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500337 multiplier *
338 potentiometer_translate(encoder.last_potentiometer_voltage()) +
339 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500340 position->index_pulses = encoder.index_posedge_count();
341 }
342
Brian Silverman335c20e2015-01-26 21:47:58 -0500343 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
344
345 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
346 elevator_left_encoder_, elevator_right_encoder_;
347
348 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
349
Austin Schuh010eb812014-10-25 18:06:49 -0700350 ::std::unique_ptr<Encoder> left_encoder_;
351 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700352
Brian Silverman1f90d672015-01-26 20:20:45 -0500353 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700354 DigitalGlitchFilter filter_;
355};
356
Brian Silvermand8f403a2014-12-13 19:12:04 -0500357class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700358 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500359 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800360 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800361 fridge_(".frc971.control_loops.fridge_queue.output"),
362 claw_(".frc971.control_loops.claw_queue.output") {}
363
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700364 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800365 pressure_switch_ = ::std::move(pressure_switch);
366 }
367
368 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
369 compressor_relay_ = ::std::move(compressor_relay);
370 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500371
Daniel Pettiadf38432015-01-26 17:13:35 -0800372 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
373 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700374 }
375
Daniel Pettiadf38432015-01-26 17:13:35 -0800376 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
377 fridge_grabbers_top_back_ = ::std::move(s);
378 }
379
380 void set_fridge_grabbers_bottom_front(
381 ::std::unique_ptr<BufferedSolenoid> s) {
382 fridge_grabbers_bottom_front_ = ::std::move(s);
383 }
384
385 void set_fridge_grabbers_bottom_back(
386 ::std::unique_ptr<BufferedSolenoid> s) {
387 fridge_grabbers_bottom_back_ = ::std::move(s);
388 }
389
390 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
391 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500392 }
Austin Schuh010eb812014-10-25 18:06:49 -0700393
Brian Silverman93936f72015-03-19 23:38:30 -0700394 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
395 grabber_latch_release_ = ::std::move(s);
396 }
397
398 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
399 grabber_fold_up_ = ::std::move(s);
400 }
401
Brian Silvermand8f403a2014-12-13 19:12:04 -0500402 void operator()() {
403 ::aos::SetCurrentThreadName("Solenoids");
404 ::aos::SetCurrentThreadRealtimePriority(30);
405
406 while (run_) {
407 ::aos::time::PhasedLoopXMS(20, 1000);
408
409 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800410 fridge_.FetchLatest();
411 if (fridge_.get()) {
412 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800413 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
414 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
415 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
416 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800417 }
418 }
419
420 {
421 claw_.FetchLatest();
422 if (claw_.get()) {
423 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800424 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500425 }
426 }
427
Brian Silverman93936f72015-03-19 23:38:30 -0700428 ::aos::joystick_state.FetchLatest();
429 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
430 ::aos::joystick_state->autonomous);
431 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
432 ::aos::joystick_state->joysticks[1].buttons & 1);
433
Brian Silverman87541532015-03-19 23:35:12 -0700434 {
435 PneumaticsToLog to_log;
436 {
437 const bool compressor_on = !pressure_switch_->Get();
438 to_log.compressor_on = compressor_on;
439 if (compressor_on) {
440 compressor_relay_->Set(Relay::kForward);
441 } else {
442 compressor_relay_->Set(Relay::kOff);
443 }
444 }
445
446 pcm_->Flush();
447 to_log.read_solenoids = pcm_->GetAll();
448 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
449 }
Austin Schuh010eb812014-10-25 18:06:49 -0700450 }
451 }
452
Brian Silvermand8f403a2014-12-13 19:12:04 -0500453 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700454
Brian Silvermand8f403a2014-12-13 19:12:04 -0500455 private:
456 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800457 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
458 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
459 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
460 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
461 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700462 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
463 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700464 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800465 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700466
Daniel Pettiadf38432015-01-26 17:13:35 -0800467 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
468 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700469
Brian Silvermand8f403a2014-12-13 19:12:04 -0500470 ::std::atomic<bool> run_{true};
471};
472
Austin Schuhbb227f82015-09-06 15:27:52 -0700473class CanWriter : public LoopOutputHandler {
474 public:
475 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
476
477 void set_can_talon(::std::unique_ptr<Talon> t) {
478 can_talon_ = ::std::move(t);
479 }
480
481 private:
482 virtual void Read() override {
483 ::frc971::autonomous::can_control.FetchAnother();
484 }
485
486 virtual void Write() override {
487 auto &queue = ::frc971::autonomous::can_control;
488 LOG_STRUCT(DEBUG, "will output", *queue);
489 can_talon_->Set(queue->can_voltage / 12.0);
490 }
491
492 virtual void Stop() override {
493 LOG(WARNING, "Can output too old\n");
494 can_talon_->Disable();
495 }
496
497 ::std::unique_ptr<Talon> can_talon_;
498};
499
Brian Silvermand8f403a2014-12-13 19:12:04 -0500500class DrivetrainWriter : public LoopOutputHandler {
501 public:
502 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
503 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700504 }
505
Brian Silvermand8f403a2014-12-13 19:12:04 -0500506 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
507 right_drivetrain_talon_ = ::std::move(t);
508 }
Austin Schuh010eb812014-10-25 18:06:49 -0700509
Brian Silvermand8f403a2014-12-13 19:12:04 -0500510 private:
511 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500512 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500513 }
514
515 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500516 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500517 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800518 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
519 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500520 }
521
522 virtual void Stop() override {
523 LOG(WARNING, "drivetrain output too old\n");
524 left_drivetrain_talon_->Disable();
525 right_drivetrain_talon_->Disable();
526 }
527
Austin Schuh010eb812014-10-25 18:06:49 -0700528 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500529 ::std::unique_ptr<Talon> right_drivetrain_talon_;
530};
531
Daniel Pettiadf38432015-01-26 17:13:35 -0800532class FridgeWriter : public LoopOutputHandler {
533 public:
534 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
535 left_arm_talon_ = ::std::move(t);
536 }
537
538 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
539 right_arm_talon_ = ::std::move(t);
540 }
541
542 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
543 left_elevator_talon_ = ::std::move(t);
544 }
545
546 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
547 right_elevator_talon_ = ::std::move(t);
548 }
549
550 private:
551 virtual void Read() override {
552 ::frc971::control_loops::fridge_queue.output.FetchAnother();
553 }
554
555 virtual void Write() override {
556 auto &queue = ::frc971::control_loops::fridge_queue.output;
557 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800558 left_arm_talon_->Set(queue->left_arm / 12.0);
559 right_arm_talon_->Set(-queue->right_arm / 12.0);
560 left_elevator_talon_->Set(queue->left_elevator / 12.0);
561 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800562 }
563
564 virtual void Stop() override {
565 LOG(WARNING, "Fridge output too old.\n");
566 left_arm_talon_->Disable();
567 right_arm_talon_->Disable();
568 left_elevator_talon_->Disable();
569 right_elevator_talon_->Disable();
570 }
571
572 ::std::unique_ptr<Talon> left_arm_talon_;
573 ::std::unique_ptr<Talon> right_arm_talon_;
574 ::std::unique_ptr<Talon> left_elevator_talon_;
575 ::std::unique_ptr<Talon> right_elevator_talon_;
576};
577
578class ClawWriter : public LoopOutputHandler {
579 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800580 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
581 left_intake_talon_ = ::std::move(t);
582 }
583
584 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
585 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800586 }
587
588 void set_wrist_talon(::std::unique_ptr<Talon> t) {
589 wrist_talon_ = ::std::move(t);
590 }
591
592 private:
593 virtual void Read() override {
594 ::frc971::control_loops::claw_queue.output.FetchAnother();
595 }
596
597 virtual void Write() override {
598 auto &queue = ::frc971::control_loops::claw_queue.output;
599 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800600 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800601 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
602 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800603 }
604
605 virtual void Stop() override {
606 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800607 left_intake_talon_->Disable();
608 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800609 wrist_talon_->Disable();
610 }
611
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800612 ::std::unique_ptr<Talon> left_intake_talon_;
613 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800614 ::std::unique_ptr<Talon> wrist_talon_;
615};
616
Brian Silverman1f90d672015-01-26 20:20:45 -0500617// TODO(brian): Replace this with ::std::make_unique once all our toolchains
618// have support.
619template <class T, class... U>
620std::unique_ptr<T> make_unique(U &&... u) {
621 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
622}
623
Austin Schuh010eb812014-10-25 18:06:49 -0700624class WPILibRobot : public RobotBase {
625 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800626 ::std::unique_ptr<Encoder> encoder(int index) {
627 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
628 Encoder::k4X);
629 }
Austin Schuh010eb812014-10-25 18:06:49 -0700630 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500631 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800632 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500633
Brian Silverman98f6ee22015-01-26 17:50:12 -0500634 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700635 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800636 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500637
Brian Silverman98f6ee22015-01-26 17:50:12 -0500638 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800639 reader.set_arm_left_encoder(encoder(1));
640 reader.set_arm_left_index(make_unique<DigitalInput>(1));
641 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
642
643 reader.set_arm_right_encoder(encoder(5));
644 reader.set_arm_right_index(make_unique<DigitalInput>(5));
645 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
646
647 reader.set_elevator_left_encoder(encoder(0));
648 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
649 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
650
651 reader.set_elevator_right_encoder(encoder(4));
652 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
653 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
654
655 reader.set_wrist_encoder(encoder(6));
656 reader.set_wrist_index(make_unique<DigitalInput>(6));
657 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
658
659 reader.set_left_encoder(encoder(2));
660 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500661 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500662 ::std::thread reader_thread(::std::ref(reader));
663 GyroSender gyro_sender;
664 ::std::thread gyro_thread(::std::ref(gyro_sender));
665
666 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500667 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800668 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500669 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800670 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500671 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
672
Austin Schuhbb227f82015-09-06 15:27:52 -0700673 CanWriter can_writer;
674 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
675 ::std::thread can_writer_thread(::std::ref(can_writer));
676
Daniel Pettiadf38432015-01-26 17:13:35 -0800677 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
678 // claw.
679 FridgeWriter fridge_writer;
680 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800681 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800682 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800683 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800684 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800685 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800686 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800687 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800688 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
689
690 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800691 claw_writer.set_left_intake_talon(
692 ::std::unique_ptr<Talon>(new Talon(5)));
693 claw_writer.set_right_intake_talon(
694 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800695 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800696 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800697 ::std::thread claw_writer_thread(::std::ref(claw_writer));
698
699 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
700 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500701 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800702 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
703 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800704 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800705 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
706 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700707 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
708 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800709
710 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
711 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500712 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
713
714 // Wait forever. Not much else to do...
715 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
716
Austin Schuh010eb812014-10-25 18:06:49 -0700717 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800718
Austin Schuh010eb812014-10-25 18:06:49 -0700719 joystick_sender.Quit();
720 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500721 reader.Quit();
722 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800723 gyro_sender.Quit();
724 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500725
726 drivetrain_writer.Quit();
727 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700728 can_writer.Quit();
729 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500730 solenoid_writer.Quit();
731 solenoid_thread.join();
732
Austin Schuh010eb812014-10-25 18:06:49 -0700733 ::aos::Cleanup();
734 }
735};
736
Brian Silverman98f6ee22015-01-26 17:50:12 -0500737} // namespace wpilib
738} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800739
Brian Silverman98f6ee22015-01-26 17:50:12 -0500740
741START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);