Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 1 | #ifndef Y2024_CONSTANTS_H_ |
| 2 | #define Y2024_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" |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 11 | #include "frc971/shooter_interpolation/interpolation.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 12 | #include "frc971/zeroing/absolute_encoder.h" |
| 13 | #include "frc971/zeroing/pot_and_absolute_encoder.h" |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 14 | #include "y2024/constants/constants_generated.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 15 | #include "y2024/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 16 | #include "y2024/control_loops/superstructure/altitude/altitude_plant.h" |
| 17 | #include "y2024/control_loops/superstructure/catapult/catapult_plant.h" |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 18 | #include "y2024/control_loops/superstructure/climber/climber_plant.h" |
Austin Schuh | 3db875a | 2024-02-18 20:02:40 -0800 | [diff] [blame] | 19 | #include "y2024/control_loops/superstructure/extend/extend_plant.h" |
Niko Sohmers | b21dbdc | 2024-01-20 20:06:59 -0800 | [diff] [blame] | 20 | #include "y2024/control_loops/superstructure/intake_pivot/intake_pivot_plant.h" |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 21 | #include "y2024/control_loops/superstructure/turret/turret_plant.h" |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 22 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 23 | namespace y2024::constants { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 24 | |
| 25 | constexpr uint16_t kCompTeamNumber = 971; |
| 26 | constexpr uint16_t kPracticeTeamNumber = 9971; |
| 27 | constexpr uint16_t kCodingRobotTeamNumber = 7971; |
| 28 | |
| 29 | struct Values { |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 30 | static const int kSuperstructureCANWriterPriority = 35; |
| 31 | static const int kDrivetrainWriterPriority = 35; |
| 32 | static const int kDrivetrainTxPriority = 36; |
| 33 | static const int kDrivetrainRxPriority = 36; |
| 34 | |
| 35 | // TODO: These values will need to be changed for the 2024 robot. |
| 36 | static constexpr double kDrivetrainCyclesPerRevolution() { return 512.0; } |
| 37 | static constexpr double kDrivetrainEncoderCountsPerRevolution() { |
| 38 | return kDrivetrainCyclesPerRevolution() * 4; |
| 39 | } |
| 40 | static constexpr double kDrivetrainEncoderRatio() { return 1.0; } |
| 41 | static constexpr double kMaxDrivetrainEncoderPulsesPerSecond() { |
| 42 | return control_loops::drivetrain::kFreeSpeed / (2.0 * M_PI) * |
| 43 | control_loops::drivetrain::kHighOutputRatio / |
| 44 | constants::Values::kDrivetrainEncoderRatio() * |
| 45 | kDrivetrainEncoderCountsPerRevolution(); |
| 46 | } |
| 47 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 48 | static double DrivetrainEncoderToMeters(int32_t in) { |
| 49 | return ((static_cast<double>(in) / |
| 50 | kDrivetrainEncoderCountsPerRevolution()) * |
| 51 | (2.0 * M_PI)) * |
| 52 | kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius; |
| 53 | } |
| 54 | |
| 55 | static double DrivetrainCANEncoderToMeters(double rotations) { |
| 56 | return (rotations * (2.0 * M_PI)) * |
| 57 | control_loops::drivetrain::kHighOutputRatio; |
| 58 | } |
Niko Sohmers | b21dbdc | 2024-01-20 20:06:59 -0800 | [diff] [blame] | 59 | // TODO: (niko) add the gear ratios for the intake once we have them |
| 60 | static constexpr double kIntakePivotEncoderCountsPerRevolution() { |
| 61 | return 4096.0; |
| 62 | } |
| 63 | |
Maxwell Henderson | ce232a9 | 2024-02-18 12:17:37 -0800 | [diff] [blame] | 64 | static constexpr double kIntakePivotEncoderRatio() { return (15.0 / 24.0); } |
Niko Sohmers | b21dbdc | 2024-01-20 20:06:59 -0800 | [diff] [blame] | 65 | |
Niko Sohmers | b21dbdc | 2024-01-20 20:06:59 -0800 | [diff] [blame] | 66 | static constexpr double kMaxIntakePivotEncoderPulsesPerSecond() { |
| 67 | return control_loops::superstructure::intake_pivot::kFreeSpeed / |
| 68 | (2.0 * M_PI) * |
| 69 | control_loops::superstructure::intake_pivot::kOutputRatio / |
| 70 | kIntakePivotEncoderRatio() * |
| 71 | kIntakePivotEncoderCountsPerRevolution(); |
| 72 | } |
| 73 | |
Maxwell Henderson | ce232a9 | 2024-02-18 12:17:37 -0800 | [diff] [blame] | 74 | static constexpr double kClimberPotMetersPerRevolution() { |
Austin Schuh | b089387 | 2024-02-18 22:09:32 -0800 | [diff] [blame] | 75 | return 16 * 0.25 * 0.0254; |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Maxwell Henderson | ce232a9 | 2024-02-18 12:17:37 -0800 | [diff] [blame] | 78 | static constexpr double kClimberPotMetersPerVolt() { |
| 79 | return kClimberPotMetersPerRevolution() * (10.0 /*turns*/ / 5.0 /*volts*/); |
Filip Kujawa | 749f244 | 2024-02-04 01:12:35 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Austin Schuh | 3db875a | 2024-02-18 20:02:40 -0800 | [diff] [blame] | 82 | static constexpr double kExtendEncoderCountsPerRevolution() { return 4096.0; } |
Niko Sohmers | 4d93d4c | 2024-03-24 14:48:26 -0700 | [diff] [blame] | 83 | |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 84 | // TODO: (niko) add the gear ratios for the intake once we have them |
| 85 | static constexpr double kCatapultEncoderCountsPerRevolution() { |
| 86 | return 4096.0; |
| 87 | } |
| 88 | |
| 89 | static constexpr double kCatapultEncoderRatio() { return 12.0 / 24.0; } |
| 90 | |
| 91 | static constexpr double kCatapultPotRatio() { return 12.0 / 24.0; } |
| 92 | |
| 93 | static constexpr double kCatapultPotRadiansPerVolt() { |
| 94 | return kCatapultPotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) * |
| 95 | (2 * M_PI /*radians*/); |
| 96 | } |
| 97 | |
| 98 | static constexpr double kMaxCatapultEncoderPulsesPerSecond() { |
| 99 | return control_loops::superstructure::catapult::kFreeSpeed / (2.0 * M_PI) * |
| 100 | control_loops::superstructure::catapult::kOutputRatio / |
| 101 | kCatapultEncoderRatio() * kCatapultEncoderCountsPerRevolution(); |
| 102 | } |
Austin Schuh | 3db875a | 2024-02-18 20:02:40 -0800 | [diff] [blame] | 103 | |
| 104 | static constexpr double kExtendEncoderRatio() { return 1.0; } |
| 105 | |
| 106 | static constexpr double kExtendPotMetersPerRevolution() { |
| 107 | return 36 * 0.005 * kExtendEncoderRatio(); |
| 108 | } |
Maxwell Henderson | 9e3b310 | 2024-02-20 15:35:02 -0800 | [diff] [blame] | 109 | static constexpr double kExtendEncoderMetersPerRadian() { |
| 110 | return kExtendPotMetersPerRevolution() / 2.0 / M_PI; |
Austin Schuh | 3db875a | 2024-02-18 20:02:40 -0800 | [diff] [blame] | 111 | } |
| 112 | static constexpr double kExtendPotMetersPerVolt() { |
| 113 | return kExtendPotMetersPerRevolution() * (5.0 /*turns*/ / 5.0 /*volts*/); |
| 114 | } |
| 115 | static constexpr double kMaxExtendEncoderPulsesPerSecond() { |
| 116 | return control_loops::superstructure::extend::kFreeSpeed / (2.0 * M_PI) * |
| 117 | control_loops::superstructure::extend::kOutputRatio / |
| 118 | kExtendEncoderRatio() * kExtendEncoderCountsPerRevolution(); |
| 119 | } |
Filip Kujawa | ce385c3 | 2024-02-16 10:39:49 -0800 | [diff] [blame] | 120 | |
Niko Sohmers | 27d92c6 | 2024-02-19 14:15:07 -0800 | [diff] [blame] | 121 | static constexpr double kTurretEncoderCountsPerRevolution() { return 4096.0; } |
| 122 | |
| 123 | static constexpr double kTurretPotRatio() { |
| 124 | return (22.0 / 100.0) * (28.0 / 48.0) * (36.0 / 24.0); |
| 125 | } |
| 126 | |
| 127 | static constexpr double kTurretEncoderRatio() { return 22.0 / 100.0; } |
| 128 | |
| 129 | static constexpr double kTurretPotRadiansPerVolt() { |
| 130 | return kTurretPotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) * |
| 131 | (2 * M_PI /*radians*/); |
| 132 | } |
| 133 | static constexpr double kMaxTurretEncoderPulsesPerSecond() { |
| 134 | return control_loops::superstructure::turret::kFreeSpeed / (2.0 * M_PI) * |
| 135 | control_loops::superstructure::turret::kOutputRatio / |
| 136 | kTurretEncoderRatio() * kTurretEncoderCountsPerRevolution(); |
| 137 | } |
| 138 | |
| 139 | static constexpr double kAltitudeEncoderCountsPerRevolution() { |
| 140 | return 4096.0; |
| 141 | } |
| 142 | |
| 143 | static constexpr double kAltitudeEncoderRatio() { return 16.0 / 162.0; } |
| 144 | |
| 145 | static constexpr double kAltitudePotRatio() { return 16.0 / 162.0; } |
| 146 | |
| 147 | static constexpr double kAltitudePotRadiansPerVolt() { |
| 148 | return kAltitudePotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) * |
| 149 | (2 * M_PI /*radians*/); |
| 150 | } |
| 151 | static constexpr double kMaxAltitudeEncoderPulsesPerSecond() { |
| 152 | return control_loops::superstructure::altitude::kFreeSpeed / (2.0 * M_PI) * |
| 153 | control_loops::superstructure::altitude::kOutputRatio / |
| 154 | kAltitudeEncoderRatio() * kAltitudeEncoderCountsPerRevolution(); |
| 155 | } |
| 156 | |
Filip Kujawa | ce385c3 | 2024-02-16 10:39:49 -0800 | [diff] [blame] | 157 | // 20 -> 28 reduction to a 0.5" radius roller |
| 158 | static constexpr double kTransferRollerOutputRatio = (20.0 / 28.0) * 0.0127; |
| 159 | // 20 -> 34 reduction, and the 34 is on a 0.625" radius roller |
| 160 | static constexpr double kIntakeRollerOutputRatio = (20.0 / 34.0) * 0.015875; |
| 161 | // 20 -> 28 reduction to a 0.5" radius roller |
| 162 | static constexpr double kExtendRollerOutputRatio = (20.0 / 28.0) * 0.0127; |
| 163 | |
James Kuszmaul | 73d6888 | 2024-04-07 21:26:17 -0700 | [diff] [blame] | 164 | struct NoteParams { |
| 165 | // Measured in radians |
| 166 | double turret_offset = 0.0; |
| 167 | |
| 168 | static NoteParams BlendY(double coefficient, NoteParams a1, NoteParams a2) { |
| 169 | using ::frc971::shooter_interpolation::Blend; |
| 170 | return NoteParams{.turret_offset = Blend(coefficient, a1.turret_offset, |
| 171 | a2.turret_offset)}; |
| 172 | } |
| 173 | |
| 174 | static NoteParams FromFlatbuffer(const y2024::NoteParams *note_params) { |
| 175 | return NoteParams{ |
| 176 | .turret_offset = note_params->turret_offset(), |
| 177 | }; |
| 178 | } |
| 179 | }; |
| 180 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 181 | struct ShotParams { |
| 182 | // Measured in radians |
| 183 | double shot_altitude_angle = 0.0; |
| 184 | double shot_catapult_angle = 0.0; |
| 185 | |
| 186 | // Muzzle velocity (m/s) of the game piece as it is released from the |
| 187 | // catapult. |
| 188 | double shot_velocity = 0.0; |
| 189 | |
| 190 | // Speed over ground to use for shooting on the fly |
| 191 | double shot_speed_over_ground = 0.0; |
| 192 | |
| 193 | static ShotParams BlendY(double coefficient, ShotParams a1, ShotParams a2) { |
| 194 | using ::frc971::shooter_interpolation::Blend; |
| 195 | return ShotParams{ |
| 196 | .shot_altitude_angle = Blend(coefficient, a1.shot_altitude_angle, |
| 197 | a2.shot_altitude_angle), |
| 198 | .shot_catapult_angle = Blend(coefficient, a1.shot_catapult_angle, |
| 199 | a2.shot_catapult_angle), |
| 200 | .shot_velocity = |
| 201 | Blend(coefficient, a1.shot_velocity, a2.shot_velocity), |
| 202 | .shot_speed_over_ground = |
| 203 | Blend(coefficient, a1.shot_speed_over_ground, |
| 204 | a2.shot_speed_over_ground), |
| 205 | }; |
| 206 | } |
| 207 | |
| 208 | static ShotParams FromFlatbuffer(const y2024::ShotParams *shot_params) { |
| 209 | return ShotParams{ |
| 210 | .shot_altitude_angle = shot_params->shot_altitude_angle(), |
| 211 | .shot_catapult_angle = shot_params->shot_catapult_angle(), |
| 212 | .shot_velocity = shot_params->shot_velocity(), |
| 213 | .shot_speed_over_ground = shot_params->shot_speed_over_ground()}; |
| 214 | } |
| 215 | }; |
| 216 | |
James Kuszmaul | 73d6888 | 2024-04-07 21:26:17 -0700 | [diff] [blame] | 217 | static frc971::shooter_interpolation::InterpolationTable<NoteParams> |
| 218 | NoteInterpolationTableFromFlatbuffer( |
| 219 | const flatbuffers::Vector< |
| 220 | flatbuffers::Offset<y2024::NoteInterpolationTablePoint>> *table) { |
| 221 | std::vector<std::pair<double, NoteParams>> interpolation_table; |
| 222 | |
| 223 | for (const NoteInterpolationTablePoint *point : *table) { |
| 224 | interpolation_table.emplace_back( |
| 225 | point->amperage(), NoteParams::FromFlatbuffer(point->note_params())); |
| 226 | } |
| 227 | |
| 228 | return frc971::shooter_interpolation::InterpolationTable<NoteParams>( |
| 229 | interpolation_table); |
| 230 | } |
| 231 | |
Niko Sohmers | c4d2c50 | 2024-02-19 19:35:35 -0800 | [diff] [blame] | 232 | static frc971::shooter_interpolation::InterpolationTable<ShotParams> |
| 233 | InterpolationTableFromFlatbuffer( |
| 234 | const flatbuffers::Vector< |
| 235 | flatbuffers::Offset<y2024::InterpolationTablePoint>> *table) { |
| 236 | std::vector<std::pair<double, ShotParams>> interpolation_table; |
| 237 | |
| 238 | for (const InterpolationTablePoint *point : *table) { |
| 239 | interpolation_table.emplace_back( |
| 240 | point->distance_from_goal(), |
| 241 | ShotParams::FromFlatbuffer(point->shot_params())); |
| 242 | } |
| 243 | |
| 244 | return frc971::shooter_interpolation::InterpolationTable<ShotParams>( |
| 245 | interpolation_table); |
| 246 | } |
| 247 | |
Niko Sohmers | b21dbdc | 2024-01-20 20:06:59 -0800 | [diff] [blame] | 248 | struct PotAndAbsEncoderConstants { |
| 249 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 250 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 251 | subsystem_params; |
| 252 | double potentiometer_offset; |
| 253 | }; |
Niko Sohmers | 74b0ad5 | 2024-02-03 18:00:31 -0800 | [diff] [blame] | 254 | |
| 255 | struct AbsoluteEncoderConstants { |
| 256 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 257 | ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> |
| 258 | subsystem_params; |
| 259 | }; |
Niko Sohmers | 4d93d4c | 2024-03-24 14:48:26 -0700 | [diff] [blame] | 260 | |
| 261 | struct PotConstants { |
| 262 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 263 | ::frc971::zeroing::RelativeEncoderZeroingEstimator> |
| 264 | subsystem_params; |
| 265 | double potentiometer_offset; |
| 266 | }; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | // Creates and returns a Values instance for the constants. |
| 270 | // Should be called before realtime because this allocates memory. |
| 271 | // Only the first call to either of these will be used. |
| 272 | constants::Values MakeValues(uint16_t team); |
| 273 | |
| 274 | // Calls MakeValues with aos::network::GetTeamNumber() |
| 275 | constants::Values MakeValues(); |
| 276 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 277 | } // namespace y2024::constants |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 278 | |
| 279 | #endif // Y2024_CONSTANTS_H_ |