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