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