blob: 1f4ae9ed221173d47e9c1169331f702043a10b9e [file] [log] [blame]
Comran Morshed6c6a0a92016-01-17 12:45:16 +00001#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +00002
Brian Silverman1463c092020-10-30 17:28:24 -07003#include <inttypes.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00004#include <math.h>
5#include <stdint.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00006
7#include <map>
8
9#if __has_feature(address_sanitizer)
10#include "sanitizer/lsan_interface.h"
11#endif
12
John Parka13bd372019-11-11 19:26:58 -080013#include "absl/base/call_once.h"
Brian Silverman1463c092020-10-30 17:28:24 -070014#include "aos/logging/logging.h"
15#include "aos/network/team_number.h"
16#include "aos/stl_mutex/stl_mutex.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000017
Comran Morshed6c6a0a92016-01-17 12:45:16 +000018#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Brian Silverman1463c092020-10-30 17:28:24 -070019#include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000020
21#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
Comran Morshed6c6a0a92016-01-17 12:45:16 +000025namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000026namespace constants {
Comran Morshed9a9948c2016-01-16 15:58:04 +000027
Comran Morshed225f0b92016-02-10 20:34:27 +000028// ///// Mutual constants between robots. /////
29const int Values::kZeroingSampleSize;
30
Austin Schuh33bc6842016-02-17 00:38:51 -080031constexpr double Values::kDrivetrainEncoderRatio, Values::kShooterEncoderRatio,
Comran Morshed225f0b92016-02-10 20:34:27 +000032 Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio,
33 Values::kWristEncoderRatio, Values::kIntakePotRatio,
34 Values::kShoulderPotRatio, Values::kWristPotRatio,
35 Values::kIntakeEncoderIndexDifference,
36 Values::kShoulderEncoderIndexDifference,
37 Values::kWristEncoderIndexDifference;
Brian Silvermanebca77a2016-02-14 22:14:00 -050038constexpr ::frc971::constants::Range Values::kIntakeRange,
39 Values::kShoulderRange, Values::kWristRange;
Comran Morshed225f0b92016-02-10 20:34:27 +000040
41namespace {
Comran Morshed9a9948c2016-01-16 15:58:04 +000042const uint16_t kCompTeamNumber = 971;
43const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed9a9948c2016-01-16 15:58:04 +000044
Comran Morshed225f0b92016-02-10 20:34:27 +000045// ///// Dynamic constants. /////
Austin Schuh04c894e2016-02-13 23:54:42 -080046
Comran Morshed9a9948c2016-01-16 15:58:04 +000047const Values *DoGetValuesForTeam(uint16_t team) {
48 switch (team) {
Comran Morshed315cf392016-02-14 20:40:22 +000049 case 1: // for tests
50 return new Values{
51 5.0, // drivetrain max speed
52
53 // Intake
54 {
Brian Silverman1463c092020-10-30 17:28:24 -070055 0.0,
56 {Values::kZeroingSampleSize,
57 Values::kIntakeEncoderIndexDifference, 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000058 },
59
60 // Shoulder
61 {
Brian Silverman1463c092020-10-30 17:28:24 -070062 0.0,
63 {Values::kZeroingSampleSize,
64 Values::kShoulderEncoderIndexDifference, 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000065 },
66
67 // Wrist
68 {
Brian Silverman1463c092020-10-30 17:28:24 -070069 0.0,
70 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
71 0.0, 0.3},
Comran Morshed315cf392016-02-14 20:40:22 +000072 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -040073
Austin Schuh889fee82016-04-13 22:16:36 -070074 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -040075 "practice",
Comran Morshed315cf392016-02-14 20:40:22 +000076 };
77 break;
78
Comran Morshed9a9948c2016-01-16 15:58:04 +000079 case kCompTeamNumber:
80 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000081 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080082
83 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -070084 {
85 // Value to add to the pot reading for the intake.
86 -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 -
87 0.0725,
88 {Values::kZeroingSampleSize,
89 Values::kIntakeEncoderIndexDifference,
90 // Location of an index pulse.
91 0.018008, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080092 },
93
94 // Shoulder
Brian Silverman1463c092020-10-30 17:28:24 -070095 {
96 // Value to add to the pot reading for the shoulder.
97 -2.86275657117,
98 {Values::kZeroingSampleSize,
99 Values::kShoulderEncoderIndexDifference, 0.097312, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800100 },
101
102 // Wrist
Brian Silverman1463c092020-10-30 17:28:24 -0700103 {
104 // Value to add to the pot reading for the wrist.
105 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 -
106 0.0548 - 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 +
107 0.0166 - 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741,
108 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
109 0.000820, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800110 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400111
Austin Schuh889fee82016-04-13 22:16:36 -0700112 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400113 "competition",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000114 };
115 break;
116 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000117 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000118 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800119
120 // Intake
Brian Silverman1463c092020-10-30 17:28:24 -0700121 {
122 // Hard stop is 160.0185751389329 degrees.
123 -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) +
124 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081,
125 {Values::kZeroingSampleSize,
126 Values::kIntakeEncoderIndexDifference, 0.332370, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800127 },
128
Austin Schuh449966b2016-03-11 21:27:05 -0800129 // Shoulder (Now calibrated at 0)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800130 {
Brian Silverman1463c092020-10-30 17:28:24 -0700131 -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 +
132 0.0034 + 0.0065 - 0.0505,
133 {Values::kZeroingSampleSize,
134 Values::kShoulderEncoderIndexDifference, 0.027180, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800135 },
136
137 // Wrist
138 {
Brian Silverman1463c092020-10-30 17:28:24 -0700139 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 -
140 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 +
141 0.1245,
142 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
143 -0.263227, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800144 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400145
Austin Schuh889fee82016-04-13 22:16:36 -0700146 0.011,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400147 "practice",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000148 };
149 break;
150 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700151 AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000152 }
153}
154
John Parka13bd372019-11-11 19:26:58 -0800155void DoGetValues(const Values **result) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000156 uint16_t team = ::aos::network::GetTeamNumber();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700157 AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
John Parka13bd372019-11-11 19:26:58 -0800158 *result = DoGetValuesForTeam(team);
159 return;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000160}
161
162} // namespace
163
164const Values &GetValues() {
John Parka13bd372019-11-11 19:26:58 -0800165 static absl::once_flag once;
166 static const Values *result;
167 absl::call_once(once, DoGetValues, &result);
168 return *result;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000169}
170
171const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman1463c092020-10-30 17:28:24 -0700172 static aos::stl_mutex mutex;
173 std::unique_lock<aos::stl_mutex> locker(mutex);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174
175 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
176 // race conditions.
177 static ::std::map<uint16_t, const Values *> values;
178
179 if (values.count(team_number) == 0) {
180 values[team_number] = DoGetValuesForTeam(team_number);
181#if __has_feature(address_sanitizer)
182 __lsan_ignore_object(values[team_number]);
183#endif
184 }
185 return *values[team_number];
186}
187
188} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000189} // namespace y2016