blob: 822c3f4cd82d5945f05711d87f72af181ae2fdc6 [file] [log] [blame]
Campbell Crowleyae6e8422017-02-05 12:38:50 -08001#include <unistd.h>
Campbell Crowleyae6e8422017-02-05 12:38:50 -08002
Campbell Crowleyae6e8422017-02-05 12:38:50 -08003#include <array>
Austin Schuh8347cb62017-04-08 14:37:34 -07004#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
Brian Silverman50826c02017-02-18 14:40:25 -08006#include <cmath>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <cstdio>
8#include <cstring>
Austin Schuh8347cb62017-04-08 14:37:34 -07009#include <functional>
Tyler Chatowbf0609c2021-07-31 16:13:27 -070010#include <memory>
Austin Schuh8347cb62017-04-08 14:37:34 -070011#include <mutex>
12#include <thread>
Campbell Crowleyae6e8422017-02-05 12:38:50 -080013
Parker Schuhd3b7a8872018-02-19 16:42:27 -080014#include "frc971/wpilib/ahal/AnalogInput.h"
15#include "frc971/wpilib/ahal/Compressor.h"
16#include "frc971/wpilib/ahal/Counter.h"
17#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
18#include "frc971/wpilib/ahal/DriverStation.h"
19#include "frc971/wpilib/ahal/Encoder.h"
20#include "frc971/wpilib/ahal/Relay.h"
21#include "frc971/wpilib/ahal/Servo.h"
22#include "frc971/wpilib/ahal/VictorSP.h"
Campbell Crowleyae6e8422017-02-05 12:38:50 -080023#undef ERROR
24
John Park33858a32018-09-28 23:05:48 -070025#include "aos/commonmath.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070026#include "aos/events/shm_event_loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080027#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070028#include "aos/logging/logging.h"
John Park33858a32018-09-28 23:05:48 -070029#include "aos/stl_mutex/stl_mutex.h"
30#include "aos/time/time.h"
31#include "aos/util/compiler_memory_barrier.h"
32#include "aos/util/log_interval.h"
33#include "aos/util/phased_loop.h"
34#include "aos/util/wrapping_counter.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070035#include "frc971/autonomous/auto_generated.h"
Austin Schuhed5b26d2019-12-05 20:51:59 -080036#include "frc971/autonomous/auto_mode_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070037#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"
James Kuszmaul7077d342021-06-09 20:23:58 -070040#include "frc971/input/robot_state_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"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080057#include "y2017/control_loops/superstructure/superstructure_position_static.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;
Austin Schuh8347cb62017-04-08 14:37:34 -070064using ::aos::monotonic_clock;
James Kuszmaul7077d342021-06-09 20:23:58 -070065using ::y2017::constants::Values;
Austin Schuh8347cb62017-04-08 14:37:34 -070066namespace chrono = ::std::chrono;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080067using namespace frc;
Vinay Sivae52a6b32021-07-10 15:19:26 -070068using std::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_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800135 event_loop->MakeSender<superstructure::PositionStatic>(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700136 "/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
milind1f1dca32021-07-03 13:50:07 -0700225 builder.CheckOk(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 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800233 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
234 superstructure_position_sender_.MakeStaticBuilder();
235
236 CopyPosition(intake_encoder_, builder->add_intake(),
Brian Silverman50826c02017-02-18 14:40:25 -0800237 Values::kIntakeEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800238 Values::kIntakeEncoderRatio, intake_pot_translate, true,
Brian Silverman052e69d2017-02-12 16:19:55 -0800239 values.intake.pot_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800240 CopyPosition(hood_encoder_, builder->add_hood(),
Brian Silverman50826c02017-02-18 14:40:25 -0800241 Values::kHoodEncoderCountsPerRevolution,
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800242 Values::kHoodEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800243
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800244 superstructure::ColumnPositionStatic *column = builder->add_column();
245 CopyPosition(turret_counter_, column->add_turret(),
Brian Silverman50826c02017-02-18 14:40:25 -0800246 Values::kTurretEncoderCountsPerRevolution,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800247 Values::kTurretEncoderRatio, false);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800248 CopyPosition(indexer_counter_, column->add_indexer(),
249 Values::kIndexerEncoderCountsPerRevolution,
250 Values::kIndexerEncoderRatio, true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800251
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800252 builder->set_theta_shooter(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700253 encoder_translate(shooter_encoder_->GetRaw(),
254 Values::kShooterEncoderCountsPerRevolution,
255 Values::kShooterEncoderRatio));
256
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800257 builder.CheckOk(builder.Send());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800258 }
259
260 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700261 auto builder = auto_mode_sender_.MakeBuilder();
262 ::frc971::autonomous::AutonomousMode::Builder auto_builder =
263 builder.MakeBuilder<::frc971::autonomous::AutonomousMode>();
264
265 int mode = 0;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800266 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
Austin Schuh8347cb62017-04-08 14:37:34 -0700267 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700268 mode |= 1 << i;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800269 }
270 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700271 auto_builder.add_mode(mode);
milind1f1dca32021-07-03 13:50:07 -0700272 builder.CheckOk(builder.Send(auto_builder.Finish()));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800273 }
274 }
275
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800276 private:
Austin Schuha250b2d2019-05-27 16:14:02 -0700277 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800278 ::aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700279 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700280 drivetrain_position_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700281
Austin Schuhbf29b6f2019-02-02 21:45:27 -0800282 DigitalGlitchFilter hall_filter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800283
Austin Schuh2a3e0632018-02-19 16:24:49 -0800284 ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800285
Brian Silverman052e69d2017-02-12 16:19:55 -0800286 ::std::unique_ptr<Encoder> indexer_encoder_;
Brianef030df2017-03-05 15:06:04 -0800287 ::std::unique_ptr<DigitalInput> indexer_hall_;
288 ::frc971::wpilib::DMAEdgeCounter indexer_counter_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800289
Brianef030df2017-03-05 15:06:04 -0800290 ::std::unique_ptr<Encoder> turret_encoder_;
291 ::std::unique_ptr<DigitalInput> turret_hall_;
292 ::frc971::wpilib::DMAEdgeCounter turret_counter_;
293
Brian Silverman7cce2d32017-02-19 21:48:48 -0800294 ::frc971::wpilib::DMAEncoder hood_encoder_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800295 ::std::unique_ptr<Encoder> shooter_encoder_;
296
297 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800298};
299
Adam Snaidere0554ef2017-03-11 23:02:45 -0800300class SolenoidWriter {
301 public:
Austin Schuh217a9782019-12-21 23:02:50 -0800302 SolenoidWriter(::aos::ShmEventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700303 : superstructure_output_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 event_loop->MakeFetcher<superstructure::Output>(
305 "/superstructure")) {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700306 event_loop->set_name("Solenoids");
307 event_loop->SetRuntimeRealtimePriority(27);
308
309 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
310 ::std::chrono::milliseconds(20),
311 ::std::chrono::milliseconds(1));
312 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800313
314 ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; }
315
Austin Schuh8347cb62017-04-08 14:37:34 -0700316 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Adam Snaidere0554ef2017-03-11 23:02:45 -0800317 lights_ = ::std::move(s);
318 }
319
Austin Schuh8347cb62017-04-08 14:37:34 -0700320 void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhc587c882017-03-29 21:33:10 -0700321 rgb_lights_ = ::std::move(s);
322 }
323
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700324 void Loop(const int iterations) {
325 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700326 AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800327 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800328
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700329 {
330 superstructure_output_fetcher_.Fetch();
331 if (superstructure_output_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700332 lights_->Set(superstructure_output_fetcher_->lights_on());
333 rgb_lights_->Set(superstructure_output_fetcher_->red_light_on() |
334 superstructure_output_fetcher_->green_light_on() |
335 superstructure_output_fetcher_->blue_light_on());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700336 }
337 }
338
339 pcm_.Flush();
340 }
Adam Snaidere0554ef2017-03-11 23:02:45 -0800341
342 private:
343 ::frc971::wpilib::BufferedPcm pcm_;
344
Austin Schuhc587c882017-03-29 21:33:10 -0700345 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800346
Alex Perrycb7da4b2019-08-28 19:35:56 -0700347 ::aos::Fetcher<::y2017::control_loops::superstructure::Output>
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700348 superstructure_output_fetcher_;
Adam Snaidere0554ef2017-03-11 23:02:45 -0800349};
350
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700351class SuperstructureWriter
Alex Perrycb7da4b2019-08-28 19:35:56 -0700352 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800353 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800354 SuperstructureWriter(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -0700355 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
356 event_loop, "/superstructure") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800357
Austin Schuh8347cb62017-04-08 14:37:34 -0700358 void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800359 intake_victor_ = ::std::move(t);
360 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700361 void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800362 intake_rollers_victor_ = ::std::move(t);
363 }
364
Austin Schuh8347cb62017-04-08 14:37:34 -0700365 void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800366 indexer_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800367 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700368 void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) {
Brian Silverman052e69d2017-02-12 16:19:55 -0800369 indexer_roller_victor_ = ::std::move(t);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800370 }
371
Austin Schuh6a8131b2017-04-08 15:39:22 -0700372 void set_gear_servo(::std::unique_ptr<::frc::Servo> t) {
373 gear_servo_ = ::std::move(t);
374 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700375 void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800376 shooter_victor_ = ::std::move(t);
377 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700378 void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800379 turret_victor_ = ::std::move(t);
380 }
Austin Schuh8347cb62017-04-08 14:37:34 -0700381 void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) {
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800382 hood_victor_ = ::std::move(t);
383 }
384
Austin Schuhc587c882017-03-29 21:33:10 -0700385 void set_red_light(::std::unique_ptr<DigitalOutput> t) {
386 red_light_ = ::std::move(t);
387 }
388 void set_green_light(::std::unique_ptr<DigitalOutput> t) {
389 green_light_ = ::std::move(t);
390 }
391 void set_blue_light(::std::unique_ptr<DigitalOutput> t) {
392 blue_light_ = ::std::move(t);
393 }
394
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800395 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700396 virtual void Write(const superstructure::Output &output) override {
397 intake_victor_->SetSpeed(::aos::Clip(output.voltage_intake(),
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800398 -kMaxBringupPower, kMaxBringupPower) /
399 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700400 intake_rollers_victor_->SetSpeed(output.voltage_intake_rollers() / 12.0);
401 indexer_victor_->SetSpeed(-output.voltage_indexer() / 12.0);
402 indexer_roller_victor_->SetSpeed(output.voltage_indexer_rollers() / 12.0);
403 turret_victor_->SetSpeed(::aos::Clip(-output.voltage_turret(),
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800404 -kMaxBringupPower, kMaxBringupPower) /
405 12.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700406 hood_victor_->SetSpeed(::aos::Clip(output.voltage_hood(), -kMaxBringupPower,
407 kMaxBringupPower) /
408 12.0);
409 shooter_victor_->SetSpeed(output.voltage_shooter() / 12.0);
Austin Schuhc587c882017-03-29 21:33:10 -0700410
Alex Perrycb7da4b2019-08-28 19:35:56 -0700411 red_light_->Set(output.red_light_on());
412 green_light_->Set(output.green_light_on());
413 blue_light_->Set(output.blue_light_on());
Austin Schuh6a8131b2017-04-08 15:39:22 -0700414
Alex Perrycb7da4b2019-08-28 19:35:56 -0700415 gear_servo_->SetPosition(output.gear_servo());
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800416 }
417
418 virtual void Stop() override {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700419 AOS_LOG(WARNING, "Superstructure output too old.\n");
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800420 intake_victor_->SetDisabled();
421 intake_rollers_victor_->SetDisabled();
Brian Silverman052e69d2017-02-12 16:19:55 -0800422 indexer_victor_->SetDisabled();
423 indexer_roller_victor_->SetDisabled();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800424 turret_victor_->SetDisabled();
425 hood_victor_->SetDisabled();
426 shooter_victor_->SetDisabled();
Austin Schuhc587c882017-03-29 21:33:10 -0700427
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800428 gear_servo_->SetRaw(0);
Austin Schuh6a8131b2017-04-08 15:39:22 -0700429
Austin Schuhc587c882017-03-29 21:33:10 -0700430 red_light_->Set(true);
431 green_light_->Set(true);
432 blue_light_->Set(true);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800433 }
434
Austin Schuh8347cb62017-04-08 14:37:34 -0700435 ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_,
436 indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_,
437 hood_victor_;
Austin Schuhc587c882017-03-29 21:33:10 -0700438
Austin Schuh6a8131b2017-04-08 15:39:22 -0700439 ::std::unique_ptr<::frc::Servo> gear_servo_;
440
Austin Schuhc587c882017-03-29 21:33:10 -0700441 ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_;
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800442};
443
444class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
445 public:
446 ::std::unique_ptr<Encoder> make_encoder(int index) {
447 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
448 Encoder::k4X);
449 }
450
451 void Run() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700452 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800453 aos::configuration::ReadConfig("aos_config.json");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700454
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700455 // Thread 1.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700456 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700457 ::frc971::wpilib::JoystickSender joystick_sender(
458 &joystick_sender_event_loop);
459 AddLoop(&joystick_sender_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800460
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700461 // Thread 2.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700462 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700463 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
464 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800465
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700466 // Thread 3.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700467 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700468 SensorReader sensor_reader(&sensor_reader_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800469
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700470 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
471 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800472
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700473 sensor_reader.set_intake_encoder(make_encoder(3));
474 sensor_reader.set_intake_absolute(make_unique<DigitalInput>(0));
475 sensor_reader.set_intake_potentiometer(make_unique<AnalogInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800476
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700477 sensor_reader.set_indexer_encoder(make_encoder(5));
478 sensor_reader.set_indexer_hall(make_unique<DigitalInput>(4));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800479
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700480 sensor_reader.set_turret_encoder(make_encoder(6));
481 sensor_reader.set_turret_hall(make_unique<DigitalInput>(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800482
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700483 sensor_reader.set_hood_encoder(make_encoder(4));
484 sensor_reader.set_hood_index(make_unique<DigitalInput>(1));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800485
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700486 sensor_reader.set_shooter_encoder(make_encoder(2));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800487
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700488 sensor_reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
489 sensor_reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800490
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700491 sensor_reader.set_pwm_trigger(true);
Austin Schuh8347cb62017-04-08 14:37:34 -0700492
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700493 AddLoop(&sensor_reader_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800494
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700495 // Thread 5.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700496 ::aos::ShmEventLoop imu_event_loop(&config.message());
Brian Silvermanb4439852017-02-24 19:49:09 -0800497 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700498 ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, SPI::Port::kOnboardCS1,
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800499 imu_trigger.get());
Brian Silvermana70994f2017-03-16 22:32:55 -0700500 imu.SetDummySPI(SPI::Port::kOnboardCS2);
501 auto imu_reset = make_unique<DigitalOutput>(6);
502 imu.set_reset(imu_reset.get());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700503 AddLoop(&imu_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800504
Alex Perrycb7da4b2019-08-28 19:35:56 -0700505 ::aos::ShmEventLoop output_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700506 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davis7af11ad2019-02-03 01:16:45 -0800507 drivetrain_writer.set_left_controller0(
508 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)), true);
509 drivetrain_writer.set_right_controller0(
510 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), false);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800511
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700512 SuperstructureWriter superstructure_writer(&output_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800513 superstructure_writer.set_intake_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700514 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800515 superstructure_writer.set_intake_rollers_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700516 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4)));
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800517 superstructure_writer.set_indexer_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700518 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6)));
Brian Silverman052e69d2017-02-12 16:19:55 -0800519 superstructure_writer.set_indexer_roller_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700520 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800521 superstructure_writer.set_turret_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700522 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800523 superstructure_writer.set_hood_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700524 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)));
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800525 superstructure_writer.set_shooter_victor(
Austin Schuh8347cb62017-04-08 14:37:34 -0700526 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8)));
Austin Schuhc587c882017-03-29 21:33:10 -0700527
Austin Schuh6a8131b2017-04-08 15:39:22 -0700528 superstructure_writer.set_gear_servo(
529 ::std::unique_ptr<Servo>(new Servo(0)));
530
Austin Schuhc587c882017-03-29 21:33:10 -0700531 superstructure_writer.set_red_light(
532 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5)));
533 superstructure_writer.set_green_light(
534 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24)));
535 superstructure_writer.set_blue_light(
536 ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25)));
537
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700538 AddLoop(&output_event_loop);
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800539
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700540 // Thread 6.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700541 ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700542 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800543 solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0));
Austin Schuhc587c882017-03-29 21:33:10 -0700544 solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700545 AddLoop(&solenoid_writer_event_loop);
Adam Snaidere0554ef2017-03-11 23:02:45 -0800546
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700547 RunLoops();
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800548 }
549};
550
Brian Silverman052e69d2017-02-12 16:19:55 -0800551} // namespace
Campbell Crowleyae6e8422017-02-05 12:38:50 -0800552} // namespace wpilib
553} // namespace y2017
554
555AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot);