blob: 84b921ad2e6f1dda08d19e9b5ec1f6c1e9b881a2 [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()() {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800231 LOG(INFO, "In sensor reader thread\n");
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 Silvermanc7e8fdd2015-12-06 02:48:27 -0500241 pdp_.reset(new PowerDistributionPanel());
Brian Silverman699f0cb2015-02-05 19:45:01 -0500242
Brian Silverman335c20e2015-01-26 21:47:58 -0500243 wrist_encoder_.Start();
244 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800245 LOG(INFO, "Things are now started\n");
Austin Schuh010eb812014-10-25 18:06:49 -0700246
Brian Silverman2fe007c2014-12-28 12:20:01 -0800247 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700248 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800249 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700250 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700251 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500252
253 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700254 }
255
256 void RunIteration() {
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500257 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
Austin Schuh010eb812014-10-25 18:06:49 -0700258
Austin Schuh35d06612015-02-15 23:35:23 -0800259 {
260 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
261 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800262 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800263 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800264 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800265 drivetrain_message->left_speed =
266 drivetrain_velocity_translate(left_encoder_->GetPeriod());
267 drivetrain_message->right_speed =
268 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Austin Schuh35d06612015-02-15 23:35:23 -0800269
270 drivetrain_message.Send();
271 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500272
273 dma_synchronizer_->RunIteration();
274
Philipp Schrader82c65072015-02-16 00:47:09 +0000275 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500276
Brian Silverman335c20e2015-01-26 21:47:58 -0500277 {
278 auto fridge_message = fridge_queue.position.MakeMessage();
279 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500280 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800281 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500282 CopyPotAndIndexPosition(
283 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
284 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800285 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500286 CopyPotAndIndexPosition(
287 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500288 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800289 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500290 CopyPotAndIndexPosition(
291 elevator_right_encoder_, &fridge_message->elevator.right,
292 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800293 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500294 fridge_message.Send();
295 }
296
297 {
298 auto claw_message = claw_queue.position.MakeMessage();
299 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500300 claw_translate, claw_potentiometer_translate,
301 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500302 claw_message.Send();
303 }
Austin Schuh010eb812014-10-25 18:06:49 -0700304 }
305
306 void Quit() { run_ = false; }
307
308 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500309 static const int kPriority = 30;
310 static const int kInterruptPriority = 55;
311
Brian Silverman699f0cb2015-02-05 19:45:01 -0500312 int32_t my_pid_;
313 DriverStation *ds_;
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -0500314 ::std::unique_ptr<PowerDistributionPanel> pdp_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500315
Brian Silverman335c20e2015-01-26 21:47:58 -0500316 void CopyPotAndIndexPosition(
317 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
318 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500319 ::std::function<double(double)> potentiometer_translate, bool reverse,
320 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500321 const double multiplier = reverse ? -1.0 : 1.0;
322 position->encoder =
323 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500324 position->pot = multiplier * potentiometer_translate(
325 encoder.polled_potentiometer_voltage()) +
326 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500327 position->latched_encoder =
328 multiplier * encoder_translate(encoder.last_encoder_value());
329 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500330 multiplier *
331 potentiometer_translate(encoder.last_potentiometer_voltage()) +
332 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500333 position->index_pulses = encoder.index_posedge_count();
334 }
335
336 void CopyPotAndIndexPosition(
337 const InterruptEncoderAndPotentiometer &encoder,
338 PotAndIndexPosition *position,
339 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500340 ::std::function<double(double)> potentiometer_translate, bool reverse,
341 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500342 const double multiplier = reverse ? -1.0 : 1.0;
343 position->encoder =
344 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500345 position->pot = multiplier * potentiometer_translate(
346 encoder.potentiometer()->GetVoltage()) +
347 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500348 position->latched_encoder =
349 multiplier * encoder_translate(encoder.last_encoder_value());
350 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500351 multiplier *
352 potentiometer_translate(encoder.last_potentiometer_voltage()) +
353 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500354 position->index_pulses = encoder.index_posedge_count();
355 }
356
Brian Silverman335c20e2015-01-26 21:47:58 -0500357 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
358
359 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
360 elevator_left_encoder_, elevator_right_encoder_;
361
362 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
363
Austin Schuh010eb812014-10-25 18:06:49 -0700364 ::std::unique_ptr<Encoder> left_encoder_;
365 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700366
Brian Silverman1f90d672015-01-26 20:20:45 -0500367 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700368 DigitalGlitchFilter filter_;
369};
370
Brian Silvermand8f403a2014-12-13 19:12:04 -0500371class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700372 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500373 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800374 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800375 fridge_(".frc971.control_loops.fridge_queue.output"),
376 claw_(".frc971.control_loops.claw_queue.output") {}
377
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700378 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800379 pressure_switch_ = ::std::move(pressure_switch);
380 }
381
382 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
383 compressor_relay_ = ::std::move(compressor_relay);
384 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500385
Daniel Pettiadf38432015-01-26 17:13:35 -0800386 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
387 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700388 }
389
Daniel Pettiadf38432015-01-26 17:13:35 -0800390 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
391 fridge_grabbers_top_back_ = ::std::move(s);
392 }
393
394 void set_fridge_grabbers_bottom_front(
395 ::std::unique_ptr<BufferedSolenoid> s) {
396 fridge_grabbers_bottom_front_ = ::std::move(s);
397 }
398
399 void set_fridge_grabbers_bottom_back(
400 ::std::unique_ptr<BufferedSolenoid> s) {
401 fridge_grabbers_bottom_back_ = ::std::move(s);
402 }
403
404 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
405 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500406 }
Austin Schuh010eb812014-10-25 18:06:49 -0700407
Brian Silverman93936f72015-03-19 23:38:30 -0700408 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
409 grabber_latch_release_ = ::std::move(s);
410 }
411
412 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
413 grabber_fold_up_ = ::std::move(s);
414 }
415
Brian Silvermand8f403a2014-12-13 19:12:04 -0500416 void operator()() {
417 ::aos::SetCurrentThreadName("Solenoids");
418 ::aos::SetCurrentThreadRealtimePriority(30);
419
420 while (run_) {
421 ::aos::time::PhasedLoopXMS(20, 1000);
422
423 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800424 fridge_.FetchLatest();
425 if (fridge_.get()) {
426 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800427 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
428 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
429 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
430 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800431 }
432 }
433
434 {
435 claw_.FetchLatest();
436 if (claw_.get()) {
437 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800438 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500439 }
440 }
441
Brian Silverman93936f72015-03-19 23:38:30 -0700442 ::aos::joystick_state.FetchLatest();
443 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
444 ::aos::joystick_state->autonomous);
445 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
446 ::aos::joystick_state->joysticks[1].buttons & 1);
447
Brian Silverman87541532015-03-19 23:35:12 -0700448 {
449 PneumaticsToLog to_log;
450 {
451 const bool compressor_on = !pressure_switch_->Get();
452 to_log.compressor_on = compressor_on;
453 if (compressor_on) {
454 compressor_relay_->Set(Relay::kForward);
455 } else {
456 compressor_relay_->Set(Relay::kOff);
457 }
458 }
459
460 pcm_->Flush();
461 to_log.read_solenoids = pcm_->GetAll();
462 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
463 }
Austin Schuh010eb812014-10-25 18:06:49 -0700464 }
465 }
466
Brian Silvermand8f403a2014-12-13 19:12:04 -0500467 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700468
Brian Silvermand8f403a2014-12-13 19:12:04 -0500469 private:
470 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800471 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
472 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
473 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
474 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
475 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700476 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
477 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700478 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800479 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700480
Daniel Pettiadf38432015-01-26 17:13:35 -0800481 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
482 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700483
Brian Silvermand8f403a2014-12-13 19:12:04 -0500484 ::std::atomic<bool> run_{true};
485};
486
Austin Schuhbb227f82015-09-06 15:27:52 -0700487class CanWriter : public LoopOutputHandler {
488 public:
489 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
490
491 void set_can_talon(::std::unique_ptr<Talon> t) {
492 can_talon_ = ::std::move(t);
493 }
494
495 private:
496 virtual void Read() override {
497 ::frc971::autonomous::can_control.FetchAnother();
498 }
499
500 virtual void Write() override {
501 auto &queue = ::frc971::autonomous::can_control;
502 LOG_STRUCT(DEBUG, "will output", *queue);
503 can_talon_->Set(queue->can_voltage / 12.0);
504 }
505
506 virtual void Stop() override {
507 LOG(WARNING, "Can output too old\n");
508 can_talon_->Disable();
509 }
510
511 ::std::unique_ptr<Talon> can_talon_;
512};
513
Brian Silvermand8f403a2014-12-13 19:12:04 -0500514class DrivetrainWriter : public LoopOutputHandler {
515 public:
516 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
517 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700518 }
519
Brian Silvermand8f403a2014-12-13 19:12:04 -0500520 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
521 right_drivetrain_talon_ = ::std::move(t);
522 }
Austin Schuh010eb812014-10-25 18:06:49 -0700523
Brian Silvermand8f403a2014-12-13 19:12:04 -0500524 private:
525 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500526 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500527 }
528
529 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500530 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500531 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800532 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
533 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500534 }
535
536 virtual void Stop() override {
537 LOG(WARNING, "drivetrain output too old\n");
538 left_drivetrain_talon_->Disable();
539 right_drivetrain_talon_->Disable();
540 }
541
Austin Schuh010eb812014-10-25 18:06:49 -0700542 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500543 ::std::unique_ptr<Talon> right_drivetrain_talon_;
544};
545
Daniel Pettiadf38432015-01-26 17:13:35 -0800546class FridgeWriter : public LoopOutputHandler {
547 public:
548 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
549 left_arm_talon_ = ::std::move(t);
550 }
551
552 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
553 right_arm_talon_ = ::std::move(t);
554 }
555
556 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
557 left_elevator_talon_ = ::std::move(t);
558 }
559
560 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
561 right_elevator_talon_ = ::std::move(t);
562 }
563
564 private:
565 virtual void Read() override {
566 ::frc971::control_loops::fridge_queue.output.FetchAnother();
567 }
568
569 virtual void Write() override {
570 auto &queue = ::frc971::control_loops::fridge_queue.output;
571 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800572 left_arm_talon_->Set(queue->left_arm / 12.0);
573 right_arm_talon_->Set(-queue->right_arm / 12.0);
574 left_elevator_talon_->Set(queue->left_elevator / 12.0);
575 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800576 }
577
578 virtual void Stop() override {
579 LOG(WARNING, "Fridge output too old.\n");
580 left_arm_talon_->Disable();
581 right_arm_talon_->Disable();
582 left_elevator_talon_->Disable();
583 right_elevator_talon_->Disable();
584 }
585
586 ::std::unique_ptr<Talon> left_arm_talon_;
587 ::std::unique_ptr<Talon> right_arm_talon_;
588 ::std::unique_ptr<Talon> left_elevator_talon_;
589 ::std::unique_ptr<Talon> right_elevator_talon_;
590};
591
592class ClawWriter : public LoopOutputHandler {
593 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800594 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
595 left_intake_talon_ = ::std::move(t);
596 }
597
598 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
599 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800600 }
601
602 void set_wrist_talon(::std::unique_ptr<Talon> t) {
603 wrist_talon_ = ::std::move(t);
604 }
605
606 private:
607 virtual void Read() override {
608 ::frc971::control_loops::claw_queue.output.FetchAnother();
609 }
610
611 virtual void Write() override {
612 auto &queue = ::frc971::control_loops::claw_queue.output;
613 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800614 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800615 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
616 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800617 }
618
619 virtual void Stop() override {
620 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800621 left_intake_talon_->Disable();
622 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800623 wrist_talon_->Disable();
624 }
625
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800626 ::std::unique_ptr<Talon> left_intake_talon_;
627 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800628 ::std::unique_ptr<Talon> wrist_talon_;
629};
630
Brian Silverman1f90d672015-01-26 20:20:45 -0500631// TODO(brian): Replace this with ::std::make_unique once all our toolchains
632// have support.
633template <class T, class... U>
634std::unique_ptr<T> make_unique(U &&... u) {
635 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
636}
637
Austin Schuh010eb812014-10-25 18:06:49 -0700638class WPILibRobot : public RobotBase {
639 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800640 ::std::unique_ptr<Encoder> encoder(int index) {
641 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
642 Encoder::k4X);
643 }
Austin Schuh010eb812014-10-25 18:06:49 -0700644 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500645 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800646 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500647
Brian Silverman98f6ee22015-01-26 17:50:12 -0500648 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700649 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800650 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500651
Brian Silverman98f6ee22015-01-26 17:50:12 -0500652 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800653 LOG(INFO, "Creating the reader\n");
654 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 Silvermand8f403a2014-12-13 19:12:04 -0500736 reader.Quit();
737 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800738 gyro_sender.Quit();
739 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500740
741 drivetrain_writer.Quit();
742 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700743 can_writer.Quit();
744 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500745 solenoid_writer.Quit();
746 solenoid_thread.join();
747
Austin Schuh010eb812014-10-25 18:06:49 -0700748 ::aos::Cleanup();
749 }
750};
751
Brian Silverman98f6ee22015-01-26 17:50:12 -0500752} // namespace wpilib
753} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800754
Brian Silverman98f6ee22015-01-26 17:50:12 -0500755
756START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);