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