blob: 35fe2079f0cd25043960374b4299a24d6de441cb [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,
45 Values::kHoodEncoderRatio, Values::kHoodPotRatio,
46 Values::kHoodEncoderIndexDifference, 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;
74 intake->zeroing.measured_absolute_position = 0;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -080075 intake->zeroing.zeroing_threshold = 0.001;
76 intake->zeroing.moving_buffer_size = 9;
Brian Silvermandb8498a2017-02-11 17:16:09 -080077
78 turret->zeroing.average_filter_size = Values::kZeroingSampleSize;
Adam Snaider79900c22017-02-08 20:23:15 -080079 turret->zeroing.one_revolution_distance = Values::kTurretEncoderIndexDifference;
80 turret->zeroing.measured_absolute_position = 0;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -080081 turret->zeroing.zeroing_threshold = 0.001;
82 turret->zeroing.moving_buffer_size = 9;
Brian Silvermandb8498a2017-02-11 17:16:09 -080083
84 hood->zeroing.average_filter_size = Values::kZeroingSampleSize;
85 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
86 hood->zeroing.measured_index_position = 0.1;
87 hood->zeroing.allowable_encoder_error = 0.3;
88
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;
93 turret->pot_offset = 0;
94 hood->pot_offset = 0.1;
95 r->down_error = 0;
96 r->vision_name = "test";
Tyler Chatow6107aba2017-01-22 01:39:40 +000097 break;
98
99 case kCompTeamNumber:
Brian Silvermandb8498a2017-02-11 17:16:09 -0800100 intake->pot_offset = 0;
101 turret->pot_offset = 0;
102 hood->pot_offset = 0.1;
103 r->down_error = 0;
104 r->vision_name = "competition";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000105 break;
106
107 case kPracticeTeamNumber:
Brian Silvermandb8498a2017-02-11 17:16:09 -0800108 intake->pot_offset = 0;
109 turret->pot_offset = 0;
110 hood->pot_offset = 0.1;
111 r->down_error = 0;
112 r->vision_name = "practice";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000113 break;
114
115 default:
116 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
117 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800118
119 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000120}
121
122const Values *DoGetValues() {
123 uint16_t team = ::aos::network::GetTeamNumber();
124 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
125 return DoGetValuesForTeam(team);
126}
127
128} // namespace
129
130const Values &GetValues() {
131 static ::aos::Once<const Values> once(DoGetValues);
132 return *once.Get();
133}
134
135const Values &GetValuesForTeam(uint16_t team_number) {
136 static ::aos::Mutex mutex;
137 ::aos::MutexLocker locker(&mutex);
138
139 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
140 // race conditions.
141 static ::std::map<uint16_t, const Values *> values;
142
143 if (values.count(team_number) == 0) {
144 values[team_number] = DoGetValuesForTeam(team_number);
145#if __has_feature(address_sanitizer)
146 __lsan_ignore_object(values[team_number]);
147#endif
148 }
149 return *values[team_number];
150}
151
152} // namespace constants
153} // namespace y2017