blob: b9361c7f1ba72c3507bce1f7408ccfb160c8300a [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;
Brian Silvermandb8498a2017-02-11 17:16:09 -080051
Brianef030df2017-03-05 15:06:04 -080052constexpr double Values::kIndexerEncoderCountsPerRevolution,
Brian Silverman052e69d2017-02-12 16:19:55 -080053 Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference,
54 Values::kMaxIndexerEncoderPulsesPerSecond;
Tyler Chatow6107aba2017-01-22 01:39:40 +000055
56namespace {
Brian Silvermandb8498a2017-02-11 17:16:09 -080057
Tyler Chatow6107aba2017-01-22 01:39:40 +000058const uint16_t kCompTeamNumber = 971;
59const uint16_t kPracticeTeamNumber = 9971;
60
Tyler Chatow6107aba2017-01-22 01:39:40 +000061const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080062 Values *const r = new Values();
63 Values::Intake *const intake = &r->intake;
Brian Silvermandb8498a2017-02-11 17:16:09 -080064 Values::Hood *const hood = &r->hood;
Austin Schuh55934032017-03-11 12:45:27 -080065 Values::Column *const column = &r->column;
Brian Silvermandb8498a2017-02-11 17:16:09 -080066
67 r->drivetrain_max_speed = 5;
68
69 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
Adam Snaider79900c22017-02-08 20:23:15 -080070 intake->zeroing.one_revolution_distance = Values::kIntakeEncoderIndexDifference;
Austin Schuh0fc1e6d2017-02-21 02:04:10 -080071 intake->zeroing.zeroing_threshold = 0.0005;
72 intake->zeroing.moving_buffer_size = 20;
Brian Silvermana10d20a2017-02-19 14:28:53 -080073 intake->zeroing.allowable_encoder_error = 0.3;
Brian Silvermandb8498a2017-02-11 17:16:09 -080074
Austin Schuh55934032017-03-11 12:45:27 -080075 column->indexer_zeroing.index_difference = 2.0 * M_PI;
76 column->turret_zeroing.index_difference = 2.0 * M_PI;
77
Austin Schuh6a90cd92017-02-19 20:55:33 -080078 hood->zeroing.index_pulse_count = 2;
Brian Silvermandb8498a2017-02-11 17:16:09 -080079 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
Austin Schuh6a90cd92017-02-19 20:55:33 -080080 hood->zeroing.known_index_pulse = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -080081
Tyler Chatow6107aba2017-01-22 01:39:40 +000082 switch (team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080083 // A set of constants for tests.
84 case 1:
85 intake->pot_offset = 0;
Austin Schuh6a90cd92017-02-19 20:55:33 -080086 intake->zeroing.measured_absolute_position = 0;
87
Austin Schuh55934032017-03-11 12:45:27 -080088 column->indexer_zeroing.lower_hall_position = 2.0;
89 column->indexer_zeroing.upper_hall_position = 2.1;
90
91 column->turret_zeroing.lower_hall_position = 0;
92 column->turret_zeroing.upper_hall_position = 0.1;
93
Brian Silvermandb8498a2017-02-11 17:16:09 -080094 hood->pot_offset = 0.1;
Austin Schuh6a90cd92017-02-19 20:55:33 -080095 hood->zeroing.measured_index_position = 0.05;
96
Brian Silvermandb8498a2017-02-11 17:16:09 -080097 r->down_error = 0;
98 r->vision_name = "test";
Tyler Chatow6107aba2017-01-22 01:39:40 +000099 break;
100
101 case kCompTeamNumber:
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800102 intake->pot_offset = 0.26712;
103 intake->zeroing.measured_absolute_position = 0.008913;
104
Austin Schuh55934032017-03-11 12:45:27 -0800105 column->indexer_zeroing.lower_hall_position = 2.0;
106 column->indexer_zeroing.upper_hall_position = 2.1;
107
108 column->turret_zeroing.lower_hall_position = 0;
109 column->turret_zeroing.upper_hall_position = 0.1;
110
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800111 hood->zeroing.measured_index_position = 0.652898 - 0.488117;
112
113 r->down_error = 0;
114 r->vision_name = "competition";
115 break;
116
117 case kPracticeTeamNumber:
Austin Schuhe6baace2017-03-04 15:05:02 -0800118 intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602;
119 intake->zeroing.measured_absolute_position = 0.031437;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800120
Austin Schuh55934032017-03-11 12:45:27 -0800121 column->indexer_zeroing.lower_hall_position = 2.0;
122 column->indexer_zeroing.upper_hall_position = 2.1;
123
124 column->turret_zeroing.lower_hall_position = 0;
125 column->turret_zeroing.upper_hall_position = 0.1;
126
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800127 hood->zeroing.measured_index_position = 0.655432 - 0.460505;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800128
Brian Silvermandb8498a2017-02-11 17:16:09 -0800129 r->down_error = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -0800130 r->vision_name = "practice";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000131 break;
132
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800133
Tyler Chatow6107aba2017-01-22 01:39:40 +0000134 default:
135 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
136 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800137
138 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000139}
140
141const Values *DoGetValues() {
142 uint16_t team = ::aos::network::GetTeamNumber();
143 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
144 return DoGetValuesForTeam(team);
145}
146
147} // namespace
148
149const Values &GetValues() {
150 static ::aos::Once<const Values> once(DoGetValues);
151 return *once.Get();
152}
153
154const Values &GetValuesForTeam(uint16_t team_number) {
155 static ::aos::Mutex mutex;
156 ::aos::MutexLocker locker(&mutex);
157
158 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
159 // race conditions.
160 static ::std::map<uint16_t, const Values *> values;
161
162 if (values.count(team_number) == 0) {
163 values[team_number] = DoGetValuesForTeam(team_number);
164#if __has_feature(address_sanitizer)
165 __lsan_ignore_object(values[team_number]);
166#endif
167 }
168 return *values[team_number];
169}
170
171} // namespace constants
172} // namespace y2017