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