blob: 293be55bbe7acd6dd821e34e535ae2e47f00eb4b [file] [log] [blame]
Campbell Crowleyae6e8422017-02-05 12:38:50 -08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
6#include <chrono>
7#include <thread>
8#include <mutex>
9#include <functional>
10#include <array>
Brian Silverman50826c02017-02-18 14:40:25 -080011#include <cmath>
Campbell Crowleyae6e8422017-02-05 12:38:50 -080012
Brian Silverman50826c02017-02-18 14:40:25 -080013#include "Counter.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080014#include "Encoder.h"
15#include "VictorSP.h"
Austin Schuh6a8131b2017-04-08 15:39:22 -070016#include "Servo.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080017#include "Relay.h"
18#include "DriverStation.h"
19#include "AnalogInput.h"
20#include "Compressor.h"
21#include "DigitalGlitchFilter.h"
22#undef ERROR
23
24#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
26#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
30#include "aos/common/stl_mutex.h"
31#include "aos/linux_code/init.h"
32#include "aos/common/messages/robot_state.q.h"
33#include "aos/common/commonmath.h"
34
Philipp Schrader996a2a22017-02-22 05:02:48 +000035#include "frc971/autonomous/auto.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080036#include "frc971/control_loops/control_loops.q.h"
37#include "frc971/control_loops/drivetrain/drivetrain.q.h"
38#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080039#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080040
41#include "frc971/wpilib/wpilib_robot_base.h"
42#include "frc971/wpilib/joystick_sender.h"
43#include "frc971/wpilib/loop_output_handler.h"
44#include "frc971/wpilib/buffered_solenoid.h"
45#include "frc971/wpilib/buffered_pcm.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080046#include "frc971/wpilib/dma_edge_counting.h"
47#include "frc971/wpilib/interrupt_edge_counting.h"
48#include "frc971/wpilib/encoder_and_potentiometer.h"
49#include "frc971/wpilib/logging.q.h"
50#include "frc971/wpilib/wpilib_interface.h"
51#include "frc971/wpilib/pdp_fetcher.h"
52#include "frc971/wpilib/ADIS16448.h"
53#include "frc971/wpilib/dma.h"
54
55#ifndef M_PI
56#define M_PI 3.14159265358979323846
57#endif
58
59using ::frc971::control_loops::drivetrain_queue;
60using ::y2017::control_loops::superstructure_queue;
Brian Silverman052e69d2017-02-12 16:19:55 -080061using ::y2017::constants::Values;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080062
63namespace y2017 {
64namespace wpilib {
65namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080066
Campbell Crowleyae6e8422017-02-05 12:38:50 -080067constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080068
69// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
70// DMA stuff and then removing the * 2.0 in *_translate.
71// The low bit is direction.
72
73// TODO(brian): Replace this with ::std::make_unique once all our toolchains
74// have support.
75template <class T, class... U>
76std::unique_ptr<T> make_unique(U &&... u) {
77 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
78}
79
Brian Silverman052e69d2017-02-12 16:19:55 -080080// TODO(brian): Use ::std::max instead once we have C++14 so that can be
81// constexpr.
82template <typename T>
83constexpr T max(T a, T b) {
84 return (a > b) ? a : b;
85}
86template <typename T, typename... Rest>
87constexpr T max(T a, T b, T c, Rest... rest) {
88 return max(max(a, b), c, rest...);
89}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080090
Campbell Crowleyae6e8422017-02-05 12:38:50 -080091double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080092 return static_cast<double>(in) /
93 Values::kDrivetrainEncoderCountsPerRevolution *
94 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080095}
96
97double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080098 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
99 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800100}
101
Brian Silverman50826c02017-02-18 14:40:25 -0800102// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800103double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800104 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800105 (2 * M_PI /*radians*/);
106}
107
Brian Silverman052e69d2017-02-12 16:19:55 -0800108constexpr double kMaxFastEncoderPulsesPerSecond =
109 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
110 Values::kMaxShooterEncoderPulsesPerSecond);
111static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
112 "fast encoders are too fast");
113constexpr double kMaxMediumEncoderPulsesPerSecond =
114 max(Values::kMaxIntakeEncoderPulsesPerSecond,
115 Values::kMaxTurretEncoderPulsesPerSecond,
116 Values::kMaxIndexerEncoderPulsesPerSecond);
117static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
118 "medium encoders are too fast");
119constexpr double kMaxSlowEncoderPulsesPerSecond =
120 Values::kMaxHoodEncoderPulsesPerSecond;
121static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
122 "slow encoders are too fast");
Brianef030df2017-03-05 15:06:04 -0800123static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond,
124 "slow encoders are faster than medium?");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800125
Brian Silverman50826c02017-02-18 14:40:25 -0800126// Handles reading the duty cycle on a DigitalInput.
127class DutyCycleReader {
128 public:
129 void set_input(::std::unique_ptr<DigitalInput> input) {
130 high_counter_.reset(new Counter(input.get()));
131 high_counter_->SetMaxPeriod(kMaxPeriod);
132 high_counter_->SetSemiPeriodMode(true);
133
134 period_length_counter_.reset(new Counter(input.get()));
135 period_length_counter_->SetMaxPeriod(kMaxPeriod);
136 period_length_counter_->SetUpSourceEdge(true, false);
137
138 input_ = ::std::move(input);
139 }
140
141 double Read() const {
142 const double high_time = high_counter_->GetPeriod();
143 const double period_length = period_length_counter_->GetPeriod();
144 if (!::std::isfinite(high_time) || !::std::isfinite(period_length)) {
145 return ::std::numeric_limits<double>::quiet_NaN();
146 }
147 return high_time / period_length;
148 }
149
150 private:
151 static constexpr ::std::chrono::nanoseconds kNominalPeriod =
152 ::std::chrono::microseconds(4096);
153 static constexpr double kMaxPeriod =
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800154 (::std::chrono::duration_cast<::std::chrono::duration<double>>(
155 kNominalPeriod) *
156 2).count();
Brian Silverman50826c02017-02-18 14:40:25 -0800157
158 ::std::unique_ptr<Counter> high_counter_, period_length_counter_;
159 ::std::unique_ptr<DigitalInput> input_;
160};
161
162class AbsoluteEncoderAndPotentiometer {
163 public:
164 void set_absolute_pwm(::std::unique_ptr<DigitalInput> input) {
165 duty_cycle_.set_input(::std::move(input));
166 }
167
168 void set_encoder(::std::unique_ptr<Encoder> encoder) {
169 encoder_ = ::std::move(encoder);
170 }
171
172 void set_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
173 potentiometer_ = ::std::move(potentiometer);
174 }
175
176 double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); }
177
178 int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); }
179
180 double ReadPotentiometerVoltage() const {
181 return potentiometer_->GetVoltage();
182 }
183
184 private:
185 DutyCycleReader duty_cycle_;
186 ::std::unique_ptr<Encoder> encoder_;
187 ::std::unique_ptr<AnalogInput> potentiometer_;
188};
189
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800190// Class to send position messages with sensor readings to our loops.
191class SensorReader {
192 public:
193 SensorReader() {
Brian Silverman052e69d2017-02-12 16:19:55 -0800194 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800195 // we should ever see.
Brian Silverman052e69d2017-02-12 16:19:55 -0800196 fast_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800197 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800198 kMaxFastEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800199 0.5));
Brian Silverman052e69d2017-02-12 16:19:55 -0800200 medium_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800201 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800202 kMaxMediumEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800203 0.5));
Brianef030df2017-03-05 15:06:04 -0800204 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800205 }
206
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800207 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800208 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800209 drivetrain_left_encoder_ = ::std::move(encoder);
210 }
211
212 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800213 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800214 drivetrain_right_encoder_ = ::std::move(encoder);
215 }
216
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800217 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800218 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800219 shooter_encoder_ = ::std::move(encoder);
220 }
221
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800222 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800223 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800224 intake_encoder_.set_encoder(::std::move(encoder));
225 }
226
227 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
228 intake_encoder_.set_potentiometer(::std::move(potentiometer));
229 }
230
Brian Silverman50826c02017-02-18 14:40:25 -0800231 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
232 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800233 }
234
Brian Silverman052e69d2017-02-12 16:19:55 -0800235 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
236 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800237 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800238 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800239 }
240
Brianef030df2017-03-05 15:06:04 -0800241 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
242 hall_filter_.Add(input.get());
243 indexer_counter_.set_input(input.get());
244 indexer_hall_ = ::std::move(input);
245 }
246
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800247 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800248 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800249 turret_counter_.set_encoder(encoder.get());
250 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800251 }
252
Brianef030df2017-03-05 15:06:04 -0800253 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
254 hall_filter_.Add(input.get());
255 turret_counter_.set_input(input.get());
256 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800257 }
258
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800259 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800260 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800261 hood_encoder_.set_encoder(::std::move(encoder));
262 }
263
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800264 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800265 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800266 hood_encoder_.set_index(::std::move(index));
267 }
268
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800269 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
270 autonomous_modes_.at(i) = ::std::move(sensor);
271 }
272
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800273 // All of the DMA-related set_* calls must be made before this, and it doesn't
274 // hurt to do all of them.
275 void set_dma(::std::unique_ptr<DMA> dma) {
276 dma_synchronizer_.reset(
277 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brianef030df2017-03-05 15:06:04 -0800278 dma_synchronizer_->Add(&indexer_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800279 dma_synchronizer_->Add(&hood_encoder_);
Brianef030df2017-03-05 15:06:04 -0800280 dma_synchronizer_->Add(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800281 }
282
283 void operator()() {
284 ::aos::SetCurrentThreadName("SensorReader");
285
286 my_pid_ = getpid();
287 ds_ =
288 &DriverStation::GetInstance();
289
290 dma_synchronizer_->Start();
291
292 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Brian Silverman7ba8f1a2017-02-24 20:01:39 -0800293 ::std::chrono::milliseconds(0));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800294
295 ::aos::SetCurrentThreadRealtimePriority(40);
296 while (run_) {
297 {
298 const int iterations = phased_loop.SleepUntilNext();
299 if (iterations != 1) {
300 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
301 }
302 }
303 RunIteration();
304 }
305 }
306
307 void RunIteration() {
308 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
309
310 const auto values = constants::GetValues();
311
312 {
313 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
314 drivetrain_message->right_encoder =
315 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800316 drivetrain_message->right_speed =
317 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
318
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800319 drivetrain_message->left_encoder =
320 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
321 drivetrain_message->left_speed =
322 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800323
324 drivetrain_message.Send();
325 }
326
327 dma_synchronizer_->RunIteration();
328
329 {
330 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800331 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800332 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800333 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800334 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800335
Brianef030df2017-03-05 15:06:04 -0800336 CopyPosition(indexer_counter_, &superstructure_message->column.indexer,
337 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800338 Values::kIndexerEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800339
Brian Silverman50826c02017-02-18 14:40:25 -0800340 superstructure_message->theta_shooter =
341 encoder_translate(shooter_encoder_->GetRaw(),
342 Values::kShooterEncoderCountsPerRevolution,
343 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800344
Brian Silverman50826c02017-02-18 14:40:25 -0800345 CopyPosition(hood_encoder_, &superstructure_message->hood,
346 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800347 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800348
Brianef030df2017-03-05 15:06:04 -0800349 CopyPosition(turret_counter_, &superstructure_message->column.turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800350 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800351 Values::kTurretEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800352
353 superstructure_message.Send();
354 }
355
356 {
Philipp Schrader996a2a22017-02-22 05:02:48 +0000357 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800358 auto_mode_message->mode = 0;
359 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
360 if (autonomous_modes_[i]->Get()) {
361 auto_mode_message->mode |= 1 << i;
362 }
363 }
364 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
365 auto_mode_message.Send();
366 }
367 }
368
369 void Quit() { run_ = false; }
370
371 private:
Brian Silverman50826c02017-02-18 14:40:25 -0800372 double encoder_translate(int32_t value, double counts_per_revolution,
373 double ratio) {
374 return static_cast<double>(value) / counts_per_revolution * ratio *
375 (2.0 * M_PI);
376 }
377
Brian Silverman7cce2d32017-02-19 21:48:48 -0800378 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
379 ::frc971::IndexPosition *position,
Brian Silverman50826c02017-02-18 14:40:25 -0800380 double encoder_counts_per_revolution, double encoder_ratio,
Brian Silverman7cce2d32017-02-19 21:48:48 -0800381 bool reverse) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800382 const double multiplier = reverse ? -1.0 : 1.0;
383 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800384 multiplier * encoder_translate(encoder.polled_encoder_value(),
385 encoder_counts_per_revolution,
386 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800387 position->latched_encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800388 multiplier * encoder_translate(encoder.last_encoder_value(),
389 encoder_counts_per_revolution,
390 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800391 position->index_pulses = encoder.index_posedge_count();
392 }
393
Brian Silverman50826c02017-02-18 14:40:25 -0800394 void CopyPosition(const AbsoluteEncoderAndPotentiometer &encoder,
395 ::frc971::PotAndAbsolutePosition *position,
396 double encoder_counts_per_revolution, double encoder_ratio,
397 ::std::function<double(double)> potentiometer_translate,
398 bool reverse, double pot_offset) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800399 const double multiplier = reverse ? -1.0 : 1.0;
400 position->pot = multiplier * potentiometer_translate(
Brian Silverman50826c02017-02-18 14:40:25 -0800401 encoder.ReadPotentiometerVoltage()) +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800402 pot_offset;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800403 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800404 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
405 encoder_counts_per_revolution,
406 encoder_ratio);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800407
408 position->absolute_encoder =
409 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
410 : encoder.ReadAbsoluteEncoder()) *
411 encoder_ratio * (2.0 * M_PI);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800412 }
413
Brianef030df2017-03-05 15:06:04 -0800414 void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter,
415 ::frc971::HallEffectAndPosition *position,
416 double encoder_counts_per_revolution, double encoder_ratio,
417 bool reverse) {
418 const double multiplier = reverse ? -1.0 : 1.0;
419 position->position =
420 multiplier * encoder_translate(counter.polled_encoder(),
421 encoder_counts_per_revolution,
422 encoder_ratio);
423 position->current = !counter.polled_value();
424 position->posedge_count = counter.negative_count();
425 position->negedge_count = counter.positive_count();
426 position->posedge_value =
427 multiplier * encoder_translate(counter.last_negative_encoder_value(),
428 encoder_counts_per_revolution,
429 encoder_ratio);
430 position->negedge_value =
431 multiplier * encoder_translate(counter.last_positive_encoder_value(),
432 encoder_counts_per_revolution,
433 encoder_ratio);
434 }
435
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800436 int32_t my_pid_;
437 DriverStation *ds_;
438
439 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
440
Brian Silverman052e69d2017-02-12 16:19:55 -0800441 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
Brianef030df2017-03-05 15:06:04 -0800442 hall_filter_;
Brian Silverman052e69d2017-02-12 16:19:55 -0800443
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800444 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
445 drivetrain_right_encoder_;
446
Brian Silverman50826c02017-02-18 14:40:25 -0800447 AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800448
Brian Silverman052e69d2017-02-12 16:19:55 -0800449 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800450 ::std::unique_ptr<DigitalInput> indexer_hall_;
451 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800452
Brianef030df2017-03-05 15:06:04 -0800453 ::std::unique_ptr<Encoder> turret_encoder_;
454 ::std::unique_ptr<DigitalInput> turret_hall_;
455 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
456
Brian Silverman7cce2d32017-02-19 21:48:48 -0800457 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800458 ::std::unique_ptr<Encoder> shooter_encoder_;
459
460 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
461
462 ::std::atomic<bool> run_{true};
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800463};
464
Adam Snaidere0554ef2017-03-11 23:02:45 -0800465class SolenoidWriter {
466 public:
467 SolenoidWriter()
468 : superstructure_(".y2017.control_loops.superstructure_queue.output") {}
469
470 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
471
472 void set_lights(
473 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
474 lights_ = ::std::move(s);
475 }
476
Austin Schuhc587c882017-03-29 21:33:10 -0700477 void set_rgb_light(
478 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
479 rgb_lights_ = ::std::move(s);
480 }
481
Adam Snaidere0554ef2017-03-11 23:02:45 -0800482 void operator()() {
483 ::aos::SetCurrentThreadName("Solenoids");
484 ::aos::SetCurrentThreadRealtimePriority(27);
485
486 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
487 ::std::chrono::milliseconds(1));
488
489 while (run_) {
490 {
491 const int iterations = phased_loop.SleepUntilNext();
492 if (iterations != 1) {
493 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
494 }
495 }
496
497 {
498 superstructure_.FetchLatest();
499 if (superstructure_.get()) {
500 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
501 lights_->Set(superstructure_->lights_on);
Austin Schuhc587c882017-03-29 21:33:10 -0700502 rgb_lights_->Set(superstructure_->red_light_on |
503 superstructure_->green_light_on |
504 superstructure_->blue_light_on);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800505 }
506 }
507
508 pcm_.Flush();
509 }
510 }
511
512 void Quit() { run_ = false; }
513
514 private:
515 ::frc971::wpilib::BufferedPcm pcm_;
516
Austin Schuhc587c882017-03-29 21:33:10 -0700517 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800518
519 ::aos::Queue<
520 ::y2017::control_loops::SuperstructureQueue::Output>
521 superstructure_;
522
523 ::std::atomic<bool> run_{true};
524};
525
526
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800527class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
528 public:
529 void set_drivetrain_left_victor(::std::unique_ptr<VictorSP> t) {
530 drivetrain_left_victor_ = ::std::move(t);
531 }
532
533 void set_drivetrain_right_victor(::std::unique_ptr<VictorSP> t) {
534 drivetrain_right_victor_ = ::std::move(t);
535 }
536
537 private:
538 virtual void Read() override {
539 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
540 }
541
542 virtual void Write() override {
543 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
544 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh410e3812017-02-21 16:44:03 -0800545 drivetrain_left_victor_->SetSpeed(-queue->left_voltage / 12.0);
546 drivetrain_right_victor_->SetSpeed(queue->right_voltage / 12.0);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800547 }
548
549 virtual void Stop() override {
550 LOG(WARNING, "drivetrain output too old\n");
551 drivetrain_left_victor_->SetDisabled();
552 drivetrain_right_victor_->SetDisabled();
553 }
554
555 ::std::unique_ptr<VictorSP> drivetrain_left_victor_, drivetrain_right_victor_;
556};
557
558class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
559 public:
560 void set_intake_victor(::std::unique_ptr<VictorSP> t) {
561 intake_victor_ = ::std::move(t);
562 }
563 void set_intake_rollers_victor(::std::unique_ptr<VictorSP> t) {
564 intake_rollers_victor_ = ::std::move(t);
565 }
566
Brian Silverman052e69d2017-02-12 16:19:55 -0800567 void set_indexer_victor(::std::unique_ptr<VictorSP> t) {
568 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800569 }
Brian Silverman052e69d2017-02-12 16:19:55 -0800570 void set_indexer_roller_victor(::std::unique_ptr<VictorSP> t) {
571 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800572 }
573
Austin Schuh6a8131b2017-04-08 15:39:22 -0700574 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
575 gear_servo_ = ::std::move(t);
576 }
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800577 void set_shooter_victor(::std::unique_ptr<VictorSP> t) {
578 shooter_victor_ = ::std::move(t);
579 }
580 void set_turret_victor(::std::unique_ptr<VictorSP> t) {
581 turret_victor_ = ::std::move(t);
582 }
583 void set_hood_victor(::std::unique_ptr<VictorSP> t) {
584 hood_victor_ = ::std::move(t);
585 }
586
Austin Schuhc587c882017-03-29 21:33:10 -0700587 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
588 red_light_ = ::std::move(t);
589 }
590 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
591 green_light_ = ::std::move(t);
592 }
593 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
594 blue_light_ = ::std::move(t);
595 }
596
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800597 private:
598 virtual void Read() override {
599 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
600 }
601
602 virtual void Write() override {
603 auto &queue = ::y2017::control_loops::superstructure_queue.output;
604 LOG_STRUCT(DEBUG, "will output", *queue);
605 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
606 -kMaxBringupPower, kMaxBringupPower) /
607 12.0);
608 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800609 indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0);
610 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0);
Austin Schuh410e3812017-02-21 16:44:03 -0800611 turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800612 -kMaxBringupPower, kMaxBringupPower) /
613 12.0);
614 hood_victor_->SetSpeed(
615 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
616 12.0);
617 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700618
619 red_light_->Set(queue->red_light_on);
620 green_light_->Set(queue->green_light_on);
621 blue_light_->Set(queue->blue_light_on);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700622
623 gear_servo_->Set(queue->gear_servo);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800624 }
625
626 virtual void Stop() override {
627 LOG(WARNING, "Superstructure output too old.\n");
628 intake_victor_->SetDisabled();
629 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800630 indexer_victor_->SetDisabled();
631 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800632 turret_victor_->SetDisabled();
633 hood_victor_->SetDisabled();
634 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700635
Austin Schuh6a8131b2017-04-08 15:39:22 -0700636 gear_servo_->SetOffline();
637
Austin Schuhc587c882017-03-29 21:33:10 -0700638 red_light_->Set(true);
639 green_light_->Set(true);
640 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800641 }
642
643 ::std::unique_ptr<VictorSP> intake_victor_, intake_rollers_victor_,
Brian Silverman052e69d2017-02-12 16:19:55 -0800644 indexer_victor_, indexer_roller_victor_, shooter_victor_,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800645 turret_victor_, hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700646
Austin Schuh6a8131b2017-04-08 15:39:22 -0700647 ::std::unique_ptr<::frc::Servo> gear_servo_;
648
Austin Schuhc587c882017-03-29 21:33:10 -0700649 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800650};
651
652class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
653 public:
654 ::std::unique_ptr<Encoder> make_encoder(int index) {
655 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
656 Encoder::k4X);
657 }
658
659 void Run() override {
660 ::aos::InitNRT();
661 ::aos::SetCurrentThreadName("StartCompetition");
662
663 ::frc971::wpilib::JoystickSender joystick_sender;
664 ::std::thread joystick_thread(::std::ref(joystick_sender));
665
666 ::frc971::wpilib::PDPFetcher pdp_fetcher;
667 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
668 SensorReader reader;
669
670 // TODO(campbell): Update port numbers
671 reader.set_drivetrain_left_encoder(make_encoder(0));
672 reader.set_drivetrain_right_encoder(make_encoder(1));
673
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800674 reader.set_intake_encoder(make_encoder(3));
Brian Silverman50826c02017-02-18 14:40:25 -0800675 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800676 reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800677
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800678 reader.set_indexer_encoder(make_encoder(5));
Brianef030df2017-03-05 15:06:04 -0800679 reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800680
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800681 reader.set_turret_encoder(make_encoder(6));
Brianef030df2017-03-05 15:06:04 -0800682 reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800683
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800684 reader.set_hood_encoder(make_encoder(4));
685 reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800686
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800687 reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800688
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800689 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
690 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
691 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
692 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800693
694 reader.set_dma(make_unique<DMA>());
695 ::std::thread reader_thread(::std::ref(reader));
696
Brian Silvermanb4439852017-02-24 19:49:09 -0800697 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800698 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700699 imu.SetDummySPI(SPI::Port::kOnboardCS2);
700 auto imu_reset = make_unique<DigitalOutput>(6);
701 imu.set_reset(imu_reset.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800702 ::std::thread imu_thread(::std::ref(imu));
703
704 DrivetrainWriter drivetrain_writer;
705 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800706 ::std::unique_ptr<VictorSP>(new VictorSP(7)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800707 drivetrain_writer.set_drivetrain_right_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800708 ::std::unique_ptr<VictorSP>(new VictorSP(3)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800709 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
710
711 SuperstructureWriter superstructure_writer;
712 superstructure_writer.set_intake_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800713 ::std::unique_ptr<VictorSP>(new VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800714 superstructure_writer.set_intake_rollers_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800715 ::std::unique_ptr<VictorSP>(new VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800716 superstructure_writer.set_indexer_victor(
717 ::std::unique_ptr<VictorSP>(new VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800718 superstructure_writer.set_indexer_roller_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800719 ::std::unique_ptr<VictorSP>(new VictorSP(5)));
720 superstructure_writer.set_turret_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800721 ::std::unique_ptr<VictorSP>(new VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800722 superstructure_writer.set_hood_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800723 ::std::unique_ptr<VictorSP>(new VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800724 superstructure_writer.set_shooter_victor(
725 ::std::unique_ptr<VictorSP>(new VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700726
Austin Schuh6a8131b2017-04-08 15:39:22 -0700727 superstructure_writer.set_gear_servo(
728 ::std::unique_ptr<Servo>(new Servo(0)));
729
Austin Schuhc587c882017-03-29 21:33:10 -0700730 superstructure_writer.set_red_light(
731 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
732 superstructure_writer.set_green_light(
733 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
734 superstructure_writer.set_blue_light(
735 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
736
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800737 ::std::thread superstructure_writer_thread(
738 ::std::ref(superstructure_writer));
739
Adam Snaidere0554ef2017-03-11 23:02:45 -0800740 SolenoidWriter solenoid_writer;
741 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700742 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800743
744 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
745
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800746 // Wait forever. Not much else to do...
747 while (true) {
748 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
749 if (r != 0) {
750 PLOG(WARNING, "infinite select failed");
751 } else {
752 PLOG(WARNING, "infinite select succeeded??\n");
753 }
754 }
755
756 LOG(ERROR, "Exiting WPILibRobot\n");
757
758 joystick_sender.Quit();
759 joystick_thread.join();
760 pdp_fetcher.Quit();
761 pdp_fetcher_thread.join();
762 reader.Quit();
763 reader_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800764 imu.Quit();
765 imu_thread.join();
766
767 drivetrain_writer.Quit();
768 drivetrain_writer_thread.join();
769 superstructure_writer.Quit();
770 superstructure_writer_thread.join();
771
772 ::aos::Cleanup();
773 }
774};
775
Brian Silverman052e69d2017-02-12 16:19:55 -0800776} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800777} // namespace wpilib
778} // namespace y2017
779
780AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);