blob: 6a5203e0de4913960d8a3b0f007d54f575f17471 [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"
Brian Silvermanf819b442019-01-20 16:51:04 -080025#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070026#include "aos/logging/logging.h"
27#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080028#include "aos/make_unique.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/robot_state/robot_state.q.h"
30#include "aos/stl_mutex/stl_mutex.h"
31#include "aos/time/time.h"
32#include "aos/util/compiler_memory_barrier.h"
33#include "aos/util/log_interval.h"
34#include "aos/util/phased_loop.h"
35#include "aos/util/wrapping_counter.h"
Philipp Schrader996a2a22017-02-22 05:02:48 +000036#include "frc971/autonomous/auto.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080037#include "frc971/control_loops/control_loops.q.h"
38#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070039#include "frc971/wpilib/ADIS16448.h"
40#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/buffered_solenoid.h"
42#include "frc971/wpilib/dma.h"
43#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davis7af11ad2019-02-03 01:16:45 -080044#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070045#include "frc971/wpilib/encoder_and_potentiometer.h"
46#include "frc971/wpilib/interrupt_edge_counting.h"
47#include "frc971/wpilib/joystick_sender.h"
48#include "frc971/wpilib/logging.q.h"
49#include "frc971/wpilib/loop_output_handler.h"
50#include "frc971/wpilib/pdp_fetcher.h"
Austin Schuhbf29b6f2019-02-02 21:45:27 -080051#include "frc971/wpilib/sensor_reader.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070052#include "frc971/wpilib/wpilib_robot_base.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080053#include "y2017/constants.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080054#include "y2017/control_loops/superstructure/superstructure.q.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080055
Campbell Crowleyae6e8422017-02-05 12:38:50 -080056#ifndef M_PI
57#define M_PI 3.14159265358979323846
58#endif
59
60using ::frc971::control_loops::drivetrain_queue;
61using ::y2017::control_loops::superstructure_queue;
Brian Silverman052e69d2017-02-12 16:19:55 -080062using ::y2017::constants::Values;
Austin Schuh8347cb62017-04-08 14:37:34 -070063using ::aos::monotonic_clock;
64namespace chrono = ::std::chrono;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080065using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080066using aos::make_unique;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080067
68namespace y2017 {
69namespace wpilib {
70namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080071
Campbell Crowleyae6e8422017-02-05 12:38:50 -080072constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080073
74// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
75// DMA stuff and then removing the * 2.0 in *_translate.
76// The low bit is direction.
77
Brian Silverman052e69d2017-02-12 16:19:55 -080078// TODO(brian): Use ::std::max instead once we have C++14 so that can be
79// constexpr.
80template <typename T>
81constexpr T max(T a, T b) {
82 return (a > b) ? a : b;
83}
84template <typename T, typename... Rest>
85constexpr T max(T a, T b, T c, Rest... rest) {
86 return max(max(a, b), c, rest...);
87}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080088
Campbell Crowleyae6e8422017-02-05 12:38:50 -080089double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080090 return static_cast<double>(in) /
91 Values::kDrivetrainEncoderCountsPerRevolution *
92 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080093}
94
95double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080096 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
97 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080098}
99
Brian Silverman50826c02017-02-18 14:40:25 -0800100// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800101double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800102 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800103 (2 * M_PI /*radians*/);
104}
105
Brian Silverman052e69d2017-02-12 16:19:55 -0800106constexpr double kMaxFastEncoderPulsesPerSecond =
107 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
108 Values::kMaxShooterEncoderPulsesPerSecond);
109static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
110 "fast encoders are too fast");
111constexpr double kMaxMediumEncoderPulsesPerSecond =
112 max(Values::kMaxIntakeEncoderPulsesPerSecond,
113 Values::kMaxTurretEncoderPulsesPerSecond,
114 Values::kMaxIndexerEncoderPulsesPerSecond);
115static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
116 "medium encoders are too fast");
117constexpr double kMaxSlowEncoderPulsesPerSecond =
118 Values::kMaxHoodEncoderPulsesPerSecond;
119static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
120 "slow encoders are too fast");
Brianef030df2017-03-05 15:06:04 -0800121static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond,
122 "slow encoders are faster than medium?");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800123
124// Class to send position messages with sensor readings to our loops.
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800125class SensorReader : public ::frc971::wpilib::SensorReader {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800126 public:
127 SensorReader() {
Brian Silverman052e69d2017-02-12 16:19:55 -0800128 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800129 // we should ever see.
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800130 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
131 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Brianef030df2017-03-05 15:06:04 -0800132 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800133 }
134
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800135 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800136 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800137 shooter_encoder_ = ::std::move(encoder);
138 }
139
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800140 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800141 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800142 intake_encoder_.set_encoder(::std::move(encoder));
143 }
144
145 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
146 intake_encoder_.set_potentiometer(::std::move(potentiometer));
147 }
148
Brian Silverman50826c02017-02-18 14:40:25 -0800149 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
150 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800151 }
152
Brian Silverman052e69d2017-02-12 16:19:55 -0800153 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
154 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800155 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800156 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800157 }
158
Brianef030df2017-03-05 15:06:04 -0800159 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
160 hall_filter_.Add(input.get());
161 indexer_counter_.set_input(input.get());
162 indexer_hall_ = ::std::move(input);
163 }
164
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800165 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800166 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800167 turret_counter_.set_encoder(encoder.get());
168 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800169 }
170
Brianef030df2017-03-05 15:06:04 -0800171 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
172 hall_filter_.Add(input.get());
173 turret_counter_.set_input(input.get());
174 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800175 }
176
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800177 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800178 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800179 hood_encoder_.set_encoder(::std::move(encoder));
180 }
181
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800182 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800183 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800184 hood_encoder_.set_index(::std::move(index));
185 }
186
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800187 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
188 autonomous_modes_.at(i) = ::std::move(sensor);
189 }
190
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800191 void Start() {
192 AddToDMA(&indexer_counter_);
193 AddToDMA(&hood_encoder_);
194 AddToDMA(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800195 }
196
197 void RunIteration() {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800198 {
199 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
200 drivetrain_message->right_encoder =
201 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800202 drivetrain_message->right_speed =
203 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
204
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800205 drivetrain_message->left_encoder =
206 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
207 drivetrain_message->left_speed =
208 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800209
210 drivetrain_message.Send();
211 }
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800212 }
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800213
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800214 void RunDMAIteration() {
215 const auto values = constants::GetValues();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800216
217 {
218 auto superstructure_message = superstructure_queue.position.MakeMessage();
Brian Silverman052e69d2017-02-12 16:19:55 -0800219 CopyPosition(intake_encoder_, &superstructure_message->intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800220 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800221 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800222 values.intake.pot_offset);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800223
Brianef030df2017-03-05 15:06:04 -0800224 CopyPosition(indexer_counter_, &superstructure_message->column.indexer,
225 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuh546a0382017-04-16 19:10:18 -0700226 Values::kIndexerEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800227
Brian Silverman50826c02017-02-18 14:40:25 -0800228 superstructure_message->theta_shooter =
229 encoder_translate(shooter_encoder_->GetRaw(),
230 Values::kShooterEncoderCountsPerRevolution,
231 Values::kShooterEncoderRatio);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800232
Brian Silverman50826c02017-02-18 14:40:25 -0800233 CopyPosition(hood_encoder_, &superstructure_message->hood,
234 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800235 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800236
Brianef030df2017-03-05 15:06:04 -0800237 CopyPosition(turret_counter_, &superstructure_message->column.turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800238 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800239 Values::kTurretEncoderRatio, false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800240
241 superstructure_message.Send();
242 }
243
244 {
Philipp Schrader996a2a22017-02-22 05:02:48 +0000245 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800246 auto_mode_message->mode = 0;
247 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700248 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800249 auto_mode_message->mode |= 1 << i;
250 }
251 }
252 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
253 auto_mode_message.Send();
254 }
255 }
256
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800257 private:
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800258 DigitalGlitchFilter hall_filter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800259
Austin Schuh2a3e0632018-02-19 16:24:49 -0800260 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800261
Brian Silverman052e69d2017-02-12 16:19:55 -0800262 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800263 ::std::unique_ptr<DigitalInput> indexer_hall_;
264 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800265
Brianef030df2017-03-05 15:06:04 -0800266 ::std::unique_ptr<Encoder> turret_encoder_;
267 ::std::unique_ptr<DigitalInput> turret_hall_;
268 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
269
Brian Silverman7cce2d32017-02-19 21:48:48 -0800270 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800271 ::std::unique_ptr<Encoder> shooter_encoder_;
272
273 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800274};
275
Adam Snaidere0554ef2017-03-11 23:02:45 -0800276class SolenoidWriter {
277 public:
278 SolenoidWriter()
279 : superstructure_(".y2017.control_loops.superstructure_queue.output") {}
280
281 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
282
Austin Schuh8347cb62017-04-08 14:37:34 -0700283 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800284 lights_ = ::std::move(s);
285 }
286
Austin Schuh8347cb62017-04-08 14:37:34 -0700287 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700288 rgb_lights_ = ::std::move(s);
289 }
290
Adam Snaidere0554ef2017-03-11 23:02:45 -0800291 void operator()() {
292 ::aos::SetCurrentThreadName("Solenoids");
293 ::aos::SetCurrentThreadRealtimePriority(27);
294
295 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
296 ::std::chrono::milliseconds(1));
297
298 while (run_) {
299 {
300 const int iterations = phased_loop.SleepUntilNext();
301 if (iterations != 1) {
302 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
303 }
304 }
305
306 {
307 superstructure_.FetchLatest();
308 if (superstructure_.get()) {
309 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
310 lights_->Set(superstructure_->lights_on);
Austin Schuhc587c882017-03-29 21:33:10 -0700311 rgb_lights_->Set(superstructure_->red_light_on |
312 superstructure_->green_light_on |
313 superstructure_->blue_light_on);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800314 }
315 }
316
317 pcm_.Flush();
318 }
319 }
320
321 void Quit() { run_ = false; }
322
323 private:
324 ::frc971::wpilib::BufferedPcm pcm_;
325
Austin Schuhc587c882017-03-29 21:33:10 -0700326 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800327
Austin Schuh8347cb62017-04-08 14:37:34 -0700328 ::aos::Queue<::y2017::control_loops::SuperstructureQueue::Output>
Adam Snaidere0554ef2017-03-11 23:02:45 -0800329 superstructure_;
330
331 ::std::atomic<bool> run_{true};
332};
333
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800334class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
335 public:
Austin Schuh8347cb62017-04-08 14:37:34 -0700336 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800337 intake_victor_ = ::std::move(t);
338 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700339 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800340 intake_rollers_victor_ = ::std::move(t);
341 }
342
Austin Schuh8347cb62017-04-08 14:37:34 -0700343 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800344 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800345 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700346 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800347 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800348 }
349
Austin Schuh6a8131b2017-04-08 15:39:22 -0700350 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
351 gear_servo_ = ::std::move(t);
352 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700353 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800354 shooter_victor_ = ::std::move(t);
355 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700356 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800357 turret_victor_ = ::std::move(t);
358 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700359 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800360 hood_victor_ = ::std::move(t);
361 }
362
Austin Schuhc587c882017-03-29 21:33:10 -0700363 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
364 red_light_ = ::std::move(t);
365 }
366 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
367 green_light_ = ::std::move(t);
368 }
369 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
370 blue_light_ = ::std::move(t);
371 }
372
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800373 private:
374 virtual void Read() override {
375 ::y2017::control_loops::superstructure_queue.output.FetchAnother();
376 }
377
378 virtual void Write() override {
379 auto &queue = ::y2017::control_loops::superstructure_queue.output;
380 LOG_STRUCT(DEBUG, "will output", *queue);
381 intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake,
382 -kMaxBringupPower, kMaxBringupPower) /
383 12.0);
384 intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800385 indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0);
386 indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0);
Austin Schuh410e3812017-02-21 16:44:03 -0800387 turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret,
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800388 -kMaxBringupPower, kMaxBringupPower) /
389 12.0);
390 hood_victor_->SetSpeed(
391 ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) /
392 12.0);
393 shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700394
395 red_light_->Set(queue->red_light_on);
396 green_light_->Set(queue->green_light_on);
397 blue_light_->Set(queue->blue_light_on);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700398
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800399 gear_servo_->SetPosition(queue->gear_servo);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800400 }
401
402 virtual void Stop() override {
403 LOG(WARNING, "Superstructure output too old.\n");
404 intake_victor_->SetDisabled();
405 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800406 indexer_victor_->SetDisabled();
407 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800408 turret_victor_->SetDisabled();
409 hood_victor_->SetDisabled();
410 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700411
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800412 gear_servo_->SetRaw(0);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700413
Austin Schuhc587c882017-03-29 21:33:10 -0700414 red_light_->Set(true);
415 green_light_->Set(true);
416 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800417 }
418
Austin Schuh8347cb62017-04-08 14:37:34 -0700419 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
420 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
421 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700422
Austin Schuh6a8131b2017-04-08 15:39:22 -0700423 ::std::unique_ptr<::frc::Servo> gear_servo_;
424
Austin Schuhc587c882017-03-29 21:33:10 -0700425 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800426};
427
428class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
429 public:
430 ::std::unique_ptr<Encoder> make_encoder(int index) {
431 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
432 Encoder::k4X);
433 }
434
435 void Run() override {
436 ::aos::InitNRT();
437 ::aos::SetCurrentThreadName("StartCompetition");
438
439 ::frc971::wpilib::JoystickSender joystick_sender;
440 ::std::thread joystick_thread(::std::ref(joystick_sender));
441
442 ::frc971::wpilib::PDPFetcher pdp_fetcher;
443 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
444 SensorReader reader;
445
446 // TODO(campbell): Update port numbers
447 reader.set_drivetrain_left_encoder(make_encoder(0));
448 reader.set_drivetrain_right_encoder(make_encoder(1));
449
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800450 reader.set_intake_encoder(make_encoder(3));
Brian Silverman50826c02017-02-18 14:40:25 -0800451 reader.set_intake_absolute(make_unique<DigitalInput>(0));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800452 reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800453
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800454 reader.set_indexer_encoder(make_encoder(5));
Brianef030df2017-03-05 15:06:04 -0800455 reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800456
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800457 reader.set_turret_encoder(make_encoder(6));
Brianef030df2017-03-05 15:06:04 -0800458 reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800459
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800460 reader.set_hood_encoder(make_encoder(4));
461 reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800462
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800463 reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800464
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800465 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
466 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Austin Schuh8347cb62017-04-08 14:37:34 -0700467
468 reader.set_pwm_trigger(make_unique<DigitalInput>(7));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800469
470 reader.set_dma(make_unique<DMA>());
471 ::std::thread reader_thread(::std::ref(reader));
472
Brian Silvermanb4439852017-02-24 19:49:09 -0800473 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800474 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700475 imu.SetDummySPI(SPI::Port::kOnboardCS2);
476 auto imu_reset = make_unique<DigitalOutput>(6);
477 imu.set_reset(imu_reset.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800478 ::std::thread imu_thread(::std::ref(imu));
479
Sabina Davis7af11ad2019-02-03 01:16:45 -0800480 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
481 drivetrain_writer.set_left_controller0(
482 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)), true);
483 drivetrain_writer.set_right_controller0(
484 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800485 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
486
487 SuperstructureWriter superstructure_writer;
488 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700489 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800490 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700491 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800492 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700493 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800494 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700495 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800496 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700497 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800498 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700499 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800500 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700501 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700502
Austin Schuh6a8131b2017-04-08 15:39:22 -0700503 superstructure_writer.set_gear_servo(
504 ::std::unique_ptr<Servo>(new Servo(0)));
505
Austin Schuhc587c882017-03-29 21:33:10 -0700506 superstructure_writer.set_red_light(
507 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
508 superstructure_writer.set_green_light(
509 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
510 superstructure_writer.set_blue_light(
511 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
512
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800513 ::std::thread superstructure_writer_thread(
514 ::std::ref(superstructure_writer));
515
Adam Snaidere0554ef2017-03-11 23:02:45 -0800516 SolenoidWriter solenoid_writer;
517 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700518 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Adam Snaidere0554ef2017-03-11 23:02:45 -0800519
520 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
521
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800522 // Wait forever. Not much else to do...
523 while (true) {
524 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
525 if (r != 0) {
526 PLOG(WARNING, "infinite select failed");
527 } else {
528 PLOG(WARNING, "infinite select succeeded??\n");
529 }
530 }
531
532 LOG(ERROR, "Exiting WPILibRobot\n");
533
534 joystick_sender.Quit();
535 joystick_thread.join();
536 pdp_fetcher.Quit();
537 pdp_fetcher_thread.join();
538 reader.Quit();
539 reader_thread.join();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800540 imu.Quit();
541 imu_thread.join();
542
543 drivetrain_writer.Quit();
544 drivetrain_writer_thread.join();
545 superstructure_writer.Quit();
546 superstructure_writer_thread.join();
547
548 ::aos::Cleanup();
549 }
550};
551
Brian Silverman052e69d2017-02-12 16:19:55 -0800552} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800553} // namespace wpilib
554} // namespace y2017
555
556AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);