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" |
| 11 | #include "aos/logging/logging.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 12 | #include "aos/network/team_number.h" |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 13 | #include "aos/stl_mutex/stl_mutex.h" |
ravago | 901c426 | 2020-02-16 15:33:14 -0800 | [diff] [blame] | 14 | #include "y2020/control_loops/superstructure/control_panel/integral_control_panel_plant.h" |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 15 | #include "y2020/control_loops/superstructure/hood/integral_hood_plant.h" |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 16 | #include "y2020/control_loops/superstructure/intake/integral_intake_plant.h" |
| 17 | #include "y2020/control_loops/superstructure/turret/integral_turret_plant.h" |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 18 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 19 | namespace y2020 { |
| 20 | namespace constants { |
| 21 | |
| 22 | const int Values::kZeroingSampleSize; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const uint16_t kCompTeamNumber = 971; |
| 27 | const uint16_t kPracticeTeamNumber = 9971; |
James Kuszmaul | 6535d56 | 2020-05-09 16:58:13 -0700 | [diff] [blame] | 28 | const uint16_t kSpareRoborioTeamNumber = 6971; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 29 | |
| 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; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 35 | |
milind-u | b1b7e47 | 2021-10-03 17:11:19 -0700 | [diff] [blame] | 36 | constexpr double kInchesToMeters = 0.0254; |
| 37 | // Approximate length from the front bumpers to the middle of the robot. |
| 38 | constexpr double kHalfRobotLength = (36.00 / 2) * kInchesToMeters; |
milind-u | 0a178a8 | 2021-09-28 18:42:09 -0700 | [diff] [blame] | 39 | // We found that the finisher velocity does not change ball velocity much, so |
| 40 | // keep it constant. |
| 41 | constexpr double kVelocityFinisher = 350.0; |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 42 | r->shot_interpolation_table = InterpolationTable<Values::ShotParams>( |
milind-u | b1b7e47 | 2021-10-03 17:11:19 -0700 | [diff] [blame] | 43 | {{40.00 * kInchesToMeters + kHalfRobotLength, {0.1, 10.6}}, |
| 44 | {113.5 * kInchesToMeters + kHalfRobotLength, {0.42, 13.2}}, |
| 45 | {168.5 * kInchesToMeters + kHalfRobotLength, {0.51, 13.2}}, |
| 46 | {231.3 * kInchesToMeters + kHalfRobotLength, {0.51, 13.2}}, |
| 47 | {276.5 * kInchesToMeters + kHalfRobotLength, {0.53, 13.2}}}); |
milind-u | 0a178a8 | 2021-09-28 18:42:09 -0700 | [diff] [blame] | 48 | |
| 49 | r->flywheel_shot_interpolation_table = |
| 50 | InterpolationTable<Values::FlywheelShotParams>( |
| 51 | {{10.6, {250.0, kVelocityFinisher}}, |
| 52 | {12.0, {275.0, kVelocityFinisher}}, |
| 53 | {13.2, {300.0, kVelocityFinisher}}, |
| 54 | {14.0, {325.0, kVelocityFinisher}}, |
| 55 | {14.6, {350.0, kVelocityFinisher}}, |
| 56 | {15.2, {375.0, kVelocityFinisher}}, |
| 57 | {15.6, {400.0, kVelocityFinisher}}, |
| 58 | {16.1, {425.0, kVelocityFinisher}}, |
| 59 | {16.3, {450.0, kVelocityFinisher}}, |
| 60 | {16.6, {475.0, kVelocityFinisher}}, |
| 61 | {17.0, {500.0, kVelocityFinisher}}}); |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 62 | |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 63 | // Hood constants. |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 64 | hood->zeroing_voltage = 2.0; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 65 | hood->operating_voltage = 12.0; |
| 66 | hood->zeroing_profile_params = {0.5, 3.0}; |
| 67 | hood->default_profile_params = {6.0, 30.0}; |
| 68 | hood->range = Values::kHoodRange(); |
| 69 | hood->make_integral_loop = |
| 70 | control_loops::superstructure::hood::MakeIntegralHoodLoop; |
| 71 | hood->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 72 | hood->zeroing_constants.zeroing_threshold = 0.0005; |
| 73 | hood->zeroing_constants.moving_buffer_size = 20; |
| 74 | hood->zeroing_constants.allowable_encoder_error = 0.9; |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 75 | hood->zeroing_constants.one_revolution_distance = |
| 76 | M_PI * 2.0 * constants::Values::kHoodEncoderRatio(); |
| 77 | hood->zeroing_constants.single_turn_middle_position = |
| 78 | Values::kHoodRange().middle(); |
| 79 | hood->zeroing_constants.single_turn_one_revolution_distance = |
| 80 | M_PI * 2.0 * constants::Values::kHoodSingleTurnEncoderRatio(); |
| 81 | hood->zeroing_constants.measured_absolute_position = 0; |
| 82 | hood->zeroing_constants.single_turn_measured_absolute_position = 0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 83 | |
Sabina Davis | e8d3899 | 2020-02-02 15:00:31 -0800 | [diff] [blame] | 84 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 85 | ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake = |
| 86 | &r->intake; |
| 87 | |
| 88 | // Intake constants. |
| 89 | intake->zeroing_voltage = 3.0; |
| 90 | intake->operating_voltage = 12.0; |
| 91 | intake->zeroing_profile_params = {0.5, 3.0}; |
| 92 | intake->default_profile_params = {6.0, 30.0}; |
| 93 | intake->range = Values::kIntakeRange(); |
| 94 | intake->make_integral_loop = |
| 95 | control_loops::superstructure::intake::MakeIntegralIntakeLoop; |
| 96 | intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
| 97 | intake->zeroing_constants.one_revolution_distance = |
| 98 | M_PI * 2.0 * constants::Values::kIntakeEncoderRatio(); |
| 99 | intake->zeroing_constants.zeroing_threshold = 0.0005; |
| 100 | intake->zeroing_constants.moving_buffer_size = 20; |
| 101 | intake->zeroing_constants.allowable_encoder_error = 0.9; |
| 102 | intake->zeroing_constants.middle_position = Values::kIntakeRange().middle(); |
| 103 | |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 104 | Values::PotAndAbsEncoderConstants *const turret = &r->turret; |
| 105 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 106 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 107 | *const turret_params = &turret->subsystem_params; |
| 108 | |
ravago | 901c426 | 2020-02-16 15:33:14 -0800 | [diff] [blame] | 109 | // Turret Constants |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 110 | turret_params->zeroing_voltage = 4.0; |
James Kuszmaul | 519585d | 2020-03-08 22:32:48 -0700 | [diff] [blame] | 111 | turret_params->operating_voltage = 8.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 112 | // TODO(austin): Tune these. |
| 113 | turret_params->zeroing_profile_params = {0.5, 2.0}; |
| 114 | turret_params->default_profile_params = {15.0, 40.0}; |
| 115 | turret_params->range = Values::kTurretRange(); |
| 116 | turret_params->make_integral_loop = |
| 117 | &control_loops::superstructure::turret::MakeIntegralTurretLoop; |
| 118 | turret_params->zeroing_constants.average_filter_size = |
| 119 | Values::kZeroingSampleSize; |
| 120 | turret_params->zeroing_constants.one_revolution_distance = |
| 121 | M_PI * 2.0 * constants::Values::kTurretEncoderRatio(); |
| 122 | turret_params->zeroing_constants.zeroing_threshold = 0.0005; |
| 123 | turret_params->zeroing_constants.moving_buffer_size = 20; |
| 124 | turret_params->zeroing_constants.allowable_encoder_error = 0.9; |
| 125 | |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 126 | CHECK_LE(hood->range.range(), |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 127 | hood->zeroing_constants.single_turn_one_revolution_distance); |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 128 | CHECK_LE(intake->range.range(), |
| 129 | intake->zeroing_constants.one_revolution_distance); |
| 130 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 131 | switch (team) { |
| 132 | // A set of constants for tests. |
| 133 | case 1: |
James Kuszmaul | 6535d56 | 2020-05-09 16:58:13 -0700 | [diff] [blame] | 134 | case kSpareRoborioTeamNumber: |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case kCompTeamNumber: |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 138 | intake->zeroing_constants.measured_absolute_position = |
| 139 | 1.42977866919024 - Values::kIntakeZero(); |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 140 | |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 141 | turret->potentiometer_offset = 5.52519370141247 + 0.00853506822980376 + |
James Kuszmaul | f774d2d | 2021-10-03 08:02:35 -0700 | [diff] [blame] | 142 | 0.0109413725126625 - 0.223719825811759 + |
Austin Schuh | d8a0470 | 2021-10-09 15:09:07 -0700 | [diff] [blame^] | 143 | 0.261356551915472 - 0.0490168170767848; |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 144 | turret_params->zeroing_constants.measured_absolute_position = |
Austin Schuh | d8a0470 | 2021-10-09 15:09:07 -0700 | [diff] [blame^] | 145 | 2.37257083307489; |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 146 | |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 147 | hood->zeroing_constants.measured_absolute_position = 0.0344482433884915; |
| 148 | hood->zeroing_constants.single_turn_measured_absolute_position = |
| 149 | 0.31055891442198; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 150 | break; |
| 151 | |
| 152 | case kPracticeTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 153 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 154 | |
Austin Schuh | 31904ee | 2021-03-06 23:52:08 -0800 | [diff] [blame] | 155 | intake->zeroing_constants.measured_absolute_position = 0.347; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 156 | |
Austin Schuh | 31904ee | 2021-03-06 23:52:08 -0800 | [diff] [blame] | 157 | turret->potentiometer_offset = 5.3931926228241; |
| 158 | turret_params->zeroing_constants.measured_absolute_position = 4.22; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 159 | break; |
| 160 | |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 161 | case Values::kCodingRobotTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 162 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 163 | |
Sabina Davis | e8d3899 | 2020-02-02 15:00:31 -0800 | [diff] [blame] | 164 | intake->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 165 | |
| 166 | turret->potentiometer_offset = 0.0; |
| 167 | turret_params->zeroing_constants.measured_absolute_position = 0.0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 168 | break; |
| 169 | |
| 170 | default: |
| 171 | AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 172 | } |
| 173 | |
| 174 | return r; |
| 175 | } |
| 176 | |
| 177 | void DoGetValues(const Values **result) { |
| 178 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 179 | AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 180 | *result = DoGetValuesForTeam(team); |
| 181 | } |
| 182 | |
| 183 | } // namespace |
| 184 | |
| 185 | const Values &GetValues() { |
| 186 | static absl::once_flag once; |
| 187 | static const Values *result; |
| 188 | absl::call_once(once, DoGetValues, &result); |
| 189 | return *result; |
| 190 | } |
| 191 | |
| 192 | const Values &GetValuesForTeam(uint16_t team_number) { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 193 | static aos::stl_mutex mutex; |
| 194 | std::unique_lock<aos::stl_mutex> locker(mutex); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 195 | |
ravago | 901c426 | 2020-02-16 15:33:14 -0800 | [diff] [blame] | 196 | // IMPORTANT: This declaration has to stay after the mutex is locked to |
| 197 | // avoid race conditions. |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 198 | static ::std::map<uint16_t, const Values *> values; |
| 199 | |
| 200 | if (values.count(team_number) == 0) { |
| 201 | values[team_number] = DoGetValuesForTeam(team_number); |
| 202 | #if __has_feature(address_sanitizer) |
| 203 | __lsan_ignore_object(values[team_number]); |
| 204 | #endif |
| 205 | } |
| 206 | return *values[team_number]; |
| 207 | } |
| 208 | |
| 209 | } // namespace constants |
| 210 | } // namespace y2020 |