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