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