blob: 536a072028cf4997b322605dc71bb73b11a24e7d [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
13#include "aos/common/logging/logging.h"
Tyler Chatow6107aba2017-01-22 01:39:40 +000014#include "aos/common/mutex.h"
Ed Jordan8683f432017-02-12 00:13:26 +000015#include "aos/common/network/team_number.h"
16#include "aos/common/once.h"
17
18#include "y2017/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
19#include "y2017/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Tyler Chatow6107aba2017-01-22 01:39:40 +000020
21#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
25namespace y2017 {
26namespace constants {
27
Tyler Chatow6107aba2017-01-22 01:39:40 +000028const int Values::kZeroingSampleSize;
29
Brian Silverman052e69d2017-02-12 16:19:55 -080030constexpr double Values::kDrivetrainCyclesPerRevolution,
31 Values::kDrivetrainEncoderCountsPerRevolution,
32 Values::kDrivetrainEncoderRatio,
33 Values::kMaxDrivetrainEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080034
Brian Silverman052e69d2017-02-12 16:19:55 -080035constexpr double Values::kShooterEncoderCountsPerRevolution,
36 Values::kShooterEncoderRatio, Values::kMaxShooterEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080037
Brian Silverman052e69d2017-02-12 16:19:55 -080038constexpr double Values::kIntakeEncoderCountsPerRevolution,
39 Values::kIntakeEncoderRatio, Values::kIntakePotRatio,
40 Values::kIntakeEncoderIndexDifference,
41 Values::kMaxIntakeEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080042constexpr ::frc971::constants::Range Values::kIntakeRange;
43
Brian Silverman052e69d2017-02-12 16:19:55 -080044constexpr double Values::kHoodEncoderCountsPerRevolution,
Brian Silverman7cce2d32017-02-19 21:48:48 -080045 Values::kHoodEncoderRatio, Values::kHoodEncoderIndexDifference,
46 Values::kMaxHoodEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080047constexpr ::frc971::constants::Range Values::kHoodRange;
48
Brian Silverman052e69d2017-02-12 16:19:55 -080049constexpr double Values::kTurretEncoderCountsPerRevolution,
Brianef030df2017-03-05 15:06:04 -080050 Values::kTurretEncoderRatio, Values::kMaxTurretEncoderPulsesPerSecond;
Austin Schuhd5ccb862017-03-11 22:06:36 -080051constexpr ::frc971::constants::Range Values::kTurretRange;
Brian Silvermandb8498a2017-02-11 17:16:09 -080052
Brianef030df2017-03-05 15:06:04 -080053constexpr double Values::kIndexerEncoderCountsPerRevolution,
Brian Silverman052e69d2017-02-12 16:19:55 -080054 Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference,
55 Values::kMaxIndexerEncoderPulsesPerSecond;
Tyler Chatow6107aba2017-01-22 01:39:40 +000056
57namespace {
Brian Silvermandb8498a2017-02-11 17:16:09 -080058
Tyler Chatow6107aba2017-01-22 01:39:40 +000059const uint16_t kCompTeamNumber = 971;
60const uint16_t kPracticeTeamNumber = 9971;
61
Tyler Chatow6107aba2017-01-22 01:39:40 +000062const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080063 Values *const r = new Values();
64 Values::Intake *const intake = &r->intake;
Brian Silvermandb8498a2017-02-11 17:16:09 -080065 Values::Hood *const hood = &r->hood;
Austin Schuh55934032017-03-11 12:45:27 -080066 Values::Column *const column = &r->column;
Brian Silvermandb8498a2017-02-11 17:16:09 -080067
68 r->drivetrain_max_speed = 5;
69
70 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
Adam Snaider79900c22017-02-08 20:23:15 -080071 intake->zeroing.one_revolution_distance = Values::kIntakeEncoderIndexDifference;
Austin Schuh0fc1e6d2017-02-21 02:04:10 -080072 intake->zeroing.zeroing_threshold = 0.0005;
73 intake->zeroing.moving_buffer_size = 20;
Austin Schuh53a2c452017-03-22 21:18:20 -070074 intake->zeroing.allowable_encoder_error = 1.9;
Brian Silvermandb8498a2017-02-11 17:16:09 -080075
Austin Schuhd5ccb862017-03-11 22:06:36 -080076 column->turret_zeroed_distance = M_PI / 2.0;
Austin Schuh55934032017-03-11 12:45:27 -080077 column->indexer_zeroing.index_difference = 2.0 * M_PI;
Austin Schuhd5ccb862017-03-11 22:06:36 -080078 column->indexer_zeroing.hall_trigger_zeroing_length = 2;
79 column->indexer_zeroing.zeroing_move_direction = true;
Austin Schuh55934032017-03-11 12:45:27 -080080 column->turret_zeroing.index_difference = 2.0 * M_PI;
Austin Schuhd5ccb862017-03-11 22:06:36 -080081 column->turret_zeroing.hall_trigger_zeroing_length = 2;
82 column->turret_zeroing.zeroing_move_direction = false;
Austin Schuh55934032017-03-11 12:45:27 -080083
Austin Schuh6a90cd92017-02-19 20:55:33 -080084 hood->zeroing.index_pulse_count = 2;
Brian Silvermandb8498a2017-02-11 17:16:09 -080085 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
Austin Schuh6a90cd92017-02-19 20:55:33 -080086 hood->zeroing.known_index_pulse = 0;
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 Schuh25db1262017-04-05 19:39:55 -0700107 r->vision_error = 0.0;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000108 break;
109
110 case kCompTeamNumber:
Austin Schuh52f80532017-04-05 19:22:30 -0700111 intake->pot_offset = 0.26712 + 0.0035 + 0.033 + 0.0011 -0.046872;
112 intake->zeroing.measured_absolute_position = 0.003397;
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 Schuh52f80532017-04-05 19:22:30 -0700120 hood->zeroing.measured_index_position = 0.234766;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800121
122 r->down_error = 0;
123 r->vision_name = "competition";
Austin Schuh25db1262017-04-05 19:39:55 -0700124 r->vision_error = 0.015;
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800125 break;
126
127 case kPracticeTeamNumber:
Austin Schuhd5ccb862017-03-11 22:06:36 -0800128 intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602 + 0.010722;
Austin Schuh53a2c452017-03-22 21:18:20 -0700129 intake->zeroing.measured_absolute_position = 0.044490;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800130
Austin Schuhd5ccb862017-03-11 22:06:36 -0800131 column->indexer_zeroing.lower_hall_position = 5.014364;
132 column->indexer_zeroing.upper_hall_position = 5.292234;
Austin Schuh55934032017-03-11 12:45:27 -0800133
Austin Schuhd5ccb862017-03-11 22:06:36 -0800134 column->turret_zeroing.lower_hall_position = -4.838110;
135 column->turret_zeroing.upper_hall_position = -4.655730;
Austin Schuh55934032017-03-11 12:45:27 -0800136
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800137 hood->zeroing.measured_index_position = 0.655432 - 0.460505;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800138
Brian Silvermandb8498a2017-02-11 17:16:09 -0800139 r->down_error = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -0800140 r->vision_name = "practice";
Austin Schuh25db1262017-04-05 19:39:55 -0700141 r->vision_error = 0.0;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000142 break;
143
144 default:
145 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
146 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800147
148 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000149}
150
151const Values *DoGetValues() {
152 uint16_t team = ::aos::network::GetTeamNumber();
153 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
154 return DoGetValuesForTeam(team);
155}
156
157} // namespace
158
159const Values &GetValues() {
160 static ::aos::Once<const Values> once(DoGetValues);
161 return *once.Get();
162}
163
164const Values &GetValuesForTeam(uint16_t team_number) {
165 static ::aos::Mutex mutex;
166 ::aos::MutexLocker locker(&mutex);
167
168 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
169 // race conditions.
170 static ::std::map<uint16_t, const Values *> values;
171
172 if (values.count(team_number) == 0) {
173 values[team_number] = DoGetValuesForTeam(team_number);
174#if __has_feature(address_sanitizer)
175 __lsan_ignore_object(values[team_number]);
176#endif
177 }
178 return *values[team_number];
179}
180
181} // namespace constants
182} // namespace y2017