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