Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 1 | #include "y2016/constants.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 2 | |
| 3 | #include <math.h> |
| 4 | #include <stdint.h> |
| 5 | #include <inttypes.h> |
| 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" |
| 14 | #include "aos/common/once.h" |
| 15 | #include "aos/common/network/team_number.h" |
| 16 | #include "aos/common/mutex.h" |
| 17 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 18 | #include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h" |
| 19 | #include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 20 | |
| 21 | #ifndef M_PI |
| 22 | #define M_PI 3.14159265358979323846 |
| 23 | #endif |
| 24 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 25 | namespace y2016 { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 26 | namespace constants { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 27 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 28 | // ///// Mutual constants between robots. ///// |
| 29 | const int Values::kZeroingSampleSize; |
| 30 | |
| 31 | constexpr double Values::kDrivetrainEncoderRatio, Values::kLowGearRatio, |
Austin Schuh | 3130b37 | 2016-02-17 00:34:51 -0800 | [diff] [blame^] | 32 | Values::kHighGearRatio, Values::kShooterEncoderRatio, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 33 | Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio, |
| 34 | Values::kWristEncoderRatio, Values::kIntakePotRatio, |
| 35 | Values::kShoulderPotRatio, Values::kWristPotRatio, |
| 36 | Values::kIntakeEncoderIndexDifference, |
| 37 | Values::kShoulderEncoderIndexDifference, |
| 38 | Values::kWristEncoderIndexDifference; |
Brian Silverman | ebca77a | 2016-02-14 22:14:00 -0500 | [diff] [blame] | 39 | constexpr ::frc971::constants::Range Values::kIntakeRange, |
| 40 | Values::kShoulderRange, Values::kWristRange; |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 41 | |
| 42 | namespace { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 43 | const uint16_t kCompTeamNumber = 971; |
| 44 | const uint16_t kPracticeTeamNumber = 9971; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 45 | |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 46 | // ///// Dynamic constants. ///// |
Austin Schuh | 04c894e | 2016-02-13 23:54:42 -0800 | [diff] [blame] | 47 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 48 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 49 | switch (team) { |
Comran Morshed | 315cf39 | 2016-02-14 20:40:22 +0000 | [diff] [blame] | 50 | case 1: // for tests |
| 51 | return new Values{ |
| 52 | 5.0, // drivetrain max speed |
| 53 | |
| 54 | // Intake |
| 55 | { |
| 56 | 0.0, |
| 57 | {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference, |
| 58 | 0.0, 0.3}, |
| 59 | }, |
| 60 | |
| 61 | // Shoulder |
| 62 | { |
| 63 | 0.0, |
| 64 | {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference, |
| 65 | 0.0, 0.3}, |
| 66 | }, |
| 67 | |
| 68 | // Wrist |
| 69 | { |
| 70 | 0.0, |
| 71 | {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference, |
| 72 | 0.0, 0.3}, |
| 73 | }, |
| 74 | }; |
| 75 | break; |
| 76 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 77 | case kCompTeamNumber: |
| 78 | return new Values{ |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 79 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 80 | |
| 81 | // Intake |
| 82 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 83 | 0.0, |
| 84 | {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference, |
| 85 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 86 | }, |
| 87 | |
| 88 | // Shoulder |
| 89 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 90 | 0.0, |
| 91 | {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference, |
| 92 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 93 | }, |
| 94 | |
| 95 | // Wrist |
| 96 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 97 | 0.0, |
| 98 | {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference, |
| 99 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 100 | }, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 101 | }; |
| 102 | break; |
| 103 | case kPracticeTeamNumber: |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 104 | return new Values{ |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 105 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 106 | |
| 107 | // Intake |
| 108 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 109 | 0.0, |
| 110 | {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference, |
| 111 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 112 | }, |
| 113 | |
| 114 | // Shoulder |
| 115 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 116 | 0.0, |
| 117 | {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference, |
| 118 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 119 | }, |
| 120 | |
| 121 | // Wrist |
| 122 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 123 | 0.0, |
| 124 | {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference, |
| 125 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 126 | }, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 127 | }; |
| 128 | break; |
| 129 | default: |
| 130 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | const Values *DoGetValues() { |
| 135 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 136 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 137 | return DoGetValuesForTeam(team); |
| 138 | } |
| 139 | |
| 140 | } // namespace |
| 141 | |
| 142 | const Values &GetValues() { |
| 143 | static ::aos::Once<const Values> once(DoGetValues); |
| 144 | return *once.Get(); |
| 145 | } |
| 146 | |
| 147 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 148 | static ::aos::Mutex mutex; |
| 149 | ::aos::MutexLocker locker(&mutex); |
| 150 | |
| 151 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 152 | // race conditions. |
| 153 | static ::std::map<uint16_t, const Values *> values; |
| 154 | |
| 155 | if (values.count(team_number) == 0) { |
| 156 | values[team_number] = DoGetValuesForTeam(team_number); |
| 157 | #if __has_feature(address_sanitizer) |
| 158 | __lsan_ignore_object(values[team_number]); |
| 159 | #endif |
| 160 | } |
| 161 | return *values[team_number]; |
| 162 | } |
| 163 | |
| 164 | } // namespace constants |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 165 | } // namespace y2016 |