blob: 4c4c5cf1e8b50fb2afca3223aedd9282fc5fadba [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#include "y2014/constants.h"
2
Brian Silverman1463c092020-10-30 17:28:24 -07003#include <inttypes.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07004#include <math.h>
5#include <stdint.h>
Brian Silverman17f503e2015-08-02 18:17:18 -07006
7#include <map>
8
9#if __has_feature(address_sanitizer)
10#include "sanitizer/lsan_interface.h"
11#endif
12
John Park54de36c2019-11-11 19:14:10 -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"
Brian Silverman17f503e2015-08-02 18:17:18 -070017
Brian Silverman17f503e2015-08-02 18:17:18 -070018#include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Brian Silverman1463c092020-10-30 17:28:24 -070019#include "y2014/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Brian Silverman17f503e2015-08-02 18:17:18 -070020
21#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
Austin Schuh6197a182015-11-28 16:04:40 -080025namespace y2014 {
Brian Silverman17f503e2015-08-02 18:17:18 -070026namespace constants {
27namespace {
28
29const uint16_t kCompTeamNumber = 971;
30const uint16_t kPracticeTeamNumber = 9971;
31const uint16_t kRoboRioTeamNumber = 254;
32
33const double kCompDrivetrainEncoderRatio =
34 (18.0 / 50.0) /*output reduction*/ * (56.0 / 30.0) /*encoder gears*/;
35const double kCompLowGearRatio = 18.0 / 60.0 * 18.0 / 50.0;
36const double kCompHighGearRatio = 28.0 / 50.0 * 18.0 / 50.0;
37
38const double kPracticeDrivetrainEncoderRatio = kCompDrivetrainEncoderRatio;
39const double kPracticeLowGearRatio = kCompLowGearRatio;
40const double kPracticeHighGearRatio = kCompHighGearRatio;
41
Brian Silverman1463c092020-10-30 17:28:24 -070042const DualHallShifterHallEffect kCompLeftDriveShifter{
43 {2.33, 4.25, 0.2, 0.7}, 2.61, 3.28};
44const DualHallShifterHallEffect kCompRightDriveShifter{
45 {4.31, 4.32, 0.2, 0.7}, 2.94, 3.25};
Brian Silverman17f503e2015-08-02 18:17:18 -070046
Brian Silverman1463c092020-10-30 17:28:24 -070047const DualHallShifterHallEffect kPracticeLeftDriveShifter{
48 {3.05, 4.15, 0.2, 0.7}, 2.80, 3.2};
49const DualHallShifterHallEffect kPracticeRightDriveShifter{
50 {3.75, 3.80, 0.2, 0.7}, 2.90, 2.98};
Brian Silverman17f503e2015-08-02 18:17:18 -070051
Brian Silverman17f503e2015-08-02 18:17:18 -070052const double shooter_zeroing_speed = 0.05;
53const double shooter_unload_speed = 0.08;
54
55// Smaller (more negative) = opening.
56const double kCompTopClawOffset = -0.120;
57
58const Values *DoGetValuesForTeam(uint16_t team) {
59 switch (team) {
60 case 1: // for tests
61 return new Values{
62 kCompDrivetrainEncoderRatio,
63 kCompLowGearRatio,
64 kCompHighGearRatio,
65 kCompLeftDriveShifter,
66 kCompRightDriveShifter,
67 false,
Austin Schuh0e997732015-11-08 15:14:53 -080068 ::y2014::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
Austin Schuh572ff402015-11-08 12:17:50 -080069 ::y2014::control_loops::drivetrain::MakeDrivetrainLoop,
Brian Silverman1463c092020-10-30 17:28:24 -070070 5.0, // drivetrain max speed
Brian Silverman17f503e2015-08-02 18:17:18 -070071
72 // ShooterLimits
Brian Silverman1463c092020-10-30 17:28:24 -070073 {-0.00127,
74 0.298196,
75 -0.0017,
76 0.305054,
77 0.0149098,
Brian Silverman17f503e2015-08-02 18:17:18 -070078 {-0.001778, 0.000762, 0, 0},
79 {-0.001778, 0.008906, 0, 0},
80 {0.006096, 0.026416, 0, 0},
81 shooter_zeroing_speed,
Brian Silverman1463c092020-10-30 17:28:24 -070082 shooter_unload_speed},
83 {
84 0.5,
85 0.1,
86 0.1,
87 0.0,
88 1.57,
89 0.05,
90 1.5,
91 {0.0,
92 2.05,
93 0.02,
94 2.02,
95 {-0.1, 0.05, -0.1, 0.05},
96 {1.0, 1.1, 1.0, 1.1},
97 {2.0, 2.1, 2.0, 2.1}},
98 {0.0,
99 2.05,
100 0.02,
101 2.02,
102 {-0.1, 0.05, -0.1, 0.05},
103 {1.0, 1.1, 1.0, 1.1},
104 {2.0, 2.1, 2.0, 2.1}},
105 0.01, // claw_unimportant_epsilon
106 0.9, // start_fine_tune_pos
107 4.0,
Brian Silverman17f503e2015-08-02 18:17:18 -0700108 },
Brian Silverman1463c092020-10-30 17:28:24 -0700109 {0.07, 0.15}, // shooter_action
Brian Silverman17f503e2015-08-02 18:17:18 -0700110 };
111 break;
112 case kCompTeamNumber:
113 return new Values{
114 kCompDrivetrainEncoderRatio,
115 kCompLowGearRatio,
116 kCompHighGearRatio,
117 kCompLeftDriveShifter,
118 kCompRightDriveShifter,
119 false,
Austin Schuh0e997732015-11-08 15:14:53 -0800120 ::y2014::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
Austin Schuh572ff402015-11-08 12:17:50 -0800121 ::y2014::control_loops::drivetrain::MakeDrivetrainLoop,
Brian Silverman1463c092020-10-30 17:28:24 -0700122 5.0, // drivetrain max speed
Brian Silverman17f503e2015-08-02 18:17:18 -0700123
124 // ShooterLimits
Brian Silverman1463c092020-10-30 17:28:24 -0700125 {-0.001041,
126 0.296019,
127 -0.001488,
128 0.302717,
129 0.0149098,
Brian Silverman17f503e2015-08-02 18:17:18 -0700130 {-0.002, 0.000446, -0.002, 0.000446},
131 {-0.002, 0.009078, -0.002, 0.009078},
132 {0.003870, 0.026194, 0.003869, 0.026343},
133 shooter_zeroing_speed,
Brian Silverman1463c092020-10-30 17:28:24 -0700134 shooter_unload_speed},
135 {
136 0.800000,
137 0.400000,
138 0.000000,
139 -1.220821,
140 1.822142,
141 -0.849484,
142 1.42309,
143 // 0.0371
144 {-3.3284,
145 2.0917,
146 -3.1661,
147 1.95,
148 {-3.4, -3.02 + kCompTopClawOffset, -3.4,
149 -2.9876 + kCompTopClawOffset},
150 {-0.1433 + kCompTopClawOffset, 0.0670 + kCompTopClawOffset,
151 -0.1460 + kCompTopClawOffset, 0.0648 + kCompTopClawOffset},
152 {1.9952 + kCompTopClawOffset, 2.2, 1.9898 + kCompTopClawOffset,
153 2.2}},
154 {-2.453460,
155 3.082960,
156 -2.453460,
157 3.082960,
158 {-2.6, -2.185752, -2.6, -2.184843},
159 {-0.322249, -0.053177, -0.332248, -0.059086},
160 {2.892065, 3.2, 2.888429, 3.2}},
161 0.040000, // claw_unimportant_epsilon
162 -0.400000, // start_fine_tune_pos
163 4.000000,
Brian Silverman17f503e2015-08-02 18:17:18 -0700164 },
Brian Silverman1463c092020-10-30 17:28:24 -0700165 // TODO(james): Get realer numbers for shooter_action.
166 {0.07, 0.15}, // shooter_action
Brian Silverman17f503e2015-08-02 18:17:18 -0700167 };
168 break;
169 case kPracticeTeamNumber:
170 case kRoboRioTeamNumber:
171 return new Values{
172 kPracticeDrivetrainEncoderRatio,
173 kPracticeLowGearRatio,
174 kPracticeHighGearRatio,
175 kPracticeLeftDriveShifter,
176 kPracticeRightDriveShifter,
177 false,
Austin Schuh0e997732015-11-08 15:14:53 -0800178 ::y2014::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
Austin Schuh572ff402015-11-08 12:17:50 -0800179 ::y2014::control_loops::drivetrain::MakeDrivetrainLoop,
Brian Silverman1463c092020-10-30 17:28:24 -0700180 5.0, // drivetrain max speed
Brian Silverman17f503e2015-08-02 18:17:18 -0700181
182 // ShooterLimits
Brian Silverman1463c092020-10-30 17:28:24 -0700183 {-0.001042,
184 0.294084,
185 -0.001935,
186 0.303460,
187 0.0138401,
Brian Silverman17f503e2015-08-02 18:17:18 -0700188 {-0.002, 0.000446, -0.002, 0.000446},
Brian Silvermanba622de2017-08-05 19:27:17 -0700189 {-0.002 + 0.001, 0.009078 + 0.001, -0.002 + 0.001, 0.009078 + 0.001},
Brian Silverman1463c092020-10-30 17:28:24 -0700190 {0.003869 + 0.001, 0.026194 + 0.001, 0.003869 + 0.001,
191 0.026194 + 0.001},
Brian Silverman17f503e2015-08-02 18:17:18 -0700192 shooter_zeroing_speed,
Brian Silverman1463c092020-10-30 17:28:24 -0700193 shooter_unload_speed},
194 {
195 0.400000 * 2.0,
196 0.200000 * 2.0,
197 0.000000 * 2.0,
198 -0.762218 * 2.0,
199 1.767146,
200 -0.849484,
201 1.42308,
202 {-3.364758,
203 2.086668,
204 -3.166136,
205 1.95,
206 {-1.7 * 2.0, -1.544662 * 2.0 + 0.139081, -1.7 * 2.0,
207 -1.547616 * 2.0 + 0.139081 + 0.013636},
208 {-0.115446 * 2.0, 0.030452 * 2.0, -0.120900 * 2.0,
209 0.023862 * 2.0},
210 {0.977884 * 2.0, 1.4 * 2.0, 0.963113 * 2.0, 1.4 * 2.0}},
211 {-2.451642,
212 3.107504,
213 -2.273474,
214 2.750,
215 {-1.5 * 2.0, -1.027199 * 2.0, -1.5 * 2.0, -1.037880 * 2.0},
216 {-0.116355 * 2.0, 0.017726 * 2.0, -0.125673 * 2.0,
217 0.008636 * 2.0},
218 {2.894792 + 0.122719, 3.2, 2.887974 + 0.122719 - 0.029088, 3.2}},
219 0.040000, // claw_unimportant_epsilon
220 -0.400000, // start_fine_tune_pos
221 4.000000,
Brian Silverman17f503e2015-08-02 18:17:18 -0700222 },
Brian Silverman1463c092020-10-30 17:28:24 -0700223 // TODO(james): Get realer numbers for shooter_action.
224 {0.07, 0.15}, // shooter_action
Brian Silverman17f503e2015-08-02 18:17:18 -0700225 };
226 break;
227 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700228 AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
Brian Silverman17f503e2015-08-02 18:17:18 -0700229 }
230}
231
John Park54de36c2019-11-11 19:14:10 -0800232void DoGetValues(const Values **result) {
Brian Silverman17f503e2015-08-02 18:17:18 -0700233 uint16_t team = ::aos::network::GetTeamNumber();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700234 AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
John Park54de36c2019-11-11 19:14:10 -0800235 *result = DoGetValuesForTeam(team);
236 return;
Brian Silverman17f503e2015-08-02 18:17:18 -0700237}
238
239} // namespace
240
241const Values &GetValues() {
John Park54de36c2019-11-11 19:14:10 -0800242 static absl::once_flag once;
243 static const Values *result;
244 absl::call_once(once, DoGetValues, &result);
245 return *result;
Brian Silverman17f503e2015-08-02 18:17:18 -0700246}
247
248const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman1463c092020-10-30 17:28:24 -0700249 static aos::stl_mutex mutex;
250 std::unique_lock<aos::stl_mutex> locker(mutex);
Brian Silverman17f503e2015-08-02 18:17:18 -0700251
252 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
253 // race conditions.
254 static ::std::map<uint16_t, const Values *> values;
255
256 if (values.count(team_number) == 0) {
257 values[team_number] = DoGetValuesForTeam(team_number);
258#if __has_feature(address_sanitizer)
259 __lsan_ignore_object(values[team_number]);
260#endif
261 }
262 return *values[team_number];
263}
264
265} // namespace constants
Austin Schuh6197a182015-11-28 16:04:40 -0800266} // namespace y2014