Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include "y2020/constants.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include <map> |
| 6 | |
| 7 | #if __has_feature(address_sanitizer) |
| 8 | #include "sanitizer/lsan_interface.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "absl/base/call_once.h" |
| 12 | #include "aos/logging/logging.h" |
| 13 | #include "aos/mutex/mutex.h" |
| 14 | #include "aos/network/team_number.h" |
| 15 | |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame^] | 16 | #include "y2020/control_loops/superstructure/hood/integral_hood_plant.h" |
| 17 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 18 | namespace y2020 { |
| 19 | namespace constants { |
| 20 | |
| 21 | const int Values::kZeroingSampleSize; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | const uint16_t kCompTeamNumber = 971; |
| 26 | const uint16_t kPracticeTeamNumber = 9971; |
| 27 | const uint16_t kCodingRobotTeamNumber = 7971; |
| 28 | |
| 29 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 30 | Values *const r = new Values(); |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame^] | 31 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 32 | ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const hood = |
| 33 | &r->hood; |
| 34 | |
| 35 | // Hood constants. |
| 36 | hood->zeroing_voltage = 3.0; |
| 37 | hood->operating_voltage = 12.0; |
| 38 | hood->zeroing_profile_params = {0.5, 3.0}; |
| 39 | hood->default_profile_params = {6.0, 30.0}; |
| 40 | hood->range = Values::kHoodRange(); |
| 41 | hood->make_integral_loop = |
| 42 | control_loops::superstructure::hood::MakeIntegralHoodLoop; |
| 43 | hood->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
| 44 | hood->zeroing_constants.one_revolution_distance = |
| 45 | M_PI * 2.0 * constants::Values::kHoodEncoderRatio(); |
| 46 | hood->zeroing_constants.zeroing_threshold = 0.0005; |
| 47 | hood->zeroing_constants.moving_buffer_size = 20; |
| 48 | hood->zeroing_constants.allowable_encoder_error = 0.9; |
| 49 | hood->zeroing_constants.middle_position = Values::kHoodRange().middle(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 50 | |
| 51 | switch (team) { |
| 52 | // A set of constants for tests. |
| 53 | case 1: |
| 54 | break; |
| 55 | |
| 56 | case kCompTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame^] | 57 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 58 | break; |
| 59 | |
| 60 | case kPracticeTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame^] | 61 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 62 | break; |
| 63 | |
| 64 | case kCodingRobotTeamNumber: |
Sabina Davis | a587fbd | 2020-01-31 22:11:15 -0800 | [diff] [blame^] | 65 | hood->zeroing_constants.measured_absolute_position = 0.0; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 66 | break; |
| 67 | |
| 68 | default: |
| 69 | AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 70 | } |
| 71 | |
| 72 | return r; |
| 73 | } |
| 74 | |
| 75 | void DoGetValues(const Values **result) { |
| 76 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 77 | AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 78 | *result = DoGetValuesForTeam(team); |
| 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | const Values &GetValues() { |
| 84 | static absl::once_flag once; |
| 85 | static const Values *result; |
| 86 | absl::call_once(once, DoGetValues, &result); |
| 87 | return *result; |
| 88 | } |
| 89 | |
| 90 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 91 | static ::aos::Mutex mutex; |
| 92 | ::aos::MutexLocker locker(&mutex); |
| 93 | |
| 94 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 95 | // race conditions. |
| 96 | static ::std::map<uint16_t, const Values *> values; |
| 97 | |
| 98 | if (values.count(team_number) == 0) { |
| 99 | values[team_number] = DoGetValuesForTeam(team_number); |
| 100 | #if __has_feature(address_sanitizer) |
| 101 | __lsan_ignore_object(values[team_number]); |
| 102 | #endif |
| 103 | } |
| 104 | return *values[team_number]; |
| 105 | } |
| 106 | |
| 107 | } // namespace constants |
| 108 | } // namespace y2020 |