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