blob: fa7d14a585bf1d433dddc47a9ec619bd30706815 [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
72double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080073 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080074 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080075 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080076 (2 * M_PI /*radians*/);
77}
78
Brian Silverman5d712fc2015-02-15 03:39:31 -050079double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080080 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080081 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080082 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080083 (2 * M_PI /*radians*/);
84}
85
86double elevator_translate(int32_t in) {
87 return static_cast<double>(in) /
88 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080089 constants::GetValues().elev_encoder_ratio *
90 (2 * M_PI /*radians*/) *
91 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080092}
93
Brian Silverman5d712fc2015-02-15 03:39:31 -050094double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -080095 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080096 constants::GetValues().elev_pot_ratio *
97 (2 * M_PI /*radians*/) *
98 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -080099 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -0800100}
101
102double claw_translate(int32_t in) {
103 return static_cast<double>(in) /
104 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800105 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800106 (2 * M_PI /*radians*/);
107}
108
Brian Silverman5d712fc2015-02-15 03:39:31 -0500109double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800110 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800111 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800112 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800113 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700114}
115
Brian Silverman335c20e2015-01-26 21:47:58 -0500116static const double kMaximumEncoderPulsesPerSecond =
117 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
118 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
119 4.0 /* index pulse = 1/4 cycle */;
120
Austin Schuh010eb812014-10-25 18:06:49 -0700121class SensorReader {
122 public:
Brian Silverman1f90d672015-01-26 20:20:45 -0500123 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500124 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
125 // we should ever see.
126 filter_.SetPeriodNanoSeconds(
127 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
128 }
129
130 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
131 filter_.Add(encoder.get());
132 arm_left_encoder_.set_encoder(::std::move(encoder));
133 }
134
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700135 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500136 filter_.Add(index.get());
137 arm_left_encoder_.set_index(::std::move(index));
138 }
139
140 void set_arm_left_potentiometer(
141 ::std::unique_ptr<AnalogInput> potentiometer) {
142 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
143 }
144
145 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
146 filter_.Add(encoder.get());
147 arm_right_encoder_.set_encoder(::std::move(encoder));
148 }
149
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700150 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500151 filter_.Add(index.get());
152 arm_right_encoder_.set_index(::std::move(index));
153 }
154
155 void set_arm_right_potentiometer(
156 ::std::unique_ptr<AnalogInput> potentiometer) {
157 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
158 }
159
160 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
161 filter_.Add(encoder.get());
162 elevator_left_encoder_.set_encoder(::std::move(encoder));
163 }
164
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700165 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500166 filter_.Add(index.get());
167 elevator_left_encoder_.set_index(::std::move(index));
168 }
169
170 void set_elevator_left_potentiometer(
171 ::std::unique_ptr<AnalogInput> potentiometer) {
172 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
173 }
174
175 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
176 filter_.Add(encoder.get());
177 elevator_right_encoder_.set_encoder(::std::move(encoder));
178 }
179
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700180 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500181 filter_.Add(index.get());
182 elevator_right_encoder_.set_index(::std::move(index));
183 }
184
185 void set_elevator_right_potentiometer(
186 ::std::unique_ptr<AnalogInput> potentiometer) {
187 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
188 }
189
190 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
191 filter_.Add(encoder.get());
192 wrist_encoder_.set_encoder(::std::move(encoder));
193 }
194
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700195 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500196 filter_.Add(index.get());
197 wrist_encoder_.set_index(::std::move(index));
198 }
199
200 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
201 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700202 }
203
Brian Silverman1f90d672015-01-26 20:20:45 -0500204 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
205 left_encoder_ = ::std::move(left_encoder);
206 }
207
208 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
209 right_encoder_ = ::std::move(right_encoder);
210 }
211
Brian Silverman335c20e2015-01-26 21:47:58 -0500212 // All of the DMA-related set_* calls must be made before this, and it doesn't
213 // hurt to do all of them.
214 void set_dma(::std::unique_ptr<DMA> dma) {
215 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
216 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500217 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800218 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500219 dma_synchronizer_->Add(&elevator_right_encoder_);
220 }
221
Austin Schuh010eb812014-10-25 18:06:49 -0700222 void operator()() {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800223 LOG(INFO, "In sensor reader thread\n");
Brian Silverman2fe007c2014-12-28 12:20:01 -0800224 ::aos::SetCurrentThreadName("SensorReader");
225
Brian Silverman699f0cb2015-02-05 19:45:01 -0500226 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700227 ds_ =
228#ifdef WPILIB2015
229 DriverStation::GetInstance();
230#else
231 &DriverStation::GetInstance();
232#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500233 pdp_.reset(new PowerDistributionPanel());
Brian Silverman699f0cb2015-02-05 19:45:01 -0500234
Brian Silverman335c20e2015-01-26 21:47:58 -0500235 wrist_encoder_.Start();
236 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800237 LOG(INFO, "Things are now started\n");
Austin Schuh010eb812014-10-25 18:06:49 -0700238
Brian Silverman2fe007c2014-12-28 12:20:01 -0800239 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700240 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800241 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700242 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700243 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500244
245 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700246 }
247
248 void RunIteration() {
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500249 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
Austin Schuh010eb812014-10-25 18:06:49 -0700250
Austin Schuh35d06612015-02-15 23:35:23 -0800251 {
252 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
253 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800254 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800255 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800256 drivetrain_translate(left_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800257
258 drivetrain_message.Send();
259 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500260
261 dma_synchronizer_->RunIteration();
262
Philipp Schrader82c65072015-02-16 00:47:09 +0000263 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500264
Brian Silverman335c20e2015-01-26 21:47:58 -0500265 {
266 auto fridge_message = fridge_queue.position.MakeMessage();
267 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500268 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800269 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500270 CopyPotAndIndexPosition(
271 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
272 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800273 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500274 CopyPotAndIndexPosition(
275 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500276 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800277 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500278 CopyPotAndIndexPosition(
279 elevator_right_encoder_, &fridge_message->elevator.right,
280 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800281 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500282 fridge_message.Send();
283 }
284
285 {
286 auto claw_message = claw_queue.position.MakeMessage();
287 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500288 claw_translate, claw_potentiometer_translate,
289 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500290 claw_message.Send();
291 }
Austin Schuh010eb812014-10-25 18:06:49 -0700292 }
293
294 void Quit() { run_ = false; }
295
296 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500297 static const int kPriority = 30;
298 static const int kInterruptPriority = 55;
299
Brian Silverman699f0cb2015-02-05 19:45:01 -0500300 int32_t my_pid_;
301 DriverStation *ds_;
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500302 ::std::unique_ptr<PowerDistributionPanel> pdp_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500303
Brian Silverman335c20e2015-01-26 21:47:58 -0500304 void CopyPotAndIndexPosition(
305 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
306 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500307 ::std::function<double(double)> potentiometer_translate, bool reverse,
308 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500309 const double multiplier = reverse ? -1.0 : 1.0;
310 position->encoder =
311 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500312 position->pot = multiplier * potentiometer_translate(
313 encoder.polled_potentiometer_voltage()) +
314 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500315 position->latched_encoder =
316 multiplier * encoder_translate(encoder.last_encoder_value());
317 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500318 multiplier *
319 potentiometer_translate(encoder.last_potentiometer_voltage()) +
320 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500321 position->index_pulses = encoder.index_posedge_count();
322 }
323
324 void CopyPotAndIndexPosition(
325 const InterruptEncoderAndPotentiometer &encoder,
326 PotAndIndexPosition *position,
327 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500328 ::std::function<double(double)> potentiometer_translate, bool reverse,
329 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500330 const double multiplier = reverse ? -1.0 : 1.0;
331 position->encoder =
332 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500333 position->pot = multiplier * potentiometer_translate(
334 encoder.potentiometer()->GetVoltage()) +
335 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500336 position->latched_encoder =
337 multiplier * encoder_translate(encoder.last_encoder_value());
338 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500339 multiplier *
340 potentiometer_translate(encoder.last_potentiometer_voltage()) +
341 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500342 position->index_pulses = encoder.index_posedge_count();
343 }
344
Brian Silverman335c20e2015-01-26 21:47:58 -0500345 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
346
347 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
348 elevator_left_encoder_, elevator_right_encoder_;
349
350 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
351
Austin Schuh010eb812014-10-25 18:06:49 -0700352 ::std::unique_ptr<Encoder> left_encoder_;
353 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700354
Brian Silverman1f90d672015-01-26 20:20:45 -0500355 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700356 DigitalGlitchFilter filter_;
357};
358
Brian Silvermand8f403a2014-12-13 19:12:04 -0500359class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700360 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500361 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800362 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800363 fridge_(".frc971.control_loops.fridge_queue.output"),
364 claw_(".frc971.control_loops.claw_queue.output") {}
365
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700366 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800367 pressure_switch_ = ::std::move(pressure_switch);
368 }
369
370 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
371 compressor_relay_ = ::std::move(compressor_relay);
372 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500373
Daniel Pettiadf38432015-01-26 17:13:35 -0800374 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
375 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700376 }
377
Daniel Pettiadf38432015-01-26 17:13:35 -0800378 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
379 fridge_grabbers_top_back_ = ::std::move(s);
380 }
381
382 void set_fridge_grabbers_bottom_front(
383 ::std::unique_ptr<BufferedSolenoid> s) {
384 fridge_grabbers_bottom_front_ = ::std::move(s);
385 }
386
387 void set_fridge_grabbers_bottom_back(
388 ::std::unique_ptr<BufferedSolenoid> s) {
389 fridge_grabbers_bottom_back_ = ::std::move(s);
390 }
391
392 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
393 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500394 }
Austin Schuh010eb812014-10-25 18:06:49 -0700395
Brian Silverman93936f72015-03-19 23:38:30 -0700396 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
397 grabber_latch_release_ = ::std::move(s);
398 }
399
400 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
401 grabber_fold_up_ = ::std::move(s);
402 }
403
Brian Silvermand8f403a2014-12-13 19:12:04 -0500404 void operator()() {
405 ::aos::SetCurrentThreadName("Solenoids");
406 ::aos::SetCurrentThreadRealtimePriority(30);
407
408 while (run_) {
409 ::aos::time::PhasedLoopXMS(20, 1000);
410
411 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800412 fridge_.FetchLatest();
413 if (fridge_.get()) {
414 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800415 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
416 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
417 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
418 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800419 }
420 }
421
422 {
423 claw_.FetchLatest();
424 if (claw_.get()) {
425 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800426 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500427 }
428 }
429
Brian Silverman93936f72015-03-19 23:38:30 -0700430 ::aos::joystick_state.FetchLatest();
431 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
432 ::aos::joystick_state->autonomous);
433 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
434 ::aos::joystick_state->joysticks[1].buttons & 1);
435
Brian Silverman87541532015-03-19 23:35:12 -0700436 {
437 PneumaticsToLog to_log;
438 {
439 const bool compressor_on = !pressure_switch_->Get();
440 to_log.compressor_on = compressor_on;
441 if (compressor_on) {
442 compressor_relay_->Set(Relay::kForward);
443 } else {
444 compressor_relay_->Set(Relay::kOff);
445 }
446 }
447
448 pcm_->Flush();
449 to_log.read_solenoids = pcm_->GetAll();
450 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
451 }
Austin Schuh010eb812014-10-25 18:06:49 -0700452 }
453 }
454
Brian Silvermand8f403a2014-12-13 19:12:04 -0500455 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700456
Brian Silvermand8f403a2014-12-13 19:12:04 -0500457 private:
458 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800459 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
460 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
461 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
462 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
463 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700464 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
465 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700466 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800467 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700468
Daniel Pettiadf38432015-01-26 17:13:35 -0800469 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
470 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700471
Brian Silvermand8f403a2014-12-13 19:12:04 -0500472 ::std::atomic<bool> run_{true};
473};
474
Austin Schuhbb227f82015-09-06 15:27:52 -0700475class CanWriter : public LoopOutputHandler {
476 public:
477 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
478
479 void set_can_talon(::std::unique_ptr<Talon> t) {
480 can_talon_ = ::std::move(t);
481 }
482
483 private:
484 virtual void Read() override {
485 ::frc971::autonomous::can_control.FetchAnother();
486 }
487
488 virtual void Write() override {
489 auto &queue = ::frc971::autonomous::can_control;
490 LOG_STRUCT(DEBUG, "will output", *queue);
491 can_talon_->Set(queue->can_voltage / 12.0);
492 }
493
494 virtual void Stop() override {
495 LOG(WARNING, "Can output too old\n");
496 can_talon_->Disable();
497 }
498
499 ::std::unique_ptr<Talon> can_talon_;
500};
501
Brian Silvermand8f403a2014-12-13 19:12:04 -0500502class DrivetrainWriter : public LoopOutputHandler {
503 public:
504 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
505 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700506 }
507
Brian Silvermand8f403a2014-12-13 19:12:04 -0500508 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
509 right_drivetrain_talon_ = ::std::move(t);
510 }
Austin Schuh010eb812014-10-25 18:06:49 -0700511
Brian Silvermand8f403a2014-12-13 19:12:04 -0500512 private:
513 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500514 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500515 }
516
517 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500518 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500519 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800520 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
521 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500522 }
523
524 virtual void Stop() override {
525 LOG(WARNING, "drivetrain output too old\n");
526 left_drivetrain_talon_->Disable();
527 right_drivetrain_talon_->Disable();
528 }
529
Austin Schuh010eb812014-10-25 18:06:49 -0700530 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500531 ::std::unique_ptr<Talon> right_drivetrain_talon_;
532};
533
Daniel Pettiadf38432015-01-26 17:13:35 -0800534class FridgeWriter : public LoopOutputHandler {
535 public:
536 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
537 left_arm_talon_ = ::std::move(t);
538 }
539
540 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
541 right_arm_talon_ = ::std::move(t);
542 }
543
544 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
545 left_elevator_talon_ = ::std::move(t);
546 }
547
548 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
549 right_elevator_talon_ = ::std::move(t);
550 }
551
552 private:
553 virtual void Read() override {
554 ::frc971::control_loops::fridge_queue.output.FetchAnother();
555 }
556
557 virtual void Write() override {
558 auto &queue = ::frc971::control_loops::fridge_queue.output;
559 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800560 left_arm_talon_->Set(queue->left_arm / 12.0);
561 right_arm_talon_->Set(-queue->right_arm / 12.0);
562 left_elevator_talon_->Set(queue->left_elevator / 12.0);
563 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800564 }
565
566 virtual void Stop() override {
567 LOG(WARNING, "Fridge output too old.\n");
568 left_arm_talon_->Disable();
569 right_arm_talon_->Disable();
570 left_elevator_talon_->Disable();
571 right_elevator_talon_->Disable();
572 }
573
574 ::std::unique_ptr<Talon> left_arm_talon_;
575 ::std::unique_ptr<Talon> right_arm_talon_;
576 ::std::unique_ptr<Talon> left_elevator_talon_;
577 ::std::unique_ptr<Talon> right_elevator_talon_;
578};
579
580class ClawWriter : public LoopOutputHandler {
581 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800582 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
583 left_intake_talon_ = ::std::move(t);
584 }
585
586 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
587 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800588 }
589
590 void set_wrist_talon(::std::unique_ptr<Talon> t) {
591 wrist_talon_ = ::std::move(t);
592 }
593
594 private:
595 virtual void Read() override {
596 ::frc971::control_loops::claw_queue.output.FetchAnother();
597 }
598
599 virtual void Write() override {
600 auto &queue = ::frc971::control_loops::claw_queue.output;
601 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800602 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800603 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
604 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800605 }
606
607 virtual void Stop() override {
608 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800609 left_intake_talon_->Disable();
610 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800611 wrist_talon_->Disable();
612 }
613
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800614 ::std::unique_ptr<Talon> left_intake_talon_;
615 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800616 ::std::unique_ptr<Talon> wrist_talon_;
617};
618
Brian Silverman1f90d672015-01-26 20:20:45 -0500619// TODO(brian): Replace this with ::std::make_unique once all our toolchains
620// have support.
621template <class T, class... U>
622std::unique_ptr<T> make_unique(U &&... u) {
623 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
624}
625
Austin Schuh010eb812014-10-25 18:06:49 -0700626class WPILibRobot : public RobotBase {
627 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800628 ::std::unique_ptr<Encoder> encoder(int index) {
629 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
630 Encoder::k4X);
631 }
Austin Schuh010eb812014-10-25 18:06:49 -0700632 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500633 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800634 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500635
Brian Silverman98f6ee22015-01-26 17:50:12 -0500636 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700637 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800638 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500639
Brian Silverman98f6ee22015-01-26 17:50:12 -0500640 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800641 LOG(INFO, "Creating the reader\n");
642 reader.set_arm_left_encoder(encoder(1));
643 reader.set_arm_left_index(make_unique<DigitalInput>(1));
644 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
645
646 reader.set_arm_right_encoder(encoder(5));
647 reader.set_arm_right_index(make_unique<DigitalInput>(5));
648 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
649
650 reader.set_elevator_left_encoder(encoder(0));
651 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
652 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
653
654 reader.set_elevator_right_encoder(encoder(4));
655 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
656 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
657
658 reader.set_wrist_encoder(encoder(6));
659 reader.set_wrist_index(make_unique<DigitalInput>(6));
660 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
661
662 reader.set_left_encoder(encoder(2));
663 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500664 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500665 ::std::thread reader_thread(::std::ref(reader));
666 GyroSender gyro_sender;
667 ::std::thread gyro_thread(::std::ref(gyro_sender));
668
669 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500670 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800671 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500672 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800673 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500674 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
675
Austin Schuhbb227f82015-09-06 15:27:52 -0700676 CanWriter can_writer;
677 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
678 ::std::thread can_writer_thread(::std::ref(can_writer));
679
Daniel Pettiadf38432015-01-26 17:13:35 -0800680 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
681 // claw.
682 FridgeWriter fridge_writer;
683 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800684 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800685 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800686 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800687 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800688 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800689 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800690 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800691 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
692
693 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800694 claw_writer.set_left_intake_talon(
695 ::std::unique_ptr<Talon>(new Talon(5)));
696 claw_writer.set_right_intake_talon(
697 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800698 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800699 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800700 ::std::thread claw_writer_thread(::std::ref(claw_writer));
701
702 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
703 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500704 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800705 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
706 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800707 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800708 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
709 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700710 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
711 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800712
713 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
714 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500715 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
716
717 // Wait forever. Not much else to do...
718 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
719
Austin Schuh010eb812014-10-25 18:06:49 -0700720 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800721
Austin Schuh010eb812014-10-25 18:06:49 -0700722 joystick_sender.Quit();
723 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500724 reader.Quit();
725 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800726 gyro_sender.Quit();
727 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500728
729 drivetrain_writer.Quit();
730 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700731 can_writer.Quit();
732 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500733 solenoid_writer.Quit();
734 solenoid_thread.join();
735
Austin Schuh010eb812014-10-25 18:06:49 -0700736 ::aos::Cleanup();
737 }
738};
739
Brian Silverman98f6ee22015-01-26 17:50:12 -0500740} // namespace wpilib
741} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800742
Brian Silverman98f6ee22015-01-26 17:50:12 -0500743
744START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);