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