blob: 0dce6854adaebc0c20084402783b7332ae3a345f [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 Hendersonce232a92024-02-18 12:17:37 -080084 static constexpr double kClimberEncoderMetersPerRevolution() {
Austin Schuhb0893872024-02-18 22:09:32 -080085 return kClimberEncoderRatio() * kClimberPotMetersPerRevolution();
Maxwell Hendersonce232a92024-02-18 12:17:37 -080086 }
Filip Kujawa749f2442024-02-04 01:12:35 -080087
Maxwell Hendersonce232a92024-02-18 12:17:37 -080088 static constexpr double kClimberPotMetersPerVolt() {
89 return kClimberPotMetersPerRevolution() * (10.0 /*turns*/ / 5.0 /*volts*/);
Filip Kujawa749f2442024-02-04 01:12:35 -080090 }
91
92 static constexpr double kMaxClimberEncoderPulsesPerSecond() {
93 return control_loops::superstructure::climber::kFreeSpeed / (2.0 * M_PI) *
94 control_loops::superstructure::climber::kOutputRatio /
95 kClimberEncoderRatio() * kClimberEncoderCountsPerRevolution();
96 }
97
Austin Schuh3db875a2024-02-18 20:02:40 -080098 static constexpr double kExtendEncoderCountsPerRevolution() { return 4096.0; }
Niko Sohmers27d92c62024-02-19 14:15:07 -080099 // TODO: (niko) add the gear ratios for the intake once we have them
100 static constexpr double kCatapultEncoderCountsPerRevolution() {
101 return 4096.0;
102 }
103
104 static constexpr double kCatapultEncoderRatio() { return 12.0 / 24.0; }
105
106 static constexpr double kCatapultPotRatio() { return 12.0 / 24.0; }
107
108 static constexpr double kCatapultPotRadiansPerVolt() {
109 return kCatapultPotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) *
110 (2 * M_PI /*radians*/);
111 }
112
113 static constexpr double kMaxCatapultEncoderPulsesPerSecond() {
114 return control_loops::superstructure::catapult::kFreeSpeed / (2.0 * M_PI) *
115 control_loops::superstructure::catapult::kOutputRatio /
116 kCatapultEncoderRatio() * kCatapultEncoderCountsPerRevolution();
117 }
Austin Schuh3db875a2024-02-18 20:02:40 -0800118
119 static constexpr double kExtendEncoderRatio() { return 1.0; }
120
121 static constexpr double kExtendPotMetersPerRevolution() {
122 return 36 * 0.005 * kExtendEncoderRatio();
123 }
124 static constexpr double kExtendEncoderMetersPerRevolution() {
125 return kExtendPotMetersPerRevolution();
126 }
127 static constexpr double kExtendPotMetersPerVolt() {
128 return kExtendPotMetersPerRevolution() * (5.0 /*turns*/ / 5.0 /*volts*/);
129 }
130 static constexpr double kMaxExtendEncoderPulsesPerSecond() {
131 return control_loops::superstructure::extend::kFreeSpeed / (2.0 * M_PI) *
132 control_loops::superstructure::extend::kOutputRatio /
133 kExtendEncoderRatio() * kExtendEncoderCountsPerRevolution();
134 }
Filip Kujawace385c32024-02-16 10:39:49 -0800135
Niko Sohmers27d92c62024-02-19 14:15:07 -0800136 static constexpr double kTurretEncoderCountsPerRevolution() { return 4096.0; }
137
138 static constexpr double kTurretPotRatio() {
139 return (22.0 / 100.0) * (28.0 / 48.0) * (36.0 / 24.0);
140 }
141
142 static constexpr double kTurretEncoderRatio() { return 22.0 / 100.0; }
143
144 static constexpr double kTurretPotRadiansPerVolt() {
145 return kTurretPotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) *
146 (2 * M_PI /*radians*/);
147 }
148 static constexpr double kMaxTurretEncoderPulsesPerSecond() {
149 return control_loops::superstructure::turret::kFreeSpeed / (2.0 * M_PI) *
150 control_loops::superstructure::turret::kOutputRatio /
151 kTurretEncoderRatio() * kTurretEncoderCountsPerRevolution();
152 }
153
154 static constexpr double kAltitudeEncoderCountsPerRevolution() {
155 return 4096.0;
156 }
157
158 static constexpr double kAltitudeEncoderRatio() { return 16.0 / 162.0; }
159
160 static constexpr double kAltitudePotRatio() { return 16.0 / 162.0; }
161
162 static constexpr double kAltitudePotRadiansPerVolt() {
163 return kAltitudePotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) *
164 (2 * M_PI /*radians*/);
165 }
166 static constexpr double kMaxAltitudeEncoderPulsesPerSecond() {
167 return control_loops::superstructure::altitude::kFreeSpeed / (2.0 * M_PI) *
168 control_loops::superstructure::altitude::kOutputRatio /
169 kAltitudeEncoderRatio() * kAltitudeEncoderCountsPerRevolution();
170 }
171
Filip Kujawace385c32024-02-16 10:39:49 -0800172 // 20 -> 28 reduction to a 0.5" radius roller
173 static constexpr double kTransferRollerOutputRatio = (20.0 / 28.0) * 0.0127;
174 // 20 -> 34 reduction, and the 34 is on a 0.625" radius roller
175 static constexpr double kIntakeRollerOutputRatio = (20.0 / 34.0) * 0.015875;
176 // 20 -> 28 reduction to a 0.5" radius roller
177 static constexpr double kExtendRollerOutputRatio = (20.0 / 28.0) * 0.0127;
178
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800179 struct ShotParams {
180 // Measured in radians
181 double shot_altitude_angle = 0.0;
182 double shot_catapult_angle = 0.0;
183
184 // Muzzle velocity (m/s) of the game piece as it is released from the
185 // catapult.
186 double shot_velocity = 0.0;
187
188 // Speed over ground to use for shooting on the fly
189 double shot_speed_over_ground = 0.0;
190
191 static ShotParams BlendY(double coefficient, ShotParams a1, ShotParams a2) {
192 using ::frc971::shooter_interpolation::Blend;
193 return ShotParams{
194 .shot_altitude_angle = Blend(coefficient, a1.shot_altitude_angle,
195 a2.shot_altitude_angle),
196 .shot_catapult_angle = Blend(coefficient, a1.shot_catapult_angle,
197 a2.shot_catapult_angle),
198 .shot_velocity =
199 Blend(coefficient, a1.shot_velocity, a2.shot_velocity),
200 .shot_speed_over_ground =
201 Blend(coefficient, a1.shot_speed_over_ground,
202 a2.shot_speed_over_ground),
203 };
204 }
205
206 static ShotParams FromFlatbuffer(const y2024::ShotParams *shot_params) {
207 return ShotParams{
208 .shot_altitude_angle = shot_params->shot_altitude_angle(),
209 .shot_catapult_angle = shot_params->shot_catapult_angle(),
210 .shot_velocity = shot_params->shot_velocity(),
211 .shot_speed_over_ground = shot_params->shot_speed_over_ground()};
212 }
213 };
214
215 static frc971::shooter_interpolation::InterpolationTable<ShotParams>
216 InterpolationTableFromFlatbuffer(
217 const flatbuffers::Vector<
218 flatbuffers::Offset<y2024::InterpolationTablePoint>> *table) {
219 std::vector<std::pair<double, ShotParams>> interpolation_table;
220
221 for (const InterpolationTablePoint *point : *table) {
222 interpolation_table.emplace_back(
223 point->distance_from_goal(),
224 ShotParams::FromFlatbuffer(point->shot_params()));
225 }
226
227 return frc971::shooter_interpolation::InterpolationTable<ShotParams>(
228 interpolation_table);
229 }
230
Niko Sohmersb21dbdc2024-01-20 20:06:59 -0800231 struct PotAndAbsEncoderConstants {
232 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
233 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
234 subsystem_params;
235 double potentiometer_offset;
236 };
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800237
238 struct AbsoluteEncoderConstants {
239 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
240 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator>
241 subsystem_params;
242 };
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800243};
244
245// Creates and returns a Values instance for the constants.
246// Should be called before realtime because this allocates memory.
247// Only the first call to either of these will be used.
248constants::Values MakeValues(uint16_t team);
249
250// Calls MakeValues with aos::network::GetTeamNumber()
251constants::Values MakeValues();
252
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800253} // namespace y2024::constants
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800254
255#endif // Y2024_CONSTANTS_H_