blob: 363ef264d953740eb5047dc8102fd3dcd275150a [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"
Brian Silverman1463c092020-10-30 17:28:24 -070013#include "aos/logging/logging.h"
14#include "aos/network/team_number.h"
15#include "aos/stl_mutex/stl_mutex.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000016#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Brian Silverman1463c092020-10-30 17:28:24 -070017#include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000018
19#ifndef M_PI
20#define M_PI 3.14159265358979323846
21#endif
22
Comran Morshed6c6a0a92016-01-17 12:45:16 +000023namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000024namespace 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,
54 {Values::kZeroingSampleSize,
55 Values::kIntakeEncoderIndexDifference, 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000056 },
57
58 // Shoulder
59 {
Brian Silverman1463c092020-10-30 17:28:24 -070060 0.0,
61 {Values::kZeroingSampleSize,
62 Values::kShoulderEncoderIndexDifference, 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000063 },
64
65 // Wrist
66 {
Brian Silverman1463c092020-10-30 17:28:24 -070067 0.0,
68 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
69 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000070 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -040071
Austin Schuh889fee82016-04-13 22:16:36 -070072 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -040073 "practice",
Comran Morshed315cf392016-02-14 20:40:22 +000074 };
75 break;
76
Comran Morshed9a9948c2016-01-16 15:58:04 +000077 case kCompTeamNumber:
78 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000079 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080080
81 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -070082 {
83 // Value to add to the pot reading for the intake.
84 -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 -
85 0.0725,
86 {Values::kZeroingSampleSize,
87 Values::kIntakeEncoderIndexDifference,
88 // Location of an index pulse.
89 0.018008, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080090 },
91
92 // Shoulder
Brian Silverman1463c092020-10-30 17:28:24 -070093 {
94 // Value to add to the pot reading for the shoulder.
95 -2.86275657117,
96 {Values::kZeroingSampleSize,
97 Values::kShoulderEncoderIndexDifference, 0.097312, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080098 },
99
100 // Wrist
Brian Silverman1463c092020-10-30 17:28:24 -0700101 {
102 // Value to add to the pot reading for the wrist.
103 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 -
104 0.0548 - 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 +
105 0.0166 - 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741,
106 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
107 0.000820, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800108 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400109
Austin Schuh889fee82016-04-13 22:16:36 -0700110 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400111 "competition",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000112 };
113 break;
114 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000115 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000116 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800117
118 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -0700119 {
120 // Hard stop is 160.0185751389329 degrees.
121 -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) +
122 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081,
123 {Values::kZeroingSampleSize,
124 Values::kIntakeEncoderIndexDifference, 0.332370, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800125 },
126
Austin Schuh449966b2016-03-11 21:27:05 -0800127 // Shoulder (Now calibrated at 0)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800128 {
Brian Silverman1463c092020-10-30 17:28:24 -0700129 -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 +
130 0.0034 + 0.0065 - 0.0505,
131 {Values::kZeroingSampleSize,
132 Values::kShoulderEncoderIndexDifference, 0.027180, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800133 },
134
135 // Wrist
136 {
Brian Silverman1463c092020-10-30 17:28:24 -0700137 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 -
138 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 +
139 0.1245,
140 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
141 -0.263227, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800142 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400143
Austin Schuh889fee82016-04-13 22:16:36 -0700144 0.011,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400145 "practice",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000146 };
147 break;
148 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700149 AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000150 }
151}
152
John Parka13bd372019-11-11 19:26:58 -0800153void DoGetValues(const Values **result) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000154 uint16_t team = ::aos::network::GetTeamNumber();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700155 AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
John Parka13bd372019-11-11 19:26:58 -0800156 *result = DoGetValuesForTeam(team);
157 return;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000158}
159
160} // namespace
161
162const Values &GetValues() {
John Parka13bd372019-11-11 19:26:58 -0800163 static absl::once_flag once;
164 static const Values *result;
165 absl::call_once(once, DoGetValues, &result);
166 return *result;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000167}
168
169const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman1463c092020-10-30 17:28:24 -0700170 static aos::stl_mutex mutex;
171 std::unique_lock<aos::stl_mutex> locker(mutex);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000172
173 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
174 // race conditions.
175 static ::std::map<uint16_t, const Values *> values;
176
177 if (values.count(team_number) == 0) {
178 values[team_number] = DoGetValuesForTeam(team_number);
179#if __has_feature(address_sanitizer)
180 __lsan_ignore_object(values[team_number]);
181#endif
182 }
183 return *values[team_number];
184}
185
186} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000187} // namespace y2016