Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cinttypes> |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -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 | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 9 | #include <functional> |
Vinay Siva | e52a6b3 | 2021-07-10 15:19:26 -0700 | [diff] [blame] | 10 | #include <memory> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 11 | #include <thread> |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 12 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 13 | #include "ctre/phoenix/CANifier.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/AnalogInput.h" |
Austin Schuh | 9950f68 | 2021-11-06 15:27:58 -0700 | [diff] [blame] | 15 | #include "frc971/wpilib/ahal/Compressor.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 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" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -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/time/time.h" |
| 30 | #include "aos/util/compiler_memory_barrier.h" |
| 31 | #include "aos/util/log_interval.h" |
| 32 | #include "aos/util/phased_loop.h" |
| 33 | #include "aos/util/wrapping_counter.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 35 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/ADIS16448.h" |
| 37 | #include "frc971/wpilib/buffered_pcm.h" |
| 38 | #include "frc971/wpilib/buffered_solenoid.h" |
| 39 | #include "frc971/wpilib/dma.h" |
| 40 | #include "frc971/wpilib/dma_edge_counting.h" |
Sabina Davis | caa2a6b | 2019-02-03 01:15:37 -0800 | [diff] [blame] | 41 | #include "frc971/wpilib/drivetrain_writer.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 42 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 43 | #include "frc971/wpilib/joystick_sender.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | #include "frc971/wpilib/logging_generated.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 45 | #include "frc971/wpilib/loop_output_handler.h" |
| 46 | #include "frc971/wpilib/pdp_fetcher.h" |
Austin Schuh | 6abf5b7 | 2019-02-02 20:20:54 -0800 | [diff] [blame] | 47 | #include "frc971/wpilib/sensor_reader.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 48 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 49 | #include "y2018/constants.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | #include "y2018/control_loops/superstructure/superstructure_output_generated.h" |
| 51 | #include "y2018/control_loops/superstructure/superstructure_position_generated.h" |
| 52 | #include "y2018/status_light_generated.h" |
| 53 | #include "y2018/vision/vision_generated.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 54 | |
| 55 | #ifndef M_PI |
| 56 | #define M_PI 3.14159265358979323846 |
| 57 | #endif |
| 58 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 59 | using ::aos::monotonic_clock; |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 60 | using std::make_unique; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | using ::y2018::constants::Values; |
| 62 | namespace chrono = ::std::chrono; |
| 63 | namespace superstructure = ::y2018::control_loops::superstructure; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 64 | |
| 65 | namespace y2018 { |
| 66 | namespace wpilib { |
| 67 | namespace { |
| 68 | |
| 69 | constexpr double kMaxBringupPower = 12.0; |
| 70 | |
| 71 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 72 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 73 | // The low bit is direction. |
| 74 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 75 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 76 | // constexpr. |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 77 | template <typename T> |
| 78 | constexpr T max(T a, T b) { |
| 79 | return (a > b) ? a : b; |
| 80 | } |
| 81 | |
| 82 | template <typename T, typename... Rest> |
| 83 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 84 | return max(max(a, b), c, rest...); |
| 85 | } |
| 86 | |
| 87 | double drivetrain_translate(int32_t in) { |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 88 | return ((static_cast<double>(in) / |
| 89 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
| 90 | (2.0 * M_PI)) * |
| 91 | Values::kDrivetrainEncoderRatio() * |
| 92 | control_loops::drivetrain::kWheelRadius; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | double drivetrain_velocity_translate(double in) { |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 96 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 97 | (2.0 * M_PI)) * |
| 98 | Values::kDrivetrainEncoderRatio() * |
| 99 | control_loops::drivetrain::kWheelRadius; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | double proximal_pot_translate(double voltage) { |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 103 | return -voltage * Values::kProximalPotRatio() * |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 104 | (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 105 | } |
| 106 | |
| 107 | double distal_pot_translate(double voltage) { |
| 108 | return voltage * Values::kDistalPotRatio() * |
| 109 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 110 | } |
| 111 | |
| 112 | double intake_pot_translate(double voltage) { |
| 113 | return voltage * Values::kIntakeMotorPotRatio() * |
| 114 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 115 | } |
| 116 | |
| 117 | double intake_spring_translate(double voltage) { |
| 118 | return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) / |
| 119 | (5.0 /*volts*/); |
| 120 | } |
| 121 | |
| 122 | // TODO() figure out differnce between max and min voltages on shifter pots. |
| 123 | // Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be |
| 124 | // passed drectly into the drivetrain position queue. |
| 125 | double drivetrain_shifter_pot_translate(double voltage) { |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 126 | return (voltage - Values::kDrivetrainShifterPotMinVoltage()) / |
| 127 | (Values::kDrivetrainShifterPotMaxVoltage() - |
| 128 | Values::kDrivetrainShifterPotMinVoltage()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 132 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 133 | Values::kMaxIntakeMotorEncoderPulsesPerSecond()); |
| 134 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 135 | "fast encoders are too fast"); |
| 136 | |
| 137 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 138 | max(Values::kMaxProximalEncoderPulsesPerSecond(), |
| 139 | Values::kMaxDistalEncoderPulsesPerSecond()); |
| 140 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 141 | "medium encoders are too fast"); |
| 142 | |
| 143 | // Class to send position messages with sensor readings to our loops. |
Austin Schuh | 6abf5b7 | 2019-02-02 20:20:54 -0800 | [diff] [blame] | 144 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 145 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 146 | SensorReader(::aos::ShmEventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 147 | : ::frc971::wpilib::SensorReader(event_loop), |
| 148 | superstructure_position_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 149 | event_loop->MakeSender<superstructure::Position>( |
| 150 | "/superstructure")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 151 | drivetrain_position_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 152 | event_loop |
| 153 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 154 | "/drivetrain")) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 155 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 156 | // we should ever see. |
Austin Schuh | 6abf5b7 | 2019-02-02 20:20:54 -0800 | [diff] [blame] | 157 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 158 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void set_left_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 162 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 163 | left_drivetrain_shifter_ = ::std::move(potentiometer); |
| 164 | } |
| 165 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 166 | void set_right_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 167 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 168 | right_drivetrain_shifter_ = ::std::move(potentiometer); |
| 169 | } |
| 170 | |
| 171 | // Proximal joint. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 172 | void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 173 | medium_encoder_filter_.Add(encoder.get()); |
| 174 | proximal_encoder_.set_encoder(::std::move(encoder)); |
| 175 | } |
| 176 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 177 | void set_proximal_absolute_pwm( |
| 178 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 179 | proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 180 | } |
| 181 | |
| 182 | void set_proximal_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 183 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 184 | proximal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 185 | } |
| 186 | |
| 187 | // Distal joint. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 188 | void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 189 | medium_encoder_filter_.Add(encoder.get()); |
| 190 | distal_encoder_.set_encoder(::std::move(encoder)); |
| 191 | } |
| 192 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 193 | void set_distal_absolute_pwm( |
| 194 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 195 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 196 | distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 197 | } |
| 198 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 199 | void set_distal_potentiometer( |
| 200 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 201 | distal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 202 | } |
| 203 | |
| 204 | // Left intake side. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 205 | void set_left_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 206 | fast_encoder_filter_.Add(encoder.get()); |
| 207 | left_intake_encoder_.set_encoder(::std::move(encoder)); |
| 208 | } |
| 209 | |
| 210 | void set_left_intake_absolute_pwm( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 211 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 212 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 213 | left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 214 | } |
| 215 | |
| 216 | void set_left_intake_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 217 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 218 | left_intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 219 | } |
| 220 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 221 | void set_left_intake_spring_angle( |
| 222 | ::std::unique_ptr<frc::AnalogInput> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 223 | left_intake_spring_angle_ = ::std::move(encoder); |
| 224 | } |
| 225 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 226 | void set_left_intake_cube_detector( |
| 227 | ::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 228 | left_intake_cube_detector_ = ::std::move(input); |
| 229 | } |
| 230 | |
| 231 | // Right intake side. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 232 | void set_right_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 233 | fast_encoder_filter_.Add(encoder.get()); |
| 234 | right_intake_encoder_.set_encoder(::std::move(encoder)); |
| 235 | } |
| 236 | |
| 237 | void set_right_intake_absolute_pwm( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 238 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 239 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 240 | right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 241 | } |
| 242 | |
| 243 | void set_right_intake_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 244 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 245 | right_intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 246 | } |
| 247 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 248 | void set_right_intake_spring_angle( |
| 249 | ::std::unique_ptr<frc::AnalogInput> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 250 | right_intake_spring_angle_ = ::std::move(encoder); |
| 251 | } |
| 252 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 253 | void set_right_intake_cube_detector( |
| 254 | ::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 255 | right_intake_cube_detector_ = ::std::move(input); |
| 256 | } |
| 257 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 258 | void set_claw_beambreak(::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 259 | claw_beambreak_ = ::std::move(input); |
| 260 | } |
| 261 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 262 | void set_box_back_beambreak(::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 263 | box_back_beambreak_ = ::std::move(input); |
| 264 | } |
| 265 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 266 | void set_lidar_lite_input( |
| 267 | ::std::unique_ptr<frc::DigitalInput> lidar_lite_input) { |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 268 | lidar_lite_input_ = ::std::move(lidar_lite_input); |
| 269 | lidar_lite_.set_input(lidar_lite_input_.get()); |
| 270 | } |
| 271 | |
Austin Schuh | 6abf5b7 | 2019-02-02 20:20:54 -0800 | [diff] [blame] | 272 | void Start() { AddToDMA(&lidar_lite_); } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 273 | |
| 274 | void RunIteration() { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 275 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 276 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 277 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 278 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 279 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 280 | drivetrain_builder.add_left_encoder( |
| 281 | drivetrain_translate(drivetrain_left_encoder_->GetRaw())); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 282 | drivetrain_builder.add_left_speed( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 283 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 284 | drivetrain_builder.add_left_shifter_position( |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 285 | drivetrain_shifter_pot_translate( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 286 | left_drivetrain_shifter_->GetVoltage())); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 287 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 288 | drivetrain_builder.add_right_encoder( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 289 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw())); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 290 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 291 | drivetrain_right_encoder_->GetPeriod())); |
| 292 | drivetrain_builder.add_right_shifter_position( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | drivetrain_shifter_pot_translate( |
| 294 | right_drivetrain_shifter_->GetVoltage())); |
| 295 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 296 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 297 | } |
Austin Schuh | 6abf5b7 | 2019-02-02 20:20:54 -0800 | [diff] [blame] | 298 | } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 299 | |
Austin Schuh | 6abf5b7 | 2019-02-02 20:20:54 -0800 | [diff] [blame] | 300 | void RunDmaIteration() { |
| 301 | const auto values = constants::GetValues(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 302 | |
| 303 | { |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 304 | auto builder = superstructure_position_sender_.MakeBuilder(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 305 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 306 | // Proximal arm |
| 307 | frc971::PotAndAbsolutePositionT arm_proximal; |
| 308 | CopyPosition(proximal_encoder_, &arm_proximal, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 309 | Values::kProximalEncoderCountsPerRevolution(), |
| 310 | Values::kProximalEncoderRatio(), proximal_pot_translate, |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 311 | true, values.arm_proximal.potentiometer_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 312 | flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_proximal_offset = |
| 313 | frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_proximal); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 314 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 315 | // Distal arm |
| 316 | frc971::PotAndAbsolutePositionT arm_distal; |
| 317 | CopyPosition(distal_encoder_, &arm_distal, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 318 | Values::kDistalEncoderCountsPerRevolution(), |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 319 | Values::kDistalEncoderRatio(), distal_pot_translate, true, |
| 320 | values.arm_distal.potentiometer_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 321 | flatbuffers::Offset<frc971::PotAndAbsolutePosition> arm_distal_offset = |
| 322 | frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &arm_distal); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 323 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 324 | superstructure::ArmPosition::Builder arm_position_builder = |
| 325 | builder.MakeBuilder<superstructure::ArmPosition>(); |
| 326 | arm_position_builder.add_proximal(arm_proximal_offset); |
| 327 | arm_position_builder.add_distal(arm_distal_offset); |
| 328 | |
| 329 | flatbuffers::Offset<superstructure::ArmPosition> arm_position_offset = |
| 330 | arm_position_builder.Finish(); |
| 331 | |
| 332 | // Left intake |
| 333 | frc971::PotAndAbsolutePositionT left_intake_motor_position; |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 334 | CopyPosition(left_intake_encoder_, &left_intake_motor_position, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 335 | Values::kIntakeMotorEncoderCountsPerRevolution(), |
| 336 | Values::kIntakeMotorEncoderRatio(), intake_pot_translate, |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 337 | false, values.left_intake.potentiometer_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 338 | flatbuffers::Offset<frc971::PotAndAbsolutePosition> |
| 339 | left_intake_motor_position_offset = |
| 340 | frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), |
| 341 | &left_intake_motor_position); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 342 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 343 | // Right intake |
| 344 | frc971::PotAndAbsolutePositionT right_intake_motor_position; |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 345 | CopyPosition(right_intake_encoder_, &right_intake_motor_position, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 346 | Values::kIntakeMotorEncoderCountsPerRevolution(), |
| 347 | Values::kIntakeMotorEncoderRatio(), intake_pot_translate, |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 348 | true, values.right_intake.potentiometer_offset); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 349 | flatbuffers::Offset<frc971::PotAndAbsolutePosition> |
| 350 | right_intake_motor_position_offset = |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 351 | frc971::PotAndAbsolutePosition::Pack( |
| 352 | *builder.fbb(), &right_intake_motor_position); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 353 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 354 | superstructure::IntakeElasticSensors::Builder |
| 355 | left_intake_sensors_builder = |
| 356 | builder.MakeBuilder<superstructure::IntakeElasticSensors>(); |
| 357 | |
| 358 | left_intake_sensors_builder.add_motor_position( |
| 359 | left_intake_motor_position_offset); |
| 360 | left_intake_sensors_builder.add_spring_angle( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 361 | intake_spring_translate(left_intake_spring_angle_->GetVoltage()) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 362 | values.left_intake.spring_offset); |
| 363 | left_intake_sensors_builder.add_beam_break( |
| 364 | !left_intake_cube_detector_->Get()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 365 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 366 | flatbuffers::Offset<superstructure::IntakeElasticSensors> |
| 367 | left_intake_offset = left_intake_sensors_builder.Finish(); |
| 368 | |
| 369 | superstructure::IntakeElasticSensors::Builder |
| 370 | right_intake_sensors_builder = |
| 371 | builder.MakeBuilder<superstructure::IntakeElasticSensors>(); |
| 372 | |
| 373 | right_intake_sensors_builder.add_motor_position( |
| 374 | right_intake_motor_position_offset); |
| 375 | right_intake_sensors_builder.add_spring_angle( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 376 | -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 377 | values.right_intake.spring_offset); |
| 378 | right_intake_sensors_builder.add_beam_break( |
| 379 | !right_intake_cube_detector_->Get()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 380 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 381 | flatbuffers::Offset<control_loops::superstructure::IntakeElasticSensors> |
| 382 | right_intake_offset = right_intake_sensors_builder.Finish(); |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 383 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 384 | superstructure::Position::Builder superstructure_builder = |
| 385 | builder.MakeBuilder<superstructure::Position>(); |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 386 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 387 | superstructure_builder.add_left_intake(left_intake_offset); |
| 388 | superstructure_builder.add_right_intake(right_intake_offset); |
| 389 | superstructure_builder.add_arm(arm_position_offset); |
| 390 | |
| 391 | superstructure_builder.add_claw_beambreak_triggered( |
| 392 | !claw_beambreak_->Get()); |
| 393 | superstructure_builder.add_box_back_beambreak_triggered( |
| 394 | !box_back_beambreak_->Get()); |
| 395 | |
| 396 | superstructure_builder.add_box_distance(lidar_lite_.last_width() / |
| 397 | 0.00001 / 100.0 / 2); |
| 398 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 399 | builder.CheckOk(builder.Send(superstructure_builder.Finish())); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 400 | } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 403 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 404 | ::aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 405 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 406 | drivetrain_position_sender_; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 407 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 408 | ::std::unique_ptr<frc::AnalogInput> left_drivetrain_shifter_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 409 | right_drivetrain_shifter_; |
| 410 | |
| 411 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_, |
| 412 | distal_encoder_; |
| 413 | |
| 414 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_, |
| 415 | right_intake_encoder_; |
| 416 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 417 | ::std::unique_ptr<frc::AnalogInput> left_intake_spring_angle_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 418 | right_intake_spring_angle_; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 419 | ::std::unique_ptr<frc::DigitalInput> left_intake_cube_detector_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 420 | right_intake_cube_detector_; |
| 421 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 422 | ::std::unique_ptr<frc::DigitalInput> claw_beambreak_; |
| 423 | ::std::unique_ptr<frc::DigitalInput> box_back_beambreak_; |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 424 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 425 | ::std::unique_ptr<frc::DigitalInput> lidar_lite_input_; |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 426 | ::frc971::wpilib::DMAPulseWidthReader lidar_lite_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | class SolenoidWriter { |
| 430 | public: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 431 | SolenoidWriter(::aos::ShmEventLoop *event_loop, |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 432 | ::frc971::wpilib::BufferedPcm *pcm) |
| 433 | : event_loop_(event_loop), |
| 434 | drivetrain_fetcher_( |
| 435 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 436 | ->MakeFetcher<::frc971::control_loops::drivetrain::Output>( |
| 437 | "/drivetrain")), |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 438 | superstructure_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 439 | event_loop->MakeFetcher<superstructure::Output>("/superstructure")), |
| 440 | status_light_fetcher_( |
| 441 | event_loop->MakeFetcher<::y2018::StatusLight>("/superstructure")), |
Austin Schuh | 300f2f6 | 2019-05-27 13:49:23 -0700 | [diff] [blame] | 442 | vision_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 443 | event_loop->MakeFetcher<::y2018::vision::VisionStatus>("/vision")), |
| 444 | pneumatics_to_log_sender_( |
| 445 | event_loop->MakeSender<::frc971::wpilib::PneumaticsToLog>("/aos")), |
Austin Schuh | 9950f68 | 2021-11-06 15:27:58 -0700 | [diff] [blame] | 446 | pcm_(pcm), |
| 447 | compressor_(0) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 448 | event_loop->set_name("Solenoids"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 449 | event_loop_->SetRuntimeRealtimePriority(27); |
| 450 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 451 | event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 452 | ::std::chrono::milliseconds(20), |
| 453 | ::std::chrono::milliseconds(1)); |
| 454 | } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 455 | |
| 456 | // left drive |
| 457 | // right drive |
| 458 | // |
| 459 | // claw |
| 460 | // arm brakes |
| 461 | // hook release |
| 462 | // fork release |
| 463 | void set_left_drivetrain_shifter( |
| 464 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 465 | left_drivetrain_shifter_ = ::std::move(s); |
| 466 | } |
| 467 | void set_right_drivetrain_shifter( |
| 468 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 469 | right_drivetrain_shifter_ = ::std::move(s); |
| 470 | } |
| 471 | |
| 472 | void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 473 | claw_ = ::std::move(s); |
| 474 | } |
| 475 | |
| 476 | void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 477 | arm_brakes_ = ::std::move(s); |
| 478 | } |
| 479 | |
| 480 | void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 481 | hook_ = ::std::move(s); |
| 482 | } |
| 483 | |
| 484 | void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 485 | forks_ = ::std::move(s); |
| 486 | } |
| 487 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 488 | void Loop(const int iterations) { |
| 489 | if (iterations != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 490 | AOS_LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 491 | } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 492 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 493 | { |
| 494 | drivetrain_fetcher_.Fetch(); |
| 495 | if (drivetrain_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 496 | left_drivetrain_shifter_->Set(!drivetrain_fetcher_->left_high()); |
| 497 | right_drivetrain_shifter_->Set(!drivetrain_fetcher_->right_high()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 500 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 501 | { |
| 502 | superstructure_fetcher_.Fetch(); |
| 503 | if (superstructure_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 504 | claw_->Set(!superstructure_fetcher_->claw_grabbed()); |
| 505 | arm_brakes_->Set(superstructure_fetcher_->release_arm_brake()); |
| 506 | hook_->Set(superstructure_fetcher_->hook_release()); |
| 507 | forks_->Set(superstructure_fetcher_->forks_release()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 512 | auto builder = pneumatics_to_log_sender_.MakeBuilder(); |
| 513 | |
| 514 | ::frc971::wpilib::PneumaticsToLog::Builder to_log_builder = |
| 515 | builder.MakeBuilder<frc971::wpilib::PneumaticsToLog>(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 516 | |
| 517 | pcm_->Flush(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 518 | to_log_builder.add_read_solenoids(pcm_->GetAll()); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 519 | (void)builder.Send(to_log_builder.Finish()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | monotonic_clock::time_point monotonic_now = event_loop_->monotonic_now(); |
| 523 | status_light_fetcher_.Fetch(); |
| 524 | // If we don't have a light request (or it's an old one), we are borked. |
| 525 | // Flash the red light slowly. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 526 | StatusLightT color; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 527 | if (!status_light_fetcher_.get() || |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 528 | monotonic_now > status_light_fetcher_.context().monotonic_event_time + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 529 | chrono::milliseconds(100)) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 530 | color.red = 0.0; |
| 531 | color.green = 0.0; |
| 532 | color.blue = 0.0; |
| 533 | |
| 534 | vision_status_fetcher_.Fetch(); |
| 535 | ++light_flash_; |
| 536 | if (light_flash_ > 10) { |
| 537 | color.red = 0.5; |
| 538 | } else if (!vision_status_fetcher_.get() || |
| 539 | monotonic_now > |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 540 | vision_status_fetcher_.context().monotonic_event_time + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 541 | chrono::seconds(1)) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 542 | color.red = 0.5; |
| 543 | color.green = 0.5; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 546 | if (light_flash_ > 20) { |
| 547 | light_flash_ = 0; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 548 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 549 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 550 | status_light_fetcher_->UnPackTo(&color); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 551 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 552 | SetColor(color); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 555 | void SetColor(const StatusLightT &status_light) { |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 556 | // Save CAN bandwidth and CPU at the cost of RT. Only change the light when |
| 557 | // it actually changes. This is pretty low priority anyways. |
| 558 | static int time_since_last_send = 0; |
| 559 | ++time_since_last_send; |
| 560 | if (time_since_last_send > 10) { |
| 561 | time_since_last_send = 0; |
| 562 | } |
| 563 | if (status_light.green != last_green_ || time_since_last_send == 0) { |
| 564 | canifier_.SetLEDOutput(1.0 - status_light.green, |
| 565 | ::ctre::phoenix::CANifier::LEDChannelB); |
| 566 | last_green_ = status_light.green; |
| 567 | } |
| 568 | |
| 569 | if (status_light.blue != last_blue_ || time_since_last_send == 0) { |
| 570 | canifier_.SetLEDOutput(1.0 - status_light.blue, |
| 571 | ::ctre::phoenix::CANifier::LEDChannelA); |
| 572 | last_blue_ = status_light.blue; |
| 573 | } |
| 574 | |
| 575 | if (status_light.red != last_red_ || time_since_last_send == 0) { |
| 576 | canifier_.SetLEDOutput(1.0 - status_light.red, |
| 577 | ::ctre::phoenix::CANifier::LEDChannelC); |
| 578 | last_red_ = status_light.red; |
| 579 | } |
| 580 | } |
| 581 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 582 | void Quit() { run_ = false; } |
| 583 | |
| 584 | private: |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 585 | ::aos::EventLoop *event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 586 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Output> |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 587 | drivetrain_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 588 | ::aos::Fetcher<superstructure::Output> superstructure_fetcher_; |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 589 | ::aos::Fetcher<::y2018::StatusLight> status_light_fetcher_; |
Austin Schuh | 300f2f6 | 2019-05-27 13:49:23 -0700 | [diff] [blame] | 590 | ::aos::Fetcher<::y2018::vision::VisionStatus> vision_status_fetcher_; |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 591 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 592 | aos::Sender<::frc971::wpilib::PneumaticsToLog> pneumatics_to_log_sender_; |
| 593 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 594 | ::frc971::wpilib::BufferedPcm *pcm_; |
| 595 | |
| 596 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> |
| 597 | left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_, |
| 598 | hook_, forks_; |
| 599 | |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 600 | ::ctre::phoenix::CANifier canifier_{0}; |
| 601 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 602 | ::std::atomic<bool> run_{true}; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 603 | |
| 604 | double last_red_ = -1.0; |
| 605 | double last_green_ = -1.0; |
| 606 | double last_blue_ = -1.0; |
| 607 | |
Austin Schuh | 9950f68 | 2021-11-06 15:27:58 -0700 | [diff] [blame] | 608 | frc::Compressor compressor_; |
| 609 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 610 | int light_flash_ = 0; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 611 | }; |
| 612 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 613 | class SuperstructureWriter |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 614 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 615 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 616 | SuperstructureWriter(::aos::EventLoop *event_loop) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 617 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 618 | event_loop, "/superstructure") {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 619 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 620 | void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 621 | proximal_victor_ = ::std::move(t); |
| 622 | } |
| 623 | void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 624 | distal_victor_ = ::std::move(t); |
| 625 | } |
| 626 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 627 | void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 628 | hanger_victor_ = ::std::move(t); |
| 629 | } |
| 630 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 631 | void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 632 | left_intake_elastic_victor_ = ::std::move(t); |
| 633 | } |
| 634 | void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 635 | right_intake_elastic_victor_ = ::std::move(t); |
| 636 | } |
| 637 | |
| 638 | void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 639 | left_intake_rollers_victor_ = ::std::move(t); |
| 640 | } |
| 641 | |
| 642 | void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 643 | right_intake_rollers_victor_ = ::std::move(t); |
| 644 | } |
| 645 | |
| 646 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 647 | virtual void Write(const superstructure::Output &output) override { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 648 | left_intake_elastic_victor_->SetSpeed( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 649 | ::aos::Clip(-output.left_intake()->voltage_elastic(), -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 650 | kMaxBringupPower) / |
| 651 | 12.0); |
| 652 | |
| 653 | right_intake_elastic_victor_->SetSpeed( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 654 | ::aos::Clip(output.right_intake()->voltage_elastic(), -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 655 | kMaxBringupPower) / |
| 656 | 12.0); |
| 657 | |
| 658 | left_intake_rollers_victor_->SetSpeed( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 659 | ::aos::Clip(-output.left_intake()->voltage_rollers(), -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 660 | kMaxBringupPower) / |
| 661 | 12.0); |
| 662 | |
| 663 | right_intake_rollers_victor_->SetSpeed( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 664 | ::aos::Clip(output.right_intake()->voltage_rollers(), -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 665 | kMaxBringupPower) / |
| 666 | 12.0); |
| 667 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 668 | proximal_victor_->SetSpeed(::aos::Clip(-output.voltage_proximal(), |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 669 | -kMaxBringupPower, |
| 670 | kMaxBringupPower) / |
| 671 | 12.0); |
| 672 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 673 | distal_victor_->SetSpeed(::aos::Clip(output.voltage_distal(), |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 674 | -kMaxBringupPower, kMaxBringupPower) / |
| 675 | 12.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 676 | hanger_victor_->SetSpeed(::aos::Clip(-output.voltage_winch(), |
| 677 | -kMaxBringupPower, kMaxBringupPower) / |
| 678 | 12.0); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | virtual void Stop() override { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 682 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 683 | |
| 684 | left_intake_rollers_victor_->SetDisabled(); |
| 685 | right_intake_rollers_victor_->SetDisabled(); |
| 686 | left_intake_elastic_victor_->SetDisabled(); |
| 687 | right_intake_elastic_victor_->SetDisabled(); |
| 688 | |
| 689 | proximal_victor_->SetDisabled(); |
| 690 | distal_victor_->SetDisabled(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 691 | hanger_victor_->SetDisabled(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_, |
| 695 | right_intake_rollers_victor_, left_intake_elastic_victor_, |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 696 | right_intake_elastic_victor_, proximal_victor_, distal_victor_, |
| 697 | hanger_victor_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 698 | }; |
| 699 | |
| 700 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 701 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 702 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 703 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 704 | frc::Encoder::k4X); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | void Run() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 708 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 709 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 710 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 711 | // Thread 1. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 712 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 713 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 714 | &joystick_sender_event_loop); |
| 715 | AddLoop(&joystick_sender_event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 716 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 717 | // Thread 2. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 718 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 719 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 720 | AddLoop(&pdp_fetcher_event_loop); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 721 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 722 | // Thread 3. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 723 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 724 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 725 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 726 | sensor_reader.set_left_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 727 | make_unique<frc::AnalogInput>(6)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 728 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 729 | sensor_reader.set_right_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 730 | make_unique<frc::AnalogInput>(7)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 731 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 732 | sensor_reader.set_proximal_encoder(make_encoder(4)); |
| 733 | sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
| 734 | sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(2)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 735 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 736 | sensor_reader.set_distal_encoder(make_encoder(2)); |
| 737 | sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
| 738 | sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(3)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 739 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 740 | sensor_reader.set_right_intake_encoder(make_encoder(5)); |
| 741 | sensor_reader.set_right_intake_absolute_pwm( |
| 742 | make_unique<frc::DigitalInput>(7)); |
| 743 | sensor_reader.set_right_intake_potentiometer( |
| 744 | make_unique<frc::AnalogInput>(1)); |
| 745 | sensor_reader.set_right_intake_spring_angle( |
| 746 | make_unique<frc::AnalogInput>(5)); |
| 747 | sensor_reader.set_right_intake_cube_detector( |
| 748 | make_unique<frc::DigitalInput>(1)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 749 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 750 | sensor_reader.set_left_intake_encoder(make_encoder(3)); |
| 751 | sensor_reader.set_left_intake_absolute_pwm( |
| 752 | make_unique<frc::DigitalInput>(4)); |
| 753 | sensor_reader.set_left_intake_potentiometer( |
| 754 | make_unique<frc::AnalogInput>(0)); |
| 755 | sensor_reader.set_left_intake_spring_angle( |
| 756 | make_unique<frc::AnalogInput>(4)); |
| 757 | sensor_reader.set_left_intake_cube_detector( |
| 758 | make_unique<frc::DigitalInput>(0)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 759 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 760 | sensor_reader.set_claw_beambreak(make_unique<frc::DigitalInput>(8)); |
| 761 | sensor_reader.set_box_back_beambreak(make_unique<frc::DigitalInput>(9)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 762 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 763 | sensor_reader.set_pwm_trigger(true); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 764 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 765 | sensor_reader.set_lidar_lite_input(make_unique<frc::DigitalInput>(22)); |
| 766 | AddLoop(&sensor_reader_event_loop); |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 767 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 768 | // Thread 4. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 769 | ::aos::ShmEventLoop imu_event_loop(&config.message()); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 770 | auto imu_trigger = make_unique<frc::DigitalInput>(5); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 771 | ::frc971::wpilib::ADIS16448 imu( |
| 772 | &imu_event_loop, frc::SPI::Port::kOnboardCS1, imu_trigger.get()); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 773 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 774 | auto imu_reset = make_unique<frc::DigitalOutput>(6); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 775 | imu.set_reset(imu_reset.get()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 776 | AddLoop(&imu_event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 777 | |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 778 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 779 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 780 | // variety so all the Victors are written as SPs. |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 781 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 782 | // Thread 5. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 783 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 784 | |
| 785 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
Sabina Davis | caa2a6b | 2019-02-03 01:15:37 -0800 | [diff] [blame] | 786 | drivetrain_writer.set_left_controller0( |
| 787 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)), false); |
| 788 | drivetrain_writer.set_right_controller0( |
| 789 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), true); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 790 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 791 | SuperstructureWriter superstructure_writer(&output_event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 792 | superstructure_writer.set_left_intake_elastic_victor( |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 793 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 794 | superstructure_writer.set_left_intake_rollers_victor( |
| 795 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 796 | superstructure_writer.set_right_intake_elastic_victor( |
| 797 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7))); |
| 798 | superstructure_writer.set_right_intake_rollers_victor( |
| 799 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 800 | superstructure_writer.set_proximal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 801 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 802 | superstructure_writer.set_distal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 803 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 804 | superstructure_writer.set_hanger_victor( |
| 805 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 806 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 807 | AddLoop(&output_event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 808 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 809 | // Thread 6. |
Austin Schuh | 01a9f2a | 2019-05-27 13:36:30 -0700 | [diff] [blame] | 810 | // This is a separate event loop because we want to run it at much lower |
| 811 | // priority. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 812 | ::aos::ShmEventLoop solenoid_writer_event_loop(&config.message()); |
Austin Schuh | bfbaa37 | 2019-02-15 23:05:31 -0800 | [diff] [blame] | 813 | ::frc971::wpilib::BufferedPcm pcm; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 814 | SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, &pcm); |
Austin Schuh | bfbaa37 | 2019-02-15 23:05:31 -0800 | [diff] [blame] | 815 | solenoid_writer.set_left_drivetrain_shifter(pcm.MakeSolenoid(0)); |
| 816 | solenoid_writer.set_right_drivetrain_shifter(pcm.MakeSolenoid(1)); |
| 817 | solenoid_writer.set_claw(pcm.MakeSolenoid(2)); |
| 818 | solenoid_writer.set_arm_brakes(pcm.MakeSolenoid(3)); |
| 819 | solenoid_writer.set_hook(pcm.MakeSolenoid(4)); |
| 820 | solenoid_writer.set_forks(pcm.MakeSolenoid(5)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 821 | AddLoop(&solenoid_writer_event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 822 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 823 | RunLoops(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 824 | } |
| 825 | }; |
| 826 | |
| 827 | } // namespace |
| 828 | } // namespace wpilib |
| 829 | } // namespace y2018 |
| 830 | |
| 831 | AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot); |