blob: 9284bead0dfc5544d29c97c08054b98362d323e7 [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,
55 {Values::kZeroingSampleSize,
56 Values::kIntakeEncoderIndexDifference, 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000057 },
58
59 // Shoulder
60 {
Brian Silverman1463c092020-10-30 17:28:24 -070061 0.0,
62 {Values::kZeroingSampleSize,
63 Values::kShoulderEncoderIndexDifference, 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000064 },
65
66 // Wrist
67 {
Brian Silverman1463c092020-10-30 17:28:24 -070068 0.0,
69 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
70 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000071 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -040072
Austin Schuh889fee82016-04-13 22:16:36 -070073 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -040074 "practice",
Comran Morshed315cf392016-02-14 20:40:22 +000075 };
76 break;
77
Comran Morshed9a9948c2016-01-16 15:58:04 +000078 case kCompTeamNumber:
79 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000080 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080081
82 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -070083 {
84 // Value to add to the pot reading for the intake.
85 -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 -
86 0.0725,
87 {Values::kZeroingSampleSize,
88 Values::kIntakeEncoderIndexDifference,
89 // Location of an index pulse.
90 0.018008, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080091 },
92
93 // Shoulder
Brian Silverman1463c092020-10-30 17:28:24 -070094 {
95 // Value to add to the pot reading for the shoulder.
96 -2.86275657117,
97 {Values::kZeroingSampleSize,
98 Values::kShoulderEncoderIndexDifference, 0.097312, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080099 },
100
101 // Wrist
Brian Silverman1463c092020-10-30 17:28:24 -0700102 {
103 // Value to add to the pot reading for the wrist.
104 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 -
105 0.0548 - 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 +
106 0.0166 - 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741,
107 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
108 0.000820, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800109 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400110
Austin Schuh889fee82016-04-13 22:16:36 -0700111 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400112 "competition",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000113 };
114 break;
115 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000116 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000117 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800118
119 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -0700120 {
121 // Hard stop is 160.0185751389329 degrees.
122 -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) +
123 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081,
124 {Values::kZeroingSampleSize,
125 Values::kIntakeEncoderIndexDifference, 0.332370, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800126 },
127
Austin Schuh449966b2016-03-11 21:27:05 -0800128 // Shoulder (Now calibrated at 0)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800129 {
Brian Silverman1463c092020-10-30 17:28:24 -0700130 -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 +
131 0.0034 + 0.0065 - 0.0505,
132 {Values::kZeroingSampleSize,
133 Values::kShoulderEncoderIndexDifference, 0.027180, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800134 },
135
136 // Wrist
137 {
Brian Silverman1463c092020-10-30 17:28:24 -0700138 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 -
139 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 +
140 0.1245,
141 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
142 -0.263227, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800143 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400144
Austin Schuh889fee82016-04-13 22:16:36 -0700145 0.011,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400146 "practice",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000147 };
148 break;
149 default:
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700150 LOG(FATAL) << "unknown team: " << team;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000151 }
152}
153
John Parka13bd372019-11-11 19:26:58 -0800154void DoGetValues(const Values **result) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000155 uint16_t team = ::aos::network::GetTeamNumber();
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700156 LOG(INFO) << "creating a Constants for team: " << team;
John Parka13bd372019-11-11 19:26:58 -0800157 *result = DoGetValuesForTeam(team);
158 return;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000159}
160
161} // namespace
162
163const Values &GetValues() {
John Parka13bd372019-11-11 19:26:58 -0800164 static absl::once_flag once;
165 static const Values *result;
166 absl::call_once(once, DoGetValues, &result);
167 return *result;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000168}
169
170const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman1463c092020-10-30 17:28:24 -0700171 static aos::stl_mutex mutex;
172 std::unique_lock<aos::stl_mutex> locker(mutex);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173
174 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
175 // race conditions.
176 static ::std::map<uint16_t, const Values *> values;
177
178 if (values.count(team_number) == 0) {
179 values[team_number] = DoGetValuesForTeam(team_number);
180#if __has_feature(address_sanitizer)
181 __lsan_ignore_object(values[team_number]);
182#endif
183 }
184 return *values[team_number];
185}
186
187} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000188} // namespace y2016