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, |
| 32 | Values::kHighGearRatio, Values::kTurnWidth, Values::kShooterEncoderRatio, |
| 33 | Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio, |
| 34 | Values::kWristEncoderRatio, Values::kIntakePotRatio, |
| 35 | Values::kShoulderPotRatio, Values::kWristPotRatio, |
| 36 | Values::kIntakeEncoderIndexDifference, |
| 37 | Values::kShoulderEncoderIndexDifference, |
| 38 | Values::kWristEncoderIndexDifference; |
| 39 | constexpr Values::Range Values::kIntakeRange, Values::kShoulderRange, |
| 40 | Values::kWristRange; |
| 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 | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame^] | 50 | case 1: |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 51 | case kCompTeamNumber: |
| 52 | return new Values{ |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 53 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 54 | |
| 55 | // Intake |
| 56 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame^] | 57 | 0.0, |
| 58 | {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference, |
| 59 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 60 | }, |
| 61 | |
| 62 | // Shoulder |
| 63 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame^] | 64 | 0.0, |
| 65 | {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference, |
| 66 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 67 | }, |
| 68 | |
| 69 | // Wrist |
| 70 | { |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame^] | 71 | 0.0, |
| 72 | {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference, |
| 73 | 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 74 | }, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 75 | }; |
| 76 | break; |
| 77 | case kPracticeTeamNumber: |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 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 | default: |
| 104 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | const Values *DoGetValues() { |
| 109 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 110 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 111 | return DoGetValuesForTeam(team); |
| 112 | } |
| 113 | |
| 114 | } // namespace |
| 115 | |
| 116 | const Values &GetValues() { |
| 117 | static ::aos::Once<const Values> once(DoGetValues); |
| 118 | return *once.Get(); |
| 119 | } |
| 120 | |
| 121 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 122 | static ::aos::Mutex mutex; |
| 123 | ::aos::MutexLocker locker(&mutex); |
| 124 | |
| 125 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 126 | // race conditions. |
| 127 | static ::std::map<uint16_t, const Values *> values; |
| 128 | |
| 129 | if (values.count(team_number) == 0) { |
| 130 | values[team_number] = DoGetValuesForTeam(team_number); |
| 131 | #if __has_feature(address_sanitizer) |
| 132 | __lsan_ignore_object(values[team_number]); |
| 133 | #endif |
| 134 | } |
| 135 | return *values[team_number]; |
| 136 | } |
| 137 | |
| 138 | } // namespace constants |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 139 | } // namespace y2016 |