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