blob: 9822b0a9da08ff4adc6b2c14d60e9b0da77f658d [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,
50 Values::kTurretEncoderRatio, Values::kTurretPotRatio,
51 Values::kTurretEncoderIndexDifference,
52 Values::kMaxTurretEncoderPulsesPerSecond;
Brian Silvermandb8498a2017-02-11 17:16:09 -080053constexpr ::frc971::constants::Range Values::kTurretRange;
54
Brian Silverman052e69d2017-02-12 16:19:55 -080055constexpr double Values::kMaxIndexerEncoderCountsPerRevolution,
56 Values::kIndexerEncoderRatio, Values::kIndexerEncoderIndexDifference,
57 Values::kMaxIndexerEncoderPulsesPerSecond;
Tyler Chatow6107aba2017-01-22 01:39:40 +000058
59namespace {
Brian Silvermandb8498a2017-02-11 17:16:09 -080060
Tyler Chatow6107aba2017-01-22 01:39:40 +000061const uint16_t kCompTeamNumber = 971;
62const uint16_t kPracticeTeamNumber = 9971;
63
Tyler Chatow6107aba2017-01-22 01:39:40 +000064const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080065 Values *const r = new Values();
66 Values::Intake *const intake = &r->intake;
67 Values::Turret *const turret = &r->turret;
68 Values::Hood *const hood = &r->hood;
69
70 r->drivetrain_max_speed = 5;
71
72 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
Adam Snaider79900c22017-02-08 20:23:15 -080073 intake->zeroing.one_revolution_distance = Values::kIntakeEncoderIndexDifference;
Austin Schuh0fc1e6d2017-02-21 02:04:10 -080074 intake->zeroing.zeroing_threshold = 0.0005;
75 intake->zeroing.moving_buffer_size = 20;
Brian Silvermana10d20a2017-02-19 14:28:53 -080076 intake->zeroing.allowable_encoder_error = 0.3;
Brian Silvermandb8498a2017-02-11 17:16:09 -080077
78 turret->zeroing.average_filter_size = Values::kZeroingSampleSize;
Austin Schuh6a90cd92017-02-19 20:55:33 -080079 turret->zeroing.one_revolution_distance =
80 Values::kTurretEncoderIndexDifference;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -080081 turret->zeroing.zeroing_threshold = 0.001;
82 turret->zeroing.moving_buffer_size = 9;
Brian Silvermana10d20a2017-02-19 14:28:53 -080083 turret->zeroing.allowable_encoder_error = 0.3;
Brian Silvermandb8498a2017-02-11 17:16:09 -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;
Brian Silvermandb8498a2017-02-11 17:16:09 -080088
Tyler Chatow6107aba2017-01-22 01:39:40 +000089 switch (team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080090 // A set of constants for tests.
91 case 1:
92 intake->pot_offset = 0;
Austin Schuh6a90cd92017-02-19 20:55:33 -080093 intake->zeroing.measured_absolute_position = 0;
94
Brian Silvermandb8498a2017-02-11 17:16:09 -080095 turret->pot_offset = 0;
Austin Schuh6a90cd92017-02-19 20:55:33 -080096 turret->zeroing.measured_absolute_position = 0;
97
Brian Silvermandb8498a2017-02-11 17:16:09 -080098 hood->pot_offset = 0.1;
Austin Schuh6a90cd92017-02-19 20:55:33 -080099 hood->zeroing.measured_index_position = 0.05;
100
Brian Silvermandb8498a2017-02-11 17:16:09 -0800101 r->down_error = 0;
102 r->vision_name = "test";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000103 break;
104
105 case kCompTeamNumber:
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800106 intake->pot_offset = 0.26712;
107 intake->zeroing.measured_absolute_position = 0.008913;
108
109 turret->pot_offset = 0;
110 turret->zeroing.measured_absolute_position = 0;
111
112 hood->zeroing.measured_index_position = 0.652898 - 0.488117;
113
114 r->down_error = 0;
115 r->vision_name = "competition";
116 break;
117
118 case kPracticeTeamNumber:
Austin Schuhe6baace2017-03-04 15:05:02 -0800119 intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602;
120 intake->zeroing.measured_absolute_position = 0.031437;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800121
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800122 turret->pot_offset = -5.45 - 0.026111;
123 turret->zeroing.measured_absolute_position = 0.2429;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800124
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800125 hood->zeroing.measured_index_position = 0.655432 - 0.460505;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800126
Brian Silvermandb8498a2017-02-11 17:16:09 -0800127 r->down_error = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -0800128 r->vision_name = "practice";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000129 break;
130
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800131
Tyler Chatow6107aba2017-01-22 01:39:40 +0000132 default:
133 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
134 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800135
136 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000137}
138
139const Values *DoGetValues() {
140 uint16_t team = ::aos::network::GetTeamNumber();
141 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
142 return DoGetValuesForTeam(team);
143}
144
145} // namespace
146
147const Values &GetValues() {
148 static ::aos::Once<const Values> once(DoGetValues);
149 return *once.Get();
150}
151
152const Values &GetValuesForTeam(uint16_t team_number) {
153 static ::aos::Mutex mutex;
154 ::aos::MutexLocker locker(&mutex);
155
156 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
157 // race conditions.
158 static ::std::map<uint16_t, const Values *> values;
159
160 if (values.count(team_number) == 0) {
161 values[team_number] = DoGetValuesForTeam(team_number);
162#if __has_feature(address_sanitizer)
163 __lsan_ignore_object(values[team_number]);
164#endif
165 }
166 return *values[team_number];
167}
168
169} // namespace constants
170} // namespace y2017