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