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: |
| 385 | SolenoidWriter(::frc971::wpilib::BufferedPcm *pcm) |
| 386 | : pcm_(pcm), |
| 387 | drivetrain_(".frc971.control_loops.drivetrain_queue.output"), |
| 388 | superstructure_(".y2018.control_loops.superstructure_queue.output") {} |
| 389 | |
| 390 | // left drive |
| 391 | // right drive |
| 392 | // |
| 393 | // claw |
| 394 | // arm brakes |
| 395 | // hook release |
| 396 | // fork release |
| 397 | void set_left_drivetrain_shifter( |
| 398 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 399 | left_drivetrain_shifter_ = ::std::move(s); |
| 400 | } |
| 401 | void set_right_drivetrain_shifter( |
| 402 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 403 | right_drivetrain_shifter_ = ::std::move(s); |
| 404 | } |
| 405 | |
| 406 | void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 407 | claw_ = ::std::move(s); |
| 408 | } |
| 409 | |
| 410 | void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 411 | arm_brakes_ = ::std::move(s); |
| 412 | } |
| 413 | |
| 414 | void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 415 | hook_ = ::std::move(s); |
| 416 | } |
| 417 | |
| 418 | void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 419 | forks_ = ::std::move(s); |
| 420 | } |
| 421 | |
| 422 | void operator()() { |
| 423 | ::aos::SetCurrentThreadName("Solenoids"); |
| 424 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 425 | |
| 426 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 427 | ::std::chrono::milliseconds(1)); |
| 428 | |
| 429 | while (run_) { |
| 430 | { |
| 431 | const int iterations = phased_loop.SleepUntilNext(); |
| 432 | if (iterations != 1) { |
| 433 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | { |
| 438 | drivetrain_.FetchLatest(); |
| 439 | if (drivetrain_.get()) { |
| 440 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
| 441 | left_drivetrain_shifter_->Set(!drivetrain_->left_high); |
| 442 | right_drivetrain_shifter_->Set(!drivetrain_->right_high); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | { |
| 447 | superstructure_.FetchLatest(); |
| 448 | if (superstructure_.get()) { |
| 449 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 450 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 451 | claw_->Set(!superstructure_->claw_grabbed); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 452 | arm_brakes_->Set(superstructure_->release_arm_brake); |
| 453 | hook_->Set(superstructure_->hook_release); |
| 454 | forks_->Set(superstructure_->forks_release); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | { |
| 459 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 460 | |
| 461 | pcm_->Flush(); |
| 462 | to_log.read_solenoids = pcm_->GetAll(); |
| 463 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 464 | } |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 465 | |
| 466 | status_light.FetchLatest(); |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 467 | // If we don't have a light request (or it's an old one), we are borked. |
| 468 | // Flash the red light slowly. |
| 469 | if (!status_light.get() || |
| 470 | status_light.Age() > chrono::milliseconds(100)) { |
| 471 | StatusLight color; |
| 472 | color.red = 0.0; |
| 473 | color.green = 0.0; |
| 474 | color.blue = 0.0; |
| 475 | |
| 476 | ::y2018::vision::vision_status.FetchLatest(); |
| 477 | ++light_flash_; |
| 478 | if (light_flash_ > 10) { |
| 479 | color.red = 0.5; |
| 480 | } else if (!y2018::vision::vision_status.get() || |
| 481 | y2018::vision::vision_status.Age() > chrono::seconds(1)) { |
| 482 | color.red = 0.5; |
| 483 | color.green = 0.5; |
Austin Schuh | 1e2d498 | 2018-03-21 20:34:33 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 486 | if (light_flash_ > 20) { |
| 487 | light_flash_ = 0; |
Austin Schuh | 1e2d498 | 2018-03-21 20:34:33 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 490 | LOG_STRUCT(DEBUG, "color", color); |
| 491 | SetColor(color); |
| 492 | } else { |
| 493 | LOG_STRUCT(DEBUG, "color", *status_light); |
| 494 | SetColor(*status_light); |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 495 | } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 499 | void SetColor(const StatusLight &status_light) { |
| 500 | // Save CAN bandwidth and CPU at the cost of RT. Only change the light when |
| 501 | // it actually changes. This is pretty low priority anyways. |
| 502 | static int time_since_last_send = 0; |
| 503 | ++time_since_last_send; |
| 504 | if (time_since_last_send > 10) { |
| 505 | time_since_last_send = 0; |
| 506 | } |
| 507 | if (status_light.green != last_green_ || time_since_last_send == 0) { |
| 508 | canifier_.SetLEDOutput(1.0 - status_light.green, |
| 509 | ::ctre::phoenix::CANifier::LEDChannelB); |
| 510 | last_green_ = status_light.green; |
| 511 | } |
| 512 | |
| 513 | if (status_light.blue != last_blue_ || time_since_last_send == 0) { |
| 514 | canifier_.SetLEDOutput(1.0 - status_light.blue, |
| 515 | ::ctre::phoenix::CANifier::LEDChannelA); |
| 516 | last_blue_ = status_light.blue; |
| 517 | } |
| 518 | |
| 519 | if (status_light.red != last_red_ || time_since_last_send == 0) { |
| 520 | canifier_.SetLEDOutput(1.0 - status_light.red, |
| 521 | ::ctre::phoenix::CANifier::LEDChannelC); |
| 522 | last_red_ = status_light.red; |
| 523 | } |
| 524 | } |
| 525 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 526 | void Quit() { run_ = false; } |
| 527 | |
| 528 | private: |
| 529 | ::frc971::wpilib::BufferedPcm *pcm_; |
| 530 | |
| 531 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> |
| 532 | left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_, |
| 533 | hook_, forks_; |
| 534 | |
| 535 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
| 536 | ::aos::Queue<::y2018::control_loops::SuperstructureQueue::Output> |
| 537 | superstructure_; |
| 538 | |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 539 | ::ctre::phoenix::CANifier canifier_{0}; |
| 540 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 541 | ::std::atomic<bool> run_{true}; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 542 | |
| 543 | double last_red_ = -1.0; |
| 544 | double last_green_ = -1.0; |
| 545 | double last_blue_ = -1.0; |
| 546 | |
| 547 | int light_flash_ = 0; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 548 | }; |
| 549 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 550 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 551 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 552 | SuperstructureWriter(::aos::EventLoop *event_loop) |
| 553 | : ::frc971::wpilib::LoopOutputHandler(event_loop) {} |
| 554 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 555 | void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 556 | proximal_victor_ = ::std::move(t); |
| 557 | } |
| 558 | void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 559 | distal_victor_ = ::std::move(t); |
| 560 | } |
| 561 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 562 | void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 563 | hanger_victor_ = ::std::move(t); |
| 564 | } |
| 565 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 566 | void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 567 | left_intake_elastic_victor_ = ::std::move(t); |
| 568 | } |
| 569 | void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 570 | right_intake_elastic_victor_ = ::std::move(t); |
| 571 | } |
| 572 | |
| 573 | void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 574 | left_intake_rollers_victor_ = ::std::move(t); |
| 575 | } |
| 576 | |
| 577 | void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 578 | right_intake_rollers_victor_ = ::std::move(t); |
| 579 | } |
| 580 | |
| 581 | private: |
| 582 | virtual void Read() override { |
| 583 | ::y2018::control_loops::superstructure_queue.output.FetchAnother(); |
| 584 | } |
| 585 | |
| 586 | virtual void Write() override { |
| 587 | auto &queue = ::y2018::control_loops::superstructure_queue.output; |
| 588 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 589 | |
| 590 | left_intake_elastic_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 591 | ::aos::Clip(-queue->left_intake.voltage_elastic, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 592 | kMaxBringupPower) / |
| 593 | 12.0); |
| 594 | |
| 595 | right_intake_elastic_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 596 | ::aos::Clip(queue->right_intake.voltage_elastic, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 597 | kMaxBringupPower) / |
| 598 | 12.0); |
| 599 | |
| 600 | left_intake_rollers_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 601 | ::aos::Clip(-queue->left_intake.voltage_rollers, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 602 | kMaxBringupPower) / |
| 603 | 12.0); |
| 604 | |
| 605 | right_intake_rollers_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 606 | ::aos::Clip(queue->right_intake.voltage_rollers, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 607 | kMaxBringupPower) / |
| 608 | 12.0); |
| 609 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 610 | proximal_victor_->SetSpeed(::aos::Clip(-queue->voltage_proximal, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 611 | -kMaxBringupPower, |
| 612 | kMaxBringupPower) / |
| 613 | 12.0); |
| 614 | |
| 615 | distal_victor_->SetSpeed(::aos::Clip(queue->voltage_distal, |
| 616 | -kMaxBringupPower, kMaxBringupPower) / |
| 617 | 12.0); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 618 | hanger_victor_->SetSpeed( |
| 619 | ::aos::Clip(-queue->voltage_winch, -kMaxBringupPower, kMaxBringupPower) / |
| 620 | 12.0); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | virtual void Stop() override { |
| 624 | LOG(WARNING, "Superstructure output too old.\n"); |
| 625 | |
| 626 | left_intake_rollers_victor_->SetDisabled(); |
| 627 | right_intake_rollers_victor_->SetDisabled(); |
| 628 | left_intake_elastic_victor_->SetDisabled(); |
| 629 | right_intake_elastic_victor_->SetDisabled(); |
| 630 | |
| 631 | proximal_victor_->SetDisabled(); |
| 632 | distal_victor_->SetDisabled(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 633 | hanger_victor_->SetDisabled(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_, |
| 637 | right_intake_rollers_victor_, left_intake_elastic_victor_, |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 638 | right_intake_elastic_victor_, proximal_victor_, distal_victor_, |
| 639 | hanger_victor_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 640 | }; |
| 641 | |
| 642 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 643 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 644 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 645 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 646 | frc::Encoder::k4X); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | void Run() override { |
| 650 | ::aos::InitNRT(); |
| 651 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 652 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 653 | ::aos::ShmEventLoop event_loop; |
| 654 | |
| 655 | ::frc971::wpilib::JoystickSender joystick_sender(&event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 656 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 657 | |
| 658 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 659 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 660 | |
| 661 | SensorReader reader(&event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 662 | |
| 663 | // TODO(Sabina): Update port numbers(Sensors and Victors) |
| 664 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 665 | reader.set_left_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 666 | make_unique<frc::AnalogInput>(6)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 667 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 668 | reader.set_right_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 669 | make_unique<frc::AnalogInput>(7)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 670 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 671 | reader.set_proximal_encoder(make_encoder(4)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 672 | reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
| 673 | reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(2)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 674 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 675 | reader.set_distal_encoder(make_encoder(2)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 676 | reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
| 677 | reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(3)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 678 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 679 | reader.set_right_intake_encoder(make_encoder(5)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 680 | reader.set_right_intake_absolute_pwm(make_unique<frc::DigitalInput>(7)); |
| 681 | reader.set_right_intake_potentiometer(make_unique<frc::AnalogInput>(1)); |
| 682 | reader.set_right_intake_spring_angle(make_unique<frc::AnalogInput>(5)); |
| 683 | reader.set_right_intake_cube_detector(make_unique<frc::DigitalInput>(1)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 684 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 685 | reader.set_left_intake_encoder(make_encoder(3)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 686 | reader.set_left_intake_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 687 | reader.set_left_intake_potentiometer(make_unique<frc::AnalogInput>(0)); |
| 688 | reader.set_left_intake_spring_angle(make_unique<frc::AnalogInput>(4)); |
| 689 | reader.set_left_intake_cube_detector(make_unique<frc::DigitalInput>(0)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 690 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 691 | reader.set_claw_beambreak(make_unique<frc::DigitalInput>(8)); |
| 692 | reader.set_box_back_beambreak(make_unique<frc::DigitalInput>(9)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 693 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 694 | reader.set_pwm_trigger(true); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 695 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 696 | reader.set_lidar_lite_input(make_unique<frc::DigitalInput>(22)); |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 697 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 698 | ::std::thread reader_thread(::std::ref(reader)); |
| 699 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 700 | auto imu_trigger = make_unique<frc::DigitalInput>(5); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 701 | ::frc971::wpilib::ADIS16448 imu(&event_loop, frc::SPI::Port::kOnboardCS1, |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 702 | imu_trigger.get()); |
| 703 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 704 | auto imu_reset = make_unique<frc::DigitalOutput>(6); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 705 | imu.set_reset(imu_reset.get()); |
| 706 | ::std::thread imu_thread(::std::ref(imu)); |
| 707 | |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 708 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 709 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 710 | // variety so all the Victors are written as SPs. |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 711 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 712 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop); |
Sabina Davis | caa2a6b | 2019-02-03 01:15:37 -0800 | [diff] [blame] | 713 | drivetrain_writer.set_left_controller0( |
| 714 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2)), false); |
| 715 | drivetrain_writer.set_right_controller0( |
| 716 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3)), true); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 717 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 718 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame^] | 719 | SuperstructureWriter superstructure_writer(&event_loop); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 720 | superstructure_writer.set_left_intake_elastic_victor( |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 721 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 722 | superstructure_writer.set_left_intake_rollers_victor( |
| 723 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 724 | superstructure_writer.set_right_intake_elastic_victor( |
| 725 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7))); |
| 726 | superstructure_writer.set_right_intake_rollers_victor( |
| 727 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 728 | superstructure_writer.set_proximal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 729 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 730 | superstructure_writer.set_distal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 731 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
| 732 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 733 | superstructure_writer.set_hanger_victor( |
| 734 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 735 | |
| 736 | ::std::thread superstructure_writer_thread( |
| 737 | ::std::ref(superstructure_writer)); |
| 738 | |
Austin Schuh | bfbaa37 | 2019-02-15 23:05:31 -0800 | [diff] [blame] | 739 | ::frc971::wpilib::BufferedPcm pcm; |
| 740 | SolenoidWriter solenoid_writer(&pcm); |
| 741 | solenoid_writer.set_left_drivetrain_shifter(pcm.MakeSolenoid(0)); |
| 742 | solenoid_writer.set_right_drivetrain_shifter(pcm.MakeSolenoid(1)); |
| 743 | solenoid_writer.set_claw(pcm.MakeSolenoid(2)); |
| 744 | solenoid_writer.set_arm_brakes(pcm.MakeSolenoid(3)); |
| 745 | solenoid_writer.set_hook(pcm.MakeSolenoid(4)); |
| 746 | solenoid_writer.set_forks(pcm.MakeSolenoid(5)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 747 | |
| 748 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 749 | |
Austin Schuh | 005d3cb | 2018-03-21 20:49:54 -0700 | [diff] [blame] | 750 | int32_t status = 0; |
| 751 | HAL_CompressorHandle compressor = HAL_InitializeCompressor(0, &status); |
| 752 | if (status != 0) { |
| 753 | LOG(ERROR, "Compressor status is nonzero, %d\n", |
| 754 | static_cast<int>(status)); |
| 755 | } |
| 756 | HAL_SetCompressorClosedLoopControl(compressor, true, &status); |
| 757 | if (status != 0) { |
| 758 | LOG(ERROR, "Compressor status is nonzero, %d\n", |
| 759 | static_cast<int>(status)); |
| 760 | } |
| 761 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 762 | // Wait forever. Not much else to do... |
| 763 | while (true) { |
| 764 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 765 | if (r != 0) { |
| 766 | PLOG(WARNING, "infinite select failed"); |
| 767 | } else { |
| 768 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 773 | |
| 774 | joystick_sender.Quit(); |
| 775 | joystick_thread.join(); |
| 776 | pdp_fetcher.Quit(); |
| 777 | pdp_fetcher_thread.join(); |
| 778 | reader.Quit(); |
| 779 | reader_thread.join(); |
| 780 | imu.Quit(); |
| 781 | imu_thread.join(); |
| 782 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 783 | drivetrain_writer.Quit(); |
| 784 | drivetrain_writer_thread.join(); |
| 785 | superstructure_writer.Quit(); |
| 786 | superstructure_writer_thread.join(); |
| 787 | |
| 788 | ::aos::Cleanup(); |
| 789 | } |
| 790 | }; |
| 791 | |
| 792 | } // namespace |
| 793 | } // namespace wpilib |
| 794 | } // namespace y2018 |
| 795 | |
| 796 | AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot); |