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, |
| 45 | Values::kHoodEncoderRatio, Values::kHoodPotRatio, |
| 46 | Values::kHoodEncoderIndexDifference, 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, |
| 50 | Values::kTurretEncoderRatio, Values::kTurretPotRatio, |
| 51 | Values::kTurretEncoderIndexDifference, |
| 52 | Values::kMaxTurretEncoderPulsesPerSecond; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 53 | constexpr ::frc971::constants::Range Values::kTurretRange; |
| 54 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 55 | constexpr double Values::kMaxIndexerEncoderCountsPerRevolution, |
| 56 | Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference, |
| 57 | Values::kMaxIndexerEncoderPulsesPerSecond; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 58 | |
| 59 | namespace { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 60 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 61 | const uint16_t kCompTeamNumber = 971; |
| 62 | const uint16_t kPracticeTeamNumber = 9971; |
| 63 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 64 | const Values *DoGetValuesForTeam(uint16_t team) { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 65 | Values *const r = new Values(); |
| 66 | Values::Intake *const intake = &r->intake; |
| 67 | Values::Turret *const turret = &r->turret; |
| 68 | Values::Hood *const hood = &r->hood; |
| 69 | |
| 70 | r->drivetrain_max_speed = 5; |
| 71 | |
| 72 | intake->zeroing.average_filter_size = Values::kZeroingSampleSize; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 73 | intake->zeroing.one_revolution_distance = Values::kIntakeEncoderIndexDifference; |
| 74 | intake->zeroing.measured_absolute_position = 0; |
| 75 | intake->zeroing.zeroing_threshold = 0.3; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 76 | |
| 77 | turret->zeroing.average_filter_size = Values::kZeroingSampleSize; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 78 | turret->zeroing.one_revolution_distance = Values::kTurretEncoderIndexDifference; |
| 79 | turret->zeroing.measured_absolute_position = 0; |
| 80 | turret->zeroing.zeroing_threshold = 0.3; |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 81 | |
| 82 | hood->zeroing.average_filter_size = Values::kZeroingSampleSize; |
| 83 | hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference; |
| 84 | hood->zeroing.measured_index_position = 0.1; |
| 85 | hood->zeroing.allowable_encoder_error = 0.3; |
| 86 | |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 87 | switch (team) { |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 88 | // A set of constants for tests. |
| 89 | case 1: |
| 90 | intake->pot_offset = 0; |
| 91 | turret->pot_offset = 0; |
| 92 | hood->pot_offset = 0.1; |
| 93 | r->down_error = 0; |
| 94 | r->vision_name = "test"; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 95 | break; |
| 96 | |
| 97 | case kCompTeamNumber: |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 98 | intake->pot_offset = 0; |
| 99 | turret->pot_offset = 0; |
| 100 | hood->pot_offset = 0.1; |
| 101 | r->down_error = 0; |
| 102 | r->vision_name = "competition"; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 103 | break; |
| 104 | |
| 105 | case kPracticeTeamNumber: |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 106 | intake->pot_offset = 0; |
| 107 | turret->pot_offset = 0; |
| 108 | hood->pot_offset = 0.1; |
| 109 | r->down_error = 0; |
| 110 | r->vision_name = "practice"; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 111 | break; |
| 112 | |
| 113 | default: |
| 114 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 115 | } |
Brian Silverman | db8498a | 2017-02-11 17:16:09 -0800 | [diff] [blame] | 116 | |
| 117 | return r; |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | const Values *DoGetValues() { |
| 121 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 122 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 123 | return DoGetValuesForTeam(team); |
| 124 | } |
| 125 | |
| 126 | } // namespace |
| 127 | |
| 128 | const Values &GetValues() { |
| 129 | static ::aos::Once<const Values> once(DoGetValues); |
| 130 | return *once.Get(); |
| 131 | } |
| 132 | |
| 133 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 134 | static ::aos::Mutex mutex; |
| 135 | ::aos::MutexLocker locker(&mutex); |
| 136 | |
| 137 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 138 | // race conditions. |
| 139 | static ::std::map<uint16_t, const Values *> values; |
| 140 | |
| 141 | if (values.count(team_number) == 0) { |
| 142 | values[team_number] = DoGetValuesForTeam(team_number); |
| 143 | #if __has_feature(address_sanitizer) |
| 144 | __lsan_ignore_object(values[team_number]); |
| 145 | #endif |
| 146 | } |
| 147 | return *values[team_number]; |
| 148 | } |
| 149 | |
| 150 | } // namespace constants |
| 151 | } // namespace y2017 |