blob: 4d9d06627f6feef4a6f022793f3be98889f251ba [file] [log] [blame]
Stephan Massaltd021f972020-01-05 20:41:23 -08001#include "y2020/constants.h"
2
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cinttypes>
Stephan Massaltd021f972020-01-05 20:41:23 -08004#include <map>
5
6#if __has_feature(address_sanitizer)
7#include "sanitizer/lsan_interface.h"
8#endif
9
10#include "absl/base/call_once.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070011#include "glog/logging.h"
12
Stephan Massaltd021f972020-01-05 20:41:23 -080013#include "aos/network/team_number.h"
Brian Silverman1463c092020-10-30 17:28:24 -070014#include "aos/stl_mutex/stl_mutex.h"
ravago901c4262020-02-16 15:33:14 -080015#include "y2020/control_loops/superstructure/control_panel/integral_control_panel_plant.h"
Sabina Davisa587fbd2020-01-31 22:11:15 -080016#include "y2020/control_loops/superstructure/hood/integral_hood_plant.h"
Kai Tinkess10943cf2020-02-01 15:49:57 -080017#include "y2020/control_loops/superstructure/intake/integral_intake_plant.h"
18#include "y2020/control_loops/superstructure/turret/integral_turret_plant.h"
Sabina Davisa587fbd2020-01-31 22:11:15 -080019
Stephan Massaltd021f972020-01-05 20:41:23 -080020namespace y2020 {
21namespace constants {
22
23const int Values::kZeroingSampleSize;
24
25namespace {
26
Stephan Massaltd021f972020-01-05 20:41:23 -080027const Values *DoGetValuesForTeam(uint16_t team) {
28 Values *const r = new Values();
Sabina Davisa587fbd2020-01-31 22:11:15 -080029 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
Ravago Jones937587c2020-12-26 17:21:09 -080030 ::frc971::zeroing::AbsoluteAndAbsoluteEncoderZeroingEstimator>
31 *const hood = &r->hood;
milind-ud53408e2021-10-21 19:43:58 -070032 Values::HoodGeometry *const hood_geometry = &r->hood_geometry;
Sabina Davisa587fbd2020-01-31 22:11:15 -080033
milind-ud53408e2021-10-21 19:43:58 -070034 // We found that the finisher velocity does not change ball velocity much,
35 // so keep it constant.
Austin Schuh4408c422021-10-16 13:56:07 -070036 r->shot_interpolation_table =
milind-uf7fadbf2021-11-07 14:10:54 -080037 InterpolationTable<Values::ShotParams>({{1.128, {0.01, 225, 170}},
38 {1.509, {0.01, 215, 160}},
Austin Schuhe17cbaf2021-11-14 21:02:37 -080039 {1.89, {0.01, 250, 175}},
40 {2.15, {0.06, 235, 170}},
41 {2.68, {0.195, 250, 200}},
milind-uf7fadbf2021-11-07 14:10:54 -080042 {3.19, {0.27, 250, 220}},
43 {3.93, {0.365, 285, 250}},
44 {4.63, {0.42, 320, 280}},
45 {5.32, {0.515, 375, 330}},
46 {6, {0.565, 440, 400}},
47 {6.68, {0.58, 480, 450}},
48 {7.37, {0.645, 520, 540}},
49 {8.36, {0.645, 550, 560}},
50 {9.39, {0.66, 550, 580}},
Austin Schuhb24e9052021-11-12 19:53:33 -080051 {10.4, {0.670, 550, 600}},
52 {11.4, {0.670, 550, 600}}});
James Kuszmaul98154a22021-04-03 16:09:29 -070053
Sabina Davisa587fbd2020-01-31 22:11:15 -080054 // Hood constants.
Austin Schuh2efe1682021-03-06 22:47:15 -080055 hood->zeroing_voltage = 2.0;
Sabina Davisa587fbd2020-01-31 22:11:15 -080056 hood->operating_voltage = 12.0;
57 hood->zeroing_profile_params = {0.5, 3.0};
58 hood->default_profile_params = {6.0, 30.0};
59 hood->range = Values::kHoodRange();
60 hood->make_integral_loop =
61 control_loops::superstructure::hood::MakeIntegralHoodLoop;
62 hood->zeroing_constants.average_filter_size = Values::kZeroingSampleSize;
Sabina Davisa587fbd2020-01-31 22:11:15 -080063 hood->zeroing_constants.zeroing_threshold = 0.0005;
64 hood->zeroing_constants.moving_buffer_size = 20;
65 hood->zeroing_constants.allowable_encoder_error = 0.9;
Ravago Jones937587c2020-12-26 17:21:09 -080066 hood->zeroing_constants.one_revolution_distance =
67 M_PI * 2.0 * constants::Values::kHoodEncoderRatio();
68 hood->zeroing_constants.single_turn_middle_position =
69 Values::kHoodRange().middle();
70 hood->zeroing_constants.single_turn_one_revolution_distance =
71 M_PI * 2.0 * constants::Values::kHoodSingleTurnEncoderRatio();
72 hood->zeroing_constants.measured_absolute_position = 0;
73 hood->zeroing_constants.single_turn_measured_absolute_position = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -080074
milind-ud53408e2021-10-21 19:43:58 -070075 constexpr double kDegToRad = M_PI / 180.0;
76 constexpr double kMmToM = 1.0 / 1000.0;
77 hood_geometry->theta_0 = 22.98004 * kDegToRad;
78 hood_geometry->screw_length_0 = 110.33888 * kMmToM;
79 hood_geometry->radius = 269.6262 * kMmToM;
80 hood_geometry->diagonal_length = 288.4353 * kMmToM;
81 hood_geometry->back_plate_diagonal_length = 22.86 * kMmToM;
82
Sabina Davise8d38992020-02-02 15:00:31 -080083 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
84 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake =
85 &r->intake;
86
87 // Intake constants.
88 intake->zeroing_voltage = 3.0;
89 intake->operating_voltage = 12.0;
90 intake->zeroing_profile_params = {0.5, 3.0};
91 intake->default_profile_params = {6.0, 30.0};
92 intake->range = Values::kIntakeRange();
93 intake->make_integral_loop =
94 control_loops::superstructure::intake::MakeIntegralIntakeLoop;
95 intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize;
96 intake->zeroing_constants.one_revolution_distance =
97 M_PI * 2.0 * constants::Values::kIntakeEncoderRatio();
98 intake->zeroing_constants.zeroing_threshold = 0.0005;
99 intake->zeroing_constants.moving_buffer_size = 20;
100 intake->zeroing_constants.allowable_encoder_error = 0.9;
101 intake->zeroing_constants.middle_position = Values::kIntakeRange().middle();
102
Kai Tinkess10943cf2020-02-01 15:49:57 -0800103 Values::PotAndAbsEncoderConstants *const turret = &r->turret;
104 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
105 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
106 *const turret_params = &turret->subsystem_params;
107
ravago901c4262020-02-16 15:33:14 -0800108 // Turret Constants
Kai Tinkess10943cf2020-02-01 15:49:57 -0800109 turret_params->zeroing_voltage = 4.0;
James Kuszmaul519585d2020-03-08 22:32:48 -0700110 turret_params->operating_voltage = 8.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800111 // TODO(austin): Tune these.
112 turret_params->zeroing_profile_params = {0.5, 2.0};
113 turret_params->default_profile_params = {15.0, 40.0};
114 turret_params->range = Values::kTurretRange();
115 turret_params->make_integral_loop =
116 &control_loops::superstructure::turret::MakeIntegralTurretLoop;
117 turret_params->zeroing_constants.average_filter_size =
118 Values::kZeroingSampleSize;
119 turret_params->zeroing_constants.one_revolution_distance =
120 M_PI * 2.0 * constants::Values::kTurretEncoderRatio();
121 turret_params->zeroing_constants.zeroing_threshold = 0.0005;
122 turret_params->zeroing_constants.moving_buffer_size = 20;
123 turret_params->zeroing_constants.allowable_encoder_error = 0.9;
124
Austin Schuh9dcd5202020-02-20 20:06:04 -0800125 CHECK_LE(hood->range.range(),
Ravago Jones937587c2020-12-26 17:21:09 -0800126 hood->zeroing_constants.single_turn_one_revolution_distance);
Austin Schuh9dcd5202020-02-20 20:06:04 -0800127 CHECK_LE(intake->range.range(),
128 intake->zeroing_constants.one_revolution_distance);
129
Stephan Massaltd021f972020-01-05 20:41:23 -0800130 switch (team) {
131 // A set of constants for tests.
132 case 1:
milind-uf7fadbf2021-11-07 14:10:54 -0800133 r->shot_interpolation_table = InterpolationTable<Values::ShotParams>(
milind-udfe0bcd2021-11-07 16:23:26 -0800134 {{1, {0.01, 250, 350}}, {10, {0.67, 500, 350}}});
James Kuszmaul0a981402021-10-09 21:00:34 -0700135 case Values::kSpareRoborioTeamNumber:
Stephan Massaltd021f972020-01-05 20:41:23 -0800136 break;
137
James Kuszmaul0a981402021-10-09 21:00:34 -0700138 case Values::kCompTeamNumber:
Sabina Davisf7afd112020-02-23 13:42:14 -0800139 intake->zeroing_constants.measured_absolute_position =
Austin Schuh4dc06c72021-10-17 17:39:41 -0700140 0.433936997731885 - Values::kIntakeZero();
Kai Tinkess10943cf2020-02-01 15:49:57 -0800141
Austin Schuh2fb23642020-02-29 15:10:51 -0800142 turret->potentiometer_offset = 5.52519370141247 + 0.00853506822980376 +
James Kuszmaulf774d2d2021-10-03 08:02:35 -0700143 0.0109413725126625 - 0.223719825811759 +
Austin Schuh1216d992021-10-23 19:23:43 -0700144 0.261356551915472 - 0.0490168170767848 -
Austin Schuhda596272021-11-13 11:06:26 -0800145 0.179342788816305 + 0.800488361764795;
Sabina Davisf7afd112020-02-23 13:42:14 -0800146 turret_params->zeroing_constants.measured_absolute_position =
Austin Schuhda596272021-11-13 11:06:26 -0800147 2.55091426937561;
Ravago Jones937587c2020-12-26 17:21:09 -0800148
Austin Schuha2f01c62021-10-25 22:01:10 -0700149 hood->zeroing_constants.measured_absolute_position = 0.0102882878327271;
Austin Schuh2efe1682021-03-06 22:47:15 -0800150 hood->zeroing_constants.single_turn_measured_absolute_position =
Austin Schuhc09cbe02021-10-24 17:16:15 -0700151 0.302574797776192;
Stephan Massaltd021f972020-01-05 20:41:23 -0800152 break;
153
James Kuszmaul0a981402021-10-09 21:00:34 -0700154 case Values::kPracticeTeamNumber:
Sabina Davisa587fbd2020-01-31 22:11:15 -0800155 hood->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800156
Austin Schuh81c5f002021-11-06 22:59:07 -0700157 intake->zeroing_constants.measured_absolute_position = 0.205469223604347;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800158
Austin Schuh31904ee2021-03-06 23:52:08 -0800159 turret->potentiometer_offset = 5.3931926228241;
160 turret_params->zeroing_constants.measured_absolute_position = 4.22;
Stephan Massaltd021f972020-01-05 20:41:23 -0800161 break;
162
Austin Schuh83873c32020-02-22 14:58:39 -0800163 case Values::kCodingRobotTeamNumber:
Sabina Davisa587fbd2020-01-31 22:11:15 -0800164 hood->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800165
Sabina Davise8d38992020-02-02 15:00:31 -0800166 intake->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800167
168 turret->potentiometer_offset = 0.0;
169 turret_params->zeroing_constants.measured_absolute_position = 0.0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800170 break;
171
172 default:
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700173 LOG(FATAL) << "unknown team: " << team;
Stephan Massaltd021f972020-01-05 20:41:23 -0800174 }
175
176 return r;
177}
178
milind-u62d4a8e2021-10-11 16:08:41 -0700179const Values *values = nullptr;
180
181void DoGetValues() {
Stephan Massaltd021f972020-01-05 20:41:23 -0800182 uint16_t team = ::aos::network::GetTeamNumber();
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700183 LOG(INFO) << "creating a Constants for team: " << team;
milind-u62d4a8e2021-10-11 16:08:41 -0700184 values = DoGetValuesForTeam(team);
Stephan Massaltd021f972020-01-05 20:41:23 -0800185}
186
187} // namespace
188
milind-u62d4a8e2021-10-11 16:08:41 -0700189void InitValues() {
Stephan Massaltd021f972020-01-05 20:41:23 -0800190 static absl::once_flag once;
milind-u62d4a8e2021-10-11 16:08:41 -0700191 absl::call_once(once, DoGetValues);
Stephan Massaltd021f972020-01-05 20:41:23 -0800192}
193
milind-u62d4a8e2021-10-11 16:08:41 -0700194const Values &GetValues() {
195 CHECK(values)
196 << "Values are uninitialized. Call InitValues before accessing them.";
197 return *values;
Stephan Massaltd021f972020-01-05 20:41:23 -0800198}
199
200} // namespace constants
201} // namespace y2020