blob: 0ea4b8d0daf5c4cf875aaea04c18ed4e71e6de13 [file] [log] [blame]
Austin Schuh010eb812014-10-25 18:06:49 -07001#include <stdio.h>
2#include <string.h>
Austin Schuh010eb812014-10-25 18:06:49 -07003#include <unistd.h>
4#include <inttypes.h>
5
Brian Silvermand8f403a2014-12-13 19:12:04 -05006#include <thread>
7#include <mutex>
8#include <functional>
9
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070010#include "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
15#include "Relay.h"
16#include "RobotBase.h"
17#include "dma.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -050021#include "PowerDistributionPanel.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070022#undef ERROR
23
Austin Schuh010eb812014-10-25 18:06:49 -070024#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
Austin Schuh010eb812014-10-25 18:06:49 -070026#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040030#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070031#include "aos/linux_code/init.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050032#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070033
Brian Silverman335c20e2015-01-26 21:47:58 -050034#include "frc971/control_loops/control_loops.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050035
36#include "y2015/constants.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070037#include "y2015/control_loops/drivetrain/drivetrain.q.h"
38#include "y2015/control_loops/fridge/fridge.q.h"
39#include "y2015/control_loops/claw/claw.q.h"
Austin Schuhbb227f82015-09-06 15:27:52 -070040#include "y2015/autonomous/auto.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070041
Brian Silvermanda45b6c2014-12-28 11:36:50 -080042#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050043#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/buffered_solenoid.h"
45#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080046#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050047#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050048#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050049#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silverman87541532015-03-19 23:35:12 -070050#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050051#include "frc971/wpilib/wpilib_interface.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080052
Austin Schuh010eb812014-10-25 18:06:49 -070053#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050058using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050059using ::frc971::control_loops::fridge_queue;
60using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070061
62namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080063namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070064
Austin Schuh010eb812014-10-25 18:06:49 -070065double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080066 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080067 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080068 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080069 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
70}
71
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 Silverman1f90d672015-01-26 20:20:45 -0500129 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 Silvermanc7e8fdd2015-12-06 02:48:27 -0500240 pdp_.reset(new PowerDistributionPanel());
Brian Silverman699f0cb2015-02-05 19:45:01 -0500241
Brian Silverman335c20e2015-01-26 21:47:58 -0500242 wrist_encoder_.Start();
243 dma_synchronizer_->Start();
Austin Schuh010eb812014-10-25 18:06:49 -0700244
Brian Silverman2fe007c2014-12-28 12:20:01 -0800245 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700246 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800247 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700248 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700249 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500250
251 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700252 }
253
254 void RunIteration() {
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500255 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
Austin Schuh010eb812014-10-25 18:06:49 -0700256
Austin Schuh35d06612015-02-15 23:35:23 -0800257 {
258 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
259 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800260 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800261 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800262 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800263 drivetrain_message->left_speed =
264 drivetrain_velocity_translate(left_encoder_->GetPeriod());
265 drivetrain_message->right_speed =
266 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Austin Schuh35d06612015-02-15 23:35:23 -0800267
268 drivetrain_message.Send();
269 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500270
271 dma_synchronizer_->RunIteration();
272
Philipp Schrader82c65072015-02-16 00:47:09 +0000273 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500274
Brian Silverman335c20e2015-01-26 21:47:58 -0500275 {
276 auto fridge_message = fridge_queue.position.MakeMessage();
277 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500278 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800279 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500280 CopyPotAndIndexPosition(
281 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
282 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800283 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500284 CopyPotAndIndexPosition(
285 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500286 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800287 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500288 CopyPotAndIndexPosition(
289 elevator_right_encoder_, &fridge_message->elevator.right,
290 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800291 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500292 fridge_message.Send();
293 }
294
295 {
296 auto claw_message = claw_queue.position.MakeMessage();
297 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500298 claw_translate, claw_potentiometer_translate,
299 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500300 claw_message.Send();
301 }
Austin Schuh010eb812014-10-25 18:06:49 -0700302 }
303
304 void Quit() { run_ = false; }
305
306 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500307 static const int kPriority = 30;
308 static const int kInterruptPriority = 55;
309
Brian Silverman699f0cb2015-02-05 19:45:01 -0500310 int32_t my_pid_;
311 DriverStation *ds_;
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500312 ::std::unique_ptr<PowerDistributionPanel> pdp_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500313
Brian Silverman335c20e2015-01-26 21:47:58 -0500314 void CopyPotAndIndexPosition(
315 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
316 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500317 ::std::function<double(double)> potentiometer_translate, bool reverse,
318 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500319 const double multiplier = reverse ? -1.0 : 1.0;
320 position->encoder =
321 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500322 position->pot = multiplier * potentiometer_translate(
323 encoder.polled_potentiometer_voltage()) +
324 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500325 position->latched_encoder =
326 multiplier * encoder_translate(encoder.last_encoder_value());
327 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500328 multiplier *
329 potentiometer_translate(encoder.last_potentiometer_voltage()) +
330 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500331 position->index_pulses = encoder.index_posedge_count();
332 }
333
334 void CopyPotAndIndexPosition(
335 const InterruptEncoderAndPotentiometer &encoder,
336 PotAndIndexPosition *position,
337 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500338 ::std::function<double(double)> potentiometer_translate, bool reverse,
339 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500340 const double multiplier = reverse ? -1.0 : 1.0;
341 position->encoder =
342 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500343 position->pot = multiplier * potentiometer_translate(
344 encoder.potentiometer()->GetVoltage()) +
345 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500346 position->latched_encoder =
347 multiplier * encoder_translate(encoder.last_encoder_value());
348 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500349 multiplier *
350 potentiometer_translate(encoder.last_potentiometer_voltage()) +
351 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500352 position->index_pulses = encoder.index_posedge_count();
353 }
354
Brian Silverman335c20e2015-01-26 21:47:58 -0500355 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
356
357 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
358 elevator_left_encoder_, elevator_right_encoder_;
359
360 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
361
Austin Schuh010eb812014-10-25 18:06:49 -0700362 ::std::unique_ptr<Encoder> left_encoder_;
363 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700364
Brian Silverman1f90d672015-01-26 20:20:45 -0500365 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700366 DigitalGlitchFilter filter_;
367};
368
Brian Silvermand8f403a2014-12-13 19:12:04 -0500369class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700370 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500371 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800372 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800373 fridge_(".frc971.control_loops.fridge_queue.output"),
374 claw_(".frc971.control_loops.claw_queue.output") {}
375
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700376 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800377 pressure_switch_ = ::std::move(pressure_switch);
378 }
379
380 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
381 compressor_relay_ = ::std::move(compressor_relay);
382 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500383
Daniel Pettiadf38432015-01-26 17:13:35 -0800384 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
385 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700386 }
387
Daniel Pettiadf38432015-01-26 17:13:35 -0800388 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
389 fridge_grabbers_top_back_ = ::std::move(s);
390 }
391
392 void set_fridge_grabbers_bottom_front(
393 ::std::unique_ptr<BufferedSolenoid> s) {
394 fridge_grabbers_bottom_front_ = ::std::move(s);
395 }
396
397 void set_fridge_grabbers_bottom_back(
398 ::std::unique_ptr<BufferedSolenoid> s) {
399 fridge_grabbers_bottom_back_ = ::std::move(s);
400 }
401
402 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
403 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500404 }
Austin Schuh010eb812014-10-25 18:06:49 -0700405
Brian Silverman93936f72015-03-19 23:38:30 -0700406 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
407 grabber_latch_release_ = ::std::move(s);
408 }
409
410 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
411 grabber_fold_up_ = ::std::move(s);
412 }
413
Brian Silvermand8f403a2014-12-13 19:12:04 -0500414 void operator()() {
415 ::aos::SetCurrentThreadName("Solenoids");
416 ::aos::SetCurrentThreadRealtimePriority(30);
417
418 while (run_) {
419 ::aos::time::PhasedLoopXMS(20, 1000);
420
421 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800422 fridge_.FetchLatest();
423 if (fridge_.get()) {
424 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800425 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
426 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
427 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
428 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800429 }
430 }
431
432 {
433 claw_.FetchLatest();
434 if (claw_.get()) {
435 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800436 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500437 }
438 }
439
Brian Silverman93936f72015-03-19 23:38:30 -0700440 ::aos::joystick_state.FetchLatest();
441 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
442 ::aos::joystick_state->autonomous);
443 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
444 ::aos::joystick_state->joysticks[1].buttons & 1);
445
Brian Silverman87541532015-03-19 23:35:12 -0700446 {
447 PneumaticsToLog to_log;
448 {
449 const bool compressor_on = !pressure_switch_->Get();
450 to_log.compressor_on = compressor_on;
451 if (compressor_on) {
452 compressor_relay_->Set(Relay::kForward);
453 } else {
454 compressor_relay_->Set(Relay::kOff);
455 }
456 }
457
458 pcm_->Flush();
459 to_log.read_solenoids = pcm_->GetAll();
460 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
461 }
Austin Schuh010eb812014-10-25 18:06:49 -0700462 }
463 }
464
Brian Silvermand8f403a2014-12-13 19:12:04 -0500465 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700466
Brian Silvermand8f403a2014-12-13 19:12:04 -0500467 private:
468 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800469 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
470 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
471 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
472 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
473 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700474 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
475 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700476 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800477 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700478
Daniel Pettiadf38432015-01-26 17:13:35 -0800479 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
480 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700481
Brian Silvermand8f403a2014-12-13 19:12:04 -0500482 ::std::atomic<bool> run_{true};
483};
484
Austin Schuhbb227f82015-09-06 15:27:52 -0700485class CanWriter : public LoopOutputHandler {
486 public:
487 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
488
489 void set_can_talon(::std::unique_ptr<Talon> t) {
490 can_talon_ = ::std::move(t);
491 }
492
493 private:
494 virtual void Read() override {
495 ::frc971::autonomous::can_control.FetchAnother();
496 }
497
498 virtual void Write() override {
499 auto &queue = ::frc971::autonomous::can_control;
500 LOG_STRUCT(DEBUG, "will output", *queue);
501 can_talon_->Set(queue->can_voltage / 12.0);
502 }
503
504 virtual void Stop() override {
505 LOG(WARNING, "Can output too old\n");
506 can_talon_->Disable();
507 }
508
509 ::std::unique_ptr<Talon> can_talon_;
510};
511
Brian Silvermand8f403a2014-12-13 19:12:04 -0500512class DrivetrainWriter : public LoopOutputHandler {
513 public:
514 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
515 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700516 }
517
Brian Silvermand8f403a2014-12-13 19:12:04 -0500518 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
519 right_drivetrain_talon_ = ::std::move(t);
520 }
Austin Schuh010eb812014-10-25 18:06:49 -0700521
Brian Silvermand8f403a2014-12-13 19:12:04 -0500522 private:
523 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500524 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500525 }
526
527 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500528 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500529 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800530 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
531 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500532 }
533
534 virtual void Stop() override {
535 LOG(WARNING, "drivetrain output too old\n");
536 left_drivetrain_talon_->Disable();
537 right_drivetrain_talon_->Disable();
538 }
539
Austin Schuh010eb812014-10-25 18:06:49 -0700540 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500541 ::std::unique_ptr<Talon> right_drivetrain_talon_;
542};
543
Daniel Pettiadf38432015-01-26 17:13:35 -0800544class FridgeWriter : public LoopOutputHandler {
545 public:
546 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
547 left_arm_talon_ = ::std::move(t);
548 }
549
550 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
551 right_arm_talon_ = ::std::move(t);
552 }
553
554 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
555 left_elevator_talon_ = ::std::move(t);
556 }
557
558 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
559 right_elevator_talon_ = ::std::move(t);
560 }
561
562 private:
563 virtual void Read() override {
564 ::frc971::control_loops::fridge_queue.output.FetchAnother();
565 }
566
567 virtual void Write() override {
568 auto &queue = ::frc971::control_loops::fridge_queue.output;
569 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800570 left_arm_talon_->Set(queue->left_arm / 12.0);
571 right_arm_talon_->Set(-queue->right_arm / 12.0);
572 left_elevator_talon_->Set(queue->left_elevator / 12.0);
573 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800574 }
575
576 virtual void Stop() override {
577 LOG(WARNING, "Fridge output too old.\n");
578 left_arm_talon_->Disable();
579 right_arm_talon_->Disable();
580 left_elevator_talon_->Disable();
581 right_elevator_talon_->Disable();
582 }
583
584 ::std::unique_ptr<Talon> left_arm_talon_;
585 ::std::unique_ptr<Talon> right_arm_talon_;
586 ::std::unique_ptr<Talon> left_elevator_talon_;
587 ::std::unique_ptr<Talon> right_elevator_talon_;
588};
589
590class ClawWriter : public LoopOutputHandler {
591 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800592 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
593 left_intake_talon_ = ::std::move(t);
594 }
595
596 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
597 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800598 }
599
600 void set_wrist_talon(::std::unique_ptr<Talon> t) {
601 wrist_talon_ = ::std::move(t);
602 }
603
604 private:
605 virtual void Read() override {
606 ::frc971::control_loops::claw_queue.output.FetchAnother();
607 }
608
609 virtual void Write() override {
610 auto &queue = ::frc971::control_loops::claw_queue.output;
611 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800612 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800613 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
614 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800615 }
616
617 virtual void Stop() override {
618 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800619 left_intake_talon_->Disable();
620 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800621 wrist_talon_->Disable();
622 }
623
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800624 ::std::unique_ptr<Talon> left_intake_talon_;
625 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800626 ::std::unique_ptr<Talon> wrist_talon_;
627};
628
Brian Silverman1f90d672015-01-26 20:20:45 -0500629// TODO(brian): Replace this with ::std::make_unique once all our toolchains
630// have support.
631template <class T, class... U>
632std::unique_ptr<T> make_unique(U &&... u) {
633 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
634}
635
Austin Schuh010eb812014-10-25 18:06:49 -0700636class WPILibRobot : public RobotBase {
637 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800638 ::std::unique_ptr<Encoder> encoder(int index) {
639 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
640 Encoder::k4X);
641 }
Austin Schuh010eb812014-10-25 18:06:49 -0700642 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500643 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800644 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500645
Brian Silverman98f6ee22015-01-26 17:50:12 -0500646 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700647 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800648 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500649
Brian Silverman98f6ee22015-01-26 17:50:12 -0500650 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800651 reader.set_arm_left_encoder(encoder(1));
652 reader.set_arm_left_index(make_unique<DigitalInput>(1));
653 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
654
655 reader.set_arm_right_encoder(encoder(5));
656 reader.set_arm_right_index(make_unique<DigitalInput>(5));
657 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
658
659 reader.set_elevator_left_encoder(encoder(0));
660 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
661 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
662
663 reader.set_elevator_right_encoder(encoder(4));
664 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
665 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
666
667 reader.set_wrist_encoder(encoder(6));
668 reader.set_wrist_index(make_unique<DigitalInput>(6));
669 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
670
671 reader.set_left_encoder(encoder(2));
672 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500673 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500674 ::std::thread reader_thread(::std::ref(reader));
675 GyroSender gyro_sender;
676 ::std::thread gyro_thread(::std::ref(gyro_sender));
677
678 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500679 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800680 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500681 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800682 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500683 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
684
Austin Schuhbb227f82015-09-06 15:27:52 -0700685 CanWriter can_writer;
686 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
687 ::std::thread can_writer_thread(::std::ref(can_writer));
688
Daniel Pettiadf38432015-01-26 17:13:35 -0800689 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
690 // claw.
691 FridgeWriter fridge_writer;
692 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800693 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800694 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800695 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800696 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800697 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800698 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800699 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800700 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
701
702 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800703 claw_writer.set_left_intake_talon(
704 ::std::unique_ptr<Talon>(new Talon(5)));
705 claw_writer.set_right_intake_talon(
706 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800707 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800708 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800709 ::std::thread claw_writer_thread(::std::ref(claw_writer));
710
711 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
712 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500713 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800714 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
715 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800716 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800717 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
718 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700719 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
720 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800721
722 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
723 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500724 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
725
726 // Wait forever. Not much else to do...
727 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
728
Austin Schuh010eb812014-10-25 18:06:49 -0700729 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800730
Austin Schuh010eb812014-10-25 18:06:49 -0700731 joystick_sender.Quit();
732 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500733 reader.Quit();
734 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800735 gyro_sender.Quit();
736 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500737
738 drivetrain_writer.Quit();
739 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700740 can_writer.Quit();
741 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500742 solenoid_writer.Quit();
743 solenoid_thread.join();
744
Austin Schuh010eb812014-10-25 18:06:49 -0700745 ::aos::Cleanup();
746 }
747};
748
Brian Silverman98f6ee22015-01-26 17:50:12 -0500749} // namespace wpilib
750} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800751
Brian Silverman98f6ee22015-01-26 17:50:12 -0500752
753START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);