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