Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <inttypes.h> |
| 5 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 6 | #include <chrono> |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 7 | #include <thread> |
| 8 | #include <mutex> |
| 9 | #include <functional> |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 10 | #include <array> |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 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/Compressor.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/Talon.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 19 | #include "frc971/wpilib/wpilib_robot_base.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 20 | #undef ERROR |
| 21 | |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 22 | #include "aos/commonmath.h" |
| 23 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 24 | #include "aos/logging/logging.h" |
| 25 | #include "aos/logging/queue_logging.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 26 | #include "aos/make_unique.h" |
| 27 | #include "aos/robot_state/robot_state.q.h" |
| 28 | #include "aos/stl_mutex/stl_mutex.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 29 | #include "aos/time/time.h" |
| 30 | #include "aos/util/log_interval.h" |
| 31 | #include "aos/util/phased_loop.h" |
| 32 | #include "aos/util/wrapping_counter.h" |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 33 | #include "frc971/autonomous/auto.q.h" |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 34 | #include "frc971/control_loops/control_loops.q.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 35 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/ADIS16448.h" |
| 37 | #include "frc971/wpilib/buffered_pcm.h" |
| 38 | #include "frc971/wpilib/buffered_solenoid.h" |
| 39 | #include "frc971/wpilib/dma.h" |
| 40 | #include "frc971/wpilib/dma_edge_counting.h" |
| 41 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 42 | #include "frc971/wpilib/gyro_sender.h" |
| 43 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 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" |
| 48 | #include "frc971/wpilib/sensor_reader.h" |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 49 | #include "y2016/constants.h" |
Comran Morshed | 5bb1211 | 2016-02-16 13:48:57 +0000 | [diff] [blame] | 50 | #include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 51 | #include "y2016/control_loops/shooter/shooter.q.h" |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 52 | #include "y2016/control_loops/shooter/shooter.q.h" |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 53 | #include "y2016/control_loops/superstructure/superstructure.q.h" |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 54 | #include "y2016/queues/ball_detector.q.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 55 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 56 | #ifndef M_PI |
| 57 | #define M_PI 3.14159265358979323846 |
| 58 | #endif |
| 59 | |
| 60 | using ::frc971::control_loops::drivetrain_queue; |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 61 | using ::y2016::control_loops::shooter::shooter_queue; |
| 62 | using ::y2016::control_loops::superstructure_queue; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 63 | using namespace frc; |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 64 | using aos::make_unique; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 65 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 66 | namespace y2016 { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 67 | namespace wpilib { |
Austin Schuh | a9992ff | 2016-02-28 21:59:23 -0800 | [diff] [blame] | 68 | namespace { |
| 69 | constexpr double kMaxBringupPower = 12.0; |
| 70 | } // namespace |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 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 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 76 | // Translates for the sensor values to convert raw index pulses into something |
| 77 | // with proper units. |
| 78 | |
| 79 | // TODO(comran): Template these methods since there is a lot of repetition here. |
| 80 | double hall_translate(double in) { |
| 81 | // Turn voltage from our 3-state halls into a ratio that the loop can use. |
| 82 | return in / 5.0; |
| 83 | } |
| 84 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 85 | double drivetrain_translate(int32_t in) { |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 86 | return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) * |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 87 | constants::Values::kDrivetrainEncoderRatio * |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 88 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | double drivetrain_velocity_translate(double in) { |
| 92 | return (1.0 / in) / 256.0 /*cpr*/ * |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 93 | constants::Values::kDrivetrainEncoderRatio * |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 94 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 97 | double shooter_translate(int32_t in) { |
Comran Morshed | 5bb1211 | 2016-02-16 13:48:57 +0000 | [diff] [blame] | 98 | return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) * |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 99 | constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/); |
| 100 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 101 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 102 | double intake_translate(int32_t in) { |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 103 | return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 104 | constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/); |
| 105 | } |
| 106 | |
| 107 | double shoulder_translate(int32_t in) { |
| 108 | return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 109 | constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/); |
| 110 | } |
| 111 | |
| 112 | double wrist_translate(int32_t in) { |
| 113 | return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 114 | constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/); |
| 115 | } |
| 116 | |
| 117 | double intake_pot_translate(double voltage) { |
| 118 | return voltage * constants::Values::kIntakePotRatio * |
Comran Morshed | 5bb1211 | 2016-02-16 13:48:57 +0000 | [diff] [blame] | 119 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | double shoulder_pot_translate(double voltage) { |
| 123 | return voltage * constants::Values::kShoulderPotRatio * |
Comran Morshed | 5bb1211 | 2016-02-16 13:48:57 +0000 | [diff] [blame] | 124 | (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | double wrist_pot_translate(double voltage) { |
| 128 | return voltage * constants::Values::kWristPotRatio * |
Comran Morshed | 5bb1211 | 2016-02-16 13:48:57 +0000 | [diff] [blame] | 129 | (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Comran Morshed | 1b76432 | 2016-02-14 20:18:12 +0000 | [diff] [blame] | 132 | constexpr double kMaxDrivetrainEncoderPulsesPerSecond = |
| 133 | 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 / |
| 134 | 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ / |
| 135 | 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */; |
| 136 | |
| 137 | constexpr double kMaxShooterEncoderPulsesPerSecond = |
| 138 | 18700.0 /* 775pro free speed RPM */ * 12.0 / |
| 139 | 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ * |
| 140 | 128.0 /* CPR */ * 4 /* edges per cycle */; |
| 141 | |
| 142 | double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max( |
| 143 | kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond); |
| 144 | |
| 145 | constexpr double kMaxSuperstructureEncoderPulsesPerSecond = |
| 146 | 18700.0 /* 775pro free speed RPM */ * 12.0 / |
| 147 | 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ * |
| 148 | 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 149 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 150 | // Class to send position messages with sensor readings to our loops. |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 151 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 152 | public: |
| 153 | SensorReader() { |
| 154 | // Set it to filter out anything shorter than 1/4 of the minimum pulse width |
| 155 | // we should ever see. |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 156 | UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond); |
| 157 | UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 158 | hall_filter_.SetPeriodNanoSeconds(100000); |
| 159 | } |
| 160 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 161 | void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) { |
| 162 | drivetrain_left_hall_ = ::std::move(analog); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 165 | void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) { |
| 166 | drivetrain_right_hall_ = ::std::move(analog); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 169 | // Shooter setters. |
| 170 | void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 171 | fast_encoder_filter_.Add(encoder.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 172 | shooter_left_encoder_ = ::std::move(encoder); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 175 | void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 176 | fast_encoder_filter_.Add(encoder.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 177 | shooter_right_encoder_ = ::std::move(encoder); |
| 178 | } |
| 179 | |
| 180 | // Intake setters. |
| 181 | void set_intake_encoder(::std::unique_ptr<Encoder> encoder) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 182 | medium_encoder_filter_.Add(encoder.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 183 | intake_encoder_.set_encoder(::std::move(encoder)); |
| 184 | } |
| 185 | |
| 186 | void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 187 | intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 188 | } |
| 189 | |
| 190 | void set_intake_index(::std::unique_ptr<DigitalInput> index) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 191 | medium_encoder_filter_.Add(index.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 192 | intake_encoder_.set_index(::std::move(index)); |
| 193 | } |
| 194 | |
| 195 | // Shoulder setters. |
| 196 | void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 197 | medium_encoder_filter_.Add(encoder.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 198 | shoulder_encoder_.set_encoder(::std::move(encoder)); |
| 199 | } |
| 200 | |
| 201 | void set_shoulder_potentiometer( |
| 202 | ::std::unique_ptr<AnalogInput> potentiometer) { |
| 203 | shoulder_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 204 | } |
| 205 | |
| 206 | void set_shoulder_index(::std::unique_ptr<DigitalInput> index) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 207 | medium_encoder_filter_.Add(index.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 208 | shoulder_encoder_.set_index(::std::move(index)); |
| 209 | } |
| 210 | |
| 211 | // Wrist setters. |
| 212 | void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 213 | medium_encoder_filter_.Add(encoder.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 214 | wrist_encoder_.set_encoder(::std::move(encoder)); |
| 215 | } |
| 216 | |
| 217 | void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 218 | wrist_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 219 | } |
| 220 | |
| 221 | void set_wrist_index(::std::unique_ptr<DigitalInput> index) { |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 222 | medium_encoder_filter_.Add(index.get()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 223 | wrist_encoder_.set_index(::std::move(index)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 226 | // Ball detector setter. |
| 227 | void set_ball_detector(::std::unique_ptr<AnalogInput> analog) { |
| 228 | ball_detector_ = ::std::move(analog); |
| 229 | } |
| 230 | |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 231 | // Autonomous mode switch setter. |
| 232 | void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) { |
| 233 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 234 | } |
| 235 | |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 236 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 237 | // All of the DMA-related set_* calls must be made before this, and it doesn't |
| 238 | // hurt to do all of them. |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 239 | |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 240 | void Start() { |
| 241 | AddToDMA(&intake_encoder_); |
| 242 | AddToDMA(&shoulder_encoder_); |
| 243 | AddToDMA(&wrist_encoder_); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void RunIteration() { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 247 | { |
| 248 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 249 | drivetrain_message->right_encoder = |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 250 | drivetrain_translate(-drivetrain_right_encoder_->GetRaw()); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 251 | drivetrain_message->left_encoder = |
| 252 | -drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 253 | drivetrain_message->left_speed = |
| 254 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 255 | drivetrain_message->right_speed = |
| 256 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 257 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 258 | drivetrain_message->left_shifter_position = |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 259 | hall_translate(drivetrain_left_hall_->GetVoltage()); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 260 | drivetrain_message->right_shifter_position = |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 261 | hall_translate(drivetrain_right_hall_->GetVoltage()); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 262 | |
| 263 | drivetrain_message.Send(); |
| 264 | } |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 265 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 266 | |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 267 | void RunDmaIteration() { |
| 268 | const auto &values = constants::GetValues(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 269 | { |
| 270 | auto shooter_message = shooter_queue.position.MakeMessage(); |
| 271 | shooter_message->theta_left = |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 272 | shooter_translate(-shooter_left_encoder_->GetRaw()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 273 | shooter_message->theta_right = |
| 274 | shooter_translate(shooter_right_encoder_->GetRaw()); |
| 275 | shooter_message.Send(); |
| 276 | } |
| 277 | |
| 278 | { |
| 279 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 280 | CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake, |
| 281 | intake_translate, intake_pot_translate, false, |
| 282 | values.intake.pot_offset); |
| 283 | CopyPotAndIndexPosition(shoulder_encoder_, |
| 284 | &superstructure_message->shoulder, |
| 285 | shoulder_translate, shoulder_pot_translate, false, |
| 286 | values.shoulder.pot_offset); |
| 287 | CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist, |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 288 | wrist_translate, wrist_pot_translate, true, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 289 | values.wrist.pot_offset); |
| 290 | |
| 291 | superstructure_message.Send(); |
| 292 | } |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 293 | |
| 294 | { |
| 295 | auto ball_detector_message = |
| 296 | ::y2016::sensors::ball_detector.MakeMessage(); |
| 297 | ball_detector_message->voltage = ball_detector_->GetVoltage(); |
| 298 | LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message); |
| 299 | ball_detector_message.Send(); |
| 300 | } |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 301 | |
| 302 | { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 303 | auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage(); |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 304 | auto_mode_message->mode = 0; |
| 305 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 306 | if (autonomous_modes_[i]->Get()) { |
| 307 | auto_mode_message->mode |= 1 << i; |
| 308 | } |
| 309 | } |
| 310 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 311 | auto_mode_message.Send(); |
| 312 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 315 | private: |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 316 | void CopyPotAndIndexPosition( |
| 317 | const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 318 | ::frc971::PotAndIndexPosition *position, |
| 319 | ::std::function<double(int32_t)> encoder_translate, |
| 320 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 321 | double pot_offset) { |
| 322 | const double multiplier = reverse ? -1.0 : 1.0; |
| 323 | position->encoder = |
| 324 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 325 | position->pot = multiplier * potentiometer_translate( |
| 326 | encoder.polled_potentiometer_voltage()) + |
| 327 | pot_offset; |
| 328 | position->latched_encoder = |
| 329 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 330 | position->latched_pot = |
| 331 | multiplier * |
| 332 | potentiometer_translate(encoder.last_potentiometer_voltage()) + |
| 333 | pot_offset; |
| 334 | position->index_pulses = encoder.index_posedge_count(); |
| 335 | } |
| 336 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 337 | ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_; |
| 338 | |
| 339 | ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_; |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 340 | ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_, |
| 341 | shoulder_encoder_, wrist_encoder_; |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 342 | ::std::unique_ptr<AnalogInput> ball_detector_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 343 | |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 344 | ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_; |
| 345 | |
Austin Schuh | 54667ac | 2019-02-02 16:44:49 -0800 | [diff] [blame] | 346 | DigitalGlitchFilter hall_filter_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | class SolenoidWriter { |
| 350 | public: |
| 351 | SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
| 352 | : pcm_(pcm), |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 353 | drivetrain_(".frc971.control_loops.drivetrain_queue.output"), |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 354 | shooter_(".y2016.control_loops.shooter.shooter_queue.output"), |
| 355 | superstructure_( |
| 356 | ".y2016.control_loops.superstructure_queue.output") { |
| 357 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 358 | |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 359 | void set_compressor(::std::unique_ptr<Compressor> compressor) { |
| 360 | compressor_ = ::std::move(compressor); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 363 | void set_drivetrain_shifter( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 364 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 365 | drivetrain_shifter_ = ::std::move(s); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 368 | void set_climber_trigger( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 369 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 370 | climber_trigger_ = ::std::move(s); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 373 | void set_traverse( |
| 374 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 375 | traverse_ = ::std::move(s); |
| 376 | } |
| 377 | |
| 378 | void set_traverse_latch( |
| 379 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 380 | traverse_latch_ = ::std::move(s); |
| 381 | } |
| 382 | |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 383 | void set_shooter_clamp( |
| 384 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 385 | shooter_clamp_ = ::std::move(s); |
| 386 | } |
| 387 | |
| 388 | void set_shooter_pusher( |
| 389 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 390 | shooter_pusher_ = ::std::move(s); |
| 391 | } |
| 392 | |
Austin Schuh | e0729a6 | 2016-03-12 21:54:17 -0800 | [diff] [blame] | 393 | void set_lights( |
| 394 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 395 | lights_ = ::std::move(s); |
| 396 | } |
| 397 | |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 398 | void set_flashlight(::std::unique_ptr<Relay> relay) { |
| 399 | flashlight_ = ::std::move(relay); |
| 400 | } |
| 401 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 402 | void operator()() { |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 403 | compressor_->Start(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 404 | ::aos::SetCurrentThreadName("Solenoids"); |
| 405 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 406 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 407 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 408 | ::std::chrono::milliseconds(1)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 409 | |
| 410 | while (run_) { |
| 411 | { |
| 412 | const int iterations = phased_loop.SleepUntilNext(); |
| 413 | if (iterations != 1) { |
| 414 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 419 | drivetrain_.FetchLatest(); |
| 420 | if (drivetrain_.get()) { |
| 421 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 422 | drivetrain_shifter_->Set( |
| 423 | !(drivetrain_->left_high || drivetrain_->right_high)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | { |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 428 | shooter_.FetchLatest(); |
| 429 | if (shooter_.get()) { |
| 430 | LOG_STRUCT(DEBUG, "solenoids", *shooter_); |
| 431 | shooter_clamp_->Set(shooter_->clamp_open); |
| 432 | shooter_pusher_->Set(shooter_->push_to_shooter); |
Austin Schuh | e0729a6 | 2016-03-12 21:54:17 -0800 | [diff] [blame] | 433 | lights_->Set(shooter_->lights_on); |
Austin Schuh | b2c3338 | 2016-04-03 16:09:17 -0700 | [diff] [blame] | 434 | if (shooter_->forwards_flashlight) { |
| 435 | if (shooter_->backwards_flashlight) { |
| 436 | flashlight_->Set(Relay::kOn); |
| 437 | } else { |
| 438 | flashlight_->Set(Relay::kReverse); |
| 439 | } |
| 440 | } else { |
| 441 | if (shooter_->backwards_flashlight) { |
| 442 | flashlight_->Set(Relay::kForward); |
| 443 | } else { |
| 444 | flashlight_->Set(Relay::kOff); |
| 445 | } |
| 446 | } |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | { |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 451 | superstructure_.FetchLatest(); |
| 452 | if (superstructure_.get()) { |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 453 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 454 | |
| 455 | climber_trigger_->Set(superstructure_->unfold_climber); |
| 456 | |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 457 | traverse_->Set(superstructure_->traverse_down); |
| 458 | traverse_latch_->Set(superstructure_->traverse_unlatched); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 463 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 464 | { |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 465 | to_log.compressor_on = compressor_->Enabled(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | pcm_->Flush(); |
| 469 | to_log.read_solenoids = pcm_->GetAll(); |
| 470 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void Quit() { run_ = false; } |
| 476 | |
| 477 | private: |
| 478 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_; |
| 479 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 480 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_, |
| 481 | shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_, |
| 482 | climber_trigger_; |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 483 | ::std::unique_ptr<Relay> flashlight_; |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 484 | ::std::unique_ptr<Compressor> compressor_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 485 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 486 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
Comran Morshed | 3263e8f | 2016-02-14 17:55:45 +0000 | [diff] [blame] | 487 | ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_; |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 488 | ::aos::Queue< |
| 489 | ::y2016::control_loops::SuperstructureQueue::Output> |
| 490 | superstructure_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 491 | |
| 492 | ::std::atomic<bool> run_{true}; |
| 493 | }; |
| 494 | |
| 495 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 496 | public: |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 497 | void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) { |
| 498 | drivetrain_left_talon_ = ::std::move(t); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 501 | void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) { |
| 502 | drivetrain_right_talon_ = ::std::move(t); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | private: |
| 506 | virtual void Read() override { |
| 507 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 508 | } |
| 509 | |
| 510 | virtual void Write() override { |
| 511 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 512 | LOG_STRUCT(DEBUG, "will output", *queue); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 513 | drivetrain_left_talon_->SetSpeed(queue->left_voltage / 12.0); |
| 514 | drivetrain_right_talon_->SetSpeed(-queue->right_voltage / 12.0); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | virtual void Stop() override { |
| 518 | LOG(WARNING, "drivetrain output too old\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 519 | drivetrain_left_talon_->SetDisabled(); |
| 520 | drivetrain_right_talon_->SetDisabled(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 523 | ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_; |
| 524 | }; |
| 525 | |
| 526 | class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 527 | public: |
| 528 | void set_shooter_left_talon(::std::unique_ptr<Talon> t) { |
| 529 | shooter_left_talon_ = ::std::move(t); |
| 530 | } |
| 531 | |
| 532 | void set_shooter_right_talon(::std::unique_ptr<Talon> t) { |
| 533 | shooter_right_talon_ = ::std::move(t); |
| 534 | } |
| 535 | |
| 536 | private: |
| 537 | virtual void Read() override { |
| 538 | ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother(); |
| 539 | } |
| 540 | |
| 541 | virtual void Write() override { |
| 542 | auto &queue = ::y2016::control_loops::shooter::shooter_queue.output; |
| 543 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 544 | |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 545 | shooter_left_talon_->SetSpeed(queue->voltage_left / 12.0); |
| 546 | shooter_right_talon_->SetSpeed(-queue->voltage_right / 12.0); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | virtual void Stop() override { |
| 550 | LOG(WARNING, "Shooter output too old.\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 551 | shooter_left_talon_->SetDisabled(); |
| 552 | shooter_right_talon_->SetDisabled(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_; |
| 556 | }; |
| 557 | |
| 558 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 559 | public: |
| 560 | void set_intake_talon(::std::unique_ptr<Talon> t) { |
| 561 | intake_talon_ = ::std::move(t); |
| 562 | } |
| 563 | |
| 564 | void set_shoulder_talon(::std::unique_ptr<Talon> t) { |
| 565 | shoulder_talon_ = ::std::move(t); |
| 566 | } |
| 567 | |
| 568 | void set_wrist_talon(::std::unique_ptr<Talon> t) { |
| 569 | wrist_talon_ = ::std::move(t); |
| 570 | } |
| 571 | |
Campbell Crowley | d4fd655 | 2016-02-21 17:53:46 -0800 | [diff] [blame] | 572 | void set_top_rollers_talon(::std::unique_ptr<Talon> t) { |
| 573 | top_rollers_talon_ = ::std::move(t); |
| 574 | } |
| 575 | |
| 576 | void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) { |
| 577 | bottom_rollers_talon_ = ::std::move(t); |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 580 | void set_climber_talon(::std::unique_ptr<Talon> t) { |
| 581 | climber_talon_ = ::std::move(t); |
| 582 | } |
| 583 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 584 | private: |
| 585 | virtual void Read() override { |
| 586 | ::y2016::control_loops::superstructure_queue.output.FetchAnother(); |
| 587 | } |
| 588 | |
| 589 | virtual void Write() override { |
| 590 | auto &queue = ::y2016::control_loops::superstructure_queue.output; |
| 591 | LOG_STRUCT(DEBUG, "will output", *queue); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 592 | intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake, |
| 593 | -kMaxBringupPower, kMaxBringupPower) / |
| 594 | 12.0); |
| 595 | shoulder_talon_->SetSpeed(::aos::Clip(-queue->voltage_shoulder, |
| 596 | -kMaxBringupPower, kMaxBringupPower) / |
| 597 | 12.0); |
| 598 | wrist_talon_->SetSpeed( |
Austin Schuh | a9992ff | 2016-02-28 21:59:23 -0800 | [diff] [blame] | 599 | ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) / |
| 600 | 12.0); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 601 | top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0); |
| 602 | bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0); |
| 603 | climber_talon_->SetSpeed(-queue->voltage_climber / 12.0); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | virtual void Stop() override { |
| 607 | LOG(WARNING, "Superstructure output too old.\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 608 | intake_talon_->SetDisabled(); |
| 609 | shoulder_talon_->SetDisabled(); |
| 610 | wrist_talon_->SetDisabled(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 613 | ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_, |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 614 | top_rollers_talon_, bottom_rollers_talon_, climber_talon_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 615 | }; |
| 616 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 617 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 618 | public: |
| 619 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 620 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 621 | Encoder::k4X); |
| 622 | } |
| 623 | |
| 624 | void Run() override { |
| 625 | ::aos::InitNRT(); |
| 626 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 627 | |
| 628 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 629 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 630 | |
| 631 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 632 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 633 | SensorReader reader; |
| 634 | |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 635 | reader.set_drivetrain_left_encoder(make_encoder(5)); |
| 636 | reader.set_drivetrain_right_encoder(make_encoder(6)); |
| 637 | reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5)); |
| 638 | reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 639 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 640 | reader.set_shooter_left_encoder(make_encoder(7)); |
| 641 | reader.set_shooter_right_encoder(make_encoder(-3)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 642 | |
| 643 | reader.set_intake_encoder(make_encoder(0)); |
| 644 | reader.set_intake_index(make_unique<DigitalInput>(0)); |
| 645 | reader.set_intake_potentiometer(make_unique<AnalogInput>(0)); |
| 646 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 647 | reader.set_shoulder_encoder(make_encoder(4)); |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 648 | reader.set_shoulder_index(make_unique<DigitalInput>(2)); |
| 649 | reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 650 | |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 651 | reader.set_wrist_encoder(make_encoder(1)); |
| 652 | reader.set_wrist_index(make_unique<DigitalInput>(1)); |
| 653 | reader.set_wrist_potentiometer(make_unique<AnalogInput>(1)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 654 | |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 655 | reader.set_ball_detector(make_unique<AnalogInput>(7)); |
| 656 | |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 657 | reader.set_autonomous_mode(0, make_unique<DigitalInput>(9)); |
| 658 | reader.set_autonomous_mode(1, make_unique<DigitalInput>(8)); |
| 659 | reader.set_autonomous_mode(2, make_unique<DigitalInput>(7)); |
| 660 | reader.set_autonomous_mode(3, make_unique<DigitalInput>(6)); |
| 661 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 662 | reader.set_dma(make_unique<DMA>()); |
| 663 | ::std::thread reader_thread(::std::ref(reader)); |
| 664 | |
Brian Silverman | 003a473 | 2018-03-11 14:02:15 -0700 | [diff] [blame] | 665 | // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448. |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 666 | ::frc971::wpilib::GyroSender gyro_sender; |
| 667 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 668 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 669 | auto imu_trigger = make_unique<DigitalInput>(3); |
| 670 | ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get()); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 671 | ::std::thread imu_thread(::std::ref(imu)); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 672 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 673 | DrivetrainWriter drivetrain_writer; |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 674 | drivetrain_writer.set_drivetrain_left_talon( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 675 | ::std::unique_ptr<Talon>(new Talon(5))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 676 | drivetrain_writer.set_drivetrain_right_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 677 | ::std::unique_ptr<Talon>(new Talon(4))); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 678 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 679 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 680 | ShooterWriter shooter_writer; |
| 681 | shooter_writer.set_shooter_left_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 682 | ::std::unique_ptr<Talon>(new Talon(9))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 683 | shooter_writer.set_shooter_right_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 684 | ::std::unique_ptr<Talon>(new Talon(8))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 685 | ::std::thread shooter_writer_thread(::std::ref(shooter_writer)); |
| 686 | |
| 687 | SuperstructureWriter superstructure_writer; |
| 688 | superstructure_writer.set_intake_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 689 | ::std::unique_ptr<Talon>(new Talon(3))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 690 | superstructure_writer.set_shoulder_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 691 | ::std::unique_ptr<Talon>(new Talon(6))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 692 | superstructure_writer.set_wrist_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 693 | ::std::unique_ptr<Talon>(new Talon(2))); |
Campbell Crowley | d4fd655 | 2016-02-21 17:53:46 -0800 | [diff] [blame] | 694 | superstructure_writer.set_top_rollers_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 695 | ::std::unique_ptr<Talon>(new Talon(1))); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 696 | superstructure_writer.set_bottom_rollers_talon( |
| 697 | ::std::unique_ptr<Talon>(new Talon(0))); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 698 | superstructure_writer.set_climber_talon( |
| 699 | ::std::unique_ptr<Talon>(new Talon(7))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 700 | ::std::thread superstructure_writer_thread( |
| 701 | ::std::ref(superstructure_writer)); |
| 702 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 703 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 704 | new ::frc971::wpilib::BufferedPcm()); |
| 705 | SolenoidWriter solenoid_writer(pcm); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 706 | solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0)); |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 707 | solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2)); |
| 708 | solenoid_writer.set_traverse(pcm->MakeSolenoid(3)); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 709 | solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4)); |
| 710 | solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5)); |
Austin Schuh | e0729a6 | 2016-03-12 21:54:17 -0800 | [diff] [blame] | 711 | solenoid_writer.set_lights(pcm->MakeSolenoid(6)); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 712 | solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1)); |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 713 | solenoid_writer.set_flashlight(make_unique<Relay>(0)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 714 | |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 715 | solenoid_writer.set_compressor(make_unique<Compressor>()); |
| 716 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 717 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 718 | |
| 719 | // Wait forever. Not much else to do... |
| 720 | while (true) { |
| 721 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 722 | if (r != 0) { |
| 723 | PLOG(WARNING, "infinite select failed"); |
| 724 | } else { |
| 725 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 730 | |
| 731 | joystick_sender.Quit(); |
| 732 | joystick_thread.join(); |
| 733 | pdp_fetcher.Quit(); |
| 734 | pdp_fetcher_thread.join(); |
| 735 | reader.Quit(); |
| 736 | reader_thread.join(); |
| 737 | gyro_sender.Quit(); |
| 738 | gyro_thread.join(); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 739 | imu.Quit(); |
| 740 | imu_thread.join(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 741 | |
| 742 | drivetrain_writer.Quit(); |
| 743 | drivetrain_writer_thread.join(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 744 | shooter_writer.Quit(); |
| 745 | shooter_writer_thread.join(); |
| 746 | superstructure_writer.Quit(); |
| 747 | superstructure_writer_thread.join(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 748 | solenoid_writer.Quit(); |
| 749 | solenoid_thread.join(); |
| 750 | |
| 751 | ::aos::Cleanup(); |
| 752 | } |
| 753 | }; |
| 754 | |
| 755 | } // namespace wpilib |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 756 | } // namespace y2016 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 757 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 758 | AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot); |