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 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 13 | #include "frc971/wpilib/ahal/Compressor.h" |
| 14 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 15 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 16 | #include "frc971/wpilib/ahal/Encoder.h" |
| 17 | #include "frc971/wpilib/ahal/Relay.h" |
| 18 | #include "frc971/wpilib/ahal/Talon.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 19 | #include "frc971/wpilib/wpilib_robot_base.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 20 | #undef ERROR |
| 21 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 22 | #include "aos/logging/logging.h" |
| 23 | #include "aos/logging/queue_logging.h" |
| 24 | #include "aos/time/time.h" |
| 25 | #include "aos/util/log_interval.h" |
| 26 | #include "aos/util/phased_loop.h" |
| 27 | #include "aos/util/wrapping_counter.h" |
| 28 | #include "aos/stl_mutex/stl_mutex.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 29 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 30 | #include "aos/robot_state/robot_state.q.h" |
| 31 | #include "aos/commonmath.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 32 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 33 | #include "frc971/autonomous/auto.q.h" |
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" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 42 | |
| 43 | #include "frc971/wpilib/joystick_sender.h" |
| 44 | #include "frc971/wpilib/loop_output_handler.h" |
| 45 | #include "frc971/wpilib/buffered_solenoid.h" |
| 46 | #include "frc971/wpilib/buffered_pcm.h" |
| 47 | #include "frc971/wpilib/gyro_sender.h" |
| 48 | #include "frc971/wpilib/dma_edge_counting.h" |
| 49 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 50 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 51 | #include "frc971/wpilib/logging.q.h" |
| 52 | #include "frc971/wpilib/wpilib_interface.h" |
| 53 | #include "frc971/wpilib/pdp_fetcher.h" |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 54 | #include "frc971/wpilib/ADIS16448.h" |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 55 | #include "frc971/wpilib/dma.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 56 | |
| 57 | #ifndef M_PI |
| 58 | #define M_PI 3.14159265358979323846 |
| 59 | #endif |
| 60 | |
| 61 | using ::frc971::control_loops::drivetrain_queue; |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 62 | using ::y2016::control_loops::shooter::shooter_queue; |
| 63 | using ::y2016::control_loops::superstructure_queue; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 64 | using namespace frc; |
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(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 276 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 277 | dma_synchronizer_->Start(); |
| 278 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 279 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 280 | ::std::chrono::milliseconds(4)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 281 | |
| 282 | ::aos::SetCurrentThreadRealtimePriority(40); |
| 283 | while (run_) { |
| 284 | { |
| 285 | const int iterations = phased_loop.SleepUntilNext(); |
| 286 | if (iterations != 1) { |
| 287 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 288 | } |
| 289 | } |
| 290 | RunIteration(); |
| 291 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void RunIteration() { |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame] | 295 | ::frc971::wpilib::SendRobotState(my_pid_); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 296 | |
| 297 | const auto &values = constants::GetValues(); |
| 298 | |
| 299 | { |
| 300 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 301 | drivetrain_message->right_encoder = |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 302 | drivetrain_translate(-drivetrain_right_encoder_->GetRaw()); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 303 | drivetrain_message->left_encoder = |
| 304 | -drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 305 | drivetrain_message->left_speed = |
| 306 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 307 | drivetrain_message->right_speed = |
| 308 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 309 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 310 | drivetrain_message->left_shifter_position = |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 311 | hall_translate(drivetrain_left_hall_->GetVoltage()); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 312 | drivetrain_message->right_shifter_position = |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 313 | hall_translate(drivetrain_right_hall_->GetVoltage()); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 314 | |
| 315 | drivetrain_message.Send(); |
| 316 | } |
| 317 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 318 | dma_synchronizer_->RunIteration(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 319 | |
| 320 | { |
| 321 | auto shooter_message = shooter_queue.position.MakeMessage(); |
| 322 | shooter_message->theta_left = |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 323 | shooter_translate(-shooter_left_encoder_->GetRaw()); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 324 | shooter_message->theta_right = |
| 325 | shooter_translate(shooter_right_encoder_->GetRaw()); |
| 326 | shooter_message.Send(); |
| 327 | } |
| 328 | |
| 329 | { |
| 330 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 331 | CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake, |
| 332 | intake_translate, intake_pot_translate, false, |
| 333 | values.intake.pot_offset); |
| 334 | CopyPotAndIndexPosition(shoulder_encoder_, |
| 335 | &superstructure_message->shoulder, |
| 336 | shoulder_translate, shoulder_pot_translate, false, |
| 337 | values.shoulder.pot_offset); |
| 338 | CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist, |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 339 | wrist_translate, wrist_pot_translate, true, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 340 | values.wrist.pot_offset); |
| 341 | |
| 342 | superstructure_message.Send(); |
| 343 | } |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 344 | |
| 345 | { |
| 346 | auto ball_detector_message = |
| 347 | ::y2016::sensors::ball_detector.MakeMessage(); |
| 348 | ball_detector_message->voltage = ball_detector_->GetVoltage(); |
| 349 | LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message); |
| 350 | ball_detector_message.Send(); |
| 351 | } |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 352 | |
| 353 | { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 354 | auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage(); |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 355 | auto_mode_message->mode = 0; |
| 356 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 357 | if (autonomous_modes_[i]->Get()) { |
| 358 | auto_mode_message->mode |= 1 << i; |
| 359 | } |
| 360 | } |
| 361 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 362 | auto_mode_message.Send(); |
| 363 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void Quit() { run_ = false; } |
| 367 | |
| 368 | private: |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 369 | void CopyPotAndIndexPosition( |
| 370 | const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 371 | ::frc971::PotAndIndexPosition *position, |
| 372 | ::std::function<double(int32_t)> encoder_translate, |
| 373 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 374 | double pot_offset) { |
| 375 | const double multiplier = reverse ? -1.0 : 1.0; |
| 376 | position->encoder = |
| 377 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 378 | position->pot = multiplier * potentiometer_translate( |
| 379 | encoder.polled_potentiometer_voltage()) + |
| 380 | pot_offset; |
| 381 | position->latched_encoder = |
| 382 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 383 | position->latched_pot = |
| 384 | multiplier * |
| 385 | potentiometer_translate(encoder.last_potentiometer_voltage()) + |
| 386 | pot_offset; |
| 387 | position->index_pulses = encoder.index_posedge_count(); |
| 388 | } |
| 389 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 390 | int32_t my_pid_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 391 | |
| 392 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 393 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 394 | ::std::unique_ptr<Encoder> drivetrain_left_encoder_, |
| 395 | drivetrain_right_encoder_; |
| 396 | ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_; |
| 397 | |
| 398 | ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_; |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 399 | ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_, |
| 400 | shoulder_encoder_, wrist_encoder_; |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 401 | ::std::unique_ptr<AnalogInput> ball_detector_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 402 | |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 403 | ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_; |
| 404 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 405 | ::std::atomic<bool> run_{true}; |
Comran Morshed | 1b76432 | 2016-02-14 20:18:12 +0000 | [diff] [blame] | 406 | DigitalGlitchFilter drivetrain_shooter_encoder_filter_, hall_filter_, |
| 407 | superstructure_encoder_filter_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | class SolenoidWriter { |
| 411 | public: |
| 412 | SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
| 413 | : pcm_(pcm), |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 414 | drivetrain_(".frc971.control_loops.drivetrain_queue.output"), |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 415 | shooter_(".y2016.control_loops.shooter.shooter_queue.output"), |
| 416 | superstructure_( |
| 417 | ".y2016.control_loops.superstructure_queue.output") { |
| 418 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 419 | |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 420 | void set_compressor(::std::unique_ptr<Compressor> compressor) { |
| 421 | compressor_ = ::std::move(compressor); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 424 | void set_drivetrain_shifter( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 425 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 426 | drivetrain_shifter_ = ::std::move(s); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 429 | void set_climber_trigger( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 430 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 431 | climber_trigger_ = ::std::move(s); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 434 | void set_traverse( |
| 435 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 436 | traverse_ = ::std::move(s); |
| 437 | } |
| 438 | |
| 439 | void set_traverse_latch( |
| 440 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 441 | traverse_latch_ = ::std::move(s); |
| 442 | } |
| 443 | |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 444 | void set_shooter_clamp( |
| 445 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 446 | shooter_clamp_ = ::std::move(s); |
| 447 | } |
| 448 | |
| 449 | void set_shooter_pusher( |
| 450 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 451 | shooter_pusher_ = ::std::move(s); |
| 452 | } |
| 453 | |
Austin Schuh | e0729a6 | 2016-03-12 21:54:17 -0800 | [diff] [blame] | 454 | void set_lights( |
| 455 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 456 | lights_ = ::std::move(s); |
| 457 | } |
| 458 | |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 459 | void set_flashlight(::std::unique_ptr<Relay> relay) { |
| 460 | flashlight_ = ::std::move(relay); |
| 461 | } |
| 462 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 463 | void operator()() { |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 464 | compressor_->Start(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 465 | ::aos::SetCurrentThreadName("Solenoids"); |
| 466 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 467 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 468 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 469 | ::std::chrono::milliseconds(1)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 470 | |
| 471 | while (run_) { |
| 472 | { |
| 473 | const int iterations = phased_loop.SleepUntilNext(); |
| 474 | if (iterations != 1) { |
| 475 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 480 | drivetrain_.FetchLatest(); |
| 481 | if (drivetrain_.get()) { |
| 482 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 483 | drivetrain_shifter_->Set( |
| 484 | !(drivetrain_->left_high || drivetrain_->right_high)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | { |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 489 | shooter_.FetchLatest(); |
| 490 | if (shooter_.get()) { |
| 491 | LOG_STRUCT(DEBUG, "solenoids", *shooter_); |
| 492 | shooter_clamp_->Set(shooter_->clamp_open); |
| 493 | shooter_pusher_->Set(shooter_->push_to_shooter); |
Austin Schuh | e0729a6 | 2016-03-12 21:54:17 -0800 | [diff] [blame] | 494 | lights_->Set(shooter_->lights_on); |
Austin Schuh | b2c3338 | 2016-04-03 16:09:17 -0700 | [diff] [blame] | 495 | if (shooter_->forwards_flashlight) { |
| 496 | if (shooter_->backwards_flashlight) { |
| 497 | flashlight_->Set(Relay::kOn); |
| 498 | } else { |
| 499 | flashlight_->Set(Relay::kReverse); |
| 500 | } |
| 501 | } else { |
| 502 | if (shooter_->backwards_flashlight) { |
| 503 | flashlight_->Set(Relay::kForward); |
| 504 | } else { |
| 505 | flashlight_->Set(Relay::kOff); |
| 506 | } |
| 507 | } |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | { |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 512 | superstructure_.FetchLatest(); |
| 513 | if (superstructure_.get()) { |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 514 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 515 | |
| 516 | climber_trigger_->Set(superstructure_->unfold_climber); |
| 517 | |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 518 | traverse_->Set(superstructure_->traverse_down); |
| 519 | traverse_latch_->Set(superstructure_->traverse_unlatched); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 524 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 525 | { |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 526 | to_log.compressor_on = compressor_->Enabled(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | pcm_->Flush(); |
| 530 | to_log.read_solenoids = pcm_->GetAll(); |
| 531 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void Quit() { run_ = false; } |
| 537 | |
| 538 | private: |
| 539 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_; |
| 540 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 541 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_, |
| 542 | shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_, |
| 543 | climber_trigger_; |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 544 | ::std::unique_ptr<Relay> flashlight_; |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 545 | ::std::unique_ptr<Compressor> compressor_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 546 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 547 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
Comran Morshed | 3263e8f | 2016-02-14 17:55:45 +0000 | [diff] [blame] | 548 | ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_; |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 549 | ::aos::Queue< |
| 550 | ::y2016::control_loops::SuperstructureQueue::Output> |
| 551 | superstructure_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 552 | |
| 553 | ::std::atomic<bool> run_{true}; |
| 554 | }; |
| 555 | |
| 556 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 557 | public: |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 558 | void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) { |
| 559 | drivetrain_left_talon_ = ::std::move(t); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 562 | void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) { |
| 563 | drivetrain_right_talon_ = ::std::move(t); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | private: |
| 567 | virtual void Read() override { |
| 568 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 569 | } |
| 570 | |
| 571 | virtual void Write() override { |
| 572 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 573 | LOG_STRUCT(DEBUG, "will output", *queue); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 574 | drivetrain_left_talon_->SetSpeed(queue->left_voltage / 12.0); |
| 575 | drivetrain_right_talon_->SetSpeed(-queue->right_voltage / 12.0); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | virtual void Stop() override { |
| 579 | LOG(WARNING, "drivetrain output too old\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 580 | drivetrain_left_talon_->SetDisabled(); |
| 581 | drivetrain_right_talon_->SetDisabled(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 584 | ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_; |
| 585 | }; |
| 586 | |
| 587 | class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 588 | public: |
| 589 | void set_shooter_left_talon(::std::unique_ptr<Talon> t) { |
| 590 | shooter_left_talon_ = ::std::move(t); |
| 591 | } |
| 592 | |
| 593 | void set_shooter_right_talon(::std::unique_ptr<Talon> t) { |
| 594 | shooter_right_talon_ = ::std::move(t); |
| 595 | } |
| 596 | |
| 597 | private: |
| 598 | virtual void Read() override { |
| 599 | ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother(); |
| 600 | } |
| 601 | |
| 602 | virtual void Write() override { |
| 603 | auto &queue = ::y2016::control_loops::shooter::shooter_queue.output; |
| 604 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 605 | |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 606 | shooter_left_talon_->SetSpeed(queue->voltage_left / 12.0); |
| 607 | shooter_right_talon_->SetSpeed(-queue->voltage_right / 12.0); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | virtual void Stop() override { |
| 611 | LOG(WARNING, "Shooter output too old.\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 612 | shooter_left_talon_->SetDisabled(); |
| 613 | shooter_right_talon_->SetDisabled(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_; |
| 617 | }; |
| 618 | |
| 619 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 620 | public: |
| 621 | void set_intake_talon(::std::unique_ptr<Talon> t) { |
| 622 | intake_talon_ = ::std::move(t); |
| 623 | } |
| 624 | |
| 625 | void set_shoulder_talon(::std::unique_ptr<Talon> t) { |
| 626 | shoulder_talon_ = ::std::move(t); |
| 627 | } |
| 628 | |
| 629 | void set_wrist_talon(::std::unique_ptr<Talon> t) { |
| 630 | wrist_talon_ = ::std::move(t); |
| 631 | } |
| 632 | |
Campbell Crowley | d4fd655 | 2016-02-21 17:53:46 -0800 | [diff] [blame] | 633 | void set_top_rollers_talon(::std::unique_ptr<Talon> t) { |
| 634 | top_rollers_talon_ = ::std::move(t); |
| 635 | } |
| 636 | |
| 637 | void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) { |
| 638 | bottom_rollers_talon_ = ::std::move(t); |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 641 | void set_climber_talon(::std::unique_ptr<Talon> t) { |
| 642 | climber_talon_ = ::std::move(t); |
| 643 | } |
| 644 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 645 | private: |
| 646 | virtual void Read() override { |
| 647 | ::y2016::control_loops::superstructure_queue.output.FetchAnother(); |
| 648 | } |
| 649 | |
| 650 | virtual void Write() override { |
| 651 | auto &queue = ::y2016::control_loops::superstructure_queue.output; |
| 652 | LOG_STRUCT(DEBUG, "will output", *queue); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 653 | intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake, |
| 654 | -kMaxBringupPower, kMaxBringupPower) / |
| 655 | 12.0); |
| 656 | shoulder_talon_->SetSpeed(::aos::Clip(-queue->voltage_shoulder, |
| 657 | -kMaxBringupPower, kMaxBringupPower) / |
| 658 | 12.0); |
| 659 | wrist_talon_->SetSpeed( |
Austin Schuh | a9992ff | 2016-02-28 21:59:23 -0800 | [diff] [blame] | 660 | ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) / |
| 661 | 12.0); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 662 | top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0); |
| 663 | bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0); |
| 664 | climber_talon_->SetSpeed(-queue->voltage_climber / 12.0); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | virtual void Stop() override { |
| 668 | LOG(WARNING, "Superstructure output too old.\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 669 | intake_talon_->SetDisabled(); |
| 670 | shoulder_talon_->SetDisabled(); |
| 671 | wrist_talon_->SetDisabled(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 674 | ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_, |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 675 | top_rollers_talon_, bottom_rollers_talon_, climber_talon_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 676 | }; |
| 677 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 678 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 679 | public: |
| 680 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 681 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 682 | Encoder::k4X); |
| 683 | } |
| 684 | |
| 685 | void Run() override { |
| 686 | ::aos::InitNRT(); |
| 687 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 688 | |
| 689 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 690 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 691 | |
| 692 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 693 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 694 | SensorReader reader; |
| 695 | |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 696 | reader.set_drivetrain_left_encoder(make_encoder(5)); |
| 697 | reader.set_drivetrain_right_encoder(make_encoder(6)); |
| 698 | reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5)); |
| 699 | reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 700 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 701 | reader.set_shooter_left_encoder(make_encoder(7)); |
| 702 | reader.set_shooter_right_encoder(make_encoder(-3)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 703 | |
| 704 | reader.set_intake_encoder(make_encoder(0)); |
| 705 | reader.set_intake_index(make_unique<DigitalInput>(0)); |
| 706 | reader.set_intake_potentiometer(make_unique<AnalogInput>(0)); |
| 707 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 708 | reader.set_shoulder_encoder(make_encoder(4)); |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 709 | reader.set_shoulder_index(make_unique<DigitalInput>(2)); |
| 710 | reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2)); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 711 | |
Austin Schuh | 9f77fd2 | 2016-02-21 02:53:58 -0800 | [diff] [blame] | 712 | reader.set_wrist_encoder(make_encoder(1)); |
| 713 | reader.set_wrist_index(make_unique<DigitalInput>(1)); |
| 714 | reader.set_wrist_potentiometer(make_unique<AnalogInput>(1)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 715 | |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 716 | reader.set_ball_detector(make_unique<AnalogInput>(7)); |
| 717 | |
Brian Silverman | 68cb5c2 | 2016-03-20 18:11:14 -0700 | [diff] [blame] | 718 | reader.set_autonomous_mode(0, make_unique<DigitalInput>(9)); |
| 719 | reader.set_autonomous_mode(1, make_unique<DigitalInput>(8)); |
| 720 | reader.set_autonomous_mode(2, make_unique<DigitalInput>(7)); |
| 721 | reader.set_autonomous_mode(3, make_unique<DigitalInput>(6)); |
| 722 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 723 | reader.set_dma(make_unique<DMA>()); |
| 724 | ::std::thread reader_thread(::std::ref(reader)); |
| 725 | |
Brian Silverman | 003a473 | 2018-03-11 14:02:15 -0700 | [diff] [blame] | 726 | // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448. |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 727 | ::frc971::wpilib::GyroSender gyro_sender; |
| 728 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 729 | |
Austin Schuh | f0c0576 | 2016-04-03 16:06:49 -0700 | [diff] [blame] | 730 | auto imu_trigger = make_unique<DigitalInput>(3); |
| 731 | ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get()); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 732 | ::std::thread imu_thread(::std::ref(imu)); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 733 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 734 | DrivetrainWriter drivetrain_writer; |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 735 | drivetrain_writer.set_drivetrain_left_talon( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 736 | ::std::unique_ptr<Talon>(new Talon(5))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 737 | drivetrain_writer.set_drivetrain_right_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 738 | ::std::unique_ptr<Talon>(new Talon(4))); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 739 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 740 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 741 | ShooterWriter shooter_writer; |
| 742 | shooter_writer.set_shooter_left_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 743 | ::std::unique_ptr<Talon>(new Talon(9))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 744 | shooter_writer.set_shooter_right_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 745 | ::std::unique_ptr<Talon>(new Talon(8))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 746 | ::std::thread shooter_writer_thread(::std::ref(shooter_writer)); |
| 747 | |
| 748 | SuperstructureWriter superstructure_writer; |
| 749 | superstructure_writer.set_intake_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 750 | ::std::unique_ptr<Talon>(new Talon(3))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 751 | superstructure_writer.set_shoulder_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 752 | ::std::unique_ptr<Talon>(new Talon(6))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 753 | superstructure_writer.set_wrist_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 754 | ::std::unique_ptr<Talon>(new Talon(2))); |
Campbell Crowley | d4fd655 | 2016-02-21 17:53:46 -0800 | [diff] [blame] | 755 | superstructure_writer.set_top_rollers_talon( |
Austin Schuh | 0c2b58c | 2016-02-21 17:23:46 -0800 | [diff] [blame] | 756 | ::std::unique_ptr<Talon>(new Talon(1))); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 757 | superstructure_writer.set_bottom_rollers_talon( |
| 758 | ::std::unique_ptr<Talon>(new Talon(0))); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 759 | superstructure_writer.set_climber_talon( |
| 760 | ::std::unique_ptr<Talon>(new Talon(7))); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 761 | ::std::thread superstructure_writer_thread( |
| 762 | ::std::ref(superstructure_writer)); |
| 763 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 764 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 765 | new ::frc971::wpilib::BufferedPcm()); |
| 766 | SolenoidWriter solenoid_writer(pcm); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 767 | solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0)); |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 768 | solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2)); |
| 769 | solenoid_writer.set_traverse(pcm->MakeSolenoid(3)); |
Austin Schuh | caa1ee9 | 2016-02-27 14:45:37 -0800 | [diff] [blame] | 770 | solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4)); |
| 771 | solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5)); |
Austin Schuh | e0729a6 | 2016-03-12 21:54:17 -0800 | [diff] [blame] | 772 | solenoid_writer.set_lights(pcm->MakeSolenoid(6)); |
Comran Morshed | 71466fe | 2016-04-21 20:21:14 -0700 | [diff] [blame] | 773 | solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1)); |
Austin Schuh | 8b89d33 | 2016-03-24 20:19:12 -0700 | [diff] [blame] | 774 | solenoid_writer.set_flashlight(make_unique<Relay>(0)); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 775 | |
Campbell Crowley | 1ab5fab | 2016-02-21 13:39:31 -0800 | [diff] [blame] | 776 | solenoid_writer.set_compressor(make_unique<Compressor>()); |
| 777 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 778 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 779 | |
| 780 | // Wait forever. Not much else to do... |
| 781 | while (true) { |
| 782 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 783 | if (r != 0) { |
| 784 | PLOG(WARNING, "infinite select failed"); |
| 785 | } else { |
| 786 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 791 | |
| 792 | joystick_sender.Quit(); |
| 793 | joystick_thread.join(); |
| 794 | pdp_fetcher.Quit(); |
| 795 | pdp_fetcher_thread.join(); |
| 796 | reader.Quit(); |
| 797 | reader_thread.join(); |
| 798 | gyro_sender.Quit(); |
| 799 | gyro_thread.join(); |
Brian Silverman | 5f17a97 | 2016-02-28 01:49:32 -0500 | [diff] [blame] | 800 | imu.Quit(); |
| 801 | imu_thread.join(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 802 | |
| 803 | drivetrain_writer.Quit(); |
| 804 | drivetrain_writer_thread.join(); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 805 | shooter_writer.Quit(); |
| 806 | shooter_writer_thread.join(); |
| 807 | superstructure_writer.Quit(); |
| 808 | superstructure_writer_thread.join(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 809 | solenoid_writer.Quit(); |
| 810 | solenoid_thread.join(); |
| 811 | |
| 812 | ::aos::Cleanup(); |
| 813 | } |
| 814 | }; |
| 815 | |
| 816 | } // namespace wpilib |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 817 | } // namespace y2016 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 818 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 819 | AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot); |