blob: 0c65b11a3698a7bd68e35a72eed7024aeda3902b [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080024namespace y2016::constants {
Comran Morshed9a9948c2016-01-16 15:58:04 +000025
Comran Morshed225f0b92016-02-10 20:34:27 +000026// ///// Mutual constants between robots. /////
27const int Values::kZeroingSampleSize;
28
Austin Schuh33bc6842016-02-17 00:38:51 -080029constexpr double Values::kDrivetrainEncoderRatio, Values::kShooterEncoderRatio,
Comran Morshed225f0b92016-02-10 20:34:27 +000030 Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio,
31 Values::kWristEncoderRatio, Values::kIntakePotRatio,
32 Values::kShoulderPotRatio, Values::kWristPotRatio,
33 Values::kIntakeEncoderIndexDifference,
34 Values::kShoulderEncoderIndexDifference,
35 Values::kWristEncoderIndexDifference;
Brian Silvermanebca77a2016-02-14 22:14:00 -050036constexpr ::frc971::constants::Range Values::kIntakeRange,
37 Values::kShoulderRange, Values::kWristRange;
Comran Morshed225f0b92016-02-10 20:34:27 +000038
39namespace {
Comran Morshed9a9948c2016-01-16 15:58:04 +000040const uint16_t kCompTeamNumber = 971;
41const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed9a9948c2016-01-16 15:58:04 +000042
Comran Morshed225f0b92016-02-10 20:34:27 +000043// ///// Dynamic constants. /////
Austin Schuh04c894e2016-02-13 23:54:42 -080044
Comran Morshed9a9948c2016-01-16 15:58:04 +000045const Values *DoGetValuesForTeam(uint16_t team) {
46 switch (team) {
Comran Morshed315cf392016-02-14 20:40:22 +000047 case 1: // for tests
48 return new Values{
49 5.0, // drivetrain max speed
50
51 // Intake
52 {
Brian Silverman1463c092020-10-30 17:28:24 -070053 0.0,
James Kuszmauld12497a2024-01-14 18:00:34 -080054 {{},
55 Values::kZeroingSampleSize,
56 Values::kIntakeEncoderIndexDifference,
57 0.0,
58 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000059 },
60
61 // Shoulder
62 {
Brian Silverman1463c092020-10-30 17:28:24 -070063 0.0,
James Kuszmauld12497a2024-01-14 18:00:34 -080064 {{},
65 Values::kZeroingSampleSize,
66 Values::kShoulderEncoderIndexDifference,
67 0.0,
68 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000069 },
70
71 // Wrist
72 {
Brian Silverman1463c092020-10-30 17:28:24 -070073 0.0,
James Kuszmauld12497a2024-01-14 18:00:34 -080074 {{},
75 Values::kZeroingSampleSize,
76 Values::kWristEncoderIndexDifference,
77 0.0,
78 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000079 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -040080
Austin Schuh889fee82016-04-13 22:16:36 -070081 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -040082 "practice",
Comran Morshed315cf392016-02-14 20:40:22 +000083 };
84 break;
85
Comran Morshed9a9948c2016-01-16 15:58:04 +000086 case kCompTeamNumber:
87 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000088 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080089
90 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -070091 {
92 // Value to add to the pot reading for the intake.
93 -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 -
94 0.0725,
James Kuszmauld12497a2024-01-14 18:00:34 -080095 {{},
96 Values::kZeroingSampleSize,
Brian Silverman1463c092020-10-30 17:28:24 -070097 Values::kIntakeEncoderIndexDifference,
98 // Location of an index pulse.
James Kuszmauld12497a2024-01-14 18:00:34 -080099 0.018008,
100 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800101 },
102
103 // Shoulder
Brian Silverman1463c092020-10-30 17:28:24 -0700104 {
105 // Value to add to the pot reading for the shoulder.
106 -2.86275657117,
James Kuszmauld12497a2024-01-14 18:00:34 -0800107 {{},
108 Values::kZeroingSampleSize,
109 Values::kShoulderEncoderIndexDifference,
110 0.097312,
111 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800112 },
113
114 // Wrist
Brian Silverman1463c092020-10-30 17:28:24 -0700115 {
116 // Value to add to the pot reading for the wrist.
117 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 -
118 0.0548 - 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 +
119 0.0166 - 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741,
James Kuszmauld12497a2024-01-14 18:00:34 -0800120 {{},
121 Values::kZeroingSampleSize,
122 Values::kWristEncoderIndexDifference,
123 0.000820,
124 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800125 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400126
Austin Schuh889fee82016-04-13 22:16:36 -0700127 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400128 "competition",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000129 };
130 break;
131 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000132 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000133 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800134
135 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -0700136 {
137 // Hard stop is 160.0185751389329 degrees.
138 -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) +
139 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081,
James Kuszmauld12497a2024-01-14 18:00:34 -0800140 {{},
141 Values::kZeroingSampleSize,
142 Values::kIntakeEncoderIndexDifference,
143 0.332370,
144 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800145 },
146
Austin Schuh449966b2016-03-11 21:27:05 -0800147 // Shoulder (Now calibrated at 0)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800148 {
Brian Silverman1463c092020-10-30 17:28:24 -0700149 -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 +
150 0.0034 + 0.0065 - 0.0505,
James Kuszmauld12497a2024-01-14 18:00:34 -0800151 {{},
152 Values::kZeroingSampleSize,
153 Values::kShoulderEncoderIndexDifference,
154 0.027180,
155 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800156 },
157
158 // Wrist
159 {
Brian Silverman1463c092020-10-30 17:28:24 -0700160 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 -
161 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 +
162 0.1245,
James Kuszmauld12497a2024-01-14 18:00:34 -0800163 {{},
164 Values::kZeroingSampleSize,
165 Values::kWristEncoderIndexDifference,
166 -0.263227,
167 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800168 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400169
Austin Schuh889fee82016-04-13 22:16:36 -0700170 0.011,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400171 "practice",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000172 };
173 break;
174 default:
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700175 LOG(FATAL) << "unknown team: " << team;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000176 }
177}
178
John Parka13bd372019-11-11 19:26:58 -0800179void DoGetValues(const Values **result) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000180 uint16_t team = ::aos::network::GetTeamNumber();
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700181 LOG(INFO) << "creating a Constants for team: " << team;
John Parka13bd372019-11-11 19:26:58 -0800182 *result = DoGetValuesForTeam(team);
183 return;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000184}
185
186} // namespace
187
188const Values &GetValues() {
John Parka13bd372019-11-11 19:26:58 -0800189 static absl::once_flag once;
190 static const Values *result;
191 absl::call_once(once, DoGetValues, &result);
192 return *result;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000193}
194
195const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman1463c092020-10-30 17:28:24 -0700196 static aos::stl_mutex mutex;
197 std::unique_lock<aos::stl_mutex> locker(mutex);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000198
199 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
200 // race conditions.
201 static ::std::map<uint16_t, const Values *> values;
202
203 if (values.count(team_number) == 0) {
204 values[team_number] = DoGetValuesForTeam(team_number);
205#if __has_feature(address_sanitizer)
206 __lsan_ignore_object(values[team_number]);
207#endif
208 }
209 return *values[team_number];
210}
211
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800212} // namespace y2016::constants