blob: 9923660733ca956f9c000c4907ff0c0225a94b86 [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#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 Sohmersc4d2c502024-02-19 19:35:35 -080011#include "frc971/shooter_interpolation/interpolation.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080012#include "frc971/zeroing/absolute_encoder.h"
13#include "frc971/zeroing/pot_and_absolute_encoder.h"
Niko Sohmersc4d2c502024-02-19 19:35:35 -080014#include "y2024/constants/constants_generated.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080015#include "y2024/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Niko Sohmers27d92c62024-02-19 14:15:07 -080016#include "y2024/control_loops/superstructure/altitude/altitude_plant.h"
17#include "y2024/control_loops/superstructure/catapult/catapult_plant.h"
Filip Kujawa749f2442024-02-04 01:12:35 -080018#include "y2024/control_loops/superstructure/climber/climber_plant.h"
Austin Schuh3db875a2024-02-18 20:02:40 -080019#include "y2024/control_loops/superstructure/extend/extend_plant.h"
Niko Sohmersb21dbdc2024-01-20 20:06:59 -080020#include "y2024/control_loops/superstructure/intake_pivot/intake_pivot_plant.h"
Niko Sohmers27d92c62024-02-19 14:15:07 -080021#include "y2024/control_loops/superstructure/turret/turret_plant.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080022
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080023namespace y2024::constants {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080024
25constexpr uint16_t kCompTeamNumber = 971;
26constexpr uint16_t kPracticeTeamNumber = 9971;
27constexpr uint16_t kCodingRobotTeamNumber = 7971;
28
29struct Values {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080030 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 Sohmers3860f8a2024-01-12 21:05:19 -080048 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 Sohmersb21dbdc2024-01-20 20:06:59 -080059 // 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 Hendersonce232a92024-02-18 12:17:37 -080064 static constexpr double kIntakePivotEncoderRatio() { return (15.0 / 24.0); }
Niko Sohmersb21dbdc2024-01-20 20:06:59 -080065
Niko Sohmersb21dbdc2024-01-20 20:06:59 -080066 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
Filip Kujawa749f2442024-02-04 01:12:35 -080074 static constexpr double kClimberEncoderCountsPerRevolution() {
75 return 4096.0;
76 }
77
Maxwell Hendersonce232a92024-02-18 12:17:37 -080078 static constexpr double kClimberEncoderRatio() { return (16.0 / 60.0); }
79
80 static constexpr double kClimberPotMetersPerRevolution() {
Austin Schuhb0893872024-02-18 22:09:32 -080081 return 16 * 0.25 * 0.0254;
Filip Kujawa749f2442024-02-04 01:12:35 -080082 }
83
Maxwell Henderson9e3b3102024-02-20 15:35:02 -080084 static constexpr double kClimberEncoderMetersPerRadian() {
85 return kClimberEncoderRatio() * kClimberPotMetersPerRevolution() / 2.0 /
86 M_PI;
Maxwell Hendersonce232a92024-02-18 12:17:37 -080087 }
Filip Kujawa749f2442024-02-04 01:12:35 -080088
Maxwell Hendersonce232a92024-02-18 12:17:37 -080089 static constexpr double kClimberPotMetersPerVolt() {
90 return kClimberPotMetersPerRevolution() * (10.0 /*turns*/ / 5.0 /*volts*/);
Filip Kujawa749f2442024-02-04 01:12:35 -080091 }
92
93 static constexpr double kMaxClimberEncoderPulsesPerSecond() {
94 return control_loops::superstructure::climber::kFreeSpeed / (2.0 * M_PI) *
95 control_loops::superstructure::climber::kOutputRatio /
96 kClimberEncoderRatio() * kClimberEncoderCountsPerRevolution();
97 }
98
Austin Schuh3db875a2024-02-18 20:02:40 -080099 static constexpr double kExtendEncoderCountsPerRevolution() { return 4096.0; }
Niko Sohmers27d92c62024-02-19 14:15:07 -0800100 // TODO: (niko) add the gear ratios for the intake once we have them
101 static constexpr double kCatapultEncoderCountsPerRevolution() {
102 return 4096.0;
103 }
104
105 static constexpr double kCatapultEncoderRatio() { return 12.0 / 24.0; }
106
107 static constexpr double kCatapultPotRatio() { return 12.0 / 24.0; }
108
109 static constexpr double kCatapultPotRadiansPerVolt() {
110 return kCatapultPotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) *
111 (2 * M_PI /*radians*/);
112 }
113
114 static constexpr double kMaxCatapultEncoderPulsesPerSecond() {
115 return control_loops::superstructure::catapult::kFreeSpeed / (2.0 * M_PI) *
116 control_loops::superstructure::catapult::kOutputRatio /
117 kCatapultEncoderRatio() * kCatapultEncoderCountsPerRevolution();
118 }
Austin Schuh3db875a2024-02-18 20:02:40 -0800119
120 static constexpr double kExtendEncoderRatio() { return 1.0; }
121
122 static constexpr double kExtendPotMetersPerRevolution() {
123 return 36 * 0.005 * kExtendEncoderRatio();
124 }
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800125 static constexpr double kExtendEncoderMetersPerRadian() {
126 return kExtendPotMetersPerRevolution() / 2.0 / M_PI;
Austin Schuh3db875a2024-02-18 20:02:40 -0800127 }
128 static constexpr double kExtendPotMetersPerVolt() {
129 return kExtendPotMetersPerRevolution() * (5.0 /*turns*/ / 5.0 /*volts*/);
130 }
131 static constexpr double kMaxExtendEncoderPulsesPerSecond() {
132 return control_loops::superstructure::extend::kFreeSpeed / (2.0 * M_PI) *
133 control_loops::superstructure::extend::kOutputRatio /
134 kExtendEncoderRatio() * kExtendEncoderCountsPerRevolution();
135 }
Filip Kujawace385c32024-02-16 10:39:49 -0800136
Niko Sohmers27d92c62024-02-19 14:15:07 -0800137 static constexpr double kTurretEncoderCountsPerRevolution() { return 4096.0; }
138
139 static constexpr double kTurretPotRatio() {
140 return (22.0 / 100.0) * (28.0 / 48.0) * (36.0 / 24.0);
141 }
142
143 static constexpr double kTurretEncoderRatio() { return 22.0 / 100.0; }
144
145 static constexpr double kTurretPotRadiansPerVolt() {
146 return kTurretPotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) *
147 (2 * M_PI /*radians*/);
148 }
149 static constexpr double kMaxTurretEncoderPulsesPerSecond() {
150 return control_loops::superstructure::turret::kFreeSpeed / (2.0 * M_PI) *
151 control_loops::superstructure::turret::kOutputRatio /
152 kTurretEncoderRatio() * kTurretEncoderCountsPerRevolution();
153 }
154
155 static constexpr double kAltitudeEncoderCountsPerRevolution() {
156 return 4096.0;
157 }
158
159 static constexpr double kAltitudeEncoderRatio() { return 16.0 / 162.0; }
160
161 static constexpr double kAltitudePotRatio() { return 16.0 / 162.0; }
162
163 static constexpr double kAltitudePotRadiansPerVolt() {
164 return kAltitudePotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) *
165 (2 * M_PI /*radians*/);
166 }
167 static constexpr double kMaxAltitudeEncoderPulsesPerSecond() {
168 return control_loops::superstructure::altitude::kFreeSpeed / (2.0 * M_PI) *
169 control_loops::superstructure::altitude::kOutputRatio /
170 kAltitudeEncoderRatio() * kAltitudeEncoderCountsPerRevolution();
171 }
172
Filip Kujawace385c32024-02-16 10:39:49 -0800173 // 20 -> 28 reduction to a 0.5" radius roller
174 static constexpr double kTransferRollerOutputRatio = (20.0 / 28.0) * 0.0127;
175 // 20 -> 34 reduction, and the 34 is on a 0.625" radius roller
176 static constexpr double kIntakeRollerOutputRatio = (20.0 / 34.0) * 0.015875;
177 // 20 -> 28 reduction to a 0.5" radius roller
178 static constexpr double kExtendRollerOutputRatio = (20.0 / 28.0) * 0.0127;
179
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800180 struct ShotParams {
181 // Measured in radians
182 double shot_altitude_angle = 0.0;
183 double shot_catapult_angle = 0.0;
184
185 // Muzzle velocity (m/s) of the game piece as it is released from the
186 // catapult.
187 double shot_velocity = 0.0;
188
189 // Speed over ground to use for shooting on the fly
190 double shot_speed_over_ground = 0.0;
191
192 static ShotParams BlendY(double coefficient, ShotParams a1, ShotParams a2) {
193 using ::frc971::shooter_interpolation::Blend;
194 return ShotParams{
195 .shot_altitude_angle = Blend(coefficient, a1.shot_altitude_angle,
196 a2.shot_altitude_angle),
197 .shot_catapult_angle = Blend(coefficient, a1.shot_catapult_angle,
198 a2.shot_catapult_angle),
199 .shot_velocity =
200 Blend(coefficient, a1.shot_velocity, a2.shot_velocity),
201 .shot_speed_over_ground =
202 Blend(coefficient, a1.shot_speed_over_ground,
203 a2.shot_speed_over_ground),
204 };
205 }
206
207 static ShotParams FromFlatbuffer(const y2024::ShotParams *shot_params) {
208 return ShotParams{
209 .shot_altitude_angle = shot_params->shot_altitude_angle(),
210 .shot_catapult_angle = shot_params->shot_catapult_angle(),
211 .shot_velocity = shot_params->shot_velocity(),
212 .shot_speed_over_ground = shot_params->shot_speed_over_ground()};
213 }
214 };
215
216 static frc971::shooter_interpolation::InterpolationTable<ShotParams>
217 InterpolationTableFromFlatbuffer(
218 const flatbuffers::Vector<
219 flatbuffers::Offset<y2024::InterpolationTablePoint>> *table) {
220 std::vector<std::pair<double, ShotParams>> interpolation_table;
221
222 for (const InterpolationTablePoint *point : *table) {
223 interpolation_table.emplace_back(
224 point->distance_from_goal(),
225 ShotParams::FromFlatbuffer(point->shot_params()));
226 }
227
228 return frc971::shooter_interpolation::InterpolationTable<ShotParams>(
229 interpolation_table);
230 }
231
Niko Sohmersb21dbdc2024-01-20 20:06:59 -0800232 struct PotAndAbsEncoderConstants {
233 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
234 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
235 subsystem_params;
236 double potentiometer_offset;
237 };
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800238
239 struct AbsoluteEncoderConstants {
240 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
241 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator>
242 subsystem_params;
243 };
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800244};
245
246// Creates and returns a Values instance for the constants.
247// Should be called before realtime because this allocates memory.
248// Only the first call to either of these will be used.
249constants::Values MakeValues(uint16_t team);
250
251// Calls MakeValues with aos::network::GetTeamNumber()
252constants::Values MakeValues();
253
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800254} // namespace y2024::constants
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800255
256#endif // Y2024_CONSTANTS_H_