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 { |
| 27 | namespace { |
| 28 | |
| 29 | const uint16_t kCompTeamNumber = 971; |
| 30 | const uint16_t kPracticeTeamNumber = 9971; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 31 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 32 | // TODO(constants): Update these to what we're using this year. |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 33 | const double kCompDrivetrainEncoderRatio = |
| 34 | (18.0 / 50.0) /*output reduction*/ * (56.0 / 30.0) /*encoder gears*/; |
| 35 | const double kCompLowGearRatio = 18.0 / 60.0 * 18.0 / 50.0; |
| 36 | const double kCompHighGearRatio = 28.0 / 50.0 * 18.0 / 50.0; |
| 37 | |
| 38 | const double kPracticeDrivetrainEncoderRatio = kCompDrivetrainEncoderRatio; |
| 39 | const double kPracticeLowGearRatio = kCompLowGearRatio; |
| 40 | const double kPracticeHighGearRatio = kCompHighGearRatio; |
| 41 | |
| 42 | const ShifterHallEffect kCompLeftDriveShifter{2.61, 2.33, 4.25, 3.28, 0.2, 0.7}; |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 43 | const ShifterHallEffect kCompRightDriveShifter{2.94, 4.31, 4.32, |
| 44 | 3.25, 0.2, 0.7}; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 45 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 46 | const ShifterHallEffect kPracticeLeftDriveShifter{2.80, 3.05, 4.15, |
| 47 | 3.2, 0.2, 0.7}; |
| 48 | const ShifterHallEffect kPracticeRightDriveShifter{2.90, 3.75, 3.80, |
| 49 | 2.98, 0.2, 0.7}; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 50 | |
| 51 | const double kRobotWidth = 25.0 / 100.0 * 2.54; |
| 52 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 53 | const int kZeroingSampleSize = 200; |
| 54 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 55 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 56 | switch (team) { |
| 57 | case 1: // for tests |
| 58 | return new Values{ |
| 59 | kCompDrivetrainEncoderRatio, |
| 60 | kCompLowGearRatio, |
| 61 | kCompHighGearRatio, |
| 62 | kCompLeftDriveShifter, |
| 63 | kCompRightDriveShifter, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 64 | 0.5, |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 65 | ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop, |
| 66 | ::y2016::control_loops::drivetrain::MakeDrivetrainLoop, |
| 67 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 68 | |
| 69 | // Intake |
| 70 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 71 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
Austin Schuh | 0efb195 | 2016-02-13 22:58:17 -0800 | [diff] [blame^] | 72 | {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.0, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 73 | }, |
| 74 | |
| 75 | // Shoulder |
| 76 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 77 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
Austin Schuh | 0efb195 | 2016-02-13 22:58:17 -0800 | [diff] [blame^] | 78 | {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.0, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 79 | }, |
| 80 | |
| 81 | // Wrist |
| 82 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 83 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
Austin Schuh | 0efb195 | 2016-02-13 22:58:17 -0800 | [diff] [blame^] | 84 | {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.0, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 85 | }, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 86 | }; |
| 87 | break; |
| 88 | case kCompTeamNumber: |
| 89 | return new Values{ |
| 90 | kCompDrivetrainEncoderRatio, |
| 91 | kCompLowGearRatio, |
| 92 | kCompHighGearRatio, |
| 93 | kCompLeftDriveShifter, |
| 94 | kCompRightDriveShifter, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 95 | kRobotWidth, |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 96 | ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop, |
| 97 | ::y2016::control_loops::drivetrain::MakeDrivetrainLoop, |
| 98 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 99 | |
| 100 | // Intake |
| 101 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 102 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
| 103 | {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 104 | }, |
| 105 | |
| 106 | // Shoulder |
| 107 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 108 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
| 109 | {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 110 | }, |
| 111 | |
| 112 | // Wrist |
| 113 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 114 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
| 115 | {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 116 | }, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 117 | }; |
| 118 | break; |
| 119 | case kPracticeTeamNumber: |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 120 | return new Values{ |
| 121 | kPracticeDrivetrainEncoderRatio, |
| 122 | kPracticeLowGearRatio, |
| 123 | kPracticeHighGearRatio, |
| 124 | kPracticeLeftDriveShifter, |
| 125 | kPracticeRightDriveShifter, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 126 | kRobotWidth, |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 127 | ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop, |
| 128 | ::y2016::control_loops::drivetrain::MakeDrivetrainLoop, |
| 129 | 5.0, // drivetrain max speed |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 130 | |
| 131 | // Intake |
| 132 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 133 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
| 134 | {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 135 | }, |
| 136 | |
| 137 | // Shoulder |
| 138 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 139 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
| 140 | {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 141 | }, |
| 142 | |
| 143 | // Wrist |
| 144 | { |
Austin Schuh | c5467a1 | 2016-02-13 20:25:13 -0800 | [diff] [blame] | 145 | {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI}, |
| 146 | {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3}, |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 147 | }, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 148 | }; |
| 149 | break; |
| 150 | default: |
| 151 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const Values *DoGetValues() { |
| 156 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 157 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 158 | return DoGetValuesForTeam(team); |
| 159 | } |
| 160 | |
| 161 | } // namespace |
| 162 | |
| 163 | const Values &GetValues() { |
| 164 | static ::aos::Once<const Values> once(DoGetValues); |
| 165 | return *once.Get(); |
| 166 | } |
| 167 | |
| 168 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 169 | static ::aos::Mutex mutex; |
| 170 | ::aos::MutexLocker locker(&mutex); |
| 171 | |
| 172 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 173 | // race conditions. |
| 174 | static ::std::map<uint16_t, const Values *> values; |
| 175 | |
| 176 | if (values.count(team_number) == 0) { |
| 177 | values[team_number] = DoGetValuesForTeam(team_number); |
| 178 | #if __has_feature(address_sanitizer) |
| 179 | __lsan_ignore_object(values[team_number]); |
| 180 | #endif |
| 181 | } |
| 182 | return *values[team_number]; |
| 183 | } |
| 184 | |
| 185 | } // namespace constants |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 186 | } // namespace y2016 |