blob: 4d49b4106dc1eb8e0f804d9106580a60e3c28d50 [file] [log] [blame]
Austin Schuh010eb812014-10-25 18:06:49 -07001#include <stdio.h>
2#include <string.h>
Austin Schuh010eb812014-10-25 18:06:49 -07003#include <unistd.h>
4#include <inttypes.h>
5
Brian Silvermand8f403a2014-12-13 19:12:04 -05006#include <thread>
7#include <mutex>
8#include <functional>
9
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070010#include "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
15#include "Relay.h"
16#include "RobotBase.h"
17#include "dma.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#undef ERROR
22
Austin Schuh010eb812014-10-25 18:06:49 -070023#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
Austin Schuh010eb812014-10-25 18:06:49 -070025#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040029#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070030#include "aos/linux_code/init.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050031#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070032
Brian Silverman335c20e2015-01-26 21:47:58 -050033#include "frc971/control_loops/control_loops.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050034
35#include "y2015/constants.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070036#include "y2015/control_loops/drivetrain/drivetrain.q.h"
37#include "y2015/control_loops/fridge/fridge.q.h"
38#include "y2015/control_loops/claw/claw.q.h"
Austin Schuhbb227f82015-09-06 15:27:52 -070039#include "y2015/autonomous/auto.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070040
Brian Silvermanda45b6c2014-12-28 11:36:50 -080041#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050042#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080045#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050046#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050047#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050048#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silverman87541532015-03-19 23:35:12 -070049#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050050#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman425492b2015-12-30 15:23:55 -080051#include "frc971/wpilib/pdp_fetcher.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 Silverman425492b2015-12-30 15:23:55 -0800129 SensorReader(::frc971::wpilib::PDPFetcher *pdp_fetcher)
130 : pdp_fetcher_(pdp_fetcher) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500131 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
132 // we should ever see.
133 filter_.SetPeriodNanoSeconds(
134 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
135 }
136
137 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
138 filter_.Add(encoder.get());
139 arm_left_encoder_.set_encoder(::std::move(encoder));
140 }
141
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700142 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500143 filter_.Add(index.get());
144 arm_left_encoder_.set_index(::std::move(index));
145 }
146
147 void set_arm_left_potentiometer(
148 ::std::unique_ptr<AnalogInput> potentiometer) {
149 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
150 }
151
152 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
153 filter_.Add(encoder.get());
154 arm_right_encoder_.set_encoder(::std::move(encoder));
155 }
156
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700157 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500158 filter_.Add(index.get());
159 arm_right_encoder_.set_index(::std::move(index));
160 }
161
162 void set_arm_right_potentiometer(
163 ::std::unique_ptr<AnalogInput> potentiometer) {
164 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
165 }
166
167 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
168 filter_.Add(encoder.get());
169 elevator_left_encoder_.set_encoder(::std::move(encoder));
170 }
171
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700172 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500173 filter_.Add(index.get());
174 elevator_left_encoder_.set_index(::std::move(index));
175 }
176
177 void set_elevator_left_potentiometer(
178 ::std::unique_ptr<AnalogInput> potentiometer) {
179 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
180 }
181
182 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
183 filter_.Add(encoder.get());
184 elevator_right_encoder_.set_encoder(::std::move(encoder));
185 }
186
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700187 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500188 filter_.Add(index.get());
189 elevator_right_encoder_.set_index(::std::move(index));
190 }
191
192 void set_elevator_right_potentiometer(
193 ::std::unique_ptr<AnalogInput> potentiometer) {
194 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
195 }
196
197 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
198 filter_.Add(encoder.get());
199 wrist_encoder_.set_encoder(::std::move(encoder));
200 }
201
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700202 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500203 filter_.Add(index.get());
204 wrist_encoder_.set_index(::std::move(index));
205 }
206
207 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
208 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700209 }
210
Brian Silverman1f90d672015-01-26 20:20:45 -0500211 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
212 left_encoder_ = ::std::move(left_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800213 left_encoder_->SetMaxPeriod(0.005);
Brian Silverman1f90d672015-01-26 20:20:45 -0500214 }
215
216 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
217 right_encoder_ = ::std::move(right_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800218 right_encoder_->SetMaxPeriod(0.005);
Brian Silverman1f90d672015-01-26 20:20:45 -0500219 }
220
Brian Silverman335c20e2015-01-26 21:47:58 -0500221 // All of the DMA-related set_* calls must be made before this, and it doesn't
222 // hurt to do all of them.
223 void set_dma(::std::unique_ptr<DMA> dma) {
224 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
225 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500226 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800227 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500228 dma_synchronizer_->Add(&elevator_right_encoder_);
229 }
230
Austin Schuh010eb812014-10-25 18:06:49 -0700231 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -0800232 ::aos::SetCurrentThreadName("SensorReader");
233
Brian Silverman699f0cb2015-02-05 19:45:01 -0500234 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700235 ds_ =
236#ifdef WPILIB2015
237 DriverStation::GetInstance();
238#else
239 &DriverStation::GetInstance();
240#endif
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 Silverman425492b2015-12-30 15:23:55 -0800255 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_fetcher_);
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 Silverman425492b2015-12-30 15:23:55 -0800312 ::frc971::wpilib::PDPFetcher *const pdp_fetcher_;
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 Silverman425492b2015-12-30 15:23:55 -0800650 ::frc971::wpilib::PDPFetcher pdp_fetcher;
651 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
652
653 SensorReader reader(&pdp_fetcher);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800654 reader.set_arm_left_encoder(encoder(1));
655 reader.set_arm_left_index(make_unique<DigitalInput>(1));
656 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
657
658 reader.set_arm_right_encoder(encoder(5));
659 reader.set_arm_right_index(make_unique<DigitalInput>(5));
660 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
661
662 reader.set_elevator_left_encoder(encoder(0));
663 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
664 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
665
666 reader.set_elevator_right_encoder(encoder(4));
667 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
668 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
669
670 reader.set_wrist_encoder(encoder(6));
671 reader.set_wrist_index(make_unique<DigitalInput>(6));
672 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
673
674 reader.set_left_encoder(encoder(2));
675 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500676 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500677 ::std::thread reader_thread(::std::ref(reader));
678 GyroSender gyro_sender;
679 ::std::thread gyro_thread(::std::ref(gyro_sender));
680
681 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500682 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800683 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500684 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800685 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500686 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
687
Austin Schuhbb227f82015-09-06 15:27:52 -0700688 CanWriter can_writer;
689 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
690 ::std::thread can_writer_thread(::std::ref(can_writer));
691
Daniel Pettiadf38432015-01-26 17:13:35 -0800692 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
693 // claw.
694 FridgeWriter fridge_writer;
695 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800696 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800697 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800698 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800699 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800700 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800701 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800702 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800703 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
704
705 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800706 claw_writer.set_left_intake_talon(
707 ::std::unique_ptr<Talon>(new Talon(5)));
708 claw_writer.set_right_intake_talon(
709 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800710 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800711 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800712 ::std::thread claw_writer_thread(::std::ref(claw_writer));
713
714 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
715 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500716 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800717 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
718 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800719 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800720 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
721 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700722 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
723 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800724
725 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
726 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500727 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
728
729 // Wait forever. Not much else to do...
730 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
731
Austin Schuh010eb812014-10-25 18:06:49 -0700732 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800733
Austin Schuh010eb812014-10-25 18:06:49 -0700734 joystick_sender.Quit();
735 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800736 pdp_fetcher.Quit();
737 pdp_fetcher_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);