blob: b53fb992245fa1ffe86aa8b19a8abdcf72d21a07 [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.
Austin Schuh7f87b472019-02-15 23:20:57 -080052 elevator_params->zeroing_voltage = 3.0;
Theo Bafrali00e42272019-02-12 01:07:46 -080053 elevator_params->operating_voltage = 12.0;
54 elevator_params->zeroing_profile_params = {0.1, 1.0};
Austin Schuh7f87b472019-02-15 23:20:57 -080055 elevator_params->default_profile_params = {4.0, 16.0};
Theo Bafrali00e42272019-02-12 01:07:46 -080056 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};
Austin Schuh7f87b472019-02-15 23:20:57 -080071 wrist_params->default_profile_params = {10.0, 40.0};
Theo Bafrali00e42272019-02-12 01:07:46 -080072 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.
Austin Schuh7f87b472019-02-15 23:20:57 -080084 intake->zeroing_voltage = 3.0;
Theo Bafrali00e42272019-02-12 01:07:46 -080085 intake->operating_voltage = 12.0;
86 intake->zeroing_profile_params = {0.5, 3.0};
Austin Schuh7f87b472019-02-15 23:20:57 -080087 intake->default_profile_params = {6.0, 30.0};
Theo Bafrali00e42272019-02-12 01:07:46 -080088 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.
Austin Schuh7f87b472019-02-15 23:20:57 -080099 stilts_params->zeroing_voltage = 3.0;
Theo Bafrali00e42272019-02-12 01:07:46 -0800100 stilts_params->operating_voltage = 12.0;
Austin Schuh7f87b472019-02-15 23:20:57 -0800101 stilts_params->zeroing_profile_params = {0.1, 0.5};
102 stilts_params->default_profile_params = {0.5, 0.5};
Theo Bafrali00e42272019-02-12 01:07:46 -0800103 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:
Austin Schuhed7f8632019-02-15 23:12:20 -0800145 elevator_params->zeroing_constants.measured_absolute_position = 0.049419;
146 elevator->potentiometer_offset = -0.022320;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800147
Austin Schuh862d4de2019-02-17 14:15:18 -0800148 intake->zeroing_constants.measured_absolute_position = 1.756847;
Austin Schuhed7f8632019-02-15 23:12:20 -0800149 intake->zeroing_constants.middle_position =
150 Values::kIntakeRange().middle();
Theo Bafrali00e42272019-02-12 01:07:46 -0800151
152 stilts_params->zeroing_constants.measured_absolute_position = 0.0;
153 stilts->potentiometer_offset = 0.0;
Austin Schuhed7f8632019-02-15 23:12:20 -0800154
155 wrist_params->zeroing_constants.measured_absolute_position = 0.357394;
156 wrist->potentiometer_offset = -1.479097 - 2.740303;
157
158 stilts_params->zeroing_constants.measured_absolute_position = 0.047838;
159 stilts->potentiometer_offset = -0.093820;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800160 break;
161
162 case kCodingRobotTeamNumber:
Theo Bafrali00e42272019-02-12 01:07:46 -0800163 elevator_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800164 elevator->potentiometer_offset = 0.0;
165
Theo Bafrali00e42272019-02-12 01:07:46 -0800166 intake->zeroing_constants.measured_absolute_position = 0.0;
167 intake->zeroing_constants.middle_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800168
Theo Bafrali00e42272019-02-12 01:07:46 -0800169 wrist_params->zeroing_constants.measured_absolute_position = 0.0;
Alex Perry5fb5ff22019-02-09 21:53:17 -0800170 wrist->potentiometer_offset = 0.0;
Theo Bafrali00e42272019-02-12 01:07:46 -0800171
172 stilts_params->zeroing_constants.measured_absolute_position = 0.0;
173 stilts->potentiometer_offset = 0.0;
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800174 break;
175
176 default:
177 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
178 }
179
180 return r;
181}
182
183const Values *DoGetValues() {
184 uint16_t team = ::aos::network::GetTeamNumber();
185 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
186 return DoGetValuesForTeam(team);
187}
188
189} // namespace
190
191const Values &GetValues() {
192 static ::aos::Once<const Values> once(DoGetValues);
193 return *once.Get();
194}
195
196const Values &GetValuesForTeam(uint16_t team_number) {
197 static ::aos::Mutex mutex;
198 ::aos::MutexLocker locker(&mutex);
199
200 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
201 // race conditions.
202 static ::std::map<uint16_t, const Values *> values;
203
204 if (values.count(team_number) == 0) {
205 values[team_number] = DoGetValuesForTeam(team_number);
206#if __has_feature(address_sanitizer)
207 __lsan_ignore_object(values[team_number]);
208#endif
209 }
210 return *values[team_number];
211}
212
Tyler Chatow37ecdcd2019-01-26 20:18:42 -0800213} // namespace constants
214} // namespace y2019