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