blob: c56d2115219888911837db660ad142818415d64d [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/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 Silvermanda45b6c2014-12-28 11:36:50 -080050
Austin Schuh010eb812014-10-25 18:06:49 -070051#ifndef M_PI
52#define M_PI 3.14159265358979323846
53#endif
54
55using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050056using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050057using ::frc971::control_loops::fridge_queue;
58using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070059
60namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080061namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070062
Austin Schuh010eb812014-10-25 18:06:49 -070063double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080064 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080065 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080066 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080067 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
68}
69
70double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080071 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080072 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080073 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080074 (2 * M_PI /*radians*/);
75}
76
Brian Silverman5d712fc2015-02-15 03:39:31 -050077double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080078 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080079 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080080 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080081 (2 * M_PI /*radians*/);
82}
83
84double elevator_translate(int32_t in) {
85 return static_cast<double>(in) /
86 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080087 constants::GetValues().elev_encoder_ratio *
88 (2 * M_PI /*radians*/) *
89 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080090}
91
Brian Silverman5d712fc2015-02-15 03:39:31 -050092double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -080093 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080094 constants::GetValues().elev_pot_ratio *
95 (2 * M_PI /*radians*/) *
96 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -080097 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -080098}
99
100double claw_translate(int32_t in) {
101 return static_cast<double>(in) /
102 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800103 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800104 (2 * M_PI /*radians*/);
105}
106
Brian Silverman5d712fc2015-02-15 03:39:31 -0500107double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800108 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800109 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800110 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800111 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700112}
113
Brian Silverman335c20e2015-01-26 21:47:58 -0500114static const double kMaximumEncoderPulsesPerSecond =
115 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
116 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
117 4.0 /* index pulse = 1/4 cycle */;
118
Austin Schuh010eb812014-10-25 18:06:49 -0700119class SensorReader {
120 public:
Brian Silverman1f90d672015-01-26 20:20:45 -0500121 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500122 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
123 // we should ever see.
124 filter_.SetPeriodNanoSeconds(
125 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
126 }
127
128 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
129 filter_.Add(encoder.get());
130 arm_left_encoder_.set_encoder(::std::move(encoder));
131 }
132
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700133 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500134 filter_.Add(index.get());
135 arm_left_encoder_.set_index(::std::move(index));
136 }
137
138 void set_arm_left_potentiometer(
139 ::std::unique_ptr<AnalogInput> potentiometer) {
140 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
141 }
142
143 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
144 filter_.Add(encoder.get());
145 arm_right_encoder_.set_encoder(::std::move(encoder));
146 }
147
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700148 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500149 filter_.Add(index.get());
150 arm_right_encoder_.set_index(::std::move(index));
151 }
152
153 void set_arm_right_potentiometer(
154 ::std::unique_ptr<AnalogInput> potentiometer) {
155 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
156 }
157
158 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
159 filter_.Add(encoder.get());
160 elevator_left_encoder_.set_encoder(::std::move(encoder));
161 }
162
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700163 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500164 filter_.Add(index.get());
165 elevator_left_encoder_.set_index(::std::move(index));
166 }
167
168 void set_elevator_left_potentiometer(
169 ::std::unique_ptr<AnalogInput> potentiometer) {
170 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
171 }
172
173 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
174 filter_.Add(encoder.get());
175 elevator_right_encoder_.set_encoder(::std::move(encoder));
176 }
177
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700178 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500179 filter_.Add(index.get());
180 elevator_right_encoder_.set_index(::std::move(index));
181 }
182
183 void set_elevator_right_potentiometer(
184 ::std::unique_ptr<AnalogInput> potentiometer) {
185 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
186 }
187
188 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
189 filter_.Add(encoder.get());
190 wrist_encoder_.set_encoder(::std::move(encoder));
191 }
192
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700193 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500194 filter_.Add(index.get());
195 wrist_encoder_.set_index(::std::move(index));
196 }
197
198 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
199 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700200 }
201
Brian Silverman1f90d672015-01-26 20:20:45 -0500202 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
203 left_encoder_ = ::std::move(left_encoder);
204 }
205
206 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
207 right_encoder_ = ::std::move(right_encoder);
208 }
209
Brian Silverman335c20e2015-01-26 21:47:58 -0500210 // All of the DMA-related set_* calls must be made before this, and it doesn't
211 // hurt to do all of them.
212 void set_dma(::std::unique_ptr<DMA> dma) {
213 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
214 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500215 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800216 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500217 dma_synchronizer_->Add(&elevator_right_encoder_);
218 }
219
Austin Schuh010eb812014-10-25 18:06:49 -0700220 void operator()() {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800221 LOG(INFO, "In sensor reader thread\n");
Brian Silverman2fe007c2014-12-28 12:20:01 -0800222 ::aos::SetCurrentThreadName("SensorReader");
223
Brian Silverman699f0cb2015-02-05 19:45:01 -0500224 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700225 ds_ =
226#ifdef WPILIB2015
227 DriverStation::GetInstance();
228#else
229 &DriverStation::GetInstance();
230#endif
Brian Silverman699f0cb2015-02-05 19:45:01 -0500231
Brian Silverman335c20e2015-01-26 21:47:58 -0500232 wrist_encoder_.Start();
233 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800234 LOG(INFO, "Things are now started\n");
Austin Schuh010eb812014-10-25 18:06:49 -0700235
Brian Silverman2fe007c2014-12-28 12:20:01 -0800236 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700237 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800238 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700239 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700240 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500241
242 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700243 }
244
245 void RunIteration() {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500246 {
247 auto new_state = ::aos::robot_state.MakeMessage();
Austin Schuh010eb812014-10-25 18:06:49 -0700248
Austin Schuhfae69172015-02-20 22:11:26 -0800249 new_state->reader_pid = my_pid_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500250 new_state->outputs_enabled = ds_->IsSysActive();
251 new_state->browned_out = ds_->IsSysBrownedOut();
252
253 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
254 new_state->is_5v_active = ControllerPower::GetEnabled5V();
255 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
256 new_state->voltage_5v = ControllerPower::GetVoltage5V();
257
258 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
259 new_state->voltage_battery = ds_->GetBatteryVoltage();
260
Brian Silverman4d882aa2015-02-20 13:40:42 -0500261 LOG_STRUCT(DEBUG, "robot_state", *new_state);
262
Brian Silverman699f0cb2015-02-05 19:45:01 -0500263 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700264 }
265
Austin Schuh35d06612015-02-15 23:35:23 -0800266 {
267 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
268 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800269 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800270 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800271 drivetrain_translate(left_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800272
273 drivetrain_message.Send();
274 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500275
276 dma_synchronizer_->RunIteration();
277
Philipp Schrader82c65072015-02-16 00:47:09 +0000278 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500279
Brian Silverman335c20e2015-01-26 21:47:58 -0500280 {
281 auto fridge_message = fridge_queue.position.MakeMessage();
282 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500283 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800284 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500285 CopyPotAndIndexPosition(
286 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
287 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800288 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500289 CopyPotAndIndexPosition(
290 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500291 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800292 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500293 CopyPotAndIndexPosition(
294 elevator_right_encoder_, &fridge_message->elevator.right,
295 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800296 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500297 fridge_message.Send();
298 }
299
300 {
301 auto claw_message = claw_queue.position.MakeMessage();
302 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500303 claw_translate, claw_potentiometer_translate,
304 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500305 claw_message.Send();
306 }
Austin Schuh010eb812014-10-25 18:06:49 -0700307 }
308
309 void Quit() { run_ = false; }
310
311 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500312 static const int kPriority = 30;
313 static const int kInterruptPriority = 55;
314
Brian Silverman699f0cb2015-02-05 19:45:01 -0500315 int32_t my_pid_;
316 DriverStation *ds_;
317
Brian Silverman335c20e2015-01-26 21:47:58 -0500318 void CopyPotAndIndexPosition(
319 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
320 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500321 ::std::function<double(double)> potentiometer_translate, bool reverse,
322 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500323 const double multiplier = reverse ? -1.0 : 1.0;
324 position->encoder =
325 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500326 position->pot = multiplier * potentiometer_translate(
327 encoder.polled_potentiometer_voltage()) +
328 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500329 position->latched_encoder =
330 multiplier * encoder_translate(encoder.last_encoder_value());
331 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500332 multiplier *
333 potentiometer_translate(encoder.last_potentiometer_voltage()) +
334 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500335 position->index_pulses = encoder.index_posedge_count();
336 }
337
338 void CopyPotAndIndexPosition(
339 const InterruptEncoderAndPotentiometer &encoder,
340 PotAndIndexPosition *position,
341 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500342 ::std::function<double(double)> potentiometer_translate, bool reverse,
343 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500344 const double multiplier = reverse ? -1.0 : 1.0;
345 position->encoder =
346 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500347 position->pot = multiplier * potentiometer_translate(
348 encoder.potentiometer()->GetVoltage()) +
349 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500350 position->latched_encoder =
351 multiplier * encoder_translate(encoder.last_encoder_value());
352 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500353 multiplier *
354 potentiometer_translate(encoder.last_potentiometer_voltage()) +
355 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500356 position->index_pulses = encoder.index_posedge_count();
357 }
358
Brian Silverman335c20e2015-01-26 21:47:58 -0500359 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
360
361 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
362 elevator_left_encoder_, elevator_right_encoder_;
363
364 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
365
Austin Schuh010eb812014-10-25 18:06:49 -0700366 ::std::unique_ptr<Encoder> left_encoder_;
367 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700368
Brian Silverman1f90d672015-01-26 20:20:45 -0500369 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700370 DigitalGlitchFilter filter_;
371};
372
Brian Silvermand8f403a2014-12-13 19:12:04 -0500373class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700374 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500375 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800376 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800377 fridge_(".frc971.control_loops.fridge_queue.output"),
378 claw_(".frc971.control_loops.claw_queue.output") {}
379
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700380 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800381 pressure_switch_ = ::std::move(pressure_switch);
382 }
383
384 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
385 compressor_relay_ = ::std::move(compressor_relay);
386 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500387
Daniel Pettiadf38432015-01-26 17:13:35 -0800388 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
389 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700390 }
391
Daniel Pettiadf38432015-01-26 17:13:35 -0800392 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
393 fridge_grabbers_top_back_ = ::std::move(s);
394 }
395
396 void set_fridge_grabbers_bottom_front(
397 ::std::unique_ptr<BufferedSolenoid> s) {
398 fridge_grabbers_bottom_front_ = ::std::move(s);
399 }
400
401 void set_fridge_grabbers_bottom_back(
402 ::std::unique_ptr<BufferedSolenoid> s) {
403 fridge_grabbers_bottom_back_ = ::std::move(s);
404 }
405
406 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
407 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500408 }
Austin Schuh010eb812014-10-25 18:06:49 -0700409
Brian Silverman93936f72015-03-19 23:38:30 -0700410 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
411 grabber_latch_release_ = ::std::move(s);
412 }
413
414 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
415 grabber_fold_up_ = ::std::move(s);
416 }
417
Brian Silvermand8f403a2014-12-13 19:12:04 -0500418 void operator()() {
419 ::aos::SetCurrentThreadName("Solenoids");
420 ::aos::SetCurrentThreadRealtimePriority(30);
421
422 while (run_) {
423 ::aos::time::PhasedLoopXMS(20, 1000);
424
425 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800426 fridge_.FetchLatest();
427 if (fridge_.get()) {
428 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800429 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
430 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
431 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
432 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800433 }
434 }
435
436 {
437 claw_.FetchLatest();
438 if (claw_.get()) {
439 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800440 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500441 }
442 }
443
Brian Silverman93936f72015-03-19 23:38:30 -0700444 ::aos::joystick_state.FetchLatest();
445 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
446 ::aos::joystick_state->autonomous);
447 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
448 ::aos::joystick_state->joysticks[1].buttons & 1);
449
Brian Silverman87541532015-03-19 23:35:12 -0700450 {
451 PneumaticsToLog to_log;
452 {
453 const bool compressor_on = !pressure_switch_->Get();
454 to_log.compressor_on = compressor_on;
455 if (compressor_on) {
456 compressor_relay_->Set(Relay::kForward);
457 } else {
458 compressor_relay_->Set(Relay::kOff);
459 }
460 }
461
462 pcm_->Flush();
463 to_log.read_solenoids = pcm_->GetAll();
464 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
465 }
Austin Schuh010eb812014-10-25 18:06:49 -0700466 }
467 }
468
Brian Silvermand8f403a2014-12-13 19:12:04 -0500469 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700470
Brian Silvermand8f403a2014-12-13 19:12:04 -0500471 private:
472 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800473 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
474 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
475 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
476 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
477 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700478 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
479 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700480 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800481 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700482
Daniel Pettiadf38432015-01-26 17:13:35 -0800483 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
484 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700485
Brian Silvermand8f403a2014-12-13 19:12:04 -0500486 ::std::atomic<bool> run_{true};
487};
488
Austin Schuhbb227f82015-09-06 15:27:52 -0700489class CanWriter : public LoopOutputHandler {
490 public:
491 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
492
493 void set_can_talon(::std::unique_ptr<Talon> t) {
494 can_talon_ = ::std::move(t);
495 }
496
497 private:
498 virtual void Read() override {
499 ::frc971::autonomous::can_control.FetchAnother();
500 }
501
502 virtual void Write() override {
503 auto &queue = ::frc971::autonomous::can_control;
504 LOG_STRUCT(DEBUG, "will output", *queue);
505 can_talon_->Set(queue->can_voltage / 12.0);
506 }
507
508 virtual void Stop() override {
509 LOG(WARNING, "Can output too old\n");
510 can_talon_->Disable();
511 }
512
513 ::std::unique_ptr<Talon> can_talon_;
514};
515
Brian Silvermand8f403a2014-12-13 19:12:04 -0500516class DrivetrainWriter : public LoopOutputHandler {
517 public:
518 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
519 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700520 }
521
Brian Silvermand8f403a2014-12-13 19:12:04 -0500522 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
523 right_drivetrain_talon_ = ::std::move(t);
524 }
Austin Schuh010eb812014-10-25 18:06:49 -0700525
Brian Silvermand8f403a2014-12-13 19:12:04 -0500526 private:
527 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500528 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500529 }
530
531 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500532 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500533 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800534 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
535 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500536 }
537
538 virtual void Stop() override {
539 LOG(WARNING, "drivetrain output too old\n");
540 left_drivetrain_talon_->Disable();
541 right_drivetrain_talon_->Disable();
542 }
543
Austin Schuh010eb812014-10-25 18:06:49 -0700544 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500545 ::std::unique_ptr<Talon> right_drivetrain_talon_;
546};
547
Daniel Pettiadf38432015-01-26 17:13:35 -0800548class FridgeWriter : public LoopOutputHandler {
549 public:
550 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
551 left_arm_talon_ = ::std::move(t);
552 }
553
554 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
555 right_arm_talon_ = ::std::move(t);
556 }
557
558 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
559 left_elevator_talon_ = ::std::move(t);
560 }
561
562 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
563 right_elevator_talon_ = ::std::move(t);
564 }
565
566 private:
567 virtual void Read() override {
568 ::frc971::control_loops::fridge_queue.output.FetchAnother();
569 }
570
571 virtual void Write() override {
572 auto &queue = ::frc971::control_loops::fridge_queue.output;
573 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800574 left_arm_talon_->Set(queue->left_arm / 12.0);
575 right_arm_talon_->Set(-queue->right_arm / 12.0);
576 left_elevator_talon_->Set(queue->left_elevator / 12.0);
577 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800578 }
579
580 virtual void Stop() override {
581 LOG(WARNING, "Fridge output too old.\n");
582 left_arm_talon_->Disable();
583 right_arm_talon_->Disable();
584 left_elevator_talon_->Disable();
585 right_elevator_talon_->Disable();
586 }
587
588 ::std::unique_ptr<Talon> left_arm_talon_;
589 ::std::unique_ptr<Talon> right_arm_talon_;
590 ::std::unique_ptr<Talon> left_elevator_talon_;
591 ::std::unique_ptr<Talon> right_elevator_talon_;
592};
593
594class ClawWriter : public LoopOutputHandler {
595 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800596 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
597 left_intake_talon_ = ::std::move(t);
598 }
599
600 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
601 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800602 }
603
604 void set_wrist_talon(::std::unique_ptr<Talon> t) {
605 wrist_talon_ = ::std::move(t);
606 }
607
608 private:
609 virtual void Read() override {
610 ::frc971::control_loops::claw_queue.output.FetchAnother();
611 }
612
613 virtual void Write() override {
614 auto &queue = ::frc971::control_loops::claw_queue.output;
615 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800616 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800617 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
618 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800619 }
620
621 virtual void Stop() override {
622 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800623 left_intake_talon_->Disable();
624 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800625 wrist_talon_->Disable();
626 }
627
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800628 ::std::unique_ptr<Talon> left_intake_talon_;
629 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800630 ::std::unique_ptr<Talon> wrist_talon_;
631};
632
Brian Silverman1f90d672015-01-26 20:20:45 -0500633// TODO(brian): Replace this with ::std::make_unique once all our toolchains
634// have support.
635template <class T, class... U>
636std::unique_ptr<T> make_unique(U &&... u) {
637 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
638}
639
Austin Schuh010eb812014-10-25 18:06:49 -0700640class WPILibRobot : public RobotBase {
641 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800642 ::std::unique_ptr<Encoder> encoder(int index) {
643 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
644 Encoder::k4X);
645 }
Austin Schuh010eb812014-10-25 18:06:49 -0700646 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500647 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800648 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500649
Brian Silverman98f6ee22015-01-26 17:50:12 -0500650 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700651 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800652 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500653
Brian Silverman98f6ee22015-01-26 17:50:12 -0500654 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800655 LOG(INFO, "Creating the reader\n");
656 reader.set_arm_left_encoder(encoder(1));
657 reader.set_arm_left_index(make_unique<DigitalInput>(1));
658 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
659
660 reader.set_arm_right_encoder(encoder(5));
661 reader.set_arm_right_index(make_unique<DigitalInput>(5));
662 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
663
664 reader.set_elevator_left_encoder(encoder(0));
665 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
666 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
667
668 reader.set_elevator_right_encoder(encoder(4));
669 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
670 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
671
672 reader.set_wrist_encoder(encoder(6));
673 reader.set_wrist_index(make_unique<DigitalInput>(6));
674 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
675
676 reader.set_left_encoder(encoder(2));
677 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500678 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500679 ::std::thread reader_thread(::std::ref(reader));
680 GyroSender gyro_sender;
681 ::std::thread gyro_thread(::std::ref(gyro_sender));
682
683 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500684 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800685 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500686 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800687 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500688 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
689
Austin Schuhbb227f82015-09-06 15:27:52 -0700690 CanWriter can_writer;
691 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
692 ::std::thread can_writer_thread(::std::ref(can_writer));
693
Daniel Pettiadf38432015-01-26 17:13:35 -0800694 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
695 // claw.
696 FridgeWriter fridge_writer;
697 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800698 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800699 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800700 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800701 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800702 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800703 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800704 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800705 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
706
707 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800708 claw_writer.set_left_intake_talon(
709 ::std::unique_ptr<Talon>(new Talon(5)));
710 claw_writer.set_right_intake_talon(
711 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800712 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800713 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800714 ::std::thread claw_writer_thread(::std::ref(claw_writer));
715
716 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
717 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500718 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800719 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
720 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800721 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800722 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
723 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700724 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
725 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800726
727 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
728 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500729 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
730
731 // Wait forever. Not much else to do...
732 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
733
Austin Schuh010eb812014-10-25 18:06:49 -0700734 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800735
Austin Schuh010eb812014-10-25 18:06:49 -0700736 joystick_sender.Quit();
737 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500738 reader.Quit();
739 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800740 gyro_sender.Quit();
741 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500742
743 drivetrain_writer.Quit();
744 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700745 can_writer.Quit();
746 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500747 solenoid_writer.Quit();
748 solenoid_thread.join();
749
Austin Schuh010eb812014-10-25 18:06:49 -0700750 ::aos::Cleanup();
751 }
752};
753
Brian Silverman98f6ee22015-01-26 17:50:12 -0500754} // namespace wpilib
755} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800756
Brian Silverman98f6ee22015-01-26 17:50:12 -0500757
758START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);