blob: f27b6cdba1786296caa52ba915e3de4503b06332 [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
milind-u086d7262022-01-19 20:44:18 -080021namespace y2022 {
22namespace constants {
23
Milind Upadhyayc76bb4c2022-09-21 09:31:48 -070024constexpr uint16_t kCompTeamNumber = 971;
25constexpr uint16_t kPracticeTeamNumber = 9971;
26constexpr uint16_t kCodingRobotTeamNumber = 7971;
27
milind-u086d7262022-01-19 20:44:18 -080028struct Values {
29 static const int kZeroingSampleSize = 200;
30
31 static constexpr double kDrivetrainCyclesPerRevolution() { return 512.0; }
32 static constexpr double kDrivetrainEncoderCountsPerRevolution() {
33 return kDrivetrainCyclesPerRevolution() * 4;
34 }
James Kuszmaulc2b2a802022-02-25 21:59:31 -080035 static constexpr double kDrivetrainEncoderRatio() { return 1.0; }
milind-u086d7262022-01-19 20:44:18 -080036 static constexpr double kMaxDrivetrainEncoderPulsesPerSecond() {
37 return control_loops::drivetrain::kFreeSpeed / (2.0 * M_PI) *
38 control_loops::drivetrain::kHighOutputRatio /
39 constants::Values::kDrivetrainEncoderRatio() *
40 kDrivetrainEncoderCountsPerRevolution();
41 }
James Kuszmaul53507e12022-02-12 18:36:40 -080042
43 static double DrivetrainEncoderToMeters(int32_t in) {
44 return ((static_cast<double>(in) /
45 kDrivetrainEncoderCountsPerRevolution()) *
46 (2.0 * M_PI)) *
Henry Speiser55aa3ba2022-02-21 23:21:12 -080047 kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius;
Yash Chainani997a7492022-01-29 15:48:56 -080048 }
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080049
milind-u086d7262022-01-19 20:44:18 -080050 // Climber
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080051 static constexpr ::frc971::constants::Range kClimberRange() {
Milind Upadhyaye9075d12022-04-12 22:45:16 -070052 return ::frc971::constants::Range{.lower_hard = -0.01,
53 .upper_hard = 0.59,
54 .lower = 0.003,
55 .upper = 0.555};
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080056 }
57 static constexpr double kClimberPotMetersPerRevolution() {
58 return 22 * 0.25 * 0.0254;
59 }
60 static constexpr double kClimberPotRatio() { return 1.0; }
61
Nathan Leong342b85e2023-01-08 13:49:33 -080062 static constexpr double kClimberPotMetersPerVolt() {
63 return kClimberPotRatio() * (5.0 /*turns*/ / 5.0 /*volts*/) *
64 kClimberPotMetersPerRevolution();
65 }
66
Siddhant Kanwar0e37f592022-02-21 19:26:50 -080067 struct PotConstants {
68 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
69 ::frc971::zeroing::RelativeEncoderZeroingEstimator>
70 subsystem_params;
71 double potentiometer_offset;
72 };
73
74 PotConstants climber;
Milo Lin6b5e4582022-01-29 14:51:37 -080075
76 // Intake
77 // two encoders with same gear ratio for intake
Henry Speiser55aa3ba2022-02-21 23:21:12 -080078 static constexpr double kIntakeEncoderCountsPerRevolution() { return 4096.0; }
Milo Lin6b5e4582022-01-29 14:51:37 -080079
80 static constexpr double kIntakeEncoderRatio() {
Austin Schuh275f9812022-03-05 14:02:37 -080081 return (16.0 / 64.0) * (18.0 / 62.0);
Milo Lin6b5e4582022-01-29 14:51:37 -080082 }
83
Henry Speiser55aa3ba2022-02-21 23:21:12 -080084 static constexpr double kIntakePotRatio() { return 16.0 / 64.0; }
85
Nathan Leong342b85e2023-01-08 13:49:33 -080086 static constexpr double kIntakePotRadiansPerVolt() {
87 return kIntakePotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) *
88 (2 * M_PI /*radians*/);
89 }
90
Henry Speiser55aa3ba2022-02-21 23:21:12 -080091 static constexpr double kMaxIntakeEncoderPulsesPerSecond() {
92 return control_loops::superstructure::intake::kFreeSpeed / (2.0 * M_PI) *
93 control_loops::superstructure::intake::kOutputRatio /
94 kIntakeEncoderRatio() * kIntakeEncoderCountsPerRevolution();
95 }
96
97 struct PotAndAbsEncoderConstants {
98 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
99 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
100 subsystem_params;
101 double potentiometer_offset;
102 };
103
104 PotAndAbsEncoderConstants intake_front;
105 PotAndAbsEncoderConstants intake_back;
106
107 // TODO (Yash): Constants need to be tuned
108 static constexpr ::frc971::constants::Range kIntakeRange() {
109 return ::frc971::constants::Range{
Austin Schuh465a2882022-03-05 15:39:04 -0800110 .lower_hard = -0.85, // Back Hard
111 .upper_hard = 1.85, // Front Hard
112 .lower = -0.400, // Back Soft
113 .upper = 1.57 // Front Soft
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800114 };
115 }
116
117 // Intake rollers
118 static constexpr double kIntakeRollerSupplyCurrentLimit() { return 40.0; }
119 static constexpr double kIntakeRollerStatorCurrentLimit() { return 60.0; }
120
Ravago Jones5da06352022-03-04 20:26:24 -0800121 // Transfer rollers
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800122 static constexpr double kTransferRollerVoltage() { return 12.0; }
Ravago Jones5da06352022-03-04 20:26:24 -0800123
124 // Voltage to wiggle the transfer rollers and keep a ball in.
Milind Upadhyayb1a74ea2022-03-09 20:34:35 -0800125 static constexpr double kTransferRollerWiggleVoltage() { return 3.0; }
126
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800127 // Turret
128 PotAndAbsEncoderConstants turret;
Milind Upadhyaye9075d12022-04-12 22:45:16 -0700129 frc971::constants::Range turret_range;
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800130
Austin Schuh42d7e5f2022-03-16 23:35:09 -0700131 static constexpr double kTurretBackIntakePos() { return -M_PI; }
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800132 static constexpr double kTurretFrontIntakePos() { return 0; }
Ravago Jones5da06352022-03-04 20:26:24 -0800133
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800134 static constexpr double kTurretPotRatio() { return 27.0 / 110.0; }
Nathan Leong342b85e2023-01-08 13:49:33 -0800135 static constexpr double kTurretPotRadiansPerVolt() {
136 return kTurretPotRatio() * (10.0 /*turns*/ / 5.0 /*volts*/) *
137 (2 * M_PI /*radians*/);
138 }
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800139 static constexpr double kTurretEncoderRatio() { return kTurretPotRatio(); }
140 static constexpr double kTurretEncoderCountsPerRevolution() { return 4096.0; }
141
142 static constexpr double kMaxTurretEncoderPulsesPerSecond() {
143 return control_loops::superstructure::turret::kFreeSpeed / (2.0 * M_PI) *
144 control_loops::superstructure::turret::kOutputRatio /
145 kTurretEncoderRatio() * kTurretEncoderCountsPerRevolution();
146 }
Griffin Buibcbef482022-02-23 15:32:10 -0800147
148 // Flipper arms
milind-u37dc40b2022-03-08 19:51:27 -0800149 static constexpr double kFlipperArmSupplyCurrentLimit() { return 40.0; }
150 static constexpr double kFlipperArmStatorCurrentLimit() { return 60.0; }
Griffin Buibcbef482022-02-23 15:32:10 -0800151
Ravago Jones5da06352022-03-04 20:26:24 -0800152 // Voltage to open the flippers for firing
153 static constexpr double kFlipperOpenVoltage() { return 3.0; }
154 // Voltage to keep the flippers open for firing once they already are
Austin Schuh465a2882022-03-05 15:39:04 -0800155 static constexpr double kFlipperHoldVoltage() { return 2.5; }
Ravago Jones5da06352022-03-04 20:26:24 -0800156 // Voltage to feed a ball from the transfer rollers to the catpult with the
157 // flippers
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800158 static constexpr double kFlipperFeedVoltage() { return -12.0; }
Ravago Jones5da06352022-03-04 20:26:24 -0800159
160 // Ball is fed into catapult for atleast this time no matter what
161 static constexpr std::chrono::milliseconds kExtraLoadingTime() {
162 return std::chrono::milliseconds(100);
163 }
164 // If we have been trying to transfer the ball for this amount of time, it
165 // probably got lost so abort
166 static constexpr std::chrono::seconds kBallLostTime() {
167 return std::chrono::seconds(2);
168 }
169 // If the flippers took more than this amount of time to open for firing,
170 // reseat the ball
171 static constexpr std::chrono::milliseconds kFlipperOpeningTimeout() {
milind-u37dc40b2022-03-08 19:51:27 -0800172 return std::chrono::milliseconds(1000);
Ravago Jones5da06352022-03-04 20:26:24 -0800173 }
174 // Don't use flipper velocity readings more than this amount of time in the
175 // past
176 static constexpr std::chrono::milliseconds kFlipperVelocityValidTime() {
177 return std::chrono::milliseconds(100);
178 }
179
Griffin Buibcbef482022-02-23 15:32:10 -0800180 // TODO: (Griffin) this needs to be set
181 static constexpr ::frc971::constants::Range kFlipperArmRange() {
182 return ::frc971::constants::Range{
Austin Schuh465a2882022-03-05 15:39:04 -0800183 .lower_hard = -0.01, .upper_hard = 0.4, .lower = 0.0, .upper = 0.5};
Griffin Buibcbef482022-02-23 15:32:10 -0800184 }
Ravago Jones5da06352022-03-04 20:26:24 -0800185 // Position of the flippers when they are open
Austin Schuh6b1e4d92022-03-12 12:02:46 -0800186 static constexpr double kFlipperOpenPosition() { return 0.20; }
Ravago Jones5da06352022-03-04 20:26:24 -0800187 // If the flippers were open but now moved back, reseat the ball if they go
188 // below this position
Austin Schuh465a2882022-03-05 15:39:04 -0800189 static constexpr double kReseatFlipperPosition() { return 0.1; }
Griffin Buibcbef482022-02-23 15:32:10 -0800190
191 static constexpr double kFlipperArmsPotRatio() { return 16.0 / 36.0; }
192
Nathan Leong342b85e2023-01-08 13:49:33 -0800193 static constexpr double kFlipperArmsPotRadiansPerVolt() {
194 return kFlipperArmsPotRatio() * (3.0 /*turns*/ / 5.0 /*volts*/) *
195 (2 * M_PI /*radians*/);
196 }
197
Griffin Buibcbef482022-02-23 15:32:10 -0800198 PotConstants flipper_arm_left;
199 PotConstants flipper_arm_right;
Austin Schuh39f26f62022-02-24 21:34:46 -0800200
201 // Catapult.
202 static constexpr double kCatapultPotRatio() { return (12.0 / 33.0); }
203 static constexpr double kCatapultEncoderRatio() {
204 return kCatapultPotRatio();
205 }
206 static constexpr double kCatapultEncoderCountsPerRevolution() {
207 return 4096.0;
208 }
209
210 static constexpr double kMaxCatapultEncoderPulsesPerSecond() {
211 return control_loops::superstructure::catapult::kFreeSpeed / (2.0 * M_PI) *
212 control_loops::superstructure::catapult::kOutputRatio /
213 kCatapultEncoderRatio() * kCatapultEncoderCountsPerRevolution();
214 }
215 static constexpr ::frc971::constants::Range kCatapultRange() {
216 return ::frc971::constants::Range{
Milind Upadhyay47e0d032022-03-05 23:06:25 -0800217 .lower_hard = -1.0,
Austin Schuh39f26f62022-02-24 21:34:46 -0800218 .upper_hard = 2.0,
milind-u37dc40b2022-03-08 19:51:27 -0800219 .lower = -0.91,
Austin Schuh39f26f62022-02-24 21:34:46 -0800220 .upper = 1.57,
221 };
222 }
223
224 PotAndAbsEncoderConstants catapult;
milind-ucafdd5d2022-03-01 19:58:57 -0800225
226 // TODO(milind): set this
227 static constexpr double kImuHeight() { return 0.0; }
Ravago Jones3283ce02022-03-09 19:31:29 -0800228
229 struct ShotParams {
230 // Measured in radians
231 double shot_angle;
232 // Muzzle velocity (m/s) of the ball as it is released from the catapult.
233 double shot_velocity;
234
235 static ShotParams BlendY(double coefficient, ShotParams a1, ShotParams a2) {
236 using ::frc971::shooter_interpolation::Blend;
237 return ShotParams{
James Kuszmaulb9ba9a52022-03-31 22:16:01 -0700238 .shot_angle = Blend(coefficient, a1.shot_angle, a2.shot_angle),
239 .shot_velocity =
240 Blend(coefficient, a1.shot_velocity, a2.shot_velocity),
Ravago Jones3283ce02022-03-09 19:31:29 -0800241 };
242 }
243 };
244
James Kuszmaulb9ba9a52022-03-31 22:16:01 -0700245 struct ShotVelocityParams {
246 // Speed over ground to use for shooting-on-the-fly.
247 double shot_speed_over_ground;
248
249 static ShotVelocityParams BlendY(double coefficient, ShotVelocityParams a1,
250 ShotVelocityParams a2) {
251 using ::frc971::shooter_interpolation::Blend;
252 return ShotVelocityParams{Blend(coefficient, a1.shot_speed_over_ground,
253 a2.shot_speed_over_ground)};
254 }
255 };
256
Ravago Jones3283ce02022-03-09 19:31:29 -0800257 InterpolationTable<ShotParams> shot_interpolation_table;
James Kuszmaulb9ba9a52022-03-31 22:16:01 -0700258
259 InterpolationTable<ShotVelocityParams> shot_velocity_interpolation_table;
Milind Upadhyayc76bb4c2022-09-21 09:31:48 -0700260
261 struct BallColorParams {
262 // Rects stored as {x, y, width, height}
263 std::array<int, 4> reference_red;
264 std::array<int, 4> reference_blue;
265 std::array<int, 4> ball_location;
266 };
267
268 BallColorParams ball_color;
milind-u086d7262022-01-19 20:44:18 -0800269};
270
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800271// Creates and returns a Values instance for the constants.
272// Should be called before realtime because this allocates memory.
273// Only the first call to either of these will be used.
274Values MakeValues(uint16_t team);
milind-u086d7262022-01-19 20:44:18 -0800275
Henry Speiser55aa3ba2022-02-21 23:21:12 -0800276// Calls MakeValues with aos::network::GetTeamNumber()
277Values MakeValues();
milind-u086d7262022-01-19 20:44:18 -0800278
279} // namespace constants
280} // namespace y2022
281
282#endif // Y2022_CONSTANTS_H_