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