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