blob: 574f29939882ad5c842a66300d2b72c79b35c322 [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"
Theo Bafrali00e42272019-02-12 01:07:46 -080015#include "y2019/control_loops/superstructure/elevator/integral_elevator_plant.h"
16#include "y2019/control_loops/superstructure/intake/integral_intake_plant.h"
17#include "y2019/control_loops/superstructure/stilts/integral_stilts_plant.h"
18#include "y2019/control_loops/superstructure/wrist/integral_wrist_plant.h"
Tyler Chatow37ecdcd2019-01-26 20:18:42 -080019
20namespace y2019 {
21namespace constants {
22
Theo Bafrali00e42272019-02-12 01:07:46 -080023using ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator;
24
Tyler Chatow37ecdcd2019-01-26 20:18:42 -080025const int Values::kZeroingSampleSize;
26
27namespace {
28
29const uint16_t kCompTeamNumber = 971;
30const uint16_t kPracticeTeamNumber = 9971;
Alex Perry5fb5ff22019-02-09 21:53:17 -080031const uint16_t kCodingRobotTeamNumber = 7971;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -080032
33const Values *DoGetValuesForTeam(uint16_t team) {
34 Values *const r = new Values();
Alex Perry5fb5ff22019-02-09 21:53:17 -080035 Values::PotAndAbsConstants *const elevator = &r->elevator;
Theo Bafrali00e42272019-02-12 01:07:46 -080036 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
37 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
38 *const elevator_params = &(elevator->subsystem_params);
Alex Perry5fb5ff22019-02-09 21:53:17 -080039 Values::PotAndAbsConstants *const stilts = &r->stilts;
Theo Bafrali00e42272019-02-12 01:07:46 -080040 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
41 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
42 *const stilts_params = &(stilts->subsystem_params);
Alex Perry5fb5ff22019-02-09 21:53:17 -080043 Values::PotAndAbsConstants *const wrist = &r->wrist;
Theo Bafrali00e42272019-02-12 01:07:46 -080044 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
45 ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator>
46 *const wrist_params = &(wrist->subsystem_params);
47 ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemParams<
48 ::frc971::zeroing::AbsoluteEncoderZeroingEstimator> *const intake =
49 &r->intake;
Alex Perry5fb5ff22019-02-09 21:53:17 -080050
Theo Bafrali00e42272019-02-12 01:07:46 -080051 // Elevator constants.
52 elevator_params->zeroing_voltage = 4.0;
53 elevator_params->operating_voltage = 12.0;
54 elevator_params->zeroing_profile_params = {0.1, 1.0};
55 elevator_params->default_profile_params = {4.0, 3.0};
56 elevator_params->range = Values::kElevatorRange();
57 elevator_params->make_integral_loop =
58 &control_loops::superstructure::elevator::MakeIntegralElevatorLoop;
59 elevator_params->zeroing_constants.average_filter_size =
60 Values::kZeroingSampleSize;
61 elevator_params->zeroing_constants.one_revolution_distance =
Alex Perry5fb5ff22019-02-09 21:53:17 -080062 M_PI * 2.0 * constants::Values::kElevatorEncoderRatio();
Theo Bafrali00e42272019-02-12 01:07:46 -080063 elevator_params->zeroing_constants.zeroing_threshold = 0.005;
64 elevator_params->zeroing_constants.moving_buffer_size = 20;
65 elevator_params->zeroing_constants.allowable_encoder_error = 0.9;
Alex Perry5fb5ff22019-02-09 21:53:17 -080066
Theo Bafrali00e42272019-02-12 01:07:46 -080067 // Wrist constants.
68 wrist_params->zeroing_voltage = 4.0;
69 wrist_params->operating_voltage = 12.0;
70 wrist_params->zeroing_profile_params = {0.5, 2.0};
71 wrist_params->default_profile_params = {6.0, 5.0};
72 wrist_params->range = Values::kWristRange();
73 wrist_params->make_integral_loop =
74 &control_loops::superstructure::wrist::MakeIntegralWristLoop;
75 wrist_params->zeroing_constants.average_filter_size =
76 Values::kZeroingSampleSize;
77 wrist_params->zeroing_constants.one_revolution_distance =
Alex Perry5fb5ff22019-02-09 21:53:17 -080078 M_PI * 2.0 * constants::Values::kWristEncoderRatio();
Theo Bafrali00e42272019-02-12 01:07:46 -080079 wrist_params->zeroing_constants.zeroing_threshold = 0.0005;
80 wrist_params->zeroing_constants.moving_buffer_size = 20;
81 wrist_params->zeroing_constants.allowable_encoder_error = 0.9;
82
83 // Intake constants.
84 intake->zeroing_voltage = 4.0;
85 intake->operating_voltage = 12.0;
86 intake->zeroing_profile_params = {0.5, 3.0};
87 intake->default_profile_params = {6.0, 5.0};
88 intake->range = Values::kIntakeRange();
89 intake->make_integral_loop =
90 control_loops::superstructure::intake::MakeIntegralIntakeLoop;
91 intake->zeroing_constants.average_filter_size = Values::kZeroingSampleSize;
92 intake->zeroing_constants.one_revolution_distance =
93 M_PI * 2.0 * constants::Values::kIntakeEncoderRatio();
94 intake->zeroing_constants.zeroing_threshold = 0.0005;
95 intake->zeroing_constants.moving_buffer_size = 20;
96 intake->zeroing_constants.allowable_encoder_error = 0.9;
97
98 // Stilts constants.
99 stilts_params->zeroing_voltage = 4.0;
100 stilts_params->operating_voltage = 12.0;
101 stilts_params->zeroing_profile_params = {0.1, 3.0};
102 stilts_params->default_profile_params = {2.0, 4.0};
103 stilts_params->range = Values::kStiltsRange();
104 stilts_params->make_integral_loop =
105 &control_loops::superstructure::stilts::MakeIntegralStiltsLoop;
106 stilts_params->zeroing_constants.average_filter_size =
107 Values::kZeroingSampleSize;
108 stilts_params->zeroing_constants.one_revolution_distance =
109 M_PI * 2.0 * constants::Values::kStiltsEncoderRatio();
110 stilts_params->zeroing_constants.zeroing_threshold = 0.0005;
111 stilts_params->zeroing_constants.moving_buffer_size = 20;
112 stilts_params->zeroing_constants.allowable_encoder_error = 0.9;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800113
114 switch (team) {
115 // A set of constants for tests.
116 case 1:
Theo Bafrali00e42272019-02-12 01:07:46 -0800117 elevator_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800118 elevator->potentiometer_offset = 0.0;
119
Theo Bafrali00e42272019-02-12 01:07:46 -0800120 intake->zeroing_constants.measured_absolute_position = 0.0;
121 intake->zeroing_constants.middle_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800122
Theo Bafrali00e42272019-02-12 01:07:46 -0800123 wrist_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800124 wrist->potentiometer_offset = 0.0;
Theo Bafrali00e42272019-02-12 01:07:46 -0800125
126 stilts_params->zeroing_constants.measured_absolute_position = 0.0;
127 stilts->potentiometer_offset = 0.0;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800128 break;
129
130 case kCompTeamNumber:
Theo Bafrali00e42272019-02-12 01:07:46 -0800131 elevator_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800132 elevator->potentiometer_offset = 0.0;
133
Theo Bafrali00e42272019-02-12 01:07:46 -0800134 intake->zeroing_constants.measured_absolute_position = 0.0;
135 intake->zeroing_constants.middle_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800136
Theo Bafrali00e42272019-02-12 01:07:46 -0800137 wrist_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800138 wrist->potentiometer_offset = 0.0;
Theo Bafrali00e42272019-02-12 01:07:46 -0800139
140 stilts_params->zeroing_constants.measured_absolute_position = 0.0;
141 stilts->potentiometer_offset = 0.0;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800142 break;
143
144 case kPracticeTeamNumber:
Theo Bafrali00e42272019-02-12 01:07:46 -0800145 elevator_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800146 elevator->potentiometer_offset = 0.0;
147
Theo Bafrali00e42272019-02-12 01:07:46 -0800148 intake->zeroing_constants.measured_absolute_position = 0.0;
149 intake->zeroing_constants.middle_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800150
Theo Bafrali00e42272019-02-12 01:07:46 -0800151 wrist_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800152 wrist->potentiometer_offset = 0.0;
Theo Bafrali00e42272019-02-12 01:07:46 -0800153
154 stilts_params->zeroing_constants.measured_absolute_position = 0.0;
155 stilts->potentiometer_offset = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800156 break;
157
158 case kCodingRobotTeamNumber:
Theo Bafrali00e42272019-02-12 01:07:46 -0800159 elevator_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800160 elevator->potentiometer_offset = 0.0;
161
Theo Bafrali00e42272019-02-12 01:07:46 -0800162 intake->zeroing_constants.measured_absolute_position = 0.0;
163 intake->zeroing_constants.middle_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800164
Theo Bafrali00e42272019-02-12 01:07:46 -0800165 wrist_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800166 wrist->potentiometer_offset = 0.0;
Theo Bafrali00e42272019-02-12 01:07:46 -0800167
168 stilts_params->zeroing_constants.measured_absolute_position = 0.0;
169 stilts->potentiometer_offset = 0.0;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800170 break;
171
172 default:
173 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
174 }
175
176 return r;
177}
178
179const Values *DoGetValues() {
180 uint16_t team = ::aos::network::GetTeamNumber();
181 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
182 return DoGetValuesForTeam(team);
183}
184
185} // namespace
186
187const Values &GetValues() {
188 static ::aos::Once<const Values> once(DoGetValues);
189 return *once.Get();
190}
191
192const Values &GetValuesForTeam(uint16_t team_number) {
193 static ::aos::Mutex mutex;
194 ::aos::MutexLocker locker(&mutex);
195
196 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
197 // race conditions.
198 static ::std::map<uint16_t, const Values *> values;
199
200 if (values.count(team_number) == 0) {
201 values[team_number] = DoGetValuesForTeam(team_number);
202#if __has_feature(address_sanitizer)
203 __lsan_ignore_object(values[team_number]);
204#endif
205 }
206 return *values[team_number];
207}
208
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800209} // namespace constants
210} // namespace y2019