blob: 7accc921c3bf60d124dc60f405a07dd6db4bdf64 [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
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Brian Silvermand8f403a2014-12-13 19:12:04 -05007#include <functional>
Austin Schuh8aec1ed2016-05-01 13:29:20 -07008#include <mutex>
9#include <thread>
Brian Silvermand8f403a2014-12-13 19:12:04 -050010
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070011#include "Encoder.h"
12#include "Talon.h"
13#include "DriverStation.h"
14#include "AnalogInput.h"
15#include "Compressor.h"
16#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080017#include "frc971/wpilib/wpilib_robot_base.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
Austin Schuh70810b92016-11-26 14:55:34 -080035#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050036#include "frc971/wpilib/buffered_pcm.h"
Austin Schuh70810b92016-11-26 14:55:34 -080037#include "frc971/wpilib/buffered_solenoid.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050038#include "frc971/wpilib/dma.h"
Austin Schuh70810b92016-11-26 14:55:34 -080039#include "frc971/wpilib/dma_edge_counting.h"
40#include "frc971/wpilib/encoder_and_potentiometer.h"
41#include "frc971/wpilib/gyro_sender.h"
42#include "frc971/wpilib/interrupt_edge_counting.h"
43#include "frc971/wpilib/joystick_sender.h"
44#include "frc971/wpilib/logging.q.h"
45#include "frc971/wpilib/loop_output_handler.h"
46#include "frc971/wpilib/pdp_fetcher.h"
47#include "frc971/wpilib/wpilib_interface.h"
48#include "y2015/autonomous/auto.q.h"
49#include "y2015/constants.h"
50#include "y2015/control_loops/claw/claw.q.h"
51#include "y2015/control_loops/fridge/fridge.q.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
Brian Silverman51091a02015-12-26 15:56:58 -080072double drivetrain_velocity_translate(double in) {
73 return (1.0 / in) / 256.0 /*cpr*/ *
74 constants::GetValues().drivetrain_encoder_ratio *
75 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
76}
77
Daniel Pettiadf38432015-01-26 17:13:35 -080078double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080079 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080080 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080081 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080082 (2 * M_PI /*radians*/);
83}
84
Brian Silverman5d712fc2015-02-15 03:39:31 -050085double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080086 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080087 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080088 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080089 (2 * M_PI /*radians*/);
90}
91
92double elevator_translate(int32_t in) {
93 return static_cast<double>(in) /
94 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080095 constants::GetValues().elev_encoder_ratio *
96 (2 * M_PI /*radians*/) *
97 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080098}
99
Brian Silverman5d712fc2015-02-15 03:39:31 -0500100double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800101 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800102 constants::GetValues().elev_pot_ratio *
103 (2 * M_PI /*radians*/) *
104 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -0800105 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -0800106}
107
108double claw_translate(int32_t in) {
109 return static_cast<double>(in) /
110 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800111 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800112 (2 * M_PI /*radians*/);
113}
114
Brian Silverman5d712fc2015-02-15 03:39:31 -0500115double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800116 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800117 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800118 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800119 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700120}
121
Brian Silverman335c20e2015-01-26 21:47:58 -0500122static const double kMaximumEncoderPulsesPerSecond =
123 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
124 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
125 4.0 /* index pulse = 1/4 cycle */;
126
Austin Schuh010eb812014-10-25 18:06:49 -0700127class SensorReader {
128 public:
Brian Silverman39b339e2016-01-03 13:24:22 -0800129 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500130 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
131 // we should ever see.
132 filter_.SetPeriodNanoSeconds(
133 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
134 }
135
136 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
137 filter_.Add(encoder.get());
138 arm_left_encoder_.set_encoder(::std::move(encoder));
139 }
140
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700141 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500142 filter_.Add(index.get());
143 arm_left_encoder_.set_index(::std::move(index));
144 }
145
146 void set_arm_left_potentiometer(
147 ::std::unique_ptr<AnalogInput> potentiometer) {
148 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
149 }
150
151 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
152 filter_.Add(encoder.get());
153 arm_right_encoder_.set_encoder(::std::move(encoder));
154 }
155
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700156 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500157 filter_.Add(index.get());
158 arm_right_encoder_.set_index(::std::move(index));
159 }
160
161 void set_arm_right_potentiometer(
162 ::std::unique_ptr<AnalogInput> potentiometer) {
163 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
164 }
165
166 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
167 filter_.Add(encoder.get());
168 elevator_left_encoder_.set_encoder(::std::move(encoder));
169 }
170
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700171 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500172 filter_.Add(index.get());
173 elevator_left_encoder_.set_index(::std::move(index));
174 }
175
176 void set_elevator_left_potentiometer(
177 ::std::unique_ptr<AnalogInput> potentiometer) {
178 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
179 }
180
181 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
182 filter_.Add(encoder.get());
183 elevator_right_encoder_.set_encoder(::std::move(encoder));
184 }
185
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700186 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500187 filter_.Add(index.get());
188 elevator_right_encoder_.set_index(::std::move(index));
189 }
190
191 void set_elevator_right_potentiometer(
192 ::std::unique_ptr<AnalogInput> potentiometer) {
193 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
194 }
195
196 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
197 filter_.Add(encoder.get());
198 wrist_encoder_.set_encoder(::std::move(encoder));
199 }
200
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700201 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500202 filter_.Add(index.get());
203 wrist_encoder_.set_index(::std::move(index));
204 }
205
206 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
207 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700208 }
209
Brian Silverman1f90d672015-01-26 20:20:45 -0500210 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
211 left_encoder_ = ::std::move(left_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800212 left_encoder_->SetMaxPeriod(0.005);
Brian Silverman1f90d672015-01-26 20:20:45 -0500213 }
214
215 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
216 right_encoder_ = ::std::move(right_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800217 right_encoder_->SetMaxPeriod(0.005);
Brian Silverman1f90d672015-01-26 20:20:45 -0500218 }
219
Brian Silverman335c20e2015-01-26 21:47:58 -0500220 // All of the DMA-related set_* calls must be made before this, and it doesn't
221 // hurt to do all of them.
222 void set_dma(::std::unique_ptr<DMA> dma) {
223 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
224 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500225 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800226 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500227 dma_synchronizer_->Add(&elevator_right_encoder_);
228 }
229
Austin Schuh010eb812014-10-25 18:06:49 -0700230 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -0800231 ::aos::SetCurrentThreadName("SensorReader");
232
Brian Silverman699f0cb2015-02-05 19:45:01 -0500233 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700234 ds_ =
235#ifdef WPILIB2015
236 DriverStation::GetInstance();
237#else
238 &DriverStation::GetInstance();
239#endif
Brian Silverman699f0cb2015-02-05 19:45:01 -0500240
Brian Silverman335c20e2015-01-26 21:47:58 -0500241 wrist_encoder_.Start();
242 dma_synchronizer_->Start();
Austin Schuh010eb812014-10-25 18:06:49 -0700243
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700244 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
245 ::std::chrono::milliseconds(4));
Brian Silverman5090c432016-01-02 14:44:26 -0800246
247 ::aos::SetCurrentThreadRealtimePriority(40);
Austin Schuh010eb812014-10-25 18:06:49 -0700248 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800249 {
250 const int iterations = phased_loop.SleepUntilNext();
251 if (iterations != 1) {
252 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
253 }
254 }
Austin Schuh010eb812014-10-25 18:06:49 -0700255 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700256 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500257
258 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700259 }
260
261 void RunIteration() {
Brian Silverman39b339e2016-01-03 13:24:22 -0800262 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Austin Schuh010eb812014-10-25 18:06:49 -0700263
Austin Schuh35d06612015-02-15 23:35:23 -0800264 {
265 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
266 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800267 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800268 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800269 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800270 drivetrain_message->left_speed =
271 drivetrain_velocity_translate(left_encoder_->GetPeriod());
272 drivetrain_message->right_speed =
273 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Austin Schuh35d06612015-02-15 23:35:23 -0800274
275 drivetrain_message.Send();
276 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500277
278 dma_synchronizer_->RunIteration();
279
Philipp Schrader82c65072015-02-16 00:47:09 +0000280 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500281
Brian Silverman335c20e2015-01-26 21:47:58 -0500282 {
283 auto fridge_message = fridge_queue.position.MakeMessage();
284 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500285 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800286 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500287 CopyPotAndIndexPosition(
288 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
289 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800290 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500291 CopyPotAndIndexPosition(
292 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500293 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800294 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500295 CopyPotAndIndexPosition(
296 elevator_right_encoder_, &fridge_message->elevator.right,
297 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800298 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500299 fridge_message.Send();
300 }
301
302 {
303 auto claw_message = claw_queue.position.MakeMessage();
304 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500305 claw_translate, claw_potentiometer_translate,
306 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500307 claw_message.Send();
308 }
Austin Schuh010eb812014-10-25 18:06:49 -0700309 }
310
311 void Quit() { run_ = false; }
312
313 private:
Brian Silverman699f0cb2015-02-05 19:45:01 -0500314 int32_t my_pid_;
315 DriverStation *ds_;
316
Brian Silverman335c20e2015-01-26 21:47:58 -0500317 void CopyPotAndIndexPosition(
318 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
319 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500320 ::std::function<double(double)> potentiometer_translate, bool reverse,
321 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500322 const double multiplier = reverse ? -1.0 : 1.0;
323 position->encoder =
324 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500325 position->pot = multiplier * potentiometer_translate(
326 encoder.polled_potentiometer_voltage()) +
327 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500328 position->latched_encoder =
329 multiplier * encoder_translate(encoder.last_encoder_value());
330 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500331 multiplier *
332 potentiometer_translate(encoder.last_potentiometer_voltage()) +
333 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500334 position->index_pulses = encoder.index_posedge_count();
335 }
336
337 void CopyPotAndIndexPosition(
338 const InterruptEncoderAndPotentiometer &encoder,
339 PotAndIndexPosition *position,
340 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500341 ::std::function<double(double)> potentiometer_translate, bool reverse,
342 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500343 const double multiplier = reverse ? -1.0 : 1.0;
344 position->encoder =
345 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500346 position->pot = multiplier * potentiometer_translate(
347 encoder.potentiometer()->GetVoltage()) +
348 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500349 position->latched_encoder =
350 multiplier * encoder_translate(encoder.last_encoder_value());
351 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500352 multiplier *
353 potentiometer_translate(encoder.last_potentiometer_voltage()) +
354 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500355 position->index_pulses = encoder.index_posedge_count();
356 }
357
Brian Silverman335c20e2015-01-26 21:47:58 -0500358 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
359
360 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
361 elevator_left_encoder_, elevator_right_encoder_;
362
Brian Silverman5090c432016-01-02 14:44:26 -0800363 InterruptEncoderAndPotentiometer wrist_encoder_{55};
Brian Silverman335c20e2015-01-26 21:47:58 -0500364
Austin Schuh010eb812014-10-25 18:06:49 -0700365 ::std::unique_ptr<Encoder> left_encoder_;
366 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700367
Brian Silverman1f90d672015-01-26 20:20:45 -0500368 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700369 DigitalGlitchFilter filter_;
370};
371
Brian Silvermand8f403a2014-12-13 19:12:04 -0500372class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700373 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500374 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800375 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800376 fridge_(".frc971.control_loops.fridge_queue.output"),
377 claw_(".frc971.control_loops.claw_queue.output") {}
378
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700379 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800380 pressure_switch_ = ::std::move(pressure_switch);
381 }
382
383 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
384 compressor_relay_ = ::std::move(compressor_relay);
385 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500386
Daniel Pettiadf38432015-01-26 17:13:35 -0800387 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
388 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700389 }
390
Daniel Pettiadf38432015-01-26 17:13:35 -0800391 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
392 fridge_grabbers_top_back_ = ::std::move(s);
393 }
394
395 void set_fridge_grabbers_bottom_front(
396 ::std::unique_ptr<BufferedSolenoid> s) {
397 fridge_grabbers_bottom_front_ = ::std::move(s);
398 }
399
400 void set_fridge_grabbers_bottom_back(
401 ::std::unique_ptr<BufferedSolenoid> s) {
402 fridge_grabbers_bottom_back_ = ::std::move(s);
403 }
404
405 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
406 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500407 }
Austin Schuh010eb812014-10-25 18:06:49 -0700408
Brian Silverman93936f72015-03-19 23:38:30 -0700409 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
410 grabber_latch_release_ = ::std::move(s);
411 }
412
413 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
414 grabber_fold_up_ = ::std::move(s);
415 }
416
Brian Silvermand8f403a2014-12-13 19:12:04 -0500417 void operator()() {
418 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800419 ::aos::SetCurrentThreadRealtimePriority(27);
420
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700421 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
422 ::std::chrono::milliseconds(1));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500423
424 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800425 {
426 const int iterations = phased_loop.SleepUntilNext();
427 if (iterations != 1) {
428 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
429 }
430 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500431
432 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800433 fridge_.FetchLatest();
434 if (fridge_.get()) {
435 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800436 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
437 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
438 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
439 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800440 }
441 }
442
443 {
444 claw_.FetchLatest();
445 if (claw_.get()) {
446 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800447 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500448 }
449 }
450
Brian Silverman93936f72015-03-19 23:38:30 -0700451 ::aos::joystick_state.FetchLatest();
452 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
453 ::aos::joystick_state->autonomous);
454 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
455 ::aos::joystick_state->joysticks[1].buttons & 1);
456
Brian Silverman87541532015-03-19 23:35:12 -0700457 {
458 PneumaticsToLog to_log;
459 {
460 const bool compressor_on = !pressure_switch_->Get();
461 to_log.compressor_on = compressor_on;
462 if (compressor_on) {
463 compressor_relay_->Set(Relay::kForward);
464 } else {
465 compressor_relay_->Set(Relay::kOff);
466 }
467 }
468
469 pcm_->Flush();
470 to_log.read_solenoids = pcm_->GetAll();
471 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
472 }
Austin Schuh010eb812014-10-25 18:06:49 -0700473 }
474 }
475
Brian Silvermand8f403a2014-12-13 19:12:04 -0500476 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700477
Brian Silvermand8f403a2014-12-13 19:12:04 -0500478 private:
479 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800480 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
481 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
482 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
483 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
484 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700485 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
486 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700487 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800488 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700489
Daniel Pettiadf38432015-01-26 17:13:35 -0800490 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
491 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700492
Brian Silvermand8f403a2014-12-13 19:12:04 -0500493 ::std::atomic<bool> run_{true};
494};
495
Austin Schuhbb227f82015-09-06 15:27:52 -0700496class CanWriter : public LoopOutputHandler {
497 public:
498 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
499
500 void set_can_talon(::std::unique_ptr<Talon> t) {
501 can_talon_ = ::std::move(t);
502 }
503
504 private:
505 virtual void Read() override {
506 ::frc971::autonomous::can_control.FetchAnother();
507 }
508
509 virtual void Write() override {
510 auto &queue = ::frc971::autonomous::can_control;
511 LOG_STRUCT(DEBUG, "will output", *queue);
512 can_talon_->Set(queue->can_voltage / 12.0);
513 }
514
515 virtual void Stop() override {
516 LOG(WARNING, "Can output too old\n");
517 can_talon_->Disable();
518 }
519
520 ::std::unique_ptr<Talon> can_talon_;
521};
522
Brian Silvermand8f403a2014-12-13 19:12:04 -0500523class DrivetrainWriter : public LoopOutputHandler {
524 public:
525 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
526 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700527 }
528
Brian Silvermand8f403a2014-12-13 19:12:04 -0500529 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
530 right_drivetrain_talon_ = ::std::move(t);
531 }
Austin Schuh010eb812014-10-25 18:06:49 -0700532
Brian Silvermand8f403a2014-12-13 19:12:04 -0500533 private:
534 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500535 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500536 }
537
538 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500539 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500540 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800541 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
542 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500543 }
544
545 virtual void Stop() override {
546 LOG(WARNING, "drivetrain output too old\n");
547 left_drivetrain_talon_->Disable();
548 right_drivetrain_talon_->Disable();
549 }
550
Austin Schuh010eb812014-10-25 18:06:49 -0700551 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500552 ::std::unique_ptr<Talon> right_drivetrain_talon_;
553};
554
Daniel Pettiadf38432015-01-26 17:13:35 -0800555class FridgeWriter : public LoopOutputHandler {
556 public:
557 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
558 left_arm_talon_ = ::std::move(t);
559 }
560
561 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
562 right_arm_talon_ = ::std::move(t);
563 }
564
565 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
566 left_elevator_talon_ = ::std::move(t);
567 }
568
569 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
570 right_elevator_talon_ = ::std::move(t);
571 }
572
573 private:
574 virtual void Read() override {
575 ::frc971::control_loops::fridge_queue.output.FetchAnother();
576 }
577
578 virtual void Write() override {
579 auto &queue = ::frc971::control_loops::fridge_queue.output;
580 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800581 left_arm_talon_->Set(queue->left_arm / 12.0);
582 right_arm_talon_->Set(-queue->right_arm / 12.0);
583 left_elevator_talon_->Set(queue->left_elevator / 12.0);
584 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800585 }
586
587 virtual void Stop() override {
588 LOG(WARNING, "Fridge output too old.\n");
589 left_arm_talon_->Disable();
590 right_arm_talon_->Disable();
591 left_elevator_talon_->Disable();
592 right_elevator_talon_->Disable();
593 }
594
595 ::std::unique_ptr<Talon> left_arm_talon_;
596 ::std::unique_ptr<Talon> right_arm_talon_;
597 ::std::unique_ptr<Talon> left_elevator_talon_;
598 ::std::unique_ptr<Talon> right_elevator_talon_;
599};
600
601class ClawWriter : public LoopOutputHandler {
602 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800603 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
604 left_intake_talon_ = ::std::move(t);
605 }
606
607 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
608 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800609 }
610
611 void set_wrist_talon(::std::unique_ptr<Talon> t) {
612 wrist_talon_ = ::std::move(t);
613 }
614
615 private:
616 virtual void Read() override {
617 ::frc971::control_loops::claw_queue.output.FetchAnother();
618 }
619
620 virtual void Write() override {
621 auto &queue = ::frc971::control_loops::claw_queue.output;
622 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800623 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800624 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
625 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800626 }
627
628 virtual void Stop() override {
629 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800630 left_intake_talon_->Disable();
631 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800632 wrist_talon_->Disable();
633 }
634
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800635 ::std::unique_ptr<Talon> left_intake_talon_;
636 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800637 ::std::unique_ptr<Talon> wrist_talon_;
638};
639
Brian Silverman1f90d672015-01-26 20:20:45 -0500640// TODO(brian): Replace this with ::std::make_unique once all our toolchains
641// have support.
642template <class T, class... U>
643std::unique_ptr<T> make_unique(U &&... u) {
644 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
645}
646
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800647class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Austin Schuh010eb812014-10-25 18:06:49 -0700648 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800649 ::std::unique_ptr<Encoder> encoder(int index) {
650 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
651 Encoder::k4X);
652 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800653 virtual void Run() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500654 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800655 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500656
Brian Silverman98f6ee22015-01-26 17:50:12 -0500657 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700658 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800659 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500660
Brian Silverman425492b2015-12-30 15:23:55 -0800661 ::frc971::wpilib::PDPFetcher pdp_fetcher;
662 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
663
Brian Silverman39b339e2016-01-03 13:24:22 -0800664 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800665 reader.set_arm_left_encoder(encoder(1));
666 reader.set_arm_left_index(make_unique<DigitalInput>(1));
667 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
668
669 reader.set_arm_right_encoder(encoder(5));
670 reader.set_arm_right_index(make_unique<DigitalInput>(5));
671 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
672
673 reader.set_elevator_left_encoder(encoder(0));
674 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
675 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
676
677 reader.set_elevator_right_encoder(encoder(4));
678 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
679 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
680
681 reader.set_wrist_encoder(encoder(6));
682 reader.set_wrist_index(make_unique<DigitalInput>(6));
683 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
684
685 reader.set_left_encoder(encoder(2));
686 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500687 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500688 ::std::thread reader_thread(::std::ref(reader));
689 GyroSender gyro_sender;
690 ::std::thread gyro_thread(::std::ref(gyro_sender));
691
692 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500693 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800694 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500695 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800696 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500697 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
698
Austin Schuhbb227f82015-09-06 15:27:52 -0700699 CanWriter can_writer;
700 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
701 ::std::thread can_writer_thread(::std::ref(can_writer));
702
Daniel Pettiadf38432015-01-26 17:13:35 -0800703 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
704 // claw.
705 FridgeWriter fridge_writer;
706 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800707 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800708 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800709 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800710 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800711 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800712 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800713 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800714 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
715
716 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800717 claw_writer.set_left_intake_talon(
718 ::std::unique_ptr<Talon>(new Talon(5)));
719 claw_writer.set_right_intake_talon(
720 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800721 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800722 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800723 ::std::thread claw_writer_thread(::std::ref(claw_writer));
724
725 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
726 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500727 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800728 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
729 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800730 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800731 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
732 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700733 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
734 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800735
736 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
737 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500738 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
739
740 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800741 while (true) {
742 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
743 if (r != 0) {
744 PLOG(WARNING, "infinite select failed");
745 } else {
746 PLOG(WARNING, "infinite select succeeded??\n");
747 }
748 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500749
Austin Schuh010eb812014-10-25 18:06:49 -0700750 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800751
Austin Schuh010eb812014-10-25 18:06:49 -0700752 joystick_sender.Quit();
753 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800754 pdp_fetcher.Quit();
755 pdp_fetcher_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500756 reader.Quit();
757 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800758 gyro_sender.Quit();
759 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500760
761 drivetrain_writer.Quit();
762 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700763 can_writer.Quit();
764 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500765 solenoid_writer.Quit();
766 solenoid_thread.join();
767
Austin Schuh010eb812014-10-25 18:06:49 -0700768 ::aos::Cleanup();
769 }
770};
771
Brian Silverman98f6ee22015-01-26 17:50:12 -0500772} // namespace wpilib
773} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800774
Brian Silverman98f6ee22015-01-26 17:50:12 -0500775
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800776AOS_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);