blob: 6a7d8608eca5ab6b0cbdfd89cb788e20c8f06927 [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
Daniel Pettia7827412015-02-13 20:55:57 -080020#include "frc971/constants.h"
Brian Silverman335c20e2015-01-26 21:47:58 -050021#include "frc971/control_loops/control_loops.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070022#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Daniel Pettiadf38432015-01-26 17:13:35 -080023#include "frc971/control_loops/fridge/fridge.q.h"
24#include "frc971/control_loops/claw/claw.q.h"
Austin Schuh010eb812014-10-25 18:06:49 -070025
Brian Silvermanda45b6c2014-12-28 11:36:50 -080026#include "frc971/wpilib/hall_effect.h"
27#include "frc971/wpilib/joystick_sender.h"
Brian Silvermand8f403a2014-12-13 19:12:04 -050028#include "frc971/wpilib/loop_output_handler.h"
29#include "frc971/wpilib/buffered_solenoid.h"
30#include "frc971/wpilib/buffered_pcm.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -080031#include "frc971/wpilib/gyro_sender.h"
Brian Silvermanff7b3472015-01-26 17:53:04 -050032#include "frc971/wpilib/dma_edge_counting.h"
Brian Silverman70ec7192015-01-26 17:52:40 -050033#include "frc971/wpilib/interrupt_edge_counting.h"
Brian Silverman4da58072015-01-26 20:18:52 -050034#include "frc971/wpilib/encoder_and_potentiometer.h"
Brian Silvermanda45b6c2014-12-28 11:36:50 -080035
Brian Silvermancb77f232014-12-19 21:48:36 -080036#include "Encoder.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080037#include "Talon.h"
38#include "DriverStation.h"
39#include "AnalogInput.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080040#include "Compressor.h"
Austin Schuh17a2a492015-02-20 22:12:24 -080041#include "Relay.h"
Brian Silvermancb77f232014-12-19 21:48:36 -080042#include "RobotBase.h"
Brian Silverman335c20e2015-01-26 21:47:58 -050043#include "dma.h"
Brian Silverman699f0cb2015-02-05 19:45:01 -050044#include "ControllerPower.h"
Austin Schuh010eb812014-10-25 18:06:49 -070045
46#ifndef M_PI
47#define M_PI 3.14159265358979323846
48#endif
49
50using ::aos::util::SimpleLogInterval;
Brian Silvermanada5f2c2015-02-01 02:41:14 -050051using ::frc971::control_loops::drivetrain_queue;
Brian Silverman335c20e2015-01-26 21:47:58 -050052using ::frc971::control_loops::fridge_queue;
53using ::frc971::control_loops::claw_queue;
Austin Schuh010eb812014-10-25 18:06:49 -070054
55namespace frc971 {
Brian Silvermanda45b6c2014-12-28 11:36:50 -080056namespace wpilib {
Austin Schuh010eb812014-10-25 18:06:49 -070057
Austin Schuh010eb812014-10-25 18:06:49 -070058double drivetrain_translate(int32_t in) {
Austin Schuhdb516032014-12-28 00:12:38 -080059 return static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080060 (256.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080061 constants::GetValues().drivetrain_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080062 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
63}
64
65double arm_translate(int32_t in) {
Austin Schuh6246c542015-02-16 02:59:09 -080066 return -static_cast<double>(in) /
Daniel Pettiadf38432015-01-26 17:13:35 -080067 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080068 constants::GetValues().arm_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080069 (2 * M_PI /*radians*/);
70}
71
Brian Silverman5d712fc2015-02-15 03:39:31 -050072double arm_potentiometer_translate(double voltage) {
Austin Schuh35d06612015-02-15 23:35:23 -080073 return voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080074 constants::GetValues().arm_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -080075 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -080076 (2 * M_PI /*radians*/);
77}
78
79double elevator_translate(int32_t in) {
80 return static_cast<double>(in) /
81 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080082 constants::GetValues().elev_encoder_ratio *
83 (2 * M_PI /*radians*/) *
84 constants::GetValues().elev_distance_per_radian;
Daniel Pettiadf38432015-01-26 17:13:35 -080085}
86
Brian Silverman5d712fc2015-02-15 03:39:31 -050087double elevator_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -080088 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -080089 constants::GetValues().elev_pot_ratio *
90 (2 * M_PI /*radians*/) *
91 constants::GetValues().elev_distance_per_radian *
Austin Schuh35d06612015-02-15 23:35:23 -080092 (5.0 /*turns*/ / 5.0 /*volts*/);
Daniel Pettiadf38432015-01-26 17:13:35 -080093}
94
95double claw_translate(int32_t in) {
96 return static_cast<double>(in) /
97 (512.0 /*cpr*/ * 4.0 /*4x*/) *
Daniel Pettia7827412015-02-13 20:55:57 -080098 constants::GetValues().claw_encoder_ratio *
Daniel Pettiadf38432015-01-26 17:13:35 -080099 (2 * M_PI /*radians*/);
100}
101
Brian Silverman5d712fc2015-02-15 03:39:31 -0500102double claw_potentiometer_translate(double voltage) {
Austin Schuh6246c542015-02-16 02:59:09 -0800103 return -voltage *
Daniel Pettia7827412015-02-13 20:55:57 -0800104 constants::GetValues().claw_pot_ratio *
Austin Schuh35d06612015-02-15 23:35:23 -0800105 (5.0 /*turns*/ / 5.0 /*volts*/) *
Daniel Pettiadf38432015-01-26 17:13:35 -0800106 (2 * M_PI /*radians*/);
Austin Schuh010eb812014-10-25 18:06:49 -0700107}
108
Brian Silverman335c20e2015-01-26 21:47:58 -0500109static const double kMaximumEncoderPulsesPerSecond =
110 19500.0 /* free speed RPM */ * 12.0 / 56.0 /* belt reduction */ /
111 60.0 /* seconds / minute */ * 256.0 /* CPR */ *
112 4.0 /* index pulse = 1/4 cycle */;
113
Austin Schuh010eb812014-10-25 18:06:49 -0700114class SensorReader {
115 public:
Brian Silverman1f90d672015-01-26 20:20:45 -0500116 SensorReader() {
Brian Silverman335c20e2015-01-26 21:47:58 -0500117 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
118 // we should ever see.
119 filter_.SetPeriodNanoSeconds(
120 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
121 }
122
123 void set_arm_left_encoder(::std::unique_ptr<Encoder> encoder) {
124 filter_.Add(encoder.get());
125 arm_left_encoder_.set_encoder(::std::move(encoder));
126 }
127
128 void set_arm_left_index(::std::unique_ptr<DigitalSource> index) {
129 filter_.Add(index.get());
130 arm_left_encoder_.set_index(::std::move(index));
131 }
132
133 void set_arm_left_potentiometer(
134 ::std::unique_ptr<AnalogInput> potentiometer) {
135 arm_left_encoder_.set_potentiometer(::std::move(potentiometer));
136 }
137
138 void set_arm_right_encoder(::std::unique_ptr<Encoder> encoder) {
139 filter_.Add(encoder.get());
140 arm_right_encoder_.set_encoder(::std::move(encoder));
141 }
142
143 void set_arm_right_index(::std::unique_ptr<DigitalSource> index) {
144 filter_.Add(index.get());
145 arm_right_encoder_.set_index(::std::move(index));
146 }
147
148 void set_arm_right_potentiometer(
149 ::std::unique_ptr<AnalogInput> potentiometer) {
150 arm_right_encoder_.set_potentiometer(::std::move(potentiometer));
151 }
152
153 void set_elevator_left_encoder(::std::unique_ptr<Encoder> encoder) {
154 filter_.Add(encoder.get());
155 elevator_left_encoder_.set_encoder(::std::move(encoder));
156 }
157
158 void set_elevator_left_index(::std::unique_ptr<DigitalSource> index) {
159 filter_.Add(index.get());
160 elevator_left_encoder_.set_index(::std::move(index));
161 }
162
163 void set_elevator_left_potentiometer(
164 ::std::unique_ptr<AnalogInput> potentiometer) {
165 elevator_left_encoder_.set_potentiometer(::std::move(potentiometer));
166 }
167
168 void set_elevator_right_encoder(::std::unique_ptr<Encoder> encoder) {
169 filter_.Add(encoder.get());
170 elevator_right_encoder_.set_encoder(::std::move(encoder));
171 }
172
173 void set_elevator_right_index(::std::unique_ptr<DigitalSource> index) {
174 filter_.Add(index.get());
175 elevator_right_encoder_.set_index(::std::move(index));
176 }
177
178 void set_elevator_right_potentiometer(
179 ::std::unique_ptr<AnalogInput> potentiometer) {
180 elevator_right_encoder_.set_potentiometer(::std::move(potentiometer));
181 }
182
183 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
184 filter_.Add(encoder.get());
185 wrist_encoder_.set_encoder(::std::move(encoder));
186 }
187
188 void set_wrist_index(::std::unique_ptr<DigitalSource> index) {
189 filter_.Add(index.get());
190 wrist_encoder_.set_index(::std::move(index));
191 }
192
193 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
194 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
Austin Schuh010eb812014-10-25 18:06:49 -0700195 }
196
Brian Silverman1f90d672015-01-26 20:20:45 -0500197 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
198 left_encoder_ = ::std::move(left_encoder);
199 }
200
201 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
202 right_encoder_ = ::std::move(right_encoder);
203 }
204
Brian Silverman335c20e2015-01-26 21:47:58 -0500205 // All of the DMA-related set_* calls must be made before this, and it doesn't
206 // hurt to do all of them.
207 void set_dma(::std::unique_ptr<DMA> dma) {
208 dma_synchronizer_.reset(new DMASynchronizer(::std::move(dma)));
209 dma_synchronizer_->Add(&arm_left_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500210 dma_synchronizer_->Add(&elevator_left_encoder_);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800211 dma_synchronizer_->Add(&arm_right_encoder_);
Brian Silverman335c20e2015-01-26 21:47:58 -0500212 dma_synchronizer_->Add(&elevator_right_encoder_);
213 }
214
Austin Schuh010eb812014-10-25 18:06:49 -0700215 void operator()() {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800216 LOG(INFO, "In sensor reader thread\n");
Brian Silverman2fe007c2014-12-28 12:20:01 -0800217 ::aos::SetCurrentThreadName("SensorReader");
218
Brian Silverman699f0cb2015-02-05 19:45:01 -0500219 my_pid_ = getpid();
220 ds_ = DriverStation::GetInstance();
221
Brian Silverman335c20e2015-01-26 21:47:58 -0500222 wrist_encoder_.Start();
223 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800224 LOG(INFO, "Things are now started\n");
Austin Schuh010eb812014-10-25 18:06:49 -0700225
Brian Silverman2fe007c2014-12-28 12:20:01 -0800226 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700227 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800228 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700229 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700230 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500231
232 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700233 }
234
235 void RunIteration() {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500236 {
237 auto new_state = ::aos::robot_state.MakeMessage();
Austin Schuh010eb812014-10-25 18:06:49 -0700238
Austin Schuhfae69172015-02-20 22:11:26 -0800239 new_state->reader_pid = my_pid_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500240 new_state->outputs_enabled = ds_->IsSysActive();
241 new_state->browned_out = ds_->IsSysBrownedOut();
242
243 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
244 new_state->is_5v_active = ControllerPower::GetEnabled5V();
245 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
246 new_state->voltage_5v = ControllerPower::GetVoltage5V();
247
248 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
249 new_state->voltage_battery = ds_->GetBatteryVoltage();
250
Brian Silverman4d882aa2015-02-20 13:40:42 -0500251 LOG_STRUCT(DEBUG, "robot_state", *new_state);
252
Brian Silverman699f0cb2015-02-05 19:45:01 -0500253 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700254 }
255
Austin Schuh35d06612015-02-15 23:35:23 -0800256 {
257 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
258 drivetrain_message->right_encoder =
259 drivetrain_translate(right_encoder_->GetRaw());
260 drivetrain_message->left_encoder =
261 -drivetrain_translate(left_encoder_->GetRaw());
262
263 drivetrain_message.Send();
264 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500265
266 dma_synchronizer_->RunIteration();
267
Philipp Schrader82c65072015-02-16 00:47:09 +0000268 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500269
Brian Silverman335c20e2015-01-26 21:47:58 -0500270 {
271 auto fridge_message = fridge_queue.position.MakeMessage();
272 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500273 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800274 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500275 CopyPotAndIndexPosition(
276 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
277 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800278 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500279 CopyPotAndIndexPosition(
280 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500281 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800282 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500283 CopyPotAndIndexPosition(
284 elevator_right_encoder_, &fridge_message->elevator.right,
285 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800286 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500287 fridge_message.Send();
288 }
289
290 {
291 auto claw_message = claw_queue.position.MakeMessage();
292 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500293 claw_translate, claw_potentiometer_translate,
294 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500295 claw_message.Send();
296 }
Austin Schuh010eb812014-10-25 18:06:49 -0700297 }
298
299 void Quit() { run_ = false; }
300
301 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500302 static const int kPriority = 30;
303 static const int kInterruptPriority = 55;
304
Brian Silverman699f0cb2015-02-05 19:45:01 -0500305 int32_t my_pid_;
306 DriverStation *ds_;
307
Brian Silverman335c20e2015-01-26 21:47:58 -0500308 void CopyPotAndIndexPosition(
309 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
310 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500311 ::std::function<double(double)> potentiometer_translate, bool reverse,
312 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500313 const double multiplier = reverse ? -1.0 : 1.0;
314 position->encoder =
315 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500316 position->pot = multiplier * potentiometer_translate(
317 encoder.polled_potentiometer_voltage()) +
318 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500319 position->latched_encoder =
320 multiplier * encoder_translate(encoder.last_encoder_value());
321 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500322 multiplier *
323 potentiometer_translate(encoder.last_potentiometer_voltage()) +
324 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500325 position->index_pulses = encoder.index_posedge_count();
326 }
327
328 void CopyPotAndIndexPosition(
329 const InterruptEncoderAndPotentiometer &encoder,
330 PotAndIndexPosition *position,
331 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500332 ::std::function<double(double)> potentiometer_translate, bool reverse,
333 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500334 const double multiplier = reverse ? -1.0 : 1.0;
335 position->encoder =
336 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500337 position->pot = multiplier * potentiometer_translate(
338 encoder.potentiometer()->GetVoltage()) +
339 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500340 position->latched_encoder =
341 multiplier * encoder_translate(encoder.last_encoder_value());
342 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500343 multiplier *
344 potentiometer_translate(encoder.last_potentiometer_voltage()) +
345 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500346 position->index_pulses = encoder.index_posedge_count();
347 }
348
Brian Silverman335c20e2015-01-26 21:47:58 -0500349 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
350
351 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
352 elevator_left_encoder_, elevator_right_encoder_;
353
354 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
355
Austin Schuh010eb812014-10-25 18:06:49 -0700356 ::std::unique_ptr<Encoder> left_encoder_;
357 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700358
Brian Silverman1f90d672015-01-26 20:20:45 -0500359 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700360 DigitalGlitchFilter filter_;
361};
362
Brian Silvermand8f403a2014-12-13 19:12:04 -0500363class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700364 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500365 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800366 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800367 fridge_(".frc971.control_loops.fridge_queue.output"),
368 claw_(".frc971.control_loops.claw_queue.output") {}
369
370 void set_pressure_switch(::std::unique_ptr<DigitalSource> pressure_switch) {
371 pressure_switch_ = ::std::move(pressure_switch);
372 }
373
374 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
375 compressor_relay_ = ::std::move(compressor_relay);
376 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500377
Daniel Pettiadf38432015-01-26 17:13:35 -0800378 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
379 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700380 }
381
Daniel Pettiadf38432015-01-26 17:13:35 -0800382 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
383 fridge_grabbers_top_back_ = ::std::move(s);
384 }
385
386 void set_fridge_grabbers_bottom_front(
387 ::std::unique_ptr<BufferedSolenoid> s) {
388 fridge_grabbers_bottom_front_ = ::std::move(s);
389 }
390
391 void set_fridge_grabbers_bottom_back(
392 ::std::unique_ptr<BufferedSolenoid> s) {
393 fridge_grabbers_bottom_back_ = ::std::move(s);
394 }
395
396 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
397 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500398 }
Austin Schuh010eb812014-10-25 18:06:49 -0700399
Brian Silvermand8f403a2014-12-13 19:12:04 -0500400 void operator()() {
401 ::aos::SetCurrentThreadName("Solenoids");
402 ::aos::SetCurrentThreadRealtimePriority(30);
403
404 while (run_) {
405 ::aos::time::PhasedLoopXMS(20, 1000);
406
407 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800408 fridge_.FetchLatest();
409 if (fridge_.get()) {
410 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
411 fridge_grabbers_top_front_->Set(fridge_->grabbers.top_front);
412 fridge_grabbers_top_back_->Set(fridge_->grabbers.top_back);
413 fridge_grabbers_bottom_front_->Set(fridge_->grabbers.bottom_front);
414 fridge_grabbers_bottom_back_->Set(fridge_->grabbers.bottom_back);
415 }
416 }
417
418 {
419 claw_.FetchLatest();
420 if (claw_.get()) {
421 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh17a2a492015-02-20 22:12:24 -0800422 claw_pinchers_->Set(!claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500423 }
424 }
Austin Schuh17a2a492015-02-20 22:12:24 -0800425 if (!pressure_switch_->Get()) {
426 compressor_relay_->Set(Relay::kForward);
427 } else {
428 compressor_relay_->Set(Relay::kOff);
429 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500430
Brian Silvermand8f403a2014-12-13 19:12:04 -0500431 pcm_->Flush();
Austin Schuh010eb812014-10-25 18:06:49 -0700432 }
433 }
434
Brian Silvermand8f403a2014-12-13 19:12:04 -0500435 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700436
Brian Silvermand8f403a2014-12-13 19:12:04 -0500437 private:
438 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800439 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
440 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
441 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
442 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
443 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800444 ::std::unique_ptr<DigitalSource> pressure_switch_;
445 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700446
Daniel Pettiadf38432015-01-26 17:13:35 -0800447 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
448 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700449
Brian Silvermand8f403a2014-12-13 19:12:04 -0500450 ::std::atomic<bool> run_{true};
451};
452
453class DrivetrainWriter : public LoopOutputHandler {
454 public:
455 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
456 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700457 }
458
Brian Silvermand8f403a2014-12-13 19:12:04 -0500459 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
460 right_drivetrain_talon_ = ::std::move(t);
461 }
Austin Schuh010eb812014-10-25 18:06:49 -0700462
Brian Silvermand8f403a2014-12-13 19:12:04 -0500463 private:
464 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500465 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500466 }
467
468 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500469 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500470 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800471 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
472 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500473 }
474
475 virtual void Stop() override {
476 LOG(WARNING, "drivetrain output too old\n");
477 left_drivetrain_talon_->Disable();
478 right_drivetrain_talon_->Disable();
479 }
480
Austin Schuh010eb812014-10-25 18:06:49 -0700481 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500482 ::std::unique_ptr<Talon> right_drivetrain_talon_;
483};
484
Daniel Pettiadf38432015-01-26 17:13:35 -0800485class FridgeWriter : public LoopOutputHandler {
486 public:
487 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
488 left_arm_talon_ = ::std::move(t);
489 }
490
491 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
492 right_arm_talon_ = ::std::move(t);
493 }
494
495 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
496 left_elevator_talon_ = ::std::move(t);
497 }
498
499 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
500 right_elevator_talon_ = ::std::move(t);
501 }
502
503 private:
504 virtual void Read() override {
505 ::frc971::control_loops::fridge_queue.output.FetchAnother();
506 }
507
508 virtual void Write() override {
509 auto &queue = ::frc971::control_loops::fridge_queue.output;
510 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800511 left_arm_talon_->Set(queue->left_arm / 12.0);
512 right_arm_talon_->Set(-queue->right_arm / 12.0);
513 left_elevator_talon_->Set(queue->left_elevator / 12.0);
514 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800515 }
516
517 virtual void Stop() override {
518 LOG(WARNING, "Fridge output too old.\n");
519 left_arm_talon_->Disable();
520 right_arm_talon_->Disable();
521 left_elevator_talon_->Disable();
522 right_elevator_talon_->Disable();
523 }
524
525 ::std::unique_ptr<Talon> left_arm_talon_;
526 ::std::unique_ptr<Talon> right_arm_talon_;
527 ::std::unique_ptr<Talon> left_elevator_talon_;
528 ::std::unique_ptr<Talon> right_elevator_talon_;
529};
530
531class ClawWriter : public LoopOutputHandler {
532 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800533 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
534 left_intake_talon_ = ::std::move(t);
535 }
536
537 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
538 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800539 }
540
541 void set_wrist_talon(::std::unique_ptr<Talon> t) {
542 wrist_talon_ = ::std::move(t);
543 }
544
545 private:
546 virtual void Read() override {
547 ::frc971::control_loops::claw_queue.output.FetchAnother();
548 }
549
550 virtual void Write() override {
551 auto &queue = ::frc971::control_loops::claw_queue.output;
552 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800553 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800554 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
555 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800556 }
557
558 virtual void Stop() override {
559 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800560 left_intake_talon_->Disable();
561 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800562 wrist_talon_->Disable();
563 }
564
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800565 ::std::unique_ptr<Talon> left_intake_talon_;
566 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800567 ::std::unique_ptr<Talon> wrist_talon_;
568};
569
Brian Silverman1f90d672015-01-26 20:20:45 -0500570// TODO(brian): Replace this with ::std::make_unique once all our toolchains
571// have support.
572template <class T, class... U>
573std::unique_ptr<T> make_unique(U &&... u) {
574 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
575}
576
Austin Schuh010eb812014-10-25 18:06:49 -0700577class WPILibRobot : public RobotBase {
578 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800579 ::std::unique_ptr<Encoder> encoder(int index) {
580 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
581 Encoder::k4X);
582 }
Austin Schuh010eb812014-10-25 18:06:49 -0700583 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500584 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800585 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500586
Brian Silverman98f6ee22015-01-26 17:50:12 -0500587 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700588 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800589 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500590
Brian Silverman98f6ee22015-01-26 17:50:12 -0500591 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800592 LOG(INFO, "Creating the reader\n");
593 reader.set_arm_left_encoder(encoder(1));
594 reader.set_arm_left_index(make_unique<DigitalInput>(1));
595 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
596
597 reader.set_arm_right_encoder(encoder(5));
598 reader.set_arm_right_index(make_unique<DigitalInput>(5));
599 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
600
601 reader.set_elevator_left_encoder(encoder(0));
602 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
603 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
604
605 reader.set_elevator_right_encoder(encoder(4));
606 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
607 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
608
609 reader.set_wrist_encoder(encoder(6));
610 reader.set_wrist_index(make_unique<DigitalInput>(6));
611 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
612
613 reader.set_left_encoder(encoder(2));
614 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500615 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500616 ::std::thread reader_thread(::std::ref(reader));
617 GyroSender gyro_sender;
618 ::std::thread gyro_thread(::std::ref(gyro_sender));
619
620 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500621 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800622 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500623 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800624 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500625 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
626
Daniel Pettiadf38432015-01-26 17:13:35 -0800627 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
628 // claw.
629 FridgeWriter fridge_writer;
630 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800631 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800632 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800633 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800634 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800635 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800636 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800637 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800638 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
639
640 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800641 claw_writer.set_left_intake_talon(
642 ::std::unique_ptr<Talon>(new Talon(5)));
643 claw_writer.set_right_intake_talon(
644 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800645 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800646 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800647 ::std::thread claw_writer_thread(::std::ref(claw_writer));
648
649 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
650 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500651 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800652 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
653 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800654 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800655 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
656 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
657
658 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
659 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500660 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
661
662 // Wait forever. Not much else to do...
663 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
664
Austin Schuh010eb812014-10-25 18:06:49 -0700665 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800666
Austin Schuh010eb812014-10-25 18:06:49 -0700667 joystick_sender.Quit();
668 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500669 reader.Quit();
670 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800671 gyro_sender.Quit();
672 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500673
674 drivetrain_writer.Quit();
675 drivetrain_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500676 solenoid_writer.Quit();
677 solenoid_thread.join();
678
Austin Schuh010eb812014-10-25 18:06:49 -0700679 ::aos::Cleanup();
680 }
681};
682
Brian Silverman98f6ee22015-01-26 17:50:12 -0500683} // namespace wpilib
684} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800685
Brian Silverman98f6ee22015-01-26 17:50:12 -0500686
687START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);