blob: c1d96288170868f8072c68afa957111d4de7c06a [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
21#undef ERROR
22
Austin Schuh010eb812014-10-25 18:06:49 -070023#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
Austin Schuh010eb812014-10-25 18:06:49 -070025#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040029#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070030#include "aos/linux_code/init.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050031#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070032
Brian Silverman335c20e2015-01-26 21:47:58 -050033#include "frc971/control_loops/control_loops.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050034
35#include "y2015/constants.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070036#include "y2015/control_loops/drivetrain/drivetrain.q.h"
37#include "y2015/control_loops/fridge/fridge.q.h"
38#include "y2015/control_loops/claw/claw.q.h"
Austin Schuhbb227f82015-09-06 15:27:52 -070039#include "y2015/autonomous/auto.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070040
Brian Silvermanda45b6c2014-12-28 11:36:50 -080041#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050042#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080045#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050046#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050047#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050048#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silverman87541532015-03-19 23:35:12 -070049#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050050#include "frc971/wpilib/wpilib_interface.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080051
Austin Schuh010eb812014-10-25 18:06:49 -070052#ifndef M_PI
53#define M_PI 3.14159265358979323846
54#endif
55
56using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050057using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050058using ::frc971::control_loops::fridge_queue;
59using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070060
61namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080062namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070063
Austin Schuh010eb812014-10-25 18:06:49 -070064double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080065 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080066 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080067 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080068 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
69}
70
71double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080072 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080073 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080074 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080075 (2 * M_PI /*radians*/);
76}
77
Brian Silverman5d712fc2015-02-15 03:39:31 -050078double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080079 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080080 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080081 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080082 (2 * M_PI /*radians*/);
83}
84
85double elevator_translate(int32_t in) {
86 return static_cast<double>(in) /
87 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080088 constants::GetValues().elev_encoder_ratio *
89 (2 * M_PI /*radians*/) *
90 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080091}
92
Brian Silverman5d712fc2015-02-15 03:39:31 -050093double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -080094 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080095 constants::GetValues().elev_pot_ratio *
96 (2 * M_PI /*radians*/) *
97 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -080098 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -080099}
100
101double claw_translate(int32_t in) {
102 return static_cast<double>(in) /
103 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800104 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800105 (2 * M_PI /*radians*/);
106}
107
Brian Silverman5d712fc2015-02-15 03:39:31 -0500108double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800109 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800110 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800111 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800112 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700113}
114
Brian Silverman335c20e2015-01-26 21:47:58 -0500115static const double kMaximumEncoderPulsesPerSecond =
116 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
117 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
118 4.0 /* index pulse = 1/4 cycle */;
119
Austin Schuh010eb812014-10-25 18:06:49 -0700120class SensorReader {
121 public:
Brian Silverman1f90d672015-01-26 20:20:45 -0500122 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500123 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
124 // we should ever see.
125 filter_.SetPeriodNanoSeconds(
126 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
127 }
128
129 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
130 filter_.Add(encoder.get());
131 arm_left_encoder_.set_encoder(::std::move(encoder));
132 }
133
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700134 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500135 filter_.Add(index.get());
136 arm_left_encoder_.set_index(::std::move(index));
137 }
138
139 void set_arm_left_potentiometer(
140 ::std::unique_ptr<AnalogInput> potentiometer) {
141 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
142 }
143
144 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
145 filter_.Add(encoder.get());
146 arm_right_encoder_.set_encoder(::std::move(encoder));
147 }
148
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700149 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500150 filter_.Add(index.get());
151 arm_right_encoder_.set_index(::std::move(index));
152 }
153
154 void set_arm_right_potentiometer(
155 ::std::unique_ptr<AnalogInput> potentiometer) {
156 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
157 }
158
159 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
160 filter_.Add(encoder.get());
161 elevator_left_encoder_.set_encoder(::std::move(encoder));
162 }
163
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700164 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500165 filter_.Add(index.get());
166 elevator_left_encoder_.set_index(::std::move(index));
167 }
168
169 void set_elevator_left_potentiometer(
170 ::std::unique_ptr<AnalogInput> potentiometer) {
171 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
172 }
173
174 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
175 filter_.Add(encoder.get());
176 elevator_right_encoder_.set_encoder(::std::move(encoder));
177 }
178
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700179 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500180 filter_.Add(index.get());
181 elevator_right_encoder_.set_index(::std::move(index));
182 }
183
184 void set_elevator_right_potentiometer(
185 ::std::unique_ptr<AnalogInput> potentiometer) {
186 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
187 }
188
189 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
190 filter_.Add(encoder.get());
191 wrist_encoder_.set_encoder(::std::move(encoder));
192 }
193
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700194 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500195 filter_.Add(index.get());
196 wrist_encoder_.set_index(::std::move(index));
197 }
198
199 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
200 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700201 }
202
Brian Silverman1f90d672015-01-26 20:20:45 -0500203 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
204 left_encoder_ = ::std::move(left_encoder);
205 }
206
207 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
208 right_encoder_ = ::std::move(right_encoder);
209 }
210
Brian Silverman335c20e2015-01-26 21:47:58 -0500211 // All of the DMA-related set_* calls must be made before this, and it doesn't
212 // hurt to do all of them.
213 void set_dma(::std::unique_ptr<DMA> dma) {
214 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
215 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500216 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800217 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500218 dma_synchronizer_->Add(&elevator_right_encoder_);
219 }
220
Austin Schuh010eb812014-10-25 18:06:49 -0700221 void operator()() {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800222 LOG(INFO, "In sensor reader thread\n");
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 Silverman699f0cb2015-02-05 19:45:01 -0500232
Brian Silverman335c20e2015-01-26 21:47:58 -0500233 wrist_encoder_.Start();
234 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800235 LOG(INFO, "Things are now started\n");
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 Silverman811f8ec2015-12-06 01:29:42 -0500247 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
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_;
300
Brian Silverman335c20e2015-01-26 21:47:58 -0500301 void CopyPotAndIndexPosition(
302 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
303 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500304 ::std::function<double(double)> potentiometer_translate, bool reverse,
305 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500306 const double multiplier = reverse ? -1.0 : 1.0;
307 position->encoder =
308 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500309 position->pot = multiplier * potentiometer_translate(
310 encoder.polled_potentiometer_voltage()) +
311 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500312 position->latched_encoder =
313 multiplier * encoder_translate(encoder.last_encoder_value());
314 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500315 multiplier *
316 potentiometer_translate(encoder.last_potentiometer_voltage()) +
317 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500318 position->index_pulses = encoder.index_posedge_count();
319 }
320
321 void CopyPotAndIndexPosition(
322 const InterruptEncoderAndPotentiometer &encoder,
323 PotAndIndexPosition *position,
324 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500325 ::std::function<double(double)> potentiometer_translate, bool reverse,
326 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500327 const double multiplier = reverse ? -1.0 : 1.0;
328 position->encoder =
329 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500330 position->pot = multiplier * potentiometer_translate(
331 encoder.potentiometer()->GetVoltage()) +
332 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500333 position->latched_encoder =
334 multiplier * encoder_translate(encoder.last_encoder_value());
335 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500336 multiplier *
337 potentiometer_translate(encoder.last_potentiometer_voltage()) +
338 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500339 position->index_pulses = encoder.index_posedge_count();
340 }
341
Brian Silverman335c20e2015-01-26 21:47:58 -0500342 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
343
344 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
345 elevator_left_encoder_, elevator_right_encoder_;
346
347 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
348
Austin Schuh010eb812014-10-25 18:06:49 -0700349 ::std::unique_ptr<Encoder> left_encoder_;
350 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700351
Brian Silverman1f90d672015-01-26 20:20:45 -0500352 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700353 DigitalGlitchFilter filter_;
354};
355
Brian Silvermand8f403a2014-12-13 19:12:04 -0500356class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700357 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500358 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800359 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800360 fridge_(".frc971.control_loops.fridge_queue.output"),
361 claw_(".frc971.control_loops.claw_queue.output") {}
362
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700363 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800364 pressure_switch_ = ::std::move(pressure_switch);
365 }
366
367 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
368 compressor_relay_ = ::std::move(compressor_relay);
369 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500370
Daniel Pettiadf38432015-01-26 17:13:35 -0800371 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
372 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700373 }
374
Daniel Pettiadf38432015-01-26 17:13:35 -0800375 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
376 fridge_grabbers_top_back_ = ::std::move(s);
377 }
378
379 void set_fridge_grabbers_bottom_front(
380 ::std::unique_ptr<BufferedSolenoid> s) {
381 fridge_grabbers_bottom_front_ = ::std::move(s);
382 }
383
384 void set_fridge_grabbers_bottom_back(
385 ::std::unique_ptr<BufferedSolenoid> s) {
386 fridge_grabbers_bottom_back_ = ::std::move(s);
387 }
388
389 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
390 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500391 }
Austin Schuh010eb812014-10-25 18:06:49 -0700392
Brian Silverman93936f72015-03-19 23:38:30 -0700393 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
394 grabber_latch_release_ = ::std::move(s);
395 }
396
397 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
398 grabber_fold_up_ = ::std::move(s);
399 }
400
Brian Silvermand8f403a2014-12-13 19:12:04 -0500401 void operator()() {
402 ::aos::SetCurrentThreadName("Solenoids");
403 ::aos::SetCurrentThreadRealtimePriority(30);
404
405 while (run_) {
406 ::aos::time::PhasedLoopXMS(20, 1000);
407
408 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800409 fridge_.FetchLatest();
410 if (fridge_.get()) {
411 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800412 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
413 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
414 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
415 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800416 }
417 }
418
419 {
420 claw_.FetchLatest();
421 if (claw_.get()) {
422 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800423 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500424 }
425 }
426
Brian Silverman93936f72015-03-19 23:38:30 -0700427 ::aos::joystick_state.FetchLatest();
428 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
429 ::aos::joystick_state->autonomous);
430 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
431 ::aos::joystick_state->joysticks[1].buttons & 1);
432
Brian Silverman87541532015-03-19 23:35:12 -0700433 {
434 PneumaticsToLog to_log;
435 {
436 const bool compressor_on = !pressure_switch_->Get();
437 to_log.compressor_on = compressor_on;
438 if (compressor_on) {
439 compressor_relay_->Set(Relay::kForward);
440 } else {
441 compressor_relay_->Set(Relay::kOff);
442 }
443 }
444
445 pcm_->Flush();
446 to_log.read_solenoids = pcm_->GetAll();
447 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
448 }
Austin Schuh010eb812014-10-25 18:06:49 -0700449 }
450 }
451
Brian Silvermand8f403a2014-12-13 19:12:04 -0500452 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700453
Brian Silvermand8f403a2014-12-13 19:12:04 -0500454 private:
455 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800456 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
457 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
458 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
459 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
460 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700461 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
462 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700463 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800464 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700465
Daniel Pettiadf38432015-01-26 17:13:35 -0800466 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
467 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700468
Brian Silvermand8f403a2014-12-13 19:12:04 -0500469 ::std::atomic<bool> run_{true};
470};
471
Austin Schuhbb227f82015-09-06 15:27:52 -0700472class CanWriter : public LoopOutputHandler {
473 public:
474 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
475
476 void set_can_talon(::std::unique_ptr<Talon> t) {
477 can_talon_ = ::std::move(t);
478 }
479
480 private:
481 virtual void Read() override {
482 ::frc971::autonomous::can_control.FetchAnother();
483 }
484
485 virtual void Write() override {
486 auto &queue = ::frc971::autonomous::can_control;
487 LOG_STRUCT(DEBUG, "will output", *queue);
488 can_talon_->Set(queue->can_voltage / 12.0);
489 }
490
491 virtual void Stop() override {
492 LOG(WARNING, "Can output too old\n");
493 can_talon_->Disable();
494 }
495
496 ::std::unique_ptr<Talon> can_talon_;
497};
498
Brian Silvermand8f403a2014-12-13 19:12:04 -0500499class DrivetrainWriter : public LoopOutputHandler {
500 public:
501 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
502 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700503 }
504
Brian Silvermand8f403a2014-12-13 19:12:04 -0500505 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
506 right_drivetrain_talon_ = ::std::move(t);
507 }
Austin Schuh010eb812014-10-25 18:06:49 -0700508
Brian Silvermand8f403a2014-12-13 19:12:04 -0500509 private:
510 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500511 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500512 }
513
514 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500515 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500516 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800517 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
518 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500519 }
520
521 virtual void Stop() override {
522 LOG(WARNING, "drivetrain output too old\n");
523 left_drivetrain_talon_->Disable();
524 right_drivetrain_talon_->Disable();
525 }
526
Austin Schuh010eb812014-10-25 18:06:49 -0700527 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500528 ::std::unique_ptr<Talon> right_drivetrain_talon_;
529};
530
Daniel Pettiadf38432015-01-26 17:13:35 -0800531class FridgeWriter : public LoopOutputHandler {
532 public:
533 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
534 left_arm_talon_ = ::std::move(t);
535 }
536
537 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
538 right_arm_talon_ = ::std::move(t);
539 }
540
541 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
542 left_elevator_talon_ = ::std::move(t);
543 }
544
545 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
546 right_elevator_talon_ = ::std::move(t);
547 }
548
549 private:
550 virtual void Read() override {
551 ::frc971::control_loops::fridge_queue.output.FetchAnother();
552 }
553
554 virtual void Write() override {
555 auto &queue = ::frc971::control_loops::fridge_queue.output;
556 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800557 left_arm_talon_->Set(queue->left_arm / 12.0);
558 right_arm_talon_->Set(-queue->right_arm / 12.0);
559 left_elevator_talon_->Set(queue->left_elevator / 12.0);
560 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800561 }
562
563 virtual void Stop() override {
564 LOG(WARNING, "Fridge output too old.\n");
565 left_arm_talon_->Disable();
566 right_arm_talon_->Disable();
567 left_elevator_talon_->Disable();
568 right_elevator_talon_->Disable();
569 }
570
571 ::std::unique_ptr<Talon> left_arm_talon_;
572 ::std::unique_ptr<Talon> right_arm_talon_;
573 ::std::unique_ptr<Talon> left_elevator_talon_;
574 ::std::unique_ptr<Talon> right_elevator_talon_;
575};
576
577class ClawWriter : public LoopOutputHandler {
578 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800579 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
580 left_intake_talon_ = ::std::move(t);
581 }
582
583 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
584 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800585 }
586
587 void set_wrist_talon(::std::unique_ptr<Talon> t) {
588 wrist_talon_ = ::std::move(t);
589 }
590
591 private:
592 virtual void Read() override {
593 ::frc971::control_loops::claw_queue.output.FetchAnother();
594 }
595
596 virtual void Write() override {
597 auto &queue = ::frc971::control_loops::claw_queue.output;
598 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800599 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800600 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
601 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800602 }
603
604 virtual void Stop() override {
605 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800606 left_intake_talon_->Disable();
607 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800608 wrist_talon_->Disable();
609 }
610
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800611 ::std::unique_ptr<Talon> left_intake_talon_;
612 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800613 ::std::unique_ptr<Talon> wrist_talon_;
614};
615
Brian Silverman1f90d672015-01-26 20:20:45 -0500616// TODO(brian): Replace this with ::std::make_unique once all our toolchains
617// have support.
618template <class T, class... U>
619std::unique_ptr<T> make_unique(U &&... u) {
620 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
621}
622
Austin Schuh010eb812014-10-25 18:06:49 -0700623class WPILibRobot : public RobotBase {
624 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800625 ::std::unique_ptr<Encoder> encoder(int index) {
626 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
627 Encoder::k4X);
628 }
Austin Schuh010eb812014-10-25 18:06:49 -0700629 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500630 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800631 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500632
Brian Silverman98f6ee22015-01-26 17:50:12 -0500633 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700634 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800635 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500636
Brian Silverman98f6ee22015-01-26 17:50:12 -0500637 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800638 LOG(INFO, "Creating the reader\n");
639 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);