blob: 14ca1466b18dd9e42d3853344ab19ad40ca7a9d5 [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
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Brian Silvermand8f403a2014-12-13 19:12:04 -05007#include <functional>
Austin Schuh8aec1ed2016-05-01 13:29:20 -07008#include <mutex>
9#include <thread>
Brian Silvermand8f403a2014-12-13 19:12:04 -050010
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070011#include "Encoder.h"
12#include "Talon.h"
13#include "DriverStation.h"
14#include "AnalogInput.h"
15#include "Compressor.h"
16#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080017#include "frc971/wpilib/wpilib_robot_base.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 Silvermanb5b46ca2016-03-13 01:14:17 -050052#include "frc971/wpilib/dma.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080053
Austin Schuh010eb812014-10-25 18:06:49 -070054#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
58using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050059using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050060using ::frc971::control_loops::fridge_queue;
61using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070062
63namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080064namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070065
Austin Schuh010eb812014-10-25 18:06:49 -070066double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080067 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080068 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080069 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080070 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
71}
72
Brian Silverman51091a02015-12-26 15:56:58 -080073double drivetrain_velocity_translate(double in) {
74 return (1.0 / in) / 256.0 /*cpr*/ *
75 constants::GetValues().drivetrain_encoder_ratio *
76 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
77}
78
Daniel Pettiadf38432015-01-26 17:13:35 -080079double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080080 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080081 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080082 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080083 (2 * M_PI /*radians*/);
84}
85
Brian Silverman5d712fc2015-02-15 03:39:31 -050086double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080087 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080088 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080089 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080090 (2 * M_PI /*radians*/);
91}
92
93double elevator_translate(int32_t in) {
94 return static_cast<double>(in) /
95 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080096 constants::GetValues().elev_encoder_ratio *
97 (2 * M_PI /*radians*/) *
98 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080099}
100
Brian Silverman5d712fc2015-02-15 03:39:31 -0500101double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800102 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800103 constants::GetValues().elev_pot_ratio *
104 (2 * M_PI /*radians*/) *
105 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -0800106 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -0800107}
108
109double claw_translate(int32_t in) {
110 return static_cast<double>(in) /
111 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800112 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800113 (2 * M_PI /*radians*/);
114}
115
Brian Silverman5d712fc2015-02-15 03:39:31 -0500116double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800117 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800118 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800119 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800120 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700121}
122
Brian Silverman335c20e2015-01-26 21:47:58 -0500123static const double kMaximumEncoderPulsesPerSecond =
124 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
125 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
126 4.0 /* index pulse = 1/4 cycle */;
127
Austin Schuh010eb812014-10-25 18:06:49 -0700128class SensorReader {
129 public:
Brian Silverman39b339e2016-01-03 13:24:22 -0800130 SensorReader() {
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
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700245 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
246 ::std::chrono::milliseconds(4));
Brian Silverman5090c432016-01-02 14:44:26 -0800247
248 ::aos::SetCurrentThreadRealtimePriority(40);
Austin Schuh010eb812014-10-25 18:06:49 -0700249 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800250 {
251 const int iterations = phased_loop.SleepUntilNext();
252 if (iterations != 1) {
253 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
254 }
255 }
Austin Schuh010eb812014-10-25 18:06:49 -0700256 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700257 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500258
259 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700260 }
261
262 void RunIteration() {
Brian Silverman39b339e2016-01-03 13:24:22 -0800263 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
Austin Schuh010eb812014-10-25 18:06:49 -0700264
Austin Schuh35d06612015-02-15 23:35:23 -0800265 {
266 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
267 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800268 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800269 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800270 drivetrain_translate(left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800271 drivetrain_message->left_speed =
272 drivetrain_velocity_translate(left_encoder_->GetPeriod());
273 drivetrain_message->right_speed =
274 drivetrain_velocity_translate(right_encoder_->GetPeriod());
Austin Schuh35d06612015-02-15 23:35:23 -0800275
276 drivetrain_message.Send();
277 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500278
279 dma_synchronizer_->RunIteration();
280
Philipp Schrader82c65072015-02-16 00:47:09 +0000281 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500282
Brian Silverman335c20e2015-01-26 21:47:58 -0500283 {
284 auto fridge_message = fridge_queue.position.MakeMessage();
285 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500286 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800287 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500288 CopyPotAndIndexPosition(
289 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
290 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800291 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500292 CopyPotAndIndexPosition(
293 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500294 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800295 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500296 CopyPotAndIndexPosition(
297 elevator_right_encoder_, &fridge_message->elevator.right,
298 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800299 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500300 fridge_message.Send();
301 }
302
303 {
304 auto claw_message = claw_queue.position.MakeMessage();
305 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500306 claw_translate, claw_potentiometer_translate,
307 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500308 claw_message.Send();
309 }
Austin Schuh010eb812014-10-25 18:06:49 -0700310 }
311
312 void Quit() { run_ = false; }
313
314 private:
Brian Silverman699f0cb2015-02-05 19:45:01 -0500315 int32_t my_pid_;
316 DriverStation *ds_;
317
Brian Silverman335c20e2015-01-26 21:47:58 -0500318 void CopyPotAndIndexPosition(
319 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
320 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500321 ::std::function<double(double)> potentiometer_translate, bool reverse,
322 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500323 const double multiplier = reverse ? -1.0 : 1.0;
324 position->encoder =
325 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500326 position->pot = multiplier * potentiometer_translate(
327 encoder.polled_potentiometer_voltage()) +
328 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500329 position->latched_encoder =
330 multiplier * encoder_translate(encoder.last_encoder_value());
331 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500332 multiplier *
333 potentiometer_translate(encoder.last_potentiometer_voltage()) +
334 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500335 position->index_pulses = encoder.index_posedge_count();
336 }
337
338 void CopyPotAndIndexPosition(
339 const InterruptEncoderAndPotentiometer &encoder,
340 PotAndIndexPosition *position,
341 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500342 ::std::function<double(double)> potentiometer_translate, bool reverse,
343 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500344 const double multiplier = reverse ? -1.0 : 1.0;
345 position->encoder =
346 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500347 position->pot = multiplier * potentiometer_translate(
348 encoder.potentiometer()->GetVoltage()) +
349 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500350 position->latched_encoder =
351 multiplier * encoder_translate(encoder.last_encoder_value());
352 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500353 multiplier *
354 potentiometer_translate(encoder.last_potentiometer_voltage()) +
355 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500356 position->index_pulses = encoder.index_posedge_count();
357 }
358
Brian Silverman335c20e2015-01-26 21:47:58 -0500359 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
360
361 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
362 elevator_left_encoder_, elevator_right_encoder_;
363
Brian Silverman5090c432016-01-02 14:44:26 -0800364 InterruptEncoderAndPotentiometer wrist_encoder_{55};
Brian Silverman335c20e2015-01-26 21:47:58 -0500365
Austin Schuh010eb812014-10-25 18:06:49 -0700366 ::std::unique_ptr<Encoder> left_encoder_;
367 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700368
Brian Silverman1f90d672015-01-26 20:20:45 -0500369 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700370 DigitalGlitchFilter filter_;
371};
372
Brian Silvermand8f403a2014-12-13 19:12:04 -0500373class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700374 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500375 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800376 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800377 fridge_(".frc971.control_loops.fridge_queue.output"),
378 claw_(".frc971.control_loops.claw_queue.output") {}
379
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700380 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800381 pressure_switch_ = ::std::move(pressure_switch);
382 }
383
384 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
385 compressor_relay_ = ::std::move(compressor_relay);
386 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500387
Daniel Pettiadf38432015-01-26 17:13:35 -0800388 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
389 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700390 }
391
Daniel Pettiadf38432015-01-26 17:13:35 -0800392 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
393 fridge_grabbers_top_back_ = ::std::move(s);
394 }
395
396 void set_fridge_grabbers_bottom_front(
397 ::std::unique_ptr<BufferedSolenoid> s) {
398 fridge_grabbers_bottom_front_ = ::std::move(s);
399 }
400
401 void set_fridge_grabbers_bottom_back(
402 ::std::unique_ptr<BufferedSolenoid> s) {
403 fridge_grabbers_bottom_back_ = ::std::move(s);
404 }
405
406 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
407 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500408 }
Austin Schuh010eb812014-10-25 18:06:49 -0700409
Brian Silverman93936f72015-03-19 23:38:30 -0700410 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
411 grabber_latch_release_ = ::std::move(s);
412 }
413
414 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
415 grabber_fold_up_ = ::std::move(s);
416 }
417
Brian Silvermand8f403a2014-12-13 19:12:04 -0500418 void operator()() {
419 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800420 ::aos::SetCurrentThreadRealtimePriority(27);
421
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700422 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
423 ::std::chrono::milliseconds(1));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500424
425 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800426 {
427 const int iterations = phased_loop.SleepUntilNext();
428 if (iterations != 1) {
429 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
430 }
431 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500432
433 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800434 fridge_.FetchLatest();
435 if (fridge_.get()) {
436 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800437 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
438 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
439 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
440 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800441 }
442 }
443
444 {
445 claw_.FetchLatest();
446 if (claw_.get()) {
447 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800448 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500449 }
450 }
451
Brian Silverman93936f72015-03-19 23:38:30 -0700452 ::aos::joystick_state.FetchLatest();
453 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
454 ::aos::joystick_state->autonomous);
455 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
456 ::aos::joystick_state->joysticks[1].buttons & 1);
457
Brian Silverman87541532015-03-19 23:35:12 -0700458 {
459 PneumaticsToLog to_log;
460 {
461 const bool compressor_on = !pressure_switch_->Get();
462 to_log.compressor_on = compressor_on;
463 if (compressor_on) {
464 compressor_relay_->Set(Relay::kForward);
465 } else {
466 compressor_relay_->Set(Relay::kOff);
467 }
468 }
469
470 pcm_->Flush();
471 to_log.read_solenoids = pcm_->GetAll();
472 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
473 }
Austin Schuh010eb812014-10-25 18:06:49 -0700474 }
475 }
476
Brian Silvermand8f403a2014-12-13 19:12:04 -0500477 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700478
Brian Silvermand8f403a2014-12-13 19:12:04 -0500479 private:
480 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800481 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
482 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
483 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
484 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
485 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700486 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
487 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700488 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800489 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700490
Daniel Pettiadf38432015-01-26 17:13:35 -0800491 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
492 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700493
Brian Silvermand8f403a2014-12-13 19:12:04 -0500494 ::std::atomic<bool> run_{true};
495};
496
Austin Schuhbb227f82015-09-06 15:27:52 -0700497class CanWriter : public LoopOutputHandler {
498 public:
499 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
500
501 void set_can_talon(::std::unique_ptr<Talon> t) {
502 can_talon_ = ::std::move(t);
503 }
504
505 private:
506 virtual void Read() override {
507 ::frc971::autonomous::can_control.FetchAnother();
508 }
509
510 virtual void Write() override {
511 auto &queue = ::frc971::autonomous::can_control;
512 LOG_STRUCT(DEBUG, "will output", *queue);
513 can_talon_->Set(queue->can_voltage / 12.0);
514 }
515
516 virtual void Stop() override {
517 LOG(WARNING, "Can output too old\n");
518 can_talon_->Disable();
519 }
520
521 ::std::unique_ptr<Talon> can_talon_;
522};
523
Brian Silvermand8f403a2014-12-13 19:12:04 -0500524class DrivetrainWriter : public LoopOutputHandler {
525 public:
526 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
527 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700528 }
529
Brian Silvermand8f403a2014-12-13 19:12:04 -0500530 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
531 right_drivetrain_talon_ = ::std::move(t);
532 }
Austin Schuh010eb812014-10-25 18:06:49 -0700533
Brian Silvermand8f403a2014-12-13 19:12:04 -0500534 private:
535 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500536 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500537 }
538
539 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500540 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500541 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800542 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
543 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500544 }
545
546 virtual void Stop() override {
547 LOG(WARNING, "drivetrain output too old\n");
548 left_drivetrain_talon_->Disable();
549 right_drivetrain_talon_->Disable();
550 }
551
Austin Schuh010eb812014-10-25 18:06:49 -0700552 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500553 ::std::unique_ptr<Talon> right_drivetrain_talon_;
554};
555
Daniel Pettiadf38432015-01-26 17:13:35 -0800556class FridgeWriter : public LoopOutputHandler {
557 public:
558 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
559 left_arm_talon_ = ::std::move(t);
560 }
561
562 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
563 right_arm_talon_ = ::std::move(t);
564 }
565
566 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
567 left_elevator_talon_ = ::std::move(t);
568 }
569
570 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
571 right_elevator_talon_ = ::std::move(t);
572 }
573
574 private:
575 virtual void Read() override {
576 ::frc971::control_loops::fridge_queue.output.FetchAnother();
577 }
578
579 virtual void Write() override {
580 auto &queue = ::frc971::control_loops::fridge_queue.output;
581 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800582 left_arm_talon_->Set(queue->left_arm / 12.0);
583 right_arm_talon_->Set(-queue->right_arm / 12.0);
584 left_elevator_talon_->Set(queue->left_elevator / 12.0);
585 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800586 }
587
588 virtual void Stop() override {
589 LOG(WARNING, "Fridge output too old.\n");
590 left_arm_talon_->Disable();
591 right_arm_talon_->Disable();
592 left_elevator_talon_->Disable();
593 right_elevator_talon_->Disable();
594 }
595
596 ::std::unique_ptr<Talon> left_arm_talon_;
597 ::std::unique_ptr<Talon> right_arm_talon_;
598 ::std::unique_ptr<Talon> left_elevator_talon_;
599 ::std::unique_ptr<Talon> right_elevator_talon_;
600};
601
602class ClawWriter : public LoopOutputHandler {
603 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800604 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
605 left_intake_talon_ = ::std::move(t);
606 }
607
608 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
609 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800610 }
611
612 void set_wrist_talon(::std::unique_ptr<Talon> t) {
613 wrist_talon_ = ::std::move(t);
614 }
615
616 private:
617 virtual void Read() override {
618 ::frc971::control_loops::claw_queue.output.FetchAnother();
619 }
620
621 virtual void Write() override {
622 auto &queue = ::frc971::control_loops::claw_queue.output;
623 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800624 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800625 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
626 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800627 }
628
629 virtual void Stop() override {
630 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800631 left_intake_talon_->Disable();
632 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800633 wrist_talon_->Disable();
634 }
635
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800636 ::std::unique_ptr<Talon> left_intake_talon_;
637 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800638 ::std::unique_ptr<Talon> wrist_talon_;
639};
640
Brian Silverman1f90d672015-01-26 20:20:45 -0500641// TODO(brian): Replace this with ::std::make_unique once all our toolchains
642// have support.
643template <class T, class... U>
644std::unique_ptr<T> make_unique(U &&... u) {
645 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
646}
647
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800648class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Austin Schuh010eb812014-10-25 18:06:49 -0700649 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800650 ::std::unique_ptr<Encoder> encoder(int index) {
651 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
652 Encoder::k4X);
653 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800654 virtual void Run() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500655 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800656 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500657
Brian Silverman98f6ee22015-01-26 17:50:12 -0500658 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700659 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800660 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500661
Brian Silverman425492b2015-12-30 15:23:55 -0800662 ::frc971::wpilib::PDPFetcher pdp_fetcher;
663 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
664
Brian Silverman39b339e2016-01-03 13:24:22 -0800665 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800666 reader.set_arm_left_encoder(encoder(1));
667 reader.set_arm_left_index(make_unique<DigitalInput>(1));
668 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
669
670 reader.set_arm_right_encoder(encoder(5));
671 reader.set_arm_right_index(make_unique<DigitalInput>(5));
672 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
673
674 reader.set_elevator_left_encoder(encoder(0));
675 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
676 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
677
678 reader.set_elevator_right_encoder(encoder(4));
679 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
680 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
681
682 reader.set_wrist_encoder(encoder(6));
683 reader.set_wrist_index(make_unique<DigitalInput>(6));
684 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
685
686 reader.set_left_encoder(encoder(2));
687 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500688 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500689 ::std::thread reader_thread(::std::ref(reader));
690 GyroSender gyro_sender;
691 ::std::thread gyro_thread(::std::ref(gyro_sender));
692
693 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500694 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800695 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500696 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800697 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500698 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
699
Austin Schuhbb227f82015-09-06 15:27:52 -0700700 CanWriter can_writer;
701 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
702 ::std::thread can_writer_thread(::std::ref(can_writer));
703
Daniel Pettiadf38432015-01-26 17:13:35 -0800704 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
705 // claw.
706 FridgeWriter fridge_writer;
707 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800708 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800709 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800710 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800711 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800712 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800713 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800714 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800715 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
716
717 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800718 claw_writer.set_left_intake_talon(
719 ::std::unique_ptr<Talon>(new Talon(5)));
720 claw_writer.set_right_intake_talon(
721 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800722 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800723 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800724 ::std::thread claw_writer_thread(::std::ref(claw_writer));
725
726 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
727 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500728 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800729 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
730 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800731 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800732 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
733 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700734 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
735 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800736
737 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
738 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500739 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
740
741 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800742 while (true) {
743 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
744 if (r != 0) {
745 PLOG(WARNING, "infinite select failed");
746 } else {
747 PLOG(WARNING, "infinite select succeeded??\n");
748 }
749 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500750
Austin Schuh010eb812014-10-25 18:06:49 -0700751 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800752
Austin Schuh010eb812014-10-25 18:06:49 -0700753 joystick_sender.Quit();
754 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800755 pdp_fetcher.Quit();
756 pdp_fetcher_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500757 reader.Quit();
758 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800759 gyro_sender.Quit();
760 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500761
762 drivetrain_writer.Quit();
763 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700764 can_writer.Quit();
765 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500766 solenoid_writer.Quit();
767 solenoid_thread.join();
768
Austin Schuh010eb812014-10-25 18:06:49 -0700769 ::aos::Cleanup();
770 }
771};
772
Brian Silverman98f6ee22015-01-26 17:50:12 -0500773} // namespace wpilib
774} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800775
Brian Silverman98f6ee22015-01-26 17:50:12 -0500776
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800777AOS_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);