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 | |
| 431 | void set_drivetrain_left( |
| 432 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 433 | drivetrain_left_ = ::std::move(s); |
| 434 | } |
| 435 | |
| 436 | void set_drivetrain_right( |
| 437 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 438 | drivetrain_right_ = ::std::move(s); |
| 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_); |
| 490 | drivetrain_left_->Set(!drivetrain_->left_high); |
| 491 | drivetrain_right_->Set(!drivetrain_->right_high); |
| 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 | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 502 | flashlight_->Set(shooter_->lights_on ? Relay::kForward : Relay::kOff); |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | { |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 507 | superstructure_.FetchLatest(); |
| 508 | if (superstructure_.get()) { |
| 509 | LOG_STRUCT(DEBUG, "solenoids", *shooter_); |
| 510 | traverse_->Set(superstructure_->traverse_down); |
| 511 | traverse_latch_->Set(superstructure_->traverse_unlatched); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 516 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 517 | { |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 518 | to_log.compressor_on = compressor_->Enabled(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | pcm_->Flush(); |
| 522 | to_log.read_solenoids = pcm_->GetAll(); |
| 523 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void Quit() { run_ = false; } |
| 529 | |
| 530 | private: |
| 531 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_; |
| 532 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 533 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_left_, |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 534 | drivetrain_right_, shooter_clamp_, shooter_pusher_, lights_, traverse_, |
| 535 | traverse_latch_; |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 536 | ::std::unique_ptr<Relay> flashlight_; |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 537 | ::std::unique_ptr<Compressor> compressor_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 538 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 539 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
Comran Morshed | 3263e8f | 2016-02-14 17:55:45 +0000 | [diff] [blame] | 540 | ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_; |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 541 | ::aos::Queue< |
| 542 | ::y2016::control_loops::SuperstructureQueue::Output> |
| 543 | superstructure_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 544 | |
| 545 | ::std::atomic<bool> run_{true}; |
| 546 | }; |
| 547 | |
| 548 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 549 | public: |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 550 | void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) { |
| 551 | drivetrain_left_talon_ = ::std::move(t); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 554 | void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) { |
| 555 | drivetrain_right_talon_ = ::std::move(t); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | private: |
| 559 | virtual void Read() override { |
| 560 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 561 | } |
| 562 | |
| 563 | virtual void Write() override { |
| 564 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 565 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 566 | drivetrain_left_talon_->Set(queue->left_voltage / 12.0); |
| 567 | drivetrain_right_talon_->Set(-queue->right_voltage / 12.0); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | virtual void Stop() override { |
| 571 | LOG(WARNING, "drivetrain output too old\n"); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 572 | drivetrain_left_talon_->Disable(); |
| 573 | drivetrain_right_talon_->Disable(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 576 | ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_; |
| 577 | }; |
| 578 | |
| 579 | class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 580 | public: |
| 581 | void set_shooter_left_talon(::std::unique_ptr<Talon> t) { |
| 582 | shooter_left_talon_ = ::std::move(t); |
| 583 | } |
| 584 | |
| 585 | void set_shooter_right_talon(::std::unique_ptr<Talon> t) { |
| 586 | shooter_right_talon_ = ::std::move(t); |
| 587 | } |
| 588 | |
| 589 | private: |
| 590 | virtual void Read() override { |
| 591 | ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother(); |
| 592 | } |
| 593 | |
| 594 | virtual void Write() override { |
| 595 | auto &queue = ::y2016::control_loops::shooter::shooter_queue.output; |
| 596 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 597 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 598 | shooter_left_talon_->Set(queue->voltage_left / 12.0); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 599 | shooter_right_talon_->Set(-queue->voltage_right / 12.0); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | virtual void Stop() override { |
| 603 | LOG(WARNING, "Shooter output too old.\n"); |
| 604 | shooter_left_talon_->Disable(); |
| 605 | shooter_right_talon_->Disable(); |
| 606 | } |
| 607 | |
| 608 | ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_; |
| 609 | }; |
| 610 | |
| 611 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 612 | public: |
| 613 | void set_intake_talon(::std::unique_ptr<Talon> t) { |
| 614 | intake_talon_ = ::std::move(t); |
| 615 | } |
| 616 | |
| 617 | void set_shoulder_talon(::std::unique_ptr<Talon> t) { |
| 618 | shoulder_talon_ = ::std::move(t); |
| 619 | } |
| 620 | |
| 621 | void set_wrist_talon(::std::unique_ptr<Talon> t) { |
| 622 | wrist_talon_ = ::std::move(t); |
| 623 | } |
| 624 | |
Campbell Crowley | d4fd655 | 2016-02-21 17:53:46 -0800 | [diff] [blame] | 625 | void set_top_rollers_talon(::std::unique_ptr<Talon> t) { |
| 626 | top_rollers_talon_ = ::std::move(t); |
| 627 | } |
| 628 | |
| 629 | void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) { |
| 630 | bottom_rollers_talon_ = ::std::move(t); |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 633 | private: |
| 634 | virtual void Read() override { |
| 635 | ::y2016::control_loops::superstructure_queue.output.FetchAnother(); |
| 636 | } |
| 637 | |
| 638 | virtual void Write() override { |
| 639 | auto &queue = ::y2016::control_loops::superstructure_queue.output; |
| 640 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | a9992ff | 2016-02-28 21:59:23 -0800 | [diff] [blame] | 641 | intake_talon_->Set(::aos::Clip(queue->voltage_intake, -kMaxBringupPower, |
| 642 | kMaxBringupPower) / |
| 643 | 12.0); |
| 644 | shoulder_talon_->Set(::aos::Clip(-queue->voltage_shoulder, |
| 645 | -kMaxBringupPower, kMaxBringupPower) / |
| 646 | 12.0); |
| 647 | wrist_talon_->Set( |
| 648 | ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) / |
| 649 | 12.0); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 650 | top_rollers_talon_->Set(-queue->voltage_top_rollers / 12.0); |
| 651 | bottom_rollers_talon_->Set(-queue->voltage_bottom_rollers / 12.0); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | virtual void Stop() override { |
| 655 | LOG(WARNING, "Superstructure output too old.\n"); |
| 656 | intake_talon_->Disable(); |
| 657 | shoulder_talon_->Disable(); |
| 658 | wrist_talon_->Disable(); |
| 659 | } |
| 660 | |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 661 | ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_, |
Campbell Crowley | d4fd655 | 2016-02-21 17:53:46 -0800 | [diff] [blame] | 662 | top_rollers_talon_, bottom_rollers_talon_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 663 | }; |
| 664 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 665 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 666 | public: |
| 667 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 668 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 669 | Encoder::k4X); |
| 670 | } |
| 671 | |
| 672 | void Run() override { |
| 673 | ::aos::InitNRT(); |
| 674 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 675 | |
| 676 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 677 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 678 | |
| 679 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 680 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 681 | SensorReader reader; |
| 682 | |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 683 | reader.set_drivetrain_left_encoder(make_encoder(5)); |
| 684 | reader.set_drivetrain_right_encoder(make_encoder(6)); |
| 685 | reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5)); |
| 686 | reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 687 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame^] | 688 | reader.set_shooter_left_encoder(make_encoder(7)); |
| 689 | reader.set_shooter_right_encoder(make_encoder(-3)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 690 | |
| 691 | reader.set_intake_encoder(make_encoder(0)); |
| 692 | reader.set_intake_index(make_unique<DigitalInput>(0)); |
| 693 | reader.set_intake_potentiometer(make_unique<AnalogInput>(0)); |
| 694 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame^] | 695 | reader.set_shoulder_encoder(make_encoder(4)); |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 696 | reader.set_shoulder_index(make_unique<DigitalInput>(2)); |
| 697 | reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 698 | |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 699 | reader.set_wrist_encoder(make_encoder(1)); |
| 700 | reader.set_wrist_index(make_unique<DigitalInput>(1)); |
| 701 | reader.set_wrist_potentiometer(make_unique<AnalogInput>(1)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 702 | |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 703 | reader.set_ball_detector(make_unique<AnalogInput>(7)); |
| 704 | |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 705 | reader.set_autonomous_mode(0, make_unique<DigitalInput>(9)); |
| 706 | reader.set_autonomous_mode(1, make_unique<DigitalInput>(8)); |
| 707 | reader.set_autonomous_mode(2, make_unique<DigitalInput>(7)); |
| 708 | reader.set_autonomous_mode(3, make_unique<DigitalInput>(6)); |
| 709 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 710 | reader.set_dma(make_unique<DMA>()); |
| 711 | ::std::thread reader_thread(::std::ref(reader)); |
| 712 | |
| 713 | ::frc971::wpilib::GyroSender gyro_sender; |
| 714 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 715 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame^] | 716 | auto imu_trigger = make_unique<DigitalInput>(3); |
| 717 | ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get()); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 718 | ::std::thread imu_thread(::std::ref(imu)); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 719 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 720 | DrivetrainWriter drivetrain_writer; |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 721 | drivetrain_writer.set_drivetrain_left_talon( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 722 | ::std::unique_ptr<Talon>(new Talon(5))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 723 | drivetrain_writer.set_drivetrain_right_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 724 | ::std::unique_ptr<Talon>(new Talon(4))); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 725 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 726 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 727 | ShooterWriter shooter_writer; |
| 728 | shooter_writer.set_shooter_left_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 729 | ::std::unique_ptr<Talon>(new Talon(9))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 730 | shooter_writer.set_shooter_right_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 731 | ::std::unique_ptr<Talon>(new Talon(8))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 732 | ::std::thread shooter_writer_thread(::std::ref(shooter_writer)); |
| 733 | |
| 734 | SuperstructureWriter superstructure_writer; |
| 735 | superstructure_writer.set_intake_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 736 | ::std::unique_ptr<Talon>(new Talon(3))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 737 | superstructure_writer.set_shoulder_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 738 | ::std::unique_ptr<Talon>(new Talon(6))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 739 | superstructure_writer.set_wrist_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 740 | ::std::unique_ptr<Talon>(new Talon(2))); |
Campbell Crowley | d4fd655 | 2016-02-21 17:53:46 -0800 | [diff] [blame] | 741 | superstructure_writer.set_top_rollers_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 742 | ::std::unique_ptr<Talon>(new Talon(1))); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 743 | superstructure_writer.set_bottom_rollers_talon( |
| 744 | ::std::unique_ptr<Talon>(new Talon(0))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 745 | ::std::thread superstructure_writer_thread( |
| 746 | ::std::ref(superstructure_writer)); |
| 747 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 748 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 749 | new ::frc971::wpilib::BufferedPcm()); |
| 750 | SolenoidWriter solenoid_writer(pcm); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 751 | solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(1)); |
| 752 | solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(0)); |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 753 | solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2)); |
| 754 | solenoid_writer.set_traverse(pcm->MakeSolenoid(3)); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 755 | solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4)); |
| 756 | solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5)); |
Austin Schuh | e0729a6 | 2016-03-12 21:54:17 -0800 | [diff] [blame] | 757 | solenoid_writer.set_lights(pcm->MakeSolenoid(6)); |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 758 | solenoid_writer.set_flashlight(make_unique<Relay>(0)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 759 | |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 760 | solenoid_writer.set_compressor(make_unique<Compressor>()); |
| 761 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 762 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 763 | |
| 764 | // Wait forever. Not much else to do... |
| 765 | while (true) { |
| 766 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 767 | if (r != 0) { |
| 768 | PLOG(WARNING, "infinite select failed"); |
| 769 | } else { |
| 770 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 775 | |
| 776 | joystick_sender.Quit(); |
| 777 | joystick_thread.join(); |
| 778 | pdp_fetcher.Quit(); |
| 779 | pdp_fetcher_thread.join(); |
| 780 | reader.Quit(); |
| 781 | reader_thread.join(); |
| 782 | gyro_sender.Quit(); |
| 783 | gyro_thread.join(); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 784 | imu.Quit(); |
| 785 | imu_thread.join(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 786 | |
| 787 | drivetrain_writer.Quit(); |
| 788 | drivetrain_writer_thread.join(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 789 | shooter_writer.Quit(); |
| 790 | shooter_writer_thread.join(); |
| 791 | superstructure_writer.Quit(); |
| 792 | superstructure_writer_thread.join(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 793 | solenoid_writer.Quit(); |
| 794 | solenoid_thread.join(); |
| 795 | |
| 796 | ::aos::Cleanup(); |
| 797 | } |
| 798 | }; |
| 799 | |
| 800 | } // namespace wpilib |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 801 | } // namespace y2016 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 802 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 803 | AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot); |