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