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