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