blob: 707fa9c9756f1652a4b957f1ca07c96c121be1a4 [file] [log] [blame]
Comran Morshed6c6a0a92016-01-17 12:45:16 +00001#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +00002
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cinttypes>
4#include <cmath>
5#include <cstdint>
Comran Morshed9a9948c2016-01-16 15:58:04 +00006#include <map>
7
8#if __has_feature(address_sanitizer)
9#include "sanitizer/lsan_interface.h"
10#endif
11
John Parka13bd372019-11-11 19:26:58 -080012#include "absl/base/call_once.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070013#include "glog/logging.h"
14
Brian Silverman1463c092020-10-30 17:28:24 -070015#include "aos/network/team_number.h"
16#include "aos/stl_mutex/stl_mutex.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000017#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Brian Silverman1463c092020-10-30 17:28:24 -070018#include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000019
20#ifndef M_PI
21#define M_PI 3.14159265358979323846
22#endif
23
Comran Morshed6c6a0a92016-01-17 12:45:16 +000024namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000025namespace constants {
Comran Morshed9a9948c2016-01-16 15:58:04 +000026
Comran Morshed225f0b92016-02-10 20:34:27 +000027// ///// Mutual constants between robots. /////
28const int Values::kZeroingSampleSize;
29
Austin Schuh33bc6842016-02-17 00:38:51 -080030constexpr double Values::kDrivetrainEncoderRatio, Values::kShooterEncoderRatio,
Comran Morshed225f0b92016-02-10 20:34:27 +000031 Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio,
32 Values::kWristEncoderRatio, Values::kIntakePotRatio,
33 Values::kShoulderPotRatio, Values::kWristPotRatio,
34 Values::kIntakeEncoderIndexDifference,
35 Values::kShoulderEncoderIndexDifference,
36 Values::kWristEncoderIndexDifference;
Brian Silvermanebca77a2016-02-14 22:14:00 -050037constexpr ::frc971::constants::Range Values::kIntakeRange,
38 Values::kShoulderRange, Values::kWristRange;
Comran Morshed225f0b92016-02-10 20:34:27 +000039
40namespace {
Comran Morshed9a9948c2016-01-16 15:58:04 +000041const uint16_t kCompTeamNumber = 971;
42const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed9a9948c2016-01-16 15:58:04 +000043
Comran Morshed225f0b92016-02-10 20:34:27 +000044// ///// Dynamic constants. /////
Austin Schuh04c894e2016-02-13 23:54:42 -080045
Comran Morshed9a9948c2016-01-16 15:58:04 +000046const Values *DoGetValuesForTeam(uint16_t team) {
47 switch (team) {
Comran Morshed315cf392016-02-14 20:40:22 +000048 case 1: // for tests
49 return new Values{
50 5.0, // drivetrain max speed
51
52 // Intake
53 {
Brian Silverman1463c092020-10-30 17:28:24 -070054 0.0,
James Kuszmauld12497a2024-01-14 18:00:34 -080055 {{},
56 Values::kZeroingSampleSize,
57 Values::kIntakeEncoderIndexDifference,
58 0.0,
59 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000060 },
61
62 // Shoulder
63 {
Brian Silverman1463c092020-10-30 17:28:24 -070064 0.0,
James Kuszmauld12497a2024-01-14 18:00:34 -080065 {{},
66 Values::kZeroingSampleSize,
67 Values::kShoulderEncoderIndexDifference,
68 0.0,
69 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000070 },
71
72 // Wrist
73 {
Brian Silverman1463c092020-10-30 17:28:24 -070074 0.0,
James Kuszmauld12497a2024-01-14 18:00:34 -080075 {{},
76 Values::kZeroingSampleSize,
77 Values::kWristEncoderIndexDifference,
78 0.0,
79 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000080 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -040081
Austin Schuh889fee82016-04-13 22:16:36 -070082 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -040083 "practice",
Comran Morshed315cf392016-02-14 20:40:22 +000084 };
85 break;
86
Comran Morshed9a9948c2016-01-16 15:58:04 +000087 case kCompTeamNumber:
88 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000089 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080090
91 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -070092 {
93 // Value to add to the pot reading for the intake.
94 -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 -
95 0.0725,
James Kuszmauld12497a2024-01-14 18:00:34 -080096 {{},
97 Values::kZeroingSampleSize,
Brian Silverman1463c092020-10-30 17:28:24 -070098 Values::kIntakeEncoderIndexDifference,
99 // Location of an index pulse.
James Kuszmauld12497a2024-01-14 18:00:34 -0800100 0.018008,
101 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800102 },
103
104 // Shoulder
Brian Silverman1463c092020-10-30 17:28:24 -0700105 {
106 // Value to add to the pot reading for the shoulder.
107 -2.86275657117,
James Kuszmauld12497a2024-01-14 18:00:34 -0800108 {{},
109 Values::kZeroingSampleSize,
110 Values::kShoulderEncoderIndexDifference,
111 0.097312,
112 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800113 },
114
115 // Wrist
Brian Silverman1463c092020-10-30 17:28:24 -0700116 {
117 // Value to add to the pot reading for the wrist.
118 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 -
119 0.0548 - 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 +
120 0.0166 - 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741,
James Kuszmauld12497a2024-01-14 18:00:34 -0800121 {{},
122 Values::kZeroingSampleSize,
123 Values::kWristEncoderIndexDifference,
124 0.000820,
125 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800126 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400127
Austin Schuh889fee82016-04-13 22:16:36 -0700128 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400129 "competition",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000130 };
131 break;
132 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000133 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000134 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800135
136 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -0700137 {
138 // Hard stop is 160.0185751389329 degrees.
139 -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) +
140 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081,
James Kuszmauld12497a2024-01-14 18:00:34 -0800141 {{},
142 Values::kZeroingSampleSize,
143 Values::kIntakeEncoderIndexDifference,
144 0.332370,
145 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800146 },
147
Austin Schuh449966b2016-03-11 21:27:05 -0800148 // Shoulder (Now calibrated at 0)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800149 {
Brian Silverman1463c092020-10-30 17:28:24 -0700150 -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 +
151 0.0034 + 0.0065 - 0.0505,
James Kuszmauld12497a2024-01-14 18:00:34 -0800152 {{},
153 Values::kZeroingSampleSize,
154 Values::kShoulderEncoderIndexDifference,
155 0.027180,
156 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800157 },
158
159 // Wrist
160 {
Brian Silverman1463c092020-10-30 17:28:24 -0700161 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 -
162 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 +
163 0.1245,
James Kuszmauld12497a2024-01-14 18:00:34 -0800164 {{},
165 Values::kZeroingSampleSize,
166 Values::kWristEncoderIndexDifference,
167 -0.263227,
168 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800169 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400170
Austin Schuh889fee82016-04-13 22:16:36 -0700171 0.011,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400172 "practice",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173 };
174 break;
175 default:
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700176 LOG(FATAL) << "unknown team: " << team;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000177 }
178}
179
John Parka13bd372019-11-11 19:26:58 -0800180void DoGetValues(const Values **result) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000181 uint16_t team = ::aos::network::GetTeamNumber();
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700182 LOG(INFO) << "creating a Constants for team: " << team;
John Parka13bd372019-11-11 19:26:58 -0800183 *result = DoGetValuesForTeam(team);
184 return;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000185}
186
187} // namespace
188
189const Values &GetValues() {
John Parka13bd372019-11-11 19:26:58 -0800190 static absl::once_flag once;
191 static const Values *result;
192 absl::call_once(once, DoGetValues, &result);
193 return *result;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000194}
195
196const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman1463c092020-10-30 17:28:24 -0700197 static aos::stl_mutex mutex;
198 std::unique_lock<aos::stl_mutex> locker(mutex);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000199
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
213} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000214} // namespace y2016