blob: b89613fcd0d994a13c1756d9f10a21694d51e726 [file] [log] [blame]
milind-u086d7262022-01-19 20:44:18 -08001#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 Jones3283ce02022-03-09 19:31:29 -080011#include "frc971/shooter_interpolation/interpolation.h"
James Kuszmaulec635d22023-08-12 18:39:24 -070012#include "frc971/zeroing/pot_and_absolute_encoder.h"
milind-u086d7262022-01-19 20:44:18 -080013#include "y2022/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Austin Schuh39f26f62022-02-24 21:34:46 -080014#include "y2022/control_loops/superstructure/catapult/catapult_plant.h"
Henry Speiser55aa3ba2022-02-21 23:21:12 -080015#include "y2022/control_loops/superstructure/climber/climber_plant.h"
Yash Chainani997a7492022-01-29 15:48:56 -080016#include "y2022/control_loops/superstructure/intake/intake_plant.h"
Henry Speiser55aa3ba2022-02-21 23:21:12 -080017#include "y2022/control_loops/superstructure/turret/turret_plant.h"
milind-u086d7262022-01-19 20:44:18 -080018
Ravago Jones3283ce02022-03-09 19:31:29 -080019using ::frc971::shooter_interpolation::InterpolationTable;
20
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080021namespace y2022::constants {
milind-u086d7262022-01-19 20:44:18 -080022
Milind Upadhyayc76bb4c2022-09-21 09:31:48 -070023constexpr uint16_t kCompTeamNumber = 971;
24constexpr uint16_t kPracticeTeamNumber = 9971;
25constexpr uint16_t kCodingRobotTeamNumber = 7971;
26
milind-u086d7262022-01-19 20:44:18 -080027struct Values {
28 static const int kZeroingSampleSize = 200;
29
30 static constexpr double kDrivetrainCyclesPerRevolution() { return 512.0; }
31 static constexpr double kDrivetrainEncoderCountsPerRevolution() {
32 return kDrivetrainCyclesPerRevolution() * 4;
33 }
James Kuszmaulc2b2a802022-02-25 21:59:31 -080034 static constexpr double kDrivetrainEncoderRatio() { return 1.0; }
milind-u086d7262022-01-19 20:44:18 -080035 static constexpr double kMaxDrivetrainEncoderPulsesPerSecond() {
36 return control_loops::drivetrain::kFreeSpeed / (2.0 * M_PI) *
37 control_loops::drivetrain::kHighOutputRatio /
38 constants::Values::kDrivetrainEncoderRatio() *
39 kDrivetrainEncoderCountsPerRevolution();
40 }
James Kuszmaul53507e12022-02-12 18:36:40 -080041
42 static double DrivetrainEncoderToMeters(int32_t in) {
43 return ((static_cast<double>(in) /
44 kDrivetrainEncoderCountsPerRevolution()) *
45 (2.0 * M_PI)) *
Henry Speiser55aa3ba2022-02-21 23:21:12 -080046 kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius;
Yash Chainani997a7492022-01-29 15:48:56 -080047 }
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080048
milind-u086d7262022-01-19 20:44:18 -080049 // Climber
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080050 static constexpr ::frc971::constants::Range kClimberRange() {
Milind Upadhyaye9075d12022-04-12 22:45:16 -070051 return ::frc971::constants::Range{.lower_hard = -0.01,
52 .upper_hard = 0.59,
53 .lower = 0.003,
54 .upper = 0.555};
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080055 }
56 static constexpr double kClimberPotMetersPerRevolution() {
57 return 22 * 0.25 * 0.0254;
58 }
59 static constexpr double kClimberPotRatio() { return 1.0; }
60
Nathan Leong342b85e2023-01-08 13:49:33 -080061 static constexpr double kClimberPotMetersPerVolt() {
62 return kClimberPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
63 kClimberPotMetersPerRevolution();
64 }
65
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080066 struct PotConstants {
67 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
68 ::frc971::zeroing::RelativeEncoderZeroingEstimator>
69 subsystem_params;
70 double potentiometer_offset;
71 };
72
73 PotConstants climber;
Milo Lin6b5e4582022-01-29 14:51:37 -080074
75 // Intake
76 // two encoders with same gear ratio for intake
Henry Speiser55aa3ba2022-02-21 23:21:12 -080077 static constexpr double kIntakeEncoderCountsPerRevolution() { return 4096.0; }
Milo Lin6b5e4582022-01-29 14:51:37 -080078
79 static constexpr double kIntakeEncoderRatio() {
Austin Schuh275f9812022-03-05 14:02:37 -080080 return (16.0 / 64.0) * (18.0 / 62.0);
Milo Lin6b5e4582022-01-29 14:51:37 -080081 }
82
Henry Speiser55aa3ba2022-02-21 23:21:12 -080083 static constexpr double kIntakePotRatio() { return 16.0 / 64.0; }
84
Nathan Leong342b85e2023-01-08 13:49:33 -080085 static constexpr double kIntakePotRadiansPerVolt() {
86 return kIntakePotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) *
87 (2 * M_PI /*radians*/);
88 }
89
Henry Speiser55aa3ba2022-02-21 23:21:12 -080090 static constexpr double kMaxIntakeEncoderPulsesPerSecond() {
91 return control_loops::superstructure::intake::kFreeSpeed / (2.0 * M_PI) *
92 control_loops::superstructure::intake::kOutputRatio /
93 kIntakeEncoderRatio() * kIntakeEncoderCountsPerRevolution();
94 }
95
96 struct PotAndAbsEncoderConstants {
97 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
98 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
99 subsystem_params;
100 double potentiometer_offset;
101 };
102
103 PotAndAbsEncoderConstants intake_front;
104 PotAndAbsEncoderConstants intake_back;
105
106 // TODO (Yash): Constants need to be tuned
107 static constexpr ::frc971::constants::Range kIntakeRange() {
108 return ::frc971::constants::Range{
Austin Schuh465a2882022-03-05 15:39:04 -0800109 .lower_hard = -0.85, // Back Hard
110 .upper_hard = 1.85, // Front Hard
111 .lower = -0.400, // Back Soft
112 .upper = 1.57 // Front Soft
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800113 };
114 }
115
116 // Intake rollers
117 static constexpr double kIntakeRollerSupplyCurrentLimit() { return 40.0; }
118 static constexpr double kIntakeRollerStatorCurrentLimit() { return 60.0; }
119
Ravago Jones5da06352022-03-04 20:26:24 -0800120 // Transfer rollers
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800121 static constexpr double kTransferRollerVoltage() { return 12.0; }
Ravago Jones5da06352022-03-04 20:26:24 -0800122
123 // Voltage to wiggle the transfer rollers and keep a ball in.
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800124 static constexpr double kTransferRollerWiggleVoltage() { return 3.0; }
125
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800126 // Turret
127 PotAndAbsEncoderConstants turret;
Milind Upadhyaye9075d12022-04-12 22:45:16 -0700128 frc971::constants::Range turret_range;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800129
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700130 static constexpr double kTurretBackIntakePos() { return -M_PI; }
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800131 static constexpr double kTurretFrontIntakePos() { return 0; }
Ravago Jones5da06352022-03-04 20:26:24 -0800132
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800133 static constexpr double kTurretPotRatio() { return 27.0 / 110.0; }
Nathan Leong342b85e2023-01-08 13:49:33 -0800134 static constexpr double kTurretPotRadiansPerVolt() {
135 return kTurretPotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) *
136 (2 * M_PI /*radians*/);
137 }
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800138 static constexpr double kTurretEncoderRatio() { return kTurretPotRatio(); }
139 static constexpr double kTurretEncoderCountsPerRevolution() { return 4096.0; }
140
141 static constexpr double kMaxTurretEncoderPulsesPerSecond() {
142 return control_loops::superstructure::turret::kFreeSpeed / (2.0 * M_PI) *
143 control_loops::superstructure::turret::kOutputRatio /
144 kTurretEncoderRatio() * kTurretEncoderCountsPerRevolution();
145 }
Griffin Buibcbef482022-02-23 15:32:10 -0800146
147 // Flipper arms
milind-u37dc40b2022-03-08 19:51:27 -0800148 static constexpr double kFlipperArmSupplyCurrentLimit() { return 40.0; }
149 static constexpr double kFlipperArmStatorCurrentLimit() { return 60.0; }
Griffin Buibcbef482022-02-23 15:32:10 -0800150
Ravago Jones5da06352022-03-04 20:26:24 -0800151 // Voltage to open the flippers for firing
152 static constexpr double kFlipperOpenVoltage() { return 3.0; }
153 // Voltage to keep the flippers open for firing once they already are
Austin Schuh465a2882022-03-05 15:39:04 -0800154 static constexpr double kFlipperHoldVoltage() { return 2.5; }
Ravago Jones5da06352022-03-04 20:26:24 -0800155 // Voltage to feed a ball from the transfer rollers to the catpult with the
156 // flippers
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800157 static constexpr double kFlipperFeedVoltage() { return -12.0; }
Ravago Jones5da06352022-03-04 20:26:24 -0800158
159 // Ball is fed into catapult for atleast this time no matter what
160 static constexpr std::chrono::milliseconds kExtraLoadingTime() {
161 return std::chrono::milliseconds(100);
162 }
163 // If we have been trying to transfer the ball for this amount of time, it
164 // probably got lost so abort
165 static constexpr std::chrono::seconds kBallLostTime() {
166 return std::chrono::seconds(2);
167 }
168 // If the flippers took more than this amount of time to open for firing,
169 // reseat the ball
170 static constexpr std::chrono::milliseconds kFlipperOpeningTimeout() {
milind-u37dc40b2022-03-08 19:51:27 -0800171 return std::chrono::milliseconds(1000);
Ravago Jones5da06352022-03-04 20:26:24 -0800172 }
173 // Don't use flipper velocity readings more than this amount of time in the
174 // past
175 static constexpr std::chrono::milliseconds kFlipperVelocityValidTime() {
176 return std::chrono::milliseconds(100);
177 }
178
Griffin Buibcbef482022-02-23 15:32:10 -0800179 // TODO: (Griffin) this needs to be set
180 static constexpr ::frc971::constants::Range kFlipperArmRange() {
181 return ::frc971::constants::Range{
Austin Schuh465a2882022-03-05 15:39:04 -0800182 .lower_hard = -0.01, .upper_hard = 0.4, .lower = 0.0, .upper = 0.5};
Griffin Buibcbef482022-02-23 15:32:10 -0800183 }
Ravago Jones5da06352022-03-04 20:26:24 -0800184 // Position of the flippers when they are open
Austin Schuh6b1e4d92022-03-12 12:02:46 -0800185 static constexpr double kFlipperOpenPosition() { return 0.20; }
Ravago Jones5da06352022-03-04 20:26:24 -0800186 // If the flippers were open but now moved back, reseat the ball if they go
187 // below this position
Austin Schuh465a2882022-03-05 15:39:04 -0800188 static constexpr double kReseatFlipperPosition() { return 0.1; }
Griffin Buibcbef482022-02-23 15:32:10 -0800189
190 static constexpr double kFlipperArmsPotRatio() { return 16.0 / 36.0; }
191
Nathan Leong342b85e2023-01-08 13:49:33 -0800192 static constexpr double kFlipperArmsPotRadiansPerVolt() {
193 return kFlipperArmsPotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) *
194 (2 * M_PI /*radians*/);
195 }
196
Griffin Buibcbef482022-02-23 15:32:10 -0800197 PotConstants flipper_arm_left;
198 PotConstants flipper_arm_right;
Austin Schuh39f26f62022-02-24 21:34:46 -0800199
200 // Catapult.
201 static constexpr double kCatapultPotRatio() { return (12.0 / 33.0); }
202 static constexpr double kCatapultEncoderRatio() {
203 return kCatapultPotRatio();
204 }
205 static constexpr double kCatapultEncoderCountsPerRevolution() {
206 return 4096.0;
207 }
208
209 static constexpr double kMaxCatapultEncoderPulsesPerSecond() {
210 return control_loops::superstructure::catapult::kFreeSpeed / (2.0 * M_PI) *
211 control_loops::superstructure::catapult::kOutputRatio /
212 kCatapultEncoderRatio() * kCatapultEncoderCountsPerRevolution();
213 }
214 static constexpr ::frc971::constants::Range kCatapultRange() {
215 return ::frc971::constants::Range{
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800216 .lower_hard = -1.0,
Austin Schuh39f26f62022-02-24 21:34:46 -0800217 .upper_hard = 2.0,
milind-u37dc40b2022-03-08 19:51:27 -0800218 .lower = -0.91,
Austin Schuh39f26f62022-02-24 21:34:46 -0800219 .upper = 1.57,
220 };
221 }
222
223 PotAndAbsEncoderConstants catapult;
milind-ucafdd5d2022-03-01 19:58:57 -0800224
225 // TODO(milind): set this
226 static constexpr double kImuHeight() { return 0.0; }
Ravago Jones3283ce02022-03-09 19:31:29 -0800227
228 struct ShotParams {
229 // Measured in radians
230 double shot_angle;
231 // Muzzle velocity (m/s) of the ball as it is released from the catapult.
232 double shot_velocity;
233
234 static ShotParams BlendY(double coefficient, ShotParams a1, ShotParams a2) {
235 using ::frc971::shooter_interpolation::Blend;
236 return ShotParams{
James Kuszmaulb9ba9a52022-03-31 22:16:01 -0700237 .shot_angle = Blend(coefficient, a1.shot_angle, a2.shot_angle),
238 .shot_velocity =
239 Blend(coefficient, a1.shot_velocity, a2.shot_velocity),
Ravago Jones3283ce02022-03-09 19:31:29 -0800240 };
241 }
242 };
243
James Kuszmaulb9ba9a52022-03-31 22:16:01 -0700244 struct ShotVelocityParams {
245 // Speed over ground to use for shooting-on-the-fly.
246 double shot_speed_over_ground;
247
248 static ShotVelocityParams BlendY(double coefficient, ShotVelocityParams a1,
249 ShotVelocityParams a2) {
250 using ::frc971::shooter_interpolation::Blend;
251 return ShotVelocityParams{Blend(coefficient, a1.shot_speed_over_ground,
252 a2.shot_speed_over_ground)};
253 }
254 };
255
Ravago Jones3283ce02022-03-09 19:31:29 -0800256 InterpolationTable<ShotParams> shot_interpolation_table;
James Kuszmaulb9ba9a52022-03-31 22:16:01 -0700257
258 InterpolationTable<ShotVelocityParams> shot_velocity_interpolation_table;
Milind Upadhyayc76bb4c2022-09-21 09:31:48 -0700259
260 struct BallColorParams {
261 // Rects stored as {x, y, width, height}
262 std::array<int, 4> reference_red;
263 std::array<int, 4> reference_blue;
264 std::array<int, 4> ball_location;
265 };
266
267 BallColorParams ball_color;
milind-u086d7262022-01-19 20:44:18 -0800268};
269
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800270// Creates and returns a Values instance for the constants.
271// Should be called before realtime because this allocates memory.
272// Only the first call to either of these will be used.
273Values MakeValues(uint16_t team);
milind-u086d7262022-01-19 20:44:18 -0800274
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800275// Calls MakeValues with aos::network::GetTeamNumber()
276Values MakeValues();
milind-u086d7262022-01-19 20:44:18 -0800277
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800278} // namespace y2022::constants
milind-u086d7262022-01-19 20:44:18 -0800279
280#endif // Y2022_CONSTANTS_H_