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 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 24 | namespace y2016 { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 25 | namespace 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, |
| 55 | {Values::kZeroingSampleSize, |
| 56 | Values::kIntakeEncoderIndexDifference, 0.0, 0.3}, |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 57 | }, |
| 58 | |
| 59 | // Shoulder |
| 60 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 61 | 0.0, |
| 62 | {Values::kZeroingSampleSize, |
| 63 | Values::kShoulderEncoderIndexDifference, 0.0, 0.3}, |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 64 | }, |
| 65 | |
| 66 | // Wrist |
| 67 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 68 | 0.0, |
| 69 | {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference, |
| 70 | 0.0, 0.3}, |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 71 | }, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 72 | |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 73 | 0.0, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 74 | "practice", |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 75 | }; |
| 76 | break; |
| 77 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 78 | case kCompTeamNumber: |
| 79 | return new Values{ |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 80 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 81 | |
| 82 | // Intake |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 83 | { |
| 84 | // Value to add to the pot reading for the intake. |
| 85 | -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 - |
| 86 | 0.0725, |
| 87 | {Values::kZeroingSampleSize, |
| 88 | Values::kIntakeEncoderIndexDifference, |
| 89 | // Location of an index pulse. |
| 90 | 0.018008, 2.5}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 91 | }, |
| 92 | |
| 93 | // Shoulder |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 94 | { |
| 95 | // Value to add to the pot reading for the shoulder. |
| 96 | -2.86275657117, |
| 97 | {Values::kZeroingSampleSize, |
| 98 | Values::kShoulderEncoderIndexDifference, 0.097312, 2.5}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 99 | }, |
| 100 | |
| 101 | // Wrist |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 102 | { |
| 103 | // Value to add to the pot reading for the wrist. |
| 104 | 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 - |
| 105 | 0.0548 - 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 + |
| 106 | 0.0166 - 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741, |
| 107 | {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference, |
| 108 | 0.000820, 2.5}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 109 | }, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 110 | |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 111 | 0.0, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 112 | "competition", |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 113 | }; |
| 114 | break; |
| 115 | case kPracticeTeamNumber: |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 116 | return new Values{ |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 117 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 118 | |
| 119 | // Intake |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 120 | { |
| 121 | // Hard stop is 160.0185751389329 degrees. |
| 122 | -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) + |
| 123 | 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081, |
| 124 | {Values::kZeroingSampleSize, |
| 125 | Values::kIntakeEncoderIndexDifference, 0.332370, 1.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 126 | }, |
| 127 | |
Austin Schuh | 449966b | 2016-03-11 21:27:05 -0800 | [diff] [blame] | 128 | // Shoulder (Now calibrated at 0) |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 129 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 130 | -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 + |
| 131 | 0.0034 + 0.0065 - 0.0505, |
| 132 | {Values::kZeroingSampleSize, |
| 133 | Values::kShoulderEncoderIndexDifference, 0.027180, 1.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 134 | }, |
| 135 | |
| 136 | // Wrist |
| 137 | { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 138 | 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 - |
| 139 | 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 + |
| 140 | 0.1245, |
| 141 | {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference, |
| 142 | -0.263227, 1.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 143 | }, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 144 | |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 145 | 0.011, |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 146 | "practice", |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 147 | }; |
| 148 | break; |
| 149 | default: |
Brian Silverman | f4d329c | 2021-11-04 19:32:10 -0700 | [diff] [blame] | 150 | LOG(FATAL) << "unknown team: " << team; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
John Park | a13bd37 | 2019-11-11 19:26:58 -0800 | [diff] [blame] | 154 | void DoGetValues(const Values **result) { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 155 | uint16_t team = ::aos::network::GetTeamNumber(); |
Brian Silverman | f4d329c | 2021-11-04 19:32:10 -0700 | [diff] [blame] | 156 | LOG(INFO) << "creating a Constants for team: " << team; |
John Park | a13bd37 | 2019-11-11 19:26:58 -0800 | [diff] [blame] | 157 | *result = DoGetValuesForTeam(team); |
| 158 | return; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } // namespace |
| 162 | |
| 163 | const Values &GetValues() { |
John Park | a13bd37 | 2019-11-11 19:26:58 -0800 | [diff] [blame] | 164 | static absl::once_flag once; |
| 165 | static const Values *result; |
| 166 | absl::call_once(once, DoGetValues, &result); |
| 167 | return *result; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | const Values &GetValuesForTeam(uint16_t team_number) { |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 171 | static aos::stl_mutex mutex; |
| 172 | std::unique_lock<aos::stl_mutex> locker(mutex); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 173 | |
| 174 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 175 | // race conditions. |
| 176 | static ::std::map<uint16_t, const Values *> values; |
| 177 | |
| 178 | if (values.count(team_number) == 0) { |
| 179 | values[team_number] = DoGetValuesForTeam(team_number); |
| 180 | #if __has_feature(address_sanitizer) |
| 181 | __lsan_ignore_object(values[team_number]); |
| 182 | #endif |
| 183 | } |
| 184 | return *values[team_number]; |
| 185 | } |
| 186 | |
| 187 | } // namespace constants |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 188 | } // namespace y2016 |