Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 2 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 3 | #include <array> |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cinttypes> |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 6 | #include <cmath> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cstdio> |
| 8 | #include <cstring> |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 9 | #include <functional> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 10 | #include <memory> |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 11 | #include <mutex> |
| 12 | #include <thread> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 13 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 14 | #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 Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 23 | #undef ERROR |
| 24 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 25 | #include "aos/commonmath.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | #include "aos/events/shm_event_loop.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 27 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 28 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 29 | #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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | #include "frc971/autonomous/auto_generated.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 36 | #include "frc971/autonomous/auto_mode_generated.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 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" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 40 | #include "frc971/input/robot_state_generated.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 41 | #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 Davis | 7af11ad | 2019-02-03 01:16:45 -0800 | [diff] [blame] | 46 | #include "frc971/wpilib/drivetrain_writer.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 47 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 48 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 49 | #include "frc971/wpilib/joystick_sender.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | #include "frc971/wpilib/logging_generated.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 51 | #include "frc971/wpilib/loop_output_handler.h" |
| 52 | #include "frc971/wpilib/pdp_fetcher.h" |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 53 | #include "frc971/wpilib/sensor_reader.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 54 | #include "frc971/wpilib/wpilib_robot_base.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 55 | #include "y2017/constants.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 56 | #include "y2017/control_loops/superstructure/superstructure_output_generated.h" |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 57 | #include "y2017/control_loops/superstructure/superstructure_position_static.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 58 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 59 | #ifndef M_PI |
| 60 | #define M_PI 3.14159265358979323846 |
| 61 | #endif |
| 62 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | namespace superstructure = ::y2017::control_loops::superstructure; |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 64 | using ::aos::monotonic_clock; |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 65 | using ::y2017::constants::Values; |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 66 | namespace chrono = ::std::chrono; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 67 | using namespace frc; |
Vinay Siva | e52a6b3 | 2021-07-10 15:19:26 -0700 | [diff] [blame] | 68 | using std::make_unique; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 69 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 70 | namespace y2017::wpilib { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 71 | namespace { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 72 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 73 | constexpr double kMaxBringupPower = 12.0; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 74 | |
| 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 Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 79 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 80 | // constexpr. |
| 81 | template <typename T> |
| 82 | constexpr T max(T a, T b) { |
| 83 | return (a > b) ? a : b; |
| 84 | } |
| 85 | template <typename T, typename... Rest> |
| 86 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 87 | return max(max(a, b), c, rest...); |
| 88 | } |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 89 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 90 | double drivetrain_translate(int32_t in) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 91 | return static_cast<double>(in) / |
| 92 | Values::kDrivetrainEncoderCountsPerRevolution * |
| 93 | Values::kDrivetrainEncoderRatio * 2.0 * M_PI; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | double drivetrain_velocity_translate(double in) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 97 | return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution * |
| 98 | Values::kDrivetrainEncoderRatio * 2.0 * M_PI; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 101 | // TODO(Travis): Make sure the number of turns is right. |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 102 | double intake_pot_translate(double voltage) { |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 103 | return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) * |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 104 | (2 * M_PI /*radians*/); |
| 105 | } |
| 106 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 107 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 108 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond, |
| 109 | Values::kMaxShooterEncoderPulsesPerSecond); |
| 110 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 111 | "fast encoders are too fast"); |
| 112 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 113 | max(Values::kMaxIntakeEncoderPulsesPerSecond, |
| 114 | Values::kMaxTurretEncoderPulsesPerSecond, |
| 115 | Values::kMaxIndexerEncoderPulsesPerSecond); |
| 116 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 117 | "medium encoders are too fast"); |
| 118 | constexpr double kMaxSlowEncoderPulsesPerSecond = |
| 119 | Values::kMaxHoodEncoderPulsesPerSecond; |
| 120 | static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000, |
| 121 | "slow encoders are too fast"); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 122 | static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond, |
| 123 | "slow encoders are faster than medium?"); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 124 | |
| 125 | // Class to send position messages with sensor readings to our loops. |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 126 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 127 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 128 | SensorReader(::aos::ShmEventLoop *event_loop) |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 129 | : ::frc971::wpilib::SensorReader(event_loop), |
| 130 | auto_mode_sender_( |
| 131 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 132 | "/aos")), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 133 | superstructure_position_sender_( |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 134 | event_loop->MakeSender<superstructure::PositionStatic>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 135 | "/superstructure")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 136 | drivetrain_position_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 137 | event_loop |
| 138 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 139 | "/drivetrain")) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 140 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 141 | // we should ever see. |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 142 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 143 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 144 | hall_filter_.SetPeriodNanoSeconds(100000); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 147 | void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 148 | fast_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 149 | shooter_encoder_ = ::std::move(encoder); |
| 150 | } |
| 151 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 152 | void set_intake_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 153 | medium_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 154 | 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 Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 161 | void set_intake_absolute(::std::unique_ptr<DigitalInput> input) { |
| 162 | intake_encoder_.set_absolute_pwm(::std::move(input)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 165 | void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) { |
| 166 | medium_encoder_filter_.Add(encoder.get()); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 167 | indexer_counter_.set_encoder(encoder.get()); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 168 | indexer_encoder_ = ::std::move(encoder); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 171 | 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 Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 177 | void set_turret_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 178 | medium_encoder_filter_.Add(encoder.get()); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 179 | turret_counter_.set_encoder(encoder.get()); |
| 180 | turret_encoder_ = ::std::move(encoder); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 183 | 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 Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 189 | void set_hood_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 190 | medium_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 191 | hood_encoder_.set_encoder(::std::move(encoder)); |
| 192 | } |
| 193 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 194 | void set_hood_index(::std::unique_ptr<DigitalInput> index) { |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 195 | medium_encoder_filter_.Add(index.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 196 | hood_encoder_.set_index(::std::move(index)); |
| 197 | } |
| 198 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 199 | void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) { |
| 200 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 201 | } |
| 202 | |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 203 | void Start() { |
| 204 | AddToDMA(&indexer_counter_); |
| 205 | AddToDMA(&hood_encoder_); |
| 206 | AddToDMA(&turret_counter_); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void RunIteration() { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 210 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 211 | 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 Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 218 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 219 | 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 Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 223 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 224 | builder.CheckOk(builder.Send(position_builder.Finish())); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 225 | } |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 226 | } |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 227 | |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 228 | void RunDMAIteration() { |
| 229 | const auto values = constants::GetValues(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 230 | |
| 231 | { |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 232 | aos::Sender<superstructure::PositionStatic>::StaticBuilder builder = |
| 233 | superstructure_position_sender_.MakeStaticBuilder(); |
| 234 | |
| 235 | CopyPosition(intake_encoder_, builder->add_intake(), |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 236 | Values::kIntakeEncoderCountsPerRevolution, |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 237 | Values::kIntakeEncoderRatio, intake_pot_translate, true, |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 238 | values.intake.pot_offset); |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 239 | CopyPosition(hood_encoder_, builder->add_hood(), |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 240 | Values::kHoodEncoderCountsPerRevolution, |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 241 | Values::kHoodEncoderRatio, true); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 242 | |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 243 | superstructure::ColumnPositionStatic *column = builder->add_column(); |
| 244 | CopyPosition(turret_counter_, column->add_turret(), |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 245 | Values::kTurretEncoderCountsPerRevolution, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 246 | Values::kTurretEncoderRatio, false); |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 247 | CopyPosition(indexer_counter_, column->add_indexer(), |
| 248 | Values::kIndexerEncoderCountsPerRevolution, |
| 249 | Values::kIndexerEncoderRatio, true); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 250 | |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 251 | builder->set_theta_shooter( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 252 | encoder_translate(shooter_encoder_->GetRaw(), |
| 253 | Values::kShooterEncoderCountsPerRevolution, |
| 254 | Values::kShooterEncoderRatio)); |
| 255 | |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 256 | builder.CheckOk(builder.Send()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 260 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 261 | ::frc971::autonomous::AutonomousMode::Builder auto_builder = |
| 262 | builder.MakeBuilder<::frc971::autonomous::AutonomousMode>(); |
| 263 | |
| 264 | int mode = 0; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 265 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 266 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 267 | mode |= 1 << i; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 270 | auto_builder.add_mode(mode); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 271 | builder.CheckOk(builder.Send(auto_builder.Finish())); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 275 | private: |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 276 | ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_; |
Maxwell Henderson | cb78f35 | 2024-01-15 00:27:16 -0800 | [diff] [blame] | 277 | ::aos::Sender<superstructure::PositionStatic> superstructure_position_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 278 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 279 | drivetrain_position_sender_; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 280 | |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 281 | DigitalGlitchFilter hall_filter_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 282 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 283 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 284 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 285 | ::std::unique_ptr<Encoder> indexer_encoder_; |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 286 | ::std::unique_ptr<DigitalInput> indexer_hall_; |
| 287 | ::frc971::wpilib::DMAEdgeCounter indexer_counter_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 288 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 289 | ::std::unique_ptr<Encoder> turret_encoder_; |
| 290 | ::std::unique_ptr<DigitalInput> turret_hall_; |
| 291 | ::frc971::wpilib::DMAEdgeCounter turret_counter_; |
| 292 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 293 | ::frc971::wpilib::DMAEncoder hood_encoder_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 294 | ::std::unique_ptr<Encoder> shooter_encoder_; |
| 295 | |
| 296 | ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 297 | }; |
| 298 | |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 299 | class SolenoidWriter { |
| 300 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 301 | SolenoidWriter(::aos::ShmEventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 302 | : superstructure_output_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 303 | event_loop->MakeFetcher<superstructure::Output>( |
| 304 | "/superstructure")) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 305 | event_loop->set_name("Solenoids"); |
| 306 | event_loop->SetRuntimeRealtimePriority(27); |
| 307 | |
| 308 | event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 309 | ::std::chrono::milliseconds(20), |
| 310 | ::std::chrono::milliseconds(1)); |
| 311 | } |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 312 | |
| 313 | ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; } |
| 314 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 315 | void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 316 | lights_ = ::std::move(s); |
| 317 | } |
| 318 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 319 | void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 320 | rgb_lights_ = ::std::move(s); |
| 321 | } |
| 322 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 323 | void Loop(const int iterations) { |
| 324 | if (iterations != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 325 | AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 326 | } |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 327 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 328 | { |
| 329 | superstructure_output_fetcher_.Fetch(); |
| 330 | if (superstructure_output_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 331 | lights_->Set(superstructure_output_fetcher_->lights_on()); |
| 332 | rgb_lights_->Set(superstructure_output_fetcher_->red_light_on() | |
| 333 | superstructure_output_fetcher_->green_light_on() | |
| 334 | superstructure_output_fetcher_->blue_light_on()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | pcm_.Flush(); |
| 339 | } |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 340 | |
| 341 | private: |
| 342 | ::frc971::wpilib::BufferedPcm pcm_; |
| 343 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 344 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 345 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 346 | ::aos::Fetcher<::y2017::control_loops::superstructure::Output> |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 347 | superstructure_output_fetcher_; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 348 | }; |
| 349 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 350 | class SuperstructureWriter |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 351 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 352 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 353 | SuperstructureWriter(::aos::EventLoop *event_loop) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 354 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 355 | event_loop, "/superstructure") {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 356 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 357 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 358 | intake_victor_ = ::std::move(t); |
| 359 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 360 | void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 361 | intake_rollers_victor_ = ::std::move(t); |
| 362 | } |
| 363 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 364 | void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 365 | indexer_victor_ = ::std::move(t); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 366 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 367 | void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 368 | indexer_roller_victor_ = ::std::move(t); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 371 | void set_gear_servo(::std::unique_ptr<::frc::Servo> t) { |
| 372 | gear_servo_ = ::std::move(t); |
| 373 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 374 | void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 375 | shooter_victor_ = ::std::move(t); |
| 376 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 377 | void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 378 | turret_victor_ = ::std::move(t); |
| 379 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 380 | void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 381 | hood_victor_ = ::std::move(t); |
| 382 | } |
| 383 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 384 | void set_red_light(::std::unique_ptr<DigitalOutput> t) { |
| 385 | red_light_ = ::std::move(t); |
| 386 | } |
| 387 | void set_green_light(::std::unique_ptr<DigitalOutput> t) { |
| 388 | green_light_ = ::std::move(t); |
| 389 | } |
| 390 | void set_blue_light(::std::unique_ptr<DigitalOutput> t) { |
| 391 | blue_light_ = ::std::move(t); |
| 392 | } |
| 393 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 394 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 395 | virtual void Write(const superstructure::Output &output) override { |
| 396 | intake_victor_->SetSpeed(::aos::Clip(output.voltage_intake(), |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 397 | -kMaxBringupPower, kMaxBringupPower) / |
| 398 | 12.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 399 | intake_rollers_victor_->SetSpeed(output.voltage_intake_rollers() / 12.0); |
| 400 | indexer_victor_->SetSpeed(-output.voltage_indexer() / 12.0); |
| 401 | indexer_roller_victor_->SetSpeed(output.voltage_indexer_rollers() / 12.0); |
| 402 | turret_victor_->SetSpeed(::aos::Clip(-output.voltage_turret(), |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 403 | -kMaxBringupPower, kMaxBringupPower) / |
| 404 | 12.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 405 | hood_victor_->SetSpeed(::aos::Clip(output.voltage_hood(), -kMaxBringupPower, |
| 406 | kMaxBringupPower) / |
| 407 | 12.0); |
| 408 | shooter_victor_->SetSpeed(output.voltage_shooter() / 12.0); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 409 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 410 | red_light_->Set(output.red_light_on()); |
| 411 | green_light_->Set(output.green_light_on()); |
| 412 | blue_light_->Set(output.blue_light_on()); |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 413 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 414 | gear_servo_->SetPosition(output.gear_servo()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | virtual void Stop() override { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 418 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 419 | intake_victor_->SetDisabled(); |
| 420 | intake_rollers_victor_->SetDisabled(); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 421 | indexer_victor_->SetDisabled(); |
| 422 | indexer_roller_victor_->SetDisabled(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 423 | turret_victor_->SetDisabled(); |
| 424 | hood_victor_->SetDisabled(); |
| 425 | shooter_victor_->SetDisabled(); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 426 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 427 | gear_servo_->SetRaw(0); |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 428 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 429 | red_light_->Set(true); |
| 430 | green_light_->Set(true); |
| 431 | blue_light_->Set(true); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 434 | ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_, |
| 435 | indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_, |
| 436 | hood_victor_; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 437 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 438 | ::std::unique_ptr<::frc::Servo> gear_servo_; |
| 439 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 440 | ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 444 | public: |
| 445 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 446 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 447 | Encoder::k4X); |
| 448 | } |
| 449 | |
| 450 | void Run() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 451 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 452 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 454 | // Thread 1. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 456 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 457 | &joystick_sender_event_loop); |
| 458 | AddLoop(&joystick_sender_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 459 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 460 | // Thread 2. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 461 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 462 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 463 | AddLoop(&pdp_fetcher_event_loop); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 464 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 465 | // Thread 3. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 466 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 467 | SensorReader sensor_reader(&sensor_reader_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 468 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 469 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 470 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 471 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 472 | sensor_reader.set_intake_encoder(make_encoder(3)); |
| 473 | sensor_reader.set_intake_absolute(make_unique<DigitalInput>(0)); |
| 474 | sensor_reader.set_intake_potentiometer(make_unique<AnalogInput>(4)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 475 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 476 | sensor_reader.set_indexer_encoder(make_encoder(5)); |
| 477 | sensor_reader.set_indexer_hall(make_unique<DigitalInput>(4)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 478 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 479 | sensor_reader.set_turret_encoder(make_encoder(6)); |
| 480 | sensor_reader.set_turret_hall(make_unique<DigitalInput>(2)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 481 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 482 | sensor_reader.set_hood_encoder(make_encoder(4)); |
| 483 | sensor_reader.set_hood_index(make_unique<DigitalInput>(1)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 484 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 485 | sensor_reader.set_shooter_encoder(make_encoder(2)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 486 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 487 | sensor_reader.set_autonomous_mode(0, make_unique<DigitalInput>(9)); |
| 488 | sensor_reader.set_autonomous_mode(1, make_unique<DigitalInput>(8)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 489 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 490 | sensor_reader.set_pwm_trigger(true); |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 491 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 492 | AddLoop(&sensor_reader_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 493 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 494 | // Thread 5. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 495 | ::aos::ShmEventLoop imu_event_loop(&config.message()); |
Brian Silverman | b443985 | 2017-02-24 19:49:09 -0800 | [diff] [blame] | 496 | auto imu_trigger = make_unique<DigitalInput>(3); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 497 | ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, SPI::Port::kOnboardCS1, |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 498 | imu_trigger.get()); |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 499 | imu.SetDummySPI(SPI::Port::kOnboardCS2); |
| 500 | auto imu_reset = make_unique<DigitalOutput>(6); |
| 501 | imu.set_reset(imu_reset.get()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 502 | AddLoop(&imu_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 503 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 504 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 505 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
Sabina Davis | 7af11ad | 2019-02-03 01:16:45 -0800 | [diff] [blame] | 506 | drivetrain_writer.set_left_controller0( |
| 507 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)), true); |
| 508 | drivetrain_writer.set_right_controller0( |
| 509 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), false); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 510 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 511 | SuperstructureWriter superstructure_writer(&output_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 512 | superstructure_writer.set_intake_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 513 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 514 | superstructure_writer.set_intake_rollers_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 515 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 516 | superstructure_writer.set_indexer_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 517 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 518 | superstructure_writer.set_indexer_roller_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 519 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 520 | superstructure_writer.set_turret_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 521 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 522 | superstructure_writer.set_hood_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 523 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 524 | superstructure_writer.set_shooter_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 525 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8))); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 526 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 527 | superstructure_writer.set_gear_servo( |
| 528 | ::std::unique_ptr<Servo>(new Servo(0))); |
| 529 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 530 | superstructure_writer.set_red_light( |
| 531 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5))); |
| 532 | superstructure_writer.set_green_light( |
| 533 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24))); |
| 534 | superstructure_writer.set_blue_light( |
| 535 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25))); |
| 536 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 537 | AddLoop(&output_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 538 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 539 | // Thread 6. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 540 | ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 541 | SolenoidWriter solenoid_writer(&solenoid_writer_event_loop); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 542 | solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0)); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 543 | solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 544 | AddLoop(&solenoid_writer_event_loop); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 545 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 546 | RunLoops(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 547 | } |
| 548 | }; |
| 549 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 550 | } // namespace |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 551 | } // namespace y2017::wpilib |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 552 | |
| 553 | AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot); |