blob: b72d6e9aa27c076d13f472607c5ad1c30e52b801 [file] [log] [blame]
Austin Schuh8347cb62017-04-08 14:37:34 -07001#include <inttypes.h>
Campbell Crowleyae6e8422017-02-05 12:38:50 -08002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Campbell Crowleyae6e8422017-02-05 12:38:50 -08005
Campbell Crowleyae6e8422017-02-05 12:38:50 -08006#include <array>
Austin Schuh8347cb62017-04-08 14:37:34 -07007#include <chrono>
Brian Silverman50826c02017-02-18 14:40:25 -08008#include <cmath>
Austin Schuh8347cb62017-04-08 14:37:34 -07009#include <functional>
10#include <mutex>
11#include <thread>
Campbell Crowleyae6e8422017-02-05 12:38:50 -080012
Parker Schuhd3b7a8872018-02-19 16:42:27 -080013#include "frc971/wpilib/ahal/AnalogInput.h"
14#include "frc971/wpilib/ahal/Compressor.h"
15#include "frc971/wpilib/ahal/Counter.h"
16#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
17#include "frc971/wpilib/ahal/DriverStation.h"
18#include "frc971/wpilib/ahal/Encoder.h"
19#include "frc971/wpilib/ahal/Relay.h"
20#include "frc971/wpilib/ahal/Servo.h"
21#include "frc971/wpilib/ahal/VictorSP.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080022#undef ERROR
23
John Park33858a32018-09-28 23:05:48 -070024#include "aos/commonmath.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080025#include "aos/events/shm-event-loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080026#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070027#include "aos/logging/logging.h"
28#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080029#include "aos/make_unique.h"
John Park33858a32018-09-28 23:05:48 -070030#include "aos/robot_state/robot_state.q.h"
31#include "aos/stl_mutex/stl_mutex.h"
32#include "aos/time/time.h"
33#include "aos/util/compiler_memory_barrier.h"
34#include "aos/util/log_interval.h"
35#include "aos/util/phased_loop.h"
36#include "aos/util/wrapping_counter.h"
Philipp Schrader996a2a22017-02-22 05:02:48 +000037#include "frc971/autonomous/auto.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080038#include "frc971/control_loops/control_loops.q.h"
39#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070040#include "frc971/wpilib/ADIS16448.h"
41#include "frc971/wpilib/buffered_pcm.h"
42#include "frc971/wpilib/buffered_solenoid.h"
43#include "frc971/wpilib/dma.h"
44#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davis7af11ad2019-02-03 01:16:45 -080045#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070046#include "frc971/wpilib/encoder_and_potentiometer.h"
47#include "frc971/wpilib/interrupt_edge_counting.h"
48#include "frc971/wpilib/joystick_sender.h"
49#include "frc971/wpilib/logging.q.h"
50#include "frc971/wpilib/loop_output_handler.h"
51#include "frc971/wpilib/pdp_fetcher.h"
Austin Schuhbf29b6f2019-02-02 21:45:27 -080052#include "frc971/wpilib/sensor_reader.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070053#include "frc971/wpilib/wpilib_robot_base.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080054#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080055#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080056
Campbell Crowleyae6e8422017-02-05 12:38:50 -080057#ifndef M_PI
58#define M_PI 3.14159265358979323846
59#endif
60
61using ::frc971::control_loops::drivetrain_queue;
62using ::y2017::control_loops::superstructure_queue;
Brian Silverman052e69d2017-02-12 16:19:55 -080063using ::y2017::constants::Values;
Austin Schuh8347cb62017-04-08 14:37:34 -070064using ::aos::monotonic_clock;
65namespace chrono = ::std::chrono;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080066using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080067using aos::make_unique;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080068
69namespace y2017 {
70namespace wpilib {
71namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080072
Campbell Crowleyae6e8422017-02-05 12:38:50 -080073constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080074
75// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
76// DMA stuff and then removing the * 2.0 in *_translate.
77// The low bit is direction.
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
125// Class to send position messages with sensor readings to our loops.
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800126class SensorReader : public ::frc971::wpilib::SensorReader {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800127 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800128 SensorReader(::aos::EventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -0700129 : ::frc971::wpilib::SensorReader(event_loop),
130 auto_mode_sender_(
131 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
132 ".frc971.autonomous.auto_mode")) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800133 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800134 // we should ever see.
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800135 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
136 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Brianef030df2017-03-05 15:06:04 -0800137 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800138 }
139
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800140 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800141 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800142 shooter_encoder_ = ::std::move(encoder);
143 }
144
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800145 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800146 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800147 intake_encoder_.set_encoder(::std::move(encoder));
148 }
149
150 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
151 intake_encoder_.set_potentiometer(::std::move(potentiometer));
152 }
153
Brian Silverman50826c02017-02-18 14:40:25 -0800154 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
155 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800156 }
157
Brian Silverman052e69d2017-02-12 16:19:55 -0800158 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
159 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800160 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800161 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800162 }
163
Brianef030df2017-03-05 15:06:04 -0800164 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
165 hall_filter_.Add(input.get());
166 indexer_counter_.set_input(input.get());
167 indexer_hall_ = ::std::move(input);
168 }
169
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800170 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800171 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800172 turret_counter_.set_encoder(encoder.get());
173 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800174 }
175
Brianef030df2017-03-05 15:06:04 -0800176 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
177 hall_filter_.Add(input.get());
178 turret_counter_.set_input(input.get());
179 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800180 }
181
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800182 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800183 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800184 hood_encoder_.set_encoder(::std::move(encoder));
185 }
186
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800187 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800188 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800189 hood_encoder_.set_index(::std::move(index));
190 }
191
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800192 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
193 autonomous_modes_.at(i) = ::std::move(sensor);
194 }
195
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800196 void Start() {
197 AddToDMA(&indexer_counter_);
198 AddToDMA(&hood_encoder_);
199 AddToDMA(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800200 }
201
202 void RunIteration() {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800203 {
204 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
205 drivetrain_message->right_encoder =
206 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800207 drivetrain_message->right_speed =
208 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
209
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800210 drivetrain_message->left_encoder =
211 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
212 drivetrain_message->left_speed =
213 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800214
215 drivetrain_message.Send();
216 }
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800217 }
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800218
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800219 void RunDMAIteration() {
220 const auto values = constants::GetValues();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800221
222 {
223 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800224 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800225 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800226 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800227 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800228
Brianef030df2017-03-05 15:06:04 -0800229 CopyPosition(indexer_counter_, &superstructure_message->column.indexer,
230 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuh546a0382017-04-16 19:10:18 -0700231 Values::kIndexerEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800232
Brian Silverman50826c02017-02-18 14:40:25 -0800233 superstructure_message->theta_shooter =
234 encoder_translate(shooter_encoder_->GetRaw(),
235 Values::kShooterEncoderCountsPerRevolution,
236 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800237
Brian Silverman50826c02017-02-18 14:40:25 -0800238 CopyPosition(hood_encoder_, &superstructure_message->hood,
239 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800240 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800241
Brianef030df2017-03-05 15:06:04 -0800242 CopyPosition(turret_counter_, &superstructure_message->column.turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800243 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800244 Values::kTurretEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800245
246 superstructure_message.Send();
247 }
248
249 {
Austin Schuha250b2d2019-05-27 16:14:02 -0700250 auto auto_mode_message = auto_mode_sender_.MakeMessage();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800251 auto_mode_message->mode = 0;
252 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700253 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800254 auto_mode_message->mode |= 1 << i;
255 }
256 }
257 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
258 auto_mode_message.Send();
259 }
260 }
261
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800262 private:
Austin Schuha250b2d2019-05-27 16:14:02 -0700263 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
264
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800265 DigitalGlitchFilter hall_filter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800266
Austin Schuh2a3e0632018-02-19 16:24:49 -0800267 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800268
Brian Silverman052e69d2017-02-12 16:19:55 -0800269 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800270 ::std::unique_ptr<DigitalInput> indexer_hall_;
271 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800272
Brianef030df2017-03-05 15:06:04 -0800273 ::std::unique_ptr<Encoder> turret_encoder_;
274 ::std::unique_ptr<DigitalInput> turret_hall_;
275 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
276
Brian Silverman7cce2d32017-02-19 21:48:48 -0800277 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800278 ::std::unique_ptr<Encoder> shooter_encoder_;
279
280 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800281};
282
Adam Snaidere0554ef2017-03-11 23:02:45 -0800283class SolenoidWriter {
284 public:
285 SolenoidWriter()
286 : superstructure_(".y2017.control_loops.superstructure_queue.output") {}
287
288 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
289
Austin Schuh8347cb62017-04-08 14:37:34 -0700290 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800291 lights_ = ::std::move(s);
292 }
293
Austin Schuh8347cb62017-04-08 14:37:34 -0700294 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700295 rgb_lights_ = ::std::move(s);
296 }
297
Adam Snaidere0554ef2017-03-11 23:02:45 -0800298 void operator()() {
299 ::aos::SetCurrentThreadName("Solenoids");
300 ::aos::SetCurrentThreadRealtimePriority(27);
301
302 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
Austin Schuhd32b3622019-06-23 18:49:06 -0700303 ::aos::monotonic_clock::now(),
Adam Snaidere0554ef2017-03-11 23:02:45 -0800304 ::std::chrono::milliseconds(1));
305
306 while (run_) {
307 {
308 const int iterations = phased_loop.SleepUntilNext();
309 if (iterations != 1) {
310 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
311 }
312 }
313
314 {
315 superstructure_.FetchLatest();
316 if (superstructure_.get()) {
317 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
318 lights_->Set(superstructure_->lights_on);
Austin Schuhc587c882017-03-29 21:33:10 -0700319 rgb_lights_->Set(superstructure_->red_light_on |
320 superstructure_->green_light_on |
321 superstructure_->blue_light_on);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800322 }
323 }
324
325 pcm_.Flush();
326 }
327 }
328
329 void Quit() { run_ = false; }
330
331 private:
332 ::frc971::wpilib::BufferedPcm pcm_;
333
Austin Schuhc587c882017-03-29 21:33:10 -0700334 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800335
Austin Schuh8347cb62017-04-08 14:37:34 -0700336 ::aos::Queue<::y2017::control_loops::SuperstructureQueue::Output>
Adam Snaidere0554ef2017-03-11 23:02:45 -0800337 superstructure_;
338
339 ::std::atomic<bool> run_{true};
340};
341
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800342class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
343 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800344 SuperstructureWriter(::aos::EventLoop *event_loop)
345 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
346
Austin Schuh8347cb62017-04-08 14:37:34 -0700347 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800348 intake_victor_ = ::std::move(t);
349 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700350 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800351 intake_rollers_victor_ = ::std::move(t);
352 }
353
Austin Schuh8347cb62017-04-08 14:37:34 -0700354 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800355 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800356 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700357 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800358 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800359 }
360
Austin Schuh6a8131b2017-04-08 15:39:22 -0700361 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
362 gear_servo_ = ::std::move(t);
363 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700364 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800365 shooter_victor_ = ::std::move(t);
366 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700367 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800368 turret_victor_ = ::std::move(t);
369 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700370 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800371 hood_victor_ = ::std::move(t);
372 }
373
Austin Schuhc587c882017-03-29 21:33:10 -0700374 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
375 red_light_ = ::std::move(t);
376 }
377 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
378 green_light_ = ::std::move(t);
379 }
380 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
381 blue_light_ = ::std::move(t);
382 }
383
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800384 private:
385 virtual void Read() override {
386 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
387 }
388
389 virtual void Write() override {
390 auto &queue = ::y2017::control_loops::superstructure_queue.output;
391 LOG_STRUCT(DEBUG, "will output", *queue);
392 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
393 -kMaxBringupPower, kMaxBringupPower) /
394 12.0);
395 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800396 indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0);
397 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0);
Austin Schuh410e3812017-02-21 16:44:03 -0800398 turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800399 -kMaxBringupPower, kMaxBringupPower) /
400 12.0);
401 hood_victor_->SetSpeed(
402 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
403 12.0);
404 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700405
406 red_light_->Set(queue->red_light_on);
407 green_light_->Set(queue->green_light_on);
408 blue_light_->Set(queue->blue_light_on);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700409
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800410 gear_servo_->SetPosition(queue->gear_servo);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800411 }
412
413 virtual void Stop() override {
414 LOG(WARNING, "Superstructure output too old.\n");
415 intake_victor_->SetDisabled();
416 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800417 indexer_victor_->SetDisabled();
418 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800419 turret_victor_->SetDisabled();
420 hood_victor_->SetDisabled();
421 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700422
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800423 gear_servo_->SetRaw(0);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700424
Austin Schuhc587c882017-03-29 21:33:10 -0700425 red_light_->Set(true);
426 green_light_->Set(true);
427 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800428 }
429
Austin Schuh8347cb62017-04-08 14:37:34 -0700430 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
431 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
432 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700433
Austin Schuh6a8131b2017-04-08 15:39:22 -0700434 ::std::unique_ptr<::frc::Servo> gear_servo_;
435
Austin Schuhc587c882017-03-29 21:33:10 -0700436 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800437};
438
439class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
440 public:
441 ::std::unique_ptr<Encoder> make_encoder(int index) {
442 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
443 Encoder::k4X);
444 }
445
446 void Run() override {
447 ::aos::InitNRT();
448 ::aos::SetCurrentThreadName("StartCompetition");
449
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800450 ::aos::ShmEventLoop event_loop;
451
452 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800453 ::std::thread joystick_thread(::std::ref(joystick_sender));
454
Austin Schuh0b545432019-05-12 15:46:12 -0700455 ::frc971::wpilib::PDPFetcher pdp_fetcher(&event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800456 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800457 SensorReader reader(&event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800458
459 // TODO(campbell): Update port numbers
460 reader.set_drivetrain_left_encoder(make_encoder(0));
461 reader.set_drivetrain_right_encoder(make_encoder(1));
462
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800463 reader.set_intake_encoder(make_encoder(3));
Brian Silverman50826c02017-02-18 14:40:25 -0800464 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800465 reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800466
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800467 reader.set_indexer_encoder(make_encoder(5));
Brianef030df2017-03-05 15:06:04 -0800468 reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800469
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800470 reader.set_turret_encoder(make_encoder(6));
Brianef030df2017-03-05 15:06:04 -0800471 reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800472
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800473 reader.set_hood_encoder(make_encoder(4));
474 reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800475
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800476 reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800477
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800478 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
479 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Austin Schuh8347cb62017-04-08 14:37:34 -0700480
Austin Schuh3b010bc2019-02-24 17:25:37 -0800481 reader.set_pwm_trigger(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800482
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800483 ::std::thread reader_thread(::std::ref(reader));
484
Brian Silvermanb4439852017-02-24 19:49:09 -0800485 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800486 ::frc971::wpilib::ADIS16448 imu(&event_loop, SPI::Port::kOnboardCS1,
487 imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700488 imu.SetDummySPI(SPI::Port::kOnboardCS2);
489 auto imu_reset = make_unique<DigitalOutput>(6);
490 imu.set_reset(imu_reset.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800491 ::std::thread imu_thread(::std::ref(imu));
492
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800493 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davis7af11ad2019-02-03 01:16:45 -0800494 drivetrain_writer.set_left_controller0(
495 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)), true);
496 drivetrain_writer.set_right_controller0(
497 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800498 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
499
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800500 SuperstructureWriter superstructure_writer(&event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800501 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700502 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800503 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700504 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800505 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700506 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800507 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700508 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800509 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700510 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800511 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700512 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800513 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700514 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700515
Austin Schuh6a8131b2017-04-08 15:39:22 -0700516 superstructure_writer.set_gear_servo(
517 ::std::unique_ptr<Servo>(new Servo(0)));
518
Austin Schuhc587c882017-03-29 21:33:10 -0700519 superstructure_writer.set_red_light(
520 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
521 superstructure_writer.set_green_light(
522 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
523 superstructure_writer.set_blue_light(
524 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
525
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800526 ::std::thread superstructure_writer_thread(
527 ::std::ref(superstructure_writer));
528
Adam Snaidere0554ef2017-03-11 23:02:45 -0800529 SolenoidWriter solenoid_writer;
530 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700531 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800532
533 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
534
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800535 // Wait forever. Not much else to do...
536 while (true) {
537 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
538 if (r != 0) {
539 PLOG(WARNING, "infinite select failed");
540 } else {
541 PLOG(WARNING, "infinite select succeeded??\n");
542 }
543 }
544
545 LOG(ERROR, "Exiting WPILibRobot\n");
546
547 joystick_sender.Quit();
548 joystick_thread.join();
549 pdp_fetcher.Quit();
550 pdp_fetcher_thread.join();
551 reader.Quit();
552 reader_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800553 imu.Quit();
554 imu_thread.join();
555
556 drivetrain_writer.Quit();
557 drivetrain_writer_thread.join();
558 superstructure_writer.Quit();
559 superstructure_writer_thread.join();
560
561 ::aos::Cleanup();
562 }
563};
564
Brian Silverman052e69d2017-02-12 16:19:55 -0800565} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800566} // namespace wpilib
567} // namespace y2017
568
569AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);