blob: d08909f7919a853ece88f44863e6617b7551e909 [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"
37#include "frc971/control_loops/control_loops_generated.h"
38#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
39#include "frc971/control_loops/drivetrain/drivetrain_position_generated.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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070049#include "frc971/wpilib/logging_generated.h"
Austin Schuh8347cb62017-04-08 14:37:34 -070050#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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070055#include "y2017/control_loops/superstructure/superstructure_output_generated.h"
56#include "y2017/control_loops/superstructure/superstructure_position_generated.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080057
Campbell Crowleyae6e8422017-02-05 12:38:50 -080058#ifndef M_PI
59#define M_PI 3.14159265358979323846
60#endif
61
Alex Perrycb7da4b2019-08-28 19:35:56 -070062namespace superstructure = ::y2017::control_loops::superstructure;
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>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 "/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700133 superstructure_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134 event_loop->MakeSender<superstructure::Position>(
135 "/superstructure")),
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700136 drivetrain_position_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 event_loop
138 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
139 "/drivetrain")) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800140 // Set to filter out anything shorter than 1/4 of the minimum pulse width
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800141 // we should ever see.
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800142 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
143 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
Brianef030df2017-03-05 15:06:04 -0800144 hall_filter_.SetPeriodNanoSeconds(100000);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800145 }
146
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800147 void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800148 fast_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800149 shooter_encoder_ = ::std::move(encoder);
150 }
151
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800152 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800153 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800154 intake_encoder_.set_encoder(::std::move(encoder));
155 }
156
157 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
158 intake_encoder_.set_potentiometer(::std::move(potentiometer));
159 }
160
Brian Silverman50826c02017-02-18 14:40:25 -0800161 void set_intake_absolute(::std::unique_ptr<DigitalInput> input) {
162 intake_encoder_.set_absolute_pwm(::std::move(input));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800163 }
164
Brian Silverman052e69d2017-02-12 16:19:55 -0800165 void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) {
166 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800167 indexer_counter_.set_encoder(encoder.get());
Brian Silverman052e69d2017-02-12 16:19:55 -0800168 indexer_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800169 }
170
Brianef030df2017-03-05 15:06:04 -0800171 void set_indexer_hall(::std::unique_ptr<DigitalInput> input) {
172 hall_filter_.Add(input.get());
173 indexer_counter_.set_input(input.get());
174 indexer_hall_ = ::std::move(input);
175 }
176
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800177 void set_turret_encoder(::std::unique_ptr<Encoder> encoder) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800178 medium_encoder_filter_.Add(encoder.get());
Brianef030df2017-03-05 15:06:04 -0800179 turret_counter_.set_encoder(encoder.get());
180 turret_encoder_ = ::std::move(encoder);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800181 }
182
Brianef030df2017-03-05 15:06:04 -0800183 void set_turret_hall(::std::unique_ptr<DigitalInput> input) {
184 hall_filter_.Add(input.get());
185 turret_counter_.set_input(input.get());
186 turret_hall_ = ::std::move(input);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800187 }
188
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800189 void set_hood_encoder(::std::unique_ptr<Encoder> encoder) {
Brianef030df2017-03-05 15:06:04 -0800190 medium_encoder_filter_.Add(encoder.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800191 hood_encoder_.set_encoder(::std::move(encoder));
192 }
193
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800194 void set_hood_index(::std::unique_ptr<DigitalInput> index) {
Brianef030df2017-03-05 15:06:04 -0800195 medium_encoder_filter_.Add(index.get());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800196 hood_encoder_.set_index(::std::move(index));
197 }
198
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800199 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
200 autonomous_modes_.at(i) = ::std::move(sensor);
201 }
202
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800203 void Start() {
204 AddToDMA(&indexer_counter_);
205 AddToDMA(&hood_encoder_);
206 AddToDMA(&turret_counter_);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800207 }
208
209 void RunIteration() {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800210 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211 auto builder = drivetrain_position_sender_.MakeBuilder();
212 frc971::control_loops::drivetrain::Position::Builder position_builder =
213 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
214 position_builder.add_right_encoder(
215 drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
216 position_builder.add_right_speed(drivetrain_velocity_translate(
217 drivetrain_right_encoder_->GetPeriod()));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800218
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219 position_builder.add_left_encoder(
220 -drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
221 position_builder.add_left_speed(
222 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800223
Alex Perrycb7da4b2019-08-28 19:35:56 -0700224 builder.Send(position_builder.Finish());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800225 }
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800226 }
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800227
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800228 void RunDMAIteration() {
229 const auto values = constants::GetValues();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800230
231 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700232 auto builder = superstructure_position_sender_.MakeBuilder();
233 frc971::PotAndAbsolutePositionT intake;
234 CopyPosition(intake_encoder_, &intake,
Brian Silverman50826c02017-02-18 14:40:25 -0800235 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800236 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800237 values.intake.pot_offset);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700238 flatbuffers::Offset<frc971::PotAndAbsolutePosition> intake_offset =
239 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &intake);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800240
Alex Perrycb7da4b2019-08-28 19:35:56 -0700241 frc971::HallEffectAndPositionT indexer;
242 CopyPosition(indexer_counter_, &indexer,
Brianef030df2017-03-05 15:06:04 -0800243 Values::kIndexerEncoderCountsPerRevolution,
Austin Schuh546a0382017-04-16 19:10:18 -0700244 Values::kIndexerEncoderRatio, true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700245 flatbuffers::Offset<frc971::HallEffectAndPosition> indexer_offset =
246 frc971::HallEffectAndPosition::Pack(*builder.fbb(), &indexer);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800247
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 frc971::IndexPositionT hood;
249 CopyPosition(hood_encoder_, &hood,
Brian Silverman50826c02017-02-18 14:40:25 -0800250 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800251 Values::kHoodEncoderRatio, true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700252 flatbuffers::Offset<frc971::IndexPosition> hood_offset =
253 frc971::IndexPosition::Pack(*builder.fbb(), &hood);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800254
Alex Perrycb7da4b2019-08-28 19:35:56 -0700255 frc971::HallEffectAndPositionT turret;
256 CopyPosition(turret_counter_, &turret,
Brian Silverman50826c02017-02-18 14:40:25 -0800257 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800258 Values::kTurretEncoderRatio, false);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259 flatbuffers::Offset<frc971::HallEffectAndPosition> turret_offset =
260 frc971::HallEffectAndPosition::Pack(*builder.fbb(), &turret);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800261
Alex Perrycb7da4b2019-08-28 19:35:56 -0700262 superstructure::ColumnPosition::Builder column_builder =
263 builder.MakeBuilder<superstructure::ColumnPosition>();
264 column_builder.add_indexer(indexer_offset);
265 column_builder.add_turret(turret_offset);
266 flatbuffers::Offset<superstructure::ColumnPosition> column_offset =
267 column_builder.Finish();
268
269 superstructure::Position::Builder position_builder =
270 builder.MakeBuilder<superstructure::Position>();
271
272 position_builder.add_column(column_offset);
273 position_builder.add_hood(hood_offset);
274 position_builder.add_intake(intake_offset);
275 position_builder.add_theta_shooter(
276 encoder_translate(shooter_encoder_->GetRaw(),
277 Values::kShooterEncoderCountsPerRevolution,
278 Values::kShooterEncoderRatio));
279
280 builder.Send(position_builder.Finish());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800281 }
282
283 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 auto builder = auto_mode_sender_.MakeBuilder();
285 ::frc971::autonomous::AutonomousMode::Builder auto_builder =
286 builder.MakeBuilder<::frc971::autonomous::AutonomousMode>();
287
288 int mode = 0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800289 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700290 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700291 mode |= 1 << i;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800292 }
293 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294 auto_builder.add_mode(mode);
295 builder.Send(auto_builder.Finish());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800296 }
297 }
298
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800299 private:
Austin Schuha250b2d2019-05-27 16:14:02 -0700300 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
302 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700303 drivetrain_position_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700304
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800305 DigitalGlitchFilter hall_filter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800306
Austin Schuh2a3e0632018-02-19 16:24:49 -0800307 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800308
Brian Silverman052e69d2017-02-12 16:19:55 -0800309 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800310 ::std::unique_ptr<DigitalInput> indexer_hall_;
311 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800312
Brianef030df2017-03-05 15:06:04 -0800313 ::std::unique_ptr<Encoder> turret_encoder_;
314 ::std::unique_ptr<DigitalInput> turret_hall_;
315 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
316
Brian Silverman7cce2d32017-02-19 21:48:48 -0800317 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800318 ::std::unique_ptr<Encoder> shooter_encoder_;
319
320 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800321};
322
Adam Snaidere0554ef2017-03-11 23:02:45 -0800323class SolenoidWriter {
324 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700325 SolenoidWriter(::aos::EventLoop *event_loop)
326 : superstructure_output_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700327 event_loop->MakeFetcher<superstructure::Output>(
328 "/superstructure")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700329 event_loop->set_name("Solenoids");
330 event_loop->SetRuntimeRealtimePriority(27);
331
332 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
333 ::std::chrono::milliseconds(20),
334 ::std::chrono::milliseconds(1));
335 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800336
337 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
338
Austin Schuh8347cb62017-04-08 14:37:34 -0700339 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800340 lights_ = ::std::move(s);
341 }
342
Austin Schuh8347cb62017-04-08 14:37:34 -0700343 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700344 rgb_lights_ = ::std::move(s);
345 }
346
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700347 void Loop(const int iterations) {
348 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700349 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800350 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800351
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700352 {
353 superstructure_output_fetcher_.Fetch();
354 if (superstructure_output_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700355 lights_->Set(superstructure_output_fetcher_->lights_on());
356 rgb_lights_->Set(superstructure_output_fetcher_->red_light_on() |
357 superstructure_output_fetcher_->green_light_on() |
358 superstructure_output_fetcher_->blue_light_on());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700359 }
360 }
361
362 pcm_.Flush();
363 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800364
365 private:
366 ::frc971::wpilib::BufferedPcm pcm_;
367
Austin Schuhc587c882017-03-29 21:33:10 -0700368 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800369
Alex Perrycb7da4b2019-08-28 19:35:56 -0700370 ::aos::Fetcher<::y2017::control_loops::superstructure::Output>
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700371 superstructure_output_fetcher_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800372};
373
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700374class SuperstructureWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700375 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800376 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800377 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700378 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
379 event_loop, "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800380
Austin Schuh8347cb62017-04-08 14:37:34 -0700381 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800382 intake_victor_ = ::std::move(t);
383 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700384 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800385 intake_rollers_victor_ = ::std::move(t);
386 }
387
Austin Schuh8347cb62017-04-08 14:37:34 -0700388 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800389 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800390 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700391 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800392 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800393 }
394
Austin Schuh6a8131b2017-04-08 15:39:22 -0700395 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
396 gear_servo_ = ::std::move(t);
397 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700398 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800399 shooter_victor_ = ::std::move(t);
400 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700401 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800402 turret_victor_ = ::std::move(t);
403 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700404 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800405 hood_victor_ = ::std::move(t);
406 }
407
Austin Schuhc587c882017-03-29 21:33:10 -0700408 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
409 red_light_ = ::std::move(t);
410 }
411 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
412 green_light_ = ::std::move(t);
413 }
414 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
415 blue_light_ = ::std::move(t);
416 }
417
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800418 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700419 virtual void Write(const superstructure::Output &output) override {
420 intake_victor_->SetSpeed(::aos::Clip(output.voltage_intake(),
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800421 -kMaxBringupPower, kMaxBringupPower) /
422 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700423 intake_rollers_victor_->SetSpeed(output.voltage_intake_rollers() / 12.0);
424 indexer_victor_->SetSpeed(-output.voltage_indexer() / 12.0);
425 indexer_roller_victor_->SetSpeed(output.voltage_indexer_rollers() / 12.0);
426 turret_victor_->SetSpeed(::aos::Clip(-output.voltage_turret(),
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800427 -kMaxBringupPower, kMaxBringupPower) /
428 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700429 hood_victor_->SetSpeed(::aos::Clip(output.voltage_hood(), -kMaxBringupPower,
430 kMaxBringupPower) /
431 12.0);
432 shooter_victor_->SetSpeed(output.voltage_shooter() / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700433
Alex Perrycb7da4b2019-08-28 19:35:56 -0700434 red_light_->Set(output.red_light_on());
435 green_light_->Set(output.green_light_on());
436 blue_light_->Set(output.blue_light_on());
Austin Schuh6a8131b2017-04-08 15:39:22 -0700437
Alex Perrycb7da4b2019-08-28 19:35:56 -0700438 gear_servo_->SetPosition(output.gear_servo());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800439 }
440
441 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700442 AOS_LOG(WARNING, "Superstructure output too old.\n");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800443 intake_victor_->SetDisabled();
444 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800445 indexer_victor_->SetDisabled();
446 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800447 turret_victor_->SetDisabled();
448 hood_victor_->SetDisabled();
449 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700450
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800451 gear_servo_->SetRaw(0);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700452
Austin Schuhc587c882017-03-29 21:33:10 -0700453 red_light_->Set(true);
454 green_light_->Set(true);
455 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800456 }
457
Austin Schuh8347cb62017-04-08 14:37:34 -0700458 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
459 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
460 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700461
Austin Schuh6a8131b2017-04-08 15:39:22 -0700462 ::std::unique_ptr<::frc::Servo> gear_servo_;
463
Austin Schuhc587c882017-03-29 21:33:10 -0700464 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800465};
466
467class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
468 public:
469 ::std::unique_ptr<Encoder> make_encoder(int index) {
470 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
471 Encoder::k4X);
472 }
473
474 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700475 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
476 aos::configuration::ReadConfig("config.json");
477
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700478 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700479 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700480 ::frc971::wpilib::JoystickSender joystick_sender(
481 &joystick_sender_event_loop);
482 AddLoop(&joystick_sender_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800483
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700484 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700485 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700486 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
487 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800488
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700489 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700490 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700491 SensorReader sensor_reader(&sensor_reader_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800492
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700493 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
494 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800495
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700496 sensor_reader.set_intake_encoder(make_encoder(3));
497 sensor_reader.set_intake_absolute(make_unique<DigitalInput>(0));
498 sensor_reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800499
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700500 sensor_reader.set_indexer_encoder(make_encoder(5));
501 sensor_reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800502
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700503 sensor_reader.set_turret_encoder(make_encoder(6));
504 sensor_reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800505
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700506 sensor_reader.set_hood_encoder(make_encoder(4));
507 sensor_reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800508
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700509 sensor_reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800510
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700511 sensor_reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
512 sensor_reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800513
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700514 sensor_reader.set_pwm_trigger(true);
Austin Schuh8347cb62017-04-08 14:37:34 -0700515
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700516 AddLoop(&sensor_reader_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800517
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700518 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700519 ::aos::ShmEventLoop imu_event_loop(&config.message());
Brian Silvermanb4439852017-02-24 19:49:09 -0800520 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700521 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, SPI::Port::kOnboardCS1,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800522 imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700523 imu.SetDummySPI(SPI::Port::kOnboardCS2);
524 auto imu_reset = make_unique<DigitalOutput>(6);
525 imu.set_reset(imu_reset.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700526 AddLoop(&imu_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800527
Alex Perrycb7da4b2019-08-28 19:35:56 -0700528 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700529 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davis7af11ad2019-02-03 01:16:45 -0800530 drivetrain_writer.set_left_controller0(
531 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)), true);
532 drivetrain_writer.set_right_controller0(
533 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800534
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700535 SuperstructureWriter superstructure_writer(&output_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800536 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700537 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800538 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700539 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800540 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700541 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800542 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700543 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800544 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700545 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800546 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700547 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800548 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700549 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700550
Austin Schuh6a8131b2017-04-08 15:39:22 -0700551 superstructure_writer.set_gear_servo(
552 ::std::unique_ptr<Servo>(new Servo(0)));
553
Austin Schuhc587c882017-03-29 21:33:10 -0700554 superstructure_writer.set_red_light(
555 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
556 superstructure_writer.set_green_light(
557 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
558 superstructure_writer.set_blue_light(
559 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
560
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700561 AddLoop(&output_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800562
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700563 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700564 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700565 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800566 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700567 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700568 AddLoop(&solenoid_writer_event_loop);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800569
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700570 RunLoops();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800571 }
572};
573
Brian Silverman052e69d2017-02-12 16:19:55 -0800574} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800575} // namespace wpilib
576} // namespace y2017
577
578AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);