blob: 84e779d6ff13947c579ab265682d9cd69ff53dfa [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025#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"
Brian Silvermanf819b442019-01-20 16:51:04 -080028#include "aos/make_unique.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070029#include "aos/robot_state/robot_state_generated.h"
John Park33858a32018-09-28 23:05:48 -070030#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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070036#include "frc971/autonomous/auto_generated.h"
Austin Schuhed5b26d2019-12-05 20:51:59 -080037#include "frc971/autonomous/auto_mode_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070038#include "frc971/control_loops/control_loops_generated.h"
39#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
40#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070041#include "frc971/wpilib/ADIS16448.h"
42#include "frc971/wpilib/buffered_pcm.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/dma.h"
45#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davis7af11ad2019-02-03 01:16:45 -080046#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070047#include "frc971/wpilib/encoder_and_potentiometer.h"
48#include "frc971/wpilib/interrupt_edge_counting.h"
49#include "frc971/wpilib/joystick_sender.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070050#include "frc971/wpilib/logging_generated.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070051#include "frc971/wpilib/loop_output_handler.h"
52#include "frc971/wpilib/pdp_fetcher.h"
Austin Schuhbf29b6f2019-02-02 21:45:27 -080053#include "frc971/wpilib/sensor_reader.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070054#include "frc971/wpilib/wpilib_robot_base.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080055#include "y2017/constants.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070056#include "y2017/control_loops/superstructure/superstructure_output_generated.h"
57#include "y2017/control_loops/superstructure/superstructure_position_generated.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080058
Campbell Crowleyae6e8422017-02-05 12:38:50 -080059#ifndef M_PI
60#define M_PI 3.14159265358979323846
61#endif
62
Alex Perrycb7da4b2019-08-28 19:35:56 -070063namespace superstructure = ::y2017::control_loops::superstructure;
Brian Silverman052e69d2017-02-12 16:19:55 -080064using ::y2017::constants::Values;
Austin Schuh8347cb62017-04-08 14:37:34 -070065using ::aos::monotonic_clock;
66namespace chrono = ::std::chrono;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080067using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080068using aos::make_unique;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080069
70namespace y2017 {
71namespace wpilib {
72namespace {
Brian Silverman052e69d2017-02-12 16:19:55 -080073
Campbell Crowleyae6e8422017-02-05 12:38:50 -080074constexpr double kMaxBringupPower = 12.0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080075
76// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
77// DMA stuff and then removing the * 2.0 in *_translate.
78// The low bit is direction.
79
Brian Silverman052e69d2017-02-12 16:19:55 -080080// TODO(brian): Use ::std::max instead once we have C++14 so that can be
81// constexpr.
82template <typename T>
83constexpr T max(T a, T b) {
84 return (a > b) ? a : b;
85}
86template <typename T, typename... Rest>
87constexpr T max(T a, T b, T c, Rest... rest) {
88 return max(max(a, b), c, rest...);
89}
Campbell Crowleyae6e8422017-02-05 12:38:50 -080090
Campbell Crowleyae6e8422017-02-05 12:38:50 -080091double drivetrain_translate(int32_t in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080092 return static_cast<double>(in) /
93 Values::kDrivetrainEncoderCountsPerRevolution *
94 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -080095}
96
97double drivetrain_velocity_translate(double in) {
Brian Silverman052e69d2017-02-12 16:19:55 -080098 return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution *
99 Values::kDrivetrainEncoderRatio * 2.0 * M_PI;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800100}
101
Brian Silverman50826c02017-02-18 14:40:25 -0800102// TODO(Travis): Make sure the number of turns is right.
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800103double intake_pot_translate(double voltage) {
Brian Silverman50826c02017-02-18 14:40:25 -0800104 return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) *
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800105 (2 * M_PI /*radians*/);
106}
107
Brian Silverman052e69d2017-02-12 16:19:55 -0800108constexpr double kMaxFastEncoderPulsesPerSecond =
109 max(Values::kMaxDrivetrainEncoderPulsesPerSecond,
110 Values::kMaxShooterEncoderPulsesPerSecond);
111static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
112 "fast encoders are too fast");
113constexpr double kMaxMediumEncoderPulsesPerSecond =
114 max(Values::kMaxIntakeEncoderPulsesPerSecond,
115 Values::kMaxTurretEncoderPulsesPerSecond,
116 Values::kMaxIndexerEncoderPulsesPerSecond);
117static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
118 "medium encoders are too fast");
119constexpr double kMaxSlowEncoderPulsesPerSecond =
120 Values::kMaxHoodEncoderPulsesPerSecond;
121static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000,
122 "slow encoders are too fast");
Brianef030df2017-03-05 15:06:04 -0800123static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond,
124 "slow encoders are faster than medium?");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800125
126// Class to send position messages with sensor readings to our loops.
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800127class SensorReader : public ::frc971::wpilib::SensorReader {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800128 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800129 SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuha250b2d2019-05-27 16:14:02 -0700130 : ::frc971::wpilib::SensorReader(event_loop),
131 auto_mode_sender_(
132 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700133 "/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700134 superstructure_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700135 event_loop->MakeSender<superstructure::Position>(
136 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700137 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 event_loop
139 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
140 "/drivetrain")) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800141 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800142 // we should ever see.
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800143 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
144 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Brianef030df2017-03-05 15:06:04 -0800145 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800146 }
147
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800148 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800149 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800150 shooter_encoder_ = ::std::move(encoder);
151 }
152
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800153 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800154 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800155 intake_encoder_.set_encoder(::std::move(encoder));
156 }
157
158 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
159 intake_encoder_.set_potentiometer(::std::move(potentiometer));
160 }
161
Brian Silverman50826c02017-02-18 14:40:25 -0800162 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
163 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800164 }
165
Brian Silverman052e69d2017-02-12 16:19:55 -0800166 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
167 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800168 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800169 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800170 }
171
Brianef030df2017-03-05 15:06:04 -0800172 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
173 hall_filter_.Add(input.get());
174 indexer_counter_.set_input(input.get());
175 indexer_hall_ = ::std::move(input);
176 }
177
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800178 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800179 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800180 turret_counter_.set_encoder(encoder.get());
181 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800182 }
183
Brianef030df2017-03-05 15:06:04 -0800184 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
185 hall_filter_.Add(input.get());
186 turret_counter_.set_input(input.get());
187 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800188 }
189
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800190 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800191 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800192 hood_encoder_.set_encoder(::std::move(encoder));
193 }
194
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800195 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800196 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800197 hood_encoder_.set_index(::std::move(index));
198 }
199
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800200 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
201 autonomous_modes_.at(i) = ::std::move(sensor);
202 }
203
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800204 void Start() {
205 AddToDMA(&indexer_counter_);
206 AddToDMA(&hood_encoder_);
207 AddToDMA(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800208 }
209
210 void RunIteration() {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800211 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700212 auto builder = drivetrain_position_sender_.MakeBuilder();
213 frc971::control_loops::drivetrain::Position::Builder position_builder =
214 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
215 position_builder.add_right_encoder(
216 drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
217 position_builder.add_right_speed(drivetrain_velocity_translate(
218 drivetrain_right_encoder_->GetPeriod()));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800219
Alex Perrycb7da4b2019-08-28 19:35:56 -0700220 position_builder.add_left_encoder(
221 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
222 position_builder.add_left_speed(
223 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800224
Alex Perrycb7da4b2019-08-28 19:35:56 -0700225 builder.Send(position_builder.Finish());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800226 }
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800227 }
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800228
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800229 void RunDMAIteration() {
230 const auto values = constants::GetValues();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800231
232 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700233 auto builder = superstructure_position_sender_.MakeBuilder();
234 frc971::PotAndAbsolutePositionT intake;
235 CopyPosition(intake_encoder_, &intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800236 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800237 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800238 values.intake.pot_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700239 flatbuffers::Offset<frc971::PotAndAbsolutePosition> intake_offset =
240 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &intake);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800241
Alex Perrycb7da4b2019-08-28 19:35:56 -0700242 frc971::HallEffectAndPositionT indexer;
243 CopyPosition(indexer_counter_, &indexer,
Brianef030df2017-03-05 15:06:04 -0800244 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuh546a0382017-04-16 19:10:18 -0700245 Values::kIndexerEncoderRatio, true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700246 flatbuffers::Offset<frc971::HallEffectAndPosition> indexer_offset =
247 frc971::HallEffectAndPosition::Pack(*builder.fbb(), &indexer);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800248
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 frc971::IndexPositionT hood;
250 CopyPosition(hood_encoder_, &hood,
Brian Silverman50826c02017-02-18 14:40:25 -0800251 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800252 Values::kHoodEncoderRatio, true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700253 flatbuffers::Offset<frc971::IndexPosition> hood_offset =
254 frc971::IndexPosition::Pack(*builder.fbb(), &hood);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800255
Alex Perrycb7da4b2019-08-28 19:35:56 -0700256 frc971::HallEffectAndPositionT turret;
257 CopyPosition(turret_counter_, &turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800258 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800259 Values::kTurretEncoderRatio, false);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260 flatbuffers::Offset<frc971::HallEffectAndPosition> turret_offset =
261 frc971::HallEffectAndPosition::Pack(*builder.fbb(), &turret);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800262
Alex Perrycb7da4b2019-08-28 19:35:56 -0700263 superstructure::ColumnPosition::Builder column_builder =
264 builder.MakeBuilder<superstructure::ColumnPosition>();
265 column_builder.add_indexer(indexer_offset);
266 column_builder.add_turret(turret_offset);
267 flatbuffers::Offset<superstructure::ColumnPosition> column_offset =
268 column_builder.Finish();
269
270 superstructure::Position::Builder position_builder =
271 builder.MakeBuilder<superstructure::Position>();
272
273 position_builder.add_column(column_offset);
274 position_builder.add_hood(hood_offset);
275 position_builder.add_intake(intake_offset);
276 position_builder.add_theta_shooter(
277 encoder_translate(shooter_encoder_->GetRaw(),
278 Values::kShooterEncoderCountsPerRevolution,
279 Values::kShooterEncoderRatio));
280
281 builder.Send(position_builder.Finish());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800282 }
283
284 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700285 auto builder = auto_mode_sender_.MakeBuilder();
286 ::frc971::autonomous::AutonomousMode::Builder auto_builder =
287 builder.MakeBuilder<::frc971::autonomous::AutonomousMode>();
288
289 int mode = 0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800290 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700291 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700292 mode |= 1 << i;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800293 }
294 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295 auto_builder.add_mode(mode);
296 builder.Send(auto_builder.Finish());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800297 }
298 }
299
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800300 private:
Austin Schuha250b2d2019-05-27 16:14:02 -0700301 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
303 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700304 drivetrain_position_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700305
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800306 DigitalGlitchFilter hall_filter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800307
Austin Schuh2a3e0632018-02-19 16:24:49 -0800308 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800309
Brian Silverman052e69d2017-02-12 16:19:55 -0800310 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800311 ::std::unique_ptr<DigitalInput> indexer_hall_;
312 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800313
Brianef030df2017-03-05 15:06:04 -0800314 ::std::unique_ptr<Encoder> turret_encoder_;
315 ::std::unique_ptr<DigitalInput> turret_hall_;
316 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
317
Brian Silverman7cce2d32017-02-19 21:48:48 -0800318 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800319 ::std::unique_ptr<Encoder> shooter_encoder_;
320
321 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800322};
323
Adam Snaidere0554ef2017-03-11 23:02:45 -0800324class SolenoidWriter {
325 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800326 SolenoidWriter(::aos::ShmEventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700327 : superstructure_output_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328 event_loop->MakeFetcher<superstructure::Output>(
329 "/superstructure")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700330 event_loop->set_name("Solenoids");
331 event_loop->SetRuntimeRealtimePriority(27);
332
333 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
334 ::std::chrono::milliseconds(20),
335 ::std::chrono::milliseconds(1));
336 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800337
338 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
339
Austin Schuh8347cb62017-04-08 14:37:34 -0700340 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800341 lights_ = ::std::move(s);
342 }
343
Austin Schuh8347cb62017-04-08 14:37:34 -0700344 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700345 rgb_lights_ = ::std::move(s);
346 }
347
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700348 void Loop(const int iterations) {
349 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700350 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800351 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800352
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700353 {
354 superstructure_output_fetcher_.Fetch();
355 if (superstructure_output_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700356 lights_->Set(superstructure_output_fetcher_->lights_on());
357 rgb_lights_->Set(superstructure_output_fetcher_->red_light_on() |
358 superstructure_output_fetcher_->green_light_on() |
359 superstructure_output_fetcher_->blue_light_on());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700360 }
361 }
362
363 pcm_.Flush();
364 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800365
366 private:
367 ::frc971::wpilib::BufferedPcm pcm_;
368
Austin Schuhc587c882017-03-29 21:33:10 -0700369 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800370
Alex Perrycb7da4b2019-08-28 19:35:56 -0700371 ::aos::Fetcher<::y2017::control_loops::superstructure::Output>
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700372 superstructure_output_fetcher_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800373};
374
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700375class SuperstructureWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700376 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800377 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800378 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700379 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
380 event_loop, "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800381
Austin Schuh8347cb62017-04-08 14:37:34 -0700382 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800383 intake_victor_ = ::std::move(t);
384 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700385 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800386 intake_rollers_victor_ = ::std::move(t);
387 }
388
Austin Schuh8347cb62017-04-08 14:37:34 -0700389 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800390 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800391 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700392 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800393 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800394 }
395
Austin Schuh6a8131b2017-04-08 15:39:22 -0700396 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
397 gear_servo_ = ::std::move(t);
398 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700399 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800400 shooter_victor_ = ::std::move(t);
401 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700402 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800403 turret_victor_ = ::std::move(t);
404 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700405 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800406 hood_victor_ = ::std::move(t);
407 }
408
Austin Schuhc587c882017-03-29 21:33:10 -0700409 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
410 red_light_ = ::std::move(t);
411 }
412 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
413 green_light_ = ::std::move(t);
414 }
415 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
416 blue_light_ = ::std::move(t);
417 }
418
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800419 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420 virtual void Write(const superstructure::Output &output) override {
421 intake_victor_->SetSpeed(::aos::Clip(output.voltage_intake(),
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800422 -kMaxBringupPower, kMaxBringupPower) /
423 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700424 intake_rollers_victor_->SetSpeed(output.voltage_intake_rollers() / 12.0);
425 indexer_victor_->SetSpeed(-output.voltage_indexer() / 12.0);
426 indexer_roller_victor_->SetSpeed(output.voltage_indexer_rollers() / 12.0);
427 turret_victor_->SetSpeed(::aos::Clip(-output.voltage_turret(),
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800428 -kMaxBringupPower, kMaxBringupPower) /
429 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700430 hood_victor_->SetSpeed(::aos::Clip(output.voltage_hood(), -kMaxBringupPower,
431 kMaxBringupPower) /
432 12.0);
433 shooter_victor_->SetSpeed(output.voltage_shooter() / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700434
Alex Perrycb7da4b2019-08-28 19:35:56 -0700435 red_light_->Set(output.red_light_on());
436 green_light_->Set(output.green_light_on());
437 blue_light_->Set(output.blue_light_on());
Austin Schuh6a8131b2017-04-08 15:39:22 -0700438
Alex Perrycb7da4b2019-08-28 19:35:56 -0700439 gear_servo_->SetPosition(output.gear_servo());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800440 }
441
442 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700443 AOS_LOG(WARNING, "Superstructure output too old.\n");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800444 intake_victor_->SetDisabled();
445 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800446 indexer_victor_->SetDisabled();
447 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800448 turret_victor_->SetDisabled();
449 hood_victor_->SetDisabled();
450 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700451
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800452 gear_servo_->SetRaw(0);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700453
Austin Schuhc587c882017-03-29 21:33:10 -0700454 red_light_->Set(true);
455 green_light_->Set(true);
456 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800457 }
458
Austin Schuh8347cb62017-04-08 14:37:34 -0700459 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
460 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
461 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700462
Austin Schuh6a8131b2017-04-08 15:39:22 -0700463 ::std::unique_ptr<::frc::Servo> gear_servo_;
464
Austin Schuhc587c882017-03-29 21:33:10 -0700465 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800466};
467
468class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
469 public:
470 ::std::unique_ptr<Encoder> make_encoder(int index) {
471 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
472 Encoder::k4X);
473 }
474
475 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700476 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
477 aos::configuration::ReadConfig("config.json");
478
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700479 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700481 ::frc971::wpilib::JoystickSender joystick_sender(
482 &joystick_sender_event_loop);
483 AddLoop(&joystick_sender_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800484
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700485 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700486 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700487 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
488 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800489
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700490 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700491 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700492 SensorReader sensor_reader(&sensor_reader_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800493
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700494 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
495 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800496
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700497 sensor_reader.set_intake_encoder(make_encoder(3));
498 sensor_reader.set_intake_absolute(make_unique<DigitalInput>(0));
499 sensor_reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800500
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700501 sensor_reader.set_indexer_encoder(make_encoder(5));
502 sensor_reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800503
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700504 sensor_reader.set_turret_encoder(make_encoder(6));
505 sensor_reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800506
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700507 sensor_reader.set_hood_encoder(make_encoder(4));
508 sensor_reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800509
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700510 sensor_reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800511
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700512 sensor_reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
513 sensor_reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800514
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700515 sensor_reader.set_pwm_trigger(true);
Austin Schuh8347cb62017-04-08 14:37:34 -0700516
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700517 AddLoop(&sensor_reader_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800518
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700519 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700520 ::aos::ShmEventLoop imu_event_loop(&config.message());
Brian Silvermanb4439852017-02-24 19:49:09 -0800521 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700522 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, SPI::Port::kOnboardCS1,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800523 imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700524 imu.SetDummySPI(SPI::Port::kOnboardCS2);
525 auto imu_reset = make_unique<DigitalOutput>(6);
526 imu.set_reset(imu_reset.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700527 AddLoop(&imu_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800528
Alex Perrycb7da4b2019-08-28 19:35:56 -0700529 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700530 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davis7af11ad2019-02-03 01:16:45 -0800531 drivetrain_writer.set_left_controller0(
532 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)), true);
533 drivetrain_writer.set_right_controller0(
534 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800535
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700536 SuperstructureWriter superstructure_writer(&output_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800537 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700538 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800539 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700540 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800541 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700542 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800543 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700544 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800545 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700546 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800547 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700548 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800549 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700550 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700551
Austin Schuh6a8131b2017-04-08 15:39:22 -0700552 superstructure_writer.set_gear_servo(
553 ::std::unique_ptr<Servo>(new Servo(0)));
554
Austin Schuhc587c882017-03-29 21:33:10 -0700555 superstructure_writer.set_red_light(
556 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
557 superstructure_writer.set_green_light(
558 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
559 superstructure_writer.set_blue_light(
560 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
561
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700562 AddLoop(&output_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800563
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700564 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700565 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700566 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800567 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700568 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700569 AddLoop(&solenoid_writer_event_loop);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800570
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700571 RunLoops();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800572 }
573};
574
Brian Silverman052e69d2017-02-12 16:19:55 -0800575} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800576} // namespace wpilib
577} // namespace y2017
578
579AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);