blob: 4f1a01e3411a0ea260849ee41ca14d06fd03a907 [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
13#include "aos/common/logging/logging.h"
14#include "aos/common/once.h"
15#include "aos/common/network/team_number.h"
16#include "aos/common/mutex.h"
17
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
74 "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
83 {
Austin Schuh9f77fd22016-02-21 02:53:58 -080084 // 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 Schuh89031932016-03-26 19:44:27 -070088 0.018008, 1.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080089 },
90
91 // Shoulder
92 {
Austin Schuh9f77fd22016-02-21 02:53:58 -080093 // Value to add to the pot reading for the shoulder.
Austin Schuh89031932016-03-26 19:44:27 -070094 -1.0 - 0.0822 + 0.06138835 * M_PI / 180.0 - 0.0323 - 0.1057 +
95 0.0035 + 0.0055,
Comran Morshed225f0b92016-02-10 20:34:27 +000096 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
Austin Schuh89031932016-03-26 19:44:27 -070097 0.536989, 1.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080098 },
99
100 // Wrist
101 {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800102 // Value to add to the pot reading for the wrist.
Austin Schuh89031932016-03-26 19:44:27 -0700103 3.2390714288298668 + -0.06138835 * M_PI / 180.0 + 0.0078 - 0.0548 -
104 0.0167 + 0.002,
Comran Morshed225f0b92016-02-10 20:34:27 +0000105 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
Austin Schuh89031932016-03-26 19:44:27 -0700106 -1.040454, 1.5},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800107 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400108
109 "competition",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000110 };
111 break;
112 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000113 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000114 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800115
116 // Intake
117 {
Austin Schuh449966b2016-03-11 21:27:05 -0800118 // Hard stop is 164.2067247 degrees.
Austin Schuha3240bd2016-03-13 15:31:01 -0700119 -4.2193 + (164.2067247 * M_PI / 180.0 + 0.02 - 0.0235) + 0.0549 -
120 0.104,
Comran Morshed225f0b92016-02-10 20:34:27 +0000121 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
Austin Schuha3240bd2016-03-13 15:31:01 -0700122 0.363074, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800123 },
124
Austin Schuh449966b2016-03-11 21:27:05 -0800125 // Shoulder (Now calibrated at 0)
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800126 {
Austin Schuh449966b2016-03-11 21:27:05 -0800127 -1.0016 - 0.0841 + 0.06138835 * M_PI / 180.0,
Comran Morshed225f0b92016-02-10 20:34:27 +0000128 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
Austin Schuh449966b2016-03-11 21:27:05 -0800129 0.014860, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800130 },
131
132 // Wrist
133 {
Austin Schuha3240bd2016-03-13 15:31:01 -0700134 3.326328571170133 - 0.06138835 * M_PI / 180.0 - 0.177 + 0.0323 -
135 0.023,
Comran Morshed225f0b92016-02-10 20:34:27 +0000136 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
Austin Schuha3240bd2016-03-13 15:31:01 -0700137 -0.622423, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800138 },
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400139
140 "practice",
Comran Morshed9a9948c2016-01-16 15:58:04 +0000141 };
142 break;
143 default:
144 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
145 }
146}
147
148const Values *DoGetValues() {
149 uint16_t team = ::aos::network::GetTeamNumber();
150 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
151 return DoGetValuesForTeam(team);
152}
153
154} // namespace
155
156const Values &GetValues() {
157 static ::aos::Once<const Values> once(DoGetValues);
158 return *once.Get();
159}
160
161const Values &GetValuesForTeam(uint16_t team_number) {
162 static ::aos::Mutex mutex;
163 ::aos::MutexLocker locker(&mutex);
164
165 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
166 // race conditions.
167 static ::std::map<uint16_t, const Values *> values;
168
169 if (values.count(team_number) == 0) {
170 values[team_number] = DoGetValuesForTeam(team_number);
171#if __has_feature(address_sanitizer)
172 __lsan_ignore_object(values[team_number]);
173#endif
174 }
175 return *values[team_number];
176}
177
178} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000179} // namespace y2016