Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cinttypes> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 6 | #include <cmath> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cstdio> |
| 8 | #include <cstring> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 9 | #include <functional> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 10 | #include <memory> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 11 | #include <mutex> |
| 12 | #include <thread> |
| 13 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 15 | #include "frc971/wpilib/ahal/Counter.h" |
| 16 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 17 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 18 | #include "frc971/wpilib/ahal/Encoder.h" |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 19 | #include "frc971/wpilib/ahal/TalonFX.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 20 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 21 | #undef ERROR |
| 22 | |
| 23 | #include "aos/commonmath.h" |
| 24 | #include "aos/events/event_loop.h" |
| 25 | #include "aos/events/shm_event_loop.h" |
| 26 | #include "aos/init.h" |
| 27 | #include "aos/logging/logging.h" |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 28 | #include "aos/network/team_number.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 29 | #include "aos/realtime.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [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" |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 34 | #include "ctre/phoenix/motorcontrol/can/TalonFX.h" |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 35 | #include "ctre/phoenix/motorcontrol/can/VictorSPX.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 36 | #include "frc971/autonomous/auto_mode_generated.h" |
| 37 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 38 | #include "frc971/input/robot_state_generated.h" |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 39 | #include "frc971/wpilib/ADIS16448.h" |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 40 | #include "frc971/wpilib/ADIS16470.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 41 | #include "frc971/wpilib/buffered_pcm.h" |
| 42 | #include "frc971/wpilib/buffered_solenoid.h" |
| 43 | #include "frc971/wpilib/dma.h" |
| 44 | #include "frc971/wpilib/drivetrain_writer.h" |
| 45 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 46 | #include "frc971/wpilib/joystick_sender.h" |
| 47 | #include "frc971/wpilib/logging_generated.h" |
| 48 | #include "frc971/wpilib/loop_output_handler.h" |
| 49 | #include "frc971/wpilib/pdp_fetcher.h" |
| 50 | #include "frc971/wpilib/sensor_reader.h" |
| 51 | #include "frc971/wpilib/wpilib_robot_base.h" |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 52 | #include "gflags/gflags.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 53 | #include "y2020/constants.h" |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 54 | #include "y2020/control_loops/superstructure/shooter/shooter_tuning_readings_generated.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 55 | #include "y2020/control_loops/superstructure/superstructure_output_generated.h" |
| 56 | #include "y2020/control_loops/superstructure/superstructure_position_generated.h" |
| 57 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 58 | DEFINE_bool(shooter_tuning, true, |
| 59 | "If true, reads from ball beambreak sensors and sends shooter " |
| 60 | "tuning readings"); |
| 61 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 62 | using ::aos::monotonic_clock; |
| 63 | using ::y2020::constants::Values; |
| 64 | namespace superstructure = ::y2020::control_loops::superstructure; |
| 65 | namespace chrono = ::std::chrono; |
Vinay Siva | e52a6b3 | 2021-07-10 15:19:26 -0700 | [diff] [blame] | 66 | using std::make_unique; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 67 | |
| 68 | namespace y2020 { |
| 69 | namespace wpilib { |
| 70 | namespace { |
| 71 | |
| 72 | constexpr double kMaxBringupPower = 12.0; |
| 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 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 78 | double drivetrain_translate(int32_t in) { |
| 79 | return ((static_cast<double>(in) / |
| 80 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
| 81 | (2.0 * M_PI)) * |
| 82 | Values::kDrivetrainEncoderRatio() * |
| 83 | control_loops::drivetrain::kWheelRadius; |
| 84 | } |
| 85 | |
| 86 | double drivetrain_velocity_translate(double in) { |
| 87 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 88 | (2.0 * M_PI)) * |
| 89 | Values::kDrivetrainEncoderRatio() * |
| 90 | control_loops::drivetrain::kWheelRadius; |
| 91 | } |
| 92 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 93 | double turret_pot_translate(double voltage) { |
| 94 | return voltage * Values::kTurretPotRatio() * |
| 95 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 96 | } |
| 97 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 98 | constexpr double kMaxFastEncoderPulsesPerSecond = |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 99 | std::max({Values::kMaxControlPanelEncoderPulsesPerSecond(), |
| 100 | Values::kMaxFinisherEncoderPulsesPerSecond(), |
| 101 | Values::kMaxAcceleratorEncoderPulsesPerSecond()}); |
| 102 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1000000.0, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 103 | "fast encoders are too fast"); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 104 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 105 | std::max({Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 106 | Values::kMaxHoodEncoderPulsesPerSecond(), |
| 107 | Values::kMaxIntakeEncoderPulsesPerSecond(), |
| 108 | Values::kMaxTurretEncoderPulsesPerSecond()}); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 109 | |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 110 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000.0, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 111 | "medium encoders are too fast"); |
| 112 | |
| 113 | } // namespace |
| 114 | |
| 115 | // Class to send position messages with sensor readings to our loops. |
| 116 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 117 | public: |
| 118 | SensorReader(::aos::ShmEventLoop *event_loop) |
| 119 | : ::frc971::wpilib::SensorReader(event_loop), |
| 120 | auto_mode_sender_( |
| 121 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 122 | "/autonomous")), |
| 123 | superstructure_position_sender_( |
| 124 | event_loop->MakeSender<superstructure::Position>( |
| 125 | "/superstructure")), |
| 126 | drivetrain_position_sender_( |
| 127 | event_loop |
| 128 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 129 | "/drivetrain")), |
| 130 | shooter_tuning_readings_sender_( |
| 131 | event_loop->MakeSender<superstructure::shooter::TuningReadings>( |
| 132 | "/superstructure")) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 133 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 134 | // we should ever see. |
| 135 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 136 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
milind-u | 62d4a8e | 2021-10-11 16:08:41 -0700 | [diff] [blame] | 137 | |
| 138 | constants::InitValues(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 141 | // Hood |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 142 | void set_hood_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 143 | medium_encoder_filter_.Add(encoder.get()); |
| 144 | hood_encoder_.set_encoder(::std::move(encoder)); |
| 145 | } |
| 146 | |
| 147 | void set_hood_absolute_pwm( |
| 148 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 149 | hood_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 150 | } |
| 151 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 152 | void set_hood_single_turn_absolute_pwm( |
| 153 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 154 | hood_encoder_.set_single_turn_absolute_pwm(::std::move(absolute_pwm)); |
| 155 | } |
| 156 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 157 | // Intake |
| 158 | |
| 159 | void set_intake_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 160 | medium_encoder_filter_.Add(encoder.get()); |
| 161 | intake_joint_encoder_.set_encoder(::std::move(encoder)); |
| 162 | } |
| 163 | |
| 164 | void set_intake_absolute_pwm( |
| 165 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 166 | intake_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 167 | } |
| 168 | |
| 169 | // Turret |
| 170 | |
| 171 | void set_turret_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 172 | medium_encoder_filter_.Add(encoder.get()); |
| 173 | turret_encoder_.set_encoder(::std::move(encoder)); |
| 174 | } |
| 175 | |
| 176 | void set_turret_absolute_pwm( |
| 177 | ::std::unique_ptr<frc::DigitalInput> absolute_pwm) { |
| 178 | turret_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 179 | } |
| 180 | |
| 181 | void set_turret_potentiometer( |
| 182 | ::std::unique_ptr<frc::AnalogInput> potentiometer) { |
| 183 | turret_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 184 | } |
| 185 | |
| 186 | // Shooter |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 187 | void set_finisher_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 188 | fast_encoder_filter_.Add(encoder.get()); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 189 | finisher_encoder_ = ::std::move(encoder); |
| 190 | } |
| 191 | void set_left_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
| 192 | fast_encoder_filter_.Add(encoder.get()); |
| 193 | left_accelerator_encoder_ = ::std::move(encoder); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 196 | void set_right_accelerator_encoder(::std::unique_ptr<frc::Encoder> encoder) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 197 | fast_encoder_filter_.Add(encoder.get()); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 198 | right_accelerator_encoder_ = ::std::move(encoder); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 201 | // Auto mode switches. |
| 202 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 203 | medium_encoder_filter_.Add(sensor.get()); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 204 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 205 | } |
| 206 | |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 207 | void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; } |
| 208 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 209 | void set_ball_beambreak_inputs(::std::unique_ptr<frc::DigitalInput> sensor1, |
| 210 | ::std::unique_ptr<frc::DigitalInput> sensor2) { |
| 211 | ball_beambreak_inputs_[0] = ::std::move(sensor1); |
| 212 | ball_beambreak_inputs_[1] = ::std::move(sensor2); |
Austin Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 213 | ball_beambreak_reader_.set_input_one(ball_beambreak_inputs_[0].get()); |
| 214 | ball_beambreak_reader_.set_input_two(ball_beambreak_inputs_[1].get()); |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 217 | void set_ball_intake_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 218 | ball_intake_beambreak_ = ::std::move(sensor); |
| 219 | } |
| 220 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 221 | void Start() override { |
| 222 | if (FLAGS_shooter_tuning) { |
| 223 | AddToDMA(&ball_beambreak_reader_); |
| 224 | } |
| 225 | } |
| 226 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 227 | void RunIteration() override { |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 228 | if (imu_ != nullptr) { |
| 229 | imu_->DoReads(); |
| 230 | } |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 231 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 232 | { |
| 233 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 234 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 235 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 236 | drivetrain_builder.add_left_encoder( |
| 237 | drivetrain_translate(drivetrain_left_encoder_->GetRaw())); |
| 238 | drivetrain_builder.add_left_speed( |
| 239 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 240 | |
| 241 | drivetrain_builder.add_right_encoder( |
| 242 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw())); |
| 243 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 244 | drivetrain_right_encoder_->GetPeriod())); |
| 245 | |
| 246 | builder.Send(drivetrain_builder.Finish()); |
| 247 | } |
Austin Schuh | 37d2c30 | 2021-10-12 22:07:43 -0700 | [diff] [blame] | 248 | const constants::Values &values = constants::GetValues(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 249 | |
| 250 | { |
| 251 | auto builder = superstructure_position_sender_.MakeBuilder(); |
Austin Schuh | 18ca45f | 2020-02-29 22:58:49 -0800 | [diff] [blame] | 252 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 253 | // TODO(alex): check new absolute encoder api. |
| 254 | // Hood |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 255 | frc971::AbsoluteAndAbsolutePositionT hood; |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 256 | CopyPosition(hood_encoder_, &hood, |
| 257 | Values::kHoodEncoderCountsPerRevolution(), |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 258 | Values::kHoodEncoderRatio(), |
| 259 | Values::kHoodSingleTurnEncoderRatio(), false); |
| 260 | flatbuffers::Offset<frc971::AbsoluteAndAbsolutePosition> hood_offset = |
| 261 | frc971::AbsoluteAndAbsolutePosition::Pack(*builder.fbb(), &hood); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 262 | |
| 263 | // Intake |
| 264 | frc971::AbsolutePositionT intake_joint; |
| 265 | CopyPosition(intake_joint_encoder_, &intake_joint, |
| 266 | Values::kIntakeEncoderCountsPerRevolution(), |
| 267 | Values::kIntakeEncoderRatio(), false); |
| 268 | flatbuffers::Offset<frc971::AbsolutePosition> intake_joint_offset = |
| 269 | frc971::AbsolutePosition::Pack(*builder.fbb(), &intake_joint); |
| 270 | |
| 271 | // Turret |
| 272 | frc971::PotAndAbsolutePositionT turret; |
| 273 | CopyPosition(turret_encoder_, &turret, |
| 274 | Values::kTurretEncoderCountsPerRevolution(), |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 275 | Values::kTurretEncoderRatio(), turret_pot_translate, true, |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 276 | values.turret.potentiometer_offset); |
| 277 | flatbuffers::Offset<frc971::PotAndAbsolutePosition> turret_offset = |
| 278 | frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &turret); |
| 279 | |
| 280 | // Shooter |
| 281 | y2020::control_loops::superstructure::ShooterPositionT shooter; |
| 282 | shooter.theta_finisher = |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 283 | encoder_translate(-finisher_encoder_->GetRaw(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 284 | Values::kFinisherEncoderCountsPerRevolution(), |
| 285 | Values::kFinisherEncoderRatio()); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 286 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 287 | shooter.theta_accelerator_left = |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 288 | encoder_translate(-left_accelerator_encoder_->GetRaw(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 289 | Values::kAcceleratorEncoderCountsPerRevolution(), |
| 290 | Values::kAcceleratorEncoderRatio()); |
| 291 | shooter.theta_accelerator_right = |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 292 | encoder_translate(right_accelerator_encoder_->GetRaw(), |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 293 | Values::kAcceleratorEncoderCountsPerRevolution(), |
| 294 | Values::kAcceleratorEncoderRatio()); |
| 295 | flatbuffers::Offset<y2020::control_loops::superstructure::ShooterPosition> |
| 296 | shooter_offset = |
| 297 | y2020::control_loops::superstructure::ShooterPosition::Pack( |
| 298 | *builder.fbb(), &shooter); |
| 299 | |
Austin Schuh | 18ca45f | 2020-02-29 22:58:49 -0800 | [diff] [blame] | 300 | superstructure::Position::Builder position_builder = |
| 301 | builder.MakeBuilder<superstructure::Position>(); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 302 | position_builder.add_hood(hood_offset); |
| 303 | position_builder.add_intake_joint(intake_joint_offset); |
| 304 | position_builder.add_turret(turret_offset); |
| 305 | position_builder.add_shooter(shooter_offset); |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 306 | position_builder.add_intake_beambreak_triggered( |
Austin Schuh | 02e1496 | 2021-10-13 20:32:24 -0700 | [diff] [blame] | 307 | ball_intake_beambreak_->Get()); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 308 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 309 | builder.Send(position_builder.Finish()); |
| 310 | } |
| 311 | |
| 312 | { |
| 313 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 314 | |
| 315 | uint32_t mode = 0; |
| 316 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 317 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 318 | mode |= 1 << i; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | auto auto_mode_builder = |
| 323 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 324 | |
| 325 | auto_mode_builder.add_mode(mode); |
| 326 | |
| 327 | builder.Send(auto_mode_builder.Finish()); |
| 328 | } |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 329 | |
Austin Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 330 | if (FLAGS_shooter_tuning) { |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 331 | // Distance between beambreak sensors, in meters. |
| 332 | constexpr double kDistanceBetweenBeambreaks = 0.4813; |
| 333 | |
Austin Schuh | 6c053ef | 2021-09-26 14:32:16 -0700 | [diff] [blame] | 334 | if (ball_beambreak_reader_.pulses_detected() > balls_detected_) { |
| 335 | balls_detected_ = ball_beambreak_reader_.pulses_detected(); |
| 336 | |
| 337 | auto builder = shooter_tuning_readings_sender_.MakeBuilder(); |
| 338 | auto shooter_tuning_readings_builder = |
| 339 | builder.MakeBuilder<superstructure::shooter::TuningReadings>(); |
| 340 | shooter_tuning_readings_builder.add_velocity_ball( |
| 341 | kDistanceBetweenBeambreaks / ball_beambreak_reader_.last_width()); |
| 342 | builder.Send(shooter_tuning_readings_builder.Finish()); |
| 343 | } |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 344 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | private: |
| 348 | ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 349 | ::aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 350 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
| 351 | drivetrain_position_sender_; |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 352 | ::aos::Sender<superstructure::shooter::TuningReadings> |
| 353 | shooter_tuning_readings_sender_; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 354 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 355 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer turret_encoder_; |
| 356 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 357 | ::frc971::wpilib::AbsoluteAndAbsoluteEncoder hood_encoder_; |
| 358 | |
| 359 | ::frc971::wpilib::AbsoluteEncoder intake_joint_encoder_; |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 360 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 361 | ::std::unique_ptr<::frc::Encoder> finisher_encoder_, |
milind-u | 3d68aa7 | 2021-09-26 12:19:03 -0700 | [diff] [blame] | 362 | left_accelerator_encoder_, right_accelerator_encoder_; |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 363 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 364 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 365 | |
| 366 | frc971::wpilib::ADIS16470 *imu_ = nullptr; |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 367 | |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 368 | std::unique_ptr<frc::DigitalInput> ball_intake_beambreak_; |
| 369 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 370 | // Used to interface with the two beam break sensors that the ball for tuning |
| 371 | // shooter parameters has to pass through. |
| 372 | // We will time how long it takes to pass between the two sensors to get its |
| 373 | // velocity. |
| 374 | std::array<std::unique_ptr<frc::DigitalInput>, 2> ball_beambreak_inputs_; |
| 375 | frc971::wpilib::DMAPulseSeparationReader ball_beambreak_reader_; |
| 376 | int balls_detected_ = 0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | class SuperstructureWriter |
| 380 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 381 | public: |
| 382 | SuperstructureWriter(::aos::EventLoop *event_loop) |
| 383 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 384 | event_loop, "/superstructure") {} |
| 385 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 386 | void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 387 | hood_victor_ = ::std::move(t); |
| 388 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 389 | |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 390 | void set_intake_joint_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 391 | intake_joint_victor_ = ::std::move(t); |
| 392 | } |
| 393 | |
| 394 | void set_intake_roller_falcon( |
| 395 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
| 396 | intake_roller_falcon_ = ::std::move(t); |
| 397 | intake_roller_falcon_->ConfigSupplyCurrentLimit( |
| 398 | {true, Values::kIntakeRollerSupplyCurrentLimit(), |
| 399 | Values::kIntakeRollerSupplyCurrentLimit(), 0}); |
| 400 | intake_roller_falcon_->ConfigStatorCurrentLimit( |
| 401 | {true, Values::kIntakeRollerStatorCurrentLimit(), |
| 402 | Values::kIntakeRollerStatorCurrentLimit(), 0}); |
| 403 | } |
| 404 | |
| 405 | void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 406 | turret_victor_ = ::std::move(t); |
| 407 | } |
| 408 | |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 409 | void set_feeder_falcon( |
| 410 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 411 | feeder_falcon_ = ::std::move(t); |
Austin Schuh | 3399d12 | 2021-03-20 14:29:20 -0700 | [diff] [blame] | 412 | { |
| 413 | auto result = feeder_falcon_->ConfigSupplyCurrentLimit( |
| 414 | {true, Values::kFeederSupplyCurrentLimit(), |
| 415 | Values::kFeederSupplyCurrentLimit(), 0}); |
| 416 | if (result != ctre::phoenix::OKAY) { |
| 417 | LOG(WARNING) << "Failed to configure feeder supply current limit: " |
| 418 | << result; |
| 419 | } |
| 420 | } |
| 421 | { |
| 422 | auto result = feeder_falcon_->ConfigStatorCurrentLimit( |
| 423 | {true, Values::kFeederStatorCurrentLimit(), |
| 424 | Values::kFeederStatorCurrentLimit(), 0}); |
| 425 | if (result != ctre::phoenix::OKAY) { |
| 426 | LOG(WARNING) << "Failed to configure feeder stator current limit: " |
| 427 | << result; |
| 428 | } |
| 429 | } |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 430 | } |
| 431 | |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 432 | void set_washing_machine_control_panel_victor( |
| 433 | ::std::unique_ptr<::frc::VictorSP> t) { |
| 434 | washing_machine_control_panel_victor_ = ::std::move(t); |
| 435 | } |
| 436 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 437 | void set_accelerator_left_falcon(::std::unique_ptr<::frc::TalonFX> t) { |
| 438 | accelerator_left_falcon_ = ::std::move(t); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 441 | void set_accelerator_right_falcon(::std::unique_ptr<::frc::TalonFX> t) { |
| 442 | accelerator_right_falcon_ = ::std::move(t); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 445 | void set_finisher_falcon0(::std::unique_ptr<::frc::TalonFX> t) { |
| 446 | finisher_falcon0_ = ::std::move(t); |
| 447 | } |
| 448 | |
| 449 | void set_finisher_falcon1(::std::unique_ptr<::frc::TalonFX> t) { |
| 450 | finisher_falcon1_ = ::std::move(t); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void set_climber_falcon( |
| 454 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
| 455 | climber_falcon_ = ::std::move(t); |
| 456 | climber_falcon_->ConfigSupplyCurrentLimit( |
| 457 | {true, Values::kClimberSupplyCurrentLimit(), |
| 458 | Values::kClimberSupplyCurrentLimit(), 0}); |
| 459 | } |
| 460 | |
| 461 | private: |
| 462 | void Write(const superstructure::Output &output) override { |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 463 | hood_victor_->SetSpeed(std::clamp(-output.hood_voltage(), -kMaxBringupPower, |
| 464 | kMaxBringupPower) / |
| 465 | 12.0); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 466 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 467 | intake_joint_victor_->SetSpeed(std::clamp(-output.intake_joint_voltage(), |
| 468 | -kMaxBringupPower, |
| 469 | kMaxBringupPower) / |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 470 | 12.0); |
| 471 | |
| 472 | intake_roller_falcon_->Set( |
| 473 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 474 | std::clamp(-output.intake_roller_voltage(), -kMaxBringupPower, |
| 475 | kMaxBringupPower) / |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 476 | 12.0); |
| 477 | |
milind-u | f70e8e1 | 2021-10-02 12:36:00 -0700 | [diff] [blame] | 478 | turret_victor_->SetSpeed(std::clamp(output.turret_voltage(), |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 479 | -kMaxBringupPower, kMaxBringupPower) / |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 480 | 12.0); |
| 481 | |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 482 | feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 483 | std::clamp(output.feeder_voltage(), -kMaxBringupPower, |
| 484 | kMaxBringupPower) / |
| 485 | 12.0); |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 486 | if (washing_machine_control_panel_victor_) { |
| 487 | washing_machine_control_panel_victor_->SetSpeed( |
| 488 | std::clamp(-output.washing_machine_spinner_voltage(), |
| 489 | -kMaxBringupPower, kMaxBringupPower) / |
| 490 | 12.0); |
| 491 | } |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 492 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 493 | accelerator_left_falcon_->SetSpeed( |
| 494 | std::clamp(-output.accelerator_left_voltage(), -kMaxBringupPower, |
| 495 | kMaxBringupPower) / |
| 496 | 12.0); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 497 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 498 | accelerator_right_falcon_->SetSpeed( |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 499 | std::clamp(output.accelerator_right_voltage(), -kMaxBringupPower, |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 500 | kMaxBringupPower) / |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 501 | 12.0); |
| 502 | |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 503 | finisher_falcon1_->SetSpeed(std::clamp(output.finisher_voltage(), |
| 504 | -kMaxBringupPower, |
| 505 | kMaxBringupPower) / |
| 506 | 12.0); |
| 507 | finisher_falcon0_->SetSpeed(std::clamp(-output.finisher_voltage(), |
| 508 | -kMaxBringupPower, |
| 509 | kMaxBringupPower) / |
| 510 | 12.0); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 511 | |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 512 | if (climber_falcon_) { |
| 513 | climber_falcon_->Set( |
| 514 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
Ravago Jones | e92ff11 | 2021-10-23 17:27:44 -0700 | [diff] [blame^] | 515 | std::clamp(output.climber_voltage(), -kMaxBringupPower, |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 516 | kMaxBringupPower) / |
| 517 | 12.0); |
| 518 | } |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void Stop() override { |
| 522 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
| 523 | hood_victor_->SetDisabled(); |
| 524 | intake_joint_victor_->SetDisabled(); |
| 525 | turret_victor_->SetDisabled(); |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 526 | if (washing_machine_control_panel_victor_) { |
| 527 | washing_machine_control_panel_victor_->SetDisabled(); |
| 528 | } |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 529 | accelerator_left_falcon_->SetDisabled(); |
| 530 | accelerator_right_falcon_->SetDisabled(); |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 531 | finisher_falcon0_->SetDisabled(); |
| 532 | finisher_falcon1_->SetDisabled(); |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 533 | feeder_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
| 534 | intake_roller_falcon_->Set( |
| 535 | ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
| 536 | climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | ::std::unique_ptr<::frc::VictorSP> hood_victor_, intake_joint_victor_, |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 540 | turret_victor_, washing_machine_control_panel_victor_; |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 541 | |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 542 | ::std::unique_ptr<::frc::TalonFX> accelerator_left_falcon_, |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 543 | accelerator_right_falcon_, finisher_falcon0_, finisher_falcon1_; |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 544 | |
| 545 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 546 | intake_roller_falcon_, climber_falcon_, feeder_falcon_; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 550 | public: |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 551 | ::std::unique_ptr<frc::Encoder> make_encoder( |
| 552 | int index, frc::Encoder::EncodingType encodingType = frc::Encoder::k4X) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 553 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 554 | encodingType); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void Run() override { |
| 558 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 559 | aos::configuration::ReadConfig("config.json"); |
| 560 | |
| 561 | // Thread 1. |
| 562 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 563 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 564 | &joystick_sender_event_loop); |
| 565 | AddLoop(&joystick_sender_event_loop); |
| 566 | |
| 567 | // Thread 2. |
| 568 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 569 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 570 | AddLoop(&pdp_fetcher_event_loop); |
| 571 | |
| 572 | // Thread 3. |
| 573 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 574 | SensorReader sensor_reader(&sensor_reader_event_loop); |
Austin Schuh | f7db58c | 2020-02-29 22:57:43 -0800 | [diff] [blame] | 575 | sensor_reader.set_pwm_trigger(true); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 576 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 577 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 578 | // TODO: pin numbers |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 579 | sensor_reader.set_hood_encoder( |
| 580 | make_unique<frc::Encoder>(22, 23, false, frc::Encoder::k4X)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 581 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 582 | sensor_reader.set_hood_absolute_pwm(make_unique<frc::DigitalInput>(25)); |
| 583 | sensor_reader.set_hood_single_turn_absolute_pwm( |
| 584 | make_unique<frc::DigitalInput>(24)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 585 | |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 586 | sensor_reader.set_intake_encoder(make_encoder(5)); |
| 587 | sensor_reader.set_intake_absolute_pwm(make_unique<frc::DigitalInput>(1)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 588 | |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 589 | sensor_reader.set_turret_encoder(make_encoder(2)); |
| 590 | sensor_reader.set_turret_absolute_pwm(make_unique<frc::DigitalInput>(0)); |
| 591 | sensor_reader.set_turret_potentiometer(make_unique<frc::AnalogInput>(0)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 592 | |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 593 | sensor_reader.set_finisher_encoder( |
| 594 | make_unique<frc::Encoder>(3, 2, false, frc::Encoder::k4X)); |
| 595 | sensor_reader.set_left_accelerator_encoder(make_encoder(4)); |
| 596 | sensor_reader.set_right_accelerator_encoder(make_encoder(3)); |
| 597 | |
Ravago Jones | f8b7bfe | 2021-10-09 16:25:29 -0700 | [diff] [blame] | 598 | sensor_reader.set_ball_intake_beambreak(make_unique<frc::DigitalInput>(4)); |
| 599 | |
milind-u | 4b31c4d | 2021-09-18 16:08:23 -0700 | [diff] [blame] | 600 | if (FLAGS_shooter_tuning) { |
| 601 | sensor_reader.set_ball_beambreak_inputs( |
| 602 | make_unique<frc::DigitalInput>(6), make_unique<frc::DigitalInput>(7)); |
| 603 | } |
| 604 | |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 605 | AddLoop(&sensor_reader_event_loop); |
| 606 | |
James Kuszmaul | 022d40e | 2020-02-11 17:06:18 -0800 | [diff] [blame] | 607 | // Note: If ADIS16470 is plugged in directly to the roboRIO SPI port without |
| 608 | // the Spartan Board, then trigger is on 26, reset 27, and chip select is |
| 609 | // CS0. |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 610 | // TODO(james): Double check whether the above is still accurate/useful with |
| 611 | // the ADIS16448. No reason it shouldn't be. |
| 612 | frc::SPI::Port spi_port = frc::SPI::Port::kOnboardCS1; |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 613 | std::unique_ptr<frc::DigitalInput> imu_trigger; |
| 614 | std::unique_ptr<frc::DigitalOutput> imu_reset; |
| 615 | if (::aos::network::GetTeamNumber() == |
| 616 | constants::Values::kCodingRobotTeamNumber) { |
| 617 | imu_trigger = make_unique<frc::DigitalInput>(26); |
| 618 | imu_reset = make_unique<frc::DigitalOutput>(27); |
| 619 | spi_port = frc::SPI::Port::kOnboardCS0; |
| 620 | } else { |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 621 | imu_trigger = make_unique<frc::DigitalInput>(9); |
| 622 | imu_reset = make_unique<frc::DigitalOutput>(8); |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 623 | } |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 624 | ::aos::ShmEventLoop imu_event_loop(&config.message()); |
| 625 | std::unique_ptr<frc971::wpilib::ADIS16448> old_imu; |
| 626 | std::unique_ptr<frc971::wpilib::ADIS16470> new_imu; |
| 627 | std::unique_ptr<frc::SPI> imu_spi; |
Ravago Jones | e92ff11 | 2021-10-23 17:27:44 -0700 | [diff] [blame^] | 628 | if (::aos::network::GetTeamNumber() == constants::Values::kCompTeamNumber) { |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 629 | old_imu = make_unique<frc971::wpilib::ADIS16448>( |
| 630 | &imu_event_loop, spi_port, imu_trigger.get()); |
| 631 | old_imu->SetDummySPI(frc::SPI::Port::kOnboardCS2); |
| 632 | old_imu->set_reset(imu_reset.get()); |
| 633 | } else { |
| 634 | imu_spi = make_unique<frc::SPI>(spi_port); |
| 635 | new_imu = make_unique<frc971::wpilib::ADIS16470>( |
| 636 | &imu_event_loop, imu_spi.get(), imu_trigger.get(), imu_reset.get()); |
| 637 | sensor_reader.set_imu(new_imu.get()); |
| 638 | } |
| 639 | AddLoop(&imu_event_loop); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 640 | |
| 641 | // Thread 4. |
| 642 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 643 | output_event_loop.set_name("output_writer"); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 644 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 645 | drivetrain_writer.set_left_controller0( |
| 646 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
| 647 | drivetrain_writer.set_right_controller0( |
| 648 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
| 649 | |
| 650 | SuperstructureWriter superstructure_writer(&output_event_loop); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 651 | superstructure_writer.set_hood_victor(make_unique<frc::VictorSP>(8)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 652 | superstructure_writer.set_intake_joint_victor( |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 653 | make_unique<frc::VictorSP>(2)); |
Alex Perry | c4691f5 | 2020-02-17 19:20:01 -0800 | [diff] [blame] | 654 | superstructure_writer.set_intake_roller_falcon( |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 655 | make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0)); |
| 656 | superstructure_writer.set_turret_victor(make_unique<frc::VictorSP>(7)); |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 657 | superstructure_writer.set_feeder_falcon( |
| 658 | make_unique<ctre::phoenix::motorcontrol::can::TalonFX>(1)); |
milind-u | 9aa62a8 | 2021-10-03 20:43:19 -0700 | [diff] [blame] | 659 | superstructure_writer.set_washing_machine_control_panel_victor( |
| 660 | make_unique<frc::VictorSP>(6)); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 661 | superstructure_writer.set_accelerator_left_falcon( |
| 662 | make_unique<::frc::TalonFX>(5)); |
| 663 | superstructure_writer.set_accelerator_right_falcon( |
| 664 | make_unique<::frc::TalonFX>(4)); |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 665 | superstructure_writer.set_finisher_falcon0(make_unique<::frc::TalonFX>(9)); |
| 666 | superstructure_writer.set_finisher_falcon1(make_unique<::frc::TalonFX>(3)); |
Sabina Davis | c2e4bdb | 2020-02-23 13:44:58 -0800 | [diff] [blame] | 667 | // TODO: check port |
Tyler Chatow | 1039e43 | 2020-02-28 21:37:50 -0800 | [diff] [blame] | 668 | superstructure_writer.set_climber_falcon( |
Ravago Jones | 3dda560 | 2021-03-10 00:33:13 -0800 | [diff] [blame] | 669 | make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2)); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 670 | |
| 671 | AddLoop(&output_event_loop); |
| 672 | |
| 673 | RunLoops(); |
| 674 | } |
| 675 | }; |
| 676 | |
| 677 | } // namespace wpilib |
| 678 | } // namespace y2020 |
| 679 | |
| 680 | AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot); |