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 | |
| 13 | #include "aos/common/logging/logging.h" |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 14 | #include "aos/common/mutex.h" |
Ed Jordan | 8683f43 | 2017-02-12 00:13:26 +0000 | [diff] [blame] | 15 | #include "aos/common/network/team_number.h" |
| 16 | #include "aos/common/once.h" |
| 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; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 51 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 52 | constexpr double Values::kIndexerEncoderCountsPerRevolution, |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 53 | Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference, |
| 54 | Values::kMaxIndexerEncoderPulsesPerSecond; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 55 | |
| 56 | namespace { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 57 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 58 | const uint16_t kCompTeamNumber = 971; |
| 59 | const uint16_t kPracticeTeamNumber = 9971; |
| 60 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 61 | const Values *DoGetValuesForTeam(uint16_t team) { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 62 | Values *const r = new Values(); |
| 63 | Values::Intake *const intake = &r->intake; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 64 | Values::Hood *const hood = &r->hood; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame^] | 65 | Values::Column *const column = &r->column; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 66 | |
| 67 | r->drivetrain_max_speed = 5; |
| 68 | |
| 69 | intake->zeroing.average_filter_size = Values::kZeroingSampleSize; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 70 | intake->zeroing.one_revolution_distance = 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; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 73 | intake->zeroing.allowable_encoder_error = 0.3; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 74 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame^] | 75 | column->indexer_zeroing.index_difference = 2.0 * M_PI; |
| 76 | column->turret_zeroing.index_difference = 2.0 * M_PI; |
| 77 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 78 | hood->zeroing.index_pulse_count = 2; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 79 | hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 80 | hood->zeroing.known_index_pulse = 0; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 81 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 82 | switch (team) { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 83 | // A set of constants for tests. |
| 84 | case 1: |
| 85 | intake->pot_offset = 0; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 86 | intake->zeroing.measured_absolute_position = 0; |
| 87 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame^] | 88 | column->indexer_zeroing.lower_hall_position = 2.0; |
| 89 | column->indexer_zeroing.upper_hall_position = 2.1; |
| 90 | |
| 91 | column->turret_zeroing.lower_hall_position = 0; |
| 92 | column->turret_zeroing.upper_hall_position = 0.1; |
| 93 | |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 94 | hood->pot_offset = 0.1; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 95 | hood->zeroing.measured_index_position = 0.05; |
| 96 | |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 97 | r->down_error = 0; |
| 98 | r->vision_name = "test"; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 99 | break; |
| 100 | |
| 101 | case kCompTeamNumber: |
Austin Schuh | 23cc9ed | 2017-02-24 19:14:06 -0800 | [diff] [blame] | 102 | intake->pot_offset = 0.26712; |
| 103 | intake->zeroing.measured_absolute_position = 0.008913; |
| 104 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame^] | 105 | column->indexer_zeroing.lower_hall_position = 2.0; |
| 106 | column->indexer_zeroing.upper_hall_position = 2.1; |
| 107 | |
| 108 | column->turret_zeroing.lower_hall_position = 0; |
| 109 | column->turret_zeroing.upper_hall_position = 0.1; |
| 110 | |
Austin Schuh | 23cc9ed | 2017-02-24 19:14:06 -0800 | [diff] [blame] | 111 | hood->zeroing.measured_index_position = 0.652898 - 0.488117; |
| 112 | |
| 113 | r->down_error = 0; |
| 114 | r->vision_name = "competition"; |
| 115 | break; |
| 116 | |
| 117 | case kPracticeTeamNumber: |
Austin Schuh | e6baace | 2017-03-04 15:05:02 -0800 | [diff] [blame] | 118 | intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602; |
| 119 | intake->zeroing.measured_absolute_position = 0.031437; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 120 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame^] | 121 | column->indexer_zeroing.lower_hall_position = 2.0; |
| 122 | column->indexer_zeroing.upper_hall_position = 2.1; |
| 123 | |
| 124 | column->turret_zeroing.lower_hall_position = 0; |
| 125 | column->turret_zeroing.upper_hall_position = 0.1; |
| 126 | |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 127 | hood->zeroing.measured_index_position = 0.655432 - 0.460505; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 128 | |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 129 | r->down_error = 0; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 130 | r->vision_name = "practice"; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 131 | break; |
| 132 | |
Austin Schuh | 23cc9ed | 2017-02-24 19:14:06 -0800 | [diff] [blame] | 133 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 134 | default: |
| 135 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 136 | } |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 137 | |
| 138 | return r; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | const Values *DoGetValues() { |
| 142 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 143 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 144 | return DoGetValuesForTeam(team); |
| 145 | } |
| 146 | |
| 147 | } // namespace |
| 148 | |
| 149 | const Values &GetValues() { |
| 150 | static ::aos::Once<const Values> once(DoGetValues); |
| 151 | return *once.Get(); |
| 152 | } |
| 153 | |
| 154 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 155 | static ::aos::Mutex mutex; |
| 156 | ::aos::MutexLocker locker(&mutex); |
| 157 | |
| 158 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 159 | // race conditions. |
| 160 | static ::std::map<uint16_t, const Values *> values; |
| 161 | |
| 162 | if (values.count(team_number) == 0) { |
| 163 | values[team_number] = DoGetValuesForTeam(team_number); |
| 164 | #if __has_feature(address_sanitizer) |
| 165 | __lsan_ignore_object(values[team_number]); |
| 166 | #endif |
| 167 | } |
| 168 | return *values[team_number]; |
| 169 | } |
| 170 | |
| 171 | } // namespace constants |
| 172 | } // namespace y2017 |