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> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
| 12 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 14 | #include "frc971/wpilib/ahal/Counter.h" |
| 15 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 16 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 17 | #include "frc971/wpilib/ahal/Encoder.h" |
| 18 | #include "frc971/wpilib/ahal/Relay.h" |
| 19 | #include "frc971/wpilib/ahal/Servo.h" |
| 20 | #include "frc971/wpilib/ahal/VictorSP.h" |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 21 | #include "ctre/phoenix/CANifier.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 22 | #undef ERROR |
| 23 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 24 | #include "aos/commonmath.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" |
| 30 | #include "aos/stl_mutex/stl_mutex.h" |
| 31 | #include "aos/time/time.h" |
| 32 | #include "aos/util/compiler_memory_barrier.h" |
| 33 | #include "aos/util/log_interval.h" |
| 34 | #include "aos/util/phased_loop.h" |
| 35 | #include "aos/util/wrapping_counter.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 36 | |
| 37 | #include "frc971/autonomous/auto.q.h" |
| 38 | #include "frc971/control_loops/control_loops.q.h" |
| 39 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 40 | #include "frc971/wpilib/ADIS16448.h" |
| 41 | #include "frc971/wpilib/buffered_pcm.h" |
| 42 | #include "frc971/wpilib/buffered_solenoid.h" |
| 43 | #include "frc971/wpilib/dma.h" |
| 44 | #include "frc971/wpilib/dma_edge_counting.h" |
| 45 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 46 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 47 | #include "frc971/wpilib/joystick_sender.h" |
| 48 | #include "frc971/wpilib/logging.q.h" |
| 49 | #include "frc971/wpilib/loop_output_handler.h" |
| 50 | #include "frc971/wpilib/pdp_fetcher.h" |
| 51 | #include "frc971/wpilib/wpilib_interface.h" |
| 52 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 53 | #include "y2018/constants.h" |
| 54 | #include "y2018/control_loops/superstructure/superstructure.q.h" |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 55 | #include "y2018/status_light.q.h" |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 56 | #include "y2018/vision/vision.q.h" |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 57 | |
| 58 | #ifndef M_PI |
| 59 | #define M_PI 3.14159265358979323846 |
| 60 | #endif |
| 61 | |
| 62 | using ::frc971::control_loops::drivetrain_queue; |
| 63 | using ::y2018::control_loops::superstructure_queue; |
| 64 | using ::y2018::constants::Values; |
| 65 | using ::aos::monotonic_clock; |
| 66 | namespace chrono = ::std::chrono; |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 67 | using aos::make_unique; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 68 | |
| 69 | namespace y2018 { |
| 70 | namespace wpilib { |
| 71 | namespace { |
| 72 | |
| 73 | constexpr double kMaxBringupPower = 12.0; |
| 74 | |
| 75 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 76 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 77 | // The low bit is direction. |
| 78 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 79 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 80 | // constexpr. |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 81 | template <typename T> |
| 82 | constexpr T max(T a, T b) { |
| 83 | return (a > b) ? a : b; |
| 84 | } |
| 85 | |
| 86 | template <typename T, typename... Rest> |
| 87 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 88 | return max(max(a, b), c, rest...); |
| 89 | } |
| 90 | |
| 91 | double drivetrain_translate(int32_t in) { |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 92 | return ((static_cast<double>(in) / |
| 93 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
| 94 | (2.0 * M_PI)) * |
| 95 | Values::kDrivetrainEncoderRatio() * |
| 96 | control_loops::drivetrain::kWheelRadius; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | double drivetrain_velocity_translate(double in) { |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 100 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 101 | (2.0 * M_PI)) * |
| 102 | Values::kDrivetrainEncoderRatio() * |
| 103 | control_loops::drivetrain::kWheelRadius; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | double proximal_pot_translate(double voltage) { |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 107 | return -voltage * Values::kProximalPotRatio() * |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 108 | (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 109 | } |
| 110 | |
| 111 | double distal_pot_translate(double voltage) { |
| 112 | return voltage * Values::kDistalPotRatio() * |
| 113 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 114 | } |
| 115 | |
| 116 | double intake_pot_translate(double voltage) { |
| 117 | return voltage * Values::kIntakeMotorPotRatio() * |
| 118 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 119 | } |
| 120 | |
| 121 | double intake_spring_translate(double voltage) { |
| 122 | return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) / |
| 123 | (5.0 /*volts*/); |
| 124 | } |
| 125 | |
| 126 | // TODO() figure out differnce between max and min voltages on shifter pots. |
| 127 | // Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be |
| 128 | // passed drectly into the drivetrain position queue. |
| 129 | double drivetrain_shifter_pot_translate(double voltage) { |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 130 | return (voltage - Values::kDrivetrainShifterPotMinVoltage()) / |
| 131 | (Values::kDrivetrainShifterPotMaxVoltage() - |
| 132 | Values::kDrivetrainShifterPotMinVoltage()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 136 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 137 | Values::kMaxIntakeMotorEncoderPulsesPerSecond()); |
| 138 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 139 | "fast encoders are too fast"); |
| 140 | |
| 141 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 142 | max(Values::kMaxProximalEncoderPulsesPerSecond(), |
| 143 | Values::kMaxDistalEncoderPulsesPerSecond()); |
| 144 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 145 | "medium encoders are too fast"); |
| 146 | |
| 147 | // Class to send position messages with sensor readings to our loops. |
| 148 | class SensorReader { |
| 149 | public: |
| 150 | SensorReader() { |
| 151 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 152 | // we should ever see. |
| 153 | fast_encoder_filter_.SetPeriodNanoSeconds( |
| 154 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 155 | kMaxFastEncoderPulsesPerSecond * 1e9 + |
| 156 | 0.5)); |
| 157 | medium_encoder_filter_.SetPeriodNanoSeconds( |
| 158 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 159 | kMaxMediumEncoderPulsesPerSecond * 1e9 + |
| 160 | 0.5)); |
| 161 | hall_filter_.SetPeriodNanoSeconds(100000); |
| 162 | } |
| 163 | |
| 164 | // Left drivetrain side. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 165 | void set_drivetrain_left_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 166 | fast_encoder_filter_.Add(encoder.get()); |
| 167 | drivetrain_left_encoder_ = ::std::move(encoder); |
| 168 | } |
| 169 | |
| 170 | void set_left_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 171 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 172 | left_drivetrain_shifter_ = ::std::move(potentiometer); |
| 173 | } |
| 174 | |
| 175 | // Right drivetrain side. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 176 | void set_drivetrain_right_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 177 | fast_encoder_filter_.Add(encoder.get()); |
| 178 | drivetrain_right_encoder_ = ::std::move(encoder); |
| 179 | } |
| 180 | |
| 181 | void set_right_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 182 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 183 | right_drivetrain_shifter_ = ::std::move(potentiometer); |
| 184 | } |
| 185 | |
| 186 | // Proximal joint. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 187 | void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 188 | medium_encoder_filter_.Add(encoder.get()); |
| 189 | proximal_encoder_.set_encoder(::std::move(encoder)); |
| 190 | } |
| 191 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 192 | void set_proximal_absolute_pwm( |
| 193 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 194 | proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 195 | } |
| 196 | |
| 197 | void set_proximal_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 198 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 199 | proximal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 200 | } |
| 201 | |
| 202 | // Distal joint. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 203 | void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 204 | medium_encoder_filter_.Add(encoder.get()); |
| 205 | distal_encoder_.set_encoder(::std::move(encoder)); |
| 206 | } |
| 207 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 208 | void set_distal_absolute_pwm( |
| 209 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 210 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 211 | distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 212 | } |
| 213 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 214 | void set_distal_potentiometer( |
| 215 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 216 | distal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 217 | } |
| 218 | |
| 219 | // Left intake side. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 220 | void set_left_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 221 | fast_encoder_filter_.Add(encoder.get()); |
| 222 | left_intake_encoder_.set_encoder(::std::move(encoder)); |
| 223 | } |
| 224 | |
| 225 | void set_left_intake_absolute_pwm( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 226 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 227 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 228 | left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 229 | } |
| 230 | |
| 231 | void set_left_intake_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 232 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 233 | left_intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 234 | } |
| 235 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 236 | void set_left_intake_spring_angle( |
| 237 | ::std::unique_ptr<frc::AnalogInput> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 238 | left_intake_spring_angle_ = ::std::move(encoder); |
| 239 | } |
| 240 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 241 | void set_left_intake_cube_detector( |
| 242 | ::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 243 | left_intake_cube_detector_ = ::std::move(input); |
| 244 | } |
| 245 | |
| 246 | // Right intake side. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 247 | void set_right_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 248 | fast_encoder_filter_.Add(encoder.get()); |
| 249 | right_intake_encoder_.set_encoder(::std::move(encoder)); |
| 250 | } |
| 251 | |
| 252 | void set_right_intake_absolute_pwm( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 253 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 254 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 255 | right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 256 | } |
| 257 | |
| 258 | void set_right_intake_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 259 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 260 | right_intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 261 | } |
| 262 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 263 | void set_right_intake_spring_angle( |
| 264 | ::std::unique_ptr<frc::AnalogInput> encoder) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 265 | right_intake_spring_angle_ = ::std::move(encoder); |
| 266 | } |
| 267 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 268 | void set_right_intake_cube_detector( |
| 269 | ::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 270 | right_intake_cube_detector_ = ::std::move(input); |
| 271 | } |
| 272 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 273 | void set_claw_beambreak(::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 274 | claw_beambreak_ = ::std::move(input); |
| 275 | } |
| 276 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 277 | void set_box_back_beambreak(::std::unique_ptr<frc::DigitalInput> input) { |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 278 | box_back_beambreak_ = ::std::move(input); |
| 279 | } |
| 280 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 281 | // Auto mode switches. |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 282 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 283 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 284 | } |
| 285 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 286 | void set_pwm_trigger(::std::unique_ptr<frc::DigitalInput> pwm_trigger) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 287 | medium_encoder_filter_.Add(pwm_trigger.get()); |
| 288 | pwm_trigger_ = ::std::move(pwm_trigger); |
| 289 | } |
| 290 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 291 | 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] | 292 | lidar_lite_input_ = ::std::move(lidar_lite_input); |
| 293 | lidar_lite_.set_input(lidar_lite_input_.get()); |
| 294 | } |
| 295 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 296 | // All of the DMA-related set_* calls must be made before this, and it |
| 297 | // doesn't hurt to do all of them. |
| 298 | void set_dma(::std::unique_ptr<DMA> dma) { |
| 299 | dma_synchronizer_.reset( |
| 300 | new ::frc971::wpilib::DMASynchronizer(::std::move(dma))); |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 301 | dma_synchronizer_->Add(&lidar_lite_); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void RunPWMDetecter() { |
| 305 | ::aos::SetCurrentThreadRealtimePriority(41); |
| 306 | |
| 307 | pwm_trigger_->RequestInterrupts(); |
| 308 | // Rising edge only. |
| 309 | pwm_trigger_->SetUpSourceEdge(true, false); |
| 310 | |
| 311 | monotonic_clock::time_point last_posedge_monotonic = |
| 312 | monotonic_clock::min_time; |
| 313 | |
| 314 | while (run_) { |
| 315 | auto ret = pwm_trigger_->WaitForInterrupt(1.0, true); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 316 | if (ret == frc::InterruptableSensorBase::WaitResult::kRisingEdge) { |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 317 | // Grab all the clocks. |
| 318 | const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp(); |
| 319 | |
| 320 | aos_compiler_memory_barrier(); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 321 | const double fpga_time_before = frc::GetFPGATime() * 1e-6; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 322 | aos_compiler_memory_barrier(); |
| 323 | const monotonic_clock::time_point monotonic_now = |
| 324 | monotonic_clock::now(); |
| 325 | aos_compiler_memory_barrier(); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 326 | const double fpga_time_after = frc::GetFPGATime() * 1e-6; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 327 | aos_compiler_memory_barrier(); |
| 328 | |
| 329 | const double fpga_offset = |
| 330 | (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time; |
| 331 | |
| 332 | // Compute when the edge was. |
| 333 | const monotonic_clock::time_point monotonic_edge = |
| 334 | monotonic_now - chrono::duration_cast<chrono::nanoseconds>( |
| 335 | chrono::duration<double>(fpga_offset)); |
| 336 | |
| 337 | LOG(DEBUG, "Got PWM pulse %f spread, %f offset, %lld trigger\n", |
| 338 | fpga_time_after - fpga_time_before, fpga_offset, |
| 339 | monotonic_edge.time_since_epoch().count()); |
| 340 | |
| 341 | // Compute bounds on the timestep and sampling times. |
| 342 | const double fpga_sample_length = fpga_time_after - fpga_time_before; |
| 343 | const chrono::nanoseconds elapsed_time = |
| 344 | monotonic_edge - last_posedge_monotonic; |
| 345 | |
| 346 | last_posedge_monotonic = monotonic_edge; |
| 347 | |
| 348 | // Verify that the values are sane. |
| 349 | if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) { |
| 350 | continue; |
| 351 | } |
| 352 | if (fpga_offset < 0 || fpga_offset > 0.00015) { |
| 353 | continue; |
| 354 | } |
| 355 | if (elapsed_time > |
| 356 | chrono::microseconds(5050) + chrono::microseconds(4) || |
| 357 | elapsed_time < |
| 358 | chrono::microseconds(5050) - chrono::microseconds(4)) { |
| 359 | continue; |
| 360 | } |
| 361 | // Good edge! |
| 362 | { |
| 363 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 364 | last_tick_time_monotonic_timepoint_ = last_posedge_monotonic; |
| 365 | last_period_ = elapsed_time; |
| 366 | } |
| 367 | } else { |
| 368 | LOG(INFO, "PWM triggered %d\n", ret); |
| 369 | } |
| 370 | } |
| 371 | pwm_trigger_->CancelInterrupts(); |
| 372 | } |
| 373 | |
| 374 | void operator()() { |
| 375 | ::aos::SetCurrentThreadName("SensorReader"); |
| 376 | |
| 377 | my_pid_ = getpid(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 378 | |
| 379 | dma_synchronizer_->Start(); |
| 380 | |
| 381 | ::aos::time::PhasedLoop phased_loop(last_period_, |
| 382 | ::std::chrono::milliseconds(3)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 383 | |
| 384 | ::std::thread pwm_detecter_thread( |
| 385 | ::std::bind(&SensorReader::RunPWMDetecter, this)); |
| 386 | |
| 387 | ::aos::SetCurrentThreadRealtimePriority(40); |
| 388 | while (run_) { |
| 389 | { |
| 390 | const int iterations = phased_loop.SleepUntilNext(); |
| 391 | if (iterations != 1) { |
| 392 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 393 | } |
| 394 | } |
| 395 | RunIteration(); |
| 396 | |
| 397 | monotonic_clock::time_point last_tick_timepoint; |
| 398 | chrono::nanoseconds period; |
| 399 | { |
| 400 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 401 | last_tick_timepoint = last_tick_time_monotonic_timepoint_; |
| 402 | period = last_period_; |
| 403 | } |
| 404 | |
| 405 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 406 | continue; |
| 407 | } |
| 408 | chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime( |
| 409 | period, last_tick_timepoint + chrono::microseconds(2050)); |
| 410 | |
| 411 | // TODO(austin): If this is the first edge in a while, skip to it (plus |
| 412 | // an offset). Otherwise, slowly drift time to line up. |
| 413 | |
| 414 | phased_loop.set_interval_and_offset(period, new_offset); |
| 415 | } |
| 416 | pwm_detecter_thread.join(); |
| 417 | } |
| 418 | |
| 419 | void RunIteration() { |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame] | 420 | ::frc971::wpilib::SendRobotState(my_pid_); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 421 | |
| 422 | const auto values = constants::GetValues(); |
| 423 | |
| 424 | { |
| 425 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 426 | drivetrain_message->left_encoder = |
| 427 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 428 | drivetrain_message->left_speed = |
| 429 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 430 | drivetrain_message->left_shifter_position = |
| 431 | drivetrain_shifter_pot_translate( |
| 432 | left_drivetrain_shifter_->GetVoltage()); |
| 433 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 434 | drivetrain_message->right_encoder = |
| 435 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 436 | drivetrain_message->right_speed = |
| 437 | -drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 438 | drivetrain_message->right_shifter_position = |
| 439 | drivetrain_shifter_pot_translate( |
| 440 | right_drivetrain_shifter_->GetVoltage()); |
| 441 | |
| 442 | drivetrain_message.Send(); |
| 443 | } |
| 444 | |
| 445 | dma_synchronizer_->RunIteration(); |
| 446 | |
| 447 | { |
| 448 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 449 | |
| 450 | CopyPosition(proximal_encoder_, &superstructure_message->arm.proximal, |
| 451 | Values::kProximalEncoderCountsPerRevolution(), |
| 452 | Values::kProximalEncoderRatio(), proximal_pot_translate, |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 453 | true, values.arm_proximal.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 454 | |
| 455 | CopyPosition(distal_encoder_, &superstructure_message->arm.distal, |
| 456 | Values::kDistalEncoderCountsPerRevolution(), |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 457 | Values::kDistalEncoderRatio(), distal_pot_translate, true, |
| 458 | values.arm_distal.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 459 | |
| 460 | CopyPosition(left_intake_encoder_, |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 461 | &superstructure_message->left_intake.motor_position, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 462 | Values::kIntakeMotorEncoderCountsPerRevolution(), |
| 463 | Values::kIntakeMotorEncoderRatio(), intake_pot_translate, |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 464 | false, values.left_intake.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 465 | |
| 466 | CopyPosition(right_intake_encoder_, |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 467 | &superstructure_message->right_intake.motor_position, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 468 | Values::kIntakeMotorEncoderCountsPerRevolution(), |
| 469 | Values::kIntakeMotorEncoderRatio(), intake_pot_translate, |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 470 | true, values.right_intake.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 471 | |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 472 | superstructure_message->left_intake.spring_angle = |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 473 | intake_spring_translate(left_intake_spring_angle_->GetVoltage()) + |
| 474 | values.left_intake.spring_offset; |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 475 | superstructure_message->left_intake.beam_break = |
Austin Schuh | ef978fc | 2018-03-21 20:38:06 -0700 | [diff] [blame] | 476 | !left_intake_cube_detector_->Get(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 477 | |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 478 | superstructure_message->right_intake.spring_angle = |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 479 | -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) + |
| 480 | values.right_intake.spring_offset; |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 481 | superstructure_message->right_intake.beam_break = |
Austin Schuh | ef978fc | 2018-03-21 20:38:06 -0700 | [diff] [blame] | 482 | !right_intake_cube_detector_->Get(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 483 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 484 | superstructure_message->claw_beambreak_triggered = !claw_beambreak_->Get(); |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 485 | superstructure_message->box_back_beambreak_triggered = |
| 486 | !box_back_beambreak_->Get(); |
| 487 | |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 488 | superstructure_message->box_distance = |
| 489 | lidar_lite_.last_width() / 0.00001 / 100.0 / 2; |
| 490 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 491 | superstructure_message.Send(); |
| 492 | } |
| 493 | |
| 494 | { |
| 495 | auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage(); |
| 496 | auto_mode_message->mode = 0; |
| 497 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 498 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 499 | auto_mode_message->mode |= 1 << i; |
| 500 | } |
| 501 | } |
| 502 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 503 | auto_mode_message.Send(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void Quit() { run_ = false; } |
| 508 | |
| 509 | private: |
| 510 | double encoder_translate(int32_t value, double counts_per_revolution, |
| 511 | double ratio) { |
| 512 | return static_cast<double>(value) / counts_per_revolution * ratio * |
| 513 | (2.0 * M_PI); |
| 514 | } |
| 515 | |
| 516 | void CopyPosition( |
| 517 | const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder, |
| 518 | ::frc971::PotAndAbsolutePosition *position, |
| 519 | double encoder_counts_per_revolution, double encoder_ratio, |
| 520 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 521 | double pot_offset) { |
| 522 | const double multiplier = reverse ? -1.0 : 1.0; |
| 523 | position->pot = multiplier * potentiometer_translate( |
| 524 | encoder.ReadPotentiometerVoltage()) + |
| 525 | pot_offset; |
| 526 | position->encoder = |
| 527 | multiplier * encoder_translate(encoder.ReadRelativeEncoder(), |
| 528 | encoder_counts_per_revolution, |
| 529 | encoder_ratio); |
| 530 | |
| 531 | position->absolute_encoder = |
| 532 | (reverse ? (1.0 - encoder.ReadAbsoluteEncoder()) |
| 533 | : encoder.ReadAbsoluteEncoder()) * |
| 534 | encoder_ratio * (2.0 * M_PI); |
| 535 | } |
| 536 | |
| 537 | int32_t my_pid_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 538 | |
| 539 | // Mutex to manage access to the period and tick time variables. |
| 540 | ::aos::stl_mutex tick_time_mutex_; |
| 541 | monotonic_clock::time_point last_tick_time_monotonic_timepoint_ = |
| 542 | monotonic_clock::min_time; |
| 543 | chrono::nanoseconds last_period_ = chrono::microseconds(5050); |
| 544 | |
| 545 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 546 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 547 | frc::DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 548 | hall_filter_; |
| 549 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 550 | ::std::unique_ptr<frc::Encoder> drivetrain_left_encoder_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 551 | drivetrain_right_encoder_; |
| 552 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 553 | ::std::unique_ptr<frc::AnalogInput> left_drivetrain_shifter_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 554 | right_drivetrain_shifter_; |
| 555 | |
| 556 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_, |
| 557 | distal_encoder_; |
| 558 | |
| 559 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_, |
| 560 | right_intake_encoder_; |
| 561 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 562 | ::std::unique_ptr<frc::AnalogInput> left_intake_spring_angle_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 563 | right_intake_spring_angle_; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 564 | ::std::unique_ptr<frc::DigitalInput> left_intake_cube_detector_, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 565 | right_intake_cube_detector_; |
| 566 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 567 | ::std::unique_ptr<frc::DigitalInput> claw_beambreak_; |
| 568 | ::std::unique_ptr<frc::DigitalInput> box_back_beambreak_; |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame] | 569 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 570 | ::std::unique_ptr<frc::DigitalInput> pwm_trigger_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 571 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 572 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 4> autonomous_modes_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 573 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 574 | ::std::unique_ptr<frc::DigitalInput> lidar_lite_input_; |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 575 | ::frc971::wpilib::DMAPulseWidthReader lidar_lite_; |
| 576 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 577 | ::std::atomic<bool> run_{true}; |
| 578 | }; |
| 579 | |
| 580 | class SolenoidWriter { |
| 581 | public: |
| 582 | SolenoidWriter(::frc971::wpilib::BufferedPcm *pcm) |
| 583 | : pcm_(pcm), |
| 584 | drivetrain_(".frc971.control_loops.drivetrain_queue.output"), |
| 585 | superstructure_(".y2018.control_loops.superstructure_queue.output") {} |
| 586 | |
| 587 | // left drive |
| 588 | // right drive |
| 589 | // |
| 590 | // claw |
| 591 | // arm brakes |
| 592 | // hook release |
| 593 | // fork release |
| 594 | void set_left_drivetrain_shifter( |
| 595 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 596 | left_drivetrain_shifter_ = ::std::move(s); |
| 597 | } |
| 598 | void set_right_drivetrain_shifter( |
| 599 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 600 | right_drivetrain_shifter_ = ::std::move(s); |
| 601 | } |
| 602 | |
| 603 | void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 604 | claw_ = ::std::move(s); |
| 605 | } |
| 606 | |
| 607 | void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 608 | arm_brakes_ = ::std::move(s); |
| 609 | } |
| 610 | |
| 611 | void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 612 | hook_ = ::std::move(s); |
| 613 | } |
| 614 | |
| 615 | void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 616 | forks_ = ::std::move(s); |
| 617 | } |
| 618 | |
| 619 | void operator()() { |
| 620 | ::aos::SetCurrentThreadName("Solenoids"); |
| 621 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 622 | |
| 623 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 624 | ::std::chrono::milliseconds(1)); |
| 625 | |
| 626 | while (run_) { |
| 627 | { |
| 628 | const int iterations = phased_loop.SleepUntilNext(); |
| 629 | if (iterations != 1) { |
| 630 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | { |
| 635 | drivetrain_.FetchLatest(); |
| 636 | if (drivetrain_.get()) { |
| 637 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
| 638 | left_drivetrain_shifter_->Set(!drivetrain_->left_high); |
| 639 | right_drivetrain_shifter_->Set(!drivetrain_->right_high); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | { |
| 644 | superstructure_.FetchLatest(); |
| 645 | if (superstructure_.get()) { |
| 646 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 647 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 648 | claw_->Set(!superstructure_->claw_grabbed); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 649 | arm_brakes_->Set(superstructure_->release_arm_brake); |
| 650 | hook_->Set(superstructure_->hook_release); |
| 651 | forks_->Set(superstructure_->forks_release); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | { |
| 656 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 657 | |
| 658 | pcm_->Flush(); |
| 659 | to_log.read_solenoids = pcm_->GetAll(); |
| 660 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 661 | } |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 662 | |
| 663 | status_light.FetchLatest(); |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 664 | // If we don't have a light request (or it's an old one), we are borked. |
| 665 | // Flash the red light slowly. |
| 666 | if (!status_light.get() || |
| 667 | status_light.Age() > chrono::milliseconds(100)) { |
| 668 | StatusLight color; |
| 669 | color.red = 0.0; |
| 670 | color.green = 0.0; |
| 671 | color.blue = 0.0; |
| 672 | |
| 673 | ::y2018::vision::vision_status.FetchLatest(); |
| 674 | ++light_flash_; |
| 675 | if (light_flash_ > 10) { |
| 676 | color.red = 0.5; |
| 677 | } else if (!y2018::vision::vision_status.get() || |
| 678 | y2018::vision::vision_status.Age() > chrono::seconds(1)) { |
| 679 | color.red = 0.5; |
| 680 | color.green = 0.5; |
Austin Schuh | 1e2d498 | 2018-03-21 20:34:33 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 683 | if (light_flash_ > 20) { |
| 684 | light_flash_ = 0; |
Austin Schuh | 1e2d498 | 2018-03-21 20:34:33 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 687 | LOG_STRUCT(DEBUG, "color", color); |
| 688 | SetColor(color); |
| 689 | } else { |
| 690 | LOG_STRUCT(DEBUG, "color", *status_light); |
| 691 | SetColor(*status_light); |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 692 | } |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 696 | void SetColor(const StatusLight &status_light) { |
| 697 | // Save CAN bandwidth and CPU at the cost of RT. Only change the light when |
| 698 | // it actually changes. This is pretty low priority anyways. |
| 699 | static int time_since_last_send = 0; |
| 700 | ++time_since_last_send; |
| 701 | if (time_since_last_send > 10) { |
| 702 | time_since_last_send = 0; |
| 703 | } |
| 704 | if (status_light.green != last_green_ || time_since_last_send == 0) { |
| 705 | canifier_.SetLEDOutput(1.0 - status_light.green, |
| 706 | ::ctre::phoenix::CANifier::LEDChannelB); |
| 707 | last_green_ = status_light.green; |
| 708 | } |
| 709 | |
| 710 | if (status_light.blue != last_blue_ || time_since_last_send == 0) { |
| 711 | canifier_.SetLEDOutput(1.0 - status_light.blue, |
| 712 | ::ctre::phoenix::CANifier::LEDChannelA); |
| 713 | last_blue_ = status_light.blue; |
| 714 | } |
| 715 | |
| 716 | if (status_light.red != last_red_ || time_since_last_send == 0) { |
| 717 | canifier_.SetLEDOutput(1.0 - status_light.red, |
| 718 | ::ctre::phoenix::CANifier::LEDChannelC); |
| 719 | last_red_ = status_light.red; |
| 720 | } |
| 721 | } |
| 722 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 723 | void Quit() { run_ = false; } |
| 724 | |
| 725 | private: |
| 726 | ::frc971::wpilib::BufferedPcm *pcm_; |
| 727 | |
| 728 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> |
| 729 | left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_, |
| 730 | hook_, forks_; |
| 731 | |
| 732 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
| 733 | ::aos::Queue<::y2018::control_loops::SuperstructureQueue::Output> |
| 734 | superstructure_; |
| 735 | |
Brian Silverman | 37281fc | 2018-03-11 18:42:17 -0700 | [diff] [blame] | 736 | ::ctre::phoenix::CANifier canifier_{0}; |
| 737 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 738 | ::std::atomic<bool> run_{true}; |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 739 | |
| 740 | double last_red_ = -1.0; |
| 741 | double last_green_ = -1.0; |
| 742 | double last_blue_ = -1.0; |
| 743 | |
| 744 | int light_flash_ = 0; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 745 | }; |
| 746 | |
| 747 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 748 | public: |
| 749 | void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 750 | drivetrain_left_victor_ = ::std::move(t); |
| 751 | } |
| 752 | |
| 753 | void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 754 | drivetrain_right_victor_ = ::std::move(t); |
| 755 | } |
| 756 | |
| 757 | private: |
| 758 | virtual void Read() override { |
| 759 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 760 | } |
| 761 | |
| 762 | virtual void Write() override { |
| 763 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 764 | LOG_STRUCT(DEBUG, "will output", *queue); |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 765 | drivetrain_left_victor_->SetSpeed( |
| 766 | ::aos::Clip(queue->left_voltage, -12.0, 12.0) / 12.0); |
| 767 | drivetrain_right_victor_->SetSpeed( |
| 768 | ::aos::Clip(-queue->right_voltage, -12.0, 12.0) / 12.0); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | virtual void Stop() override { |
| 772 | LOG(WARNING, "drivetrain output too old\n"); |
| 773 | drivetrain_left_victor_->SetDisabled(); |
| 774 | drivetrain_right_victor_->SetDisabled(); |
| 775 | } |
| 776 | |
| 777 | ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_, |
| 778 | drivetrain_right_victor_; |
| 779 | }; |
| 780 | |
| 781 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 782 | public: |
| 783 | void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 784 | proximal_victor_ = ::std::move(t); |
| 785 | } |
| 786 | void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 787 | distal_victor_ = ::std::move(t); |
| 788 | } |
| 789 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 790 | void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 791 | hanger_victor_ = ::std::move(t); |
| 792 | } |
| 793 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 794 | void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 795 | left_intake_elastic_victor_ = ::std::move(t); |
| 796 | } |
| 797 | void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 798 | right_intake_elastic_victor_ = ::std::move(t); |
| 799 | } |
| 800 | |
| 801 | void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 802 | left_intake_rollers_victor_ = ::std::move(t); |
| 803 | } |
| 804 | |
| 805 | void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 806 | right_intake_rollers_victor_ = ::std::move(t); |
| 807 | } |
| 808 | |
| 809 | private: |
| 810 | virtual void Read() override { |
| 811 | ::y2018::control_loops::superstructure_queue.output.FetchAnother(); |
| 812 | } |
| 813 | |
| 814 | virtual void Write() override { |
| 815 | auto &queue = ::y2018::control_loops::superstructure_queue.output; |
| 816 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 817 | |
| 818 | left_intake_elastic_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 819 | ::aos::Clip(-queue->left_intake.voltage_elastic, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 820 | kMaxBringupPower) / |
| 821 | 12.0); |
| 822 | |
| 823 | right_intake_elastic_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 824 | ::aos::Clip(queue->right_intake.voltage_elastic, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 825 | kMaxBringupPower) / |
| 826 | 12.0); |
| 827 | |
| 828 | left_intake_rollers_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 829 | ::aos::Clip(-queue->left_intake.voltage_rollers, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 830 | kMaxBringupPower) / |
| 831 | 12.0); |
| 832 | |
| 833 | right_intake_rollers_victor_->SetSpeed( |
Sabina Davis | cfb872f | 2018-02-25 16:28:20 -0800 | [diff] [blame] | 834 | ::aos::Clip(queue->right_intake.voltage_rollers, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 835 | kMaxBringupPower) / |
| 836 | 12.0); |
| 837 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 838 | proximal_victor_->SetSpeed(::aos::Clip(-queue->voltage_proximal, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 839 | -kMaxBringupPower, |
| 840 | kMaxBringupPower) / |
| 841 | 12.0); |
| 842 | |
| 843 | distal_victor_->SetSpeed(::aos::Clip(queue->voltage_distal, |
| 844 | -kMaxBringupPower, kMaxBringupPower) / |
| 845 | 12.0); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 846 | hanger_victor_->SetSpeed( |
| 847 | ::aos::Clip(-queue->voltage_winch, -kMaxBringupPower, kMaxBringupPower) / |
| 848 | 12.0); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | virtual void Stop() override { |
| 852 | LOG(WARNING, "Superstructure output too old.\n"); |
| 853 | |
| 854 | left_intake_rollers_victor_->SetDisabled(); |
| 855 | right_intake_rollers_victor_->SetDisabled(); |
| 856 | left_intake_elastic_victor_->SetDisabled(); |
| 857 | right_intake_elastic_victor_->SetDisabled(); |
| 858 | |
| 859 | proximal_victor_->SetDisabled(); |
| 860 | distal_victor_->SetDisabled(); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 861 | hanger_victor_->SetDisabled(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_, |
| 865 | right_intake_rollers_victor_, left_intake_elastic_victor_, |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 866 | right_intake_elastic_victor_, proximal_victor_, distal_victor_, |
| 867 | hanger_victor_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 868 | }; |
| 869 | |
| 870 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 871 | public: |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 872 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 873 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 874 | frc::Encoder::k4X); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | void Run() override { |
| 878 | ::aos::InitNRT(); |
| 879 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 880 | |
| 881 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 882 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 883 | |
| 884 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 885 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 886 | SensorReader reader; |
| 887 | |
| 888 | // TODO(Sabina): Update port numbers(Sensors and Victors) |
| 889 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 890 | reader.set_left_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 891 | make_unique<frc::AnalogInput>(6)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 892 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 893 | reader.set_right_drivetrain_shifter_potentiometer( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 894 | make_unique<frc::AnalogInput>(7)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 895 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 896 | reader.set_proximal_encoder(make_encoder(4)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 897 | reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(2)); |
| 898 | reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(2)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 899 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 900 | reader.set_distal_encoder(make_encoder(2)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 901 | reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(3)); |
| 902 | reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(3)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 903 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 904 | reader.set_right_intake_encoder(make_encoder(5)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 905 | reader.set_right_intake_absolute_pwm(make_unique<frc::DigitalInput>(7)); |
| 906 | reader.set_right_intake_potentiometer(make_unique<frc::AnalogInput>(1)); |
| 907 | reader.set_right_intake_spring_angle(make_unique<frc::AnalogInput>(5)); |
| 908 | reader.set_right_intake_cube_detector(make_unique<frc::DigitalInput>(1)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 909 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 910 | reader.set_left_intake_encoder(make_encoder(3)); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 911 | reader.set_left_intake_absolute_pwm(make_unique<frc::DigitalInput>(4)); |
| 912 | reader.set_left_intake_potentiometer(make_unique<frc::AnalogInput>(0)); |
| 913 | reader.set_left_intake_spring_angle(make_unique<frc::AnalogInput>(4)); |
| 914 | reader.set_left_intake_cube_detector(make_unique<frc::DigitalInput>(0)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 915 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 916 | reader.set_claw_beambreak(make_unique<frc::DigitalInput>(8)); |
| 917 | reader.set_box_back_beambreak(make_unique<frc::DigitalInput>(9)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 918 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 919 | reader.set_pwm_trigger(make_unique<frc::DigitalInput>(25)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 920 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 921 | reader.set_lidar_lite_input(make_unique<frc::DigitalInput>(22)); |
Austin Schuh | 8e5950d | 2018-03-21 20:29:40 -0700 | [diff] [blame] | 922 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 923 | reader.set_dma(make_unique<DMA>()); |
| 924 | ::std::thread reader_thread(::std::ref(reader)); |
| 925 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 926 | auto imu_trigger = make_unique<frc::DigitalInput>(5); |
| 927 | ::frc971::wpilib::ADIS16448 imu(frc::SPI::Port::kOnboardCS1, |
| 928 | imu_trigger.get()); |
| 929 | imu.SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 930 | auto imu_reset = make_unique<frc::DigitalOutput>(6); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 931 | imu.set_reset(imu_reset.get()); |
| 932 | ::std::thread imu_thread(::std::ref(imu)); |
| 933 | |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 934 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though |
| 935 | // they are identical, as far as DrivetrainWriter is concerned, to the SP |
| 936 | // variety so all the Victors are written as SPs. |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 937 | |
| 938 | DrivetrainWriter drivetrain_writer; |
| 939 | drivetrain_writer.set_drivetrain_left_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 940 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 941 | drivetrain_writer.set_drivetrain_right_victor( |
| 942 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
| 943 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 944 | |
| 945 | SuperstructureWriter superstructure_writer; |
| 946 | superstructure_writer.set_left_intake_elastic_victor( |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 947 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 948 | superstructure_writer.set_left_intake_rollers_victor( |
| 949 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 950 | superstructure_writer.set_right_intake_elastic_victor( |
| 951 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7))); |
| 952 | superstructure_writer.set_right_intake_rollers_victor( |
| 953 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 954 | superstructure_writer.set_proximal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 955 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 956 | superstructure_writer.set_distal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 957 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
| 958 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 959 | superstructure_writer.set_hanger_victor( |
| 960 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 961 | |
| 962 | ::std::thread superstructure_writer_thread( |
| 963 | ::std::ref(superstructure_writer)); |
| 964 | |
| 965 | ::frc971::wpilib::BufferedPcm *pcm = new ::frc971::wpilib::BufferedPcm(); |
| 966 | SolenoidWriter solenoid_writer(pcm); |
| 967 | solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(0)); |
| 968 | solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1)); |
| 969 | solenoid_writer.set_claw(pcm->MakeSolenoid(2)); |
| 970 | solenoid_writer.set_arm_brakes(pcm->MakeSolenoid(3)); |
| 971 | solenoid_writer.set_hook(pcm->MakeSolenoid(4)); |
| 972 | solenoid_writer.set_forks(pcm->MakeSolenoid(5)); |
| 973 | |
| 974 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 975 | |
Austin Schuh | 005d3cb | 2018-03-21 20:49:54 -0700 | [diff] [blame] | 976 | int32_t status = 0; |
| 977 | HAL_CompressorHandle compressor = HAL_InitializeCompressor(0, &status); |
| 978 | if (status != 0) { |
| 979 | LOG(ERROR, "Compressor status is nonzero, %d\n", |
| 980 | static_cast<int>(status)); |
| 981 | } |
| 982 | HAL_SetCompressorClosedLoopControl(compressor, true, &status); |
| 983 | if (status != 0) { |
| 984 | LOG(ERROR, "Compressor status is nonzero, %d\n", |
| 985 | static_cast<int>(status)); |
| 986 | } |
| 987 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 988 | // Wait forever. Not much else to do... |
| 989 | while (true) { |
| 990 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 991 | if (r != 0) { |
| 992 | PLOG(WARNING, "infinite select failed"); |
| 993 | } else { |
| 994 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 999 | |
| 1000 | joystick_sender.Quit(); |
| 1001 | joystick_thread.join(); |
| 1002 | pdp_fetcher.Quit(); |
| 1003 | pdp_fetcher_thread.join(); |
| 1004 | reader.Quit(); |
| 1005 | reader_thread.join(); |
| 1006 | imu.Quit(); |
| 1007 | imu_thread.join(); |
| 1008 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 1009 | drivetrain_writer.Quit(); |
| 1010 | drivetrain_writer_thread.join(); |
| 1011 | superstructure_writer.Quit(); |
| 1012 | superstructure_writer_thread.join(); |
| 1013 | |
| 1014 | ::aos::Cleanup(); |
| 1015 | } |
| 1016 | }; |
| 1017 | |
| 1018 | } // namespace |
| 1019 | } // namespace wpilib |
| 1020 | } // namespace y2018 |
| 1021 | |
| 1022 | AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot); |