Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 1 | #include "y2019/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 "aos/logging/logging.h" |
| 12 | #include "aos/mutex/mutex.h" |
| 13 | #include "aos/network/team_number.h" |
| 14 | #include "aos/once.h" |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 15 | #include "y2019/control_loops/superstructure/elevator/integral_elevator_plant.h" |
| 16 | #include "y2019/control_loops/superstructure/intake/integral_intake_plant.h" |
| 17 | #include "y2019/control_loops/superstructure/stilts/integral_stilts_plant.h" |
| 18 | #include "y2019/control_loops/superstructure/wrist/integral_wrist_plant.h" |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 19 | |
| 20 | namespace y2019 { |
| 21 | namespace constants { |
| 22 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 23 | using ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator; |
| 24 | |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 25 | const int Values::kZeroingSampleSize; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | const uint16_t kCompTeamNumber = 971; |
| 30 | const uint16_t kPracticeTeamNumber = 9971; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 31 | const uint16_t kCodingRobotTeamNumber = 7971; |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 32 | |
| 33 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 34 | Values *const r = new Values(); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 35 | Values::PotAndAbsConstants *const elevator = &r->elevator; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 36 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 37 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 38 | *const elevator_params = &(elevator->subsystem_params); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 39 | Values::PotAndAbsConstants *const stilts = &r->stilts; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 40 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 41 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 42 | *const stilts_params = &(stilts->subsystem_params); |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 43 | Values::PotAndAbsConstants *const wrist = &r->wrist; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 44 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 45 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator> |
| 46 | *const wrist_params = &(wrist->subsystem_params); |
| 47 | ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams< |
| 48 | ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake = |
| 49 | &r->intake; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 50 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 51 | // Elevator constants. |
Austin Schuh | 7f87b47 | 2019-02-15 23:20:57 -0800 | [diff] [blame] | 52 | elevator_params->zeroing_voltage = 3.0; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 53 | elevator_params->operating_voltage = 12.0; |
| 54 | elevator_params->zeroing_profile_params = {0.1, 1.0}; |
Austin Schuh | 7f87b47 | 2019-02-15 23:20:57 -0800 | [diff] [blame] | 55 | elevator_params->default_profile_params = {4.0, 16.0}; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 56 | elevator_params->range = Values::kElevatorRange(); |
| 57 | elevator_params->make_integral_loop = |
| 58 | &control_loops::superstructure::elevator::MakeIntegralElevatorLoop; |
| 59 | elevator_params->zeroing_constants.average_filter_size = |
| 60 | Values::kZeroingSampleSize; |
| 61 | elevator_params->zeroing_constants.one_revolution_distance = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 62 | M_PI * 2.0 * constants::Values::kElevatorEncoderRatio(); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 63 | elevator_params->zeroing_constants.zeroing_threshold = 0.005; |
| 64 | elevator_params->zeroing_constants.moving_buffer_size = 20; |
| 65 | elevator_params->zeroing_constants.allowable_encoder_error = 0.9; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 66 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 67 | // Wrist constants. |
| 68 | wrist_params->zeroing_voltage = 4.0; |
| 69 | wrist_params->operating_voltage = 12.0; |
| 70 | wrist_params->zeroing_profile_params = {0.5, 2.0}; |
Austin Schuh | 7f87b47 | 2019-02-15 23:20:57 -0800 | [diff] [blame] | 71 | wrist_params->default_profile_params = {10.0, 40.0}; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 72 | wrist_params->range = Values::kWristRange(); |
| 73 | wrist_params->make_integral_loop = |
| 74 | &control_loops::superstructure::wrist::MakeIntegralWristLoop; |
| 75 | wrist_params->zeroing_constants.average_filter_size = |
| 76 | Values::kZeroingSampleSize; |
| 77 | wrist_params->zeroing_constants.one_revolution_distance = |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 78 | M_PI * 2.0 * constants::Values::kWristEncoderRatio(); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 79 | wrist_params->zeroing_constants.zeroing_threshold = 0.0005; |
| 80 | wrist_params->zeroing_constants.moving_buffer_size = 20; |
| 81 | wrist_params->zeroing_constants.allowable_encoder_error = 0.9; |
| 82 | |
| 83 | // Intake constants. |
Austin Schuh | 7f87b47 | 2019-02-15 23:20:57 -0800 | [diff] [blame] | 84 | intake->zeroing_voltage = 3.0; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 85 | intake->operating_voltage = 12.0; |
| 86 | intake->zeroing_profile_params = {0.5, 3.0}; |
Austin Schuh | 7f87b47 | 2019-02-15 23:20:57 -0800 | [diff] [blame] | 87 | intake->default_profile_params = {6.0, 30.0}; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 88 | intake->range = Values::kIntakeRange(); |
| 89 | intake->make_integral_loop = |
| 90 | control_loops::superstructure::intake::MakeIntegralIntakeLoop; |
| 91 | intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize; |
| 92 | intake->zeroing_constants.one_revolution_distance = |
| 93 | M_PI * 2.0 * constants::Values::kIntakeEncoderRatio(); |
| 94 | intake->zeroing_constants.zeroing_threshold = 0.0005; |
| 95 | intake->zeroing_constants.moving_buffer_size = 20; |
| 96 | intake->zeroing_constants.allowable_encoder_error = 0.9; |
| 97 | |
| 98 | // Stilts constants. |
Austin Schuh | 7f87b47 | 2019-02-15 23:20:57 -0800 | [diff] [blame] | 99 | stilts_params->zeroing_voltage = 3.0; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 100 | stilts_params->operating_voltage = 12.0; |
Austin Schuh | 7f87b47 | 2019-02-15 23:20:57 -0800 | [diff] [blame] | 101 | stilts_params->zeroing_profile_params = {0.1, 0.5}; |
| 102 | stilts_params->default_profile_params = {0.5, 0.5}; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 103 | stilts_params->range = Values::kStiltsRange(); |
| 104 | stilts_params->make_integral_loop = |
| 105 | &control_loops::superstructure::stilts::MakeIntegralStiltsLoop; |
| 106 | stilts_params->zeroing_constants.average_filter_size = |
| 107 | Values::kZeroingSampleSize; |
| 108 | stilts_params->zeroing_constants.one_revolution_distance = |
| 109 | M_PI * 2.0 * constants::Values::kStiltsEncoderRatio(); |
| 110 | stilts_params->zeroing_constants.zeroing_threshold = 0.0005; |
| 111 | stilts_params->zeroing_constants.moving_buffer_size = 20; |
| 112 | stilts_params->zeroing_constants.allowable_encoder_error = 0.9; |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 113 | |
| 114 | switch (team) { |
| 115 | // A set of constants for tests. |
| 116 | case 1: |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 117 | elevator_params->zeroing_constants.measured_absolute_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 118 | elevator->potentiometer_offset = 0.0; |
| 119 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 120 | intake->zeroing_constants.measured_absolute_position = 0.0; |
| 121 | intake->zeroing_constants.middle_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 122 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 123 | wrist_params->zeroing_constants.measured_absolute_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 124 | wrist->potentiometer_offset = 0.0; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 125 | |
| 126 | stilts_params->zeroing_constants.measured_absolute_position = 0.0; |
| 127 | stilts->potentiometer_offset = 0.0; |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 128 | break; |
| 129 | |
| 130 | case kCompTeamNumber: |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 131 | elevator_params->zeroing_constants.measured_absolute_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 132 | elevator->potentiometer_offset = 0.0; |
| 133 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 134 | intake->zeroing_constants.measured_absolute_position = 0.0; |
| 135 | intake->zeroing_constants.middle_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 136 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 137 | wrist_params->zeroing_constants.measured_absolute_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 138 | wrist->potentiometer_offset = 0.0; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 139 | |
| 140 | stilts_params->zeroing_constants.measured_absolute_position = 0.0; |
| 141 | stilts->potentiometer_offset = 0.0; |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case kPracticeTeamNumber: |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 145 | elevator_params->zeroing_constants.measured_absolute_position = 0.049419; |
| 146 | elevator->potentiometer_offset = -0.022320; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 147 | |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 148 | intake->zeroing_constants.measured_absolute_position = 2.303729; |
| 149 | intake->zeroing_constants.middle_position = |
| 150 | Values::kIntakeRange().middle(); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 151 | |
| 152 | stilts_params->zeroing_constants.measured_absolute_position = 0.0; |
| 153 | stilts->potentiometer_offset = 0.0; |
Austin Schuh | ed7f863 | 2019-02-15 23:12:20 -0800 | [diff] [blame] | 154 | |
| 155 | wrist_params->zeroing_constants.measured_absolute_position = 0.357394; |
| 156 | wrist->potentiometer_offset = -1.479097 - 2.740303; |
| 157 | |
| 158 | stilts_params->zeroing_constants.measured_absolute_position = 0.047838; |
| 159 | stilts->potentiometer_offset = -0.093820; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 160 | break; |
| 161 | |
| 162 | case kCodingRobotTeamNumber: |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 163 | elevator_params->zeroing_constants.measured_absolute_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 164 | elevator->potentiometer_offset = 0.0; |
| 165 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 166 | intake->zeroing_constants.measured_absolute_position = 0.0; |
| 167 | intake->zeroing_constants.middle_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 168 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 169 | wrist_params->zeroing_constants.measured_absolute_position = 0.0; |
Alex Perry | 5fb5ff2 | 2019-02-09 21:53:17 -0800 | [diff] [blame] | 170 | wrist->potentiometer_offset = 0.0; |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 171 | |
| 172 | stilts_params->zeroing_constants.measured_absolute_position = 0.0; |
| 173 | stilts->potentiometer_offset = 0.0; |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 174 | break; |
| 175 | |
| 176 | default: |
| 177 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 178 | } |
| 179 | |
| 180 | return r; |
| 181 | } |
| 182 | |
| 183 | const Values *DoGetValues() { |
| 184 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 185 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 186 | return DoGetValuesForTeam(team); |
| 187 | } |
| 188 | |
| 189 | } // namespace |
| 190 | |
| 191 | const Values &GetValues() { |
| 192 | static ::aos::Once<const Values> once(DoGetValues); |
| 193 | return *once.Get(); |
| 194 | } |
| 195 | |
| 196 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 197 | static ::aos::Mutex mutex; |
| 198 | ::aos::MutexLocker locker(&mutex); |
| 199 | |
| 200 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 201 | // race conditions. |
| 202 | static ::std::map<uint16_t, const Values *> values; |
| 203 | |
| 204 | if (values.count(team_number) == 0) { |
| 205 | values[team_number] = DoGetValuesForTeam(team_number); |
| 206 | #if __has_feature(address_sanitizer) |
| 207 | __lsan_ignore_object(values[team_number]); |
| 208 | #endif |
| 209 | } |
| 210 | return *values[team_number]; |
| 211 | } |
| 212 | |
Tyler Chatow | 37ecdcd | 2019-01-26 20:18:42 -0800 | [diff] [blame] | 213 | } // namespace constants |
| 214 | } // namespace y2019 |