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