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 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 53 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 54 | switch (team) { |
| 55 | case 1: // for tests |
| 56 | return new Values{ |
| 57 | kCompDrivetrainEncoderRatio, |
| 58 | kCompLowGearRatio, |
| 59 | kCompHighGearRatio, |
| 60 | kCompLeftDriveShifter, |
| 61 | kCompRightDriveShifter, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 62 | 0.5, |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 63 | ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop, |
| 64 | ::y2016::control_loops::drivetrain::MakeDrivetrainLoop, |
| 65 | 5.0, // drivetrain max speed |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 66 | }; |
| 67 | break; |
| 68 | case kCompTeamNumber: |
| 69 | return new Values{ |
| 70 | kCompDrivetrainEncoderRatio, |
| 71 | kCompLowGearRatio, |
| 72 | kCompHighGearRatio, |
| 73 | kCompLeftDriveShifter, |
| 74 | kCompRightDriveShifter, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 75 | kRobotWidth, |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 76 | ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop, |
| 77 | ::y2016::control_loops::drivetrain::MakeDrivetrainLoop, |
| 78 | 5.0, // drivetrain max speed |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 79 | }; |
| 80 | break; |
| 81 | case kPracticeTeamNumber: |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 82 | return new Values{ |
| 83 | kPracticeDrivetrainEncoderRatio, |
| 84 | kPracticeLowGearRatio, |
| 85 | kPracticeHighGearRatio, |
| 86 | kPracticeLeftDriveShifter, |
| 87 | kPracticeRightDriveShifter, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 88 | kRobotWidth, |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 89 | ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop, |
| 90 | ::y2016::control_loops::drivetrain::MakeDrivetrainLoop, |
| 91 | 5.0, // drivetrain max speed |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 92 | }; |
| 93 | break; |
| 94 | default: |
| 95 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | const Values *DoGetValues() { |
| 100 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 101 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 102 | return DoGetValuesForTeam(team); |
| 103 | } |
| 104 | |
| 105 | } // namespace |
| 106 | |
| 107 | const Values &GetValues() { |
| 108 | static ::aos::Once<const Values> once(DoGetValues); |
| 109 | return *once.Get(); |
| 110 | } |
| 111 | |
| 112 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 113 | static ::aos::Mutex mutex; |
| 114 | ::aos::MutexLocker locker(&mutex); |
| 115 | |
| 116 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 117 | // race conditions. |
| 118 | static ::std::map<uint16_t, const Values *> values; |
| 119 | |
| 120 | if (values.count(team_number) == 0) { |
| 121 | values[team_number] = DoGetValuesForTeam(team_number); |
| 122 | #if __has_feature(address_sanitizer) |
| 123 | __lsan_ignore_object(values[team_number]); |
| 124 | #endif |
| 125 | } |
| 126 | return *values[team_number]; |
| 127 | } |
| 128 | |
| 129 | } // namespace constants |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 130 | } // namespace y2016 |