Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include "y2020/constants.h" |
| 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cinttypes> |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 4 | #include <map> |
| 5 | |
| 6 | #if __has_feature(address_sanitizer) |
| 7 | #include "sanitizer/lsan_interface.h" |
| 8 | #endif |
| 9 | |
| 10 | #include "absl/base/call_once.h" |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 11 | #include "absl/log/check.h" |
| 12 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 13 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 14 | #include "aos/network/team_number.h" |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 15 | #include "aos/stl_mutex/stl_mutex.h" |
James Kuszmaul | ec635d2 | 2023-08-12 18:39:24 -0700 | [diff] [blame] | 16 | #include "frc971/zeroing/absolute_and_absolute_encoder.h" |
| 17 | #include "frc971/zeroing/absolute_encoder.h" |
| 18 | #include "frc971/zeroing/pot_and_absolute_encoder.h" |
ravago | 901c426 | 2020-02-16 15:33:14 -0800 | [diff] [blame] | 19 | #include "y2020/control_loops/superstructure/control_panel/integral_control_panel_plant.h" |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 20 | #include "y2020/control_loops/superstructure/hood/integral_hood_plant.h" |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 21 | #include "y2020/control_loops/superstructure/intake/integral_intake_plant.h" |
| 22 | #include "y2020/control_loops/superstructure/turret/integral_turret_plant.h" |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 23 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 24 | namespace y2020::constants { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 25 | |
| 26 | const int Values::kZeroingSampleSize; |
| 27 | |
| 28 | namespace { |
| 29 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 30 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 31 | Values *const r = new Values(); |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 32 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 33 | ::frc971::zeroing::AbsoluteAndAbsoluteEncoderZeroingEstimator> |
| 34 | *const hood = &r->hood; |
milind-u | d53408e | 2021-10-21 19:43:58 -0700 | [diff] [blame] | 35 | Values::HoodGeometry *const hood_geometry = &r->hood_geometry; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 36 | |
milind-u | d53408e | 2021-10-21 19:43:58 -0700 | [diff] [blame] | 37 | // We found that the finisher velocity does not change ball velocity much, |
| 38 | // so keep it constant. |
Austin Schuh | 4408c42 | 2021-10-16 13:56:07 -0700 | [diff] [blame] | 39 | r->shot_interpolation_table = |
milind-u | f7fadbf | 2021-11-07 14:10:54 -0800 | [diff] [blame] | 40 | InterpolationTable<Values::ShotParams>({{1.128, {0.01, 225, 170}}, |
| 41 | {1.509, {0.01, 215, 160}}, |
Austin Schuh | e17cbaf | 2021-11-14 21:02:37 -0800 | [diff] [blame] | 42 | {1.89, {0.01, 250, 175}}, |
| 43 | {2.15, {0.06, 235, 170}}, |
| 44 | {2.68, {0.195, 250, 200}}, |
milind-u | f7fadbf | 2021-11-07 14:10:54 -0800 | [diff] [blame] | 45 | {3.19, {0.27, 250, 220}}, |
| 46 | {3.93, {0.365, 285, 250}}, |
| 47 | {4.63, {0.42, 320, 280}}, |
| 48 | {5.32, {0.515, 375, 330}}, |
| 49 | {6, {0.565, 440, 400}}, |
| 50 | {6.68, {0.58, 480, 450}}, |
| 51 | {7.37, {0.645, 520, 540}}, |
| 52 | {8.36, {0.645, 550, 560}}, |
| 53 | {9.39, {0.66, 550, 580}}, |
Austin Schuh | b24e905 | 2021-11-12 19:53:33 -0800 | [diff] [blame] | 54 | {10.4, {0.670, 550, 600}}, |
| 55 | {11.4, {0.670, 550, 600}}}); |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 56 | |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 57 | // Hood constants. |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 58 | hood->zeroing_voltage = 2.0; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 59 | hood->operating_voltage = 12.0; |
James Kuszmaul | 31c4535 | 2024-01-14 18:07:42 -0800 | [diff] [blame] | 60 | hood->zeroing_profile_params = {{}, 0.5, 3.0}; |
| 61 | hood->default_profile_params = {{}, 6.0, 30.0}; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 62 | hood->range = Values::kHoodRange(); |
| 63 | hood->make_integral_loop = |
| 64 | control_loops::superstructure::hood::MakeIntegralHoodLoop; |
| 65 | hood->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 66 | hood->zeroing_constants.zeroing_threshold = 0.0005; |
| 67 | hood->zeroing_constants.moving_buffer_size = 20; |
| 68 | hood->zeroing_constants.allowable_encoder_error = 0.9; |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 69 | hood->zeroing_constants.one_revolution_distance = |
| 70 | M_PI * 2.0 * constants::Values::kHoodEncoderRatio(); |
| 71 | hood->zeroing_constants.single_turn_middle_position = |
| 72 | Values::kHoodRange().middle(); |
| 73 | hood->zeroing_constants.single_turn_one_revolution_distance = |
| 74 | M_PI * 2.0 * constants::Values::kHoodSingleTurnEncoderRatio(); |
| 75 | hood->zeroing_constants.measured_absolute_position = 0; |
| 76 | hood->zeroing_constants.single_turn_measured_absolute_position = 0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 77 | |
milind-u | d53408e | 2021-10-21 19:43:58 -0700 | [diff] [blame] | 78 | constexpr double kDegToRad = M_PI / 180.0; |
| 79 | constexpr double kMmToM = 1.0 / 1000.0; |
| 80 | hood_geometry->theta_0 = 22.98004 * kDegToRad; |
| 81 | hood_geometry->screw_length_0 = 110.33888 * kMmToM; |
| 82 | hood_geometry->radius = 269.6262 * kMmToM; |
| 83 | hood_geometry->diagonal_length = 288.4353 * kMmToM; |
| 84 | hood_geometry->back_plate_diagonal_length = 22.86 * kMmToM; |
| 85 | |
Sabina Davis | e8d3899 | 2020-02-02 15:00:31 -0800 | [diff] [blame] | 86 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 87 | ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake = |
| 88 | &r->intake; |
| 89 | |
| 90 | // Intake constants. |
| 91 | intake->zeroing_voltage = 3.0; |
| 92 | intake->operating_voltage = 12.0; |
James Kuszmaul | 31c4535 | 2024-01-14 18:07:42 -0800 | [diff] [blame] | 93 | intake->zeroing_profile_params = {{}, 0.5, 3.0}; |
| 94 | intake->default_profile_params = {{}, 6.0, 30.0}; |
Sabina Davis | e8d3899 | 2020-02-02 15:00:31 -0800 | [diff] [blame] | 95 | intake->range = Values::kIntakeRange(); |
| 96 | intake->make_integral_loop = |
| 97 | control_loops::superstructure::intake::MakeIntegralIntakeLoop; |
| 98 | intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
| 99 | intake->zeroing_constants.one_revolution_distance = |
| 100 | M_PI * 2.0 * constants::Values::kIntakeEncoderRatio(); |
| 101 | intake->zeroing_constants.zeroing_threshold = 0.0005; |
| 102 | intake->zeroing_constants.moving_buffer_size = 20; |
| 103 | intake->zeroing_constants.allowable_encoder_error = 0.9; |
| 104 | intake->zeroing_constants.middle_position = Values::kIntakeRange().middle(); |
| 105 | |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 106 | Values::PotAndAbsEncoderConstants *const turret = &r->turret; |
| 107 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 108 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 109 | *const turret_params = &turret->subsystem_params; |
| 110 | |
ravago | 901c426 | 2020-02-16 15:33:14 -0800 | [diff] [blame] | 111 | // Turret Constants |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 112 | turret_params->zeroing_voltage = 4.0; |
James Kuszmaul | 519585d | 2020-03-08 22:32:48 -0700 | [diff] [blame] | 113 | turret_params->operating_voltage = 8.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 114 | // TODO(austin): Tune these. |
James Kuszmaul | 31c4535 | 2024-01-14 18:07:42 -0800 | [diff] [blame] | 115 | turret_params->zeroing_profile_params = {{}, 0.5, 2.0}; |
| 116 | turret_params->default_profile_params = {{}, 15.0, 40.0}; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 117 | turret_params->range = Values::kTurretRange(); |
| 118 | turret_params->make_integral_loop = |
| 119 | &control_loops::superstructure::turret::MakeIntegralTurretLoop; |
| 120 | turret_params->zeroing_constants.average_filter_size = |
| 121 | Values::kZeroingSampleSize; |
| 122 | turret_params->zeroing_constants.one_revolution_distance = |
| 123 | M_PI * 2.0 * constants::Values::kTurretEncoderRatio(); |
| 124 | turret_params->zeroing_constants.zeroing_threshold = 0.0005; |
| 125 | turret_params->zeroing_constants.moving_buffer_size = 20; |
| 126 | turret_params->zeroing_constants.allowable_encoder_error = 0.9; |
| 127 | |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 128 | CHECK_LE(hood->range.range(), |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 129 | hood->zeroing_constants.single_turn_one_revolution_distance); |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 130 | CHECK_LE(intake->range.range(), |
| 131 | intake->zeroing_constants.one_revolution_distance); |
| 132 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 133 | switch (team) { |
| 134 | // A set of constants for tests. |
| 135 | case 1: |
milind-u | f7fadbf | 2021-11-07 14:10:54 -0800 | [diff] [blame] | 136 | r->shot_interpolation_table = InterpolationTable<Values::ShotParams>( |
milind-u | dfe0bcd | 2021-11-07 16:23:26 -0800 | [diff] [blame] | 137 | {{1, {0.01, 250, 350}}, {10, {0.67, 500, 350}}}); |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 138 | case Values::kSpareRoborioTeamNumber: |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 139 | break; |
| 140 | |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 141 | case Values::kCompTeamNumber: |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 142 | intake->zeroing_constants.measured_absolute_position = |
Austin Schuh | 4dc06c7 | 2021-10-17 17:39:41 -0700 | [diff] [blame] | 143 | 0.433936997731885 - Values::kIntakeZero(); |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 144 | |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 145 | turret->potentiometer_offset = 5.52519370141247 + 0.00853506822980376 + |
James Kuszmaul | f774d2d | 2021-10-03 08:02:35 -0700 | [diff] [blame] | 146 | 0.0109413725126625 - 0.223719825811759 + |
Austin Schuh | 1216d99 | 2021-10-23 19:23:43 -0700 | [diff] [blame] | 147 | 0.261356551915472 - 0.0490168170767848 - |
Austin Schuh | da59627 | 2021-11-13 11:06:26 -0800 | [diff] [blame] | 148 | 0.179342788816305 + 0.800488361764795; |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 149 | turret_params->zeroing_constants.measured_absolute_position = |
Austin Schuh | da59627 | 2021-11-13 11:06:26 -0800 | [diff] [blame] | 150 | 2.55091426937561; |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 151 | |
Austin Schuh | a2f01c6 | 2021-10-25 22:01:10 -0700 | [diff] [blame] | 152 | hood->zeroing_constants.measured_absolute_position = 0.0102882878327271; |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 153 | hood->zeroing_constants.single_turn_measured_absolute_position = |
Austin Schuh | c09cbe0 | 2021-10-24 17:16:15 -0700 | [diff] [blame] | 154 | 0.302574797776192; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 155 | break; |
| 156 | |
James Kuszmaul | 0a98140 | 2021-10-09 21:00:34 -0700 | [diff] [blame] | 157 | case Values::kPracticeTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 158 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 159 | |
Austin Schuh | 81c5f00 | 2021-11-06 22:59:07 -0700 | [diff] [blame] | 160 | intake->zeroing_constants.measured_absolute_position = 0.205469223604347; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 161 | |
Austin Schuh | 31904ee | 2021-03-06 23:52:08 -0800 | [diff] [blame] | 162 | turret->potentiometer_offset = 5.3931926228241; |
| 163 | turret_params->zeroing_constants.measured_absolute_position = 4.22; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 164 | break; |
| 165 | |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 166 | case Values::kCodingRobotTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 167 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 168 | |
Sabina Davis | e8d3899 | 2020-02-02 15:00:31 -0800 | [diff] [blame] | 169 | intake->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 170 | |
| 171 | turret->potentiometer_offset = 0.0; |
| 172 | turret_params->zeroing_constants.measured_absolute_position = 0.0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 173 | break; |
| 174 | |
| 175 | default: |
Brian Silverman | f4d329c | 2021-11-04 19:32:10 -0700 | [diff] [blame] | 176 | LOG(FATAL) << "unknown team: " << team; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return r; |
| 180 | } |
| 181 | |
milind-u | 62d4a8e | 2021-10-11 16:08:41 -0700 | [diff] [blame] | 182 | const Values *values = nullptr; |
| 183 | |
| 184 | void DoGetValues() { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 185 | uint16_t team = ::aos::network::GetTeamNumber(); |
Brian Silverman | f4d329c | 2021-11-04 19:32:10 -0700 | [diff] [blame] | 186 | LOG(INFO) << "creating a Constants for team: " << team; |
milind-u | 62d4a8e | 2021-10-11 16:08:41 -0700 | [diff] [blame] | 187 | values = DoGetValuesForTeam(team); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } // namespace |
| 191 | |
milind-u | 62d4a8e | 2021-10-11 16:08:41 -0700 | [diff] [blame] | 192 | void InitValues() { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 193 | static absl::once_flag once; |
milind-u | 62d4a8e | 2021-10-11 16:08:41 -0700 | [diff] [blame] | 194 | absl::call_once(once, DoGetValues); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 195 | } |
| 196 | |
milind-u | 62d4a8e | 2021-10-11 16:08:41 -0700 | [diff] [blame] | 197 | const Values &GetValues() { |
| 198 | CHECK(values) |
| 199 | << "Values are uninitialized. Call InitValues before accessing them."; |
| 200 | return *values; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 203 | } // namespace y2020::constants |