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