Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 1 | #include "y2017/constants.h" |
| 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cinttypes> |
| 4 | #include <cmath> |
| 5 | #include <cstdint> |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 6 | #include <map> |
| 7 | |
| 8 | #if __has_feature(address_sanitizer) |
| 9 | #include "sanitizer/lsan_interface.h" |
| 10 | #endif |
| 11 | |
John Park | bb458cd | 2019-11-03 19:18:43 -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" |
Ed Jordan | 8683f43 | 2017-02-12 00:13:26 +0000 | [diff] [blame] | 16 | #include "y2017/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
| 17 | #include "y2017/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h" |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 18 | |
| 19 | #ifndef M_PI |
| 20 | #define M_PI 3.14159265358979323846 |
| 21 | #endif |
| 22 | |
| 23 | namespace y2017 { |
| 24 | namespace constants { |
| 25 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 26 | const int Values::kZeroingSampleSize; |
| 27 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 28 | constexpr double Values::kDrivetrainCyclesPerRevolution, |
| 29 | Values::kDrivetrainEncoderCountsPerRevolution, |
| 30 | Values::kDrivetrainEncoderRatio, |
| 31 | Values::kMaxDrivetrainEncoderPulsesPerSecond; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 32 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 33 | constexpr double Values::kShooterEncoderCountsPerRevolution, |
| 34 | Values::kShooterEncoderRatio, Values::kMaxShooterEncoderPulsesPerSecond; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 35 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 36 | constexpr double Values::kIntakeEncoderCountsPerRevolution, |
| 37 | Values::kIntakeEncoderRatio, Values::kIntakePotRatio, |
| 38 | Values::kIntakeEncoderIndexDifference, |
| 39 | Values::kMaxIntakeEncoderPulsesPerSecond; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 40 | constexpr ::frc971::constants::Range Values::kIntakeRange; |
| 41 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 42 | constexpr double Values::kHoodEncoderCountsPerRevolution, |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 43 | Values::kHoodEncoderRatio, Values::kHoodEncoderIndexDifference, |
| 44 | Values::kMaxHoodEncoderPulsesPerSecond; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 45 | constexpr ::frc971::constants::Range Values::kHoodRange; |
| 46 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 47 | constexpr double Values::kTurretEncoderCountsPerRevolution, |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 48 | Values::kTurretEncoderRatio, Values::kMaxTurretEncoderPulsesPerSecond; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 49 | constexpr ::frc971::constants::Range Values::kTurretRange; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 50 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 51 | constexpr double Values::kIndexerEncoderCountsPerRevolution, |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 52 | Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference, |
| 53 | Values::kMaxIndexerEncoderPulsesPerSecond; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 54 | |
| 55 | namespace { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 56 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 57 | const uint16_t kCompTeamNumber = 971; |
| 58 | const uint16_t kPracticeTeamNumber = 9971; |
| 59 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 60 | const Values *DoGetValuesForTeam(uint16_t team) { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 61 | Values *const r = new Values(); |
| 62 | Values::Intake *const intake = &r->intake; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 63 | Values::Hood *const hood = &r->hood; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 64 | Values::Column *const column = &r->column; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 65 | |
| 66 | r->drivetrain_max_speed = 5; |
| 67 | |
| 68 | intake->zeroing.average_filter_size = Values::kZeroingSampleSize; |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 69 | intake->zeroing.one_revolution_distance = |
| 70 | Values::kIntakeEncoderIndexDifference; |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 71 | intake->zeroing.zeroing_threshold = 0.0005; |
| 72 | intake->zeroing.moving_buffer_size = 20; |
Austin Schuh | 53a2c45 | 2017-03-22 21:18:20 -0700 | [diff] [blame] | 73 | intake->zeroing.allowable_encoder_error = 1.9; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 74 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 75 | column->turret_zeroed_distance = M_PI / 2.0; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 76 | column->indexer_zeroing.index_difference = 2.0 * M_PI; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 77 | column->indexer_zeroing.hall_trigger_zeroing_length = 2; |
| 78 | column->indexer_zeroing.zeroing_move_direction = true; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 79 | column->turret_zeroing.index_difference = 2.0 * M_PI; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 80 | column->turret_zeroing.hall_trigger_zeroing_length = 2; |
| 81 | column->turret_zeroing.zeroing_move_direction = false; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 82 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 83 | hood->zeroing.index_pulse_count = 2; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 84 | hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 85 | hood->zeroing.known_index_pulse = 0; |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 86 | hood->zeroing.allowable_encoder_error = 0.05; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 87 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 88 | switch (team) { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 89 | // A set of constants for tests. |
| 90 | case 1: |
| 91 | intake->pot_offset = 0; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 92 | intake->zeroing.measured_absolute_position = 0; |
| 93 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 94 | // TODO(austin): Swap the turret and indexer limits and make sure the |
| 95 | // tests still pass. |
| 96 | column->indexer_zeroing.lower_hall_position = 0.1; |
| 97 | column->indexer_zeroing.upper_hall_position = 0.2; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 98 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 99 | column->turret_zeroing.lower_hall_position = 2; |
| 100 | column->turret_zeroing.upper_hall_position = 2.1; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 101 | |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 102 | hood->pot_offset = 0.1; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 103 | hood->zeroing.measured_index_position = 0.05; |
| 104 | |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 105 | r->down_error = 0; |
| 106 | r->vision_name = "test"; |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 107 | r->vision_error = -0.030; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 108 | break; |
| 109 | |
| 110 | case kCompTeamNumber: |
Austin Schuh | 9ef80a9 | 2017-06-24 13:34:28 -0700 | [diff] [blame] | 111 | intake->pot_offset = 0.270738; |
| 112 | intake->zeroing.measured_absolute_position = 0.015669; |
Austin Schuh | 23cc9ed | 2017-02-24 19:14:06 -0800 | [diff] [blame] | 113 | |
Austin Schuh | 53a2c45 | 2017-03-22 21:18:20 -0700 | [diff] [blame] | 114 | column->indexer_zeroing.lower_hall_position = 5.201948; |
| 115 | column->indexer_zeroing.upper_hall_position = 5.508744; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 116 | |
Austin Schuh | 53a2c45 | 2017-03-22 21:18:20 -0700 | [diff] [blame] | 117 | column->turret_zeroing.lower_hall_position = -4.861087; |
| 118 | column->turret_zeroing.upper_hall_position = -4.680861; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 119 | |
Austin Schuh | 9ef80a9 | 2017-06-24 13:34:28 -0700 | [diff] [blame] | 120 | // Original hood calibration |
| 121 | // hood->zeroing.measured_index_position = 0.234766; |
| 122 | hood->zeroing.measured_index_position = 0.124275; |
Austin Schuh | 23cc9ed | 2017-02-24 19:14:06 -0800 | [diff] [blame] | 123 | |
| 124 | r->down_error = 0; |
| 125 | r->vision_name = "competition"; |
Austin Schuh | 9ef80a9 | 2017-06-24 13:34:28 -0700 | [diff] [blame] | 126 | r->vision_error = 0.0; |
Austin Schuh | 23cc9ed | 2017-02-24 19:14:06 -0800 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | case kPracticeTeamNumber: |
Austin Schuh | c74e9b6 | 2017-04-09 18:27:40 -0700 | [diff] [blame] | 130 | intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602 + 0.010722 + |
| 131 | 0.012880 - 0.01743; |
| 132 | intake->zeroing.measured_absolute_position = 0.043179; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 133 | |
Austin Schuh | c74e9b6 | 2017-04-09 18:27:40 -0700 | [diff] [blame] | 134 | column->indexer_zeroing.lower_hall_position = 2.594181; |
| 135 | column->indexer_zeroing.upper_hall_position = 2.886952; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 136 | |
Austin Schuh | c74e9b6 | 2017-04-09 18:27:40 -0700 | [diff] [blame] | 137 | column->turret_zeroing.lower_hall_position = -4.918530; |
| 138 | column->turret_zeroing.upper_hall_position = -4.720353; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 139 | |
Austin Schuh | c74e9b6 | 2017-04-09 18:27:40 -0700 | [diff] [blame] | 140 | hood->zeroing.measured_index_position = 0.124275; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 141 | |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 142 | r->down_error = 0; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 143 | r->vision_name = "practice"; |
Austin Schuh | 25db126 | 2017-04-05 19:39:55 -0700 | [diff] [blame] | 144 | r->vision_error = 0.0; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 145 | break; |
| 146 | |
| 147 | default: |
Brian Silverman | f4d329c | 2021-11-04 19:32:10 -0700 | [diff] [blame^] | 148 | LOG(FATAL) << "unknown team: " << team; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 149 | } |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 150 | |
| 151 | return r; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 154 | void DoGetValues(const Values **result) { |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +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 | bb458cd | 2019-11-03 19:18:43 -0800 | [diff] [blame] | 157 | *result = DoGetValuesForTeam(team); |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | } // namespace |
| 161 | |
| 162 | const Values &GetValues() { |
John Park | bb458cd | 2019-11-03 19:18:43 -0800 | [diff] [blame] | 163 | static absl::once_flag once; |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 164 | static const Values *result; |
John Park | bb458cd | 2019-11-03 19:18:43 -0800 | [diff] [blame] | 165 | absl::call_once(once, DoGetValues, &result); |
| 166 | return *result; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +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); |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +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 | |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 186 | Values::ShotParams Values::ShotParams::BlendY(double coefficient, |
| 187 | Values::ShotParams a1, |
| 188 | Values::ShotParams a2) { |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 189 | using ::frc971::shooter_interpolation::Blend; |
Brian Silverman | 1463c09 | 2020-10-30 17:28:24 -0700 | [diff] [blame] | 190 | return Values::ShotParams{ |
| 191 | Blend(coefficient, a1.angle, a2.angle), |
| 192 | Blend(coefficient, a1.power, a2.power), |
| 193 | Blend(coefficient, a1.indexer_velocity, a2.indexer_velocity)}; |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 196 | } // namespace constants |
| 197 | } // namespace y2017 |