blob: e91a49c7b2a389e874f3001571532e6258ae457a [file] [log] [blame]
Tyler Chatow37ecdcd2019-01-26 20:18:42 -08001#include "y2019/constants.h"
2
3#include <inttypes.h>
4
5#include <map>
6
7#if __has_feature(address_sanitizer)
8#include "sanitizer/lsan_interface.h"
9#endif
10
11#include "aos/logging/logging.h"
12#include "aos/mutex/mutex.h"
13#include "aos/network/team_number.h"
14#include "aos/once.h"
15
16#ifndef M_PI
17#define M_PI 3.14159265358979323846
18#endif
19
20namespace y2019 {
21namespace constants {
22
23const int Values::kZeroingSampleSize;
24
25namespace {
26
27const uint16_t kCompTeamNumber = 971;
28const uint16_t kPracticeTeamNumber = 9971;
Alex Perry5fb5ff22019-02-09 21:53:17 -080029const uint16_t kCodingRobotTeamNumber = 7971;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -080030
31const Values *DoGetValuesForTeam(uint16_t team) {
32 Values *const r = new Values();
Alex Perry5fb5ff22019-02-09 21:53:17 -080033 Values::PotAndAbsConstants *const elevator = &r->elevator;
34 Values::Intake *const intake = &r->intake;
35 Values::PotAndAbsConstants *const stilts = &r->stilts;
36 Values::PotAndAbsConstants *const wrist = &r->wrist;
37
38 elevator->zeroing.average_filter_size = Values::kZeroingSampleSize;
39 elevator->zeroing.one_revolution_distance =
40 M_PI * 2.0 * constants::Values::kElevatorEncoderRatio();
41 elevator->zeroing.zeroing_threshold = 0.0005;
42 elevator->zeroing.moving_buffer_size = 20;
43 elevator->zeroing.allowable_encoder_error = 0.9;
44
45 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
46 intake->zeroing.one_revolution_distance =
47 M_PI * 2.0 * constants::Values::kIntakeEncoderRatio();
48 intake->zeroing.zeroing_threshold = 0.0005;
49 intake->zeroing.moving_buffer_size = 20;
50 intake->zeroing.allowable_encoder_error = 0.9;
51
52 stilts->zeroing.average_filter_size = Values::kZeroingSampleSize;
53 stilts->zeroing.one_revolution_distance =
54 M_PI * 2.0 * constants::Values::kStiltsEncoderRatio();
55 stilts->zeroing.zeroing_threshold = 0.0005;
56 stilts->zeroing.moving_buffer_size = 20;
57 stilts->zeroing.allowable_encoder_error = 0.9;
58
59 wrist->zeroing.average_filter_size = Values::kZeroingSampleSize;
60 wrist->zeroing.one_revolution_distance =
61 M_PI * 2.0 * constants::Values::kWristEncoderRatio();
62 wrist->zeroing.zeroing_threshold = 0.0005;
63 wrist->zeroing.moving_buffer_size = 20;
64 wrist->zeroing.allowable_encoder_error = 0.9;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -080065
66 switch (team) {
67 // A set of constants for tests.
68 case 1:
Alex Perry5fb5ff22019-02-09 21:53:17 -080069 elevator->zeroing.measured_absolute_position = 0.0;
70 elevator->potentiometer_offset = 0.0;
71
72 intake->zeroing.measured_absolute_position = 0.0;
73 intake->zeroing.middle_position = 0.0;
74
75 stilts->zeroing.measured_absolute_position = 0.0;
76 stilts->potentiometer_offset = 0.0;
77
78 wrist->zeroing.measured_absolute_position = 0.0;
79 wrist->potentiometer_offset = 0.0;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -080080 break;
81
82 case kCompTeamNumber:
Alex Perry5fb5ff22019-02-09 21:53:17 -080083 elevator->zeroing.measured_absolute_position = 0.0;
84 elevator->potentiometer_offset = 0.0;
85
86 intake->zeroing.measured_absolute_position = 0.0;
87 intake->zeroing.middle_position = 0.0;
88
89 stilts->zeroing.measured_absolute_position = 0.0;
90 stilts->potentiometer_offset = 0.0;
91
92 wrist->zeroing.measured_absolute_position = 0.0;
93 wrist->potentiometer_offset = 0.0;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -080094 break;
95
96 case kPracticeTeamNumber:
Alex Perry5fb5ff22019-02-09 21:53:17 -080097 elevator->zeroing.measured_absolute_position = 0.0;
98 elevator->potentiometer_offset = 0.0;
99
100 intake->zeroing.measured_absolute_position = 0.0;
101 intake->zeroing.middle_position = 0.0;
102
103 stilts->zeroing.measured_absolute_position = 0.0;
104 stilts->potentiometer_offset = 0.0;
105
106 wrist->zeroing.measured_absolute_position = 0.0;
107 wrist->potentiometer_offset = 0.0;
108 break;
109
110 case kCodingRobotTeamNumber:
111 elevator->zeroing.measured_absolute_position = 0.0;
112 elevator->potentiometer_offset = 0.0;
113
114 intake->zeroing.measured_absolute_position = 0.0;
115 intake->zeroing.middle_position = 0.0;
116
117 stilts->zeroing.measured_absolute_position = 0.0;
118 stilts->potentiometer_offset = 0.0;
119
120 wrist->zeroing.measured_absolute_position = 0.0;
121 wrist->potentiometer_offset = 0.0;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800122 break;
123
124 default:
125 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
126 }
127
128 return r;
129}
130
131const Values *DoGetValues() {
132 uint16_t team = ::aos::network::GetTeamNumber();
133 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
134 return DoGetValuesForTeam(team);
135}
136
137} // namespace
138
139const Values &GetValues() {
140 static ::aos::Once<const Values> once(DoGetValues);
141 return *once.Get();
142}
143
144const Values &GetValuesForTeam(uint16_t team_number) {
145 static ::aos::Mutex mutex;
146 ::aos::MutexLocker locker(&mutex);
147
148 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
149 // race conditions.
150 static ::std::map<uint16_t, const Values *> values;
151
152 if (values.count(team_number) == 0) {
153 values[team_number] = DoGetValuesForTeam(team_number);
154#if __has_feature(address_sanitizer)
155 __lsan_ignore_object(values[team_number]);
156#endif
157 }
158 return *values[team_number];
159}
160
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800161} // namespace constants
162} // namespace y2019