blob: 0b1b50ad730d73ca9c863783e45864118f969383 [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"
Stephan Massaltd021f972020-01-05 20:41:23 -080011#include "aos/network/team_number.h"
Brian Silverman1463c092020-10-30 17:28:24 -070012#include "aos/stl_mutex/stl_mutex.h"
Brian Silvermanf4d329c2021-11-04 19:32:10 -070013#include "glog/logging.h"
ravago901c4262020-02-16 15:33:14 -080014#include "y2020/control_loops/superstructure/control_panel/integral_control_panel_plant.h"
Sabina Davisa587fbd2020-01-31 22:11:15 -080015#include "y2020/control_loops/superstructure/hood/integral_hood_plant.h"
Kai Tinkess10943cf2020-02-01 15:49:57 -080016#include "y2020/control_loops/superstructure/intake/integral_intake_plant.h"
17#include "y2020/control_loops/superstructure/turret/integral_turret_plant.h"
Sabina Davisa587fbd2020-01-31 22:11:15 -080018
Stephan Massaltd021f972020-01-05 20:41:23 -080019namespace y2020 {
20namespace constants {
21
22const int Values::kZeroingSampleSize;
23
24namespace {
25
Stephan Massaltd021f972020-01-05 20:41:23 -080026const Values *DoGetValuesForTeam(uint16_t team) {
27 Values *const r = new Values();
Sabina Davisa587fbd2020-01-31 22:11:15 -080028 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
Ravago Jones937587c2020-12-26 17:21:09 -080029 ::frc971::zeroing::AbsoluteAndAbsoluteEncoderZeroingEstimator>
30 *const hood = &r->hood;
milind-ud53408e2021-10-21 19:43:58 -070031 Values::HoodGeometry *const hood_geometry = &r->hood_geometry;
Sabina Davisa587fbd2020-01-31 22:11:15 -080032
milind-ud53408e2021-10-21 19:43:58 -070033 // We found that the finisher velocity does not change ball velocity much,
34 // so keep it constant.
milind-u0a178a82021-09-28 18:42:09 -070035 constexpr double kVelocityFinisher = 350.0;
Austin Schuh4408c422021-10-16 13:56:07 -070036 r->shot_interpolation_table =
Austin Schuh30e45ff2021-10-16 18:33:53 -070037 InterpolationTable<Values::ShotParams>({{1.0, {0.01, 10.7}},
38 {1.2, {0.01, 10.7}},
39 {1.4732, {0.10, 10.6}},
40 {2.5, {0.36, 12.0}},
41 {3.50, {0.43, 13.2}},
Austin Schuh48051732021-10-24 17:15:50 -070042 {3.93, {0.44, 13.2}},
43 {4.7371, {0.475, 14.2}},
44 {5.31, {0.51, 14.6}},
45 {6.332, {0.525, 15.2}},
46 {7.35, {0.52, 17.0}},
Austin Schuh4408c422021-10-16 13:56:07 -070047 {8.30, {0.565, 17.0}},
48 {9.20, {0.535, 17.0}}});
milind-u0a178a82021-09-28 18:42:09 -070049
50 r->flywheel_shot_interpolation_table =
51 InterpolationTable<Values::FlywheelShotParams>(
52 {{10.6, {250.0, kVelocityFinisher}},
53 {12.0, {275.0, kVelocityFinisher}},
54 {13.2, {300.0, kVelocityFinisher}},
55 {14.0, {325.0, kVelocityFinisher}},
56 {14.6, {350.0, kVelocityFinisher}},
57 {15.2, {375.0, kVelocityFinisher}},
58 {15.6, {400.0, kVelocityFinisher}},
59 {16.1, {425.0, kVelocityFinisher}},
60 {16.3, {450.0, kVelocityFinisher}},
61 {16.6, {475.0, kVelocityFinisher}},
Austin Schuh48051732021-10-24 17:15:50 -070062 {17.0, {500.0, kVelocityFinisher + 25}}});
James Kuszmaul98154a22021-04-03 16:09:29 -070063
Sabina Davisa587fbd2020-01-31 22:11:15 -080064 // Hood constants.
Austin Schuh2efe1682021-03-06 22:47:15 -080065 hood->zeroing_voltage = 2.0;
Sabina Davisa587fbd2020-01-31 22:11:15 -080066 hood->operating_voltage = 12.0;
67 hood->zeroing_profile_params = {0.5, 3.0};
68 hood->default_profile_params = {6.0, 30.0};
69 hood->range = Values::kHoodRange();
70 hood->make_integral_loop =
71 control_loops::superstructure::hood::MakeIntegralHoodLoop;
72 hood->zeroing_constants.average_filter_size = Values::kZeroingSampleSize;
Sabina Davisa587fbd2020-01-31 22:11:15 -080073 hood->zeroing_constants.zeroing_threshold = 0.0005;
74 hood->zeroing_constants.moving_buffer_size = 20;
75 hood->zeroing_constants.allowable_encoder_error = 0.9;
Ravago Jones937587c2020-12-26 17:21:09 -080076 hood->zeroing_constants.one_revolution_distance =
77 M_PI * 2.0 * constants::Values::kHoodEncoderRatio();
78 hood->zeroing_constants.single_turn_middle_position =
79 Values::kHoodRange().middle();
80 hood->zeroing_constants.single_turn_one_revolution_distance =
81 M_PI * 2.0 * constants::Values::kHoodSingleTurnEncoderRatio();
82 hood->zeroing_constants.measured_absolute_position = 0;
83 hood->zeroing_constants.single_turn_measured_absolute_position = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -080084
milind-ud53408e2021-10-21 19:43:58 -070085 constexpr double kDegToRad = M_PI / 180.0;
86 constexpr double kMmToM = 1.0 / 1000.0;
87 hood_geometry->theta_0 = 22.98004 * kDegToRad;
88 hood_geometry->screw_length_0 = 110.33888 * kMmToM;
89 hood_geometry->radius = 269.6262 * kMmToM;
90 hood_geometry->diagonal_length = 288.4353 * kMmToM;
91 hood_geometry->back_plate_diagonal_length = 22.86 * kMmToM;
92
Sabina Davise8d38992020-02-02 15:00:31 -080093 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
94 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake =
95 &r->intake;
96
97 // Intake constants.
98 intake->zeroing_voltage = 3.0;
99 intake->operating_voltage = 12.0;
100 intake->zeroing_profile_params = {0.5, 3.0};
101 intake->default_profile_params = {6.0, 30.0};
102 intake->range = Values::kIntakeRange();
103 intake->make_integral_loop =
104 control_loops::superstructure::intake::MakeIntegralIntakeLoop;
105 intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize;
106 intake->zeroing_constants.one_revolution_distance =
107 M_PI * 2.0 * constants::Values::kIntakeEncoderRatio();
108 intake->zeroing_constants.zeroing_threshold = 0.0005;
109 intake->zeroing_constants.moving_buffer_size = 20;
110 intake->zeroing_constants.allowable_encoder_error = 0.9;
111 intake->zeroing_constants.middle_position = Values::kIntakeRange().middle();
112
Kai Tinkess10943cf2020-02-01 15:49:57 -0800113 Values::PotAndAbsEncoderConstants *const turret = &r->turret;
114 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
115 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
116 *const turret_params = &turret->subsystem_params;
117
ravago901c4262020-02-16 15:33:14 -0800118 // Turret Constants
Kai Tinkess10943cf2020-02-01 15:49:57 -0800119 turret_params->zeroing_voltage = 4.0;
James Kuszmaul519585d2020-03-08 22:32:48 -0700120 turret_params->operating_voltage = 8.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800121 // TODO(austin): Tune these.
122 turret_params->zeroing_profile_params = {0.5, 2.0};
123 turret_params->default_profile_params = {15.0, 40.0};
124 turret_params->range = Values::kTurretRange();
125 turret_params->make_integral_loop =
126 &control_loops::superstructure::turret::MakeIntegralTurretLoop;
127 turret_params->zeroing_constants.average_filter_size =
128 Values::kZeroingSampleSize;
129 turret_params->zeroing_constants.one_revolution_distance =
130 M_PI * 2.0 * constants::Values::kTurretEncoderRatio();
131 turret_params->zeroing_constants.zeroing_threshold = 0.0005;
132 turret_params->zeroing_constants.moving_buffer_size = 20;
133 turret_params->zeroing_constants.allowable_encoder_error = 0.9;
134
Austin Schuh9dcd5202020-02-20 20:06:04 -0800135 CHECK_LE(hood->range.range(),
Ravago Jones937587c2020-12-26 17:21:09 -0800136 hood->zeroing_constants.single_turn_one_revolution_distance);
Austin Schuh9dcd5202020-02-20 20:06:04 -0800137 CHECK_LE(intake->range.range(),
138 intake->zeroing_constants.one_revolution_distance);
139
Stephan Massaltd021f972020-01-05 20:41:23 -0800140 switch (team) {
141 // A set of constants for tests.
142 case 1:
James Kuszmaul0a981402021-10-09 21:00:34 -0700143 case Values::kSpareRoborioTeamNumber:
Stephan Massaltd021f972020-01-05 20:41:23 -0800144 break;
145
James Kuszmaul0a981402021-10-09 21:00:34 -0700146 case Values::kCompTeamNumber:
Sabina Davisf7afd112020-02-23 13:42:14 -0800147 intake->zeroing_constants.measured_absolute_position =
Austin Schuh4dc06c72021-10-17 17:39:41 -0700148 0.433936997731885 - Values::kIntakeZero();
Kai Tinkess10943cf2020-02-01 15:49:57 -0800149
Austin Schuh2fb23642020-02-29 15:10:51 -0800150 turret->potentiometer_offset = 5.52519370141247 + 0.00853506822980376 +
James Kuszmaulf774d2d2021-10-03 08:02:35 -0700151 0.0109413725126625 - 0.223719825811759 +
Austin Schuh1216d992021-10-23 19:23:43 -0700152 0.261356551915472 - 0.0490168170767848 -
153 0.179342788816305;
Sabina Davisf7afd112020-02-23 13:42:14 -0800154 turret_params->zeroing_constants.measured_absolute_position =
Austin Schuh1216d992021-10-23 19:23:43 -0700155 2.75051496009509;
Ravago Jones937587c2020-12-26 17:21:09 -0800156
Austin Schuha2f01c62021-10-25 22:01:10 -0700157 hood->zeroing_constants.measured_absolute_position = 0.0102882878327271;
Austin Schuh2efe1682021-03-06 22:47:15 -0800158 hood->zeroing_constants.single_turn_measured_absolute_position =
Austin Schuhc09cbe02021-10-24 17:16:15 -0700159 0.302574797776192;
Stephan Massaltd021f972020-01-05 20:41:23 -0800160 break;
161
James Kuszmaul0a981402021-10-09 21:00:34 -0700162 case Values::kPracticeTeamNumber:
Sabina Davisa587fbd2020-01-31 22:11:15 -0800163 hood->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800164
Austin Schuh81c5f002021-11-06 22:59:07 -0700165 intake->zeroing_constants.measured_absolute_position = 0.205469223604347;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800166
Austin Schuh31904ee2021-03-06 23:52:08 -0800167 turret->potentiometer_offset = 5.3931926228241;
168 turret_params->zeroing_constants.measured_absolute_position = 4.22;
Stephan Massaltd021f972020-01-05 20:41:23 -0800169 break;
170
Austin Schuh83873c32020-02-22 14:58:39 -0800171 case Values::kCodingRobotTeamNumber:
Sabina Davisa587fbd2020-01-31 22:11:15 -0800172 hood->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800173
Sabina Davise8d38992020-02-02 15:00:31 -0800174 intake->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800175
176 turret->potentiometer_offset = 0.0;
177 turret_params->zeroing_constants.measured_absolute_position = 0.0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800178 break;
179
180 default:
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700181 LOG(FATAL) << "unknown team: " << team;
Stephan Massaltd021f972020-01-05 20:41:23 -0800182 }
183
184 return r;
185}
186
milind-u62d4a8e2021-10-11 16:08:41 -0700187const Values *values = nullptr;
188
189void DoGetValues() {
Stephan Massaltd021f972020-01-05 20:41:23 -0800190 uint16_t team = ::aos::network::GetTeamNumber();
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700191 LOG(INFO) << "creating a Constants for team: " << team;
milind-u62d4a8e2021-10-11 16:08:41 -0700192 values = DoGetValuesForTeam(team);
Stephan Massaltd021f972020-01-05 20:41:23 -0800193}
194
195} // namespace
196
milind-u62d4a8e2021-10-11 16:08:41 -0700197void InitValues() {
Stephan Massaltd021f972020-01-05 20:41:23 -0800198 static absl::once_flag once;
milind-u62d4a8e2021-10-11 16:08:41 -0700199 absl::call_once(once, DoGetValues);
Stephan Massaltd021f972020-01-05 20:41:23 -0800200}
201
milind-u62d4a8e2021-10-11 16:08:41 -0700202const Values &GetValues() {
203 CHECK(values)
204 << "Values are uninitialized. Call InitValues before accessing them.";
205 return *values;
Stephan Massaltd021f972020-01-05 20:41:23 -0800206}
207
208} // namespace constants
209} // namespace y2020