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