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