blob: 76d5976cbb9cb29d0c9f27b35b6dafaf3bff45b0 [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"
18#include "ControllerPower.h"
19#ifndef WPILIB2015
20#include "DigitalGlitchFilter.h"
21#endif
22#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 Silvermanb691f5e2015-08-02 11:37:55 -070034#include "y2015/constants.h"
Brian Silverman335c20e2015-01-26 21:47:58 -050035#include "frc971/control_loops/control_loops.q.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/hall_effect.h"
42#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 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 Silverman699f0cb2015-02-05 19:45:01 -0500247 {
248 auto new_state = ::aos::robot_state.MakeMessage();
Austin Schuh010eb812014-10-25 18:06:49 -0700249
Austin Schuhfae69172015-02-20 22:11:26 -0800250 new_state->reader_pid = my_pid_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500251 new_state->outputs_enabled = ds_->IsSysActive();
252 new_state->browned_out = ds_->IsSysBrownedOut();
253
254 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
255 new_state->is_5v_active = ControllerPower::GetEnabled5V();
256 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
257 new_state->voltage_5v = ControllerPower::GetVoltage5V();
258
259 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
260 new_state->voltage_battery = ds_->GetBatteryVoltage();
261
Brian Silverman4d882aa2015-02-20 13:40:42 -0500262 LOG_STRUCT(DEBUG, "robot_state", *new_state);
263
Brian Silverman699f0cb2015-02-05 19:45:01 -0500264 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700265 }
266
Austin Schuh35d06612015-02-15 23:35:23 -0800267 {
268 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
269 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800270 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800271 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800272 drivetrain_translate(left_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800273
274 drivetrain_message.Send();
275 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500276
277 dma_synchronizer_->RunIteration();
278
Philipp Schrader82c65072015-02-16 00:47:09 +0000279 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500280
Brian Silverman335c20e2015-01-26 21:47:58 -0500281 {
282 auto fridge_message = fridge_queue.position.MakeMessage();
283 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500284 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800285 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500286 CopyPotAndIndexPosition(
287 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
288 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800289 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500290 CopyPotAndIndexPosition(
291 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500292 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800293 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500294 CopyPotAndIndexPosition(
295 elevator_right_encoder_, &fridge_message->elevator.right,
296 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800297 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500298 fridge_message.Send();
299 }
300
301 {
302 auto claw_message = claw_queue.position.MakeMessage();
303 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500304 claw_translate, claw_potentiometer_translate,
305 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500306 claw_message.Send();
307 }
Austin Schuh010eb812014-10-25 18:06:49 -0700308 }
309
310 void Quit() { run_ = false; }
311
312 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500313 static const int kPriority = 30;
314 static const int kInterruptPriority = 55;
315
Brian Silverman699f0cb2015-02-05 19:45:01 -0500316 int32_t my_pid_;
317 DriverStation *ds_;
318
Brian Silverman335c20e2015-01-26 21:47:58 -0500319 void CopyPotAndIndexPosition(
320 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
321 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500322 ::std::function<double(double)> potentiometer_translate, bool reverse,
323 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500324 const double multiplier = reverse ? -1.0 : 1.0;
325 position->encoder =
326 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500327 position->pot = multiplier * potentiometer_translate(
328 encoder.polled_potentiometer_voltage()) +
329 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500330 position->latched_encoder =
331 multiplier * encoder_translate(encoder.last_encoder_value());
332 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500333 multiplier *
334 potentiometer_translate(encoder.last_potentiometer_voltage()) +
335 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500336 position->index_pulses = encoder.index_posedge_count();
337 }
338
339 void CopyPotAndIndexPosition(
340 const InterruptEncoderAndPotentiometer &encoder,
341 PotAndIndexPosition *position,
342 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500343 ::std::function<double(double)> potentiometer_translate, bool reverse,
344 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500345 const double multiplier = reverse ? -1.0 : 1.0;
346 position->encoder =
347 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500348 position->pot = multiplier * potentiometer_translate(
349 encoder.potentiometer()->GetVoltage()) +
350 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500351 position->latched_encoder =
352 multiplier * encoder_translate(encoder.last_encoder_value());
353 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500354 multiplier *
355 potentiometer_translate(encoder.last_potentiometer_voltage()) +
356 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500357 position->index_pulses = encoder.index_posedge_count();
358 }
359
Brian Silverman335c20e2015-01-26 21:47:58 -0500360 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
361
362 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
363 elevator_left_encoder_, elevator_right_encoder_;
364
365 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
366
Austin Schuh010eb812014-10-25 18:06:49 -0700367 ::std::unique_ptr<Encoder> left_encoder_;
368 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700369
Brian Silverman1f90d672015-01-26 20:20:45 -0500370 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700371 DigitalGlitchFilter filter_;
372};
373
Brian Silvermand8f403a2014-12-13 19:12:04 -0500374class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700375 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500376 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800377 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800378 fridge_(".frc971.control_loops.fridge_queue.output"),
379 claw_(".frc971.control_loops.claw_queue.output") {}
380
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700381 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800382 pressure_switch_ = ::std::move(pressure_switch);
383 }
384
385 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
386 compressor_relay_ = ::std::move(compressor_relay);
387 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500388
Daniel Pettiadf38432015-01-26 17:13:35 -0800389 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
390 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700391 }
392
Daniel Pettiadf38432015-01-26 17:13:35 -0800393 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
394 fridge_grabbers_top_back_ = ::std::move(s);
395 }
396
397 void set_fridge_grabbers_bottom_front(
398 ::std::unique_ptr<BufferedSolenoid> s) {
399 fridge_grabbers_bottom_front_ = ::std::move(s);
400 }
401
402 void set_fridge_grabbers_bottom_back(
403 ::std::unique_ptr<BufferedSolenoid> s) {
404 fridge_grabbers_bottom_back_ = ::std::move(s);
405 }
406
407 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
408 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500409 }
Austin Schuh010eb812014-10-25 18:06:49 -0700410
Brian Silverman93936f72015-03-19 23:38:30 -0700411 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
412 grabber_latch_release_ = ::std::move(s);
413 }
414
415 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
416 grabber_fold_up_ = ::std::move(s);
417 }
418
Brian Silvermand8f403a2014-12-13 19:12:04 -0500419 void operator()() {
420 ::aos::SetCurrentThreadName("Solenoids");
421 ::aos::SetCurrentThreadRealtimePriority(30);
422
423 while (run_) {
424 ::aos::time::PhasedLoopXMS(20, 1000);
425
426 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800427 fridge_.FetchLatest();
428 if (fridge_.get()) {
429 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800430 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
431 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
432 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
433 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800434 }
435 }
436
437 {
438 claw_.FetchLatest();
439 if (claw_.get()) {
440 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800441 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500442 }
443 }
444
Brian Silverman93936f72015-03-19 23:38:30 -0700445 ::aos::joystick_state.FetchLatest();
446 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
447 ::aos::joystick_state->autonomous);
448 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
449 ::aos::joystick_state->joysticks[1].buttons & 1);
450
Brian Silverman87541532015-03-19 23:35:12 -0700451 {
452 PneumaticsToLog to_log;
453 {
454 const bool compressor_on = !pressure_switch_->Get();
455 to_log.compressor_on = compressor_on;
456 if (compressor_on) {
457 compressor_relay_->Set(Relay::kForward);
458 } else {
459 compressor_relay_->Set(Relay::kOff);
460 }
461 }
462
463 pcm_->Flush();
464 to_log.read_solenoids = pcm_->GetAll();
465 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
466 }
Austin Schuh010eb812014-10-25 18:06:49 -0700467 }
468 }
469
Brian Silvermand8f403a2014-12-13 19:12:04 -0500470 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700471
Brian Silvermand8f403a2014-12-13 19:12:04 -0500472 private:
473 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800474 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
475 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
476 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
477 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
478 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700479 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
480 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700481 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800482 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700483
Daniel Pettiadf38432015-01-26 17:13:35 -0800484 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
485 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700486
Brian Silvermand8f403a2014-12-13 19:12:04 -0500487 ::std::atomic<bool> run_{true};
488};
489
Austin Schuhbb227f82015-09-06 15:27:52 -0700490class CanWriter : public LoopOutputHandler {
491 public:
492 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
493
494 void set_can_talon(::std::unique_ptr<Talon> t) {
495 can_talon_ = ::std::move(t);
496 }
497
498 private:
499 virtual void Read() override {
500 ::frc971::autonomous::can_control.FetchAnother();
501 }
502
503 virtual void Write() override {
504 auto &queue = ::frc971::autonomous::can_control;
505 LOG_STRUCT(DEBUG, "will output", *queue);
506 can_talon_->Set(queue->can_voltage / 12.0);
507 }
508
509 virtual void Stop() override {
510 LOG(WARNING, "Can output too old\n");
511 can_talon_->Disable();
512 }
513
514 ::std::unique_ptr<Talon> can_talon_;
515};
516
Brian Silvermand8f403a2014-12-13 19:12:04 -0500517class DrivetrainWriter : public LoopOutputHandler {
518 public:
519 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
520 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700521 }
522
Brian Silvermand8f403a2014-12-13 19:12:04 -0500523 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
524 right_drivetrain_talon_ = ::std::move(t);
525 }
Austin Schuh010eb812014-10-25 18:06:49 -0700526
Brian Silvermand8f403a2014-12-13 19:12:04 -0500527 private:
528 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500529 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500530 }
531
532 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500533 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500534 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800535 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
536 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500537 }
538
539 virtual void Stop() override {
540 LOG(WARNING, "drivetrain output too old\n");
541 left_drivetrain_talon_->Disable();
542 right_drivetrain_talon_->Disable();
543 }
544
Austin Schuh010eb812014-10-25 18:06:49 -0700545 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500546 ::std::unique_ptr<Talon> right_drivetrain_talon_;
547};
548
Daniel Pettiadf38432015-01-26 17:13:35 -0800549class FridgeWriter : public LoopOutputHandler {
550 public:
551 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
552 left_arm_talon_ = ::std::move(t);
553 }
554
555 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
556 right_arm_talon_ = ::std::move(t);
557 }
558
559 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
560 left_elevator_talon_ = ::std::move(t);
561 }
562
563 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
564 right_elevator_talon_ = ::std::move(t);
565 }
566
567 private:
568 virtual void Read() override {
569 ::frc971::control_loops::fridge_queue.output.FetchAnother();
570 }
571
572 virtual void Write() override {
573 auto &queue = ::frc971::control_loops::fridge_queue.output;
574 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800575 left_arm_talon_->Set(queue->left_arm / 12.0);
576 right_arm_talon_->Set(-queue->right_arm / 12.0);
577 left_elevator_talon_->Set(queue->left_elevator / 12.0);
578 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800579 }
580
581 virtual void Stop() override {
582 LOG(WARNING, "Fridge output too old.\n");
583 left_arm_talon_->Disable();
584 right_arm_talon_->Disable();
585 left_elevator_talon_->Disable();
586 right_elevator_talon_->Disable();
587 }
588
589 ::std::unique_ptr<Talon> left_arm_talon_;
590 ::std::unique_ptr<Talon> right_arm_talon_;
591 ::std::unique_ptr<Talon> left_elevator_talon_;
592 ::std::unique_ptr<Talon> right_elevator_talon_;
593};
594
595class ClawWriter : public LoopOutputHandler {
596 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800597 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
598 left_intake_talon_ = ::std::move(t);
599 }
600
601 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
602 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800603 }
604
605 void set_wrist_talon(::std::unique_ptr<Talon> t) {
606 wrist_talon_ = ::std::move(t);
607 }
608
609 private:
610 virtual void Read() override {
611 ::frc971::control_loops::claw_queue.output.FetchAnother();
612 }
613
614 virtual void Write() override {
615 auto &queue = ::frc971::control_loops::claw_queue.output;
616 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800617 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800618 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
619 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800620 }
621
622 virtual void Stop() override {
623 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800624 left_intake_talon_->Disable();
625 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800626 wrist_talon_->Disable();
627 }
628
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800629 ::std::unique_ptr<Talon> left_intake_talon_;
630 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800631 ::std::unique_ptr<Talon> wrist_talon_;
632};
633
Brian Silverman1f90d672015-01-26 20:20:45 -0500634// TODO(brian): Replace this with ::std::make_unique once all our toolchains
635// have support.
636template <class T, class... U>
637std::unique_ptr<T> make_unique(U &&... u) {
638 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
639}
640
Austin Schuh010eb812014-10-25 18:06:49 -0700641class WPILibRobot : public RobotBase {
642 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800643 ::std::unique_ptr<Encoder> encoder(int index) {
644 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
645 Encoder::k4X);
646 }
Austin Schuh010eb812014-10-25 18:06:49 -0700647 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500648 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800649 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500650
Brian Silverman98f6ee22015-01-26 17:50:12 -0500651 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700652 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800653 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500654
Brian Silverman98f6ee22015-01-26 17:50:12 -0500655 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800656 LOG(INFO, "Creating the reader\n");
657 reader.set_arm_left_encoder(encoder(1));
658 reader.set_arm_left_index(make_unique<DigitalInput>(1));
659 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
660
661 reader.set_arm_right_encoder(encoder(5));
662 reader.set_arm_right_index(make_unique<DigitalInput>(5));
663 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
664
665 reader.set_elevator_left_encoder(encoder(0));
666 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
667 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
668
669 reader.set_elevator_right_encoder(encoder(4));
670 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
671 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
672
673 reader.set_wrist_encoder(encoder(6));
674 reader.set_wrist_index(make_unique<DigitalInput>(6));
675 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
676
677 reader.set_left_encoder(encoder(2));
678 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500679 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500680 ::std::thread reader_thread(::std::ref(reader));
681 GyroSender gyro_sender;
682 ::std::thread gyro_thread(::std::ref(gyro_sender));
683
684 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500685 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800686 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500687 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800688 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500689 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
690
Austin Schuhbb227f82015-09-06 15:27:52 -0700691 CanWriter can_writer;
692 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
693 ::std::thread can_writer_thread(::std::ref(can_writer));
694
Daniel Pettiadf38432015-01-26 17:13:35 -0800695 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
696 // claw.
697 FridgeWriter fridge_writer;
698 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800699 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800700 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800701 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800702 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800703 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800704 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800705 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800706 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
707
708 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800709 claw_writer.set_left_intake_talon(
710 ::std::unique_ptr<Talon>(new Talon(5)));
711 claw_writer.set_right_intake_talon(
712 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800713 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800714 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800715 ::std::thread claw_writer_thread(::std::ref(claw_writer));
716
717 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
718 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500719 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800720 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
721 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800722 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800723 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
724 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700725 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
726 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800727
728 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
729 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500730 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
731
732 // Wait forever. Not much else to do...
733 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
734
Austin Schuh010eb812014-10-25 18:06:49 -0700735 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800736
Austin Schuh010eb812014-10-25 18:06:49 -0700737 joystick_sender.Quit();
738 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500739 reader.Quit();
740 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800741 gyro_sender.Quit();
742 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500743
744 drivetrain_writer.Quit();
745 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700746 can_writer.Quit();
747 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500748 solenoid_writer.Quit();
749 solenoid_thread.join();
750
Austin Schuh010eb812014-10-25 18:06:49 -0700751 ::aos::Cleanup();
752 }
753};
754
Brian Silverman98f6ee22015-01-26 17:50:12 -0500755} // namespace wpilib
756} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800757
Brian Silverman98f6ee22015-01-26 17:50:12 -0500758
759START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);