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