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