blob: e70c92fca140ec16c67469d22855dcd6f2710445 [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"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080016#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070017#include "dma.h"
Austin Schuh6d5d9ae2015-10-31 19:39:57 -070018#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#undef ERROR
22
Austin Schuh010eb812014-10-25 18:06:49 -070023#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
Austin Schuh010eb812014-10-25 18:06:49 -070025#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040029#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070030#include "aos/linux_code/init.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050031#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070032
Brian Silverman335c20e2015-01-26 21:47:58 -050033#include "frc971/control_loops/control_loops.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050034
35#include "y2015/constants.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070036#include "y2015/control_loops/drivetrain/drivetrain.q.h"
37#include "y2015/control_loops/fridge/fridge.q.h"
38#include "y2015/control_loops/claw/claw.q.h"
Austin Schuhbb227f82015-09-06 15:27:52 -070039#include "y2015/autonomous/auto.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070040
Brian Silvermanda45b6c2014-12-28 11:36:50 -080041#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050042#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080045#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050046#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050047#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050048#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silverman87541532015-03-19 23:35:12 -070049#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050050#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman425492b2015-12-30 15:23:55 -080051#include "frc971/wpilib/pdp_fetcher.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080052
Austin Schuh010eb812014-10-25 18:06:49 -070053#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050058using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050059using ::frc971::control_loops::fridge_queue;
60using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070061
62namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080063namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070064
Austin Schuh010eb812014-10-25 18:06:49 -070065double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080066 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080067 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080068 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080069 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
70}
71
Brian Silverman51091a02015-12-26 15:56:58 -080072double drivetrain_velocity_translate(double in) {
73 return (1.0 / in) / 256.0 /*cpr*/ *
74 constants::GetValues().drivetrain_encoder_ratio *
75 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
76}
77
Daniel Pettiadf38432015-01-26 17:13:35 -080078double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080079 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080080 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080081 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080082 (2 * M_PI /*radians*/);
83}
84
Brian Silverman5d712fc2015-02-15 03:39:31 -050085double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080086 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080087 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080088 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080089 (2 * M_PI /*radians*/);
90}
91
92double elevator_translate(int32_t in) {
93 return static_cast<double>(in) /
94 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080095 constants::GetValues().elev_encoder_ratio *
96 (2 * M_PI /*radians*/) *
97 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080098}
99
Brian Silverman5d712fc2015-02-15 03:39:31 -0500100double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800101 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800102 constants::GetValues().elev_pot_ratio *
103 (2 * M_PI /*radians*/) *
104 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -0800105 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -0800106}
107
108double claw_translate(int32_t in) {
109 return static_cast<double>(in) /
110 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800111 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800112 (2 * M_PI /*radians*/);
113}
114
Brian Silverman5d712fc2015-02-15 03:39:31 -0500115double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800116 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800117 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800118 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800119 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700120}
121
Brian Silverman335c20e2015-01-26 21:47:58 -0500122static const double kMaximumEncoderPulsesPerSecond =
123 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
124 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
125 4.0 /* index pulse = 1/4 cycle */;
126
Austin Schuh010eb812014-10-25 18:06:49 -0700127class SensorReader {
128 public:
Brian Silverman425492b2015-12-30 15:23:55 -0800129 SensorReader(::frc971::wpilib::PDPFetcher *pdp_fetcher)
130 : pdp_fetcher_(pdp_fetcher) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500131 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
132 // we should ever see.
133 filter_.SetPeriodNanoSeconds(
134 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
135 }
136
137 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
138 filter_.Add(encoder.get());
139 arm_left_encoder_.set_encoder(::std::move(encoder));
140 }
141
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700142 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500143 filter_.Add(index.get());
144 arm_left_encoder_.set_index(::std::move(index));
145 }
146
147 void set_arm_left_potentiometer(
148 ::std::unique_ptr<AnalogInput> potentiometer) {
149 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
150 }
151
152 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
153 filter_.Add(encoder.get());
154 arm_right_encoder_.set_encoder(::std::move(encoder));
155 }
156
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700157 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500158 filter_.Add(index.get());
159 arm_right_encoder_.set_index(::std::move(index));
160 }
161
162 void set_arm_right_potentiometer(
163 ::std::unique_ptr<AnalogInput> potentiometer) {
164 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
165 }
166
167 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
168 filter_.Add(encoder.get());
169 elevator_left_encoder_.set_encoder(::std::move(encoder));
170 }
171
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700172 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500173 filter_.Add(index.get());
174 elevator_left_encoder_.set_index(::std::move(index));
175 }
176
177 void set_elevator_left_potentiometer(
178 ::std::unique_ptr<AnalogInput> potentiometer) {
179 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
180 }
181
182 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
183 filter_.Add(encoder.get());
184 elevator_right_encoder_.set_encoder(::std::move(encoder));
185 }
186
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700187 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500188 filter_.Add(index.get());
189 elevator_right_encoder_.set_index(::std::move(index));
190 }
191
192 void set_elevator_right_potentiometer(
193 ::std::unique_ptr<AnalogInput> potentiometer) {
194 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
195 }
196
197 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
198 filter_.Add(encoder.get());
199 wrist_encoder_.set_encoder(::std::move(encoder));
200 }
201
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700202 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500203 filter_.Add(index.get());
204 wrist_encoder_.set_index(::std::move(index));
205 }
206
207 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
208 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700209 }
210
Brian Silverman1f90d672015-01-26 20:20:45 -0500211 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
212 left_encoder_ = ::std::move(left_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800213 left_encoder_->SetMaxPeriod(0.005);
Brian Silverman1f90d672015-01-26 20:20:45 -0500214 }
215
216 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
217 right_encoder_ = ::std::move(right_encoder);
Brian Silverman51091a02015-12-26 15:56:58 -0800218 right_encoder_->SetMaxPeriod(0.005);
Brian Silverman1f90d672015-01-26 20:20:45 -0500219 }
220
Brian Silverman335c20e2015-01-26 21:47:58 -0500221 // All of the DMA-related set_* calls must be made before this, and it doesn't
222 // hurt to do all of them.
223 void set_dma(::std::unique_ptr<DMA> dma) {
224 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
225 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500226 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800227 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500228 dma_synchronizer_->Add(&elevator_right_encoder_);
229 }
230
Austin Schuh010eb812014-10-25 18:06:49 -0700231 void operator()() {
Brian Silverman2fe007c2014-12-28 12:20:01 -0800232 ::aos::SetCurrentThreadName("SensorReader");
233
Brian Silverman699f0cb2015-02-05 19:45:01 -0500234 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700235 ds_ =
236#ifdef WPILIB2015
237 DriverStation::GetInstance();
238#else
239 &DriverStation::GetInstance();
240#endif
Brian Silverman699f0cb2015-02-05 19:45:01 -0500241
Brian Silverman335c20e2015-01-26 21:47:58 -0500242 wrist_encoder_.Start();
243 dma_synchronizer_->Start();
Austin Schuh010eb812014-10-25 18:06:49 -0700244
Brian Silverman5090c432016-01-02 14:44:26 -0800245 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
246 ::aos::time::Time::InMS(4));
247
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 Silverman425492b2015-12-30 15:23:55 -0800263 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_fetcher_);
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_;
Brian Silverman425492b2015-12-30 15:23:55 -0800317 ::frc971::wpilib::PDPFetcher *const pdp_fetcher_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500318
Brian Silverman335c20e2015-01-26 21:47:58 -0500319 void CopyPotAndIndexPosition(
320 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
321 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500322 ::std::function<double(double)> potentiometer_translate, bool reverse,
323 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500324 const double multiplier = reverse ? -1.0 : 1.0;
325 position->encoder =
326 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500327 position->pot = multiplier * potentiometer_translate(
328 encoder.polled_potentiometer_voltage()) +
329 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500330 position->latched_encoder =
331 multiplier * encoder_translate(encoder.last_encoder_value());
332 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500333 multiplier *
334 potentiometer_translate(encoder.last_potentiometer_voltage()) +
335 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500336 position->index_pulses = encoder.index_posedge_count();
337 }
338
339 void CopyPotAndIndexPosition(
340 const InterruptEncoderAndPotentiometer &encoder,
341 PotAndIndexPosition *position,
342 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500343 ::std::function<double(double)> potentiometer_translate, bool reverse,
344 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500345 const double multiplier = reverse ? -1.0 : 1.0;
346 position->encoder =
347 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500348 position->pot = multiplier * potentiometer_translate(
349 encoder.potentiometer()->GetVoltage()) +
350 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500351 position->latched_encoder =
352 multiplier * encoder_translate(encoder.last_encoder_value());
353 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500354 multiplier *
355 potentiometer_translate(encoder.last_potentiometer_voltage()) +
356 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500357 position->index_pulses = encoder.index_posedge_count();
358 }
359
Brian Silverman335c20e2015-01-26 21:47:58 -0500360 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
361
362 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
363 elevator_left_encoder_, elevator_right_encoder_;
364
Brian Silverman5090c432016-01-02 14:44:26 -0800365 InterruptEncoderAndPotentiometer wrist_encoder_{55};
Brian Silverman335c20e2015-01-26 21:47:58 -0500366
Austin Schuh010eb812014-10-25 18:06:49 -0700367 ::std::unique_ptr<Encoder> left_encoder_;
368 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700369
Brian Silverman1f90d672015-01-26 20:20:45 -0500370 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700371 DigitalGlitchFilter filter_;
372};
373
Brian Silvermand8f403a2014-12-13 19:12:04 -0500374class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700375 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500376 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800377 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800378 fridge_(".frc971.control_loops.fridge_queue.output"),
379 claw_(".frc971.control_loops.claw_queue.output") {}
380
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700381 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800382 pressure_switch_ = ::std::move(pressure_switch);
383 }
384
385 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
386 compressor_relay_ = ::std::move(compressor_relay);
387 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500388
Daniel Pettiadf38432015-01-26 17:13:35 -0800389 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
390 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700391 }
392
Daniel Pettiadf38432015-01-26 17:13:35 -0800393 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
394 fridge_grabbers_top_back_ = ::std::move(s);
395 }
396
397 void set_fridge_grabbers_bottom_front(
398 ::std::unique_ptr<BufferedSolenoid> s) {
399 fridge_grabbers_bottom_front_ = ::std::move(s);
400 }
401
402 void set_fridge_grabbers_bottom_back(
403 ::std::unique_ptr<BufferedSolenoid> s) {
404 fridge_grabbers_bottom_back_ = ::std::move(s);
405 }
406
407 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
408 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500409 }
Austin Schuh010eb812014-10-25 18:06:49 -0700410
Brian Silverman93936f72015-03-19 23:38:30 -0700411 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
412 grabber_latch_release_ = ::std::move(s);
413 }
414
415 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
416 grabber_fold_up_ = ::std::move(s);
417 }
418
Brian Silvermand8f403a2014-12-13 19:12:04 -0500419 void operator()() {
420 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800421 ::aos::SetCurrentThreadRealtimePriority(27);
422
423 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
424 ::aos::time::Time::InMS(1));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500425
426 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800427 {
428 const int iterations = phased_loop.SleepUntilNext();
429 if (iterations != 1) {
430 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
431 }
432 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500433
434 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800435 fridge_.FetchLatest();
436 if (fridge_.get()) {
437 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800438 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
439 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
440 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
441 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800442 }
443 }
444
445 {
446 claw_.FetchLatest();
447 if (claw_.get()) {
448 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800449 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500450 }
451 }
452
Brian Silverman93936f72015-03-19 23:38:30 -0700453 ::aos::joystick_state.FetchLatest();
454 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
455 ::aos::joystick_state->autonomous);
456 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
457 ::aos::joystick_state->joysticks[1].buttons & 1);
458
Brian Silverman87541532015-03-19 23:35:12 -0700459 {
460 PneumaticsToLog to_log;
461 {
462 const bool compressor_on = !pressure_switch_->Get();
463 to_log.compressor_on = compressor_on;
464 if (compressor_on) {
465 compressor_relay_->Set(Relay::kForward);
466 } else {
467 compressor_relay_->Set(Relay::kOff);
468 }
469 }
470
471 pcm_->Flush();
472 to_log.read_solenoids = pcm_->GetAll();
473 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
474 }
Austin Schuh010eb812014-10-25 18:06:49 -0700475 }
476 }
477
Brian Silvermand8f403a2014-12-13 19:12:04 -0500478 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700479
Brian Silvermand8f403a2014-12-13 19:12:04 -0500480 private:
481 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800482 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
483 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
484 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
485 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
486 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700487 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
488 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700489 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800490 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700491
Daniel Pettiadf38432015-01-26 17:13:35 -0800492 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
493 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700494
Brian Silvermand8f403a2014-12-13 19:12:04 -0500495 ::std::atomic<bool> run_{true};
496};
497
Austin Schuhbb227f82015-09-06 15:27:52 -0700498class CanWriter : public LoopOutputHandler {
499 public:
500 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
501
502 void set_can_talon(::std::unique_ptr<Talon> t) {
503 can_talon_ = ::std::move(t);
504 }
505
506 private:
507 virtual void Read() override {
508 ::frc971::autonomous::can_control.FetchAnother();
509 }
510
511 virtual void Write() override {
512 auto &queue = ::frc971::autonomous::can_control;
513 LOG_STRUCT(DEBUG, "will output", *queue);
514 can_talon_->Set(queue->can_voltage / 12.0);
515 }
516
517 virtual void Stop() override {
518 LOG(WARNING, "Can output too old\n");
519 can_talon_->Disable();
520 }
521
522 ::std::unique_ptr<Talon> can_talon_;
523};
524
Brian Silvermand8f403a2014-12-13 19:12:04 -0500525class DrivetrainWriter : public LoopOutputHandler {
526 public:
527 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
528 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700529 }
530
Brian Silvermand8f403a2014-12-13 19:12:04 -0500531 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
532 right_drivetrain_talon_ = ::std::move(t);
533 }
Austin Schuh010eb812014-10-25 18:06:49 -0700534
Brian Silvermand8f403a2014-12-13 19:12:04 -0500535 private:
536 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500537 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500538 }
539
540 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500541 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500542 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800543 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
544 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500545 }
546
547 virtual void Stop() override {
548 LOG(WARNING, "drivetrain output too old\n");
549 left_drivetrain_talon_->Disable();
550 right_drivetrain_talon_->Disable();
551 }
552
Austin Schuh010eb812014-10-25 18:06:49 -0700553 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500554 ::std::unique_ptr<Talon> right_drivetrain_talon_;
555};
556
Daniel Pettiadf38432015-01-26 17:13:35 -0800557class FridgeWriter : public LoopOutputHandler {
558 public:
559 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
560 left_arm_talon_ = ::std::move(t);
561 }
562
563 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
564 right_arm_talon_ = ::std::move(t);
565 }
566
567 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
568 left_elevator_talon_ = ::std::move(t);
569 }
570
571 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
572 right_elevator_talon_ = ::std::move(t);
573 }
574
575 private:
576 virtual void Read() override {
577 ::frc971::control_loops::fridge_queue.output.FetchAnother();
578 }
579
580 virtual void Write() override {
581 auto &queue = ::frc971::control_loops::fridge_queue.output;
582 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800583 left_arm_talon_->Set(queue->left_arm / 12.0);
584 right_arm_talon_->Set(-queue->right_arm / 12.0);
585 left_elevator_talon_->Set(queue->left_elevator / 12.0);
586 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800587 }
588
589 virtual void Stop() override {
590 LOG(WARNING, "Fridge output too old.\n");
591 left_arm_talon_->Disable();
592 right_arm_talon_->Disable();
593 left_elevator_talon_->Disable();
594 right_elevator_talon_->Disable();
595 }
596
597 ::std::unique_ptr<Talon> left_arm_talon_;
598 ::std::unique_ptr<Talon> right_arm_talon_;
599 ::std::unique_ptr<Talon> left_elevator_talon_;
600 ::std::unique_ptr<Talon> right_elevator_talon_;
601};
602
603class ClawWriter : public LoopOutputHandler {
604 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800605 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
606 left_intake_talon_ = ::std::move(t);
607 }
608
609 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
610 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800611 }
612
613 void set_wrist_talon(::std::unique_ptr<Talon> t) {
614 wrist_talon_ = ::std::move(t);
615 }
616
617 private:
618 virtual void Read() override {
619 ::frc971::control_loops::claw_queue.output.FetchAnother();
620 }
621
622 virtual void Write() override {
623 auto &queue = ::frc971::control_loops::claw_queue.output;
624 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800625 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800626 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
627 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800628 }
629
630 virtual void Stop() override {
631 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800632 left_intake_talon_->Disable();
633 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800634 wrist_talon_->Disable();
635 }
636
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800637 ::std::unique_ptr<Talon> left_intake_talon_;
638 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800639 ::std::unique_ptr<Talon> wrist_talon_;
640};
641
Brian Silverman1f90d672015-01-26 20:20:45 -0500642// TODO(brian): Replace this with ::std::make_unique once all our toolchains
643// have support.
644template <class T, class... U>
645std::unique_ptr<T> make_unique(U &&... u) {
646 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
647}
648
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800649class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Austin Schuh010eb812014-10-25 18:06:49 -0700650 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800651 ::std::unique_ptr<Encoder> encoder(int index) {
652 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
653 Encoder::k4X);
654 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800655 virtual void Run() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500656 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800657 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500658
Brian Silverman98f6ee22015-01-26 17:50:12 -0500659 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700660 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800661 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500662
Brian Silverman425492b2015-12-30 15:23:55 -0800663 ::frc971::wpilib::PDPFetcher pdp_fetcher;
664 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
665
666 SensorReader reader(&pdp_fetcher);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800667 reader.set_arm_left_encoder(encoder(1));
668 reader.set_arm_left_index(make_unique<DigitalInput>(1));
669 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
670
671 reader.set_arm_right_encoder(encoder(5));
672 reader.set_arm_right_index(make_unique<DigitalInput>(5));
673 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
674
675 reader.set_elevator_left_encoder(encoder(0));
676 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
677 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
678
679 reader.set_elevator_right_encoder(encoder(4));
680 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
681 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
682
683 reader.set_wrist_encoder(encoder(6));
684 reader.set_wrist_index(make_unique<DigitalInput>(6));
685 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
686
687 reader.set_left_encoder(encoder(2));
688 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500689 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500690 ::std::thread reader_thread(::std::ref(reader));
691 GyroSender gyro_sender;
692 ::std::thread gyro_thread(::std::ref(gyro_sender));
693
694 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500695 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800696 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500697 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800698 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500699 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
700
Austin Schuhbb227f82015-09-06 15:27:52 -0700701 CanWriter can_writer;
702 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
703 ::std::thread can_writer_thread(::std::ref(can_writer));
704
Daniel Pettiadf38432015-01-26 17:13:35 -0800705 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
706 // claw.
707 FridgeWriter fridge_writer;
708 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800709 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800710 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800711 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800712 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800713 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800714 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800715 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800716 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
717
718 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800719 claw_writer.set_left_intake_talon(
720 ::std::unique_ptr<Talon>(new Talon(5)));
721 claw_writer.set_right_intake_talon(
722 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800723 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800724 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800725 ::std::thread claw_writer_thread(::std::ref(claw_writer));
726
727 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
728 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500729 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800730 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
731 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800732 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800733 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
734 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700735 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
736 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800737
738 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
739 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500740 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
741
742 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800743 while (true) {
744 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
745 if (r != 0) {
746 PLOG(WARNING, "infinite select failed");
747 } else {
748 PLOG(WARNING, "infinite select succeeded??\n");
749 }
750 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500751
Austin Schuh010eb812014-10-25 18:06:49 -0700752 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800753
Austin Schuh010eb812014-10-25 18:06:49 -0700754 joystick_sender.Quit();
755 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800756 pdp_fetcher.Quit();
757 pdp_fetcher_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500758 reader.Quit();
759 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800760 gyro_sender.Quit();
761 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500762
763 drivetrain_writer.Quit();
764 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700765 can_writer.Quit();
766 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500767 solenoid_writer.Quit();
768 solenoid_thread.join();
769
Austin Schuh010eb812014-10-25 18:06:49 -0700770 ::aos::Cleanup();
771 }
772};
773
Brian Silverman98f6ee22015-01-26 17:50:12 -0500774} // namespace wpilib
775} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800776
Brian Silverman98f6ee22015-01-26 17:50:12 -0500777
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800778AOS_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);