blob: 4cb53787858c25df25c82bb99c346b1a0e58d160 [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
Maxwell Hendersonce232a92024-02-18 12:17:37 -080074 static constexpr double kClimberPotMetersPerRevolution() {
Austin Schuhb0893872024-02-18 22:09:32 -080075 return 16 * 0.25 * 0.0254;
Filip Kujawa749f2442024-02-04 01:12:35 -080076 }
77
Maxwell Hendersonce232a92024-02-18 12:17:37 -080078 static constexpr double kClimberPotMetersPerVolt() {
79 return kClimberPotMetersPerRevolution() * (10.0 /*turns*/ / 5.0 /*volts*/);
Filip Kujawa749f2442024-02-04 01:12:35 -080080 }
81
Austin Schuh3db875a2024-02-18 20:02:40 -080082 static constexpr double kExtendEncoderCountsPerRevolution() { return 4096.0; }
Niko Sohmers4d93d4c2024-03-24 14:48:26 -070083
Niko Sohmers27d92c62024-02-19 14:15:07 -080084 // 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 Schuh3db875a2024-02-18 20:02:40 -0800103
104 static constexpr double kExtendEncoderRatio() { return 1.0; }
105
106 static constexpr double kExtendPotMetersPerRevolution() {
107 return 36 * 0.005 * kExtendEncoderRatio();
108 }
Maxwell Henderson9e3b3102024-02-20 15:35:02 -0800109 static constexpr double kExtendEncoderMetersPerRadian() {
110 return kExtendPotMetersPerRevolution() / 2.0 / M_PI;
Austin Schuh3db875a2024-02-18 20:02:40 -0800111 }
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 Kujawace385c32024-02-16 10:39:49 -0800120
Niko Sohmers27d92c62024-02-19 14:15:07 -0800121 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 Kujawace385c32024-02-16 10:39:49 -0800157 // 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
Niko Sohmersc4d2c502024-02-19 19:35:35 -0800164 struct ShotParams {
165 // Measured in radians
166 double shot_altitude_angle = 0.0;
167 double shot_catapult_angle = 0.0;
168
169 // Muzzle velocity (m/s) of the game piece as it is released from the
170 // catapult.
171 double shot_velocity = 0.0;
172
173 // Speed over ground to use for shooting on the fly
174 double shot_speed_over_ground = 0.0;
175
176 static ShotParams BlendY(double coefficient, ShotParams a1, ShotParams a2) {
177 using ::frc971::shooter_interpolation::Blend;
178 return ShotParams{
179 .shot_altitude_angle = Blend(coefficient, a1.shot_altitude_angle,
180 a2.shot_altitude_angle),
181 .shot_catapult_angle = Blend(coefficient, a1.shot_catapult_angle,
182 a2.shot_catapult_angle),
183 .shot_velocity =
184 Blend(coefficient, a1.shot_velocity, a2.shot_velocity),
185 .shot_speed_over_ground =
186 Blend(coefficient, a1.shot_speed_over_ground,
187 a2.shot_speed_over_ground),
188 };
189 }
190
191 static ShotParams FromFlatbuffer(const y2024::ShotParams *shot_params) {
192 return ShotParams{
193 .shot_altitude_angle = shot_params->shot_altitude_angle(),
194 .shot_catapult_angle = shot_params->shot_catapult_angle(),
195 .shot_velocity = shot_params->shot_velocity(),
196 .shot_speed_over_ground = shot_params->shot_speed_over_ground()};
197 }
198 };
199
200 static frc971::shooter_interpolation::InterpolationTable<ShotParams>
201 InterpolationTableFromFlatbuffer(
202 const flatbuffers::Vector<
203 flatbuffers::Offset<y2024::InterpolationTablePoint>> *table) {
204 std::vector<std::pair<double, ShotParams>> interpolation_table;
205
206 for (const InterpolationTablePoint *point : *table) {
207 interpolation_table.emplace_back(
208 point->distance_from_goal(),
209 ShotParams::FromFlatbuffer(point->shot_params()));
210 }
211
212 return frc971::shooter_interpolation::InterpolationTable<ShotParams>(
213 interpolation_table);
214 }
215
Niko Sohmersb21dbdc2024-01-20 20:06:59 -0800216 struct PotAndAbsEncoderConstants {
217 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
218 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
219 subsystem_params;
220 double potentiometer_offset;
221 };
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800222
223 struct AbsoluteEncoderConstants {
224 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
225 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator>
226 subsystem_params;
227 };
Niko Sohmers4d93d4c2024-03-24 14:48:26 -0700228
229 struct PotConstants {
230 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
231 ::frc971::zeroing::RelativeEncoderZeroingEstimator>
232 subsystem_params;
233 double potentiometer_offset;
234 };
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800235};
236
237// Creates and returns a Values instance for the constants.
238// Should be called before realtime because this allocates memory.
239// Only the first call to either of these will be used.
240constants::Values MakeValues(uint16_t team);
241
242// Calls MakeValues with aos::network::GetTeamNumber()
243constants::Values MakeValues();
244
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800245} // namespace y2024::constants
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800246
247#endif // Y2024_CONSTANTS_H_