blob: d4f61c1eace3fed73698c80b72ae110c1613acfb [file] [log] [blame]
Comran Morshed6c6a0a92016-01-17 12:45:16 +00001#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +00002
3#include <math.h>
4#include <stdint.h>
5#include <inttypes.h>
6
7#include <map>
8
9#if __has_feature(address_sanitizer)
10#include "sanitizer/lsan_interface.h"
11#endif
12
John Park33858a32018-09-28 23:05:48 -070013#include "aos/logging/logging.h"
14#include "aos/mutex/mutex.h"
15#include "aos/network/team_number.h"
Sabina Davis2ed5ea22017-09-26 22:27:42 -070016#include "aos/once.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000017
Comran Morshed6c6a0a92016-01-17 12:45:16 +000018#include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
19#include "y2016/control_loops/drivetrain/drivetrain_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 {
55 0.0,
56 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
57 0.0, 0.3},
58 },
59
60 // Shoulder
61 {
62 0.0,
63 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
64 0.0, 0.3},
65 },
66
67 // Wrist
68 {
69 0.0,
70 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
71 0.0, 0.3},
72 },
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
Campbell Crowleydf286e02016-09-06 18:23:11 -070084 {// Value to add to the pot reading for the intake.
Austin Schuh89031932016-03-26 19:44:27 -070085 -4.550531 + 150.40906362 * M_PI / 180.0 + 0.5098 - 0.0178 - 0.0725,
Comran Morshed225f0b92016-02-10 20:34:27 +000086 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
Austin Schuh9f77fd22016-02-21 02:53:58 -080087 // Location of an index pulse.
Austin Schuhe54b9872016-05-01 12:29:17 -070088 0.018008, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080089 },
90
91 // Shoulder
Campbell Crowleydf286e02016-09-06 18:23:11 -070092 {// Value to add to the pot reading for the shoulder.
Adam Snaidera16f1072016-10-04 11:01:05 -070093 -2.86275657117,
Comran Morshed225f0b92016-02-10 20:34:27 +000094 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
Adam Snaidera16f1072016-10-04 11:01:05 -070095 0.097312, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080096 },
97
98 // Wrist
Campbell Crowleydf286e02016-09-06 18:23:11 -070099 {// Value to add to the pot reading for the wrist.
Austin Schuh89031932016-03-26 19:44:27 -0700100 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 - 0.0548 -
Campbell Crowleydf286e02016-09-06 18:23:11 -0700101 0.0167 + 0.002 - 0.0026 - 0.1040 - 0.0035 - 0.0012 + 0.0166 -
Adam Snaidera16f1072016-10-04 11:01:05 -0700102 0.017 + 0.148 + 0.004 + 0.024701 - 0.0741,
Comran Morshed225f0b92016-02-10 20:34:27 +0000103 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
Adam Snaidera16f1072016-10-04 11:01:05 -0700104 0.000820, 2.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800105 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400106
Austin Schuh889fee82016-04-13 22:16:36 -0700107 0.0,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400108 "competition",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000109 };
110 break;
111 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000112 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000113 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800114
115 // Intake
Campbell Crowleydf286e02016-09-06 18:23:11 -0700116 {// Hard stop is 160.0185751389329 degrees.
117 -4.2193 + (160.0185751389329 * M_PI / 180.0 + 0.02 - 0.0235) +
118 0.0549 - 0.104 + 0.019 - 0.938 + 0.660 - 0.002 - 0.2081,
Comran Morshed225f0b92016-02-10 20:34:27 +0000119 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
Austin Schuh510e2412016-04-03 16:21:26 -0700120 0.332370, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800121 },
122
Austin Schuh449966b2016-03-11 21:27:05 -0800123 // Shoulder (Now calibrated at 0)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800124 {
Austin Schuhe54b9872016-05-01 12:29:17 -0700125 -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0 + 1.07838 - 1.0441 +
Austin Schuh009b93e2017-02-05 23:22:19 -0800126 0.0034 + 0.0065 - 0.0505,
Comran Morshed225f0b92016-02-10 20:34:27 +0000127 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
Austin Schuh009b93e2017-02-05 23:22:19 -0800128 0.027180, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800129 },
130
131 // Wrist
132 {
Austin Schuha3240bd2016-03-13 15:31:01 -0700133 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 -
Campbell Crowleydf286e02016-09-06 18:23:11 -0700134 0.023 + 0.0488 + 0.0120 - 0.0005 - 0.0784 - 0.0010 - 0.080 +
135 0.1245,
Comran Morshed225f0b92016-02-10 20:34:27 +0000136 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
Austin Schuhe54b9872016-05-01 12:29:17 -0700137 -0.263227, 1.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800138 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400139
Austin Schuh889fee82016-04-13 22:16:36 -0700140 0.011,
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400141 "practice",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000142 };
143 break;
144 default:
145 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
146 }
147}
148
149const Values *DoGetValues() {
150 uint16_t team = ::aos::network::GetTeamNumber();
151 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
152 return DoGetValuesForTeam(team);
153}
154
155} // namespace
156
157const Values &GetValues() {
158 static ::aos::Once<const Values> once(DoGetValues);
159 return *once.Get();
160}
161
162const Values &GetValuesForTeam(uint16_t team_number) {
163 static ::aos::Mutex mutex;
164 ::aos::MutexLocker locker(&mutex);
165
166 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
167 // race conditions.
168 static ::std::map<uint16_t, const Values *> values;
169
170 if (values.count(team_number) == 0) {
171 values[team_number] = DoGetValuesForTeam(team_number);
172#if __has_feature(address_sanitizer)
173 __lsan_ignore_object(values[team_number]);
174#endif
175 }
176 return *values[team_number];
177}
178
179} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000180} // namespace y2016