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