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 | |
James Kuszmaul | 98154a2 | 2021-04-03 16:09:29 -0700 | [diff] [blame] | 36 | constexpr double kFeetToMeters = 0.0254 * 12.0; |
| 37 | // Approximate robot length, for converting estimates from the doc below. |
| 38 | // Rounded up from exact estimate, since I'm not sure if the original estimate |
| 39 | // includes bumpers. |
| 40 | constexpr double kRobotLength = 0.9; |
| 41 | // { distance_to_target, { hood_angle, accelerator_power, finisher_power }} |
| 42 | // Current settings based on |
| 43 | // https://docs.google.com/document/d/1NR9F-ntlSoqZ9LqDzLjn-c14t8ZrppawCCG7wQy47RU/edit |
| 44 | r->shot_interpolation_table = InterpolationTable<Values::ShotParams>( |
| 45 | {{7.6 * kFeetToMeters - kRobotLength, {0.115, 197.0, 175.0}}, |
| 46 | {7.6 * kFeetToMeters + kRobotLength, {0.31, 265.0, 235.0}}, |
| 47 | {12.6 * kFeetToMeters + kRobotLength, {0.4, 292.0, 260.0}}, |
| 48 | {17.6 * kFeetToMeters + kRobotLength, {0.52, 365.0, 325.0}}}); |
| 49 | |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 50 | // Hood constants. |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 51 | hood->zeroing_voltage = 2.0; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 52 | hood->operating_voltage = 12.0; |
| 53 | hood->zeroing_profile_params = {0.5, 3.0}; |
| 54 | hood->default_profile_params = {6.0, 30.0}; |
| 55 | hood->range = Values::kHoodRange(); |
| 56 | hood->make_integral_loop = |
| 57 | control_loops::superstructure::hood::MakeIntegralHoodLoop; |
| 58 | hood->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 59 | hood->zeroing_constants.zeroing_threshold = 0.0005; |
| 60 | hood->zeroing_constants.moving_buffer_size = 20; |
| 61 | hood->zeroing_constants.allowable_encoder_error = 0.9; |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 62 | hood->zeroing_constants.one_revolution_distance = |
| 63 | M_PI * 2.0 * constants::Values::kHoodEncoderRatio(); |
| 64 | hood->zeroing_constants.single_turn_middle_position = |
| 65 | Values::kHoodRange().middle(); |
| 66 | hood->zeroing_constants.single_turn_one_revolution_distance = |
| 67 | M_PI * 2.0 * constants::Values::kHoodSingleTurnEncoderRatio(); |
| 68 | hood->zeroing_constants.measured_absolute_position = 0; |
| 69 | hood->zeroing_constants.single_turn_measured_absolute_position = 0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 70 | |
Sabina Davis | e8d3899 | 2020-02-02 15:00:31 -0800 | [diff] [blame] | 71 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 72 | ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake = |
| 73 | &r->intake; |
| 74 | |
| 75 | // Intake constants. |
| 76 | intake->zeroing_voltage = 3.0; |
| 77 | intake->operating_voltage = 12.0; |
| 78 | intake->zeroing_profile_params = {0.5, 3.0}; |
| 79 | intake->default_profile_params = {6.0, 30.0}; |
| 80 | intake->range = Values::kIntakeRange(); |
| 81 | intake->make_integral_loop = |
| 82 | control_loops::superstructure::intake::MakeIntegralIntakeLoop; |
| 83 | intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
| 84 | intake->zeroing_constants.one_revolution_distance = |
| 85 | M_PI * 2.0 * constants::Values::kIntakeEncoderRatio(); |
| 86 | intake->zeroing_constants.zeroing_threshold = 0.0005; |
| 87 | intake->zeroing_constants.moving_buffer_size = 20; |
| 88 | intake->zeroing_constants.allowable_encoder_error = 0.9; |
| 89 | intake->zeroing_constants.middle_position = Values::kIntakeRange().middle(); |
| 90 | |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 91 | Values::PotAndAbsEncoderConstants *const turret = &r->turret; |
| 92 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 93 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 94 | *const turret_params = &turret->subsystem_params; |
| 95 | |
ravago | 901c426 | 2020-02-16 15:33:14 -0800 | [diff] [blame] | 96 | // Turret Constants |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 97 | turret_params->zeroing_voltage = 4.0; |
James Kuszmaul | 519585d | 2020-03-08 22:32:48 -0700 | [diff] [blame] | 98 | turret_params->operating_voltage = 8.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 99 | // TODO(austin): Tune these. |
| 100 | turret_params->zeroing_profile_params = {0.5, 2.0}; |
| 101 | turret_params->default_profile_params = {15.0, 40.0}; |
| 102 | turret_params->range = Values::kTurretRange(); |
| 103 | turret_params->make_integral_loop = |
| 104 | &control_loops::superstructure::turret::MakeIntegralTurretLoop; |
| 105 | turret_params->zeroing_constants.average_filter_size = |
| 106 | Values::kZeroingSampleSize; |
| 107 | turret_params->zeroing_constants.one_revolution_distance = |
| 108 | M_PI * 2.0 * constants::Values::kTurretEncoderRatio(); |
| 109 | turret_params->zeroing_constants.zeroing_threshold = 0.0005; |
| 110 | turret_params->zeroing_constants.moving_buffer_size = 20; |
| 111 | turret_params->zeroing_constants.allowable_encoder_error = 0.9; |
| 112 | |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 113 | CHECK_LE(hood->range.range(), |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 114 | hood->zeroing_constants.single_turn_one_revolution_distance); |
Austin Schuh | 9dcd520 | 2020-02-20 20:06:04 -0800 | [diff] [blame] | 115 | CHECK_LE(intake->range.range(), |
| 116 | intake->zeroing_constants.one_revolution_distance); |
| 117 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 118 | switch (team) { |
| 119 | // A set of constants for tests. |
| 120 | case 1: |
James Kuszmaul | 6535d56 | 2020-05-09 16:58:13 -0700 | [diff] [blame] | 121 | case kSpareRoborioTeamNumber: |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 122 | break; |
| 123 | |
| 124 | case kCompTeamNumber: |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 125 | intake->zeroing_constants.measured_absolute_position = |
| 126 | 1.42977866919024 - Values::kIntakeZero(); |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 127 | |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 128 | turret->potentiometer_offset = 5.52519370141247 + 0.00853506822980376 + |
| 129 | 0.0109413725126625 - 0.223719825811759; |
Sabina Davis | f7afd11 | 2020-02-23 13:42:14 -0800 | [diff] [blame] | 130 | turret_params->zeroing_constants.measured_absolute_position = |
Austin Schuh | 2fb2364 | 2020-02-29 15:10:51 -0800 | [diff] [blame] | 131 | 0.547478339799516; |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 132 | |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 133 | hood->zeroing_constants.measured_absolute_position = 0.0344482433884915; |
| 134 | hood->zeroing_constants.single_turn_measured_absolute_position = |
| 135 | 0.31055891442198; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 136 | break; |
| 137 | |
| 138 | case kPracticeTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 139 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 140 | |
Austin Schuh | 31904ee | 2021-03-06 23:52:08 -0800 | [diff] [blame] | 141 | intake->zeroing_constants.measured_absolute_position = 0.347; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 142 | |
Austin Schuh | 31904ee | 2021-03-06 23:52:08 -0800 | [diff] [blame] | 143 | turret->potentiometer_offset = 5.3931926228241; |
| 144 | turret_params->zeroing_constants.measured_absolute_position = 4.22; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 145 | break; |
| 146 | |
Austin Schuh | 83873c3 | 2020-02-22 14:58:39 -0800 | [diff] [blame] | 147 | case Values::kCodingRobotTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame] | 148 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 149 | |
Sabina Davis | e8d3899 | 2020-02-02 15:00:31 -0800 | [diff] [blame] | 150 | intake->zeroing_constants.measured_absolute_position = 0.0; |
Kai Tinkess | 10943cf | 2020-02-01 15:49:57 -0800 | [diff] [blame] | 151 | |
| 152 | turret->potentiometer_offset = 0.0; |
| 153 | turret_params->zeroing_constants.measured_absolute_position = 0.0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 154 | break; |
| 155 | |
| 156 | default: |
| 157 | AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 158 | } |
| 159 | |
| 160 | return r; |
| 161 | } |
| 162 | |
| 163 | void DoGetValues(const Values **result) { |
| 164 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 165 | AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 166 | *result = DoGetValuesForTeam(team); |
| 167 | } |
| 168 | |
| 169 | } // namespace |
| 170 | |
| 171 | const Values &GetValues() { |
| 172 | static absl::once_flag once; |
| 173 | static const Values *result; |
| 174 | absl::call_once(once, DoGetValues, &result); |
| 175 | return *result; |
| 176 | } |
| 177 | |
| 178 | const Values &GetValuesForTeam(uint16_t team_number) { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 179 | static aos::stl_mutex mutex; |
| 180 | std::unique_lock<aos::stl_mutex> locker(mutex); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 181 | |
ravago | 901c426 | 2020-02-16 15:33:14 -0800 | [diff] [blame] | 182 | // IMPORTANT: This declaration has to stay after the mutex is locked to |
| 183 | // avoid race conditions. |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 184 | static ::std::map<uint16_t, const Values *> values; |
| 185 | |
| 186 | if (values.count(team_number) == 0) { |
| 187 | values[team_number] = DoGetValuesForTeam(team_number); |
| 188 | #if __has_feature(address_sanitizer) |
| 189 | __lsan_ignore_object(values[team_number]); |
| 190 | #endif |
| 191 | } |
| 192 | return *values[team_number]; |
| 193 | } |
| 194 | |
| 195 | } // namespace constants |
| 196 | } // namespace y2020 |