blob: 07856467da8fda069730ceca3d132c8fd0fc15e9 [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 },
73 };
74 break;
75
Comran Morshed9a9948c2016-01-16 15:58:04 +000076 case kCompTeamNumber:
77 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000078 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080079
80 // Intake
81 {
Comran Morshed225f0b92016-02-10 20:34:27 +000082 0.0,
83 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
84 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080085 },
86
87 // Shoulder
88 {
Comran Morshed225f0b92016-02-10 20:34:27 +000089 0.0,
90 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
91 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080092 },
93
94 // Wrist
95 {
Comran Morshed225f0b92016-02-10 20:34:27 +000096 0.0,
97 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
98 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080099 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000100 };
101 break;
102 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000103 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000104 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800105
106 // Intake
107 {
Comran Morshed225f0b92016-02-10 20:34:27 +0000108 0.0,
109 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
110 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800111 },
112
113 // Shoulder
114 {
Comran Morshed225f0b92016-02-10 20:34:27 +0000115 0.0,
116 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
117 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800118 },
119
120 // Wrist
121 {
Comran Morshed225f0b92016-02-10 20:34:27 +0000122 0.0,
123 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
124 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800125 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000126 };
127 break;
128 default:
129 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
130 }
131}
132
133const Values *DoGetValues() {
134 uint16_t team = ::aos::network::GetTeamNumber();
135 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
136 return DoGetValuesForTeam(team);
137}
138
139} // namespace
140
141const Values &GetValues() {
142 static ::aos::Once<const Values> once(DoGetValues);
143 return *once.Get();
144}
145
146const Values &GetValuesForTeam(uint16_t team_number) {
147 static ::aos::Mutex mutex;
148 ::aos::MutexLocker locker(&mutex);
149
150 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
151 // race conditions.
152 static ::std::map<uint16_t, const Values *> values;
153
154 if (values.count(team_number) == 0) {
155 values[team_number] = DoGetValuesForTeam(team_number);
156#if __has_feature(address_sanitizer)
157 __lsan_ignore_object(values[team_number]);
158#endif
159 }
160 return *values[team_number];
161}
162
163} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000164} // namespace y2016