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