blob: d599772e40b0fd22a73d8e23724cf2a8bfa81e8d [file] [log] [blame]
Tyler Chatow6107aba2017-01-22 01:39:40 +00001#include "y2017/constants.h"
2
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cinttypes>
4#include <cmath>
5#include <cstdint>
Tyler Chatow6107aba2017-01-22 01:39:40 +00006#include <map>
7
8#if __has_feature(address_sanitizer)
9#include "sanitizer/lsan_interface.h"
10#endif
11
John Parkbb458cd2019-11-03 19:18:43 -080012#include "absl/base/call_once.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070013#include "glog/logging.h"
14
Brian Silverman1463c092020-10-30 17:28:24 -070015#include "aos/network/team_number.h"
16#include "aos/stl_mutex/stl_mutex.h"
Ed Jordan8683f432017-02-12 00:13:26 +000017#include "y2017/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
18#include "y2017/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Tyler Chatow6107aba2017-01-22 01:39:40 +000019
20#ifndef M_PI
21#define M_PI 3.14159265358979323846
22#endif
23
Stephan Pleinesf63bde82024-01-13 15:59:33 -080024namespace y2017::constants {
Tyler Chatow6107aba2017-01-22 01:39:40 +000025
Tyler Chatow6107aba2017-01-22 01:39:40 +000026const int Values::kZeroingSampleSize;
27
Brian Silverman052e69d2017-02-12 16:19:55 -080028constexpr double Values::kDrivetrainCyclesPerRevolution,
29 Values::kDrivetrainEncoderCountsPerRevolution,
30 Values::kDrivetrainEncoderRatio,
31 Values::kMaxDrivetrainEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080032
Brian Silverman052e69d2017-02-12 16:19:55 -080033constexpr double Values::kShooterEncoderCountsPerRevolution,
34 Values::kShooterEncoderRatio, Values::kMaxShooterEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080035
Brian Silverman052e69d2017-02-12 16:19:55 -080036constexpr double Values::kIntakeEncoderCountsPerRevolution,
37 Values::kIntakeEncoderRatio, Values::kIntakePotRatio,
38 Values::kIntakeEncoderIndexDifference,
39 Values::kMaxIntakeEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080040constexpr ::frc971::constants::Range Values::kIntakeRange;
41
Brian Silverman052e69d2017-02-12 16:19:55 -080042constexpr double Values::kHoodEncoderCountsPerRevolution,
Brian Silverman7cce2d32017-02-19 21:48:48 -080043 Values::kHoodEncoderRatio, Values::kHoodEncoderIndexDifference,
44 Values::kMaxHoodEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080045constexpr ::frc971::constants::Range Values::kHoodRange;
46
Brian Silverman052e69d2017-02-12 16:19:55 -080047constexpr double Values::kTurretEncoderCountsPerRevolution,
Brianef030df2017-03-05 15:06:04 -080048 Values::kTurretEncoderRatio, Values::kMaxTurretEncoderPulsesPerSecond;
Austin Schuhd5ccb862017-03-11 22:06:36 -080049constexpr ::frc971::constants::Range Values::kTurretRange;
Brian Silvermandb8498a2017-02-11 17:16:09 -080050
Brianef030df2017-03-05 15:06:04 -080051constexpr double Values::kIndexerEncoderCountsPerRevolution,
Brian Silverman052e69d2017-02-12 16:19:55 -080052 Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference,
53 Values::kMaxIndexerEncoderPulsesPerSecond;
Tyler Chatow6107aba2017-01-22 01:39:40 +000054
55namespace {
Brian Silvermandb8498a2017-02-11 17:16:09 -080056
Tyler Chatow6107aba2017-01-22 01:39:40 +000057const uint16_t kCompTeamNumber = 971;
58const uint16_t kPracticeTeamNumber = 9971;
59
Tyler Chatow6107aba2017-01-22 01:39:40 +000060const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080061 Values *const r = new Values();
62 Values::Intake *const intake = &r->intake;
Brian Silvermandb8498a2017-02-11 17:16:09 -080063 Values::Hood *const hood = &r->hood;
Austin Schuh55934032017-03-11 12:45:27 -080064 Values::Column *const column = &r->column;
Brian Silvermandb8498a2017-02-11 17:16:09 -080065
66 r->drivetrain_max_speed = 5;
67
68 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
Brian Silverman1463c092020-10-30 17:28:24 -070069 intake->zeroing.one_revolution_distance =
70 Values::kIntakeEncoderIndexDifference;
Austin Schuh0fc1e6d2017-02-21 02:04:10 -080071 intake->zeroing.zeroing_threshold = 0.0005;
72 intake->zeroing.moving_buffer_size = 20;
Austin Schuh53a2c452017-03-22 21:18:20 -070073 intake->zeroing.allowable_encoder_error = 1.9;
Brian Silvermandb8498a2017-02-11 17:16:09 -080074
Austin Schuhd5ccb862017-03-11 22:06:36 -080075 column->turret_zeroed_distance = M_PI / 2.0;
Austin Schuh55934032017-03-11 12:45:27 -080076 column->indexer_zeroing.index_difference = 2.0 * M_PI;
Austin Schuhd5ccb862017-03-11 22:06:36 -080077 column->indexer_zeroing.hall_trigger_zeroing_length = 2;
78 column->indexer_zeroing.zeroing_move_direction = true;
Austin Schuh55934032017-03-11 12:45:27 -080079 column->turret_zeroing.index_difference = 2.0 * M_PI;
Austin Schuhd5ccb862017-03-11 22:06:36 -080080 column->turret_zeroing.hall_trigger_zeroing_length = 2;
81 column->turret_zeroing.zeroing_move_direction = false;
Austin Schuh55934032017-03-11 12:45:27 -080082
Austin Schuh6a90cd92017-02-19 20:55:33 -080083 hood->zeroing.index_pulse_count = 2;
Brian Silvermandb8498a2017-02-11 17:16:09 -080084 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
Austin Schuh6a90cd92017-02-19 20:55:33 -080085 hood->zeroing.known_index_pulse = 0;
Philipp Schrader3f5b6182017-03-25 22:36:37 +000086 hood->zeroing.allowable_encoder_error = 0.05;
Brian Silvermandb8498a2017-02-11 17:16:09 -080087
Tyler Chatow6107aba2017-01-22 01:39:40 +000088 switch (team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080089 // A set of constants for tests.
90 case 1:
91 intake->pot_offset = 0;
Austin Schuh6a90cd92017-02-19 20:55:33 -080092 intake->zeroing.measured_absolute_position = 0;
93
Austin Schuhd5ccb862017-03-11 22:06:36 -080094 // TODO(austin): Swap the turret and indexer limits and make sure the
95 // tests still pass.
96 column->indexer_zeroing.lower_hall_position = 0.1;
97 column->indexer_zeroing.upper_hall_position = 0.2;
Austin Schuh55934032017-03-11 12:45:27 -080098
Austin Schuhd5ccb862017-03-11 22:06:36 -080099 column->turret_zeroing.lower_hall_position = 2;
100 column->turret_zeroing.upper_hall_position = 2.1;
Austin Schuh55934032017-03-11 12:45:27 -0800101
Brian Silvermandb8498a2017-02-11 17:16:09 -0800102 hood->pot_offset = 0.1;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800103 hood->zeroing.measured_index_position = 0.05;
104
Brian Silvermandb8498a2017-02-11 17:16:09 -0800105 r->down_error = 0;
106 r->vision_name = "test";
Austin Schuheb5c22e2017-04-09 18:30:28 -0700107 r->vision_error = -0.030;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000108 break;
109
110 case kCompTeamNumber:
Austin Schuh9ef80a92017-06-24 13:34:28 -0700111 intake->pot_offset = 0.270738;
112 intake->zeroing.measured_absolute_position = 0.015669;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800113
Austin Schuh53a2c452017-03-22 21:18:20 -0700114 column->indexer_zeroing.lower_hall_position = 5.201948;
115 column->indexer_zeroing.upper_hall_position = 5.508744;
Austin Schuh55934032017-03-11 12:45:27 -0800116
Austin Schuh53a2c452017-03-22 21:18:20 -0700117 column->turret_zeroing.lower_hall_position = -4.861087;
118 column->turret_zeroing.upper_hall_position = -4.680861;
Austin Schuh55934032017-03-11 12:45:27 -0800119
Austin Schuh9ef80a92017-06-24 13:34:28 -0700120 // Original hood calibration
121 // hood->zeroing.measured_index_position = 0.234766;
122 hood->zeroing.measured_index_position = 0.124275;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800123
124 r->down_error = 0;
125 r->vision_name = "competition";
Austin Schuh9ef80a92017-06-24 13:34:28 -0700126 r->vision_error = 0.0;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800127 break;
128
129 case kPracticeTeamNumber:
Austin Schuhc74e9b62017-04-09 18:27:40 -0700130 intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602 + 0.010722 +
131 0.012880 - 0.01743;
132 intake->zeroing.measured_absolute_position = 0.043179;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800133
Austin Schuhc74e9b62017-04-09 18:27:40 -0700134 column->indexer_zeroing.lower_hall_position = 2.594181;
135 column->indexer_zeroing.upper_hall_position = 2.886952;
Austin Schuh55934032017-03-11 12:45:27 -0800136
Austin Schuhc74e9b62017-04-09 18:27:40 -0700137 column->turret_zeroing.lower_hall_position = -4.918530;
138 column->turret_zeroing.upper_hall_position = -4.720353;
Austin Schuh55934032017-03-11 12:45:27 -0800139
Austin Schuhc74e9b62017-04-09 18:27:40 -0700140 hood->zeroing.measured_index_position = 0.124275;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800141
Brian Silvermandb8498a2017-02-11 17:16:09 -0800142 r->down_error = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -0800143 r->vision_name = "practice";
Austin Schuh25db1262017-04-05 19:39:55 -0700144 r->vision_error = 0.0;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000145 break;
146
147 default:
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700148 LOG(FATAL) << "unknown team: " << team;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000149 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800150
151 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000152}
153
Brian Silverman1463c092020-10-30 17:28:24 -0700154void DoGetValues(const Values **result) {
Tyler Chatow6107aba2017-01-22 01:39:40 +0000155 uint16_t team = ::aos::network::GetTeamNumber();
Brian Silvermanf4d329c2021-11-04 19:32:10 -0700156 LOG(INFO) << "creating a Constants for team: " << team;
John Parkbb458cd2019-11-03 19:18:43 -0800157 *result = DoGetValuesForTeam(team);
Tyler Chatow6107aba2017-01-22 01:39:40 +0000158}
159
160} // namespace
161
162const Values &GetValues() {
John Parkbb458cd2019-11-03 19:18:43 -0800163 static absl::once_flag once;
Brian Silverman1463c092020-10-30 17:28:24 -0700164 static const Values *result;
John Parkbb458cd2019-11-03 19:18:43 -0800165 absl::call_once(once, DoGetValues, &result);
166 return *result;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000167}
168
169const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman1463c092020-10-30 17:28:24 -0700170 static aos::stl_mutex mutex;
171 std::unique_lock<aos::stl_mutex> locker(mutex);
Tyler Chatow6107aba2017-01-22 01:39:40 +0000172
173 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
174 // race conditions.
175 static ::std::map<uint16_t, const Values *> values;
176
177 if (values.count(team_number) == 0) {
178 values[team_number] = DoGetValuesForTeam(team_number);
179#if __has_feature(address_sanitizer)
180 __lsan_ignore_object(values[team_number]);
181#endif
182 }
183 return *values[team_number];
184}
185
Brian Silverman1463c092020-10-30 17:28:24 -0700186Values::ShotParams Values::ShotParams::BlendY(double coefficient,
187 Values::ShotParams a1,
188 Values::ShotParams a2) {
Parker Schuh94d56792017-04-13 20:32:50 -0700189 using ::frc971::shooter_interpolation::Blend;
Brian Silverman1463c092020-10-30 17:28:24 -0700190 return Values::ShotParams{
191 Blend(coefficient, a1.angle, a2.angle),
192 Blend(coefficient, a1.power, a2.power),
193 Blend(coefficient, a1.indexer_velocity, a2.indexer_velocity)};
Parker Schuh94d56792017-04-13 20:32:50 -0700194}
195
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800196} // namespace y2017::constants