milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 1 | #ifndef Y2022_CONSTANTS_H_ |
| 2 | #define Y2022_CONSTANTS_H_ |
| 3 | |
| 4 | #include <array> |
| 5 | #include <cmath> |
| 6 | #include <cstdint> |
| 7 | |
| 8 | #include "frc971/constants.h" |
| 9 | #include "frc971/control_loops/pose.h" |
| 10 | #include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h" |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 11 | #include "frc971/shooter_interpolation/interpolation.h" |
James Kuszmaul | ec635d2 | 2023-08-12 18:39:24 -0700 | [diff] [blame^] | 12 | #include "frc971/zeroing/pot_and_absolute_encoder.h" |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 13 | #include "y2022/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 14 | #include "y2022/control_loops/superstructure/catapult/catapult_plant.h" |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 15 | #include "y2022/control_loops/superstructure/climber/climber_plant.h" |
Yash Chainani | 997a749 | 2022-01-29 15:48:56 -0800 | [diff] [blame] | 16 | #include "y2022/control_loops/superstructure/intake/intake_plant.h" |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 17 | #include "y2022/control_loops/superstructure/turret/turret_plant.h" |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 18 | |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 19 | using ::frc971::shooter_interpolation::InterpolationTable; |
| 20 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 21 | namespace y2022 { |
| 22 | namespace constants { |
| 23 | |
Milind Upadhyay | c76bb4c | 2022-09-21 09:31:48 -0700 | [diff] [blame] | 24 | constexpr uint16_t kCompTeamNumber = 971; |
| 25 | constexpr uint16_t kPracticeTeamNumber = 9971; |
| 26 | constexpr uint16_t kCodingRobotTeamNumber = 7971; |
| 27 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 28 | struct Values { |
| 29 | static const int kZeroingSampleSize = 200; |
| 30 | |
| 31 | static constexpr double kDrivetrainCyclesPerRevolution() { return 512.0; } |
| 32 | static constexpr double kDrivetrainEncoderCountsPerRevolution() { |
| 33 | return kDrivetrainCyclesPerRevolution() * 4; |
| 34 | } |
James Kuszmaul | c2b2a80 | 2022-02-25 21:59:31 -0800 | [diff] [blame] | 35 | static constexpr double kDrivetrainEncoderRatio() { return 1.0; } |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 36 | static constexpr double kMaxDrivetrainEncoderPulsesPerSecond() { |
| 37 | return control_loops::drivetrain::kFreeSpeed / (2.0 * M_PI) * |
| 38 | control_loops::drivetrain::kHighOutputRatio / |
| 39 | constants::Values::kDrivetrainEncoderRatio() * |
| 40 | kDrivetrainEncoderCountsPerRevolution(); |
| 41 | } |
James Kuszmaul | 53507e1 | 2022-02-12 18:36:40 -0800 | [diff] [blame] | 42 | |
| 43 | static double DrivetrainEncoderToMeters(int32_t in) { |
| 44 | return ((static_cast<double>(in) / |
| 45 | kDrivetrainEncoderCountsPerRevolution()) * |
| 46 | (2.0 * M_PI)) * |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 47 | kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius; |
Yash Chainani | 997a749 | 2022-01-29 15:48:56 -0800 | [diff] [blame] | 48 | } |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 49 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 50 | // Climber |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 51 | static constexpr ::frc971::constants::Range kClimberRange() { |
Milind Upadhyay | e9075d1 | 2022-04-12 22:45:16 -0700 | [diff] [blame] | 52 | return ::frc971::constants::Range{.lower_hard = -0.01, |
| 53 | .upper_hard = 0.59, |
| 54 | .lower = 0.003, |
| 55 | .upper = 0.555}; |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 56 | } |
| 57 | static constexpr double kClimberPotMetersPerRevolution() { |
| 58 | return 22 * 0.25 * 0.0254; |
| 59 | } |
| 60 | static constexpr double kClimberPotRatio() { return 1.0; } |
| 61 | |
Nathan Leong | 342b85e | 2023-01-08 13:49:33 -0800 | [diff] [blame] | 62 | static constexpr double kClimberPotMetersPerVolt() { |
| 63 | return kClimberPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) * |
| 64 | kClimberPotMetersPerRevolution(); |
| 65 | } |
| 66 | |
Siddhant Kanwar | 0e37f59 | 2022-02-21 19:26:50 -0800 | [diff] [blame] | 67 | struct PotConstants { |
| 68 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 69 | ::frc971::zeroing::RelativeEncoderZeroingEstimator> |
| 70 | subsystem_params; |
| 71 | double potentiometer_offset; |
| 72 | }; |
| 73 | |
| 74 | PotConstants climber; |
Milo Lin | 6b5e458 | 2022-01-29 14:51:37 -0800 | [diff] [blame] | 75 | |
| 76 | // Intake |
| 77 | // two encoders with same gear ratio for intake |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 78 | static constexpr double kIntakeEncoderCountsPerRevolution() { return 4096.0; } |
Milo Lin | 6b5e458 | 2022-01-29 14:51:37 -0800 | [diff] [blame] | 79 | |
| 80 | static constexpr double kIntakeEncoderRatio() { |
Austin Schuh | 275f981 | 2022-03-05 14:02:37 -0800 | [diff] [blame] | 81 | return (16.0 / 64.0) * (18.0 / 62.0); |
Milo Lin | 6b5e458 | 2022-01-29 14:51:37 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 84 | static constexpr double kIntakePotRatio() { return 16.0 / 64.0; } |
| 85 | |
Nathan Leong | 342b85e | 2023-01-08 13:49:33 -0800 | [diff] [blame] | 86 | static constexpr double kIntakePotRadiansPerVolt() { |
| 87 | return kIntakePotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) * |
| 88 | (2 * M_PI /*radians*/); |
| 89 | } |
| 90 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 91 | static constexpr double kMaxIntakeEncoderPulsesPerSecond() { |
| 92 | return control_loops::superstructure::intake::kFreeSpeed / (2.0 * M_PI) * |
| 93 | control_loops::superstructure::intake::kOutputRatio / |
| 94 | kIntakeEncoderRatio() * kIntakeEncoderCountsPerRevolution(); |
| 95 | } |
| 96 | |
| 97 | struct PotAndAbsEncoderConstants { |
| 98 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 99 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 100 | subsystem_params; |
| 101 | double potentiometer_offset; |
| 102 | }; |
| 103 | |
| 104 | PotAndAbsEncoderConstants intake_front; |
| 105 | PotAndAbsEncoderConstants intake_back; |
| 106 | |
| 107 | // TODO (Yash): Constants need to be tuned |
| 108 | static constexpr ::frc971::constants::Range kIntakeRange() { |
| 109 | return ::frc971::constants::Range{ |
Austin Schuh | 465a288 | 2022-03-05 15:39:04 -0800 | [diff] [blame] | 110 | .lower_hard = -0.85, // Back Hard |
| 111 | .upper_hard = 1.85, // Front Hard |
| 112 | .lower = -0.400, // Back Soft |
| 113 | .upper = 1.57 // Front Soft |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 114 | }; |
| 115 | } |
| 116 | |
| 117 | // Intake rollers |
| 118 | static constexpr double kIntakeRollerSupplyCurrentLimit() { return 40.0; } |
| 119 | static constexpr double kIntakeRollerStatorCurrentLimit() { return 60.0; } |
| 120 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 121 | // Transfer rollers |
Milind Upadhyay | b1a74ea | 2022-03-09 20:34:35 -0800 | [diff] [blame] | 122 | static constexpr double kTransferRollerVoltage() { return 12.0; } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 123 | |
| 124 | // Voltage to wiggle the transfer rollers and keep a ball in. |
Milind Upadhyay | b1a74ea | 2022-03-09 20:34:35 -0800 | [diff] [blame] | 125 | static constexpr double kTransferRollerWiggleVoltage() { return 3.0; } |
| 126 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 127 | // Turret |
| 128 | PotAndAbsEncoderConstants turret; |
Milind Upadhyay | e9075d1 | 2022-04-12 22:45:16 -0700 | [diff] [blame] | 129 | frc971::constants::Range turret_range; |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 130 | |
Austin Schuh | 42d7e5f | 2022-03-16 23:35:09 -0700 | [diff] [blame] | 131 | static constexpr double kTurretBackIntakePos() { return -M_PI; } |
Milind Upadhyay | 47e0d03 | 2022-03-05 23:06:25 -0800 | [diff] [blame] | 132 | static constexpr double kTurretFrontIntakePos() { return 0; } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 133 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 134 | static constexpr double kTurretPotRatio() { return 27.0 / 110.0; } |
Nathan Leong | 342b85e | 2023-01-08 13:49:33 -0800 | [diff] [blame] | 135 | static constexpr double kTurretPotRadiansPerVolt() { |
| 136 | return kTurretPotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) * |
| 137 | (2 * M_PI /*radians*/); |
| 138 | } |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 139 | static constexpr double kTurretEncoderRatio() { return kTurretPotRatio(); } |
| 140 | static constexpr double kTurretEncoderCountsPerRevolution() { return 4096.0; } |
| 141 | |
| 142 | static constexpr double kMaxTurretEncoderPulsesPerSecond() { |
| 143 | return control_loops::superstructure::turret::kFreeSpeed / (2.0 * M_PI) * |
| 144 | control_loops::superstructure::turret::kOutputRatio / |
| 145 | kTurretEncoderRatio() * kTurretEncoderCountsPerRevolution(); |
| 146 | } |
Griffin Bui | bcbef48 | 2022-02-23 15:32:10 -0800 | [diff] [blame] | 147 | |
| 148 | // Flipper arms |
milind-u | 37dc40b | 2022-03-08 19:51:27 -0800 | [diff] [blame] | 149 | static constexpr double kFlipperArmSupplyCurrentLimit() { return 40.0; } |
| 150 | static constexpr double kFlipperArmStatorCurrentLimit() { return 60.0; } |
Griffin Bui | bcbef48 | 2022-02-23 15:32:10 -0800 | [diff] [blame] | 151 | |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 152 | // Voltage to open the flippers for firing |
| 153 | static constexpr double kFlipperOpenVoltage() { return 3.0; } |
| 154 | // Voltage to keep the flippers open for firing once they already are |
Austin Schuh | 465a288 | 2022-03-05 15:39:04 -0800 | [diff] [blame] | 155 | static constexpr double kFlipperHoldVoltage() { return 2.5; } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 156 | // Voltage to feed a ball from the transfer rollers to the catpult with the |
| 157 | // flippers |
Milind Upadhyay | 47e0d03 | 2022-03-05 23:06:25 -0800 | [diff] [blame] | 158 | static constexpr double kFlipperFeedVoltage() { return -12.0; } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 159 | |
| 160 | // Ball is fed into catapult for atleast this time no matter what |
| 161 | static constexpr std::chrono::milliseconds kExtraLoadingTime() { |
| 162 | return std::chrono::milliseconds(100); |
| 163 | } |
| 164 | // If we have been trying to transfer the ball for this amount of time, it |
| 165 | // probably got lost so abort |
| 166 | static constexpr std::chrono::seconds kBallLostTime() { |
| 167 | return std::chrono::seconds(2); |
| 168 | } |
| 169 | // If the flippers took more than this amount of time to open for firing, |
| 170 | // reseat the ball |
| 171 | static constexpr std::chrono::milliseconds kFlipperOpeningTimeout() { |
milind-u | 37dc40b | 2022-03-08 19:51:27 -0800 | [diff] [blame] | 172 | return std::chrono::milliseconds(1000); |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 173 | } |
| 174 | // Don't use flipper velocity readings more than this amount of time in the |
| 175 | // past |
| 176 | static constexpr std::chrono::milliseconds kFlipperVelocityValidTime() { |
| 177 | return std::chrono::milliseconds(100); |
| 178 | } |
| 179 | |
Griffin Bui | bcbef48 | 2022-02-23 15:32:10 -0800 | [diff] [blame] | 180 | // TODO: (Griffin) this needs to be set |
| 181 | static constexpr ::frc971::constants::Range kFlipperArmRange() { |
| 182 | return ::frc971::constants::Range{ |
Austin Schuh | 465a288 | 2022-03-05 15:39:04 -0800 | [diff] [blame] | 183 | .lower_hard = -0.01, .upper_hard = 0.4, .lower = 0.0, .upper = 0.5}; |
Griffin Bui | bcbef48 | 2022-02-23 15:32:10 -0800 | [diff] [blame] | 184 | } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 185 | // Position of the flippers when they are open |
Austin Schuh | 6b1e4d9 | 2022-03-12 12:02:46 -0800 | [diff] [blame] | 186 | static constexpr double kFlipperOpenPosition() { return 0.20; } |
Ravago Jones | 5da0635 | 2022-03-04 20:26:24 -0800 | [diff] [blame] | 187 | // If the flippers were open but now moved back, reseat the ball if they go |
| 188 | // below this position |
Austin Schuh | 465a288 | 2022-03-05 15:39:04 -0800 | [diff] [blame] | 189 | static constexpr double kReseatFlipperPosition() { return 0.1; } |
Griffin Bui | bcbef48 | 2022-02-23 15:32:10 -0800 | [diff] [blame] | 190 | |
| 191 | static constexpr double kFlipperArmsPotRatio() { return 16.0 / 36.0; } |
| 192 | |
Nathan Leong | 342b85e | 2023-01-08 13:49:33 -0800 | [diff] [blame] | 193 | static constexpr double kFlipperArmsPotRadiansPerVolt() { |
| 194 | return kFlipperArmsPotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) * |
| 195 | (2 * M_PI /*radians*/); |
| 196 | } |
| 197 | |
Griffin Bui | bcbef48 | 2022-02-23 15:32:10 -0800 | [diff] [blame] | 198 | PotConstants flipper_arm_left; |
| 199 | PotConstants flipper_arm_right; |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 200 | |
| 201 | // Catapult. |
| 202 | static constexpr double kCatapultPotRatio() { return (12.0 / 33.0); } |
| 203 | static constexpr double kCatapultEncoderRatio() { |
| 204 | return kCatapultPotRatio(); |
| 205 | } |
| 206 | static constexpr double kCatapultEncoderCountsPerRevolution() { |
| 207 | return 4096.0; |
| 208 | } |
| 209 | |
| 210 | static constexpr double kMaxCatapultEncoderPulsesPerSecond() { |
| 211 | return control_loops::superstructure::catapult::kFreeSpeed / (2.0 * M_PI) * |
| 212 | control_loops::superstructure::catapult::kOutputRatio / |
| 213 | kCatapultEncoderRatio() * kCatapultEncoderCountsPerRevolution(); |
| 214 | } |
| 215 | static constexpr ::frc971::constants::Range kCatapultRange() { |
| 216 | return ::frc971::constants::Range{ |
Milind Upadhyay | 47e0d03 | 2022-03-05 23:06:25 -0800 | [diff] [blame] | 217 | .lower_hard = -1.0, |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 218 | .upper_hard = 2.0, |
milind-u | 37dc40b | 2022-03-08 19:51:27 -0800 | [diff] [blame] | 219 | .lower = -0.91, |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 220 | .upper = 1.57, |
| 221 | }; |
| 222 | } |
| 223 | |
| 224 | PotAndAbsEncoderConstants catapult; |
milind-u | cafdd5d | 2022-03-01 19:58:57 -0800 | [diff] [blame] | 225 | |
| 226 | // TODO(milind): set this |
| 227 | static constexpr double kImuHeight() { return 0.0; } |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 228 | |
| 229 | struct ShotParams { |
| 230 | // Measured in radians |
| 231 | double shot_angle; |
| 232 | // Muzzle velocity (m/s) of the ball as it is released from the catapult. |
| 233 | double shot_velocity; |
| 234 | |
| 235 | static ShotParams BlendY(double coefficient, ShotParams a1, ShotParams a2) { |
| 236 | using ::frc971::shooter_interpolation::Blend; |
| 237 | return ShotParams{ |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 238 | .shot_angle = Blend(coefficient, a1.shot_angle, a2.shot_angle), |
| 239 | .shot_velocity = |
| 240 | Blend(coefficient, a1.shot_velocity, a2.shot_velocity), |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 241 | }; |
| 242 | } |
| 243 | }; |
| 244 | |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 245 | struct ShotVelocityParams { |
| 246 | // Speed over ground to use for shooting-on-the-fly. |
| 247 | double shot_speed_over_ground; |
| 248 | |
| 249 | static ShotVelocityParams BlendY(double coefficient, ShotVelocityParams a1, |
| 250 | ShotVelocityParams a2) { |
| 251 | using ::frc971::shooter_interpolation::Blend; |
| 252 | return ShotVelocityParams{Blend(coefficient, a1.shot_speed_over_ground, |
| 253 | a2.shot_speed_over_ground)}; |
| 254 | } |
| 255 | }; |
| 256 | |
Ravago Jones | 3283ce0 | 2022-03-09 19:31:29 -0800 | [diff] [blame] | 257 | InterpolationTable<ShotParams> shot_interpolation_table; |
James Kuszmaul | b9ba9a5 | 2022-03-31 22:16:01 -0700 | [diff] [blame] | 258 | |
| 259 | InterpolationTable<ShotVelocityParams> shot_velocity_interpolation_table; |
Milind Upadhyay | c76bb4c | 2022-09-21 09:31:48 -0700 | [diff] [blame] | 260 | |
| 261 | struct BallColorParams { |
| 262 | // Rects stored as {x, y, width, height} |
| 263 | std::array<int, 4> reference_red; |
| 264 | std::array<int, 4> reference_blue; |
| 265 | std::array<int, 4> ball_location; |
| 266 | }; |
| 267 | |
| 268 | BallColorParams ball_color; |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 269 | }; |
| 270 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 271 | // Creates and returns a Values instance for the constants. |
| 272 | // Should be called before realtime because this allocates memory. |
| 273 | // Only the first call to either of these will be used. |
| 274 | Values MakeValues(uint16_t team); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 275 | |
Henry Speiser | 55aa3ba | 2022-02-21 23:21:12 -0800 | [diff] [blame] | 276 | // Calls MakeValues with aos::network::GetTeamNumber() |
| 277 | Values MakeValues(); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 278 | |
| 279 | } // namespace constants |
| 280 | } // namespace y2022 |
| 281 | |
| 282 | #endif // Y2022_CONSTANTS_H_ |