blob: 013cac1dc4275faf215112dc32364c2090bdb97f [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"
11#include "aos/logging/logging.h"
Stephan Massaltd021f972020-01-05 20:41:23 -080012#include "aos/network/team_number.h"
Brian Silverman1463c092020-10-30 17:28:24 -070013#include "aos/stl_mutex/stl_mutex.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;
Sabina Davisa587fbd2020-01-31 22:11:15 -080031
milind-u0a178a82021-09-28 18:42:09 -070032 // We found that the finisher velocity does not change ball velocity much, so
33 // keep it constant.
34 constexpr double kVelocityFinisher = 350.0;
Austin Schuh4408c422021-10-16 13:56:07 -070035 r->shot_interpolation_table =
36 InterpolationTable<Values::ShotParams>({{1.4732, {0.10, 10.6}},
37 {3.50, {0.48, 13.2}},
38 {4.7371, {0.535, 14.2}},
39 {5.27, {0.53, 14.55}},
40 {6.332, {0.53, 15.2}},
41 {7.48, {0.55, 17.0}},
42 {8.30, {0.565, 17.0}},
43 {9.20, {0.535, 17.0}}});
milind-u0a178a82021-09-28 18:42:09 -070044
45 r->flywheel_shot_interpolation_table =
46 InterpolationTable<Values::FlywheelShotParams>(
47 {{10.6, {250.0, kVelocityFinisher}},
48 {12.0, {275.0, kVelocityFinisher}},
49 {13.2, {300.0, kVelocityFinisher}},
50 {14.0, {325.0, kVelocityFinisher}},
51 {14.6, {350.0, kVelocityFinisher}},
52 {15.2, {375.0, kVelocityFinisher}},
53 {15.6, {400.0, kVelocityFinisher}},
54 {16.1, {425.0, kVelocityFinisher}},
55 {16.3, {450.0, kVelocityFinisher}},
56 {16.6, {475.0, kVelocityFinisher}},
57 {17.0, {500.0, kVelocityFinisher}}});
James Kuszmaul98154a22021-04-03 16:09:29 -070058
Sabina Davisa587fbd2020-01-31 22:11:15 -080059 // Hood constants.
Austin Schuh2efe1682021-03-06 22:47:15 -080060 hood->zeroing_voltage = 2.0;
Sabina Davisa587fbd2020-01-31 22:11:15 -080061 hood->operating_voltage = 12.0;
62 hood->zeroing_profile_params = {0.5, 3.0};
63 hood->default_profile_params = {6.0, 30.0};
64 hood->range = Values::kHoodRange();
65 hood->make_integral_loop =
66 control_loops::superstructure::hood::MakeIntegralHoodLoop;
67 hood->zeroing_constants.average_filter_size = Values::kZeroingSampleSize;
Sabina Davisa587fbd2020-01-31 22:11:15 -080068 hood->zeroing_constants.zeroing_threshold = 0.0005;
69 hood->zeroing_constants.moving_buffer_size = 20;
70 hood->zeroing_constants.allowable_encoder_error = 0.9;
Ravago Jones937587c2020-12-26 17:21:09 -080071 hood->zeroing_constants.one_revolution_distance =
72 M_PI * 2.0 * constants::Values::kHoodEncoderRatio();
73 hood->zeroing_constants.single_turn_middle_position =
74 Values::kHoodRange().middle();
75 hood->zeroing_constants.single_turn_one_revolution_distance =
76 M_PI * 2.0 * constants::Values::kHoodSingleTurnEncoderRatio();
77 hood->zeroing_constants.measured_absolute_position = 0;
78 hood->zeroing_constants.single_turn_measured_absolute_position = 0;
Stephan Massaltd021f972020-01-05 20:41:23 -080079
Sabina Davise8d38992020-02-02 15:00:31 -080080 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
81 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake =
82 &r->intake;
83
84 // Intake constants.
85 intake->zeroing_voltage = 3.0;
86 intake->operating_voltage = 12.0;
87 intake->zeroing_profile_params = {0.5, 3.0};
88 intake->default_profile_params = {6.0, 30.0};
89 intake->range = Values::kIntakeRange();
90 intake->make_integral_loop =
91 control_loops::superstructure::intake::MakeIntegralIntakeLoop;
92 intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize;
93 intake->zeroing_constants.one_revolution_distance =
94 M_PI * 2.0 * constants::Values::kIntakeEncoderRatio();
95 intake->zeroing_constants.zeroing_threshold = 0.0005;
96 intake->zeroing_constants.moving_buffer_size = 20;
97 intake->zeroing_constants.allowable_encoder_error = 0.9;
98 intake->zeroing_constants.middle_position = Values::kIntakeRange().middle();
99
Kai Tinkess10943cf2020-02-01 15:49:57 -0800100 Values::PotAndAbsEncoderConstants *const turret = &r->turret;
101 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
102 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
103 *const turret_params = &turret->subsystem_params;
104
ravago901c4262020-02-16 15:33:14 -0800105 // Turret Constants
Kai Tinkess10943cf2020-02-01 15:49:57 -0800106 turret_params->zeroing_voltage = 4.0;
James Kuszmaul519585d2020-03-08 22:32:48 -0700107 turret_params->operating_voltage = 8.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800108 // TODO(austin): Tune these.
109 turret_params->zeroing_profile_params = {0.5, 2.0};
110 turret_params->default_profile_params = {15.0, 40.0};
111 turret_params->range = Values::kTurretRange();
112 turret_params->make_integral_loop =
113 &control_loops::superstructure::turret::MakeIntegralTurretLoop;
114 turret_params->zeroing_constants.average_filter_size =
115 Values::kZeroingSampleSize;
116 turret_params->zeroing_constants.one_revolution_distance =
117 M_PI * 2.0 * constants::Values::kTurretEncoderRatio();
118 turret_params->zeroing_constants.zeroing_threshold = 0.0005;
119 turret_params->zeroing_constants.moving_buffer_size = 20;
120 turret_params->zeroing_constants.allowable_encoder_error = 0.9;
121
Austin Schuh9dcd5202020-02-20 20:06:04 -0800122 CHECK_LE(hood->range.range(),
Ravago Jones937587c2020-12-26 17:21:09 -0800123 hood->zeroing_constants.single_turn_one_revolution_distance);
Austin Schuh9dcd5202020-02-20 20:06:04 -0800124 CHECK_LE(intake->range.range(),
125 intake->zeroing_constants.one_revolution_distance);
126
Stephan Massaltd021f972020-01-05 20:41:23 -0800127 switch (team) {
128 // A set of constants for tests.
129 case 1:
James Kuszmaul0a981402021-10-09 21:00:34 -0700130 case Values::kSpareRoborioTeamNumber:
Stephan Massaltd021f972020-01-05 20:41:23 -0800131 break;
132
James Kuszmaul0a981402021-10-09 21:00:34 -0700133 case Values::kCompTeamNumber:
Sabina Davisf7afd112020-02-23 13:42:14 -0800134 intake->zeroing_constants.measured_absolute_position =
135 1.42977866919024 - Values::kIntakeZero();
Kai Tinkess10943cf2020-02-01 15:49:57 -0800136
Austin Schuh2fb23642020-02-29 15:10:51 -0800137 turret->potentiometer_offset = 5.52519370141247 + 0.00853506822980376 +
James Kuszmaulf774d2d2021-10-03 08:02:35 -0700138 0.0109413725126625 - 0.223719825811759 +
Austin Schuhd8a04702021-10-09 15:09:07 -0700139 0.261356551915472 - 0.0490168170767848;
Sabina Davisf7afd112020-02-23 13:42:14 -0800140 turret_params->zeroing_constants.measured_absolute_position =
Austin Schuhd8a04702021-10-09 15:09:07 -0700141 2.37257083307489;
Ravago Jones937587c2020-12-26 17:21:09 -0800142
Austin Schuh2efe1682021-03-06 22:47:15 -0800143 hood->zeroing_constants.measured_absolute_position = 0.0344482433884915;
144 hood->zeroing_constants.single_turn_measured_absolute_position =
145 0.31055891442198;
Stephan Massaltd021f972020-01-05 20:41:23 -0800146 break;
147
James Kuszmaul0a981402021-10-09 21:00:34 -0700148 case Values::kPracticeTeamNumber:
Sabina Davisa587fbd2020-01-31 22:11:15 -0800149 hood->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800150
Austin Schuh31904ee2021-03-06 23:52:08 -0800151 intake->zeroing_constants.measured_absolute_position = 0.347;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800152
Austin Schuh31904ee2021-03-06 23:52:08 -0800153 turret->potentiometer_offset = 5.3931926228241;
154 turret_params->zeroing_constants.measured_absolute_position = 4.22;
Stephan Massaltd021f972020-01-05 20:41:23 -0800155 break;
156
Austin Schuh83873c32020-02-22 14:58:39 -0800157 case Values::kCodingRobotTeamNumber:
Sabina Davisa587fbd2020-01-31 22:11:15 -0800158 hood->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800159
Sabina Davise8d38992020-02-02 15:00:31 -0800160 intake->zeroing_constants.measured_absolute_position = 0.0;
Kai Tinkess10943cf2020-02-01 15:49:57 -0800161
162 turret->potentiometer_offset = 0.0;
163 turret_params->zeroing_constants.measured_absolute_position = 0.0;
Stephan Massaltd021f972020-01-05 20:41:23 -0800164 break;
165
166 default:
167 AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
168 }
169
170 return r;
171}
172
milind-u62d4a8e2021-10-11 16:08:41 -0700173const Values *values = nullptr;
174
175void DoGetValues() {
Stephan Massaltd021f972020-01-05 20:41:23 -0800176 uint16_t team = ::aos::network::GetTeamNumber();
177 AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
milind-u62d4a8e2021-10-11 16:08:41 -0700178 values = DoGetValuesForTeam(team);
Stephan Massaltd021f972020-01-05 20:41:23 -0800179}
180
181} // namespace
182
milind-u62d4a8e2021-10-11 16:08:41 -0700183void InitValues() {
Stephan Massaltd021f972020-01-05 20:41:23 -0800184 static absl::once_flag once;
milind-u62d4a8e2021-10-11 16:08:41 -0700185 absl::call_once(once, DoGetValues);
Stephan Massaltd021f972020-01-05 20:41:23 -0800186}
187
milind-u62d4a8e2021-10-11 16:08:41 -0700188const Values &GetValues() {
189 CHECK(values)
190 << "Values are uninitialized. Call InitValues before accessing them.";
191 return *values;
Stephan Massaltd021f972020-01-05 20:41:23 -0800192}
193
194} // namespace constants
195} // namespace y2020