blob: 1a2924df3c53642df215cf2bc29ffc79b7860212 [file] [log] [blame]
Tyler Chatow6107aba2017-01-22 01:39:40 +00001#include "y2017/constants.h"
2
Ed Jordan8683f432017-02-12 00:13:26 +00003#include <inttypes.h>
Tyler Chatow6107aba2017-01-22 01:39:40 +00004#include <math.h>
5#include <stdint.h>
Tyler Chatow6107aba2017-01-22 01:39:40 +00006
7#include <map>
8
9#if __has_feature(address_sanitizer)
10#include "sanitizer/lsan_interface.h"
11#endif
12
John Park33858a32018-09-28 23:05:48 -070013#include "aos/logging/logging.h"
14#include "aos/mutex/mutex.h"
15#include "aos/network/team_number.h"
Sabina Davis2ed5ea22017-09-26 22:27:42 -070016#include "aos/once.h"
John Parkbb458cd2019-11-03 19:18:43 -080017#include "absl/base/call_once.h"
Ed Jordan8683f432017-02-12 00:13:26 +000018
19#include "y2017/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
20#include "y2017/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Tyler Chatow6107aba2017-01-22 01:39:40 +000021
22#ifndef M_PI
23#define M_PI 3.14159265358979323846
24#endif
25
26namespace y2017 {
27namespace constants {
28
Tyler Chatow6107aba2017-01-22 01:39:40 +000029const int Values::kZeroingSampleSize;
30
Brian Silverman052e69d2017-02-12 16:19:55 -080031constexpr double Values::kDrivetrainCyclesPerRevolution,
32 Values::kDrivetrainEncoderCountsPerRevolution,
33 Values::kDrivetrainEncoderRatio,
34 Values::kMaxDrivetrainEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080035
Brian Silverman052e69d2017-02-12 16:19:55 -080036constexpr double Values::kShooterEncoderCountsPerRevolution,
37 Values::kShooterEncoderRatio, Values::kMaxShooterEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080038
Brian Silverman052e69d2017-02-12 16:19:55 -080039constexpr double Values::kIntakeEncoderCountsPerRevolution,
40 Values::kIntakeEncoderRatio, Values::kIntakePotRatio,
41 Values::kIntakeEncoderIndexDifference,
42 Values::kMaxIntakeEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080043constexpr ::frc971::constants::Range Values::kIntakeRange;
44
Brian Silverman052e69d2017-02-12 16:19:55 -080045constexpr double Values::kHoodEncoderCountsPerRevolution,
Brian Silverman7cce2d32017-02-19 21:48:48 -080046 Values::kHoodEncoderRatio, Values::kHoodEncoderIndexDifference,
47 Values::kMaxHoodEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080048constexpr ::frc971::constants::Range Values::kHoodRange;
49
Brian Silverman052e69d2017-02-12 16:19:55 -080050constexpr double Values::kTurretEncoderCountsPerRevolution,
Brianef030df2017-03-05 15:06:04 -080051 Values::kTurretEncoderRatio, Values::kMaxTurretEncoderPulsesPerSecond;
Austin Schuhd5ccb862017-03-11 22:06:36 -080052constexpr ::frc971::constants::Range Values::kTurretRange;
Brian Silvermandb8498a2017-02-11 17:16:09 -080053
Brianef030df2017-03-05 15:06:04 -080054constexpr double Values::kIndexerEncoderCountsPerRevolution,
Brian Silverman052e69d2017-02-12 16:19:55 -080055 Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference,
56 Values::kMaxIndexerEncoderPulsesPerSecond;
Tyler Chatow6107aba2017-01-22 01:39:40 +000057
58namespace {
Brian Silvermandb8498a2017-02-11 17:16:09 -080059
Tyler Chatow6107aba2017-01-22 01:39:40 +000060const uint16_t kCompTeamNumber = 971;
61const uint16_t kPracticeTeamNumber = 9971;
62
Tyler Chatow6107aba2017-01-22 01:39:40 +000063const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080064 Values *const r = new Values();
65 Values::Intake *const intake = &r->intake;
Brian Silvermandb8498a2017-02-11 17:16:09 -080066 Values::Hood *const hood = &r->hood;
Austin Schuh55934032017-03-11 12:45:27 -080067 Values::Column *const column = &r->column;
Brian Silvermandb8498a2017-02-11 17:16:09 -080068
69 r->drivetrain_max_speed = 5;
70
71 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
Adam Snaider79900c22017-02-08 20:23:15 -080072 intake->zeroing.one_revolution_distance = Values::kIntakeEncoderIndexDifference;
Austin Schuh0fc1e6d2017-02-21 02:04:10 -080073 intake->zeroing.zeroing_threshold = 0.0005;
74 intake->zeroing.moving_buffer_size = 20;
Austin Schuh53a2c452017-03-22 21:18:20 -070075 intake->zeroing.allowable_encoder_error = 1.9;
Brian Silvermandb8498a2017-02-11 17:16:09 -080076
Austin Schuhd5ccb862017-03-11 22:06:36 -080077 column->turret_zeroed_distance = M_PI / 2.0;
Austin Schuh55934032017-03-11 12:45:27 -080078 column->indexer_zeroing.index_difference = 2.0 * M_PI;
Austin Schuhd5ccb862017-03-11 22:06:36 -080079 column->indexer_zeroing.hall_trigger_zeroing_length = 2;
80 column->indexer_zeroing.zeroing_move_direction = true;
Austin Schuh55934032017-03-11 12:45:27 -080081 column->turret_zeroing.index_difference = 2.0 * M_PI;
Austin Schuhd5ccb862017-03-11 22:06:36 -080082 column->turret_zeroing.hall_trigger_zeroing_length = 2;
83 column->turret_zeroing.zeroing_move_direction = false;
Austin Schuh55934032017-03-11 12:45:27 -080084
Austin Schuh6a90cd92017-02-19 20:55:33 -080085 hood->zeroing.index_pulse_count = 2;
Brian Silvermandb8498a2017-02-11 17:16:09 -080086 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
Austin Schuh6a90cd92017-02-19 20:55:33 -080087 hood->zeroing.known_index_pulse = 0;
Philipp Schrader3f5b6182017-03-25 22:36:37 +000088 hood->zeroing.allowable_encoder_error = 0.05;
Brian Silvermandb8498a2017-02-11 17:16:09 -080089
Tyler Chatow6107aba2017-01-22 01:39:40 +000090 switch (team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080091 // A set of constants for tests.
92 case 1:
93 intake->pot_offset = 0;
Austin Schuh6a90cd92017-02-19 20:55:33 -080094 intake->zeroing.measured_absolute_position = 0;
95
Austin Schuhd5ccb862017-03-11 22:06:36 -080096 // TODO(austin): Swap the turret and indexer limits and make sure the
97 // tests still pass.
98 column->indexer_zeroing.lower_hall_position = 0.1;
99 column->indexer_zeroing.upper_hall_position = 0.2;
Austin Schuh55934032017-03-11 12:45:27 -0800100
Austin Schuhd5ccb862017-03-11 22:06:36 -0800101 column->turret_zeroing.lower_hall_position = 2;
102 column->turret_zeroing.upper_hall_position = 2.1;
Austin Schuh55934032017-03-11 12:45:27 -0800103
Brian Silvermandb8498a2017-02-11 17:16:09 -0800104 hood->pot_offset = 0.1;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800105 hood->zeroing.measured_index_position = 0.05;
106
Brian Silvermandb8498a2017-02-11 17:16:09 -0800107 r->down_error = 0;
108 r->vision_name = "test";
Austin Schuheb5c22e2017-04-09 18:30:28 -0700109 r->vision_error = -0.030;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000110 break;
111
112 case kCompTeamNumber:
Austin Schuh9ef80a92017-06-24 13:34:28 -0700113 intake->pot_offset = 0.270738;
114 intake->zeroing.measured_absolute_position = 0.015669;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800115
Austin Schuh53a2c452017-03-22 21:18:20 -0700116 column->indexer_zeroing.lower_hall_position = 5.201948;
117 column->indexer_zeroing.upper_hall_position = 5.508744;
Austin Schuh55934032017-03-11 12:45:27 -0800118
Austin Schuh53a2c452017-03-22 21:18:20 -0700119 column->turret_zeroing.lower_hall_position = -4.861087;
120 column->turret_zeroing.upper_hall_position = -4.680861;
Austin Schuh55934032017-03-11 12:45:27 -0800121
Austin Schuh9ef80a92017-06-24 13:34:28 -0700122 // Original hood calibration
123 // hood->zeroing.measured_index_position = 0.234766;
124 hood->zeroing.measured_index_position = 0.124275;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800125
126 r->down_error = 0;
127 r->vision_name = "competition";
Austin Schuh9ef80a92017-06-24 13:34:28 -0700128 r->vision_error = 0.0;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800129 break;
130
131 case kPracticeTeamNumber:
Austin Schuhc74e9b62017-04-09 18:27:40 -0700132 intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602 + 0.010722 +
133 0.012880 - 0.01743;
134 intake->zeroing.measured_absolute_position = 0.043179;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800135
Austin Schuhc74e9b62017-04-09 18:27:40 -0700136 column->indexer_zeroing.lower_hall_position = 2.594181;
137 column->indexer_zeroing.upper_hall_position = 2.886952;
Austin Schuh55934032017-03-11 12:45:27 -0800138
Austin Schuhc74e9b62017-04-09 18:27:40 -0700139 column->turret_zeroing.lower_hall_position = -4.918530;
140 column->turret_zeroing.upper_hall_position = -4.720353;
Austin Schuh55934032017-03-11 12:45:27 -0800141
Austin Schuhc74e9b62017-04-09 18:27:40 -0700142 hood->zeroing.measured_index_position = 0.124275;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800143
Brian Silvermandb8498a2017-02-11 17:16:09 -0800144 r->down_error = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -0800145 r->vision_name = "practice";
Austin Schuh25db1262017-04-05 19:39:55 -0700146 r->vision_error = 0.0;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000147 break;
148
149 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -0700150 AOS_LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
Tyler Chatow6107aba2017-01-22 01:39:40 +0000151 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800152
153 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000154}
155
John Parkbb458cd2019-11-03 19:18:43 -0800156void DoGetValues(const Values** result) {
Tyler Chatow6107aba2017-01-22 01:39:40 +0000157 uint16_t team = ::aos::network::GetTeamNumber();
Austin Schuhf257f3c2019-10-27 21:00:43 -0700158 AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
John Parkbb458cd2019-11-03 19:18:43 -0800159 *result = DoGetValuesForTeam(team);
Tyler Chatow6107aba2017-01-22 01:39:40 +0000160}
161
162} // namespace
163
164const Values &GetValues() {
John Parkbb458cd2019-11-03 19:18:43 -0800165 static absl::once_flag once;
166 static const Values* result;
167 absl::call_once(once, DoGetValues, &result);
168 return *result;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000169}
170
171const Values &GetValuesForTeam(uint16_t team_number) {
172 static ::aos::Mutex mutex;
173 ::aos::MutexLocker locker(&mutex);
174
175 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
176 // race conditions.
177 static ::std::map<uint16_t, const Values *> values;
178
179 if (values.count(team_number) == 0) {
180 values[team_number] = DoGetValuesForTeam(team_number);
181#if __has_feature(address_sanitizer)
182 __lsan_ignore_object(values[team_number]);
183#endif
184 }
185 return *values[team_number];
186}
187
Parker Schuh94d56792017-04-13 20:32:50 -0700188Values::ShotParams Values::ShotParams::BlendY(double coefficient, Values::ShotParams a1, Values::ShotParams a2) {
189 using ::frc971::shooter_interpolation::Blend;
190 return Values::ShotParams{Blend(coefficient, a1.angle, a2.angle),
191 Blend(coefficient, a1.power, a2.power),
192 Blend(coefficient, a1.indexer_velocity, a2.indexer_velocity)};
193}
194
Tyler Chatow6107aba2017-01-22 01:39:40 +0000195} // namespace constants
196} // namespace y2017