blob: 758ecf126236c36fb1b16b94acc247ea6ba126b8 [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
130 void set_arm_left_index(::std::unique_ptr<DigitalSource> index) {
131 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
145 void set_arm_right_index(::std::unique_ptr<DigitalSource> index) {
146 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
160 void set_elevator_left_index(::std::unique_ptr<DigitalSource> index) {
161 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
175 void set_elevator_right_index(::std::unique_ptr<DigitalSource> index) {
176 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
190 void set_wrist_index(::std::unique_ptr<DigitalSource> index) {
191 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();
222 ds_ = DriverStation::GetInstance();
223
Brian Silverman335c20e2015-01-26 21:47:58 -0500224 wrist_encoder_.Start();
225 dma_synchronizer_->Start();
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800226 LOG(INFO, "Things are now started\n");
Austin Schuh010eb812014-10-25 18:06:49 -0700227
Brian Silverman2fe007c2014-12-28 12:20:01 -0800228 ::aos::SetCurrentThreadRealtimePriority(kPriority);
Austin Schuh010eb812014-10-25 18:06:49 -0700229 while (run_) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800230 ::aos::time::PhasedLoopXMS(5, 4000);
Austin Schuh010eb812014-10-25 18:06:49 -0700231 RunIteration();
Austin Schuh010eb812014-10-25 18:06:49 -0700232 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500233
234 wrist_encoder_.Stop();
Austin Schuh010eb812014-10-25 18:06:49 -0700235 }
236
237 void RunIteration() {
Brian Silverman699f0cb2015-02-05 19:45:01 -0500238 {
239 auto new_state = ::aos::robot_state.MakeMessage();
Austin Schuh010eb812014-10-25 18:06:49 -0700240
Austin Schuhfae69172015-02-20 22:11:26 -0800241 new_state->reader_pid = my_pid_;
Brian Silverman699f0cb2015-02-05 19:45:01 -0500242 new_state->outputs_enabled = ds_->IsSysActive();
243 new_state->browned_out = ds_->IsSysBrownedOut();
244
245 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
246 new_state->is_5v_active = ControllerPower::GetEnabled5V();
247 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
248 new_state->voltage_5v = ControllerPower::GetVoltage5V();
249
250 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
251 new_state->voltage_battery = ds_->GetBatteryVoltage();
252
Brian Silverman4d882aa2015-02-20 13:40:42 -0500253 LOG_STRUCT(DEBUG, "robot_state", *new_state);
254
Brian Silverman699f0cb2015-02-05 19:45:01 -0500255 new_state.Send();
Austin Schuh010eb812014-10-25 18:06:49 -0700256 }
257
Austin Schuh35d06612015-02-15 23:35:23 -0800258 {
259 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
260 drivetrain_message->right_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800261 -drivetrain_translate(right_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800262 drivetrain_message->left_encoder =
Austin Schuh2e0d2be2015-02-20 22:12:43 -0800263 drivetrain_translate(left_encoder_->GetRaw());
Austin Schuh35d06612015-02-15 23:35:23 -0800264
265 drivetrain_message.Send();
266 }
Brian Silverman335c20e2015-01-26 21:47:58 -0500267
268 dma_synchronizer_->RunIteration();
269
Philipp Schrader82c65072015-02-16 00:47:09 +0000270 const auto &values = constants::GetValues();
Brian Silverman5d712fc2015-02-15 03:39:31 -0500271
Brian Silverman335c20e2015-01-26 21:47:58 -0500272 {
273 auto fridge_message = fridge_queue.position.MakeMessage();
274 CopyPotAndIndexPosition(arm_left_encoder_, &fridge_message->arm.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500275 arm_translate, arm_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800276 values.fridge.left_arm_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500277 CopyPotAndIndexPosition(
278 arm_right_encoder_, &fridge_message->arm.right, arm_translate,
279 arm_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800280 values.fridge.right_arm_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500281 CopyPotAndIndexPosition(
282 elevator_left_encoder_, &fridge_message->elevator.left,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500283 elevator_translate, elevator_potentiometer_translate, false,
Austin Schuh6246c542015-02-16 02:59:09 -0800284 values.fridge.left_elevator_potentiometer_offset);
Brian Silverman5d712fc2015-02-15 03:39:31 -0500285 CopyPotAndIndexPosition(
286 elevator_right_encoder_, &fridge_message->elevator.right,
287 elevator_translate, elevator_potentiometer_translate, true,
Austin Schuh6246c542015-02-16 02:59:09 -0800288 values.fridge.right_elevator_potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500289 fridge_message.Send();
290 }
291
292 {
293 auto claw_message = claw_queue.position.MakeMessage();
294 CopyPotAndIndexPosition(wrist_encoder_, &claw_message->joint,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500295 claw_translate, claw_potentiometer_translate,
296 false, values.claw.potentiometer_offset);
Brian Silverman335c20e2015-01-26 21:47:58 -0500297 claw_message.Send();
298 }
Austin Schuh010eb812014-10-25 18:06:49 -0700299 }
300
301 void Quit() { run_ = false; }
302
303 private:
Brian Silverman335c20e2015-01-26 21:47:58 -0500304 static const int kPriority = 30;
305 static const int kInterruptPriority = 55;
306
Brian Silverman699f0cb2015-02-05 19:45:01 -0500307 int32_t my_pid_;
308 DriverStation *ds_;
309
Brian Silverman335c20e2015-01-26 21:47:58 -0500310 void CopyPotAndIndexPosition(
311 const DMAEncoderAndPotentiometer &encoder, PotAndIndexPosition *position,
312 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500313 ::std::function<double(double)> potentiometer_translate, bool reverse,
314 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500315 const double multiplier = reverse ? -1.0 : 1.0;
316 position->encoder =
317 multiplier * encoder_translate(encoder.polled_encoder_value());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500318 position->pot = multiplier * potentiometer_translate(
319 encoder.polled_potentiometer_voltage()) +
320 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500321 position->latched_encoder =
322 multiplier * encoder_translate(encoder.last_encoder_value());
323 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500324 multiplier *
325 potentiometer_translate(encoder.last_potentiometer_voltage()) +
326 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500327 position->index_pulses = encoder.index_posedge_count();
328 }
329
330 void CopyPotAndIndexPosition(
331 const InterruptEncoderAndPotentiometer &encoder,
332 PotAndIndexPosition *position,
333 ::std::function<double(int32_t)> encoder_translate,
Brian Silverman5d712fc2015-02-15 03:39:31 -0500334 ::std::function<double(double)> potentiometer_translate, bool reverse,
335 double potentiometer_offset) {
Brian Silverman335c20e2015-01-26 21:47:58 -0500336 const double multiplier = reverse ? -1.0 : 1.0;
337 position->encoder =
338 multiplier * encoder_translate(encoder.encoder()->GetRaw());
Brian Silverman5d712fc2015-02-15 03:39:31 -0500339 position->pot = multiplier * potentiometer_translate(
340 encoder.potentiometer()->GetVoltage()) +
341 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500342 position->latched_encoder =
343 multiplier * encoder_translate(encoder.last_encoder_value());
344 position->latched_pot =
Brian Silverman5d712fc2015-02-15 03:39:31 -0500345 multiplier *
346 potentiometer_translate(encoder.last_potentiometer_voltage()) +
347 potentiometer_offset;
Brian Silverman335c20e2015-01-26 21:47:58 -0500348 position->index_pulses = encoder.index_posedge_count();
349 }
350
Brian Silverman335c20e2015-01-26 21:47:58 -0500351 ::std::unique_ptr<DMASynchronizer> dma_synchronizer_;
352
353 DMAEncoderAndPotentiometer arm_left_encoder_, arm_right_encoder_,
354 elevator_left_encoder_, elevator_right_encoder_;
355
356 InterruptEncoderAndPotentiometer wrist_encoder_{kInterruptPriority};
357
Austin Schuh010eb812014-10-25 18:06:49 -0700358 ::std::unique_ptr<Encoder> left_encoder_;
359 ::std::unique_ptr<Encoder> right_encoder_;
Austin Schuh010eb812014-10-25 18:06:49 -0700360
Brian Silverman1f90d672015-01-26 20:20:45 -0500361 ::std::atomic<bool> run_{true};
Austin Schuh010eb812014-10-25 18:06:49 -0700362 DigitalGlitchFilter filter_;
363};
364
Brian Silvermand8f403a2014-12-13 19:12:04 -0500365class SolenoidWriter {
Austin Schuh010eb812014-10-25 18:06:49 -0700366 public:
Brian Silvermand8f403a2014-12-13 19:12:04 -0500367 SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm)
Daniel Pettiadf38432015-01-26 17:13:35 -0800368 : pcm_(pcm),
Austin Schuh17a2a492015-02-20 22:12:24 -0800369 fridge_(".frc971.control_loops.fridge_queue.output"),
370 claw_(".frc971.control_loops.claw_queue.output") {}
371
372 void set_pressure_switch(::std::unique_ptr<DigitalSource> pressure_switch) {
373 pressure_switch_ = ::std::move(pressure_switch);
374 }
375
376 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
377 compressor_relay_ = ::std::move(compressor_relay);
378 }
Brian Silvermand8f403a2014-12-13 19:12:04 -0500379
Daniel Pettiadf38432015-01-26 17:13:35 -0800380 void set_fridge_grabbers_top_front(::std::unique_ptr<BufferedSolenoid> s) {
381 fridge_grabbers_top_front_ = ::std::move(s);
Austin Schuh010eb812014-10-25 18:06:49 -0700382 }
383
Daniel Pettiadf38432015-01-26 17:13:35 -0800384 void set_fridge_grabbers_top_back(::std::unique_ptr<BufferedSolenoid> s) {
385 fridge_grabbers_top_back_ = ::std::move(s);
386 }
387
388 void set_fridge_grabbers_bottom_front(
389 ::std::unique_ptr<BufferedSolenoid> s) {
390 fridge_grabbers_bottom_front_ = ::std::move(s);
391 }
392
393 void set_fridge_grabbers_bottom_back(
394 ::std::unique_ptr<BufferedSolenoid> s) {
395 fridge_grabbers_bottom_back_ = ::std::move(s);
396 }
397
398 void set_claw_pinchers(::std::unique_ptr<BufferedSolenoid> s) {
399 claw_pinchers_ = ::std::move(s);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500400 }
Austin Schuh010eb812014-10-25 18:06:49 -0700401
Brian Silverman93936f72015-03-19 23:38:30 -0700402 void set_grabber_latch_release(::std::unique_ptr<BufferedSolenoid> s) {
403 grabber_latch_release_ = ::std::move(s);
404 }
405
406 void set_grabber_fold_up(::std::unique_ptr<BufferedSolenoid> s) {
407 grabber_fold_up_ = ::std::move(s);
408 }
409
Brian Silvermand8f403a2014-12-13 19:12:04 -0500410 void operator()() {
411 ::aos::SetCurrentThreadName("Solenoids");
412 ::aos::SetCurrentThreadRealtimePriority(30);
413
414 while (run_) {
415 ::aos::time::PhasedLoopXMS(20, 1000);
416
417 {
Daniel Pettiadf38432015-01-26 17:13:35 -0800418 fridge_.FetchLatest();
419 if (fridge_.get()) {
420 LOG_STRUCT(DEBUG, "solenoids", *fridge_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800421 fridge_grabbers_top_front_->Set(!fridge_->grabbers.top_front);
422 fridge_grabbers_top_back_->Set(!fridge_->grabbers.top_back);
423 fridge_grabbers_bottom_front_->Set(!fridge_->grabbers.bottom_front);
424 fridge_grabbers_bottom_back_->Set(!fridge_->grabbers.bottom_back);
Daniel Pettiadf38432015-01-26 17:13:35 -0800425 }
426 }
427
428 {
429 claw_.FetchLatest();
430 if (claw_.get()) {
431 LOG_STRUCT(DEBUG, "solenoids", *claw_);
Austin Schuh8ab58492015-02-22 21:32:29 -0800432 claw_pinchers_->Set(claw_->rollers_closed);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500433 }
434 }
435
Brian Silverman93936f72015-03-19 23:38:30 -0700436 ::aos::joystick_state.FetchLatest();
437 grabber_latch_release_->Set(::aos::joystick_state.get() != nullptr &&
438 ::aos::joystick_state->autonomous);
439 grabber_fold_up_->Set(::aos::joystick_state.get() != nullptr &&
440 ::aos::joystick_state->joysticks[1].buttons & 1);
441
Brian Silverman87541532015-03-19 23:35:12 -0700442 {
443 PneumaticsToLog to_log;
444 {
445 const bool compressor_on = !pressure_switch_->Get();
446 to_log.compressor_on = compressor_on;
447 if (compressor_on) {
448 compressor_relay_->Set(Relay::kForward);
449 } else {
450 compressor_relay_->Set(Relay::kOff);
451 }
452 }
453
454 pcm_->Flush();
455 to_log.read_solenoids = pcm_->GetAll();
456 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
457 }
Austin Schuh010eb812014-10-25 18:06:49 -0700458 }
459 }
460
Brian Silvermand8f403a2014-12-13 19:12:04 -0500461 void Quit() { run_ = false; }
Austin Schuh010eb812014-10-25 18:06:49 -0700462
Brian Silvermand8f403a2014-12-13 19:12:04 -0500463 private:
464 const ::std::unique_ptr<BufferedPcm> &pcm_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800465 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_front_;
466 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_top_back_;
467 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_front_;
468 ::std::unique_ptr<BufferedSolenoid> fridge_grabbers_bottom_back_;
469 ::std::unique_ptr<BufferedSolenoid> claw_pinchers_;
Brian Silverman93936f72015-03-19 23:38:30 -0700470 ::std::unique_ptr<BufferedSolenoid> grabber_latch_release_;
471 ::std::unique_ptr<BufferedSolenoid> grabber_fold_up_;
Austin Schuh17a2a492015-02-20 22:12:24 -0800472 ::std::unique_ptr<DigitalSource> pressure_switch_;
473 ::std::unique_ptr<Relay> compressor_relay_;
Austin Schuh010eb812014-10-25 18:06:49 -0700474
Daniel Pettiadf38432015-01-26 17:13:35 -0800475 ::aos::Queue<::frc971::control_loops::FridgeQueue::Output> fridge_;
476 ::aos::Queue<::frc971::control_loops::ClawQueue::Output> claw_;
Austin Schuh010eb812014-10-25 18:06:49 -0700477
Brian Silvermand8f403a2014-12-13 19:12:04 -0500478 ::std::atomic<bool> run_{true};
479};
480
Austin Schuhbb227f82015-09-06 15:27:52 -0700481class CanWriter : public LoopOutputHandler {
482 public:
483 CanWriter() : LoopOutputHandler(::aos::time::Time::InSeconds(0.10)) {}
484
485 void set_can_talon(::std::unique_ptr<Talon> t) {
486 can_talon_ = ::std::move(t);
487 }
488
489 private:
490 virtual void Read() override {
491 ::frc971::autonomous::can_control.FetchAnother();
492 }
493
494 virtual void Write() override {
495 auto &queue = ::frc971::autonomous::can_control;
496 LOG_STRUCT(DEBUG, "will output", *queue);
497 can_talon_->Set(queue->can_voltage / 12.0);
498 }
499
500 virtual void Stop() override {
501 LOG(WARNING, "Can output too old\n");
502 can_talon_->Disable();
503 }
504
505 ::std::unique_ptr<Talon> can_talon_;
506};
507
Brian Silvermand8f403a2014-12-13 19:12:04 -0500508class DrivetrainWriter : public LoopOutputHandler {
509 public:
510 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
511 left_drivetrain_talon_ = ::std::move(t);
Austin Schuh010eb812014-10-25 18:06:49 -0700512 }
513
Brian Silvermand8f403a2014-12-13 19:12:04 -0500514 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
515 right_drivetrain_talon_ = ::std::move(t);
516 }
Austin Schuh010eb812014-10-25 18:06:49 -0700517
Brian Silvermand8f403a2014-12-13 19:12:04 -0500518 private:
519 virtual void Read() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500520 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500521 }
522
523 virtual void Write() override {
Brian Silvermanada5f2c2015-02-01 02:41:14 -0500524 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500525 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuha004b0c2015-02-16 17:06:30 -0800526 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
527 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
Brian Silvermand8f403a2014-12-13 19:12:04 -0500528 }
529
530 virtual void Stop() override {
531 LOG(WARNING, "drivetrain output too old\n");
532 left_drivetrain_talon_->Disable();
533 right_drivetrain_talon_->Disable();
534 }
535
Austin Schuh010eb812014-10-25 18:06:49 -0700536 ::std::unique_ptr<Talon> left_drivetrain_talon_;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500537 ::std::unique_ptr<Talon> right_drivetrain_talon_;
538};
539
Daniel Pettiadf38432015-01-26 17:13:35 -0800540class FridgeWriter : public LoopOutputHandler {
541 public:
542 void set_left_arm_talon(::std::unique_ptr<Talon> t) {
543 left_arm_talon_ = ::std::move(t);
544 }
545
546 void set_right_arm_talon(::std::unique_ptr<Talon> t) {
547 right_arm_talon_ = ::std::move(t);
548 }
549
550 void set_left_elevator_talon(::std::unique_ptr<Talon> t) {
551 left_elevator_talon_ = ::std::move(t);
552 }
553
554 void set_right_elevator_talon(::std::unique_ptr<Talon> t) {
555 right_elevator_talon_ = ::std::move(t);
556 }
557
558 private:
559 virtual void Read() override {
560 ::frc971::control_loops::fridge_queue.output.FetchAnother();
561 }
562
563 virtual void Write() override {
564 auto &queue = ::frc971::control_loops::fridge_queue.output;
565 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh859a9302015-02-16 15:45:45 -0800566 left_arm_talon_->Set(queue->left_arm / 12.0);
567 right_arm_talon_->Set(-queue->right_arm / 12.0);
568 left_elevator_talon_->Set(queue->left_elevator / 12.0);
569 right_elevator_talon_->Set(-queue->right_elevator / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800570 }
571
572 virtual void Stop() override {
573 LOG(WARNING, "Fridge output too old.\n");
574 left_arm_talon_->Disable();
575 right_arm_talon_->Disable();
576 left_elevator_talon_->Disable();
577 right_elevator_talon_->Disable();
578 }
579
580 ::std::unique_ptr<Talon> left_arm_talon_;
581 ::std::unique_ptr<Talon> right_arm_talon_;
582 ::std::unique_ptr<Talon> left_elevator_talon_;
583 ::std::unique_ptr<Talon> right_elevator_talon_;
584};
585
586class ClawWriter : public LoopOutputHandler {
587 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800588 void set_left_intake_talon(::std::unique_ptr<Talon> t) {
589 left_intake_talon_ = ::std::move(t);
590 }
591
592 void set_right_intake_talon(::std::unique_ptr<Talon> t) {
593 right_intake_talon_ = ::std::move(t);
Daniel Pettiadf38432015-01-26 17:13:35 -0800594 }
595
596 void set_wrist_talon(::std::unique_ptr<Talon> t) {
597 wrist_talon_ = ::std::move(t);
598 }
599
600 private:
601 virtual void Read() override {
602 ::frc971::control_loops::claw_queue.output.FetchAnother();
603 }
604
605 virtual void Write() override {
606 auto &queue = ::frc971::control_loops::claw_queue.output;
607 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800608 left_intake_talon_->Set(queue->intake_voltage / 12.0);
Austin Schuh8a436e82015-02-16 23:31:28 -0800609 right_intake_talon_->Set(-queue->intake_voltage / 12.0);
610 wrist_talon_->Set(-queue->voltage / 12.0);
Daniel Pettiadf38432015-01-26 17:13:35 -0800611 }
612
613 virtual void Stop() override {
614 LOG(WARNING, "Claw output too old.\n");
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800615 left_intake_talon_->Disable();
616 right_intake_talon_->Disable();
Daniel Pettiadf38432015-01-26 17:13:35 -0800617 wrist_talon_->Disable();
618 }
619
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800620 ::std::unique_ptr<Talon> left_intake_talon_;
621 ::std::unique_ptr<Talon> right_intake_talon_;
Daniel Pettiadf38432015-01-26 17:13:35 -0800622 ::std::unique_ptr<Talon> wrist_talon_;
623};
624
Brian Silverman1f90d672015-01-26 20:20:45 -0500625// TODO(brian): Replace this with ::std::make_unique once all our toolchains
626// have support.
627template <class T, class... U>
628std::unique_ptr<T> make_unique(U &&... u) {
629 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
630}
631
Austin Schuh010eb812014-10-25 18:06:49 -0700632class WPILibRobot : public RobotBase {
633 public:
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800634 ::std::unique_ptr<Encoder> encoder(int index) {
635 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
636 Encoder::k4X);
637 }
Austin Schuh010eb812014-10-25 18:06:49 -0700638 virtual void StartCompetition() {
Brian Silvermand8f403a2014-12-13 19:12:04 -0500639 ::aos::InitNRT();
Brian Silverman2fe007c2014-12-28 12:20:01 -0800640 ::aos::SetCurrentThreadName("StartCompetition");
Brian Silvermand8f403a2014-12-13 19:12:04 -0500641
Brian Silverman98f6ee22015-01-26 17:50:12 -0500642 JoystickSender joystick_sender;
Austin Schuh010eb812014-10-25 18:06:49 -0700643 ::std::thread joystick_thread(::std::ref(joystick_sender));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800644 // TODO(austin): Compressor needs to use a spike.
Brian Silvermand8f403a2014-12-13 19:12:04 -0500645
Brian Silverman98f6ee22015-01-26 17:50:12 -0500646 SensorReader reader;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800647 LOG(INFO, "Creating the reader\n");
648 reader.set_arm_left_encoder(encoder(1));
649 reader.set_arm_left_index(make_unique<DigitalInput>(1));
650 reader.set_arm_left_potentiometer(make_unique<AnalogInput>(1));
651
652 reader.set_arm_right_encoder(encoder(5));
653 reader.set_arm_right_index(make_unique<DigitalInput>(5));
654 reader.set_arm_right_potentiometer(make_unique<AnalogInput>(5));
655
656 reader.set_elevator_left_encoder(encoder(0));
657 reader.set_elevator_left_index(make_unique<DigitalInput>(0));
658 reader.set_elevator_left_potentiometer(make_unique<AnalogInput>(0));
659
660 reader.set_elevator_right_encoder(encoder(4));
661 reader.set_elevator_right_index(make_unique<DigitalInput>(4));
662 reader.set_elevator_right_potentiometer(make_unique<AnalogInput>(4));
663
664 reader.set_wrist_encoder(encoder(6));
665 reader.set_wrist_index(make_unique<DigitalInput>(6));
666 reader.set_wrist_potentiometer(make_unique<AnalogInput>(6));
667
668 reader.set_left_encoder(encoder(2));
669 reader.set_right_encoder(encoder(3));
Brian Silverman335c20e2015-01-26 21:47:58 -0500670 reader.set_dma(make_unique<DMA>());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500671 ::std::thread reader_thread(::std::ref(reader));
672 GyroSender gyro_sender;
673 ::std::thread gyro_thread(::std::ref(gyro_sender));
674
675 DrivetrainWriter drivetrain_writer;
Brian Silvermand8f403a2014-12-13 19:12:04 -0500676 drivetrain_writer.set_left_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800677 ::std::unique_ptr<Talon>(new Talon(8)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500678 drivetrain_writer.set_right_drivetrain_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800679 ::std::unique_ptr<Talon>(new Talon(0)));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500680 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
681
Austin Schuhbb227f82015-09-06 15:27:52 -0700682 CanWriter can_writer;
683 can_writer.set_can_talon(::std::unique_ptr<Talon>(new Talon(9)));
684 ::std::thread can_writer_thread(::std::ref(can_writer));
685
Daniel Pettiadf38432015-01-26 17:13:35 -0800686 // TODO(sensors): Get real PWM output and relay numbers for the fridge and
687 // claw.
688 FridgeWriter fridge_writer;
689 fridge_writer.set_left_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800690 ::std::unique_ptr<Talon>(new Talon(6)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800691 fridge_writer.set_right_arm_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800692 ::std::unique_ptr<Talon>(new Talon(2)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800693 fridge_writer.set_left_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800694 ::std::unique_ptr<Talon>(new Talon(7)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800695 fridge_writer.set_right_elevator_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800696 ::std::unique_ptr<Talon>(new Talon(1)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800697 ::std::thread fridge_writer_thread(::std::ref(fridge_writer));
698
699 ClawWriter claw_writer;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800700 claw_writer.set_left_intake_talon(
701 ::std::unique_ptr<Talon>(new Talon(5)));
702 claw_writer.set_right_intake_talon(
703 ::std::unique_ptr<Talon>(new Talon(3)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800704 claw_writer.set_wrist_talon(
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800705 ::std::unique_ptr<Talon>(new Talon(4)));
Daniel Pettiadf38432015-01-26 17:13:35 -0800706 ::std::thread claw_writer_thread(::std::ref(claw_writer));
707
708 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
709 new ::frc971::wpilib::BufferedPcm());
Brian Silverman98f6ee22015-01-26 17:50:12 -0500710 SolenoidWriter solenoid_writer(pcm);
Austin Schuh17a2a492015-02-20 22:12:24 -0800711 solenoid_writer.set_fridge_grabbers_top_front(pcm->MakeSolenoid(0));
712 solenoid_writer.set_fridge_grabbers_top_back(pcm->MakeSolenoid(0));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800713 solenoid_writer.set_fridge_grabbers_bottom_front(pcm->MakeSolenoid(2));
Austin Schuh17a2a492015-02-20 22:12:24 -0800714 solenoid_writer.set_fridge_grabbers_bottom_back(pcm->MakeSolenoid(1));
715 solenoid_writer.set_claw_pinchers(pcm->MakeSolenoid(4));
Brian Silverman93936f72015-03-19 23:38:30 -0700716 solenoid_writer.set_grabber_latch_release(pcm->MakeSolenoid(7));
717 solenoid_writer.set_grabber_fold_up(pcm->MakeSolenoid(5));
Austin Schuh17a2a492015-02-20 22:12:24 -0800718
719 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
720 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
Brian Silvermand8f403a2014-12-13 19:12:04 -0500721 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
722
723 // Wait forever. Not much else to do...
724 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
725
Austin Schuh010eb812014-10-25 18:06:49 -0700726 LOG(ERROR, "Exiting WPILibRobot\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800727
Austin Schuh010eb812014-10-25 18:06:49 -0700728 joystick_sender.Quit();
729 joystick_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500730 reader.Quit();
731 reader_thread.join();
Brian Silverman07ec88e2014-12-28 00:13:08 -0800732 gyro_sender.Quit();
733 gyro_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500734
735 drivetrain_writer.Quit();
736 drivetrain_writer_thread.join();
Austin Schuhbb227f82015-09-06 15:27:52 -0700737 can_writer.Quit();
738 can_writer_thread.join();
Brian Silvermand8f403a2014-12-13 19:12:04 -0500739 solenoid_writer.Quit();
740 solenoid_thread.join();
741
Austin Schuh010eb812014-10-25 18:06:49 -0700742 ::aos::Cleanup();
743 }
744};
745
Brian Silverman98f6ee22015-01-26 17:50:12 -0500746} // namespace wpilib
747} // namespace frc971
Austin Schuhdb516032014-12-28 00:12:38 -0800748
Brian Silverman98f6ee22015-01-26 17:50:12 -0500749
750START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);