blob: eaaeff925fe69ce3583d6f7462e28dbd35311e64 [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 Schuh010eb812014-10-25 18:06:49 -070010#include "aos/common/logging/logging.h"
11#include "aos/common/logging/queue_logging.h"
Austin Schuh010eb812014-10-25 18:06:49 -070012#include "aos/common/time.h"
13#include "aos/common/util/log_interval.h"
14#include "aos/common/util/phased_loop.h"
15#include "aos/common/util/wrapping_counter.h"
Brian Silvermanb073f242014-09-08 16:29:57 -040016#include "aos/common/stl_mutex.h"
Austin Schuh010eb812014-10-25 18:06:49 -070017#include "aos/linux_code/init.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050018#include "aos/common/messages/robot_state.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070019
Brian Silvermanb691f5e2015-08-02 11:37:55 -070020#include "y2015/constants.h"
Brian Silverman335c20e2015-01-26 21:47:58 -050021#include "frc971/control_loops/control_loops.q.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070022#include "y2015/control_loops/drivetrain/drivetrain.q.h"
23#include "y2015/control_loops/fridge/fridge.q.h"
24#include "y2015/control_loops/claw/claw.q.h"
Austin Schuhbb227f82015-09-06 15:27:52 -070025#include "y2015/autonomous/auto.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070026
Brian Silvermanda45b6c2014-12-28 11:36:50 -080027#include "frc971/wpilib/hall_effect.h"
28#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050029#include "frc971/wpilib/loop_output_handler.h"
30#include "frc971/wpilib/buffered_solenoid.h"
31#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080032#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050033#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050034#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050035#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silverman87541532015-03-19 23:35:12 -070036#include "frc971/wpilib/logging.q.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080037
Brian Silvermancb77f232014-12-19 21:48:36 -080038#include "Encoder.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080039#include "Talon.h"
40#include "DriverStation.h"
41#include "AnalogInput.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080042#include "Compressor.h"
Austin Schuh17a2a492015-02-20 22:12:24 -080043#include "Relay.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080044#include "RobotBase.h"
Brian Silverman335c20e2015-01-26 21:47:58 -050045#include "dma.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050046#include "ControllerPower.h"
Austin Schuh010eb812014-10-25 18:06:49 -070047
48#ifndef M_PI
49#define M_PI 3.14159265358979323846
50#endif
51
52using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050053using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050054using ::frc971::control_loops::fridge_queue;
55using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070056
57namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080058namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070059
Austin Schuh010eb812014-10-25 18:06:49 -070060double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080061 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080062 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080063 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080064 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
65}
66
67double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080068 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080069 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080070 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080071 (2 * M_PI /*radians*/);
72}
73
Brian Silverman5d712fc2015-02-15 03:39:31 -050074double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080075 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080076 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080077 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080078 (2 * M_PI /*radians*/);
79}
80
81double elevator_translate(int32_t in) {
82 return static_cast<double>(in) /
83 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080084 constants::GetValues().elev_encoder_ratio *
85 (2 * M_PI /*radians*/) *
86 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080087}
88
Brian Silverman5d712fc2015-02-15 03:39:31 -050089double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -080090 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080091 constants::GetValues().elev_pot_ratio *
92 (2 * M_PI /*radians*/) *
93 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -080094 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -080095}
96
97double claw_translate(int32_t in) {
98 return static_cast<double>(in) /
99 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -0800100 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -0800101 (2 * M_PI /*radians*/);
102}
103
Brian Silverman5d712fc2015-02-15 03:39:31 -0500104double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800105 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800106 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800107 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800108 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700109}
110
Brian Silverman335c20e2015-01-26 21:47:58 -0500111static const double kMaximumEncoderPulsesPerSecond =
112 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
113 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
114 4.0 /* index pulse = 1/4 cycle */;
115
Austin Schuh010eb812014-10-25 18:06:49 -0700116class SensorReader {
117 public:
Brian Silverman1f90d672015-01-26 20:20:45 -0500118 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500119 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
120 // we should ever see.
121 filter_.SetPeriodNanoSeconds(
122 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
123 }
124
125 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
126 filter_.Add(encoder.get());
127 arm_left_encoder_.set_encoder(::std::move(encoder));
128 }
129
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700130 void set_arm_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500131 filter_.Add(index.get());
132 arm_left_encoder_.set_index(::std::move(index));
133 }
134
135 void set_arm_left_potentiometer(
136 ::std::unique_ptr<AnalogInput> potentiometer) {
137 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
138 }
139
140 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
141 filter_.Add(encoder.get());
142 arm_right_encoder_.set_encoder(::std::move(encoder));
143 }
144
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700145 void set_arm_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500146 filter_.Add(index.get());
147 arm_right_encoder_.set_index(::std::move(index));
148 }
149
150 void set_arm_right_potentiometer(
151 ::std::unique_ptr<AnalogInput> potentiometer) {
152 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
153 }
154
155 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
156 filter_.Add(encoder.get());
157 elevator_left_encoder_.set_encoder(::std::move(encoder));
158 }
159
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700160 void set_elevator_left_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500161 filter_.Add(index.get());
162 elevator_left_encoder_.set_index(::std::move(index));
163 }
164
165 void set_elevator_left_potentiometer(
166 ::std::unique_ptr<AnalogInput> potentiometer) {
167 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
168 }
169
170 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
171 filter_.Add(encoder.get());
172 elevator_right_encoder_.set_encoder(::std::move(encoder));
173 }
174
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700175 void set_elevator_right_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500176 filter_.Add(index.get());
177 elevator_right_encoder_.set_index(::std::move(index));
178 }
179
180 void set_elevator_right_potentiometer(
181 ::std::unique_ptr<AnalogInput> potentiometer) {
182 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
183 }
184
185 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
186 filter_.Add(encoder.get());
187 wrist_encoder_.set_encoder(::std::move(encoder));
188 }
189
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700190 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500191 filter_.Add(index.get());
192 wrist_encoder_.set_index(::std::move(index));
193 }
194
195 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
196 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700197 }
198
Brian Silverman1f90d672015-01-26 20:20:45 -0500199 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
200 left_encoder_ = ::std::move(left_encoder);
201 }
202
203 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
204 right_encoder_ = ::std::move(right_encoder);
205 }
206
Brian Silverman335c20e2015-01-26 21:47:58 -0500207 // All of the DMA-related set_* calls must be made before this, and it doesn't
208 // hurt to do all of them.
209 void set_dma(::std::unique_ptr<DMA> dma) {
210 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
211 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500212 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800213 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500214 dma_synchronizer_->Add(&elevator_right_encoder_);
215 }
216
Austin Schuh010eb812014-10-25 18:06:49 -0700217 void operator()() {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800218 LOG(INFO, "In sensor reader thread\n");
Brian Silverman2fe007c2014-12-28 12:20:01 -0800219 ::aos::SetCurrentThreadName("SensorReader");
220
Brian Silverman699f0cb2015-02-05 19:45:01 -0500221 my_pid_ = getpid();
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700222 ds_ =
223#ifdef WPILIB2015
224 DriverStation::GetInstance();
225#else
226 &DriverStation::GetInstance();
227#endif
Brian Silverman699f0cb2015-02-05 19:45:01 -0500228
Brian Silverman335c20e2015-01-26 21:47:58 -0500229 wrist_encoder_.Start();
230 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800231 LOG(INFO, "Things are now started\n");
Austin Schuh010eb812014-10-25 18:06:49 -0700232
Brian Silverman2fe007c2014-12-28 12:20:01 -0800233 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700234 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800235 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700236 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700237 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500238
239 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700240 }
241
242 void RunIteration() {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500243 {
244 auto new_state = ::aos::robot_state.MakeMessage();
Austin Schuh010eb812014-10-25 18:06:49 -0700245
Austin Schuhfae69172015-02-20 22:11:26 -0800246 new_state->reader_pid = my_pid_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500247 new_state->outputs_enabled = ds_->IsSysActive();
248 new_state->browned_out = ds_->IsSysBrownedOut();
249
250 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
251 new_state->is_5v_active = ControllerPower::GetEnabled5V();
252 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
253 new_state->voltage_5v = ControllerPower::GetVoltage5V();
254
255 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
256 new_state->voltage_battery = ds_->GetBatteryVoltage();
257
Brian Silverman4d882aa2015-02-20 13:40:42 -0500258 LOG_STRUCT(DEBUG, "robot_state", *new_state);
259
Brian Silverman699f0cb2015-02-05 19:45:01 -0500260 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700261 }
262
Austin Schuh35d06612015-02-15 23:35:23 -0800263 {
264 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
265 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800266 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800267 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800268 drivetrain_translate(left_encoder_->GetRaw());
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_;
314
Brian Silverman335c20e2015-01-26 21:47:58 -0500315 void CopyPotAndIndexPosition(
316 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
317 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500318 ::std::function<double(double)> potentiometer_translate, bool reverse,
319 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500320 const double multiplier = reverse ? -1.0 : 1.0;
321 position->encoder =
322 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500323 position->pot = multiplier * potentiometer_translate(
324 encoder.polled_potentiometer_voltage()) +
325 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500326 position->latched_encoder =
327 multiplier * encoder_translate(encoder.last_encoder_value());
328 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500329 multiplier *
330 potentiometer_translate(encoder.last_potentiometer_voltage()) +
331 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500332 position->index_pulses = encoder.index_posedge_count();
333 }
334
335 void CopyPotAndIndexPosition(
336 const InterruptEncoderAndPotentiometer &encoder,
337 PotAndIndexPosition *position,
338 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500339 ::std::function<double(double)> potentiometer_translate, bool reverse,
340 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500341 const double multiplier = reverse ? -1.0 : 1.0;
342 position->encoder =
343 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500344 position->pot = multiplier * potentiometer_translate(
345 encoder.potentiometer()->GetVoltage()) +
346 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500347 position->latched_encoder =
348 multiplier * encoder_translate(encoder.last_encoder_value());
349 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500350 multiplier *
351 potentiometer_translate(encoder.last_potentiometer_voltage()) +
352 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500353 position->index_pulses = encoder.index_posedge_count();
354 }
355
Brian Silverman335c20e2015-01-26 21:47:58 -0500356 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
357
358 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
359 elevator_left_encoder_, elevator_right_encoder_;
360
361 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
362
Austin Schuh010eb812014-10-25 18:06:49 -0700363 ::std::unique_ptr<Encoder> left_encoder_;
364 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700365
Brian Silverman1f90d672015-01-26 20:20:45 -0500366 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700367 DigitalGlitchFilter filter_;
368};
369
Brian Silvermand8f403a2014-12-13 19:12:04 -0500370class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700371 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500372 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800373 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800374 fridge_(".frc971.control_loops.fridge_queue.output"),
375 claw_(".frc971.control_loops.claw_queue.output") {}
376
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700377 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
Austin Schuh17a2a492015-02-20 22:12:24 -0800378 pressure_switch_ = ::std::move(pressure_switch);
379 }
380
381 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
382 compressor_relay_ = ::std::move(compressor_relay);
383 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500384
Daniel Pettiadf38432015-01-26 17:13:35 -0800385 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
386 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700387 }
388
Daniel Pettiadf38432015-01-26 17:13:35 -0800389 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
390 fridge_grabbers_top_back_ = ::std::move(s);
391 }
392
393 void set_fridge_grabbers_bottom_front(
394 ::std::unique_ptr<BufferedSolenoid> s) {
395 fridge_grabbers_bottom_front_ = ::std::move(s);
396 }
397
398 void set_fridge_grabbers_bottom_back(
399 ::std::unique_ptr<BufferedSolenoid> s) {
400 fridge_grabbers_bottom_back_ = ::std::move(s);
401 }
402
403 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
404 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500405 }
Austin Schuh010eb812014-10-25 18:06:49 -0700406
Brian Silverman93936f72015-03-19 23:38:30 -0700407 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
408 grabber_latch_release_ = ::std::move(s);
409 }
410
411 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
412 grabber_fold_up_ = ::std::move(s);
413 }
414
Brian Silvermand8f403a2014-12-13 19:12:04 -0500415 void operator()() {
416 ::aos::SetCurrentThreadName("Solenoids");
417 ::aos::SetCurrentThreadRealtimePriority(30);
418
419 while (run_) {
420 ::aos::time::PhasedLoopXMS(20, 1000);
421
422 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800423 fridge_.FetchLatest();
424 if (fridge_.get()) {
425 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800426 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
427 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
428 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
429 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800430 }
431 }
432
433 {
434 claw_.FetchLatest();
435 if (claw_.get()) {
436 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800437 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500438 }
439 }
440
Brian Silverman93936f72015-03-19 23:38:30 -0700441 ::aos::joystick_state.FetchLatest();
442 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
443 ::aos::joystick_state->autonomous);
444 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
445 ::aos::joystick_state->joysticks[1].buttons & 1);
446
Brian Silverman87541532015-03-19 23:35:12 -0700447 {
448 PneumaticsToLog to_log;
449 {
450 const bool compressor_on = !pressure_switch_->Get();
451 to_log.compressor_on = compressor_on;
452 if (compressor_on) {
453 compressor_relay_->Set(Relay::kForward);
454 } else {
455 compressor_relay_->Set(Relay::kOff);
456 }
457 }
458
459 pcm_->Flush();
460 to_log.read_solenoids = pcm_->GetAll();
461 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
462 }
Austin Schuh010eb812014-10-25 18:06:49 -0700463 }
464 }
465
Brian Silvermand8f403a2014-12-13 19:12:04 -0500466 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700467
Brian Silvermand8f403a2014-12-13 19:12:04 -0500468 private:
469 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800470 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
471 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
472 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
473 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
474 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700475 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
476 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700477 ::std::unique_ptr<DigitalInput> pressure_switch_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800478 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700479
Daniel Pettiadf38432015-01-26 17:13:35 -0800480 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
481 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700482
Brian Silvermand8f403a2014-12-13 19:12:04 -0500483 ::std::atomic<bool> run_{true};
484};
485
Austin Schuhbb227f82015-09-06 15:27:52 -0700486class CanWriter : public LoopOutputHandler {
487 public:
488 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
489
490 void set_can_talon(::std::unique_ptr<Talon> t) {
491 can_talon_ = ::std::move(t);
492 }
493
494 private:
495 virtual void Read() override {
496 ::frc971::autonomous::can_control.FetchAnother();
497 }
498
499 virtual void Write() override {
500 auto &queue = ::frc971::autonomous::can_control;
501 LOG_STRUCT(DEBUG, "will output", *queue);
502 can_talon_->Set(queue->can_voltage / 12.0);
503 }
504
505 virtual void Stop() override {
506 LOG(WARNING, "Can output too old\n");
507 can_talon_->Disable();
508 }
509
510 ::std::unique_ptr<Talon> can_talon_;
511};
512
Brian Silvermand8f403a2014-12-13 19:12:04 -0500513class DrivetrainWriter : public LoopOutputHandler {
514 public:
515 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
516 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700517 }
518
Brian Silvermand8f403a2014-12-13 19:12:04 -0500519 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
520 right_drivetrain_talon_ = ::std::move(t);
521 }
Austin Schuh010eb812014-10-25 18:06:49 -0700522
Brian Silvermand8f403a2014-12-13 19:12:04 -0500523 private:
524 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500525 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500526 }
527
528 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500529 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500530 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800531 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
532 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500533 }
534
535 virtual void Stop() override {
536 LOG(WARNING, "drivetrain output too old\n");
537 left_drivetrain_talon_->Disable();
538 right_drivetrain_talon_->Disable();
539 }
540
Austin Schuh010eb812014-10-25 18:06:49 -0700541 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500542 ::std::unique_ptr<Talon> right_drivetrain_talon_;
543};
544
Daniel Pettiadf38432015-01-26 17:13:35 -0800545class FridgeWriter : public LoopOutputHandler {
546 public:
547 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
548 left_arm_talon_ = ::std::move(t);
549 }
550
551 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
552 right_arm_talon_ = ::std::move(t);
553 }
554
555 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
556 left_elevator_talon_ = ::std::move(t);
557 }
558
559 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
560 right_elevator_talon_ = ::std::move(t);
561 }
562
563 private:
564 virtual void Read() override {
565 ::frc971::control_loops::fridge_queue.output.FetchAnother();
566 }
567
568 virtual void Write() override {
569 auto &queue = ::frc971::control_loops::fridge_queue.output;
570 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800571 left_arm_talon_->Set(queue->left_arm / 12.0);
572 right_arm_talon_->Set(-queue->right_arm / 12.0);
573 left_elevator_talon_->Set(queue->left_elevator / 12.0);
574 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800575 }
576
577 virtual void Stop() override {
578 LOG(WARNING, "Fridge output too old.\n");
579 left_arm_talon_->Disable();
580 right_arm_talon_->Disable();
581 left_elevator_talon_->Disable();
582 right_elevator_talon_->Disable();
583 }
584
585 ::std::unique_ptr<Talon> left_arm_talon_;
586 ::std::unique_ptr<Talon> right_arm_talon_;
587 ::std::unique_ptr<Talon> left_elevator_talon_;
588 ::std::unique_ptr<Talon> right_elevator_talon_;
589};
590
591class ClawWriter : public LoopOutputHandler {
592 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800593 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
594 left_intake_talon_ = ::std::move(t);
595 }
596
597 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
598 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800599 }
600
601 void set_wrist_talon(::std::unique_ptr<Talon> t) {
602 wrist_talon_ = ::std::move(t);
603 }
604
605 private:
606 virtual void Read() override {
607 ::frc971::control_loops::claw_queue.output.FetchAnother();
608 }
609
610 virtual void Write() override {
611 auto &queue = ::frc971::control_loops::claw_queue.output;
612 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800613 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800614 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
615 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800616 }
617
618 virtual void Stop() override {
619 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800620 left_intake_talon_->Disable();
621 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800622 wrist_talon_->Disable();
623 }
624
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800625 ::std::unique_ptr<Talon> left_intake_talon_;
626 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800627 ::std::unique_ptr<Talon> wrist_talon_;
628};
629
Brian Silverman1f90d672015-01-26 20:20:45 -0500630// TODO(brian): Replace this with ::std::make_unique once all our toolchains
631// have support.
632template <class T, class... U>
633std::unique_ptr<T> make_unique(U &&... u) {
634 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
635}
636
Austin Schuh010eb812014-10-25 18:06:49 -0700637class WPILibRobot : public RobotBase {
638 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800639 ::std::unique_ptr<Encoder> encoder(int index) {
640 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
641 Encoder::k4X);
642 }
Austin Schuh010eb812014-10-25 18:06:49 -0700643 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500644 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800645 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500646
Brian Silverman98f6ee22015-01-26 17:50:12 -0500647 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700648 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800649 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500650
Brian Silverman98f6ee22015-01-26 17:50:12 -0500651 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800652 LOG(INFO, "Creating the reader\n");
653 reader.set_arm_left_encoder(encoder(1));
654 reader.set_arm_left_index(make_unique<DigitalInput>(1));
655 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
656
657 reader.set_arm_right_encoder(encoder(5));
658 reader.set_arm_right_index(make_unique<DigitalInput>(5));
659 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
660
661 reader.set_elevator_left_encoder(encoder(0));
662 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
663 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
664
665 reader.set_elevator_right_encoder(encoder(4));
666 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
667 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
668
669 reader.set_wrist_encoder(encoder(6));
670 reader.set_wrist_index(make_unique<DigitalInput>(6));
671 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
672
673 reader.set_left_encoder(encoder(2));
674 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500675 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500676 ::std::thread reader_thread(::std::ref(reader));
677 GyroSender gyro_sender;
678 ::std::thread gyro_thread(::std::ref(gyro_sender));
679
680 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500681 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800682 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500683 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800684 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500685 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
686
Austin Schuhbb227f82015-09-06 15:27:52 -0700687 CanWriter can_writer;
688 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
689 ::std::thread can_writer_thread(::std::ref(can_writer));
690
Daniel Pettiadf38432015-01-26 17:13:35 -0800691 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
692 // claw.
693 FridgeWriter fridge_writer;
694 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800695 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800696 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800697 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800698 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800699 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800700 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800701 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800702 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
703
704 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800705 claw_writer.set_left_intake_talon(
706 ::std::unique_ptr<Talon>(new Talon(5)));
707 claw_writer.set_right_intake_talon(
708 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800709 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800710 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800711 ::std::thread claw_writer_thread(::std::ref(claw_writer));
712
713 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
714 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500715 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800716 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
717 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800718 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800719 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
720 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700721 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
722 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800723
724 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
725 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500726 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
727
728 // Wait forever. Not much else to do...
729 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
730
Austin Schuh010eb812014-10-25 18:06:49 -0700731 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800732
Austin Schuh010eb812014-10-25 18:06:49 -0700733 joystick_sender.Quit();
734 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500735 reader.Quit();
736 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800737 gyro_sender.Quit();
738 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500739
740 drivetrain_writer.Quit();
741 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700742 can_writer.Quit();
743 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500744 solenoid_writer.Quit();
745 solenoid_thread.join();
746
Austin Schuh010eb812014-10-25 18:06:49 -0700747 ::aos::Cleanup();
748 }
749};
750
Brian Silverman98f6ee22015-01-26 17:50:12 -0500751} // namespace wpilib
752} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800753
Brian Silverman98f6ee22015-01-26 17:50:12 -0500754
755START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);