Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 1 | #include <inttypes.h> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 5 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 6 | #include <array> |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 7 | #include <chrono> |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 8 | #include <cmath> |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 9 | #include <functional> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 12 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 14 | #include "frc971/wpilib/ahal/Compressor.h" |
| 15 | #include "frc971/wpilib/ahal/Counter.h" |
| 16 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 17 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 18 | #include "frc971/wpilib/ahal/Encoder.h" |
| 19 | #include "frc971/wpilib/ahal/Relay.h" |
| 20 | #include "frc971/wpilib/ahal/Servo.h" |
| 21 | #include "frc971/wpilib/ahal/VictorSP.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 22 | #undef ERROR |
| 23 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 24 | #include "aos/commonmath.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | #include "aos/events/shm_event_loop.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 26 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 27 | #include "aos/logging/logging.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 28 | #include "aos/make_unique.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 29 | #include "aos/robot_state/robot_state_generated.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 30 | #include "aos/stl_mutex/stl_mutex.h" |
| 31 | #include "aos/time/time.h" |
| 32 | #include "aos/util/compiler_memory_barrier.h" |
| 33 | #include "aos/util/log_interval.h" |
| 34 | #include "aos/util/phased_loop.h" |
| 35 | #include "aos/util/wrapping_counter.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 36 | #include "frc971/autonomous/auto_generated.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 37 | #include "frc971/autonomous/auto_mode_generated.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | #include "frc971/control_loops/control_loops_generated.h" |
| 39 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 40 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
Austin 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" |
| 57 | #include "y2017/control_loops/superstructure/superstructure_position_generated.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; |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 64 | using ::y2017::constants::Values; |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 65 | using ::aos::monotonic_clock; |
| 66 | namespace chrono = ::std::chrono; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 67 | using namespace frc; |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 68 | using aos::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_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 135 | event_loop->MakeSender<superstructure::Position>( |
| 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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 225 | 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 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 233 | auto builder = superstructure_position_sender_.MakeBuilder(); |
| 234 | frc971::PotAndAbsolutePositionT intake; |
| 235 | CopyPosition(intake_encoder_, &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); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 239 | flatbuffers::Offset<frc971::PotAndAbsolutePosition> intake_offset = |
| 240 | frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &intake); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 241 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 242 | frc971::HallEffectAndPositionT indexer; |
| 243 | CopyPosition(indexer_counter_, &indexer, |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 244 | Values::kIndexerEncoderCountsPerRevolution, |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 245 | Values::kIndexerEncoderRatio, true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 246 | flatbuffers::Offset<frc971::HallEffectAndPosition> indexer_offset = |
| 247 | frc971::HallEffectAndPosition::Pack(*builder.fbb(), &indexer); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 248 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 249 | frc971::IndexPositionT hood; |
| 250 | CopyPosition(hood_encoder_, &hood, |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 251 | Values::kHoodEncoderCountsPerRevolution, |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 252 | Values::kHoodEncoderRatio, true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 253 | flatbuffers::Offset<frc971::IndexPosition> hood_offset = |
| 254 | frc971::IndexPosition::Pack(*builder.fbb(), &hood); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 255 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 256 | frc971::HallEffectAndPositionT turret; |
| 257 | CopyPosition(turret_counter_, &turret, |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 258 | Values::kTurretEncoderCountsPerRevolution, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 259 | Values::kTurretEncoderRatio, false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 260 | flatbuffers::Offset<frc971::HallEffectAndPosition> turret_offset = |
| 261 | frc971::HallEffectAndPosition::Pack(*builder.fbb(), &turret); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 262 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 263 | superstructure::ColumnPosition::Builder column_builder = |
| 264 | builder.MakeBuilder<superstructure::ColumnPosition>(); |
| 265 | column_builder.add_indexer(indexer_offset); |
| 266 | column_builder.add_turret(turret_offset); |
| 267 | flatbuffers::Offset<superstructure::ColumnPosition> column_offset = |
| 268 | column_builder.Finish(); |
| 269 | |
| 270 | superstructure::Position::Builder position_builder = |
| 271 | builder.MakeBuilder<superstructure::Position>(); |
| 272 | |
| 273 | position_builder.add_column(column_offset); |
| 274 | position_builder.add_hood(hood_offset); |
| 275 | position_builder.add_intake(intake_offset); |
| 276 | position_builder.add_theta_shooter( |
| 277 | encoder_translate(shooter_encoder_->GetRaw(), |
| 278 | Values::kShooterEncoderCountsPerRevolution, |
| 279 | Values::kShooterEncoderRatio)); |
| 280 | |
| 281 | builder.Send(position_builder.Finish()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 285 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 286 | ::frc971::autonomous::AutonomousMode::Builder auto_builder = |
| 287 | builder.MakeBuilder<::frc971::autonomous::AutonomousMode>(); |
| 288 | |
| 289 | int mode = 0; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 290 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 291 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 292 | mode |= 1 << i; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 295 | auto_builder.add_mode(mode); |
| 296 | builder.Send(auto_builder.Finish()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 300 | private: |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 301 | ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 302 | ::aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 303 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 304 | drivetrain_position_sender_; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 305 | |
Austin Schuh | bf29b6f | 2019-02-02 21:45:27 -0800 | [diff] [blame] | 306 | DigitalGlitchFilter hall_filter_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 307 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 308 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 309 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 310 | ::std::unique_ptr<Encoder> indexer_encoder_; |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 311 | ::std::unique_ptr<DigitalInput> indexer_hall_; |
| 312 | ::frc971::wpilib::DMAEdgeCounter indexer_counter_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 313 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 314 | ::std::unique_ptr<Encoder> turret_encoder_; |
| 315 | ::std::unique_ptr<DigitalInput> turret_hall_; |
| 316 | ::frc971::wpilib::DMAEdgeCounter turret_counter_; |
| 317 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 318 | ::frc971::wpilib::DMAEncoder hood_encoder_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 319 | ::std::unique_ptr<Encoder> shooter_encoder_; |
| 320 | |
| 321 | ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 322 | }; |
| 323 | |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 324 | class SolenoidWriter { |
| 325 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 326 | SolenoidWriter(::aos::ShmEventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 327 | : superstructure_output_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 328 | event_loop->MakeFetcher<superstructure::Output>( |
| 329 | "/superstructure")) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 330 | event_loop->set_name("Solenoids"); |
| 331 | event_loop->SetRuntimeRealtimePriority(27); |
| 332 | |
| 333 | event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 334 | ::std::chrono::milliseconds(20), |
| 335 | ::std::chrono::milliseconds(1)); |
| 336 | } |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 337 | |
| 338 | ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; } |
| 339 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 340 | void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 341 | lights_ = ::std::move(s); |
| 342 | } |
| 343 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 344 | void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 345 | rgb_lights_ = ::std::move(s); |
| 346 | } |
| 347 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 348 | void Loop(const int iterations) { |
| 349 | if (iterations != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 350 | AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 351 | } |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 352 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 353 | { |
| 354 | superstructure_output_fetcher_.Fetch(); |
| 355 | if (superstructure_output_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 356 | lights_->Set(superstructure_output_fetcher_->lights_on()); |
| 357 | rgb_lights_->Set(superstructure_output_fetcher_->red_light_on() | |
| 358 | superstructure_output_fetcher_->green_light_on() | |
| 359 | superstructure_output_fetcher_->blue_light_on()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | pcm_.Flush(); |
| 364 | } |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 365 | |
| 366 | private: |
| 367 | ::frc971::wpilib::BufferedPcm pcm_; |
| 368 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 369 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 370 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 371 | ::aos::Fetcher<::y2017::control_loops::superstructure::Output> |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 372 | superstructure_output_fetcher_; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 373 | }; |
| 374 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 375 | class SuperstructureWriter |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 376 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 377 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 378 | SuperstructureWriter(::aos::EventLoop *event_loop) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 379 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 380 | event_loop, "/superstructure") {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 381 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 382 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 383 | intake_victor_ = ::std::move(t); |
| 384 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 385 | void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 386 | intake_rollers_victor_ = ::std::move(t); |
| 387 | } |
| 388 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 389 | void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 390 | indexer_victor_ = ::std::move(t); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 391 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 392 | void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 393 | indexer_roller_victor_ = ::std::move(t); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 396 | void set_gear_servo(::std::unique_ptr<::frc::Servo> t) { |
| 397 | gear_servo_ = ::std::move(t); |
| 398 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 399 | void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 400 | shooter_victor_ = ::std::move(t); |
| 401 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 402 | void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 403 | turret_victor_ = ::std::move(t); |
| 404 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 405 | void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 406 | hood_victor_ = ::std::move(t); |
| 407 | } |
| 408 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 409 | void set_red_light(::std::unique_ptr<DigitalOutput> t) { |
| 410 | red_light_ = ::std::move(t); |
| 411 | } |
| 412 | void set_green_light(::std::unique_ptr<DigitalOutput> t) { |
| 413 | green_light_ = ::std::move(t); |
| 414 | } |
| 415 | void set_blue_light(::std::unique_ptr<DigitalOutput> t) { |
| 416 | blue_light_ = ::std::move(t); |
| 417 | } |
| 418 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 419 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | virtual void Write(const superstructure::Output &output) override { |
| 421 | intake_victor_->SetSpeed(::aos::Clip(output.voltage_intake(), |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 422 | -kMaxBringupPower, kMaxBringupPower) / |
| 423 | 12.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 424 | intake_rollers_victor_->SetSpeed(output.voltage_intake_rollers() / 12.0); |
| 425 | indexer_victor_->SetSpeed(-output.voltage_indexer() / 12.0); |
| 426 | indexer_roller_victor_->SetSpeed(output.voltage_indexer_rollers() / 12.0); |
| 427 | turret_victor_->SetSpeed(::aos::Clip(-output.voltage_turret(), |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 428 | -kMaxBringupPower, kMaxBringupPower) / |
| 429 | 12.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 430 | hood_victor_->SetSpeed(::aos::Clip(output.voltage_hood(), -kMaxBringupPower, |
| 431 | kMaxBringupPower) / |
| 432 | 12.0); |
| 433 | shooter_victor_->SetSpeed(output.voltage_shooter() / 12.0); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 434 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 435 | red_light_->Set(output.red_light_on()); |
| 436 | green_light_->Set(output.green_light_on()); |
| 437 | blue_light_->Set(output.blue_light_on()); |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 438 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 439 | gear_servo_->SetPosition(output.gear_servo()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | virtual void Stop() override { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 443 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 444 | intake_victor_->SetDisabled(); |
| 445 | intake_rollers_victor_->SetDisabled(); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 446 | indexer_victor_->SetDisabled(); |
| 447 | indexer_roller_victor_->SetDisabled(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 448 | turret_victor_->SetDisabled(); |
| 449 | hood_victor_->SetDisabled(); |
| 450 | shooter_victor_->SetDisabled(); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 451 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 452 | gear_servo_->SetRaw(0); |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 453 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 454 | red_light_->Set(true); |
| 455 | green_light_->Set(true); |
| 456 | blue_light_->Set(true); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 459 | ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_, |
| 460 | indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_, |
| 461 | hood_victor_; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 462 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 463 | ::std::unique_ptr<::frc::Servo> gear_servo_; |
| 464 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 465 | ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 469 | public: |
| 470 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 471 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 472 | Encoder::k4X); |
| 473 | } |
| 474 | |
| 475 | void Run() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 476 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 477 | aos::configuration::ReadConfig("config.json"); |
| 478 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 479 | // Thread 1. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 480 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 481 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 482 | &joystick_sender_event_loop); |
| 483 | AddLoop(&joystick_sender_event_loop); |
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 | // Thread 2. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 486 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 487 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 488 | AddLoop(&pdp_fetcher_event_loop); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 489 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 490 | // Thread 3. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 491 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 492 | SensorReader sensor_reader(&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 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 495 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 496 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 497 | sensor_reader.set_intake_encoder(make_encoder(3)); |
| 498 | sensor_reader.set_intake_absolute(make_unique<DigitalInput>(0)); |
| 499 | sensor_reader.set_intake_potentiometer(make_unique<AnalogInput>(4)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 500 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 501 | sensor_reader.set_indexer_encoder(make_encoder(5)); |
| 502 | sensor_reader.set_indexer_hall(make_unique<DigitalInput>(4)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 503 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 504 | sensor_reader.set_turret_encoder(make_encoder(6)); |
| 505 | sensor_reader.set_turret_hall(make_unique<DigitalInput>(2)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 506 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 507 | sensor_reader.set_hood_encoder(make_encoder(4)); |
| 508 | sensor_reader.set_hood_index(make_unique<DigitalInput>(1)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 509 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 510 | sensor_reader.set_shooter_encoder(make_encoder(2)); |
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 | sensor_reader.set_autonomous_mode(0, make_unique<DigitalInput>(9)); |
| 513 | sensor_reader.set_autonomous_mode(1, make_unique<DigitalInput>(8)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 514 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 515 | sensor_reader.set_pwm_trigger(true); |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 516 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 517 | AddLoop(&sensor_reader_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 518 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 519 | // Thread 5. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 520 | ::aos::ShmEventLoop imu_event_loop(&config.message()); |
Brian Silverman | b443985 | 2017-02-24 19:49:09 -0800 | [diff] [blame] | 521 | auto imu_trigger = make_unique<DigitalInput>(3); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 522 | ::frc971::wpilib::ADIS16448 imu(&imu_event_loop, SPI::Port::kOnboardCS1, |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 523 | imu_trigger.get()); |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 524 | imu.SetDummySPI(SPI::Port::kOnboardCS2); |
| 525 | auto imu_reset = make_unique<DigitalOutput>(6); |
| 526 | imu.set_reset(imu_reset.get()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 527 | AddLoop(&imu_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 528 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 529 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 530 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
Sabina Davis | 7af11ad | 2019-02-03 01:16:45 -0800 | [diff] [blame] | 531 | drivetrain_writer.set_left_controller0( |
| 532 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7)), true); |
| 533 | drivetrain_writer.set_right_controller0( |
| 534 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), false); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 535 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 536 | SuperstructureWriter superstructure_writer(&output_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 537 | superstructure_writer.set_intake_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 538 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 539 | superstructure_writer.set_intake_rollers_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 540 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 541 | superstructure_writer.set_indexer_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 542 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 543 | superstructure_writer.set_indexer_roller_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 544 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 545 | superstructure_writer.set_turret_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 546 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 547 | superstructure_writer.set_hood_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 548 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 549 | superstructure_writer.set_shooter_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 550 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8))); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 551 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 552 | superstructure_writer.set_gear_servo( |
| 553 | ::std::unique_ptr<Servo>(new Servo(0))); |
| 554 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 555 | superstructure_writer.set_red_light( |
| 556 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5))); |
| 557 | superstructure_writer.set_green_light( |
| 558 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24))); |
| 559 | superstructure_writer.set_blue_light( |
| 560 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25))); |
| 561 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 562 | AddLoop(&output_event_loop); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 563 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 564 | // Thread 6. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 565 | ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 566 | SolenoidWriter solenoid_writer(&solenoid_writer_event_loop); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 567 | solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0)); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 568 | solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 569 | AddLoop(&solenoid_writer_event_loop); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 570 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 571 | RunLoops(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 572 | } |
| 573 | }; |
| 574 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 575 | } // namespace |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 576 | } // namespace wpilib |
| 577 | } // namespace y2017 |
| 578 | |
| 579 | AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot); |