Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 1 | #include "y2016/constants.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cinttypes> |
| 4 | #include <cmath> |
| 5 | #include <cstdint> |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 6 | #include <map> |
| 7 | |
| 8 | #if __has_feature(address_sanitizer) |
| 9 | #include "sanitizer/lsan_interface.h" |
| 10 | #endif |
| 11 | |
John Park | a13bd37 | 2019-11-11 19:26:58 -0800 | [diff] [blame] | 12 | #include "absl/base/call_once.h" |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 13 | #include "absl/log/check.h" |
| 14 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 15 | |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 16 | #include "aos/network/team_number.h" |
| 17 | #include "aos/stl_mutex/stl_mutex.h" |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 18 | #include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 19 | #include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 20 | |
| 21 | #ifndef M_PI |
| 22 | #define M_PI 3.14159265358979323846 |
| 23 | #endif |
| 24 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 25 | namespace y2016::constants { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 26 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 27 | // ///// Mutual constants between robots. ///// |
| 28 | const int Values::kZeroingSampleSize; |
| 29 | |
Austin Schuh | 33bc684 | 2016-02-17 00:38:51 -0800 | [diff] [blame] | 30 | constexpr double Values::kDrivetrainEncoderRatio, Values::kShooterEncoderRatio, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 31 | Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio, |
| 32 | Values::kWristEncoderRatio, Values::kIntakePotRatio, |
| 33 | Values::kShoulderPotRatio, Values::kWristPotRatio, |
| 34 | Values::kIntakeEncoderIndexDifference, |
| 35 | Values::kShoulderEncoderIndexDifference, |
| 36 | Values::kWristEncoderIndexDifference; |
Brian Silverman | ebca77a | 2016-02-14 22:14:00 -0500 | [diff] [blame] | 37 | constexpr ::frc971::constants::Range Values::kIntakeRange, |
| 38 | Values::kShoulderRange, Values::kWristRange; |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 39 | |
| 40 | namespace { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 41 | const uint16_t kCompTeamNumber = 971; |
| 42 | const uint16_t kPracticeTeamNumber = 9971; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 43 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 44 | // ///// Dynamic constants. ///// |
Austin Schuh | 04c894e | 2016-02-13 23:54:42 -0800 | [diff] [blame] | 45 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 46 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 47 | switch (team) { |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 48 | case 1: // for tests |
| 49 | return new Values{ |
| 50 | 5.0, // drivetrain max speed |
| 51 | |
| 52 | // Intake |
| 53 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 54 | 0.0, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 55 | {{}, |
| 56 | Values::kZeroingSampleSize, |
| 57 | Values::kIntakeEncoderIndexDifference, |
| 58 | 0.0, |
| 59 | 0.3}, |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 60 | }, |
| 61 | |
| 62 | // Shoulder |
| 63 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 64 | 0.0, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 65 | {{}, |
| 66 | Values::kZeroingSampleSize, |
| 67 | Values::kShoulderEncoderIndexDifference, |
| 68 | 0.0, |
| 69 | 0.3}, |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 70 | }, |
| 71 | |
| 72 | // Wrist |
| 73 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 74 | 0.0, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 75 | {{}, |
| 76 | Values::kZeroingSampleSize, |
| 77 | Values::kWristEncoderIndexDifference, |
| 78 | 0.0, |
| 79 | 0.3}, |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 80 | }, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 81 | |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 82 | 0.0, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 83 | "practice", |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 84 | }; |
| 85 | break; |
| 86 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 87 | case kCompTeamNumber: |
| 88 | return new Values{ |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 89 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 90 | |
| 91 | // Intake |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 92 | { |
| 93 | // Value to add to the pot reading for the intake. |
| 94 | -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 - |
| 95 | 0.0725, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 96 | {{}, |
| 97 | Values::kZeroingSampleSize, |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 98 | Values::kIntakeEncoderIndexDifference, |
| 99 | // Location of an index pulse. |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 100 | 0.018008, |
| 101 | 2.5}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 102 | }, |
| 103 | |
| 104 | // Shoulder |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 105 | { |
| 106 | // Value to add to the pot reading for the shoulder. |
| 107 | -2.86275657117, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 108 | {{}, |
| 109 | Values::kZeroingSampleSize, |
| 110 | Values::kShoulderEncoderIndexDifference, |
| 111 | 0.097312, |
| 112 | 2.5}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 113 | }, |
| 114 | |
| 115 | // Wrist |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 116 | { |
| 117 | // Value to add to the pot reading for the wrist. |
| 118 | 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 - |
| 119 | 0.0548 - 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 + |
| 120 | 0.0166 - 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 121 | {{}, |
| 122 | Values::kZeroingSampleSize, |
| 123 | Values::kWristEncoderIndexDifference, |
| 124 | 0.000820, |
| 125 | 2.5}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 126 | }, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 127 | |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 128 | 0.0, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 129 | "competition", |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 130 | }; |
| 131 | break; |
| 132 | case kPracticeTeamNumber: |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 133 | return new Values{ |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 134 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 135 | |
| 136 | // Intake |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 137 | { |
| 138 | // Hard stop is 160.0185751389329 degrees. |
| 139 | -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) + |
| 140 | 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 141 | {{}, |
| 142 | Values::kZeroingSampleSize, |
| 143 | Values::kIntakeEncoderIndexDifference, |
| 144 | 0.332370, |
| 145 | 1.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 146 | }, |
| 147 | |
Austin Schuh | 449966b | 2016-03-11 21:27:05 -0800 | [diff] [blame] | 148 | // Shoulder (Now calibrated at 0) |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 149 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 150 | -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 + |
| 151 | 0.0034 + 0.0065 - 0.0505, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 152 | {{}, |
| 153 | Values::kZeroingSampleSize, |
| 154 | Values::kShoulderEncoderIndexDifference, |
| 155 | 0.027180, |
| 156 | 1.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 157 | }, |
| 158 | |
| 159 | // Wrist |
| 160 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 161 | 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 - |
| 162 | 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 + |
| 163 | 0.1245, |
James Kuszmaul | d12497a | 2024-01-14 18:00:34 -0800 | [diff] [blame] | 164 | {{}, |
| 165 | Values::kZeroingSampleSize, |
| 166 | Values::kWristEncoderIndexDifference, |
| 167 | -0.263227, |
| 168 | 1.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 169 | }, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 170 | |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 171 | 0.011, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 172 | "practice", |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 173 | }; |
| 174 | break; |
| 175 | default: |
Brian Silverman | f4d329c | 2021-11-04 19:32:10 -0700 | [diff] [blame] | 176 | LOG(FATAL) << "unknown team: " << team; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
John Park | a13bd37 | 2019-11-11 19:26:58 -0800 | [diff] [blame] | 180 | void DoGetValues(const Values **result) { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 181 | uint16_t team = ::aos::network::GetTeamNumber(); |
Brian Silverman | f4d329c | 2021-11-04 19:32:10 -0700 | [diff] [blame] | 182 | LOG(INFO) << "creating a Constants for team: " << team; |
John Park | a13bd37 | 2019-11-11 19:26:58 -0800 | [diff] [blame] | 183 | *result = DoGetValuesForTeam(team); |
| 184 | return; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | } // namespace |
| 188 | |
| 189 | const Values &GetValues() { |
John Park | a13bd37 | 2019-11-11 19:26:58 -0800 | [diff] [blame] | 190 | static absl::once_flag once; |
| 191 | static const Values *result; |
| 192 | absl::call_once(once, DoGetValues, &result); |
| 193 | return *result; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | const Values &GetValuesForTeam(uint16_t team_number) { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 197 | static aos::stl_mutex mutex; |
| 198 | std::unique_lock<aos::stl_mutex> locker(mutex); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 199 | |
| 200 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 201 | // race conditions. |
| 202 | static ::std::map<uint16_t, const Values *> values; |
| 203 | |
| 204 | if (values.count(team_number) == 0) { |
| 205 | values[team_number] = DoGetValuesForTeam(team_number); |
| 206 | #if __has_feature(address_sanitizer) |
| 207 | __lsan_ignore_object(values[team_number]); |
| 208 | #endif |
| 209 | } |
| 210 | return *values[team_number]; |
| 211 | } |
| 212 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 213 | } // namespace y2016::constants |