blob: 65b22467b702565f842cb7a2a6e47d1abbf712b4 [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"
16#include "Relay.h"
17#include "DriverStation.h"
18#include "AnalogInput.h"
19#include "Compressor.h"
20#include "DigitalGlitchFilter.h"
21#undef ERROR
22
23#include "aos/common/logging/logging.h"
24#include "aos/common/logging/queue_logging.h"
25#include "aos/common/time.h"
26#include "aos/common/util/log_interval.h"
27#include "aos/common/util/phased_loop.h"
28#include "aos/common/util/wrapping_counter.h"
29#include "aos/common/stl_mutex.h"
30#include "aos/linux_code/init.h"
31#include "aos/common/messages/robot_state.q.h"
32#include "aos/common/commonmath.h"
33
Philipp Schrader996a2a22017-02-22 05:02:48 +000034#include "frc971/autonomous/auto.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080035#include "frc971/control_loops/control_loops.q.h"
36#include "frc971/control_loops/drivetrain/drivetrain.q.h"
37#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080038#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080039
40#include "frc971/wpilib/wpilib_robot_base.h"
41#include "frc971/wpilib/joystick_sender.h"
42#include "frc971/wpilib/loop_output_handler.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/buffered_pcm.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080045#include "frc971/wpilib/dma_edge_counting.h"
46#include "frc971/wpilib/interrupt_edge_counting.h"
47#include "frc971/wpilib/encoder_and_potentiometer.h"
48#include "frc971/wpilib/logging.q.h"
49#include "frc971/wpilib/wpilib_interface.h"
50#include "frc971/wpilib/pdp_fetcher.h"
51#include "frc971/wpilib/ADIS16448.h"
52#include "frc971/wpilib/dma.h"
53
54#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
57
58using ::frc971::control_loops::drivetrain_queue;
59using ::y2017::control_loops::superstructure_queue;
Brian Silverman052e69d2017-02-12 16:19:55 -080060using ::y2017::constants::Values;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080061
62namespace y2017 {
63namespace wpilib {
64namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080065
Campbell Crowleyae6e8422017-02-05 12:38:50 -080066constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080067
68// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
69// DMA stuff and then removing the * 2.0 in *_translate.
70// The low bit is direction.
71
72// TODO(brian): Replace this with ::std::make_unique once all our toolchains
73// have support.
74template <class T, class... U>
75std::unique_ptr<T> make_unique(U &&... u) {
76 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
77}
78
Brian Silverman052e69d2017-02-12 16:19:55 -080079// TODO(brian): Use ::std::max instead once we have C++14 so that can be
80// constexpr.
81template <typename T>
82constexpr T max(T a, T b) {
83 return (a > b) ? a : b;
84}
85template <typename T, typename... Rest>
86constexpr T max(T a, T b, T c, Rest... rest) {
87 return max(max(a, b), c, rest...);
88}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080089
Campbell Crowleyae6e8422017-02-05 12:38:50 -080090double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080091 return static_cast<double>(in) /
92 Values::kDrivetrainEncoderCountsPerRevolution *
93 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080094}
95
96double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080097 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
98 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080099}
100
Brian Silverman50826c02017-02-18 14:40:25 -0800101// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800102double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800103 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800104 (2 * M_PI /*radians*/);
105}
106
Brian Silverman052e69d2017-02-12 16:19:55 -0800107constexpr double kMaxFastEncoderPulsesPerSecond =
108 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
109 Values::kMaxShooterEncoderPulsesPerSecond);
110static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
111 "fast encoders are too fast");
112constexpr double kMaxMediumEncoderPulsesPerSecond =
113 max(Values::kMaxIntakeEncoderPulsesPerSecond,
114 Values::kMaxTurretEncoderPulsesPerSecond,
115 Values::kMaxIndexerEncoderPulsesPerSecond);
116static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
117 "medium encoders are too fast");
118constexpr double kMaxSlowEncoderPulsesPerSecond =
119 Values::kMaxHoodEncoderPulsesPerSecond;
120static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
121 "slow encoders are too fast");
Brianef030df2017-03-05 15:06:04 -0800122static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond,
123 "slow encoders are faster than medium?");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800124
Brian Silverman50826c02017-02-18 14:40:25 -0800125// Handles reading the duty cycle on a DigitalInput.
126class DutyCycleReader {
127 public:
128 void set_input(::std::unique_ptr<DigitalInput> input) {
129 high_counter_.reset(new Counter(input.get()));
130 high_counter_->SetMaxPeriod(kMaxPeriod);
131 high_counter_->SetSemiPeriodMode(true);
132
133 period_length_counter_.reset(new Counter(input.get()));
134 period_length_counter_->SetMaxPeriod(kMaxPeriod);
135 period_length_counter_->SetUpSourceEdge(true, false);
136
137 input_ = ::std::move(input);
138 }
139
140 double Read() const {
141 const double high_time = high_counter_->GetPeriod();
142 const double period_length = period_length_counter_->GetPeriod();
143 if (!::std::isfinite(high_time) || !::std::isfinite(period_length)) {
144 return ::std::numeric_limits<double>::quiet_NaN();
145 }
146 return high_time / period_length;
147 }
148
149 private:
150 static constexpr ::std::chrono::nanoseconds kNominalPeriod =
151 ::std::chrono::microseconds(4096);
152 static constexpr double kMaxPeriod =
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800153 (::std::chrono::duration_cast<::std::chrono::duration<double>>(
154 kNominalPeriod) *
155 2).count();
Brian Silverman50826c02017-02-18 14:40:25 -0800156
157 ::std::unique_ptr<Counter> high_counter_, period_length_counter_;
158 ::std::unique_ptr<DigitalInput> input_;
159};
160
161class AbsoluteEncoderAndPotentiometer {
162 public:
163 void set_absolute_pwm(::std::unique_ptr<DigitalInput> input) {
164 duty_cycle_.set_input(::std::move(input));
165 }
166
167 void set_encoder(::std::unique_ptr<Encoder> encoder) {
168 encoder_ = ::std::move(encoder);
169 }
170
171 void set_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
172 potentiometer_ = ::std::move(potentiometer);
173 }
174
175 double ReadAbsoluteEncoder() const { return duty_cycle_.Read(); }
176
177 int32_t ReadRelativeEncoder() const { return encoder_->GetRaw(); }
178
179 double ReadPotentiometerVoltage() const {
180 return potentiometer_->GetVoltage();
181 }
182
183 private:
184 DutyCycleReader duty_cycle_;
185 ::std::unique_ptr<Encoder> encoder_;
186 ::std::unique_ptr<AnalogInput> potentiometer_;
187};
188
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800189// Class to send position messages with sensor readings to our loops.
190class SensorReader {
191 public:
192 SensorReader() {
Brian Silverman052e69d2017-02-12 16:19:55 -0800193 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800194 // we should ever see.
Brian Silverman052e69d2017-02-12 16:19:55 -0800195 fast_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800196 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800197 kMaxFastEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800198 0.5));
Brian Silverman052e69d2017-02-12 16:19:55 -0800199 medium_encoder_filter_.SetPeriodNanoSeconds(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800200 static_cast<int>(1 / 4.0 /* built-in tolerance */ /
Brian Silverman052e69d2017-02-12 16:19:55 -0800201 kMaxMediumEncoderPulsesPerSecond * 1e9 +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800202 0.5));
Brianef030df2017-03-05 15:06:04 -0800203 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800204 }
205
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800206 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800207 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800208 drivetrain_left_encoder_ = ::std::move(encoder);
209 }
210
211 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800212 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800213 drivetrain_right_encoder_ = ::std::move(encoder);
214 }
215
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800216 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800217 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800218 shooter_encoder_ = ::std::move(encoder);
219 }
220
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800221 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800222 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800223 intake_encoder_.set_encoder(::std::move(encoder));
224 }
225
226 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
227 intake_encoder_.set_potentiometer(::std::move(potentiometer));
228 }
229
Brian Silverman50826c02017-02-18 14:40:25 -0800230 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
231 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800232 }
233
Brian Silverman052e69d2017-02-12 16:19:55 -0800234 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
235 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800236 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800237 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800238 }
239
Brianef030df2017-03-05 15:06:04 -0800240 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
241 hall_filter_.Add(input.get());
242 indexer_counter_.set_input(input.get());
243 indexer_hall_ = ::std::move(input);
244 }
245
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800246 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800247 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800248 turret_counter_.set_encoder(encoder.get());
249 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800250 }
251
Brianef030df2017-03-05 15:06:04 -0800252 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
253 hall_filter_.Add(input.get());
254 turret_counter_.set_input(input.get());
255 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800256 }
257
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800258 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800259 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800260 hood_encoder_.set_encoder(::std::move(encoder));
261 }
262
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800263 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800264 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800265 hood_encoder_.set_index(::std::move(index));
266 }
267
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800268 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
269 autonomous_modes_.at(i) = ::std::move(sensor);
270 }
271
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800272 // All of the DMA-related set_* calls must be made before this, and it doesn't
273 // hurt to do all of them.
274 void set_dma(::std::unique_ptr<DMA> dma) {
275 dma_synchronizer_.reset(
276 new ::frc971::wpilib::DMASynchronizer(::std::move(dma)));
Brianef030df2017-03-05 15:06:04 -0800277 dma_synchronizer_->Add(&indexer_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800278 dma_synchronizer_->Add(&hood_encoder_);
Brianef030df2017-03-05 15:06:04 -0800279 dma_synchronizer_->Add(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800280 }
281
282 void operator()() {
283 ::aos::SetCurrentThreadName("SensorReader");
284
285 my_pid_ = getpid();
286 ds_ =
287 &DriverStation::GetInstance();
288
289 dma_synchronizer_->Start();
290
291 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Brian Silverman7ba8f1a2017-02-24 20:01:39 -0800292 ::std::chrono::milliseconds(0));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800293
294 ::aos::SetCurrentThreadRealtimePriority(40);
295 while (run_) {
296 {
297 const int iterations = phased_loop.SleepUntilNext();
298 if (iterations != 1) {
299 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
300 }
301 }
302 RunIteration();
303 }
304 }
305
306 void RunIteration() {
307 ::frc971::wpilib::SendRobotState(my_pid_, ds_);
308
309 const auto values = constants::GetValues();
310
311 {
312 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
313 drivetrain_message->right_encoder =
314 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800315 drivetrain_message->right_speed =
316 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
317
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800318 drivetrain_message->left_encoder =
319 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
320 drivetrain_message->left_speed =
321 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800322
323 drivetrain_message.Send();
324 }
325
326 dma_synchronizer_->RunIteration();
327
328 {
329 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800330 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800331 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800332 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800333 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800334
Brianef030df2017-03-05 15:06:04 -0800335 CopyPosition(indexer_counter_, &superstructure_message->column.indexer,
336 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800337 Values::kIndexerEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800338
Brian Silverman50826c02017-02-18 14:40:25 -0800339 superstructure_message->theta_shooter =
340 encoder_translate(shooter_encoder_->GetRaw(),
341 Values::kShooterEncoderCountsPerRevolution,
342 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800343
Brian Silverman50826c02017-02-18 14:40:25 -0800344 CopyPosition(hood_encoder_, &superstructure_message->hood,
345 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800346 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800347
Brianef030df2017-03-05 15:06:04 -0800348 CopyPosition(turret_counter_, &superstructure_message->column.turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800349 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800350 Values::kTurretEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800351
352 superstructure_message.Send();
353 }
354
355 {
Philipp Schrader996a2a22017-02-22 05:02:48 +0000356 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800357 auto_mode_message->mode = 0;
358 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
359 if (autonomous_modes_[i]->Get()) {
360 auto_mode_message->mode |= 1 << i;
361 }
362 }
363 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
364 auto_mode_message.Send();
365 }
366 }
367
368 void Quit() { run_ = false; }
369
370 private:
Brian Silverman50826c02017-02-18 14:40:25 -0800371 double encoder_translate(int32_t value, double counts_per_revolution,
372 double ratio) {
373 return static_cast<double>(value) / counts_per_revolution * ratio *
374 (2.0 * M_PI);
375 }
376
Brian Silverman7cce2d32017-02-19 21:48:48 -0800377 void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder,
378 ::frc971::IndexPosition *position,
Brian Silverman50826c02017-02-18 14:40:25 -0800379 double encoder_counts_per_revolution, double encoder_ratio,
Brian Silverman7cce2d32017-02-19 21:48:48 -0800380 bool reverse) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800381 const double multiplier = reverse ? -1.0 : 1.0;
382 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800383 multiplier * encoder_translate(encoder.polled_encoder_value(),
384 encoder_counts_per_revolution,
385 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800386 position->latched_encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800387 multiplier * encoder_translate(encoder.last_encoder_value(),
388 encoder_counts_per_revolution,
389 encoder_ratio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800390 position->index_pulses = encoder.index_posedge_count();
391 }
392
Brian Silverman50826c02017-02-18 14:40:25 -0800393 void CopyPosition(const AbsoluteEncoderAndPotentiometer &encoder,
394 ::frc971::PotAndAbsolutePosition *position,
395 double encoder_counts_per_revolution, double encoder_ratio,
396 ::std::function<double(double)> potentiometer_translate,
397 bool reverse, double pot_offset) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800398 const double multiplier = reverse ? -1.0 : 1.0;
399 position->pot = multiplier * potentiometer_translate(
Brian Silverman50826c02017-02-18 14:40:25 -0800400 encoder.ReadPotentiometerVoltage()) +
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800401 pot_offset;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800402 position->encoder =
Brian Silverman50826c02017-02-18 14:40:25 -0800403 multiplier * encoder_translate(encoder.ReadRelativeEncoder(),
404 encoder_counts_per_revolution,
405 encoder_ratio);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800406
407 position->absolute_encoder =
408 (reverse ? (1.0 - encoder.ReadAbsoluteEncoder())
409 : encoder.ReadAbsoluteEncoder()) *
410 encoder_ratio * (2.0 * M_PI);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800411 }
412
Brianef030df2017-03-05 15:06:04 -0800413 void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter,
414 ::frc971::HallEffectAndPosition *position,
415 double encoder_counts_per_revolution, double encoder_ratio,
416 bool reverse) {
417 const double multiplier = reverse ? -1.0 : 1.0;
418 position->position =
419 multiplier * encoder_translate(counter.polled_encoder(),
420 encoder_counts_per_revolution,
421 encoder_ratio);
422 position->current = !counter.polled_value();
423 position->posedge_count = counter.negative_count();
424 position->negedge_count = counter.positive_count();
425 position->posedge_value =
426 multiplier * encoder_translate(counter.last_negative_encoder_value(),
427 encoder_counts_per_revolution,
428 encoder_ratio);
429 position->negedge_value =
430 multiplier * encoder_translate(counter.last_positive_encoder_value(),
431 encoder_counts_per_revolution,
432 encoder_ratio);
433 }
434
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800435 int32_t my_pid_;
436 DriverStation *ds_;
437
438 ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_;
439
Brian Silverman052e69d2017-02-12 16:19:55 -0800440 DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_,
Brianef030df2017-03-05 15:06:04 -0800441 hall_filter_;
Brian Silverman052e69d2017-02-12 16:19:55 -0800442
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800443 ::std::unique_ptr<Encoder> drivetrain_left_encoder_,
444 drivetrain_right_encoder_;
445
Brian Silverman50826c02017-02-18 14:40:25 -0800446 AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800447
Brian Silverman052e69d2017-02-12 16:19:55 -0800448 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800449 ::std::unique_ptr<DigitalInput> indexer_hall_;
450 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800451
Brianef030df2017-03-05 15:06:04 -0800452 ::std::unique_ptr<Encoder> turret_encoder_;
453 ::std::unique_ptr<DigitalInput> turret_hall_;
454 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
455
Brian Silverman7cce2d32017-02-19 21:48:48 -0800456 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800457 ::std::unique_ptr<Encoder> shooter_encoder_;
458
459 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
460
461 ::std::atomic<bool> run_{true};
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800462};
463
Adam Snaidere0554ef2017-03-11 23:02:45 -0800464class SolenoidWriter {
465 public:
466 SolenoidWriter()
467 : superstructure_(".y2017.control_loops.superstructure_queue.output") {}
468
469 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
470
471 void set_lights(
472 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
473 lights_ = ::std::move(s);
474 }
475
Austin Schuhc587c882017-03-29 21:33:10 -0700476 void set_rgb_light(
477 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
478 rgb_lights_ = ::std::move(s);
479 }
480
Adam Snaidere0554ef2017-03-11 23:02:45 -0800481 void operator()() {
482 ::aos::SetCurrentThreadName("Solenoids");
483 ::aos::SetCurrentThreadRealtimePriority(27);
484
485 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
486 ::std::chrono::milliseconds(1));
487
488 while (run_) {
489 {
490 const int iterations = phased_loop.SleepUntilNext();
491 if (iterations != 1) {
492 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
493 }
494 }
495
496 {
497 superstructure_.FetchLatest();
498 if (superstructure_.get()) {
499 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
500 lights_->Set(superstructure_->lights_on);
Austin Schuhc587c882017-03-29 21:33:10 -0700501 rgb_lights_->Set(superstructure_->red_light_on |
502 superstructure_->green_light_on |
503 superstructure_->blue_light_on);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800504 }
505 }
506
507 pcm_.Flush();
508 }
509 }
510
511 void Quit() { run_ = false; }
512
513 private:
514 ::frc971::wpilib::BufferedPcm pcm_;
515
Austin Schuhc587c882017-03-29 21:33:10 -0700516 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800517
518 ::aos::Queue<
519 ::y2017::control_loops::SuperstructureQueue::Output>
520 superstructure_;
521
522 ::std::atomic<bool> run_{true};
523};
524
525
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800526class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
527 public:
528 void set_drivetrain_left_victor(::std::unique_ptr<VictorSP> t) {
529 drivetrain_left_victor_ = ::std::move(t);
530 }
531
532 void set_drivetrain_right_victor(::std::unique_ptr<VictorSP> t) {
533 drivetrain_right_victor_ = ::std::move(t);
534 }
535
536 private:
537 virtual void Read() override {
538 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
539 }
540
541 virtual void Write() override {
542 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
543 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh410e3812017-02-21 16:44:03 -0800544 drivetrain_left_victor_->SetSpeed(-queue->left_voltage / 12.0);
545 drivetrain_right_victor_->SetSpeed(queue->right_voltage / 12.0);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800546 }
547
548 virtual void Stop() override {
549 LOG(WARNING, "drivetrain output too old\n");
550 drivetrain_left_victor_->SetDisabled();
551 drivetrain_right_victor_->SetDisabled();
552 }
553
554 ::std::unique_ptr<VictorSP> drivetrain_left_victor_, drivetrain_right_victor_;
555};
556
557class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
558 public:
559 void set_intake_victor(::std::unique_ptr<VictorSP> t) {
560 intake_victor_ = ::std::move(t);
561 }
562 void set_intake_rollers_victor(::std::unique_ptr<VictorSP> t) {
563 intake_rollers_victor_ = ::std::move(t);
564 }
565
Brian Silverman052e69d2017-02-12 16:19:55 -0800566 void set_indexer_victor(::std::unique_ptr<VictorSP> t) {
567 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800568 }
Brian Silverman052e69d2017-02-12 16:19:55 -0800569 void set_indexer_roller_victor(::std::unique_ptr<VictorSP> t) {
570 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800571 }
572
573 void set_shooter_victor(::std::unique_ptr<VictorSP> t) {
574 shooter_victor_ = ::std::move(t);
575 }
576 void set_turret_victor(::std::unique_ptr<VictorSP> t) {
577 turret_victor_ = ::std::move(t);
578 }
579 void set_hood_victor(::std::unique_ptr<VictorSP> t) {
580 hood_victor_ = ::std::move(t);
581 }
582
Austin Schuhc587c882017-03-29 21:33:10 -0700583 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
584 red_light_ = ::std::move(t);
585 }
586 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
587 green_light_ = ::std::move(t);
588 }
589 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
590 blue_light_ = ::std::move(t);
591 }
592
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800593 private:
594 virtual void Read() override {
595 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
596 }
597
598 virtual void Write() override {
599 auto &queue = ::y2017::control_loops::superstructure_queue.output;
600 LOG_STRUCT(DEBUG, "will output", *queue);
601 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
602 -kMaxBringupPower, kMaxBringupPower) /
603 12.0);
604 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800605 indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0);
606 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0);
Austin Schuh410e3812017-02-21 16:44:03 -0800607 turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800608 -kMaxBringupPower, kMaxBringupPower) /
609 12.0);
610 hood_victor_->SetSpeed(
611 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
612 12.0);
613 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700614
615 red_light_->Set(queue->red_light_on);
616 green_light_->Set(queue->green_light_on);
617 blue_light_->Set(queue->blue_light_on);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800618 }
619
620 virtual void Stop() override {
621 LOG(WARNING, "Superstructure output too old.\n");
622 intake_victor_->SetDisabled();
623 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800624 indexer_victor_->SetDisabled();
625 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800626 turret_victor_->SetDisabled();
627 hood_victor_->SetDisabled();
628 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700629
630 red_light_->Set(true);
631 green_light_->Set(true);
632 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800633 }
634
635 ::std::unique_ptr<VictorSP> intake_victor_, intake_rollers_victor_,
Brian Silverman052e69d2017-02-12 16:19:55 -0800636 indexer_victor_, indexer_roller_victor_, shooter_victor_,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800637 turret_victor_, hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700638
639 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800640};
641
642class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
643 public:
644 ::std::unique_ptr<Encoder> make_encoder(int index) {
645 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
646 Encoder::k4X);
647 }
648
649 void Run() override {
650 ::aos::InitNRT();
651 ::aos::SetCurrentThreadName("StartCompetition");
652
653 ::frc971::wpilib::JoystickSender joystick_sender;
654 ::std::thread joystick_thread(::std::ref(joystick_sender));
655
656 ::frc971::wpilib::PDPFetcher pdp_fetcher;
657 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
658 SensorReader reader;
659
660 // TODO(campbell): Update port numbers
661 reader.set_drivetrain_left_encoder(make_encoder(0));
662 reader.set_drivetrain_right_encoder(make_encoder(1));
663
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800664 reader.set_intake_encoder(make_encoder(3));
Brian Silverman50826c02017-02-18 14:40:25 -0800665 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800666 reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800667
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800668 reader.set_indexer_encoder(make_encoder(5));
Brianef030df2017-03-05 15:06:04 -0800669 reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800670
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800671 reader.set_turret_encoder(make_encoder(6));
Brianef030df2017-03-05 15:06:04 -0800672 reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800673
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800674 reader.set_hood_encoder(make_encoder(4));
675 reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800676
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800677 reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800678
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800679 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
680 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
681 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
682 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800683
684 reader.set_dma(make_unique<DMA>());
685 ::std::thread reader_thread(::std::ref(reader));
686
Brian Silvermanb4439852017-02-24 19:49:09 -0800687 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800688 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700689 imu.SetDummySPI(SPI::Port::kOnboardCS2);
690 auto imu_reset = make_unique<DigitalOutput>(6);
691 imu.set_reset(imu_reset.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800692 ::std::thread imu_thread(::std::ref(imu));
693
694 DrivetrainWriter drivetrain_writer;
695 drivetrain_writer.set_drivetrain_left_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800696 ::std::unique_ptr<VictorSP>(new VictorSP(7)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800697 drivetrain_writer.set_drivetrain_right_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800698 ::std::unique_ptr<VictorSP>(new VictorSP(3)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800699 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
700
701 SuperstructureWriter superstructure_writer;
702 superstructure_writer.set_intake_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800703 ::std::unique_ptr<VictorSP>(new VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800704 superstructure_writer.set_intake_rollers_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800705 ::std::unique_ptr<VictorSP>(new VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800706 superstructure_writer.set_indexer_victor(
707 ::std::unique_ptr<VictorSP>(new VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800708 superstructure_writer.set_indexer_roller_victor(
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800709 ::std::unique_ptr<VictorSP>(new VictorSP(5)));
710 superstructure_writer.set_turret_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800711 ::std::unique_ptr<VictorSP>(new VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800712 superstructure_writer.set_hood_victor(
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800713 ::std::unique_ptr<VictorSP>(new VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800714 superstructure_writer.set_shooter_victor(
715 ::std::unique_ptr<VictorSP>(new VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700716
717 superstructure_writer.set_red_light(
718 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
719 superstructure_writer.set_green_light(
720 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
721 superstructure_writer.set_blue_light(
722 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
723
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800724 ::std::thread superstructure_writer_thread(
725 ::std::ref(superstructure_writer));
726
Adam Snaidere0554ef2017-03-11 23:02:45 -0800727 SolenoidWriter solenoid_writer;
728 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700729 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800730
731 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
732
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800733 // Wait forever. Not much else to do...
734 while (true) {
735 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
736 if (r != 0) {
737 PLOG(WARNING, "infinite select failed");
738 } else {
739 PLOG(WARNING, "infinite select succeeded??\n");
740 }
741 }
742
743 LOG(ERROR, "Exiting WPILibRobot\n");
744
745 joystick_sender.Quit();
746 joystick_thread.join();
747 pdp_fetcher.Quit();
748 pdp_fetcher_thread.join();
749 reader.Quit();
750 reader_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800751 imu.Quit();
752 imu_thread.join();
753
754 drivetrain_writer.Quit();
755 drivetrain_writer_thread.join();
756 superstructure_writer.Quit();
757 superstructure_writer_thread.join();
758
759 ::aos::Cleanup();
760 }
761};
762
Brian Silverman052e69d2017-02-12 16:19:55 -0800763} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800764} // namespace wpilib
765} // namespace y2017
766
767AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);