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