blob: f641e98183f6c8ee2040aad174d0b8bcbcba28bc [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
31constexpr double Values::kDrivetrainEncoderRatio, Values::kLowGearRatio,
Austin Schuh3130b372016-02-17 00:34:51 -080032 Values::kHighGearRatio, Values::kShooterEncoderRatio,
Comran Morshed225f0b92016-02-10 20:34:27 +000033 Values::kIntakeEncoderRatio, Values::kShoulderEncoderRatio,
34 Values::kWristEncoderRatio, Values::kIntakePotRatio,
35 Values::kShoulderPotRatio, Values::kWristPotRatio,
36 Values::kIntakeEncoderIndexDifference,
37 Values::kShoulderEncoderIndexDifference,
38 Values::kWristEncoderIndexDifference;
Brian Silvermanebca77a2016-02-14 22:14:00 -050039constexpr ::frc971::constants::Range Values::kIntakeRange,
40 Values::kShoulderRange, Values::kWristRange;
Comran Morshed225f0b92016-02-10 20:34:27 +000041
42namespace {
Comran Morshed9a9948c2016-01-16 15:58:04 +000043const uint16_t kCompTeamNumber = 971;
44const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed9a9948c2016-01-16 15:58:04 +000045
Comran Morshed225f0b92016-02-10 20:34:27 +000046// ///// Dynamic constants. /////
Austin Schuh04c894e2016-02-13 23:54:42 -080047
Comran Morshed9a9948c2016-01-16 15:58:04 +000048const Values *DoGetValuesForTeam(uint16_t team) {
49 switch (team) {
Comran Morshed315cf392016-02-14 20:40:22 +000050 case 1: // for tests
51 return new Values{
52 5.0, // drivetrain max speed
53
54 // Intake
55 {
56 0.0,
57 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
58 0.0, 0.3},
59 },
60
61 // Shoulder
62 {
63 0.0,
64 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
65 0.0, 0.3},
66 },
67
68 // Wrist
69 {
70 0.0,
71 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
72 0.0, 0.3},
73 },
74 };
75 break;
76
Comran Morshed9a9948c2016-01-16 15:58:04 +000077 case kCompTeamNumber:
78 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +000079 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080080
81 // Intake
82 {
Comran Morshed225f0b92016-02-10 20:34:27 +000083 0.0,
84 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
85 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080086 },
87
88 // Shoulder
89 {
Comran Morshed225f0b92016-02-10 20:34:27 +000090 0.0,
91 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
92 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080093 },
94
95 // Wrist
96 {
Comran Morshed225f0b92016-02-10 20:34:27 +000097 0.0,
98 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
99 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800100 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000101 };
102 break;
103 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000104 return new Values{
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000105 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800106
107 // Intake
108 {
Comran Morshed225f0b92016-02-10 20:34:27 +0000109 0.0,
110 {Values::kZeroingSampleSize, Values::kIntakeEncoderIndexDifference,
111 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800112 },
113
114 // Shoulder
115 {
Comran Morshed225f0b92016-02-10 20:34:27 +0000116 0.0,
117 {Values::kZeroingSampleSize, Values::kShoulderEncoderIndexDifference,
118 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800119 },
120
121 // Wrist
122 {
Comran Morshed225f0b92016-02-10 20:34:27 +0000123 0.0,
124 {Values::kZeroingSampleSize, Values::kWristEncoderIndexDifference,
125 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800126 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000127 };
128 break;
129 default:
130 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
131 }
132}
133
134const Values *DoGetValues() {
135 uint16_t team = ::aos::network::GetTeamNumber();
136 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
137 return DoGetValuesForTeam(team);
138}
139
140} // namespace
141
142const Values &GetValues() {
143 static ::aos::Once<const Values> once(DoGetValues);
144 return *once.Get();
145}
146
147const Values &GetValuesForTeam(uint16_t team_number) {
148 static ::aos::Mutex mutex;
149 ::aos::MutexLocker locker(&mutex);
150
151 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
152 // race conditions.
153 static ::std::map<uint16_t, const Values *> values;
154
155 if (values.count(team_number) == 0) {
156 values[team_number] = DoGetValuesForTeam(team_number);
157#if __has_feature(address_sanitizer)
158 __lsan_ignore_object(values[team_number]);
159#endif
160 }
161 return *values[team_number];
162}
163
164} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000165} // namespace y2016