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